def run_handler(args, config): # TODO: implement -d -a and -v task_name = args['<task>'] task_playbook = config.dig('run', task_name) not_found = [] desired_instances = [] desired_targets = [ x.strip() for x in config.dig('run', task_name)[0]['hosts'].split(',') ] for desired_target in desired_targets: instances = inventory.search(config, [desired_target]) if len(instances) == 0: not_found.append(desired_target) continue instance = inventory.Instance(desired_target, instances[0].address) desired_instances.append(instance) if len(not_found) > 0: logger.error("Unable to find instances: %s" % ", ".join(not_found)) sys.exit(1) if not task_playbook: logger.error("Playbook %s not configured." % repr(task_name)) sys.exit(1) task = RunAnsiblePlaybook(task_name, task_playbook[0], config, desired_instances) task.run()
def unmount_handler(args, config): Sshfs.ensure_sshfs_installed() question = "What instances would you like to have unmounted?" if args['-a']: instances = inventory.instances(config) sshfs_objs = [Sshfs(config, instance, dry_run=args['-d']) for instance in instances] mounted_targets = [obj.instance for obj in sshfs_objs if obj.is_mounted] target_instances = mounted_targets else: desired_targets = args['<host>'] instances = inventory.search(config, desired_targets) sshfs_objs = [Sshfs(config, instance, dry_run=args['-d']) for instance in instances] mounted_targets = [obj.instance for obj in sshfs_objs if obj.is_mounted] target_instances = prompt_targets(question, instances=mounted_targets, multiple=False, config=config) if len(target_instances) == 0: logger.error("No matching mounts found") if args['-a']: logger.warn("Did you select targets with <space> and confirm with <enter>?") sys.exit(1) for sshfsObj in sshfs_objs: if sshfsObj.instance in target_instances: if sshfsObj.unmount(): logger.info("Unmounted %s" % sshfsObj.instance.name) else: logger.error("Unable to unmount %s" % sshfsObj.instance.name)
def mount_handler(args, config): Sshfs.ensure_sshfs_installed() if config.dig('inventory', 'update_at_start') or args['-u']: update_handler(args, config) fields = args['<host>:<remotedir>'].split(':') if len(fields) != 2: logger.error("Requires exactly 2 arguments: host:remotedir") sys.exit(1) desired_target, remotedir = fields instances = inventory.search(config, [desired_target]) sshfs_objs = [Sshfs(config, instance, remotedir, dry_run=args['-d']) for instance in instances] unmounted_targets = [obj.instance for obj in sshfs_objs if not obj.is_mounted] question = "What instances would you like to have mounted?" target_instances = prompt_targets(question, instances=unmounted_targets, multiple=False, config=config) if len(target_instances) == 0: logger.info("No matching instances found") sys.exit(1) for sshfsObj in sshfs_objs: if sshfsObj.instance in target_instances: if sshfsObj.mount(): logger.info("Mounted %s at %s" % (sshfsObj.instance.name, sshfsObj.mountpoint)) else: logger.error("Unable to mount %s" % sshfsObj.instance.name)
def prompt_targets(question, targets=None, instances=None, multiple=True, config=None): if targets == None and instances == None or targets != None and instances != None: raise RuntimeError("Provide exactly one of either 'targets' or 'instances'") if targets: instances = inventory.search(config, targets) if len(instances) == 0: return [] if len(instances) == 1: return instances display_instances = collections.OrderedDict() # TODO: fix cap'd length... it's pretty arbitraty maxLen = min(max([len(instance.name) for instance in instances]), 55) for instance in sorted(instances): display = str("%-" + str(maxLen+3) + "s (%s)") % (instance.name, instance.address) display_instances[display] = instance questions = [] if multiple: question = inquirer.Checkbox('instance', message="%s%s%s (space to multi-select, enter to finish)" % (utils.term.bold + utils.term.underline, question, utils.term.normal), choices=list(display_instances.keys()) + ['all'], # default='all' ) else: question = inquirer.List('instance', message="%s%s%s (enter to select)" % (utils.term.bold, question, utils.term.normal), choices=list(display_instances.keys()), ) questions.append(question) answers = None try: answers = inquirer.prompt(questions, theme=THEMER, raise_keyboard_interrupt=True) except KeyboardInterrupt: logger.error("Cancelled by user") sys.exit(1) if 'all' in answers["instance"]: selected_hosts = instances else: selected_hosts = [] if not multiple: answers["instance"] = [answers["instance"]] for answer in answers["instance"]: selected_hosts.append(display_instances[answer]) return selected_hosts