def update_label(self, previous_settings): data = getattr(previous_settings, 'label', {}).copy() new = getattr(self, 'label', {}).copy() if not previous_settings: return # if nothing changed copy the settings from previous if not new and data: setattr(self, 'label', data) elif data != new: for k, v in new.items(): if v is not None: data[k] = v else: if k not in data: raise UnprocessableEntity('{} does not exist under {}'.format(k, 'label')) # noqa del data[k] setattr(self, 'label', data) diff = dict_diff(self.label, getattr(previous_settings, 'label', {})) added = ', '.join(list(map(lambda x: 'default' if x == '' else x, [k for k in diff.get('added', {})]))) # noqa added = 'added label ' + added if added else '' changed = ', '.join(list(map(lambda x: 'default' if x == '' else x, [k for k in diff.get('changed', {})]))) # noqa changed = 'changed label ' + changed if changed else '' deleted = ', '.join(list(map(lambda x: 'default' if x == '' else x, [k for k in diff.get('deleted', {})]))) # noqa deleted = 'deleted label ' + deleted if deleted else '' changes = ', '.join(i for i in (added, changed, deleted) if i) if changes: if self.summary: self.summary += ' and ' self.summary += ["{} {}".format(self.owner, changes)]
def _update_autoscale(self, previous_settings): data = getattr(previous_settings, 'autoscale', {}).copy() new = getattr(self, 'autoscale', {}).copy() # If no previous settings then do nothing if not previous_settings: return # if nothing changed copy the settings from previous if not new and data: setattr(self, 'autoscale', data) elif data != new: for proc, scale in new.items(): if scale is None: # error if unsetting non-existing key if proc not in data: raise UnprocessableEntity( '{} does not exist under {}'.format( proc, 'autoscale')) # noqa del data[proc] else: data[proc] = scale setattr(self, 'autoscale', data) # only apply new items for proc, scale in new.items(): self.app.autoscale(proc, scale) # if the autoscale information changed, log the dict diff changes = [] old_autoscale = getattr(previous_settings, 'autoscale', {}) diff = dict_diff(self.autoscale, old_autoscale) # try to be as succinct as possible added = ', '.join( list( map(lambda x: 'default' if x == '' else x, [k for k in diff.get('added', {})]))) # noqa added = 'added autoscale for process type ' + added if added else '' changed = ', '.join( list( map(lambda x: 'default' if x == '' else x, [k for k in diff.get('changed', {})]))) # noqa changed = 'changed autoscale for process type ' + changed if changed else '' deleted = ', '.join( list( map(lambda x: 'default' if x == '' else x, [k for k in diff.get('deleted', {})]))) # noqa deleted = 'deleted autoscale for process type ' + deleted if deleted else '' changes = ', '.join(i for i in (added, changed, deleted) if i) if changes: self.summary += ["{} {}".format(self.owner, changes)]
def _update_label(self, previous_settings): data = getattr(previous_settings, 'label', {}).copy() new = getattr(self, 'label', {}).copy() if not previous_settings: return # if nothing changed copy the settings from previous if not new and data: setattr(self, 'label', data) elif data != new: for k, v in new.items(): if v is not None: data[k] = v else: if k not in data: raise UnprocessableEntity( '{} does not exist under {}'.format( k, 'label')) # noqa del data[k] setattr(self, 'label', data) diff = dict_diff(self.label, getattr(previous_settings, 'label', {})) added = ', '.join( list( map(lambda x: 'default' if x == '' else x, [k for k in diff.get('added', {})]))) # noqa added = 'added label ' + added if added else '' changed = ', '.join( list( map(lambda x: 'default' if x == '' else x, [k for k in diff.get('changed', {})]))) # noqa changed = 'changed label ' + changed if changed else '' deleted = ', '.join( list( map(lambda x: 'default' if x == '' else x, [k for k in diff.get('deleted', {})]))) # noqa deleted = 'deleted label ' + deleted if deleted else '' changes = ', '.join(i for i in (added, changed, deleted) if i) if changes: if self.summary: self.summary += ' and ' self.summary += ["{} {}".format(self.owner, changes)]
def update_autoscale(self, previous_settings): data = getattr(previous_settings, 'autoscale', {}).copy() new = getattr(self, 'autoscale', {}).copy() # If no previous settings then do nothing if not previous_settings: return # if nothing changed copy the settings from previous if not new and data: setattr(self, 'autoscale', data) elif data != new: for proc, scale in new.items(): if scale is None: # error if unsetting non-existing key if proc not in data: raise UnprocessableEntity('{} does not exist under {}'.format(proc, 'autoscale')) # noqa del data[proc] else: data[proc] = scale setattr(self, 'autoscale', data) # only apply new items for proc, scale in new.items(): self.app.autoscale(proc, scale) # if the autoscale information changed, log the dict diff changes = [] old_autoscale = getattr(previous_settings, 'autoscale', {}) diff = dict_diff(self.autoscale, old_autoscale) # try to be as succinct as possible added = ', '.join(list(map(lambda x: 'default' if x == '' else x, [k for k in diff.get('added', {})]))) # noqa added = 'added autoscale for process type ' + added if added else '' changed = ', '.join(list(map(lambda x: 'default' if x == '' else x, [k for k in diff.get('changed', {})]))) # noqa changed = 'changed autoscale for process type ' + changed if changed else '' deleted = ', '.join(list(map(lambda x: 'default' if x == '' else x, [k for k in diff.get('deleted', {})]))) # noqa deleted = 'deleted autoscale for process type ' + deleted if deleted else '' changes = ', '.join(i for i in (added, changed, deleted) if i) if changes: self.summary += ["{} {}".format(self.owner, changes)]
def save(self, *args, **kwargs): # noqa if not self.summary: self.summary = '' prev_release = self.previous() # compare this build to the previous build old_build = prev_release.build if prev_release else None old_config = prev_release.config if prev_release else None # if the build changed, log it and who pushed it if self.version == 1: self.summary += "{} created initial release".format(self.app.owner) elif self.build != old_build: if self.build.sha: self.summary += "{} deployed {}".format(self.build.owner, self.build.sha[:7]) else: self.summary += "{} deployed {}".format(self.build.owner, self.build.image) # if the config data changed, log the dict diff if self.config != old_config: # if env vars change, log the dict diff dict1 = self.config.values dict2 = old_config.values if old_config else {} diff = dict_diff(dict1, dict2) # try to be as succinct as possible added = ', '.join(k for k in diff.get('added', {})) added = 'added ' + added if added else '' changed = ', '.join(k for k in diff.get('changed', {})) changed = 'changed ' + changed if changed else '' deleted = ', '.join(k for k in diff.get('deleted', {})) deleted = 'deleted ' + deleted if deleted else '' changes = ', '.join(i for i in (added, changed, deleted) if i) if changes: if self.summary: self.summary += ' and ' self.summary += "{} {}".format(self.config.owner, changes) # if the limits changed (memory or cpu), log the dict diff changes = [] old_mem = old_config.memory if old_config else {} diff = dict_diff(self.config.memory, old_mem) if diff.get('added') or diff.get('changed') or diff.get('deleted'): changes.append('memory') old_cpu = old_config.cpu if old_config else {} diff = dict_diff(self.config.cpu, old_cpu) if diff.get('added') or diff.get('changed') or diff.get('deleted'): changes.append('cpu') if changes: changes = 'changed limits for '+', '.join(changes) self.summary += "{} {}".format(self.config.owner, changes) # if the lifecycle_post_start hooks changed, log the dict diff changes = [] old_lifecycle_post_start = old_config.lifecycle_post_start if old_config else {} diff = dict_diff(self.config.lifecycle_post_start, old_lifecycle_post_start) # try to be as succinct as possible added = ', '.join(k for k in diff.get('added', {})) added = 'added lifecycle_post_start ' + added if added else '' changed = ', '.join(k for k in diff.get('changed', {})) changed = 'changed lifecycle_post_start ' + changed if changed else '' deleted = ', '.join(k for k in diff.get('deleted', {})) deleted = 'deleted lifecycle_post_start ' + deleted if deleted else '' changes = ', '.join(i for i in (added, changed, deleted) if i) if changes: if self.summary: self.summary += ' and ' self.summary += "{} {}".format(self.config.owner, changes) # if the lifecycle_pre_stop hooks changed, log the dict diff changes = [] old_lifecycle_pre_stop = old_config.lifecycle_pre_stop if old_config else {} diff = dict_diff(self.config.lifecycle_pre_stop, old_lifecycle_pre_stop) # try to be as succinct as possible added = ', '.join(k for k in diff.get('added', {})) added = 'added lifecycle_pre_stop ' + added if added else '' changed = ', '.join(k for k in diff.get('changed', {})) changed = 'changed lifecycle_pre_stop ' + changed if changed else '' deleted = ', '.join(k for k in diff.get('deleted', {})) deleted = 'deleted lifecycle_pre_stop ' + deleted if deleted else '' changes = ', '.join(i for i in (added, changed, deleted) if i) if changes: if self.summary: self.summary += ' and ' # if the timeouts changed, log the dict diff changes = [] old_timeout = old_config.termination_grace_period if old_config else {} diff = dict_diff(self.config.termination_grace_period, old_timeout) if diff.get('added') or diff.get('changed') or diff.get('deleted'): changes.append('termination_grace_period') if changes: changes = 'changed timeouts for '+', '.join(changes) self.summary += "{} {}".format(self.config.owner, changes) # if the tags changed, log the dict diff changes = [] old_tags = old_config.tags if old_config else {} diff = dict_diff(self.config.tags, old_tags) # try to be as succinct as possible added = ', '.join(k for k in diff.get('added', {})) added = 'added tag ' + added if added else '' changed = ', '.join(k for k in diff.get('changed', {})) changed = 'changed tag ' + changed if changed else '' deleted = ', '.join(k for k in diff.get('deleted', {})) deleted = 'deleted tag ' + deleted if deleted else '' changes = ', '.join(i for i in (added, changed, deleted) if i) if changes: if self.summary: self.summary += ' and ' self.summary += "{} {}".format(self.config.owner, changes) # if the registry information changed, log the dict diff changes = [] old_registry = old_config.registry if old_config else {} diff = dict_diff(self.config.registry, old_registry) # try to be as succinct as possible added = ', '.join(k for k in diff.get('added', {})) added = 'added registry info ' + added if added else '' changed = ', '.join(k for k in diff.get('changed', {})) changed = 'changed registry info ' + changed if changed else '' deleted = ', '.join(k for k in diff.get('deleted', {})) deleted = 'deleted registry info ' + deleted if deleted else '' changes = ', '.join(i for i in (added, changed, deleted) if i) if changes: if self.summary: self.summary += ' and ' self.summary += "{} {}".format(self.config.owner, changes) # if the healthcheck information changed, log the dict diff changes = [] old_healthcheck = old_config.healthcheck if old_config else {} diff = dict_diff(self.config.healthcheck, old_healthcheck) # try to be as succinct as possible added = ', '.join(list(map(lambda x: 'default' if x == '' else x, [k for k in diff.get('added', {})]))) # noqa added = 'added healthcheck info for proc type ' + added if added else '' changed = ', '.join(list(map(lambda x: 'default' if x == '' else x, [k for k in diff.get('changed', {})]))) # noqa changed = 'changed healthcheck info for proc type ' + changed if changed else '' deleted = ', '.join(list(map(lambda x: 'default' if x == '' else x, [k for k in diff.get('deleted', {})]))) # noqa deleted = 'deleted healthcheck info for proc type ' + deleted if deleted else '' changes = ', '.join(i for i in (added, changed, deleted) if i) if changes: if self.summary: self.summary += ' and ' self.summary += "{} {}".format(self.config.owner, changes) if not self.summary: if self.version == 1: self.summary = "{} created the initial release".format(self.owner) else: # There were no changes to this release raise AlreadyExists("{} changed nothing - release stopped".format(self.owner)) super(Release, self).save(*args, **kwargs)
def save(self, *args, **kwargs): # noqa if not self.summary: self.summary = '' prev_release = self.previous() # compare this build to the previous build old_build = prev_release.build if prev_release else None old_config = prev_release.config if prev_release else None # if the build changed, log it and who pushed it if self.version == 1: self.summary += "{} created initial release".format(self.app.owner) elif self.build != old_build: if self.build.sha: self.summary += "{} deployed {}".format(self.build.owner, self.build.sha[:7]) else: self.summary += "{} deployed {}".format(self.build.owner, self.build.image) # if the config data changed, log the dict diff if self.config != old_config: # if env vars change, log the dict diff dict1 = self.config.values dict2 = old_config.values if old_config else {} diff = dict_diff(dict1, dict2) # try to be as succinct as possible added = ', '.join(k for k in diff.get('added', {})) added = 'added ' + added if added else '' changed = ', '.join(k for k in diff.get('changed', {})) changed = 'changed ' + changed if changed else '' deleted = ', '.join(k for k in diff.get('deleted', {})) deleted = 'deleted ' + deleted if deleted else '' changes = ', '.join(i for i in (added, changed, deleted) if i) if changes: if self.summary: self.summary += ' and ' self.summary += "{} {}".format(self.config.owner, changes) # if the limits changed (memory or cpu), log the dict diff changes = [] old_mem = old_config.memory if old_config else {} diff = dict_diff(self.config.memory, old_mem) if diff.get('added') or diff.get('changed') or diff.get('deleted'): changes.append('memory') old_cpu = old_config.cpu if old_config else {} diff = dict_diff(self.config.cpu, old_cpu) if diff.get('added') or diff.get('changed') or diff.get('deleted'): changes.append('cpu') if changes: changes = 'changed limits for '+', '.join(changes) self.summary += "{} {}".format(self.config.owner, changes) # if the tags changed, log the dict diff changes = [] old_tags = old_config.tags if old_config else {} diff = dict_diff(self.config.tags, old_tags) # try to be as succinct as possible added = ', '.join(k for k in diff.get('added', {})) added = 'added tag ' + added if added else '' changed = ', '.join(k for k in diff.get('changed', {})) changed = 'changed tag ' + changed if changed else '' deleted = ', '.join(k for k in diff.get('deleted', {})) deleted = 'deleted tag ' + deleted if deleted else '' changes = ', '.join(i for i in (added, changed, deleted) if i) if changes: if self.summary: self.summary += ' and ' self.summary += "{} {}".format(self.config.owner, changes) # if the registry information changed, log the dict diff changes = [] old_registry = old_config.registry if old_config else {} diff = dict_diff(self.config.registry, old_registry) # try to be as succinct as possible added = ', '.join(k for k in diff.get('added', {})) added = 'added registry info ' + added if added else '' changed = ', '.join(k for k in diff.get('changed', {})) changed = 'changed registry info ' + changed if changed else '' deleted = ', '.join(k for k in diff.get('deleted', {})) deleted = 'deleted registry info ' + deleted if deleted else '' changes = ', '.join(i for i in (added, changed, deleted) if i) if changes: if self.summary: self.summary += ' and ' self.summary += "{} {}".format(self.config.owner, changes) if not self.summary: if self.version == 1: self.summary = "{} created the initial release".format(self.owner) else: # There were no changes to this release raise AlreadyExists("{} changed nothing - release stopped".format(self.owner)) super(Release, self).save(*args, **kwargs)
def save(self, *args, **kwargs): # noqa if not self.summary: self.summary = "" prev_release = self.previous() # compare this build to the previous build old_build = prev_release.build if prev_release else None old_config = prev_release.config if prev_release else None # if the build changed, log it and who pushed it if self.version == 1: self.summary += "{} created initial release".format(self.app.owner) elif self.build != old_build: if self.build.sha: self.summary += "{} deployed {}".format(self.build.owner, self.build.sha[:7]) else: self.summary += "{} deployed {}".format(self.build.owner, self.build.image) # if the config data changed, log the dict diff if self.config != old_config: # if env vars change, log the dict diff dict1 = self.config.values dict2 = old_config.values if old_config else {} diff = dict_diff(dict1, dict2) # try to be as succinct as possible added = ", ".join(k for k in diff.get("added", {})) added = "added " + added if added else "" changed = ", ".join(k for k in diff.get("changed", {})) changed = "changed " + changed if changed else "" deleted = ", ".join(k for k in diff.get("deleted", {})) deleted = "deleted " + deleted if deleted else "" changes = ", ".join(i for i in (added, changed, deleted) if i) if changes: if self.summary: self.summary += " and " self.summary += "{} {}".format(self.config.owner, changes) # if the limits changed (memory or cpu), log the dict diff changes = [] old_mem = old_config.memory if old_config else {} diff = dict_diff(self.config.memory, old_mem) if diff.get("added") or diff.get("changed") or diff.get("deleted"): changes.append("memory") old_cpu = old_config.cpu if old_config else {} diff = dict_diff(self.config.cpu, old_cpu) if diff.get("added") or diff.get("changed") or diff.get("deleted"): changes.append("cpu") if changes: changes = "changed limits for " + ", ".join(changes) self.summary += "{} {}".format(self.config.owner, changes) # if the tags changed, log the dict diff changes = [] old_tags = old_config.tags if old_config else {} diff = dict_diff(self.config.tags, old_tags) # try to be as succinct as possible added = ", ".join(k for k in diff.get("added", {})) added = "added tag " + added if added else "" changed = ", ".join(k for k in diff.get("changed", {})) changed = "changed tag " + changed if changed else "" deleted = ", ".join(k for k in diff.get("deleted", {})) deleted = "deleted tag " + deleted if deleted else "" changes = ", ".join(i for i in (added, changed, deleted) if i) if changes: if self.summary: self.summary += " and " self.summary += "{} {}".format(self.config.owner, changes) # if the registry information changed, log the dict diff changes = [] old_registry = old_config.registry if old_config else {} diff = dict_diff(self.config.registry, old_registry) # try to be as succinct as possible added = ", ".join(k for k in diff.get("added", {})) added = "added registry info " + added if added else "" changed = ", ".join(k for k in diff.get("changed", {})) changed = "changed registry info " + changed if changed else "" deleted = ", ".join(k for k in diff.get("deleted", {})) deleted = "deleted registry info " + deleted if deleted else "" changes = ", ".join(i for i in (added, changed, deleted) if i) if changes: if self.summary: self.summary += " and " self.summary += "{} {}".format(self.config.owner, changes) # if the healthcheck information changed, log the dict diff changes = [] old_healthcheck = old_config.healthcheck if old_config else {} diff = dict_diff(self.config.healthcheck, old_healthcheck) # try to be as succinct as possible added = ", ".join( list(map(lambda x: "default" if x == "" else x, [k for k in diff.get("added", {})])) ) # noqa added = "added healthcheck info for proc type " + added if added else "" changed = ", ".join( list(map(lambda x: "default" if x == "" else x, [k for k in diff.get("changed", {})])) ) # noqa changed = "changed healthcheck info for proc type " + changed if changed else "" deleted = ", ".join( list(map(lambda x: "default" if x == "" else x, [k for k in diff.get("deleted", {})])) ) # noqa deleted = "deleted healthcheck info for proc type " + deleted if deleted else "" changes = ", ".join(i for i in (added, changed, deleted) if i) if changes: if self.summary: self.summary += " and " self.summary += "{} {}".format(self.config.owner, changes) if not self.summary: if self.version == 1: self.summary = "{} created the initial release".format(self.owner) else: # There were no changes to this release raise AlreadyExists("{} changed nothing - release stopped".format(self.owner)) super(Release, self).save(*args, **kwargs)
def save(self, *args, **kwargs): # noqa if not self.summary: self.summary = '' prev_release = self.previous() # compare this build to the previous build old_build = prev_release.build if prev_release else None old_config = prev_release.config if prev_release else None # if the build changed, log it and who pushed it if self.version == 1: self.summary += "{} created initial release".format( self.app.owner) elif self.build != old_build: if self.build.sha: self.summary += "{} deployed {}".format( self.build.owner, self.build.sha[:7]) else: self.summary += "{} deployed {}".format( self.build.owner, self.build.image) # if the config data changed, log the dict diff if self.config != old_config: # if env vars change, log the dict diff dict1 = self.config.values dict2 = old_config.values if old_config else {} diff = dict_diff(dict1, dict2) # try to be as succinct as possible added = ', '.join(k for k in diff.get('added', {})) added = 'added ' + added if added else '' changed = ', '.join(k for k in diff.get('changed', {})) changed = 'changed ' + changed if changed else '' deleted = ', '.join(k for k in diff.get('deleted', {})) deleted = 'deleted ' + deleted if deleted else '' changes = ', '.join(i for i in (added, changed, deleted) if i) if changes: if self.summary: self.summary += ' and ' self.summary += "{} {}".format(self.config.owner, changes) # if the limits changed (memory or cpu), log the dict diff changes = [] old_mem = old_config.memory if old_config else {} diff = dict_diff(self.config.memory, old_mem) if diff.get('added') or diff.get('changed') or diff.get( 'deleted'): changes.append('memory') old_cpu = old_config.cpu if old_config else {} diff = dict_diff(self.config.cpu, old_cpu) if diff.get('added') or diff.get('changed') or diff.get( 'deleted'): changes.append('cpu') if changes: changes = 'changed limits for ' + ', '.join(changes) self.summary += "{} {}".format(self.config.owner, changes) # if the tags changed, log the dict diff changes = [] old_tags = old_config.tags if old_config else {} diff = dict_diff(self.config.tags, old_tags) # try to be as succinct as possible added = ', '.join(k for k in diff.get('added', {})) added = 'added tag ' + added if added else '' changed = ', '.join(k for k in diff.get('changed', {})) changed = 'changed tag ' + changed if changed else '' deleted = ', '.join(k for k in diff.get('deleted', {})) deleted = 'deleted tag ' + deleted if deleted else '' changes = ', '.join(i for i in (added, changed, deleted) if i) if changes: if self.summary: self.summary += ' and ' self.summary += "{} {}".format(self.config.owner, changes) # if the registry information changed, log the dict diff changes = [] old_registry = old_config.registry if old_config else {} diff = dict_diff(self.config.registry, old_registry) # try to be as succinct as possible added = ', '.join(k for k in diff.get('added', {})) added = 'added registry info ' + added if added else '' changed = ', '.join(k for k in diff.get('changed', {})) changed = 'changed registry info ' + changed if changed else '' deleted = ', '.join(k for k in diff.get('deleted', {})) deleted = 'deleted registry info ' + deleted if deleted else '' changes = ', '.join(i for i in (added, changed, deleted) if i) if changes: if self.summary: self.summary += ' and ' self.summary += "{} {}".format(self.config.owner, changes) if not self.summary: if self.version == 1: self.summary = "{} created the initial release".format( self.owner) else: # There were no changes to this release raise AlreadyExists( "{} changed nothing - release stopped".format( self.owner)) super(Release, self).save(*args, **kwargs)