def force_my_sandbox(self, session, logger, dbuser, sandbox): sbx_split = sandbox.split('/') sandbox = AqStr.normalize(sbx_split[-1]) author = '/'.join(sbx_split[:-1]) if len(sbx_split) <= 1: return sandbox # User used the name/branch syntax - that's fine. They can't # do anything on behalf of anyone else, though, so error if the # user given is anyone else. if AqStr.normalize(author) != dbuser.name: raise ArgumentError("User '%s' cannot add or get a sandbox on " "behalf of '%s'." % (dbuser.name, author)) return sandbox
def render(self, session, logger, prefix, dns_domain, hostname, machine, **args): if dns_domain: dbdns_domain = DnsDomain.get_unique(session, dns_domain, compel=True) else: dbmachine = Machine.get_unique(session, machine, compel=True) dbdns_domain = None loc = dbmachine.location while loc and not dbdns_domain: dbdns_domain = loc.default_dns_domain loc = loc.parent if not dbdns_domain: raise ArgumentError("There is no default DNS domain configured " "for the machine's location. Please " "specify --dns_domain.") # Lock the DNS domain to prevent the same name generated for # simultaneous requests dbdns_domain.lock_row() prefix = AqStr.normalize(prefix) result = search_next(session=session, cls=Fqdn, attr=Fqdn.name, value=prefix, dns_domain=dbdns_domain, start=None, pack=None) hostname = "%s%d.%s" % (prefix, result, dbdns_domain) CommandAddHost.render(self, session, logger, hostname=hostname, machine=machine, **args) logger.info("Selected host name %s" % hostname) self.audit_result(session, 'hostname', hostname, **args) return hostname
def render(self, session, machine, model, vendor, machine_type, chassis, slot, **arguments): q = session.query(Machine) if machine: # TODO: This command still mixes search/show facilities. # For now, give an error if machine name not found, but # also allow the command to be used to check if the machine has # the requested attributes (via the standard query filters). # In the future, this should be clearly separated as 'show machine' # and 'search machine'. machine = AqStr.normalize(machine) Machine.check_label(machine) Machine.get_unique(session, machine, compel=True) q = q.filter_by(label=machine) dblocation = get_location(session, **arguments) if dblocation: q = q.filter_by(location=dblocation) if chassis: dbchassis = Chassis.get_unique(session, chassis, compel=True) q = q.join('chassis_slot') q = q.filter_by(chassis=dbchassis) q = q.reset_joinpoint() if slot is not None: q = q.join('chassis_slot') q = q.filter_by(slot_number=slot) q = q.reset_joinpoint() if model or vendor or machine_type: subq = Model.get_matching_query(session, name=model, vendor=vendor, machine_type=machine_type, compel=True) q = q.filter(Machine.model_id.in_(subq)) return q.order_by(Machine.label).all()
def render(self, session, machine, model, vendor, machine_type, chassis, slot, **arguments): q = session.query(Machine) if machine: # TODO: This command still mixes search/show facilities. # For now, give an error if machine name not found, but # also allow the command to be used to check if the machine has # the requested attributes (via the standard query filters). # In the future, this should be clearly separated as 'show machine' # and 'search machine'. machine = AqStr.normalize(machine) Machine.check_label(machine) Machine.get_unique(session, machine, compel=True) q = q.filter_by(label=machine) dblocation = get_location(session, **arguments) if dblocation: q = q.filter_by(location=dblocation) if chassis: dbchassis = Chassis.get_unique(session, chassis, compel=True) q = q.join('chassis_slot') q = q.filter_by(chassis=dbchassis) q = q.reset_joinpoint() if slot is not None: q = q.join('chassis_slot') q = q.filter_by(slot_number=slot) q = q.reset_joinpoint() if model or vendor or machine_type: subq = Model.get_matching_query(session, name=model, vendor=vendor, model_type=machine_type, compel=True) q = q.filter(Machine.model_id.in_(subq)) return q.order_by(Machine.label).all()
def render(self, session, logger, prefix, **args): prefix = AqStr.normalize(prefix) result = search_next(session=session, cls=Machine, attr=Machine.label, value=prefix, start=None, pack=None) machine = '%s%d' % (prefix, result) args['machine'] = machine CommandAddMachine.render(self, session, logger, **args) logger.info("Selected hardware label %s" % machine) self.audit_result(session, 'machine', machine, **args) return machine
def __init__(self, label=None, **kwargs): label = AqStr.normalize(label) if not label: raise ArgumentError("HardwareEntity needs a label.") super(HardwareEntity, self).__init__(label=label, **kwargs)
def get_unique(cls, sess, name, hardware_type=None, compel=False, preclude=False, query_options=None): """ Returns a unique HardwareEntity given session and fqdn """ # If the hardware_type param isn't explicitly set and we have a # polymorphic identity, assume we're querying only for items of our # hardware_type. if hardware_type: if isclass(hardware_type): clslabel = hardware_type._get_class_label() hardware_type = hardware_type.__mapper_args__['polymorphic_identity'] else: pcls = cls.__mapper__.polymorphic_map[hardware_type].class_ clslabel = pcls._get_class_label() else: if 'polymorphic_identity' in cls.__mapper_args__: hardware_type = cls.__mapper_args__['polymorphic_identity'] clslabel = cls._get_class_label() # The automagic DNS lookup does not really make sense with preclude=True if preclude: name = AqStr.normalize(name) cls.check_label(name) q = sess.query(cls) if "." in name: dns_rec = DnsRecord.get_unique(sess, fqdn=name, compel=True) # We know the primary name, do not load it again q = q.options(lazyload('primary_name')) q = q.filter_by(primary_name=dns_rec) else: dns_rec = None q = q.filter_by(label=name) if query_options: q = q.options(*query_options) try: hwe = q.one() except NoResultFound: # Check if the name is in use by a different hardware type q = sess.query(HardwareEntity) if dns_rec: # We know the primary name, do not load it again q = q.options(lazyload('primary_name')) q = q.filter_by(primary_name=dns_rec) else: q = q.filter_by(label=name) try: hwe = q.one() if dns_rec: # We know the primary name, do not load it again set_committed_value(hwe, 'primary_name', dns_rec) raise ArgumentError("{0} exists, but is not a {1}." .format(hwe, clslabel.lower())) except NoResultFound: hwe = None if compel: raise NotFoundException("%s %s not found." % (clslabel, name)) if hwe: if preclude: raise ArgumentError('{0} already exists.'.format(hwe)) if dns_rec: # We know the primary name, do not load it again set_committed_value(hwe, 'primary_name', dns_rec) return hwe
def get_unique(cls, sess, name, hardware_type=None, compel=False, preclude=False, query_options=None): """ Returns a unique HardwareEntity given session and fqdn """ # If the hardware_type param isn't explicitly set and we have a # polymorphic identity, assume we're querying only for items of our # hardware_type. if hardware_type: if isclass(hardware_type): clslabel = hardware_type._get_class_label() hardware_type = hardware_type.__mapper_args__[ 'polymorphic_identity'] else: pcls = cls.__mapper__.polymorphic_map[hardware_type].class_ clslabel = pcls._get_class_label() else: if 'polymorphic_identity' in cls.__mapper_args__: hardware_type = cls.__mapper_args__['polymorphic_identity'] clslabel = cls._get_class_label() # The automagic DNS lookup does not really make sense with preclude=True if preclude: name = AqStr.normalize(name) cls.check_label(name) q = sess.query(cls) if "." in name: dns_rec = DnsRecord.get_unique(sess, fqdn=name, compel=True) # We know the primary name, do not load it again q = q.options(lazyload('primary_name')) q = q.filter_by(primary_name=dns_rec) else: dns_rec = None q = q.filter_by(label=name) if query_options: q = q.options(*query_options) try: hwe = q.one() except NoResultFound: # Check if the name is in use by a different hardware type q = sess.query(HardwareEntity) if dns_rec: # We know the primary name, do not load it again q = q.options(lazyload('primary_name')) q = q.filter_by(primary_name=dns_rec) else: q = q.filter_by(label=name) try: hwe = q.one() if dns_rec: # We know the primary name, do not load it again set_committed_value(hwe, 'primary_name', dns_rec) raise ArgumentError("{0} exists, but is not a {1}.".format( hwe, hardware_type)) except NoResultFound: hwe = None if compel: raise NotFoundException("%s %s not found." % (clslabel, name)) if hwe: if preclude: raise ArgumentError('{0} already exists.'.format(hwe)) if dns_rec: # We know the primary name, do not load it again set_committed_value(hwe, 'primary_name', dns_rec) return hwe