def render(self, session, logger, hostname, service, instance, **arguments): dbhost = hostname_to_host(session, hostname) dbservice = Service.get_unique(session, service, compel=True) msg = "Service %s" % service if instance: dbinstances = [get_service_instance(session, dbservice, instance)] msg = "Service %s, instance %s" % (service, instance) else: q = session.query(ServiceInstance) q = q.filter_by(service=dbservice) q = q.join('servers') q = q.filter_by(host=dbhost) dbinstances = q.all() for dbinstance in dbinstances: if dbhost in dbinstance.server_hosts: if (dbinstance.client_count > 0 and len(dbinstance.server_hosts) <= 1): logger.warning("WARNING: Server %s, is the last server " "bound to %s which still has clients" % (hostname, msg)) dbinstance.server_hosts.remove(dbhost) session.expire(dbhost, ['_services_provided']) session.flush() plenaries = PlenaryCollection(logger=logger) plenaries.append(Plenary.get_plenary(dbhost)) for dbinstance in dbinstances: plenaries.append(Plenary.get_plenary(dbinstance)) plenaries.write() return
def render(self, session, logger, city, timezone, campus, default_dns_domain, comments, **arguments): dbcity = get_location(session, city=city) # Updating machine templates is expensive, so only do that if needed update_machines = False if timezone is not None: dbcity.timezone = timezone if comments is not None: dbcity.comments = comments if default_dns_domain is not None: if default_dns_domain: dbdns_domain = DnsDomain.get_unique(session, default_dns_domain, compel=True) dbcity.default_dns_domain = dbdns_domain else: dbcity.default_dns_domain = None prev_campus = None dsdb_runner = None dsdb_runner = DSDBRunner(logger=logger) if campus is not None: dbcampus = get_location(session, campus=campus) # This one would change the template's locations hence forbidden if dbcampus.hub != dbcity.hub: # Doing this both to reduce user error and to limit # testing required. raise ArgumentError("Cannot change campus. {0} is in {1:l}, " "while {2:l} is in {3:l}.".format( dbcampus, dbcampus.hub, dbcity, dbcity.hub)) if dbcity.campus: prev_campus = dbcity.campus dbcity.update_parent(parent=dbcampus) update_machines = True session.flush() if campus is not None: if prev_campus: prev_name = prev_campus.name else: prev_name = None dsdb_runner.update_city(city, dbcampus.name, prev_name) plenaries = PlenaryCollection(logger=logger) plenaries.append(Plenary.get_plenary(dbcity)) if update_machines: q = session.query(Machine) q = q.filter(Machine.location_id.in_(dbcity.offspring_ids())) logger.client_info("Updating %d machines..." % q.count()) for dbmachine in q: plenaries.append(Plenary.get_plenary(dbmachine)) count = plenaries.write() dsdb_runner.commit_or_rollback() logger.client_info("Flushed %d templates." % count)
def render(self, session, logger, service, instance, comments, **arguments): dbservice = session.query(Service).filter_by(name=service).first() if dbservice and instance is None: raise ArgumentError("Service %s already exists." % dbservice.name) if not dbservice: # "add_service --service foo --comments blah" should add the comments # to Service, # "add_service --service foo --instance bar --comments blah" should # add the comments to ServiceInstance if instance: srvcomments = None else: srvcomments = comments dbservice = Service(name=service, comments=srvcomments) session.add(dbservice) plenaries = PlenaryCollection(logger=logger) plenaries.append(Plenary.get_plenary(dbservice)) if instance: ServiceInstance.get_unique(session, service=dbservice, name=instance, preclude=True) dbsi = ServiceInstance(service=dbservice, name=instance, comments=comments) session.add(dbsi) plenaries.append(Plenary.get_plenary(dbsi)) session.flush() plenaries.write() return
def render(self, session, logger, cluster, buildstatus, **arguments): dbcluster = Cluster.get_unique(session, cluster, compel=True) dbstatus = ClusterLifecycle.get_unique( session, buildstatus, compel=True) if not dbcluster.status.transition(dbcluster, dbstatus): return if not dbcluster.personality.archetype.is_compileable: return session.flush() plenaries = PlenaryCollection(logger=logger) plenaries.append(Plenary.get_plenary(dbcluster)) for dbhost in dbcluster.hosts: plenaries.append(Plenary.get_plenary(dbhost)) # Force a host lock as pan might overwrite the profile... key = CompileKey(domain=dbcluster.branch.name, logger=logger) try: lock_queue.acquire(key) plenaries.write(locked=True) td = TemplateDomain( dbcluster.branch, dbcluster.sandbox_author, logger=logger) td.compile(session, plenaries.object_templates, locked=True) except: plenaries.restore_stash() raise finally: lock_queue.release(key) return
def render(self, session, logger, service, instance, position, hostname, cluster, ip, resourcegroup, service_address, alias, **arguments): dbservice = Service.get_unique(session, service, compel=True) if instance: dbsi = ServiceInstance.get_unique(session, service=dbservice, name=instance, compel=True) dbinstances = [dbsi] else: # --position for multiple service instances sounds dangerous, so # disallow it until a real usecase emerges if position: raise ArgumentError("The --position option can only be " "specified for one service instance.") q = session.query(ServiceInstance) q = q.filter_by(service=dbservice) dbinstances = q.all() plenaries = PlenaryCollection(logger=logger) if position is not None: params = None else: params = lookup_target(session, plenaries, hostname, ip, cluster, resourcegroup, service_address, alias) for dbinstance in dbinstances: if position is not None: if position < 0 or position >= len(dbinstance.servers): raise ArgumentError("Invalid server position.") dbsrv = dbinstance.servers[position] if dbsrv.host: plenaries.append(Plenary.get_plenary(dbsrv.host)) if dbsrv.cluster: plenaries.append(Plenary.get_plenary(dbsrv.cluster)) else: dbsrv = find_server(dbinstance, params) if not dbsrv: if instance: raise NotFoundException("No such server binding.") continue plenaries.append(Plenary.get_plenary(dbinstance)) if dbsrv.host: session.expire(dbsrv.host, ['services_provided']) if dbsrv.cluster: session.expire(dbsrv.cluster, ['services_provided']) dbinstance.servers.remove(dbsrv) if dbinstance.client_count > 0 and not dbinstance.servers: logger.warning("Warning: {0} was left without servers, " "but it still has clients.".format(dbinstance)) session.flush() plenaries.write() return
def render(self, session, logger, machine, disk, controller, size, all, dbuser, **arguments): # Handle deprecated arguments if arguments.get("type", None): self.deprecated_option("type", "Please use --controller instead.", logger=logger, **arguments) controller = arguments["type"] if arguments.get("capacity", None): self.deprecated_option("capacity", "Please use --size instead.", logger=logger, **arguments) size = arguments["capacity"] dbmachine = Machine.get_unique(session, machine, compel=True) q = session.query(Disk).filter_by(machine=dbmachine) if disk: q = q.filter_by(device_name=disk) if controller: if controller not in controller_types: raise ArgumentError("%s is not a valid controller type, use " "one of: %s." % (controller, ", ".join(controller_types))) q = q.filter_by(controller_type=controller) if size is not None: q = q.filter_by(capacity=size) results = q.all() if len(results) == 0: raise NotFoundException("No disks found.") elif len(results) > 1 and not all: raise ArgumentError("More than one matching disks found. " "Use --all to delete them all.") for result in results: session.delete(result) session.flush() session.expire(dbmachine, ['disks']) plenary_machine = Plenary.get_plenary(dbmachine, logger=logger) key = plenary_machine.get_write_key() dbcontainer = dbmachine.vm_container if dbcontainer: plenary_container = Plenary.get_plenary(dbcontainer, logger=logger) key = CompileKey.merge([key, plenary_container.get_write_key()]) try: lock_queue.acquire(key) if dbcontainer: plenary_container.write(locked=True) plenary_machine.write(locked=True) except: plenary_machine.restore_stash() if dbcontainer: plenary_container.restore_stash() raise finally: lock_queue.release(key)
def render(self, session, logger, hostname, service, instance, **arguments): dbhost = hostname_to_host(session, hostname) dbservice = Service.get_unique(session, service, compel=True) msg = "Service %s" % service if instance: dbinstances = [get_service_instance(session, dbservice, instance)] msg = "Service %s, instance %s" % (service, instance) else: q = session.query(ServiceInstance) q = q.filter_by(service=dbservice) q = q.join('servers') q = q.filter_by(host=dbhost) dbinstances = q.all() for dbinstance in dbinstances: if dbhost in dbinstance.server_hosts: if (dbinstance.client_count > 0 and len(dbinstance.server_hosts) <= 1): logger.warning("WARNING: Server %s, is the last server " "bound to %s which still has clients" % (hostname, msg)) dbinstance.server_hosts.remove(dbhost) session.expire(dbhost, ['_services_provided']) session.flush() plenaries = PlenaryCollection(logger=logger) plenaries.append(Plenary.get_plenary(dbhost)) for dbinstance in dbinstances: plenaries.append(Plenary.get_plenary(dbinstance)) plenaries.write() return
def render(self, session, logger, switch, **arguments): dbswitch = Switch.get_unique(session, switch, compel=True) # Check and complain if the switch has any other addresses than its # primary address addrs = [] for addr in dbswitch.all_addresses(): if addr.ip == dbswitch.primary_ip: continue addrs.append(str(addr.ip)) if addrs: raise ArgumentError("{0} still provides the following addresses, " "delete them first: {1}.".format (dbswitch, ", ".join(addrs))) dbdns_rec = dbswitch.primary_name ip = dbswitch.primary_ip old_fqdn = str(dbswitch.primary_name.fqdn) old_comments = dbswitch.comments session.delete(dbswitch) if dbdns_rec: delete_dns_record(dbdns_rec) session.flush() # Any switch ports hanging off this switch should be deleted with # the cascade delete of the switch. switch_plenary = Plenary.get_plenary(dbswitch, logger=logger) # clusters connected to this switch plenaries = PlenaryCollection(logger=logger) for dbcluster in dbswitch.esx_clusters: plenaries.append(Plenary.get_plenary(dbcluster)) key = CompileKey.merge([switch_plenary.get_remove_key(), plenaries.get_write_key()]) try: lock_queue.acquire(key) switch_plenary.stash() plenaries.write(locked=True) switch_plenary.remove(locked=True) if ip: dsdb_runner = DSDBRunner(logger=logger) # FIXME: restore interface name/MAC on rollback dsdb_runner.delete_host_details(old_fqdn, ip, comments=old_comments) dsdb_runner.commit_or_rollback("Could not remove switch from DSDB") return except: plenaries.restore_stash() switch_plenary.restore_stash() raise finally: lock_queue.release(key)
def render(self, session, logger, machine, disk, controller, size, all, dbuser, **arguments): # Handle deprecated arguments if arguments.get("type", None): self.deprecated_option("type", "Please use --controller instead.", logger=logger, **arguments) controller = arguments["type"] if arguments.get("capacity", None): self.deprecated_option("capacity", "Please use --size instead.", logger=logger, **arguments) size = arguments["capacity"] dbmachine = Machine.get_unique(session, machine, compel=True) q = session.query(Disk).filter_by(machine=dbmachine) if disk: q = q.filter_by(device_name=disk) if controller: if controller not in controller_types: raise ArgumentError("%s is not a valid controller type, use " "one of: %s." % (controller, ", ".join(controller_types) )) q = q.filter_by(controller_type=controller) if size is not None: q = q.filter_by(capacity=size) results = q.all() if len(results) == 0: raise NotFoundException("No disks found.") elif len(results) > 1 and not all: raise ArgumentError("More than one matching disks found. " "Use --all to delete them all.") for result in results: session.delete(result) session.flush() session.expire(dbmachine, ['disks']) plenary_machine = Plenary.get_plenary(dbmachine, logger=logger) key = plenary_machine.get_write_key() dbcontainer = dbmachine.vm_container if dbcontainer: plenary_container = Plenary.get_plenary(dbcontainer, logger=logger) key = CompileKey.merge([key, plenary_container.get_write_key()]) try: lock_queue.acquire(key) if dbcontainer: plenary_container.write(locked=True) plenary_machine.write(locked=True) except: plenary_machine.restore_stash() if dbcontainer: plenary_container.restore_stash() raise finally: lock_queue.release(key)
def render(self, session, logger, network_device, **arguments): dbnetdev = NetworkDevice.get_unique(session, network_device, compel=True) # Check and complain if the network device has any other addresses than its # primary address addrs = [] for addr in dbnetdev.all_addresses(): if addr.ip == dbnetdev.primary_ip: continue addrs.append(str(addr.ip)) if addrs: raise ArgumentError("{0} still provides the following addresses, " "delete them first: {1}.".format (dbnetdev, ", ".join(addrs))) dbdns_rec = dbnetdev.primary_name ip = dbnetdev.primary_ip old_fqdn = str(dbnetdev.primary_name.fqdn) old_comments = dbnetdev.comments session.delete(dbnetdev) if dbdns_rec: delete_dns_record(dbdns_rec) session.flush() # Any network device ports hanging off this network device should be deleted with # the cascade delete of the network device. netdev_plenary = Plenary.get_plenary(dbnetdev, logger=logger) # clusters connected to this network device plenaries = PlenaryCollection(logger=logger) for dbcluster in dbnetdev.esx_clusters: plenaries.append(Plenary.get_plenary(dbcluster)) with CompileKey.merge([netdev_plenary.get_key(), plenaries.get_key()]): netdev_plenary.stash() try: plenaries.write(locked=True) netdev_plenary.remove(locked=True) if ip: dsdb_runner = DSDBRunner(logger=logger) # FIXME: restore interface name/MAC on rollback dsdb_runner.delete_host_details(old_fqdn, ip, comments=old_comments) dsdb_runner.commit_or_rollback("Could not remove network device " "from DSDB") except: plenaries.restore_stash() netdev_plenary.restore_stash() raise return
def lookup_target(session, plenaries, hostname, ip, cluster, resourcegroup, service_address, alias): """ Check the parameters of the server providing a given service Look for potential conflicts, and return a dict that is suitable to be passed to either the constructor of ServiceInstanceServer, or to the find_server() function. """ params = {} if cluster and hostname: raise ArgumentError("Only one of --cluster and --hostname may be " "specified.") if alias: dbdns_env = DnsEnvironment.get_unique_or_default(session) dbdns_rec = Alias.get_unique(session, fqdn=alias, dns_environment=dbdns_env, compel=True) params["alias"] = dbdns_rec if hostname: params["host"] = hostname_to_host(session, hostname) plenaries.append(Plenary.get_plenary(params["host"])) if cluster: params["cluster"] = Cluster.get_unique(session, cluster, compel=True) plenaries.append(Plenary.get_plenary(params["cluster"])) if service_address: # TODO: calling get_resource_holder() means doing redundant DB lookups # TODO: it would be nice to also accept an FQDN for the service address, # to be consistent with the usage of the --service_address option in # add_service_address/del_service_address holder = get_resource_holder(session, hostname=hostname, cluster=cluster, resgroup=resourcegroup, compel=True) dbsrv_addr = ServiceAddress.get_unique(session, name=service_address, holder=holder, compel=True) params["service_address"] = dbsrv_addr elif ip: for addr in params["host"].hardware_entity.all_addresses(): if ip != addr.ip: continue if addr.service_address: params["service_address"] = addr.service_address else: params["address_assignment"] = addr break return params
def render(self, session, logger, hostname, cluster, personality, **arguments): dbcluster = Cluster.get_unique(session, cluster, compel=True) dbhost = hostname_to_host(session, hostname) if not dbhost.cluster: raise ArgumentError( "{0} is not bound to a cluster.".format(dbhost)) if dbhost.cluster != dbcluster: raise ArgumentError("{0} is bound to {1:l}, not {2:l}.".format( dbhost, dbhost.cluster, dbcluster)) if personality: dbpersonality = Personality.get_unique(session, name=personality, archetype=dbhost.archetype, compel=True) if dbpersonality.cluster_required: raise ArgumentError("Cannot switch host to personality %s " "because that personality requires a " "cluster" % personality) dbhost.personality = dbpersonality elif dbhost.personality.cluster_required: raise ArgumentError("Host personality %s requires a cluster, " "use --personality to change personality " "when leaving the cluster." % dbhost.personality.name) dbcluster.hosts.remove(dbhost) remove_service_addresses(dbcluster, dbhost) dbcluster.validate() session.flush() session.expire(dbhost, ['_cluster']) # Will need to write a cluster plenary and either write or # remove a host plenary. Grab the domain key since the two # must be in the same domain. host_plenary = Plenary.get_plenary(dbhost, logger=logger) cluster_plenary = Plenary.get_plenary(dbcluster, logger=logger) key = CompileKey(domain=dbcluster.branch.name, logger=logger) try: lock_queue.acquire(key) cluster_plenary.write(locked=True) try: host_plenary.write(locked=True) except IncompleteError: host_plenary.cleanup(domain=dbhost.branch.name, locked=True) except: cluster_plenary.restore_stash() host_plenary.restore_stash() raise finally: lock_queue.release(key)
def render(self, session, logger, hostname, cluster, personality, **arguments): dbcluster = Cluster.get_unique(session, cluster, compel=True) dbhost = hostname_to_host(session, hostname) if not dbhost.cluster: raise ArgumentError("{0} is not bound to a cluster.".format(dbhost)) if dbhost.cluster != dbcluster: raise ArgumentError("{0} is bound to {1:l}, not {2:l}.".format( dbhost, dbhost.cluster, dbcluster)) if personality: dbpersonality = Personality.get_unique(session, name=personality, archetype=dbhost.archetype, compel=True) if dbpersonality.cluster_required: raise ArgumentError("Cannot switch host to personality %s " "because that personality requires a " "cluster" % personality) dbhost.personality = dbpersonality elif dbhost.personality.cluster_required: raise ArgumentError("Host personality %s requires a cluster, " "use --personality to change personality " "when leaving the cluster." % dbhost.personality.name) dbcluster.hosts.remove(dbhost) remove_service_addresses(dbcluster, dbhost) dbcluster.validate() session.flush() session.expire(dbhost, ['_cluster']) # Will need to write a cluster plenary and either write or # remove a host plenary. Grab the domain key since the two # must be in the same domain. host_plenary = Plenary.get_plenary(dbhost, logger=logger) cluster_plenary = Plenary.get_plenary(dbcluster, logger=logger) key = CompileKey(domain=dbcluster.branch.name, logger=logger) try: lock_queue.acquire(key) cluster_plenary.write(locked=True) try: host_plenary.write(locked=True) except IncompleteError: host_plenary.cleanup(domain=dbhost.branch.name, locked=True) except: cluster_plenary.restore_stash() host_plenary.restore_stash() raise finally: lock_queue.release(key)
def update_cluster_location(session, logger, dbcluster, fix_location, plenaries, remove_plenaries, **arguments): location_updated = False dblocation = get_location(session, **arguments) if fix_location: dblocation = dbcluster.minimum_location if not dblocation: raise ArgumentError("Cannot infer the cluster location from " "the host locations.") if dblocation: errors = [] if not dblocation.campus: errors.append("{0} is not within a campus.".format(dblocation)) if dbcluster.cluster_type != 'meta': for host in dbcluster.hosts: if host.machine.location != dblocation and \ dblocation not in host.machine.location.parents: errors.append("{0} has location {1}.".format( host, host.machine.location)) else: for cluster in dbcluster.members: if cluster.location_constraint != dblocation and \ dblocation not in cluster.location_constraint.parents: errors.append("{0} has location {1}.".format( cluster, cluster.location_constraint)) if errors: raise ArgumentError("Cannot set {0} location constraint to " "{1}:\n{2}".format(dbcluster, dblocation, "\n".join(errors))) if dbcluster.location_constraint != dblocation: if machine_plenary_will_move(old=dbcluster.location_constraint, new=dblocation): for dbmachine in dbcluster.machines: # This plenary will have a path to the old location. plenary = Plenary.get_plenary(dbmachine, logger=logger) remove_plenaries.append(plenary) dbmachine.location = dblocation session.add(dbmachine) # This plenary will have a path to the new location. plenaries.append(Plenary.get_plenary(dbmachine)) # Update the path to the machine plenary in the # container resource plenaries.append( Plenary.get_plenary(dbmachine.vm_container)) dbcluster.location_constraint = dblocation location_updated = True return location_updated
def update_cluster_location(session, logger, dbcluster, fix_location, plenaries, remove_plenaries, **arguments): location_updated = False dblocation = get_location(session, **arguments) if fix_location: dblocation = dbcluster.minimum_location if not dblocation: raise ArgumentError("Cannot infer the cluster location from " "the host locations.") if dblocation: errors = [] if not dblocation.campus: errors.append("{0} is not within a campus.".format(dblocation)) if dbcluster.cluster_type != 'meta': for host in dbcluster.hosts: if host.machine.location != dblocation and \ dblocation not in host.machine.location.parents: errors.append("{0} has location {1}.".format(host, host.machine.location)) else: for cluster in dbcluster.members: if cluster.location_constraint != dblocation and \ dblocation not in cluster.location_constraint.parents: errors.append("{0} has location {1}.".format(cluster, cluster.location_constraint)) if errors: raise ArgumentError("Cannot set {0} location constraint to " "{1}:\n{2}".format(dbcluster, dblocation, "\n".join(errors))) if dbcluster.location_constraint != dblocation: if machine_plenary_will_move(old=dbcluster.location_constraint, new=dblocation): for dbmachine in dbcluster.machines: # This plenary will have a path to the old location. plenary = Plenary.get_plenary(dbmachine, logger=logger) remove_plenaries.append(plenary) dbmachine.location = dblocation session.add(dbmachine) # This plenary will have a path to the new location. plenaries.append(Plenary.get_plenary(dbmachine)) # Update the path to the machine plenary in the # container resource plenaries.append(Plenary.get_plenary(dbmachine.vm_container)) dbcluster.location_constraint = dblocation location_updated = True return location_updated
def body_virtual_machine(self, lines): pan_assign(lines, "name", self.dbobj.name) machine = self.dbobj.machine pmac = Plenary.get_plenary(machine) pan_assign(lines, "hardware", StructureTemplate(pmac.plenary_template_name)) # One day we may get to the point where this will be required. # FIXME: read the data from the host data template if (machine.host): # we fill this in manually instead of just assigning # 'system' = value("hostname:/system") # because the target host might not actually have a profile. arch = machine.host.archetype os = machine.host.operating_system pn = machine.primary_name.fqdn system = { 'archetype': { 'name': arch.name, 'os': os.name, 'osversion': os.version }, 'build': machine.host.status.name, 'network': { 'hostname': pn.name, 'domainname': pn.dns_domain } } pan_assign(lines, "system", system)
def render(self, session, logger, hostname, service, instance, **arguments): dbhost = hostname_to_host(session, hostname) dbservice = Service.get_unique(session, service, compel=True) dbinstance = get_service_instance(session, dbservice, instance) if dbhost in dbinstance.server_hosts: # FIXME: This should just be a warning. There is currently # no way of returning output that would "do the right thing" # on the client but still show status 200 (OK). # The right thing would generally be writing to stderr for # a CLI (either raw or csv), and some sort of generic error # page for a web client. raise ArgumentError("Server %s is already bound to service %s " "instance %s." % (hostname, service, instance)) # The ordering_list will manage the position for us dbinstance.server_hosts.append(dbhost) session.flush() plenary_info = Plenary.get_plenary(dbinstance, logger=logger) plenary_info.write() # XXX: Need to recompile... return
class CommandRefreshWindowsHosts(BrokerCommand): required_parameters = [] def render(self, session, logger, dryrun, **arguments): containers = set() partial_error = None with SyncKey(data="windows", logger=logger): try: self.refresh_windows_hosts(session, logger, containers) if dryrun: session.rollback() return session.commit() except PartialError, e: if dryrun: raise partial_error = e # All errors were caught before hitting the session, so # keep going with whatever was successful. session.commit() if containers: plenaries = PlenaryCollection(logger=logger) for container in containers: plenaries.append(Plenary.get_plenary(container)) plenaries.write() if partial_error: raise partial_error return
def render(self, session, logger, hostname, service, instance, **arguments): dbhost = hostname_to_host(session, hostname) dbservice = Service.get_unique(session, service, compel=True) dbinstance = get_service_instance(session, dbservice, instance) if dbhost in dbinstance.server_hosts: # FIXME: This should just be a warning. There is currently # no way of returning output that would "do the right thing" # on the client but still show status 200 (OK). # The right thing would generally be writing to stderr for # a CLI (either raw or csv), and some sort of generic error # page for a web client. raise ArgumentError("Server %s is already bound to service %s " "instance %s." % (hostname, service, instance)) # The ordering_list will manage the position for us dbinstance.server_hosts.append(dbhost) session.flush() plenary_info = Plenary.get_plenary(dbinstance, logger=logger) plenary_info.write() # XXX: Need to recompile... return
def render(self, session, logger, metacluster, max_members, fix_location, high_availability, comments, **arguments): dbmetacluster = MetaCluster.get_unique(session, metacluster, compel=True) cluster_updated = False if max_members is not None: current_members = len(dbmetacluster.members) if max_members < current_members: raise ArgumentError("%s has %d clusters bound, which exceeds " "the requested limit %d." % (format(dbmetacluster), current_members, max_members)) dbmetacluster.max_clusters = max_members cluster_updated = True if comments is not None: dbmetacluster.comments = comments cluster_updated = True if high_availability is not None: dbmetacluster.high_availability = high_availability cluster_updated = True # TODO update_cluster_location would update VMs. Metaclusters # will contain VMs in Vulcan2 model. plenaries = PlenaryCollection(logger=logger) remove_plenaries = PlenaryCollection(logger=logger) location_updated = update_cluster_location(session, logger, dbmetacluster, fix_location, plenaries, remove_plenaries, **arguments) if location_updated: cluster_updated = True if not cluster_updated: return session.add(dbmetacluster) session.flush() dbmetacluster.validate() plenary_info = Plenary.get_plenary(dbmetacluster, logger=logger) key = plenary_info.get_write_key() try: lock_queue.acquire(key) plenary_info.write(locked=True) except: plenary_info.restore_stash() raise finally: lock_queue.release(key) return
def render(self, session, logger, hostname, cluster, personality, **arguments): dbcluster = Cluster.get_unique(session, cluster, compel=True) dbhost = hostname_to_host(session, hostname) if not dbhost.cluster: raise ArgumentError("{0} is not bound to a cluster.".format(dbhost)) if dbhost.cluster != dbcluster: raise ArgumentError("{0} is bound to {1:l}, not {2:l}.".format( dbhost, dbhost.cluster, dbcluster)) if personality: dbpersonality = Personality.get_unique(session, name=personality, archetype=dbhost.archetype, compel=True) if dbpersonality.cluster_required: raise ArgumentError("Cannot switch host to personality %s " "because that personality requires a " "cluster" % personality) dbhost.personality = dbpersonality elif dbhost.personality.cluster_required: raise ArgumentError("Host personality %s requires a cluster, " "use --personality to change personality " "when leaving the cluster." % dbhost.personality.name) dbcluster.hosts.remove(dbhost) remove_service_addresses(dbcluster, dbhost) dbcluster.validate() session.flush() session.expire(dbhost, ['_cluster']) host_plenary = Plenary.get_plenary(dbhost, logger=logger) cluster_plenary = Plenary.get_plenary(dbcluster, logger=logger) with CompileKey.merge([host_plenary.get_key(), cluster_plenary.get_key()]): try: cluster_plenary.write(locked=True) try: host_plenary.write(locked=True) except IncompleteError: host_plenary.remove(locked=True) except: cluster_plenary.restore_stash() host_plenary.restore_stash() raise
def render(self, session, logger, machine, dbuser, **arguments): dbmachine = Machine.get_unique(session, machine, compel=True) remove_plenaries = PlenaryCollection(logger=logger) remove_plenaries.append(Plenary.get_plenary(dbmachine)) if dbmachine.vm_container: remove_plenaries.append(Plenary.get_plenary( dbmachine.vm_container)) dbcontainer = dbmachine.vm_container.holder.holder_object else: dbcontainer = None if dbmachine.host: raise ArgumentError("{0} is still in use by {1:l} and cannot be " "deleted.".format(dbmachine, dbmachine.host)) addrs = [] for addr in dbmachine.all_addresses(): addrs.append("%s: %s" % (addr.logical_name, addr.ip)) if addrs: addrmsg = ", ".join(addrs) raise ArgumentError("{0} still provides the following addresses, " "delete them first: {1}.".format( dbmachine, addrmsg)) session.delete(dbmachine) session.flush() key = remove_plenaries.get_remove_key() if dbcontainer: plenary_container = Plenary.get_plenary(dbcontainer, logger=logger) key = CompileKey.merge([key, plenary_container.get_write_key()]) try: lock_queue.acquire(key) remove_plenaries.stash() if dbcontainer: plenary_container.write(locked=True) remove_plenaries.remove(locked=True) except: remove_plenaries.restore_stash() if dbcontainer: plenary_container.restore_stash() raise finally: lock_queue.release(key) return
def update_cluster_location(session, logger, dbcluster, fix_location, plenaries, **arguments): dblocation = get_location(session, **arguments) if fix_location: dblocation = dbcluster.minimum_location if not dblocation: raise ArgumentError("Cannot infer the cluster location from " "the host locations.") if dblocation: errors = [] if not dblocation.campus: errors.append("{0} is not within a campus.".format(dblocation)) if not isinstance(dbcluster, MetaCluster): for host in dbcluster.hosts: if host.hardware_entity.location != dblocation and \ dblocation not in host.hardware_entity.location.parents: errors.append("{0} has location {1}." .format(host, host.hardware_entity.location)) else: for cluster in dbcluster.members: if cluster.location_constraint != dblocation and \ dblocation not in cluster.location_constraint.parents: errors.append("{0} has location {1}." .format(cluster, cluster.location_constraint)) if errors: raise ArgumentError("Cannot set {0} location constraint to " "{1}:\n{2}".format(dbcluster, dblocation, "\n".join(errors))) if dbcluster.location_constraint != dblocation: for dbmachine in dbcluster.virtual_machines: # The plenary objects should be created before changing the # location, so they can track the change plenaries.append(Plenary.get_plenary(dbmachine, logger=logger)) # Update the path to the machine plenary in the container # resource plenaries.append(Plenary.get_plenary(dbmachine.vm_container)) dbmachine.location = dblocation dbcluster.location_constraint = dblocation return
def render(self, session, logger, machine, dbuser, **arguments): dbmachine = Machine.get_unique(session, machine, compel=True) remove_plenaries = PlenaryCollection(logger=logger) remove_plenaries.append(Plenary.get_plenary(dbmachine)) if dbmachine.vm_container: remove_plenaries.append(Plenary.get_plenary(dbmachine.vm_container)) dbcontainer = dbmachine.vm_container.holder.holder_object else: dbcontainer = None if dbmachine.host: raise ArgumentError("{0} is still in use by {1:l} and cannot be " "deleted.".format(dbmachine, dbmachine.host)) addrs = [] for addr in dbmachine.all_addresses(): addrs.append("%s: %s" % (addr.logical_name, addr.ip)) if addrs: addrmsg = ", ".join(addrs) raise ArgumentError("{0} still provides the following addresses, " "delete them first: {1}.".format(dbmachine, addrmsg)) session.delete(dbmachine) session.flush() key = remove_plenaries.get_remove_key() if dbcontainer: plenary_container = Plenary.get_plenary(dbcontainer, logger=logger) key = CompileKey.merge([key, plenary_container.get_write_key()]) try: lock_queue.acquire(key) remove_plenaries.stash() if dbcontainer: plenary_container.write(locked=True) remove_plenaries.remove(locked=True) except: remove_plenaries.restore_stash() if dbcontainer: plenary_container.restore_stash() raise finally: lock_queue.release(key) return
def del_cluster(session, logger, dbcluster, config): cluster = str(dbcluster.name) if hasattr(dbcluster, 'members') and dbcluster.members: raise ArgumentError("%s is still in use by clusters: %s." % (format(dbcluster), ", ".join([c.name for c in dbcluster.members]))) elif dbcluster.hosts: hosts = ", ".join([h.fqdn for h in dbcluster.hosts]) raise ArgumentError("%s is still in use by hosts: %s." % (format(dbcluster), hosts)) cluster_plenary = Plenary.get_plenary(dbcluster, logger=logger) resources = PlenaryCollection(logger=logger) if dbcluster.resholder: for res in dbcluster.resholder.resources: resources.append(Plenary.get_plenary(res)) domain = dbcluster.branch.name session.delete(dbcluster) session.flush() key = cluster_plenary.get_remove_key() with CompileKey.merge([key, resources.get_remove_key()]): cluster_plenary.cleanup(domain, locked=True) # And we also want to remove the profile itself profiles = config.get("broker", "profilesdir") # Only one of these should exist, but it doesn't hurt # to try to clean up both. xmlfile = os.path.join(profiles, "clusters", cluster + ".xml") remove_file(xmlfile, logger=logger) xmlgzfile = xmlfile + ".gz" remove_file(xmlgzfile, logger=logger) # And the cached template created by ant remove_file(os.path.join(config.get("broker", "quattordir"), "objects", "clusters", cluster + TEMPLATE_EXTENSION), logger=logger) resources.remove(locked=True) build_index(config, session, profiles, logger=logger) return
def render(self, session, logger, service, instance, position, hostname, cluster, ip, resourcegroup, service_address, alias, **arguments): # Check for invalid combinations. We allow binding as a server: # - a host, in which case the primary IP address will be used # - an auxiliary IP address of a host # - a service address of a host # - a service address of a cluster if ip: if cluster or not hostname: raise ArgumentError("Using the --ip option requires --hostname" "to be specified.") if cluster and not service_address: raise ArgumentError("Binding a cluster requires --service_address " "to be specified.") dbservice = Service.get_unique(session, service, compel=True) dbinstance = get_service_instance(session, dbservice, instance) plenaries = PlenaryCollection(logger=logger) plenaries.append(Plenary.get_plenary(dbinstance)) params = lookup_target(session, plenaries, hostname, ip, cluster, resourcegroup, service_address, alias) # TODO: someday we should verify that the target really points to the # host/cluster specified by the other options if "alias" in params and ("host" in params or "cluster" in params): logger.client_info("Warning: when using --alias, it is your " "responsibility to ensure it really points to " "the host/cluster you've specified - the broker " "does not verify that.") with session.no_autoflush: dbsrv = find_server(dbinstance, params) if dbsrv: raise ArgumentError("The server binding already exists.") dbsrv = ServiceInstanceServer(**params) # The ordering_list will manage the position for us if position is not None: dbinstance.servers.insert(position, dbsrv) else: dbinstance.servers.append(dbsrv) if dbsrv.host: session.expire(dbsrv.host, ['services_provided']) if dbsrv.cluster: session.expire(dbsrv.cluster, ['services_provided']) session.flush() plenaries.write() return
def del_cluster(session, logger, dbcluster, config): cluster = str(dbcluster.name) if hasattr(dbcluster, 'members') and dbcluster.members: raise ArgumentError( "%s is still in use by clusters: %s." % (format(dbcluster), ", ".join([c.name for c in dbcluster.members]))) elif dbcluster.hosts: hosts = ", ".join([h.fqdn for h in dbcluster.hosts]) raise ArgumentError("%s is still in use by hosts: %s." % (format(dbcluster), hosts)) cluster_plenary = Plenary.get_plenary(dbcluster, logger=logger) resources = PlenaryCollection(logger=logger) if dbcluster.resholder: for res in dbcluster.resholder.resources: resources.append(Plenary.get_plenary(res)) domain = dbcluster.branch.name session.delete(dbcluster) session.flush() key = cluster_plenary.get_remove_key() with CompileKey.merge([key, resources.get_remove_key()]): cluster_plenary.cleanup(domain, locked=True) # And we also want to remove the profile itself profiles = config.get("broker", "profilesdir") # Only one of these should exist, but it doesn't hurt # to try to clean up both. xmlfile = os.path.join(profiles, "clusters", cluster + ".xml") remove_file(xmlfile, logger=logger) xmlgzfile = xmlfile + ".gz" remove_file(xmlgzfile, logger=logger) # And the cached template created by ant remove_file(os.path.join(config.get("broker", "quattordir"), "objects", "clusters", cluster + TEMPLATE_EXTENSION), logger=logger) resources.remove(locked=True) build_index(config, session, profiles, logger=logger) return
def render(self, session, logger, service, max_clients, default, **arguments): dbservice = Service.get_unique(session, name=service, compel=True) if default: dbservice.max_clients = None elif max_clients is not None: dbservice.max_clients = max_clients else: raise ArgumentError("Missing --max_clients or --default argument " "to update service %s." % dbservice.name) session.add(dbservice) session.flush() plenaries = PlenaryCollection() plenaries.append(Plenary.get_plenary(dbservice)) for dbinstance in dbservice.instances: plenaries.append(Plenary.get_plenary(dbinstance)) plenaries.write() return
def render(self, session, logger, service, need_client_list, comments, **arguments): Service.get_unique(session, service, preclude=True) dbservice = Service(name=service, comments=comments, need_client_list=need_client_list) session.add(dbservice) plenaries = PlenaryCollection(logger=logger) plenaries.append(Plenary.get_plenary(dbservice)) session.flush() plenaries.write() return
def render(self, session, logger, service, max_clients, default, **arguments): dbservice = Service.get_unique(session, name=service, compel=True) if default: dbservice.max_clients = None elif max_clients is not None: dbservice.max_clients = max_clients else: raise ArgumentError("Missing --max_clients or --default argument " "to update service %s." % dbservice.name) session.add(dbservice) session.flush() plenaries = PlenaryCollection() plenaries.append(Plenary.get_plenary(dbservice)) for dbinstance in dbservice.instances: plenaries.append(Plenary.get_plenary(dbinstance)) plenaries.write() return
def get_key(self): host = self.dbobj.host container = self.dbobj.vm_container # Need a compile key if: # - There is a host attached. # - This is a virtual machine in a container. if not host and not container: return None # We have at least host or container, maybe both... if host: # PlenaryHost is actually a PlenaryCollection... can't call # get_key() directly, so using get_remove_key(). ph = Plenary.get_plenary(host, logger=self.logger) host_key = ph.get_remove_key() if container: pc = Plenary.get_plenary(container, self.logger) container_key = pc.get_key() if not container: return host_key if not host: return container_key return CompileKey.merge([host_key, container_key])
def get_key(self): host = self.dbobj.host container = self.dbobj.vm_container # Need a compile key if: # - There is a host attached. # - This is a virtual machine in a container. if not host and not container: return None # We have at least host or container, maybe both... if host: # PlenaryHost is actually a PlenaryCollection... can't call # get_key() directly, so using get_remove_key(). ph = Plenary.get_plenary(host, logger=self.logger) host_key = ph.get_remove_key() if container: pc = Plenary.get_plenary(container, self.logger) container_key = pc.get_key() if not container: return host_key if not host: return container_key return CompileKey.merge([host_key, container_key])
def render(self, session, logger, list, domain, sandbox, force, **arguments): dbbranch, dbauthor = get_branch_and_author(session, logger, domain=domain, sandbox=sandbox, compel=True) if hasattr(dbbranch, "allow_manage") and not dbbranch.allow_manage: raise ArgumentError("Managing hosts to {0:l} is not allowed." .format(dbbranch)) check_hostlist_size(self.command, self.config, list) dbhosts = hostlist_to_hosts(session, list) failed = [] dbsource, dbsource_author = validate_branch_author(dbhosts) for dbhost in dbhosts: # check if any host in the list is a cluster node if dbhost.cluster: failed.append("Cluster nodes must be managed at the " "cluster level; {0} is a member of {1:l}." .format(dbhost.fqdn, dbhost.cluster)) if failed: raise ArgumentError("Cannot modify the following hosts:\n%s" % "\n".join(failed)) if not force: validate_branch_commits(dbsource, dbsource_author, dbbranch, dbauthor, logger, self.config) plenaries = PlenaryCollection(logger=logger) for dbhost in dbhosts: plenaries.append(Plenary.get_plenary(dbhost)) dbhost.branch = dbbranch dbhost.sandbox_author = dbauthor session.flush() # We're crossing domains, need to lock everything. with CompileKey.merge([CompileKey(domain=dbsource.name, logger=logger), CompileKey(domain=dbbranch.name, logger=logger)]): plenaries.stash() try: plenaries.write(locked=True) except: plenaries.restore_stash() raise return
def remove_prev(self, session, logger, prev, pending_removals): """Remove the interface 'prev' and its host and machine.""" # This should probably be re-factored to call code used elsewhere. # The below seems too simple to warrant that, though... logger.info( "Removing blind host '%s', machine '%s', " "and interface '%s'" % (prev.hardware_entity.fqdn, prev.hardware_entity.label, prev.name) ) host_plenary_info = Plenary.get_plenary(prev.hardware_entity.host, logger=logger) # FIXME: Should really do everything that del_host.py does, not # just remove the host plenary but adjust all the service # plenarys and dependency files. pending_removals.append(host_plenary_info) dbmachine = prev.hardware_entity machine_plenary_info = Plenary.get_plenary(dbmachine, logger=logger) pending_removals.append(machine_plenary_info) # This will cascade to prev & the host if dbmachine.primary_name: dbdns_rec = dbmachine.primary_name dbmachine.primary_name = None delete_dns_record(dbdns_rec) session.delete(dbmachine) session.flush()
def render(self, session, logger, machine, disk, controller, size, all, **arguments): dbmachine = Machine.get_unique(session, machine, compel=True) q = session.query(Disk).filter_by(machine=dbmachine) if disk: q = q.filter_by(device_name=disk) if controller: if controller not in controller_types: raise ArgumentError("%s is not a valid controller type, use " "one of: %s." % (controller, ", ".join(controller_types) )) q = q.filter_by(controller_type=controller) if size is not None: q = q.filter_by(capacity=size) results = q.all() if len(results) == 0: raise NotFoundException("No disks found.") elif len(results) > 1 and not all: raise ArgumentError("More than one matching disks found. " "Use --all to delete them all.") for result in results: session.delete(result) session.flush() session.expire(dbmachine, ['disks']) plenaries = PlenaryCollection(logger=logger) plenaries.append(Plenary.get_plenary(dbmachine)) dbcontainer = dbmachine.vm_container if dbcontainer: plenaries.append(Plenary.get_plenary(dbcontainer, logger=logger)) plenaries.write() return
def render(self, session, logger, cluster, buildstatus, **arguments): dbcluster = Cluster.get_unique(session, cluster, compel=True) dbstatus = ClusterLifecycle.get_unique(session, buildstatus, compel=True) if not dbcluster.status.transition(dbcluster, dbstatus): return if not dbcluster.personality.archetype.is_compileable: return session.flush() plenaries = PlenaryCollection(logger=logger) plenaries.append(Plenary.get_plenary(dbcluster)) for dbhost in dbcluster.hosts: plenaries.append(Plenary.get_plenary(dbhost)) # Force a host lock as pan might overwrite the profile... key = CompileKey(domain=dbcluster.branch.name, logger=logger) try: lock_queue.acquire(key) plenaries.write(locked=True) td = TemplateDomain(dbcluster.branch, dbcluster.sandbox_author, logger=logger) td.compile(session, plenaries.object_templates, locked=True) except: plenaries.restore_stash() raise finally: lock_queue.release(key) return
def render(self, session, logger, hostname, data, generate, **arguments): dbhost = hostname_to_host(session, hostname) dbresource = get_resource(session, dbhost, **arguments) if dbresource: plenary_info = Plenary.get_plenary(dbresource, logger=logger) else: if data: plenary_info = PlenaryHostData(dbhost, logger=logger) else: plenary_info = PlenaryToplevelHost(dbhost, logger=logger) if generate: return plenary_info._generate_content() else: return plenary_info.read()
def remove_prev(self, session, logger, prev, pending_removals): """Remove the interface 'prev' and its host and machine.""" # This should probably be re-factored to call code used elsewhere. # The below seems too simple to warrant that, though... logger.info( "Removing blind host '%s', machine '%s', " "and interface '%s'" % (prev.hardware_entity.fqdn, prev.hardware_entity.label, prev.name)) host_plenary_info = Plenary.get_plenary(prev.hardware_entity.host, logger=logger) # FIXME: Should really do everything that del_host.py does, not # just remove the host plenary but adjust all the service # plenarys and dependency files. pending_removals.append(host_plenary_info) dbmachine = prev.hardware_entity machine_plenary_info = Plenary.get_plenary(dbmachine, logger=logger) pending_removals.append(machine_plenary_info) # This will cascade to prev & the host if dbmachine.primary_name: dbdns_rec = dbmachine.primary_name dbmachine.primary_name = None delete_dns_record(dbdns_rec) session.delete(dbmachine) session.flush()
def render(self, session, logger, service, max_clients, default, need_client_list, comments, **arguments): dbservice = Service.get_unique(session, name=service, compel=True) if default: dbservice.max_clients = None elif max_clients is not None: dbservice.max_clients = max_clients if need_client_list is not None: dbservice.need_client_list = need_client_list if comments is not None: dbservice.comments = comments session.flush() plenaries = PlenaryCollection(logger=logger) plenaries.append(Plenary.get_plenary(dbservice)) for dbinstance in dbservice.instances: plenaries.append(Plenary.get_plenary(dbinstance)) plenaries.write() return
def render(self, session, logger, service, instance, comments, **arguments): dbservice = Service.get_unique(session, service, compel=True) ServiceInstance.get_unique(session, service=dbservice, name=instance, preclude=True) dbsi = ServiceInstance(service=dbservice, name=instance, comments=comments) session.add(dbsi) plenaries = PlenaryCollection(logger=logger) plenaries.append(Plenary.get_plenary(dbsi)) session.flush() plenaries.write() return
def render(self, session, logger, service, instance, max_clients, default, comments, **arguments): dbservice = Service.get_unique(session, name=service, compel=True) dbsi = ServiceInstance.get_unique(session, service=dbservice, name=instance, compel=True) if default: dbsi.max_clients = None elif max_clients is not None: dbsi.max_clients = max_clients if comments is not None: dbsi.comments = comments session.flush() plenary = Plenary.get_plenary(dbsi, logger=logger) plenary.write() return
def render(self, session, logger, cluster, service, instance, **arguments): dbservice = Service.get_unique(session, service, compel=True) dbinstance = get_service_instance(session, dbservice, instance) dbcluster = Cluster.get_unique(session, cluster, compel=True) if dbinstance not in dbcluster.service_bindings: raise NotFoundException("{0} is not bound to {1:l}." .format(dbinstance, dbcluster)) if dbservice in dbcluster.required_services: raise ArgumentError("Cannot remove cluster service instance " "binding for %s cluster aligned service %s." % (dbcluster.cluster_type, dbservice.name)) dbcluster.service_bindings.remove(dbinstance) session.flush() plenary = Plenary.get_plenary(dbcluster, logger=logger) plenary.write() return
def render(self, session, logger, rack, row, column, room, building, bunker, fullname, default_dns_domain, comments, **arguments): dbrack = get_location(session, rack=rack) if row is not None: dbrack.rack_row = row if column is not None: dbrack.rack_column = column if fullname is not None: dbrack.fullname = fullname if comments is not None: dbrack.comments = comments if default_dns_domain is not None: if default_dns_domain: dbdns_domain = DnsDomain.get_unique(session, default_dns_domain, compel=True) dbrack.default_dns_domain = dbdns_domain else: dbrack.default_dns_domain = None if bunker or room or building: dbparent = get_location(session, bunker=bunker, room=room, building=building) # This one would change the template's locations hence forbidden if dbparent.building != dbrack.building: # Doing this both to reduce user error and to limit # testing required. raise ArgumentError("Cannot change buildings. {0} is in {1} " "while {2} is in {3}.".format( dbparent, dbparent.building, dbrack, dbrack.building)) dbrack.update_parent(parent=dbparent) session.flush() plenaries = PlenaryCollection(logger=logger) q = session.query(Machine) q = q.filter(Machine.location_id.in_(dbrack.offspring_ids())) for dbmachine in q: plenaries.append(Plenary.get_plenary(dbmachine)) plenaries.write()
def render(self, session, logger, service, instance, max_clients, default, **arguments): dbservice = Service.get_unique(session, name=service, compel=True) dbsi = ServiceInstance.get_unique(session, service=dbservice, name=instance, compel=True) if default: dbsi.max_clients = None elif max_clients is not None: dbsi.max_clients = max_clients else: raise ArgumentError("Missing --max_clients or --default argument " "to update service %s instance %s." % (dbservice.name, dbsi.name)) session.add(dbsi) session.flush() plenary = Plenary.get_plenary(dbsi, logger=logger) plenary.write() return
def render(self, session, logger, cluster, data, generate, **arguments): dbcluster = Cluster.get_unique(session, cluster, compel=True) dbresource = get_resource(session, dbcluster, **arguments) if dbresource: plenary_info = Plenary.get_plenary(dbresource, logger=logger) else: if isinstance(dbcluster, MetaCluster): if data: plenary_info = PlenaryMetaClusterData(dbcluster, logger=logger) else: plenary_info = PlenaryMetaClusterObject(dbcluster, logger=logger) else: if data: plenary_info = PlenaryClusterData(dbcluster, logger=logger) else: plenary_info = PlenaryClusterObject(dbcluster, logger=logger) if generate: return plenary_info._generate_content() else: return plenary_info.read()
def render(self, session, logger, service, instance, **arguments): dbservice = Service.get_unique(session, service, compel=True) dbsi = get_service_instance(session, dbservice, instance) if dbsi.client_count > 0: raise ArgumentError( "Service %s, instance %s still has clients and " "cannot be deleted." % (dbservice.name, dbsi.name)) if dbsi.server_hosts: msg = ", ".join([host.fqdn for host in dbsi.server_hosts]) raise ArgumentError("Service %s, instance %s is still being " "provided by servers: %s." % (dbservice.name, dbsi.name, msg)) # Depend on cascading to remove any mappings session.delete(dbsi) session.flush() plenary_info = Plenary.get_plenary(dbsi, logger=logger) plenary_info.remove() return
def render(self, session, logger, service, instance, max_clients, default, **arguments): dbservice = Service.get_unique(session, name=service, compel=True) dbsi = ServiceInstance.get_unique(session, service=dbservice, name=instance, compel=True) if default: dbsi.max_clients = None elif max_clients is not None: dbsi.max_clients = max_clients else: raise ArgumentError("Missing --max_clients or --default argument " "to update service %s instance %s." % (dbservice.name, dbsi.name)) session.add(dbsi) session.flush() plenary = Plenary.get_plenary(dbsi, logger=logger) plenary.write() return
def consolidate_names(self, session, logger, dbmachine, dummy_machine_name, pending_removals): short = dbmachine.label[:-1] if short != dummy_machine_name[:-1]: logger.client_info("Not altering name of machine %s, name of " "machine being removed %s is too different." % (dbmachine.label, dummy_machine_name)) return if not dbmachine.label[-1].isalpha(): logger.client_info("Not altering name of machine %s, name does " "not end with a letter." % dbmachine.label) return if session.query(Machine).filter_by(label=short).first(): logger.client_info("Not altering name of machine %s, target " "name %s is already in use." % (dbmachine.label, short)) return logger.client_info("Renaming machine %s to %s." % (dbmachine.label, short)) pending_removals.append(Plenary.get_plenary(dbmachine)) dbmachine.label = short session.add(dbmachine) session.flush()
def render(self, session, logger, hostname, cluster, personality, **arguments): dbhost = hostname_to_host(session, hostname) dbcluster = Cluster.get_unique(session, cluster, compel=True) if dbcluster.status.name == 'decommissioned': raise ArgumentError("Cannot add hosts to decommissioned clusters.") # We only support changing personality within the same # archetype. The archetype decides things like which OS, how # it builds (dhcp, etc), whether it's compilable, and # switching all of that by side-effect seems wrong # somehow. And besides, it would make the user-interface and # implementation for this command ugly in order to support # changing all of those options. personality_change = False if personality is not None: dbpersonality = Personality.get_unique(session, name=personality, archetype=dbhost.archetype, compel=True) if dbhost.personality != dbpersonality: dbhost.personality = dbpersonality personality_change = True # Allow for non-restricted clusters (the default?) if (len(dbcluster.allowed_personalities) > 0 and dbhost.personality not in dbcluster.allowed_personalities): raise ArgumentError( "The personality %s for %s is not allowed " "by the cluster. Specify --personality " "and provide one of %s" % (dbhost.personality, dbhost.fqdn, ", ".join( [x.name for x in dbcluster.allowed_personalities]))) # Now that we've changed the personality, we can check # if this is a valid membership change dbcluster.validate_membership(dbhost) plenaries = PlenaryCollection(logger=logger) plenaries.append(Plenary.get_plenary(dbcluster)) if dbhost.cluster and dbhost.cluster != dbcluster: logger.client_info("Removing {0:l} from {1:l}.".format( dbhost, dbhost.cluster)) old_cluster = dbhost.cluster old_cluster.hosts.remove(dbhost) remove_service_addresses(old_cluster, dbhost) old_cluster.validate() session.expire(dbhost, ['_cluster']) plenaries.append(Plenary.get_plenary(old_cluster)) # Apply the service addresses to the new member for res in walk_resources(dbcluster): if not isinstance(res, ServiceAddress): continue apply_service_address(dbhost, res.interfaces, res) if dbhost.cluster: if personality_change: raise ArgumentError( "{0:l} already in {1:l}, use " "aq reconfigure to change personality.".format( dbhost, dbhost.cluster)) # the cluster has not changed, therefore there's nothing # to do here. return # Calculate the node index: build a map of all possible values, remove # the used ones, and pick the smallest remaining one node_index_map = set(xrange(len(dbcluster._hosts) + 1)) for link in dbcluster._hosts: # The cluster may have been bigger in the past, so node indexes may # be larger than the current cluster size try: node_index_map.remove(link.node_index) except KeyError: pass dbcluster.hosts.append((dbhost, min(node_index_map))) dbcluster.validate() # demote a host when switching clusters # promote a host when switching clusters if dbhost.status.name == 'ready': if dbcluster.status.name != 'ready': dbalmost = HostLifecycle.get_unique(session, 'almostready', compel=True) dbhost.status.transition(dbhost, dbalmost) plenaries.append(Plenary.get_plenary(dbhost)) elif dbhost.status.name == 'almostready': if dbcluster.status.name == 'ready': dbready = HostLifecycle.get_unique(session, 'ready', compel=True) dbhost.status.transition(dbhost, dbready) plenaries.append(Plenary.get_plenary(dbhost)) session.flush() # Enforce that service instances are set correctly for the # new cluster association. chooser = Chooser(dbhost, logger=logger) chooser.set_required() chooser.flush_changes() # the chooser will include the host plenary key = CompileKey.merge( [chooser.get_write_key(), plenaries.get_write_key()]) try: lock_queue.acquire(key) chooser.write_plenary_templates(locked=True) plenaries.write(locked=True) except: chooser.restore_stash() plenaries.restore_stash() raise finally: lock_queue.release(key) return
def render(self, session, logger, services, personalities, machines, clusters, hosts, locations, resources, switches, all, **arguments): success = [] failed = [] written = 0 # Caches for keeping preloaded data pinned in memory, since the SQLA # session holds a weak reference only resource_by_id = {} resholder_by_id = {} service_instances = None racks = None # Object caches that are accessed directly disks_by_machine = defaultdict(list) interfaces_by_machine = defaultdict(list) interfaces_by_id = {} if all: services = True personalities = True machines = True clusters = True hosts = True locations = True resources = True with CompileKey(logger=logger): logger.client_info("Loading data.") # When flushing clusters/hosts, loading the resource holder is done # as the query that loads those objects. But when flushing resources # only, we need the holder and the object it belongs to. if resources and not clusters: q = session.query(ClusterResource) # Using joinedload('cluster') would generate an outer join q = q.join(Cluster) q = q.options(contains_eager('cluster')) for resholder in q: resholder_by_id[resholder.id] = resholder if resources and not hosts: q = session.query(HostResource) # Using joinedload('host') would generate an outer join q = q.join(Host) q = q.options(contains_eager('host')) for resholder in q: resholder_by_id[resholder.id] = resholder if hosts or clusters or resources: # Load the most common resource types. Using # with_polymorphic('*') on Resource would generate a huge query, # so do something more targeted. More resource subclasses may be # added later if they become common. preload_classes = { Filesystem: [], RebootSchedule: [], VirtualMachine: [ joinedload('machine'), joinedload('machine.primary_name'), joinedload('machine.primary_name.fqdn') ], Share: [], } share_info = cache_storage_data() for cls, options in preload_classes.items(): q = session.query(cls) # If only hosts or only clusters are needed, don't load # resources of the other kind if hosts and not clusters and not resources: q = q.join(ResourceHolder) q = q.options(contains_eager('holder')) q = q.filter_by(holder_type='host') if clusters and not hosts and not resources: q = q.join(ResourceHolder) q = q.filter_by(holder_type='cluster') q = q.options(contains_eager('holder')) if options: q = q.options(*options) for res in q: resource_by_id[res.id] = res try: res.populate_share_info(share_info) except AttributeError: pass if hosts or machines: # Polymorphic loading cannot be applied to eager-loaded # attributes, so load interfaces manually. q = session.query(Interface) q = q.with_polymorphic('*') q = q.options(lazyload("hardware_entity")) for iface in q: interfaces_by_machine[iface.hardware_entity_id].append( iface) interfaces_by_id[iface.id] = iface if hosts: # subqueryload() and with_polymorphic() do not play nice # together, so do it by hand q = session.query(AddressAssignment) q = q.options(joinedload("network"), joinedload("dns_records")) q = q.order_by(AddressAssignment._label) addrs_by_iface = defaultdict(list) for addr in q: addrs_by_iface[addr.interface_id].append(addr) for interface_id, addrs in addrs_by_iface.items(): set_committed_value(interfaces_by_id[interface_id], "assignments", addrs) q = session.query(Interface.id) q = q.filter(~Interface.assignments.any()) for id in q.all(): set_committed_value(interfaces_by_id[id[0]], "assignments", None) if hosts or services: q = session.query(ServiceInstance) q = q.options(subqueryload("service")) service_instances = q.all() if machines or clusters: # Most machines are in racks... q = session.query(Rack) q = q.options(subqueryload("dns_maps"), subqueryload("parents")) racks = q.all() if locations: logger.client_info("Flushing locations.") for dbloc in session.query(City).all(): try: plenary = Plenary.get_plenary(dbloc, logger=logger) written += plenary.write(locked=True) except Exception, e: failed.append("City %s failed: %s" % dbloc, e) continue if services: logger.client_info("Flushing services.") q = session.query(Service) q = q.options(subqueryload("instances")) for dbservice in q: try: plenary_info = Plenary.get_plenary(dbservice, logger=logger) written += plenary_info.write(locked=True) except Exception, e: failed.append("Service %s failed: %s" % (dbservice.name, e)) continue for dbinst in dbservice.instances: try: plenary_info = Plenary.get_plenary(dbinst, logger=logger) written += plenary_info.write(locked=True) except Exception, e: failed.append("Service %s instance %s failed: %s" % (dbservice.name, dbinst.name, e)) continue
def render(self, session, logger, switch, model, rack, type, ip, vendor, serial, rename_to, discovered_macs, clear, discover, comments, **arguments): dbswitch = Switch.get_unique(session, switch, compel=True) oldinfo = DSDBRunner.snapshot_hw(dbswitch) if discover: discover_switch(session, logger, self.config, dbswitch, False) if vendor and not model: model = dbswitch.model.name if model: dbmodel = Model.get_unique(session, name=model, vendor=vendor, machine_type='switch', compel=True) dbswitch.model = dbmodel dblocation = get_location(session, rack=rack) if dblocation: dbswitch.location = dblocation if serial is not None: dbswitch.serial_no = serial # FIXME: What do the error messages for an invalid enum (switch_type) # look like? if type: Switch.check_type(type) dbswitch.switch_type = type if ip: update_primary_ip(session, dbswitch, ip) if comments is not None: dbswitch.comments = comments remove_plenary = None if rename_to: # Handling alias renaming would not be difficult in AQDB, but the # DSDB synchronization would be painful, so don't do that for now. # In theory we should check all configured IP addresses for aliases, # but this is the most common case if dbswitch.primary_name and dbswitch.primary_name.fqdn.aliases: raise ArgumentError( "The switch has aliases and it cannot be " "renamed. Please remove all aliases first.") remove_plenary = Plenary.get_plenary(dbswitch, logger=logger) rename_hardware(session, dbswitch, rename_to) if clear: session.query(ObservedMac).filter_by(switch=dbswitch).delete() if discovered_macs: now = datetime.now() for (macaddr, port) in discovered_macs: update_or_create_observed_mac(session, dbswitch, port, macaddr, now) session.flush() switch_plenary = Plenary.get_plenary(dbswitch, logger=logger) key = switch_plenary.get_write_key() if remove_plenary: key = CompileKey.merge([key, remove_plenary.get_remove_key()]) try: lock_queue.acquire(key) if remove_plenary: remove_plenary.stash() remove_plenary.remove(locked=True) switch_plenary.write(locked=True) dsdb_runner = DSDBRunner(logger=logger) dsdb_runner.update_host(dbswitch, oldinfo) dsdb_runner.commit_or_rollback("Could not update switch in DSDB") except: if remove_plenary: remove_plenary.restore_stash() switch_plenary.restore_stash() raise finally: lock_queue.release(key) return
def render(self, session, logger, hostname, machine, archetype, domain, sandbox, osname, osversion, buildstatus, personality, comments, zebra_interfaces, grn, eon_id, skip_dsdb_check=False, **arguments): dbarchetype = Archetype.get_unique(session, archetype, compel=True) section = "archetype_" + dbarchetype.name # This is for the various add_*_host commands if not domain and not sandbox: domain = self.config.get(section, "host_domain") (dbbranch, dbauthor) = get_branch_and_author(session, logger, domain=domain, sandbox=sandbox, compel=True) if hasattr(dbbranch, "allow_manage") and not dbbranch.allow_manage: raise ArgumentError( "Adding hosts to {0:l} is not allowed.".format(dbbranch)) if not buildstatus: buildstatus = 'build' dbstatus = HostLifecycle.get_unique(session, buildstatus, compel=True) dbmachine = Machine.get_unique(session, machine, compel=True) oldinfo = DSDBRunner.snapshot_hw(dbmachine) if not personality: if self.config.has_option(section, "default_personality"): personality = self.config.get(section, "default_personality") else: personality = 'generic' dbpersonality = Personality.get_unique(session, name=personality, archetype=dbarchetype, compel=True) if not osname: if self.config.has_option(section, "default_osname"): osname = self.config.get(section, "default_osname") if not osversion: if self.config.has_option(section, "default_osversion"): osversion = self.config.get(section, "default_osversion") if not osname or not osversion: raise ArgumentError("Can not determine a sensible default OS " "for archetype %s. Please use the " "--osname and --osversion parameters." % (dbarchetype.name)) dbos = OperatingSystem.get_unique(session, name=osname, version=osversion, archetype=dbarchetype, compel=True) if (dbmachine.model.machine_type == 'aurora_node' and dbpersonality.archetype.name != 'aurora'): raise ArgumentError("Machines of type aurora_node can only be " "added with archetype aurora.") if dbmachine.host: raise ArgumentError("{0:c} {0.label} is already allocated to " "{1:l}.".format(dbmachine, dbmachine.host)) if grn or eon_id: dbgrn = lookup_grn(session, grn, eon_id, logger=logger, config=self.config) else: dbgrn = dbpersonality.owner_grn dbhost = Host(machine=dbmachine, branch=dbbranch, owner_grn=dbgrn, sandbox_author=dbauthor, personality=dbpersonality, status=dbstatus, operating_system=dbos, comments=comments) session.add(dbhost) if self.config.has_option("archetype_" + archetype, "default_grn_target"): dbhost.grns.append((dbhost, dbgrn, self.config.get("archetype_" + archetype, "default_grn_target"))) if zebra_interfaces: # --autoip does not make sense for Zebra (at least not the way it's # implemented currently) dbinterface = None else: dbinterface = get_boot_interface(dbmachine) # This method is allowed to return None. This can only happen # (currently) using add_aurora_host, add_windows_host, or possibly by # bypassing the aq client and posting a request directly. audit_results = [] ip = generate_ip(session, logger, dbinterface, audit_results=audit_results, **arguments) dbdns_rec, newly_created = grab_address(session, hostname, ip, allow_restricted_domain=True, allow_reserved=True, preclude=True) dbmachine.primary_name = dbdns_rec # Fix up auxiliary addresses to point to the primary name by default if ip: dns_env = dbdns_rec.fqdn.dns_environment for addr in dbmachine.all_addresses(): if addr.interface.interface_type == "management": continue if addr.service_address_id: # pragma: no cover continue for rec in addr.dns_records: if rec.fqdn.dns_environment == dns_env: rec.reverse_ptr = dbdns_rec.fqdn if zebra_interfaces: if not ip: raise ArgumentError( "Zebra configuration requires an IP address.") dbsrv_addr = self.assign_zebra_address(session, dbmachine, dbdns_rec, zebra_interfaces) else: if ip: if not dbinterface: raise ArgumentError( "You have specified an IP address for the " "host, but {0:l} does not have a bootable " "interface.".format(dbmachine)) assign_address(dbinterface, ip, dbdns_rec.network) dbsrv_addr = None session.flush() plenaries = PlenaryCollection(logger=logger) plenaries.append(Plenary.get_plenary(dbmachine)) if dbmachine.vm_container: plenaries.append(Plenary.get_plenary(dbmachine.vm_container)) if dbsrv_addr: plenaries.append(Plenary.get_plenary(dbsrv_addr)) key = plenaries.get_write_key() try: lock_queue.acquire(key) plenaries.write(locked=True) # XXX: This (and some of the code above) is horrible. There # should be a generic/configurable hook here that could kick # in based on archetype and/or domain. dsdb_runner = DSDBRunner(logger=logger) if dbhost.archetype.name == 'aurora': # For aurora, check that DSDB has a record of the host. if not skip_dsdb_check: try: dsdb_runner.show_host(hostname) except ProcessException, e: raise ArgumentError("Could not find host in DSDB: %s" % e) elif not dbmachine.primary_ip: logger.info("No IP for %s, not adding to DSDB." % dbmachine.fqdn)
def render(self, session, logger, city, timezone, campus, default_dns_domain, comments, **arguments): dbcity = get_location(session, city=city) # Updating machine templates is expensive, so only do that if needed update_machines = False if timezone is not None: dbcity.timezone = timezone if comments is not None: dbcity.comments = comments if default_dns_domain is not None: if default_dns_domain: dbdns_domain = DnsDomain.get_unique(session, default_dns_domain, compel=True) dbcity.default_dns_domain = dbdns_domain else: dbcity.default_dns_domain = None prev_campus = None dsdb_runner = None dsdb_runner = DSDBRunner(logger=logger) if campus is not None: dbcampus = get_location(session, campus=campus) # This one would change the template's locations hence forbidden if dbcampus.hub != dbcity.hub: # Doing this both to reduce user error and to limit # testing required. raise ArgumentError("Cannot change campus. {0} is in {1:l}, " "while {2:l} is in {3:l}.".format( dbcampus, dbcampus.hub, dbcity, dbcity.hub)) if dbcity.campus: prev_campus = dbcity.campus dbcity.update_parent(parent=dbcampus) update_machines = True session.flush() if campus is not None: if prev_campus: prev_name = prev_campus.name else: prev_name = None dsdb_runner.update_city(city, dbcampus.name, prev_name) plenaries = PlenaryCollection(logger=logger) plenaries.append(Plenary.get_plenary(dbcity)) if update_machines: q = session.query(Machine) q = q.filter(Machine.location_id.in_(dbcity.offspring_ids())) logger.client_info("Updating %d machines..." % q.count()) for dbmachine in q: plenaries.append(Plenary.get_plenary(dbmachine)) count = plenaries.write() dsdb_runner.commit_or_rollback() logger.client_info("Flushed %d templates." % count)
def render(self, session, logger, interface, machine, mac, automac, model, vendor, pg, autopg, type, comments, **arguments): dbmachine = Machine.get_unique(session, machine, compel=True) oldinfo = DSDBRunner.snapshot_hw(dbmachine) audit_results = [] q = session.query(Interface) q = q.filter_by(name=interface, hardware_entity=dbmachine) if q.first(): raise ArgumentError( "Machine %s already has an interface named %s." % (machine, interface)) if not type: type = 'public' management_types = ['bmc', 'ilo', 'ipmi'] for mtype in management_types: if interface.startswith(mtype): type = 'management' break if interface.startswith("bond"): type = 'bonding' elif interface.startswith("br"): type = 'bridge' # Test it last, VLANs can be added on top of almost anything if '.' in interface: type = 'vlan' if type == "oa" or type == "loopback": raise ArgumentError("Interface type '%s' is not valid for " "machines." % type) bootable = None if type == 'public': if interface == 'eth0': bootable = True else: bootable = False dbmanager = None pending_removals = PlenaryCollection() dsdb_runner = DSDBRunner(logger=logger) if mac: prev = session.query(Interface).filter_by(mac=mac).first() if prev and prev.hardware_entity == dbmachine: raise ArgumentError("{0} already has an interface with MAC " "address {1}.".format(dbmachine, mac)) # Is the conflicting interface something that can be # removed? It is if: # - we are currently attempting to add a management interface # - the old interface belongs to a machine # - the old interface is associated with a host # - that host was blindly created, and thus can be removed safely if prev and type == 'management' and \ prev.hardware_entity.hardware_type == 'machine' and \ prev.hardware_entity.host and \ prev.hardware_entity.host.status.name == 'blind': # FIXME: Is this just always allowed? Maybe restrict # to only aqd-admin and the host itself? dummy_machine = prev.hardware_entity dummy_ip = dummy_machine.primary_ip old_fqdn = str(dummy_machine.primary_name) old_iface = prev.name old_mac = prev.mac old_network = get_net_id_from_ip(session, dummy_ip) self.remove_prev(session, logger, prev, pending_removals) session.flush() dsdb_runner.delete_host_details(old_fqdn, dummy_ip, old_iface, old_mac) self.consolidate_names(session, logger, dbmachine, dummy_machine.label, pending_removals) # It seems like a shame to throw away the IP address that # had been allocated for the blind host. Try to use it # as it should be used... dbmanager = self.add_manager(session, logger, dbmachine, dummy_ip, old_network) elif prev: msg = describe_interface(session, prev) raise ArgumentError("MAC address %s is already in use: %s." % (mac, msg)) elif automac: mac = self.generate_mac(session, dbmachine) audit_results.append(('mac', mac)) else: #Ignore now that Mac Address can be null pass if pg is not None: port_group = verify_port_group(dbmachine, pg) elif autopg: port_group = choose_port_group(session, logger, dbmachine) audit_results.append(('pg', port_group)) else: port_group = None dbinterface = get_or_create_interface(session, dbmachine, name=interface, vendor=vendor, model=model, interface_type=type, mac=mac, bootable=bootable, port_group=port_group, comments=comments, preclude=True) # So far, we're *only* creating a manager if we happen to be # removing a blind entry and we can steal its IP address. if dbmanager: assign_address(dbinterface, dbmanager.ip, dbmanager.network) session.add(dbinterface) session.flush() plenaries = PlenaryCollection(logger=logger) plenaries.append(Plenary.get_plenary(dbmachine)) if pending_removals and dbmachine.host: # Not an exact test, but the file won't be re-written # if the contents are the same so calling too often is # not a major expense. plenaries.append(Plenary.get_plenary(dbmachine.host)) # Even though there may be removals going on the write key # should be sufficient here. key = plenaries.get_write_key() try: lock_queue.acquire(key) pending_removals.stash() plenaries.write(locked=True) pending_removals.remove(locked=True) dsdb_runner.update_host(dbmachine, oldinfo) dsdb_runner.commit_or_rollback("Could not update host in DSDB") except: plenaries.restore_stash() pending_removals.restore_stash() raise finally: lock_queue.release(key) if dbmachine.host: # FIXME: reconfigure host pass for name, value in audit_results: self.audit_result(session, name, value, **arguments) return
for dbinst in dbservice.instances: try: plenary_info = Plenary.get_plenary(dbinst, logger=logger) written += plenary_info.write(locked=True) except Exception, e: failed.append("Service %s instance %s failed: %s" % (dbservice.name, dbinst.name, e)) continue if personalities: logger.client_info("Flushing personalities.") for persona in session.query(Personality).all(): try: plenary_info = Plenary.get_plenary(persona, logger=logger) written += plenary_info.write(locked=True) except Exception, e: failed.append("Personality %s failed: %s" % (persona.name, e)) continue if machines: logger.client_info("Flushing machines.") # Polymorphic loading cannot be applied to eager-loaded # attributes, so load disks manually q = session.query(Disk) q = q.with_polymorphic('*') for disk in q: disks_by_machine[disk.machine_id].append(disk)
def render(self, session, logger, machine, model, vendor, serial, chassis, slot, clearchassis, multislot, vmhost, cluster, allow_metacluster_change, cpuname, cpuvendor, cpuspeed, cpucount, memory, ip, **arguments): dbmachine = Machine.get_unique(session, machine, compel=True) plenaries = PlenaryCollection(logger=logger) oldinfo = DSDBRunner.snapshot_hw(dbmachine) if clearchassis: del dbmachine.chassis_slot[:] remove_plenaries = PlenaryCollection(logger=logger) if chassis: dbchassis = Chassis.get_unique(session, chassis, compel=True) if machine_plenary_will_move(old=dbmachine.location, new=dbchassis.location): remove_plenaries.append(Plenary.get_plenary(dbmachine)) dbmachine.location = dbchassis.location if slot is None: raise ArgumentError("Option --chassis requires --slot " "information.") self.adjust_slot(session, logger, dbmachine, dbchassis, slot, multislot) elif slot is not None: dbchassis = None for dbslot in dbmachine.chassis_slot: if dbchassis and dbslot.chassis != dbchassis: raise ArgumentError("Machine in multiple chassis, please " "use --chassis argument.") dbchassis = dbslot.chassis if not dbchassis: raise ArgumentError("Option --slot requires --chassis " "information.") self.adjust_slot(session, logger, dbmachine, dbchassis, slot, multislot) dblocation = get_location(session, **arguments) if dblocation: loc_clear_chassis = False for dbslot in dbmachine.chassis_slot: dbcl = dbslot.chassis.location if dbcl != dblocation: if chassis or slot is not None: raise ArgumentError("{0} conflicts with chassis {1!s} " "location {2}.".format(dblocation, dbslot.chassis, dbcl)) else: loc_clear_chassis = True if loc_clear_chassis: del dbmachine.chassis_slot[:] if machine_plenary_will_move(old=dbmachine.location, new=dblocation): remove_plenaries.append(Plenary.get_plenary(dbmachine)) dbmachine.location = dblocation if model or vendor: # If overriding model, should probably overwrite default # machine specs as well. if not model: model = dbmachine.model.name if not vendor: vendor = dbmachine.model.vendor.name dbmodel = Model.get_unique(session, name=model, vendor=vendor, compel=True) if dbmodel.machine_type not in ['blade', 'rackmount', 'workstation', 'aurora_node', 'virtual_machine']: raise ArgumentError("The update_machine command cannot update " "machines of type %s." % dbmodel.machine_type) # We probably could do this by forcing either cluster or # location data to be available as appropriate, but really? # Failing seems reasonable. if dbmodel.machine_type != dbmachine.model.machine_type and \ 'virtual_machine' in [dbmodel.machine_type, dbmachine.model.machine_type]: raise ArgumentError("Cannot change machine from %s to %s." % (dbmachine.model.machine_type, dbmodel.machine_type)) old_nic_model = dbmachine.model.nic_model new_nic_model = dbmodel.nic_model if old_nic_model != new_nic_model: for iface in dbmachine.interfaces: if iface.model == old_nic_model: iface.model = new_nic_model dbmachine.model = dbmodel if cpuname or cpuvendor or cpuspeed is not None: dbcpu = Cpu.get_unique(session, name=cpuname, vendor=cpuvendor, speed=cpuspeed, compel=True) dbmachine.cpu = dbcpu if cpucount is not None: dbmachine.cpu_quantity = cpucount if memory is not None: dbmachine.memory = memory if serial: dbmachine.serial_no = serial if ip: update_primary_ip(session, dbmachine, ip) # FIXME: For now, if a machine has its interface(s) in a portgroup # this command will need to be followed by an update_interface to # re-evaluate the portgroup for overflow. # It would be better to have --pg and --autopg options to let it # happen at this point. if cluster or vmhost: if not dbmachine.vm_container: raise ArgumentError("Cannot convert a physical machine to " "virtual.") old_holder = dbmachine.vm_container.holder.holder_object resholder = get_resource_holder(session, hostname=vmhost, cluster=cluster, compel=False) new_holder = resholder.holder_object # TODO: do we want to allow moving machines between the cluster and # metacluster level? if new_holder.__class__ != old_holder.__class__: raise ArgumentError("Cannot move a VM between a cluster and a " "stand-alone host.") if cluster: if new_holder.metacluster != old_holder.metacluster \ and not allow_metacluster_change: raise ArgumentError("Current {0:l} does not match " "new {1:l}." .format(old_holder.metacluster, new_holder.metacluster)) remove_plenaries.append(Plenary.get_plenary(dbmachine.vm_container)) dbmachine.vm_container.holder = resholder for dbdisk in dbmachine.disks: if not isinstance(dbdisk, VirtualDisk): continue old_share = dbdisk.share if isinstance(old_share.holder, BundleResource): resourcegroup = old_share.holder.name else: resourcegroup = None new_share = find_share(new_holder, resourcegroup, old_share.name, error=ArgumentError) # If the shares are registered at the metacluster level and both # clusters are in the same metacluster, then there will be no # real change here if new_share != old_share: old_share.disks.remove(dbdisk) new_share.disks.append(dbdisk) if isinstance(new_holder, Cluster): dbmachine.location = new_holder.location_constraint else: dbmachine.location = new_holder.location session.flush() plenaries.append(Plenary.get_plenary(old_holder)) plenaries.append(Plenary.get_plenary(new_holder)) if dbmachine.vm_container: plenaries.append(Plenary.get_plenary(dbmachine.vm_container)) session.flush() # Check if the changed parameters still meet cluster capacity # requiremets if dbmachine.cluster: dbmachine.cluster.validate() if allow_metacluster_change: dbmachine.cluster.metacluster.validate() if dbmachine.host and dbmachine.host.cluster: dbmachine.host.cluster.validate() # The check to make sure a plenary file is not written out for # dummy aurora hardware is within the call to write(). This way # it is consistent without altering (and forgetting to alter) # all the calls to the method. plenaries.append(Plenary.get_plenary(dbmachine)) if remove_plenaries.plenaries and dbmachine.host: plenaries.append(Plenary.get_plenary(dbmachine.host)) key = CompileKey.merge([plenaries.get_write_key(), remove_plenaries.get_remove_key()]) try: lock_queue.acquire(key) remove_plenaries.stash() plenaries.write(locked=True) remove_plenaries.remove(locked=True) if dbmachine.host: # XXX: May need to reconfigure. pass dsdb_runner = DSDBRunner(logger=logger) dsdb_runner.update_host(dbmachine, oldinfo) dsdb_runner.commit_or_rollback("Could not update machine in DSDB") except: plenaries.restore_stash() remove_plenaries.restore_stash() raise finally: lock_queue.release(key) return