def provider_action(args): client = authenticate() if args.action == "list-providers": pretty = args.pretty list_providers(client, pretty)
def network_action(args): if args.action == 'list-networks': client = authenticate() backend = return_backend(client, args) pretty = args.pretty list_networks(backend, pretty)
def ansible_action(args, Runner, Inventory): client = authenticate() command = args.command tag = args.tag machines = choose_machines_by_tag(client, tag) if not machines: print "No machines found with tag: %s" % tag sys.exit(0) else: print "Found tagged machines" machines, assoc_keys = update_key_association(client, machines) assoc_machines = [machine for machine in machines if machine.info.get('assoc', None)] if not assoc_machines: print "Could not find machines with associated keys" sys.exit(0) tmp_dir, tmp_ansible_host_file = init_ansible_tmp_dir(assoc_machines, assoc_keys, tag) inventory = Inventory(host_list=tmp_ansible_host_file) runner = Runner(module_name='command', module_args=command, inventory=inventory, host_list=[tag]) result = runner.run() parse_output(result, assoc_machines) shutil.rmtree(tmp_dir)
def ansible_action(args, Runner, Inventory): client = authenticate() command = args.command tag = args.tag machines = choose_machines_by_tag(client, tag) if not machines: print "No machines found with tag: %s" % tag sys.exit(0) else: print "Found tagged machines" machines, assoc_keys = update_key_association(client, machines) assoc_machines = [ machine for machine in machines if machine.info.get('assoc', None) ] if not assoc_machines: print "Could not find machines with associated keys" sys.exit(0) tmp_dir, tmp_ansible_host_file = init_ansible_tmp_dir( assoc_machines, assoc_keys, tag) inventory = Inventory(host_list=tmp_ansible_host_file) runner = Runner(module_name='command', module_args=command, inventory=inventory, host_list=[tag]) result = runner.run() parse_output(result, assoc_machines) shutil.rmtree(tmp_dir)
def provider_action(args): client = authenticate() if args.action == "list-providers": pretty = args.pretty list_providers(client,pretty)
def cloud_action(args): client = authenticate() if args.action == 'list-clouds': pretty = args.pretty list_clouds(client, pretty) elif args.action == 'rename-cloud': cloud = choose_cloud(client, args) cloud.rename(args.new_name) print "Renamed cloud to %s" % args.new_name elif args.action == 'delete-cloud': cloud = choose_cloud(client, args) if cloud: cloud.delete() print "Deleted cloud %s" % cloud.title else: print "Could not find cloud" elif args.action == 'describe-cloud': cloud = choose_cloud(client, args) if cloud: show_cloud(cloud) else: print "Could not find cloud" elif args.action == 'add-cloud': add_cloud(client, args) print "New cloud added"
def script_action(args): client = authenticate() if args.action == 'list-scripts': pretty = args.pretty list_scripts(client, pretty)
def backend_action(args): client = authenticate() if args.action == 'list-backends': pretty = args.pretty list_backends(client, pretty) elif args.action == 'rename-backend': backend = choose_backend(client, args) backend.rename(args.new_name) print "Renamed backend to %s" % args.new_name elif args.action == 'delete-backend': backend = choose_backend(client, args) if backend: backend.delete() print "Deleted backend %s" % backend.title else: print "Could not find backend" elif args.action == 'describe-backend': backend = choose_backend(client, args) if backend: show_backend(backend) else: print "Could not find backend" elif args.action == 'add-backend': add_backend(client, args) print "New backend added"
def ssh_action(args): client = authenticate() print "Searching machine..." print machines = client.machines(name=args.machine) if not machines: print "Could not find machine: %s" % args.machine sys.exit(1) else: print "Found machine" print machine = machines[0] probed = machine.probe() if not probed: print "Machine is not probed (maybe still provisioning or no key is associated)." sys.exit(0) else: print "Machine is probed. Gathering info for ssh connection" print key, connection_info = find_key_assoc(client, machine) if key and connection_info: print "Found info" key_path = temp_keyfile(key) machine_ip = machine.info['public_ips'][0] init_client(hostname=machine_ip, port=int(connection_info['port']), user=connection_info['user'], key_file=key_path)
def location_action(args): if args.action == 'list-locations': client = authenticate() backend = return_backend(client, args) pretty = args.pretty list_locations(backend, pretty)
def size_action(args): if args.action == 'list-sizes': client = authenticate() backend = return_backend(client, args) pretty = args.pretty list_sizes(backend, pretty)
def location_action(args): if args.action == 'list-locations': client = authenticate() cloud = return_cloud(client, args) pretty = args.pretty list_locations(cloud, pretty)
def size_action(args): if args.action == 'list-sizes': client = authenticate() cloud = return_cloud(client, args) pretty = args.pretty list_sizes(cloud, pretty)
def size_action(args): if args.action == "list-sizes": client = authenticate() backend = return_backend(client, args) pretty = args.pretty list_sizes(backend, pretty)
def image_action(args): if args.action == 'list-images': client = authenticate() cloud = return_cloud(client, args) search_term = args.search pretty = args.pretty list_images(cloud, search_term, pretty)
def image_action(args): if args.action == 'list-images': client = authenticate() backend = return_backend(client, args) search_term = args.search pretty = args.pretty list_images(backend, search_term, pretty)
def script_action(args): client = authenticate() if args.action == 'list-scripts': pretty = args.pretty list_scripts(client, pretty) elif args.action == 'add-script': if args.script: script = args.script elif args.script_file: if not args.location == 'inline': print 'File input is only available for inline scripts' sys.exit(1) with open(args.script_file, 'r') as f: script = f.read() kwargs = { 'name': args.name, 'description': args.description, 'script': script, 'location_type': args.location, 'exec_type': args.type, 'entrypoint': args.entrypoint # script params ? } if args.run: machine = args.machine_name if args.machine_name else args.machine_id if not machine: print "Machine name or ID must be specified in order to run " \ "the script immediately" sys.exit(1) machine = choose_machine(client, args) machine_id = machine.id cloud_id = machine.cloud.id # TODO verify script was run successfully client.add_and_run_script(cloud_id, machine_id, script_params="", env=None, su=False, fire_and_forget=True, **kwargs) print "Script %s inserted and ran on machine %s. Check the logs " \ "for output" % (args.name, machine.name) else: client.add_script(**kwargs) print "Script %s inserted successfully" % args.name elif args.action == 'remove-script': resp = client.remove_script(args.id) # TODO verify this print "Script %s removed" % args.id elif args.action == 'run-script': machine = args.machine_name if args.machine_name else args.machine_id machine = choose_machine(client, args) machine_id = machine.id cloud_id = machine.cloud.id # TODO params client.run_script(cloud_id, machine_id, args.id) print "Script %s was run. Check the logs for the output" % args.id
def template_action(args): client = authenticate() if args.action == 'list-templates': list_templates(client, args.pretty) elif args.action == 'show-template': show_template(client, args.template_id, args.pretty) elif args.action == 'add-template': add_template(client, args) print "Template %s added successfully" % args.name elif args.action == 'delete-template': client.delete_template(args.template_id) print "Template %s removed" % args.template_id
def key_action(args): client = authenticate() if args.action == 'add-key': name = args.name key_path = args.key_path auto = args.auto_generate if auto: private = client.generate_key() else: with open(key_path, "r") as f: private = f.read().strip("\n") client.add_key(key_name=name, private=private) print "Added key %s" % name elif args.action == 'list-keys': pretty = args.pretty list_keys(client, pretty) elif args.action == 'delete-key': key_id = args.key_name if args.key_name else args.key_id keys = client.keys(id=key_id) key = keys[0] if keys else None if key: key.delete() print "Deleted %s" % key.id else: print "Cound not find key: %s" % args.key sys.exit(0) elif args.action == 'rename-key': key_name = args.key_name if args.key_name else args.key_id new_name = args.new_name keys = client.keys(id=key_name) key = keys[0] if keys else None if key: key.rename(new_name) print "Renamed key to %s" % new_name else: print "Could not find key: %s" % key_name elif args.action == 'describe-key': key_name = args.key_name if args.key_name else args.key_id keys = client.keys(id=key_name) key = keys[0] if keys else None if key: show_key(key) else: print "Could not find key: %s" % key_name
def machine_action(args): client = authenticate() if args.action == 'list-machines': if args.backend or args.backend_id or args.backend_name: backend = return_backend(client, args) else: backend = None pretty = args.pretty metadata = args.metadata list_machines(client, backend, metadata, pretty) elif args.action == 'describe-machine': machine = choose_machine(client, args) display_machine(machine) elif args.action == 'create-machine': backend = return_backend(client, args) create_machine(client, backend, args) print "Created machine %s" % args.machine_name elif args.action in ['start', 'stop', 'reboot', 'destroy', 'probe']: machine = choose_machine(client, args) machine_take_action(machine, args.action) elif args.action == 'enable-monitoring': machine = choose_machine(client, args) machine.enable_monitoring() print "Enabled monitoring to machine %s" % machine.name elif args.action == 'disable-monitoring': machine = choose_machine(client, args) machine.disable_monitoring() print "Disabled monitoring to machine %s" % machine.name elif args.action == 'tag': machine = choose_machine(client, args) if machine.info['can_tag']: machine.tag(tag=args.new_tag) print "Add tag %s to machine %s" % (args.new_tag, machine.name) else: print "Cannot tag machine on provider %s" % machine.backend.title sys.exit(0)
def machine_action(args): client = authenticate() if args.action == "list-machines": if args.backend or args.backend_id or args.backend_name: backend = return_backend(client, args) else: backend = None pretty = args.pretty metadata = args.metadata list_machines(client, backend, metadata, pretty) elif args.action == "describe-machine": machine = choose_machine(client, args) display_machine(machine) elif args.action == "create-machine": backend = return_backend(client, args) create_machine(client, backend, args) print "Created machine %s" % args.machine_name elif args.action in ["start", "stop", "reboot", "destroy", "probe"]: machine = choose_machine(client, args) machine_take_action(machine, args.action) elif args.action == "enable-monitoring": machine = choose_machine(client, args) machine.enable_monitoring() print "Enabled monitoring to machine %s" % machine.name elif args.action == "disable-monitoring": machine = choose_machine(client, args) machine.disable_monitoring() print "Disabled monitoring to machine %s" % machine.name elif args.action == "tag": machine = choose_machine(client, args) if machine.info["can_tag"]: machine.tag(tag=args.new_tag) print "Add tag %s to machine %s" % (args.new_tag, machine.name) else: print "Cannot tag machine on provider %s" % machine.backend.title sys.exit(0)
def machine_action(args): client = authenticate() if args.action == 'list-machines': if args.backend or args.backend_id or args.backend_name: backend = return_backend(client, args) else: backend = None pretty = args.pretty list_machines(client, backend, pretty) elif args.action == 'describe-machine': machine = choose_machine(client, args) display_machine(machine) elif args.action == 'create-machine': backend = return_backend(client, args) create_machine(client, backend, args) print "Created machine %s" % args.machine_name elif args.action in ['start', 'stop', 'reboot', 'destroy', 'probe']: machine = choose_machine(client, args) machine_take_action(machine, args.action) elif args.action == 'enable-monitoring': machine = choose_machine(client, args) machine.enable_monitoring() print "Enabled monitoring to machine %s" % machine.name elif args.action == 'disable-monitoring': machine = choose_machine(client, args) machine.disable_monitoring() print "Disabled monitoring to machine %s" % machine.name elif args.action == 'tag': machine = choose_machine(client, args) if machine.info['can_tag']: machine.tag(tag=args.new_tag) print "Add tag %s to machine %s" % (args.new_tag, machine.name) else: print "Cannot tag machine on provider %s" % machine.backend.title sys.exit(0)
def metric_action(args): client = authenticate() if args.action == 'list-metrics': machine = return_machine(client, args) pretty = args.pretty list_metrics(machine, pretty) elif args.action == 'add-metric': machine = return_machine(client, args) machine.add_metric(args.metric_id) print "Added Metric %s to monitored machine %s" % (args.metric_id, machine.name) elif args.action == 'add-custom-metric': machine = return_machine(client, args) name = args.metric_name python_file = args.file_path value_type = args.value_type unit = args.unit machine.add_python_plugin(name, python_file, value_type, unit) print "Added custom metric %s" % name
def key_action(args): client = authenticate() if args.action == 'add-key': name = args.name key_path = args.key_path auto = args.auto_generate if auto: private = client.generate_key() else: with open(key_path, "r") as f: private = f.read().strip("\n") client.add_key(key_name=name, private=private) print "Added key %s" % name elif args.action == 'list-keys': pretty = args.pretty list_keys(client, pretty) elif args.action == 'delete-key': key_id = args.key_name if args.key_name else args.key_id keys = client.keys(id=key_id) key = keys[0] if keys else None if key: key.delete() print "Deleted %s (%s)" % (key.name, key.id) else: print "Could not find key: %s" % args.key sys.exit(0) elif args.action == 'rename-key': key_name = args.key_name if args.key_name else args.key_id new_name = args.new_name keys = client.keys(id=key_name) key = keys[0] if keys else None if key: key.rename(new_name) print "Renamed key to %s" % new_name else: print "Could not find key: %s" % key_name elif args.action == 'describe-key': key_name = args.key_name if args.key_name else args.key_id keys = client.keys(id=key_name) key = keys[0] if keys else None if key: show_key(key) else: print "Could not find key: %s" % key_name elif args.action == 'associate-key': machine = choose_machine(client, args) key_name = args.key print "Attempting to deploy key to machine over SSH ..." keys = client.keys(id=key_name) key = keys[0] if keys else None if machine and key: cloud_id = machine.cloud.id machine_id = machine.id ips = [ip for ip in machine.info['public_ips'] if ':' not in ip] host = ips[0] key.associate(cloud_id, machine_id, host) print "Successfully associated key %s to machine %s" % (key.id, machine.name) else: print "Key association failed" elif args.action == 'disassociate-key': machine = choose_machine(client, args) key_name = args.key keys = client.keys(id=key_name) key = keys[0] if keys else None if machine and key: cloud_id = machine.cloud.id machine_id = machine.id ips = [ip for ip in machine.info['public_ips'] if ':' not in ip] host = ips[0] if host: print "Attempting to remove key from machine ..." key.disassociate(cloud_id, machine_id, host) print "Successfully disassociated key-machine " \ "(%s-%s) pair" % (key.id, machine.name) else: print "Key disassociation failed" sys.exit(1)
def machine_action(args): client = authenticate() if args.action == 'list-machines': if args.cloud or args.cloud_id or args.cloud_name: cloud = return_cloud(client, args) else: cloud = None pretty = args.pretty list_machines(client, cloud, pretty) elif args.action == 'describe-machine': machine = choose_machine(client, args) display_machine(machine) elif args.action == 'create-machine': cloud = return_cloud(client, args) create_machine(client, cloud, args) print "Created machine %s" % args.machine_name elif args.action in ['start', 'stop', 'reboot', 'destroy', 'probe']: machine = choose_machine(client, args) if not machine: print "Cannot find machine" sys.exit(0) try: machine_take_action(machine, args.action) except: print "Failed to execute %s action on %s " % (args.action, machine) sys.exit(0) elif args.action == 'enable-monitoring': machine = choose_machine(client, args) machine.enable_monitoring() print "Enabled monitoring to machine %s" % machine.name elif args.action == 'disable-monitoring': machine = choose_machine(client, args) machine.disable_monitoring() print "Disabled monitoring to machine %s" % machine.name elif args.action in ['add-tag', 'remove-tag']: machine = choose_machine(client, args) if machine.info['can_tag']: if args.action == 'add-tag': machine.add_tag(key=args.key, value=args.value) print "Added tag %s=%s to machine %s" % (args.key, args.value, machine.name) elif args.action == 'remove-tag': machine.del_tag(key=args.key, value=args.value) print "Removed tag %s=%s from machine %s" % (args.key, args.value, machine.name) else: "Unknown action to be performed on machine tags" sys.exit(0) else: print "Cannot tag machine on provider %s" % machine.cloud.title sys.exit(0)