def checkin(self, host=None): """checkin one or more VMs :param host: can be one of: None - Will use the contents of self._hosts A single host object A list of host objects A dictionary mapping host types to one or more host objects """ if host is None: host = self._hosts logger.debug(host) if isinstance(host, dict): for _host in host.values(): self.checkin(_host) elif isinstance(host, list): # reversing over a copy of the list to avoid skipping for _host in host[::-1]: self.checkin(_host) elif host: logger.info(f"Checking in {host.hostname or host.name}") host.close() try: host.release() except: pass if host in self._hosts: self._hosts.remove(host) helpers.update_inventory(remove=host.hostname)
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 checkout(self): """checkout one or more VMs :return: Host obj or list of Host objects """ hosts = self._checkout() helpers.emit(hosts=[host.to_dict() for host in hosts]) self._hosts.extend(hosts) helpers.update_inventory([host.to_dict() for host in hosts]) return hosts if not len(hosts) == 1 else hosts[0]
def checkout(self, connect=False): """checkout one or more VMs :param connect: Boolean whether to establish host ssh connection :return: Host obj or list of Host objects """ hosts = self._checkout(connect=connect) self._hosts.extend(hosts) helpers.update_inventory([host.to_dict() for host in hosts]) return hosts if not len(hosts) == 1 else hosts[0]
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(self, host=None): """checkin one or more VMs""" if host is None: host = self._hosts if isinstance(host, dict): for _host in host.values(): self.checkin(_host) elif isinstance(host, list): for _host in host: self.checkin(_host) elif host: logger.info(f"Checking in {host.hostname}") host.close() host.release() self._hosts.remove(host) helpers.update_inventory(remove=host.hostname)
def checkin(self, sequential=False, host=None): """checkin one or more VMs :param host: can be one of: None - Will use the contents of self._hosts A single host object A list of host objects A dictionary mapping host types to one or more host objects :param sequential: boolean whether to run checkins sequentially """ # default to hosts listed on the instance hosts = host or self._hosts logger.debug( f"Checkin called with: {hosts}, " f'running {"sequential" if sequential else "concurrent"}' ) # normalize the type since the function accepts multiple types if isinstance(hosts, dict): # flatten the lists of hosts from the values of the dict hosts = [host for host_list in hosts.values() for host in host_list] if not isinstance(hosts, list): hosts = [hosts] if not hosts: logger.debug("Checkin called with no hosts, taking no action") return with ProcessPoolExecutor( max_workers=1 if sequential else len(hosts) ) as workers: completed_checkins = as_completed( # reversing over a copy of the list to avoid skipping workers.submit(self._checkin, _host) for _host in hosts[::-1] ) for completed in completed_checkins: _host = completed.result() self._hosts = [ h for h in self._hosts if not (h.to_dict() == _host.to_dict()) ] logger.debug( f"Completed checkin process for {_host.hostname or _host.name}" ) helpers.update_inventory(remove=[h.hostname for h in hosts])
def checkout(self, connect=False): """checkout one or more VMs :param connect: Boolean whether to establish host ssh connection :return: Host obj or list of Host objects """ for action, arg in self._provider_actions.items(): provider, method = PROVIDER_ACTIONS[action] logger.info(f"Using provider {provider.__name__} to checkout") host = self._act(provider, method, checkout=True) if host: if connect: host.connect() self._hosts.append(host) logger.info(f"{host.__class__.__name__}: {host.hostname}") helpers.update_inventory(add=host.to_dict()) return self._hosts if not len(self._hosts) == 1 else self._hosts[0]
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 checkout(ctx, workflow, nick): """Checkout a Virtual Machine COMMAND: broker checkout --<action> <argument> """ broker_args = {} if nick: broker_args = helpers.resolve_nick(nick) if workflow: broker_args["workflow"] = workflow # if additional arguments were passes, include them in the broker args broker_args.update(dict(zip(ctx.args[::2], ctx.args[1::2]))) broker_inst = VMBroker(**broker_args) broker_inst.checkout() new_hosts = [] for host in broker_inst._hosts: logger.info(f"{host.__class__.__name__}: {host.hostname}") new_hosts.append(host.to_dict()) helpers.update_inventory(new_hosts)
def checkout(self): """checkout one or more VMs :return: Host obj or list of Host objects """ hosts = self._checkout() err, to_emit = None, [] for host in hosts: if not isinstance(host, exceptions.ProviderError): to_emit.append(host.to_dict()) else: err = host hosts.remove(host) helpers.emit(hosts=[host.to_dict() for host in hosts]) self._hosts.extend(hosts) helpers.update_inventory([host.to_dict() for host in hosts]) if err: raise err return hosts if not len(hosts) == 1 else hosts[0]