def prompt_dst(self, results: set, flattened_tags: dict) -> str: """ If selector gets more then one answer, then we are prompting user to choose destination. Parameters: results: set (of str) of all filtered instanceId. flattened_tags: dict of all instanceId with a list of all their respective tags. Returns: str: instance Id of destination. """ print_orange("Ambigous filter. Please refine.") data = list() for c, inst_id in enumerate(results): data.append([c, inst_id, '\n'.join([tag['Key'] + ": " + tag['Value'] for tag in flattened_tags[inst_id]])]) data.append([c+1, "Exit."]) print(tabulate( data, headers=["Id", "instanceId", "tags"], tablefmt="fancy_grid" )) value = click.prompt('Please choose destination', type=int) if value == (c + 1): exit(0) return list(results)[value]
def on_refresh(self): """Will write refreshed account data to specified backend.""" fp = self.backend.Backend(self.account, self.cloud, self.conf.backend_path) content_changed = fp.write_state(self.content) if content_changed: print_orange('State changed.') else: print_green('State already up-to-date.')
def discover_bounce(self, host: Host) -> Host: for user in self.conf.usernames: host.connectionString.username = user host.connectionString.connectionIP = host.privateIp if host.publicIp is None else host.publicIp host.connectionString.keyPath = self.conf.ssh_key_path + host.key + '.pem' # Means return code = 0, which is a success if not DoConnectAndSave(host, self.account_obj).lazy_connect(): return host print_orange('Failed finding a connection path for host, exiting.') sys.exit(1)
def connect(self): """Will actually run the computed SSH command.""" # Get destination Host object selected_vm = Selector(self.account_obj, self.filters).select_host_from_state_file() try: # host file found self.connect_with_host_data(selected_vm) except HostNotFound: # host file not found try: self.connect_without_host_data(selected_vm, bounce=self.bounce) except ConnectionError: # could not connect at all. print_orange("Failed connecting.")
def select_host_from_host_file(self) -> Host: results = get_instances_from_filters(self.account_obj, self.filters) if len(results) > 1: dst_instance = self.prompt_dst(results, get_tags_from_instanceIds(self.account_obj, list(results))) return self.load.load_host(dst_instance) elif len(results) == 0: print_orange('No instance found, please change filters.') sys.exit(1) else: return self.load.load_host(results.pop())
def connect_with_host_data(self, host: Host): """ Try to open host file and bounce host file, connect if exists. """ host_obj = self.content.load_host(host.instanceId) if host_obj.connectionString: print_light_grey('Found host data, trying to connect...') # Has a bounce host. if host_obj.connectionString.bounce_host: bounce_host = DiscoverHost(self.account_obj, bounce=True).get_bounce() if not DoConnectAndSave(host_obj, self.account_obj).bounce_regular_connect(bounce_host): sys.exit(0) else: if not DoConnectAndSave(host_obj, self.account_obj).regular_connect(): sys.exit(0) print_orange('Found host data is obsolete, trying to find a new path...') raise HostNotFound