def render(self, session, logger, personality, archetype, **arguments): dbpersona = Personality.get_unique(session, name=personality, archetype=archetype, compel=True) dbhost = session.query(Host).filter_by(personality=dbpersona).first() dbcls = session.query(Cluster).filter_by(personality=dbpersona).first() if dbhost or dbcls: raise ArgumentError("{0} is still in use and cannot be deleted." .format(dbpersona)) plenary = PlenaryPersonality(dbpersona, logger=logger) session.delete(dbpersona) session.flush() plenary.remove() return
def render(self, session, logger, target, grn, eon_id, hostname, list, personality, archetype, **arguments): dbgrn = lookup_grn(session, grn, eon_id, logger=logger, config=self.config) target_type = "personality" if personality else "host" if hostname: objs = [hostname_to_host(session, hostname)] elif list: check_hostlist_size(self.command, self.config, list) objs = hostlist_to_hosts(session, list) elif personality: objs = [Personality.get_unique(session, name=personality, archetype=archetype, compel=True)] for obj in objs: # INFO: Fails for archetypes other than 'aquilon', 'vmhost' valid_targets = self.config.get("archetype_" + obj.archetype.name, target_type + "_grn_targets") if target not in map(lambda s: s.strip(), valid_targets.split(",")): raise ArgumentError("Invalid %s target %s for archetype %s, please " "choose from %s" % (target_type, target, obj.archetype.name, valid_targets)) self._update_dbobj(obj, target, dbgrn) session.flush() if personality: plenary = PlenaryPersonality(objs[0], logger=logger) plenary.write() return
def render(self, session, logger, personality, archetype, grn, eon_id, host_environment, comments, cluster_required, copy_from, config_override, **arguments): if not VALID_PERSONALITY_RE.match(personality): raise ArgumentError("Personality name '%s' is not valid." % personality) if not (grn or eon_id): raise ArgumentError("GRN or EON ID is required for adding a " "personality.") dbarchetype = Archetype.get_unique(session, archetype, compel=True) if not host_environment: try: host_environment = self.config.get("archetype_" + archetype, "default_environment") except (NoSectionError, NoOptionError): raise ArgumentError("Default environment is not configured " "for {0:l}, please specify " "--host_environment.".format(dbarchetype)) HostEnvironment.polymorphic_subclass(host_environment, "Unknown environment name") Personality.validate_env_in_name(personality, host_environment) host_env = HostEnvironment.get_unique(session, host_environment, compel=True) Personality.get_unique(session, archetype=dbarchetype, name=personality, preclude=True) dbgrn = lookup_grn(session, grn, eon_id, logger=logger, config=self.config) dbpersona = Personality(name=personality, archetype=dbarchetype, cluster_required=bool(cluster_required), host_environment=host_env, owner_grn=dbgrn, comments=comments, config_override=config_override) session.add(dbpersona) if self.config.has_option("archetype_" + archetype, "default_grn_target"): dbpersona.grns.append((dbpersona, dbgrn, self.config.get("archetype_" + archetype, "default_grn_target"))) if copy_from: ## copy config data dbfrom_persona = Personality.get_unique(session, archetype=dbarchetype, name=copy_from, compel=True) src_parameters = get_parameters(session, personality=dbfrom_persona) db_param_holder = PersonalityParameter(personality=dbpersona) for param in src_parameters: dbparameter = Parameter(value=param.value, comments=param.comments, holder=db_param_holder) session.add(dbparameter) for link in dbfrom_persona.features: params = {} params["personality"] = dbpersona if link.model: params["model"] = link.model if link.interface_name: params["interface_name"] = link.interface_name add_link(session, logger, link.feature, params) ## service maps q = session.query(PersonalityServiceMap).filter_by(personality=dbfrom_persona) for sm in q.all() : dbmap = PersonalityServiceMap(service_instance=sm.service_instance, location=sm.location, network=sm.network, personality=dbpersona) session.add(dbmap) ## required services dbpersona.services.extend(dbfrom_persona.services) session.flush() plenary = PlenaryPersonality(dbpersona, logger=logger) plenary.write() return
def render(self, session, logger, feature, archetype, personality, model, vendor, interface, justification, user, **arguments): # Binding a feature to a named interface makes sense in the scope of a # personality, but not for a whole archetype. if interface and not personality: raise ArgumentError("Binding to a named interface needs " "a personality.") q = session.query(Personality) dbarchetype = None feature_type = "host" justification_required = True # Warning: order matters here! params = {} if personality: justification_required = False dbpersonality = Personality.get_unique(session, name=personality, archetype=archetype, compel=True) params["personality"] = dbpersonality if interface: params["interface_name"] = interface feature_type = "interface" dbarchetype = dbpersonality.archetype q = q.filter_by(archetype=dbarchetype) q = q.filter_by(name=personality) elif archetype: dbarchetype = Archetype.get_unique(session, archetype, compel=True) params["archetype"] = dbarchetype q = q.filter_by(archetype=dbarchetype) else: # It's highly unlikely that a feature template would work for # _any_ archetype, so disallow this case for now. As I can't # rule out that such a case will not have some uses in the # future, the restriction is here and not in the model. raise ArgumentError("Please specify either an archetype or " "a personality when binding a feature.") if model: dbmodel = Model.get_unique(session, name=model, vendor=vendor, compel=True) if dbmodel.machine_type == "nic": feature_type = "interface" else: feature_type = "hardware" params["model"] = dbmodel if dbarchetype and not dbarchetype.is_compileable: raise UnimplementedError("Binding features to non-compilable " "archetypes is not implemented.") if not feature_type: # pragma: no cover raise InternalError("Feature type is not known.") dbfeature = Feature.get_unique(session, name=feature, feature_type=feature_type, compel=True) cnt = q.count() # TODO: should the limit be configurable? if justification_required and cnt > 0: if not justification: raise AuthorizationException( "Changing feature bindings for more " "than just a personality requires --justification.") validate_justification(user, justification) self.do_link(session, logger, dbfeature, params) session.flush() idx = 0 written = 0 successful = [] failed = [] with CompileKey(logger=logger): personalities = q.all() for personality in personalities: idx += 1 if idx % 1000 == 0: # pragma: no cover logger.client_info("Processing personality %d of %d..." % (idx, cnt)) if not personality.archetype.is_compileable: # pragma: no cover continue try: plenary_personality = PlenaryPersonality(personality) written += plenary_personality.write(locked=True) successful.append(plenary_personality) except IncompleteError: pass except Exception, err: # pragma: no cover failed.append("{0} failed: {1}".format(personality, err)) if failed: # pragma: no cover for plenary in successful: plenary.restore_stash() raise PartialError([], failed)
def render(self, session, logger, feature, archetype, personality, model, vendor, interface, justification, user, **arguments): # Binding a feature to a named interface makes sense in the scope of a # personality, but not for a whole archetype. if interface and not personality: raise ArgumentError("Binding to a named interface needs " "a personality.") q = session.query(Personality) dbarchetype = None feature_type = "host" justification_required = True # Warning: order matters here! params = {} if personality: justification_required = False dbpersonality = Personality.get_unique(session, name=personality, archetype=archetype, compel=True) params["personality"] = dbpersonality if interface: params["interface_name"] = interface feature_type = "interface" dbarchetype = dbpersonality.archetype q = q.filter_by(archetype=dbarchetype) q = q.filter_by(name=personality) elif archetype: dbarchetype = Archetype.get_unique(session, archetype, compel=True) params["archetype"] = dbarchetype q = q.filter_by(archetype=dbarchetype) else: # It's highly unlikely that a feature template would work for # _any_ archetype, so disallow this case for now. As I can't # rule out that such a case will not have some uses in the # future, the restriction is here and not in the model. raise ArgumentError("Please specify either an archetype or " "a personality when binding a feature.") if model: dbmodel = Model.get_unique(session, name=model, vendor=vendor, compel=True) if dbmodel.machine_type == "nic": feature_type = "interface" else: feature_type = "hardware" params["model"] = dbmodel if dbarchetype and not dbarchetype.is_compileable: raise UnimplementedError("Binding features to non-compilable " "archetypes is not implemented.") if not feature_type: # pragma: no cover raise InternalError("Feature type is not known.") dbfeature = Feature.get_unique(session, name=feature, feature_type=feature_type, compel=True) cnt = q.count() # TODO: should the limit be configurable? if justification_required and cnt > 0: if not justification: raise AuthorizationException( "Changing feature bindings for more " "than just a personality requires --justification.") validate_justification(user, justification) self.do_link(session, logger, dbfeature, params) session.flush() idx = 0 written = 0 successful = [] failed = [] with CompileKey(logger=logger): personalities = q.all() for personality in personalities: idx += 1 if idx % 1000 == 0: # pragma: no cover logger.client_info("Processing personality %d of %d..." % (idx, cnt)) if not personality.archetype.is_compileable: # pragma: no cover continue try: plenary_personality = PlenaryPersonality(personality) written += plenary_personality.write(locked=True) successful.append(plenary_personality) except IncompleteError: pass except Exception, err: # pragma: no cover failed.append("{0} failed: {1}".format(personality, err)) if failed: # pragma: no cover for plenary in successful: plenary.restore_stash() raise PartialError([], failed)