def duplicate(vm, background, count, all_, filter): """Duplicate a broker-procured vm COMMAND: broker duplicate <vm hostname>|<local id>|all :param vm: Hostname or local id of host :param background: run a new broker subprocess to carry out command :param all_: Click option all :param filter: a filter string matching broker's specification """ if background: helpers.fork_broker() inventory = helpers.load_inventory(filter=filter) for num, host in enumerate(inventory): if str(num ) in vm or host["hostname"] in vm or host["name"] in vm or all_: broker_args = host.get("_broker_args") if broker_args: if count: broker_args["_count"] = count logger.info(f"Duplicating: {host['hostname']}") broker_inst = VMBroker(**broker_args) broker_inst.checkout() else: logger.warning( f"Unable to duplicate {host['hostname']}, no _broker_args found" )
def extend(vm, background, all_, sequential, filter, **kwargs): """Extend a host's lease time COMMAND: broker extend <vm hostname>|<vm name>|<local id> :param vm: Hostname, VM Name, or local id of host :param background: run a new broker subprocess to carry out command :param all_: Click option all :param sequential: Flag for whether to run extends sequentially :param filter: a filter string matching broker's specification """ broker_args = helpers.clean_dict(kwargs) if background: helpers.fork_broker() inventory = helpers.load_inventory(filter=filter) to_extend = [] for num, host in enumerate(inventory): if str(num ) in vm or host["hostname"] in vm or host["name"] in vm or all_: to_extend.append(VMBroker().reconstruct_host(host)) broker_inst = VMBroker(hosts=to_extend, **broker_args) broker_inst.extend(sequential=sequential)
def checkin(vm, background, all_, sequential, filter): """Checkin or "remove" a VM or series of VM broker instances COMMAND: broker checkin <vm hostname>|<local id>|all :param vm: Hostname or local id of host :param background: run a new broker subprocess to carry out command :param all_: Flag for whether to checkin everything :param sequential: Flag for whether to run checkins sequentially :param filter: a filter string matching broker's specification """ if background: helpers.fork_broker() inventory = helpers.load_inventory(filter=filter) to_remove = [] for num, host in enumerate(inventory): if str(num ) in vm or host["hostname"] in vm or host["name"] in vm or all_: to_remove.append(VMBroker().reconstruct_host(host)) broker_inst = VMBroker(hosts=to_remove) broker_inst.checkin(sequential=sequential)
def from_inventory(self, filter=None): """Reconstruct one or more hosts from the local inventory :param filter: A broker-spec filter string """ inv_hosts = helpers.load_inventory(filter=filter) return [self.reconstruct_host(inv_host) for inv_host in inv_hosts]
def sync_inventory(provider): """Acquire a list of hosts from a provider and update our inventory""" additional_arg, instance = None, {} if "::" in provider: provider, instance = provider.split("::") if ":" in provider: provider, additional_arg = provider.split(":") logger.info( f"Pulling remote inventory from {f'{instance } ' if instance else ''}{provider}" ) if instance: instance = {provider: instance} prov_inventory = PROVIDERS[provider](**instance).get_inventory(additional_arg) curr_inventory = [ host["hostname"] or host["name"] for host in helpers.load_inventory() ] new_hosts = [] remove_hosts = curr_inventory[:] for n_host in prov_inventory: name = n_host["hostname"] or n_host["name"] if name in curr_inventory: remove_hosts.remove(name) else: new_hosts.append(n_host) if new_hosts: msg = ", ".join([host["hostname"] or host["name"] for host in new_hosts]) logger.info(f"Adding new hosts: {msg}") helpers.update_inventory(add=new_hosts) else: logger.info("No new hosts found") if remove_hosts: msg = ", ".join(remove_hosts) logger.info(f"Removing old hosts: {msg}") helpers.update_inventory(remove=remove_hosts)
def from_inventory(self, connect=False, filter=None): """Reconstruct one or more hosts from the local inventory :param connect: Boolean - establish ssh connection :param filter: A broker-spec filter string """ inv_hosts = helpers.load_inventory(filter=filter) return [self.reconstruct_host(inv_host, connect) for inv_host in inv_hosts]
def inventory(details): """Get a list of all VMs you've checked out""" logger.info("Pulling local ivnentory") inventory = helpers.load_inventory() for num, host in enumerate(inventory): if details: logger.info(f"{num}: {host.pop('hostname')}, Details: {host}") else: logger.info(f"{num}: {host['hostname']}")
def checkin(vm, all_): """Checkin or "remove" a VM or series of VM broker instances COMMAND: broker checkin <vm hostname>|<local id>|all :param vm: Hostname or local id of host """ inventory = helpers.load_inventory() to_remove = [] for num, host_export in enumerate(inventory): if str(num) in vm or host_export["hostname"] in vm or all_: to_remove.append(VMBroker.reconstruct_host(host_export)) broker_inst = VMBroker(hosts=to_remove) broker_inst.checkin()
def inventory(details, sync): """Get a list of all VMs you've checked out showing hostname and local id hostname pulled from list of dictionaries """ if sync: VMBroker.sync_inventory(provider=sync) logger.info("Pulling local inventory") inventory = helpers.load_inventory() for num, host in enumerate(inventory): if details: logger.info( f"{num}: {host['hostname'] or host['name']}, Details: {helpers.yaml_format(host)}" ) else: logger.info(f"{num}: {host['hostname'] or host['name']}")
def inventory(details): """Get a list of all VMs you've checked out showing hostname and local id hostname pulled from list of dictionaries :param details: click option to display all host details """ logger.info("Pulling local inventory") inventory = helpers.load_inventory() for num, host in enumerate(inventory): if details: logger.info( f"{num}: {host.pop('hostname')}, Details: {helpers.yaml_format(host)}" ) else: logger.info(f"{num}: {host['hostname']}")
def checkin(vm): """Checkin a VM or series of VMs COMMAND: broker checkin <vm hostname>|<local id>|all """ inventory = helpers.load_inventory() to_remove = [] for num, host in enumerate(inventory): if str(num) in vm or host["hostname"] in vm or "all" in vm: to_remove.append((num, host)) # reconstruct the hosts and call their release methhod for num, host in to_remove[::-1]: del inventory[num] logger.info(f"Checking in {host['hostname']}") helpers.update_inventory(inventory, replace_all=True)
def checkin(vm, background, all_): """Checkin or "remove" a VM or series of VM broker instances COMMAND: broker checkin <vm hostname>|<local id>|all :param vm: Hostname or local id of host :param background: run a new broker subprocess to carry out command """ if background: fork_broker() inventory = helpers.load_inventory() to_remove = [] for num, host_export in enumerate(inventory): if str(num) in vm or host_export["hostname"] in vm or all_: to_remove.append(VMBroker.reconstruct_host(host_export)) broker_inst = VMBroker(hosts=to_remove) broker_inst.checkin()
def sync_inventory(provider): """Acquire a list of hosts from a provider and update our inventory""" additional_arg, instance = None, {} if "::" in provider: provider, instance = provider.split("::") if ":" in provider: provider, additional_arg = provider.split(":") logger.info( f"Pulling remote inventory from {f'{instance } ' if instance else ''}{provider}" ) if instance: instance = {provider: instance} prov_inventory = PROVIDERS[provider](**instance).get_inventory(additional_arg) curr_inventory = [ host["hostname"] or host["name"] for host in helpers.load_inventory() if host["_broker_provider"] == provider ] helpers.update_inventory(add=prov_inventory, remove=curr_inventory)
def inventory(details, sync, filter): """Get a list of all VMs you've checked out showing hostname and local id hostname pulled from list of dictionaries """ if sync: Broker.sync_inventory(provider=sync) logger.info("Pulling local inventory") inventory = helpers.load_inventory(filter=filter) emit_data = [] for num, host in enumerate(inventory): emit_data.append(host) if details: logger.info( f"{num}: {host.get('hostname', host.get('name'))}, Details: {helpers.yaml_format(host)}" ) else: logger.info(f"{num}: {host.get('hostname', host.get('name'))}") helpers.emit({"inventory": emit_data})
def extend(vm, background, all_): """Extend a host's lease time COMMAND: broker extend <vm hostname>|<vm name>|<local id> :param vm: Hostname, VM Name, or local id of host :param background: run a new broker subprocess to carry out command :param all_: Click option all """ if background: fork_broker() inventory = helpers.load_inventory() to_extend = [] for num, host in enumerate(inventory): if str(num) in vm or host["hostname"] in vm or host["name"] in vm or all_: to_extend.append(VMBroker().reconstruct_host(host)) broker_inst = VMBroker(hosts=to_extend) broker_inst.extend()
def duplicate(vm, all_): """Duplicate a broker-procured vm COMMAND: broker duplicate <vm hostname>|<local id>|all :param vm: Hostname or local id of host :param all_: Click option all """ inventory = helpers.load_inventory() for num, host in enumerate(inventory): if str(num) in vm or host["hostname"] in vm or all_: broker_args = host.get("_broker_args") if broker_args: logger.info(f"Duplicating: {host['hostname']}") broker_inst = VMBroker(**broker_args) broker_inst.checkout() else: logger.warning( f"Unable to duplicate {host['hostname']}, no _broker_args found" )