def details(self, kind, category, id, format="table"): from cloudmesh_client.db.CloudmeshDatabase import CloudmeshDatabase cm = CloudmeshDatabase() try: if kind not in self.kind: raise ValueError('{} not defined'.format(kind)) elements = None for idkey in ["cm_id", "name", "uuid", "id", "cm_id"]: s = {idkey: id} try: elements = cm.find(kind=kind, category=category, **s) except: pass if elements is not None: break if elements is None: return None if len(elements) > 0: element = elements[0] if format == "table": return Printer.attribute(element) else: return Printer.write(element, output=format) else: return None except Exception as ex: Console.error(ex.message)
def node_info(clusterid, nodeid=None, format='table'): if nodeid: url = Comet.url("cluster/{}/compute/{}/".format(clusterid, nodeid)) else: url = Comet.url("cluster/{}/frontend/".format(clusterid)) r = Comet.get(url) return Printer.attribute( r, #order=Cluster.NODEINFO_ORDER, output=format)
def boot_from_args(arg): arg.username = arg.username or Image.guess_username(arg.image) is_name_provided = arg.name is not None arg.user = Default.user for index in range(0, arg.count): vm_details = dotdict({ "cloud": arg.cloud, "name": Vm.generate_vm_name(arg.name, index), "image": arg.image, "flavor": arg.flavor, "key": arg.key, "secgroup": arg.secgroup, "group": arg.group, "username": arg.username, "user": arg.user }) # correct the username vm_details.username = Image.guess_username_from_category( vm_details.cloud, vm_details.image, username=arg.username) try: if arg.dryrun: print(Printer.attribute(vm_details, output=arg.format)) msg = "dryrun info. OK." Console.ok(msg) else: vm_id = Vm.boot(**vm_details) if vm_id is None: msg = "info. failed." Console.error(msg, traceflag=False) return "" # set name and counter in defaults Default.set_vm(value=vm_details.name) if is_name_provided is False: Default.incr_counter("name") # Add to group if vm_id is not None: Group.add(name=vm_details.group, species="vm", member=vm_details.name, category=vm_details.cloud) msg = "info. OK." Console.ok(msg) except Exception as e: Console.error( "Problem booting instance {name}".format(**vm_details), traceflag=False)
def node_info(clusterid, nodeid=None, format='table'): if nodeid: url = Comet.url("cluster/{}/compute/{}/" .format(clusterid, nodeid)) else: url = Comet.url("cluster/{}/frontend/" .format(clusterid)) r = Comet.get(url) return Printer.attribute(r, #order=Cluster.NODEINFO_ORDER, output=format)
def boot_from_args(arg): arg.username = arg.username or Image.guess_username(arg.image) is_name_provided = arg.name is not None arg.user = Default.user for index in range(0, arg.count): vm_details = dotdict({ "cloud": arg.cloud, "name": Vm.get_vm_name(arg.name, index), "image": arg.image, "flavor": arg.flavor, "key": arg.key, "secgroup": arg.secgroup, "group": arg.group, "username": arg.username, "user": arg.user }) # correct the username vm_details.username = Image.guess_username_from_category( vm_details.cloud, vm_details.image, username=arg.username) try: if arg.dryrun: print(Printer.attribute(vm_details, output=arg.format)) msg = "dryrun info. OK." Console.ok(msg) else: vm_id = Vm.boot(**vm_details) if vm_id is None: msg = "info. failed." Console.error(msg, traceflag=False) return "" # set name and counter in defaults Default.set_vm(value=vm_details.name) if is_name_provided is False: Default.incr_counter("name") # Add to group if vm_id is not None: Group.add(name=vm_details.group, species="vm", member=vm_details.name, category=vm_details.cloud) msg = "info. OK." Console.ok(msg) except Exception as e: Console.error("Problem booting instance {name}".format(**vm_details), traceflag=False)
def do_info(self, args, arguments): """ :: Usage: info [--cloud=CLOUD] [--format=FORMAT] Options: --format=FORMAT the output format [default: table] --cloud=CLOUD the cloud name Examples: cm info """ arg = dotdict(arguments) arg.cloud = arguments["--cloud"] or Default.cloud arg.FORMAT = arguments["--format"] or "table" d = { "cloud": arg.cloud, "key": Default.key, "user": Default.user, "vm": Default.vm, "group": Default.group, "secgroup": Default.secgroup, "counter": Default.get_counter(name="name"), "image": Default.get_image(category=arg.cloud), "flavor": Default.get_flavor(category=arg.cloud), "refresh": str(Default.refresh), "debug": str(Default.debug), "interactive": str(Default.interactive), "purge": str(Default.purge), } order = [ "cloud", "key", "user", "vm", "group", "secgroup", "counter", "image", "flavor", "refresh", "debug", "interactive", "purge" ] print( Printer.attribute(d, order=order, output=arg.FORMAT, sort_keys=False)) if d["key"] in ["TBD", ""] or d["user"] in ["TBD", ""]: msg = "Please replace the TBD values" msg = msg + "\nSee Also: \n\n" \ + " cm register profile \n" \ + " cm default user=YOURUSERNAME\n" Console.error(msg, traceflag=False) return ""
def node_info(clusterid, nodeid=None, format='table'): if nodeid: url = Comet.url("cluster/{}/compute/{}/" .format(clusterid, nodeid)) else: url = Comet.url("cluster/{}/frontend/" .format(clusterid)) r = Comet.get(url) ret = "ERROR: Node not available. Please check the cluster/node name!\n" if r: ret = Printer.attribute(r, #order=Cluster.NODEINFO_ORDER, output=format) return ret
def list(cls, cloud, tenant, output="table"): try: provider = CloudProvider(cloud).provider result = provider.list_quota(cloud) (order, header) = CloudProvider(cloud).get_attributes("quota") return Printer.attribute(result, header=header, output=output) except Exception as e: import sys print(sys.exc_info()[0]) return e
def export(host, output): config = ConfigDict("cloudmesh.yaml") credentials = dict( config["cloudmesh"]["clouds"][host]["credentials"]) if not arguments["--password"]: credentials["OS_PASSWORD"] = "******" if output is None: for attribute, value in credentials.items(): print("export {}={}".format(attribute, value)) elif output == "table": print(Printer.attribute(credentials)) else: print(Printer.write(credentials, output=output))
def list(cls, cloud, output="table", tenant=None): try: provider = CloudProvider(cloud).provider # if tenant is None: # tenant = provider.tenant result = provider.list_limits(tenant)["absolute"] (order, header) = CloudProvider(cloud).get_attributes("limits") return Printer.attribute(result, header=header, output=output) except Exception as e: return e
def do_info(self, args, arguments): """ :: Usage: info [--cloud=CLOUD] [--format=FORMAT] Options: --format=FORMAT the output format [default: table] --cloud=CLOUD the cloud name Examples: cm info """ arg = dotdict(arguments) arg.cloud = arguments["--cloud"] or Default.cloud arg.FORMAT = arguments["--format"] or "table" d = { "cloud": arg.cloud, "key": Default.key, "user": Default.user, "vm": Default.vm, "group": Default.group, "secgroup": Default.secgroup, "counter": Default.get_counter(name="name"), "image": Default.get_image(category=arg.cloud), "flavor": Default.get_flavor(category=arg.cloud), "refresh": str(Default.refresh), "debug": str(Default.debug), "interactive": str(Default.interactive), "purge": str(Default.purge), } order = ["cloud", "key", "user", "vm", "group", "secgroup", "counter", "image", "flavor", "refresh", "debug", "interactive", "purge"] print(Printer.attribute(d, order=order, output=arg.FORMAT, sort_keys=False)) if d["key"] in ["TBD", ""] or d["user"] in ["TBD", ""]: msg = "Please replace the TBD values" msg = msg + "\nSee Also: \n\n" \ + " cm register profile \n" \ + " cm default user=YOURUSERNAME\n" Console.error(msg, traceflag=False) return ""
def get_fixed_ip(cls, cloudname, fixed_ip_addr, output='table'): """ Method retrieves fixed ip info :param cloudname: :param fixed_ip_addr: :return: fixed_ip_info """ try: cloud_provider = CloudProvider(cloudname).provider result = cloud_provider.get_fixed_ip(fixed_ip_addr=fixed_ip_addr) return Printer.attribute(result, header=["name", "value"], output=output) except Exception as ex: Console.error(ex.message) return
def get_fixed_ip(cls, cloudname, fixed_ip_addr, output='table'): """ Method retrieves fixed ip info :param cloudname: :param fixed_ip_addr: :return: fixed_ip_info """ try: cloud_provider = CloudProvider(cloudname).provider result = cloud_provider.get_fixed_ip(fixed_ip_addr=fixed_ip_addr) return Printer.attribute(result, header=[ "name", "value" ], output=output) except Exception as ex: Console.error(ex.message) return
def do_info(self, args, arguments): """ :: Usage: info [--cloud=CLOUD] [--format=FORMAT] Options: --format=FORMAT the output format [default: table] --cloud=CLOUD the cloud name Examples: cm info """ arg = dotdict(arguments) arg.cloud = arguments["--cloud"] or Default.cloud arg.FORMAT = arguments["--format"] or "table" d = { "cloud": arg.cloud, "key": Default.key, "user": Default.user, "vm": Default.vm, "group": Default.group, "secgroup": Default.secgroup, "counter": Default.get_counter(name="name"), "image": Default.get_image(category=arg.cloud), "flavor": Default.get_flavor(category=arg.cloud), "refresh": str(Default.refresh), "debug": str(Default.debug), "interactive": str(Default.interactive), "purge": str(Default.purge), } order = ["cloud", "key", "user", "vm", "group", "secgroup", "counter", "image", "flavor", "refresh", "debug", "interactive", "purge"] print(Printer.attribute(d, order=order, output=arg.FORMAT, sort_keys=False)) return ""
def boot(cls, **kwargs): arg = dotdict(kwargs) for a in ["key", "name", "image", "flavor"]: if a not in kwargs: raise ValueError(a + " not in arguments to vm boot") conf = ConfigDict("cloudmesh.yaml") arg.username = conf["cloudmesh"]["profile"]["user"] arg.group = arg.group or Default.group cloud_provider = CloudProvider(arg.cloud).provider if "nics" in arg: nics = arg.nics else: nics = None basic_dict = { "cloud": arg.cloud, "name": arg.name, "image": arg.image, "flavor": arg.flavor, "key": arg.key, "secgroup": [arg.secgroup], "nics": nics, "meta": {'kind': 'cloudmesh', 'group': arg.group, 'image': arg.image, 'flavor': arg.flavor, 'key': arg.key, 'category': arg.cloud } } # Special case for Azure where certificate details needs to be added if arg.cloud == "azure": kwargs = dict() kwargs['kind'] = "key_azure" db_result = cls.cm.find(**kwargs) # pprint("Key DB results") key_result = None try: for key in db_result: if key['name'] == arg.key: pprint("Found the key") key_result = key break if key_result is not None: new_dict_items = dict() new_dict_items['cert_thumbprint'] = key_result['fingerprint'] new_dict_items['pub_key_path'] = key_result['key_path'] new_dict_items['cert_path'] = key_result['certificate'] new_dict_items['pfx_path'] = key_result['pfx_path'] basic_dict.update(new_dict_items) else: pprint("None found in DB") except: traceback.print_exc() pprint("Exception while processing azure boot arguments") d = dotdict(basic_dict) Console.ok("Machine {name} is being booted on cloud {cloud} ...".format(**arg)) print(Printer.attribute(d)) vm = cloud_provider.boot_vm(**d) if vm is not None: cls.refresh(cloud=arg.cloud) cls.cm.set(d.name, "key", d.key, scope="first", kind="vm") cls.cm.set(d.name, "image", d.image, scope="first", kind="vm") cls.cm.set(d.name, "flavor", d.flavor, scope="first", kind="vm") cls.cm.set(d.name, "group", arg.group, scope="first", kind="vm") cls.cm.set(d.name, "user", arg.user, scope="first", kind="vm") # update group and key # # cls.cm.update("vm", name=data.name) return vm
def boot(cls, **kwargs): arg = dotdict(kwargs) for a in ["key", "name", "image", "flavor"]: if a not in kwargs: raise ValueError(a + " not in arguments to vm boot") conf = ConfigDict("cloudmesh.yaml") arg.user = arg.user or conf["cloudmesh"]["profile"]["user"] arg.group = arg.group or Default.group cloud_provider = CloudProvider(arg.cloud).provider if "nics" in arg: nics = arg.nics else: nics = None basic_dict = { "cloud": arg.cloud, "name": arg.name, "image": arg.image, "flavor": arg.flavor, "key": arg.key, "secgroup": [arg.secgroup], "nics": nics, "meta": { 'kind': 'cloudmesh', 'group': arg.group, 'cluster': arg.get('cluster', None), 'image': arg.image, 'flavor': arg.flavor, 'key': arg.key, 'category': arg.cloud } } # Special case for Azure where certificate details needs to be added if arg.cloud == "azure": kwargs = dict() kwargs['kind'] = "key_azure" db_result = cls.cm.find(**kwargs) # pprint("Key DB results") key_result = None try: for key in db_result: if key['name'] == arg.key: pprint("Found the key") key_result = key break if key_result is not None: new_dict_items = dict() new_dict_items['cert_thumbprint'] = key_result[ 'fingerprint'] new_dict_items['pub_key_path'] = key_result['key_path'] new_dict_items['cert_path'] = key_result['certificate'] new_dict_items['pfx_path'] = key_result['pfx_path'] basic_dict.update(new_dict_items) else: pprint("None found in DB") except: traceback.print_exc() pprint("Exception while processing azure boot arguments") d = dotdict(basic_dict) Console.ok( "Machine {name} is being booted on cloud {cloud} ...".format( **arg)) print(Printer.attribute(d)) vm = cloud_provider.boot_vm(**d) if vm is not None: cls.refresh(cloud=arg.cloud) try: # TODO: Repair db schema for vm_azure, vm_libcloud, # vm_openstack. The following set only works with # openstack, no libcloud, no azure cls.cm.set(d.name, "key", d.key, scope="first", kind="vm") cls.cm.set(d.name, "image", d.image, scope="first", kind="vm") cls.cm.set(d.name, "flavor", d.flavor, scope="first", kind="vm") cls.cm.set(d.name, "group", arg.group, scope="first", kind="vm") cls.cm.set(d.name, "user", arg.user, scope="first", kind="vm") cls.cm.set(d.name, 'username', arg.username, scope='first', kind='vm') cls.cm.set(d.name, 'cluster', arg.cluster, scope='first', kind='vm') except: # cm.set error is identified as a warning, not an error import sys Console.warning("cls.cm.set error: %s" % (sys.exc_info()[0])) # update group and key # # cls.cm.update("vm", name=data.name) return vm
def list(cls, **kwargs): """ This method lists all VMs of the cloud """ arg = dotdict(kwargs) if "name" in arg: arg.name = arg.name arg.output = arg.output or 'table' # pprint (kwargs) # prevent circular dependency def vm_groups(vm): """ :param vm: name of the vm :return: a list of groups the vm is in """ try: query = { 'kind': "group", 'provider': 'general', "species": "vm", "member": vm, "scope": 'all', "output": 'dict' } d = cls.cm.find(**query) groups_vm = set() if d is not None and len(d) > 0: for vm in d: groups_vm.add(vm['name']) return list(groups_vm) except Exception as ex: Console.error(ex.message) return [] try: if "name" in arg and arg.name is not None: if cls.isUuid(arg.name): elements = cls.cm.find(kind="vm", category=arg.category, uuid=arg.name) else: elements = cls.cm.find(kind="vm", category=arg.category, label=arg.name) else: elements = cls.cm.find(kind="vm", category=arg.category) if elements is None or len(elements) == 0: return None for elem in elements: element = elem name = element["name"] groups = vm_groups(name) element["group"] = ','.join(groups) # print(elements) # order = ['id', 'uuid', 'name', 'cloud'] (order, header) = CloudProvider(arg.category).get_attributes("vm") # order = None if "name" in arg and arg.name is not None: return Printer.attribute(elements[0], output=arg.output) else: return Printer.write(elements, order=order, output=arg.output) except Exception as ex: Console.error(ex.message)
def do_vagrant(self, args, arguments): """ :: Usage: vagrant version [--format=FORMAT] vagrant image list [--format=FORMAT] vagrant image find NAME vagrant image add NAME vagrant vm list [--format=FORMAT] [-v] vagrant vm delete NAME vagrant vm config NAME vagrant vm ip NAME [--all] vagrant create NAME ([--memory=MEMORY] [--image=IMAGE] [--script=SCRIPT] | list) vagrant vm boot NAME ([--memory=MEMORY] [--image=IMAGE] [--port=PORT] [--script=SCRIPT] | list) vagrant vm ssh NAME [-e COMMAND] """ arguments.format = arguments["--format"] or "table" arguments.verbose = arguments["-v"] arguments.all = arguments["--all"] """ if arg.version: versions = { "vagrant": { "attribute": "Vagrant Version", "version": vagrant.version(), }, "cloudmesh-vagrant ": { "attribute":"cloudmesh vagrant Version", "version": __version__ } } _LIST_PRINT(versions, arg.format) """ if arguments.image and arguments.list: l = vagrant.image.list(verbose=arguments.verbose) _list_print(l, arguments.format, order=["name", "provider", "date"]) elif arguments.image and arguments.add: l = vagrant.image.add(arguments.NAME) print(l) elif arguments.image and arguments.find: l = vagrant.image.find(arguments.NAME) print(l) elif arguments.vm and arguments.list: l = vagrant.vm.list() _list_print(l, arguments.format, order=["name", "state", "id", "provider", "directory"]) elif arguments.create and arguments.list: result = Shell.cat("{NAME}/Vagrantfile".format(**arguments)) print(result) elif arguments.create: d = defaults() arguments.memory = arguments["--memory"] or d.memory arguments.image = arguments["--image"] or d.image arguments.script = arguments["--script"] or d.script vagrant.vm.create(name=arguments.NAME, memory=arguments.memory, image=arguments.image, script=arguments.script) elif arguments.config: # arguments.NAME d = vagrant.vm.info(name=arguments.NAME) result = Printer.attribute(d, output=arguments.format) print(result) elif arguments.ip: data = [] result = vagrant.vm.execute(arguments.NAME, "ifconfig") if result is not None: lines = result.splitlines()[:-1] for line in lines: if "inet addr" in line: line = line.replace("inet addr", "ip") line = ' '.join(line.split()) _adresses = line.split(" ") address = {} for element in _adresses: attribute, value = element.split(":") address[attribute] = value data.append(address) if arguments.all: d = {} i = 0 for e in data: d[str(i)] = e i = i + 1 result = Printer.attribute(d, output=arguments.format) print(result) else: for element in data: ip = element['ip'] if ip == "127.0.0.1" or ip.startswith("10."): pass else: print(element['ip']) elif arguments.boot: d = defaults() arguments.memory = arguments["--memory"] or d.memory arguments.image = arguments["--image"] or d.image arguments.script = arguments["--script"] or d.script arguments.port = arguments["--port"] or d.port vagrant.vm.boot(name=arguments.NAME, memory=arguments.memory, image=arguments.image, script=arguments.script, port=arguments.port) elif arguments.delete: result = vagrant.vm.delete(name=arguments.NAME) print(result) elif arguments.ssh: if arguments.COMMAND is None: os.system("cd {NAME}; vagrant ssh {NAME}".format(**arguments)) else: result = vagrant.vm.execute(arguments.NAME, arguments.COMMAND) if result is not None: lines = result.splitlines()[:-1] for line in lines: print(line) else: print("use help")
def get_floating_ip(cls, cloudname, floating_ip_or_id, output='table'): """ Method to get floating ip info :param cloudname: :param floating_ip_or_id: :return: floating ip info """ try: cloud_provider = CloudProvider(cloudname).provider result = None # check if argument is ip or uuid if cls.isIPAddr(ip_or_id=floating_ip_or_id): # get floating ip list floating_ips = cls.get_floating_ip_list(cloudname) for floating_ip in list(floating_ips): ip_addr = floating_ip["ip"] # if argument ip matches floating ip addr if ip_addr == floating_ip_or_id: result = floating_ip break else: # find by floating ip uuid result = cloud_provider.get_floating_ip( floating_ip_id=floating_ip_or_id) # Could not find floating IP from given args if result is None: return None instance_id = result["instance_id"] instance_name = None if instance_id is not None: # lookup instance_name from id instance_name = cls.find_instance_name(cloudname=cloudname, instance_id=instance_id) # add instance_name to dict result["instance_name"] = instance_name # add misc details to response result["cloud"] = cloudname result["user"] = cloud_provider.cloud_details["credentials"][ "OS_USERNAME"] result["project"] = cloud_provider.cloud_details["credentials"][ "OS_TENANT_NAME"] return Printer.attribute(result, header=["name", "value"], output=output) except Exception: # auto detect floating-ip-id floating_ips = cls.get_floating_ip_list(cloudname) # for each floating-ip from list for floating_ip in list(floating_ips): if floating_ip["id"].startswith(floating_ip_or_id) or \ floating_ip["ip"].startswith(floating_ip_or_id): # confirm choice with user print("Did you mean floating-ip [{}] ? (y/n)".format( floating_ip["ip"])) choice = input().lower() # if yes, return dict if choice == 'y': return Printer.attribute(floating_ip, header=["name", "value"], output=output) # Console.error(ex.message) return
def do_vm(self, args, arguments): """ :: Usage: vm default [--cloud=CLOUD][--format=FORMAT] vm refresh [all][--cloud=CLOUD] vm boot [--name=NAME] [--cloud=CLOUD] [--username=USERNAME] [--image=IMAGE] [--flavor=FLAVOR] [--group=GROUP] [--public] [--secgroup=SECGROUP] [--key=KEY] [--dryrun] vm boot [--n=COUNT] [--cloud=CLOUD] [--username=USERNAME] [--image=IMAGE] [--flavor=FLAVOR] [--group=GROUP] [--public] [--secgroup=SECGROUP] [--key=KEY] [--dryrun] vm ping [NAME] [N] vm console [NAME] [--group=GROUP] [--cloud=CLOUD] [--force] vm start [NAMES] [--group=GROUP] [--cloud=CLOUD] [--force] vm stop [NAMES] [--group=GROUP] [--cloud=CLOUD] [--force] vm terminate [NAMES] [--group=GROUP] [--cloud=CLOUD] [--force] vm delete [NAMES] [--group=GROUP] [--cloud=CLOUD] [--keep] [--dryrun] vm ip assign [NAMES] [--cloud=CLOUD] vm ip show [NAMES] [--group=GROUP] [--cloud=CLOUD] [--format=FORMAT] [--refresh] vm ip inventory [NAMES] [--header=HEADER] [--file=FILE] vm ssh [NAME] [--username=USER] [--quiet] [--ip=IP] [--cloud=CLOUD] [--key=KEY] [--command=COMMAND] vm rename [OLDNAMES] [NEWNAMES] [--force] [--dryrun] vm list [NAMES] [--cloud=CLOUDS|--active] [--group=GROUP] [--format=FORMAT] [--refresh] vm status [NAMES] vm wait [--cloud=CLOUD] [--interval=SECONDS] vm info [--cloud=CLOUD] [--format=FORMAT] vm check NAME vm username USERNAME [NAMES] [--cloud=CLOUD] Arguments: COMMAND positional arguments, the commands you want to execute on the server(e.g. ls -a) separated by ';', you will get a return of executing result instead of login to the server, note that type in -- is suggested before you input the commands NAME server name. By default it is set to the name of last vm from database. NAMES server name. By default it is set to the name of last vm from database. KEYPAIR_NAME Name of the openstack keypair to be used to create VM. Note this is not a path to key. NEWNAMES New names of the VM while renaming. OLDNAMES Old names of the VM while renaming. Options: --username=USERNAME the username to login into the vm. If not specified it will be guessed from the image name and the cloud --ip=IP give the public ip of the server --cloud=CLOUD give a cloud to work on, if not given, selected or default cloud will be used --count=COUNT give the number of servers to start --detail for table print format, a brief version is used as default, use this flag to print detailed table --flavor=FLAVOR give the name or id of the flavor --group=GROUP give the group name of server --secgroup=SECGROUP security group name for the server --image=IMAGE give the name or id of the image --key=KEY specify a key to use, input a string which is the full path to the private key file --keypair_name=KEYPAIR_NAME Name of the openstack keypair to be used to create VM. Note this is not a path to key. --user=USER give the user name of the server that you want to use to login --name=NAME give the name of the virtual machine --force rename/ delete vms without user's confirmation --command=COMMAND specify the commands to be executed Description: commands used to boot, start or delete servers of a cloud vm default [options...] Displays default parameters that are set for vm boot either on the default cloud or the specified cloud. vm boot [options...] Boots servers on a cloud, user may specify flavor, image .etc, otherwise default values will be used, see how to set default values of a cloud: cloud help vm start [options...] Starts a suspended or stopped vm instance. vm stop [options...] Stops a vm instance . vm delete [options...] Delete servers of a cloud, user may delete a server by its name or id, delete servers of a group or servers of a cloud, give prefix and/or range to find servers by their names. Or user may specify more options to narrow the search vm floating_ip_assign [options...] assign a public ip to a VM of a cloud vm ip show [options...] show the ips of VMs vm ssh [options...] login to a server or execute commands on it vm list [options...] same as command "list vm", please refer to it vm status [options...] Retrieves status of last VM booted on cloud and displays it. Tip: give the VM name, but in a hostlist style, which is very convenient when you need a range of VMs e.g. sample[1-3] => ['sample1', 'sample2', 'sample3'] sample[1-3,18] => ['sample1', 'sample2', 'sample3', 'sample18'] Quoting commands: cm vm login gvonlasz-004 --command=\"uname -a\" """ """ # terminate # issues a termination to the cloud, keeps vm in database # delete # issues a terminate if not already done # (remember you do not have to go to cloud if state is already terminated) # deletes the vm from database # # bulk rename rename abc[0-1] def[3-4] renames the abc0,abc1 -> def3,def4 if arguments["rename"]: oldnames = Parameter.expand(arguments["OLDNAME"]) newnames = Parameter.expand(arguments["NEWNAME"]) # check if new names ar not already taken # to be implemented if len(oldnames) == len(newnames): for i in range(0, len(oldnames)): oldname = oldnames[i] newname = newnames[i] if newname is None or newname == '': print("New node name cannot be empty") else: print(Cluster.rename_node(clusterid, oldname, newname)) """ cm = CloudmeshDatabase() def _print_dict(d, header=None, output='table'): return Printer.write(d, order=["id", "name", "status"], output=output, sort_keys=True) def _print_dict_ip(d, header=None, output='table'): return Printer.write(d, order=["network", "version", "addr"], output=output, sort_keys=True) def get_vm_name(name=None, offset=0, fill=3): if name is None: count = Default.get_counter(name='name') + offset prefix = Default.user if prefix is None or count is None: Console.error("Prefix and Count could not be retrieved correctly.", traceflag=False) return name = prefix + "-" + str(count).zfill(fill) return name def _refresh_cloud(cloud): try: msg = "Refresh VMs for cloud {:}.".format(cloud) if Vm.refresh(cloud=cloud): Console.ok("{:} OK.".format(msg)) else: Console.error("{:} failed".format(msg), traceflag=False) except Exception as e: Console.error("Problem running VM refresh", traceflag=False) def _get_vm_names(): vm_list = cm.find(kind="vm") vms = [vm["name"] for vm in vm_list] names = pattern = arguments["NAMES"] if pattern is not None: if "*" in pattern: names = search(vms, pattern) else: names = Parameter.expand(names) if names == ['last'] or names is None: names == [Default.vm] return vm_list, names cloud = arguments["--cloud"] or Default.cloud config = ConfigDict("cloudmesh.yaml") active_clouds = config["cloudmesh"]["active"] def _refresh(cloud): all = arguments["all"] or None if all is None: _refresh_cloud(cloud) else: for cloud in active_clouds: _refresh_cloud(cloud) arg = dotdict(arguments) arg.cloud = arguments["--cloud"] or Default.cloud arg.image = arguments["--image"] or Default.get(name="image", category=arg.cloud) arg.flavor = arguments["--flavor"] or Default.get(name="flavor", category=arg.cloud) arg.group = arguments["--group"] or Default.group arg.secgroup = arguments["--secgroup"] or Default.secgroup arg.key = arguments["--key"] or Default.key arg.dryrun = arguments["--dryrun"] arg.name = arguments["--name"] arg.format = arguments["--format"] or 'table' arg.refresh = Default.refresh or arguments["--refresh"] arg.count = int(arguments["--n"] or 1) arg.dryrun = arguments["--dryrun"] arg.verbose = not arguments["--quiet"] # # in many cases use NAMES # if arg.NAMES is not None: # arg.names = Parameter.expand(arg.NAMES) # gvonlasz[001-002] gives ["gvonlasz-001", "gvonlasz-002"] # else: # arg.names = None # if arguments["boot"]: arg.username = arguments["--username"] or Image.guess_username(arg.image) is_name_provided = arg.name is not None arg.user = Default.user for index in range(0, arg.count): vm_details = dotdict({ "cloud": arg.cloud, "name": get_vm_name(arg.name, index), "image": arg.image, "flavor": arg.flavor, "key": arg.key, "secgroup": arg.secgroup, "group": arg.group, "username": arg.username, "user": arg.user }) # correct the username vm_details.username = Image.guess_username_from_category( vm_details.cloud, vm_details.image, username=arg.username) try: if arg.dryrun: print(Printer.attribute(vm_details, output=arg.format)) msg = "dryrun info. OK." Console.ok(msg) else: vm_id = Vm.boot(**vm_details) if vm_id is None: msg = "info. failed." Console.error(msg, traceflag=False) return "" # set name and counter in defaults Default.set_vm(value=vm_details.name) if is_name_provided is False: Default.incr_counter("name") # Add to group if vm_id is not None: Group.add(name=vm_details.group, species="vm", member=vm_details.name, category=vm_details.cloud) msg = "info. OK." Console.ok(msg) except Exception as e: Console.error("Problem booting instance {name}".format(**vm_details), traceflag=False) elif arguments["username"]: arg.username = arguments["--username"] or Image.guess_username(arg.image) cloud = arg.cloud username = arg.USERNAME if arg.NAMES is None: names = [Default.vm] else: names = Parameter.expand(arg.NAMES) if len(names) == 0: return for name in names: arg.name = name Console.ok("Set username for {cloud}:{name} to {USERNAME}".format(**arg)) Vm.set_login_user(name=name, cloud=cloud, username=username) elif arguments["default"]: try: count = Default.get_counter() prefix = Username() if prefix is None or count is None: Console.error("Prefix and Count could not be retrieved correctly.", traceflag=False) return vm_name = prefix + "-" + str(count).zfill(3) arg = { "name": vm_name, "cloud": arguments["--cloud"] or Default.cloud } for attribute in ["image", "flavor"]: arg[attribute] = Default.get(name=attribute, category=cloud) for attribute in ["key", "group", "secgroup"]: arg[attribute] = Default.get(name=attribute, category='general') output = arguments["--format"] or "table" print(Printer.attribute(arg, output=output)) msg = "info. OK." Console.ok(msg) ValueError("default command not implemented properly. Upon " "first install the defaults should be read from yaml.") except Exception as e: # Error.traceback(e) Console.error("Problem listing defaults", traceflag=False) elif arguments["ping"]: try: if arguments["NAME"] is None and arguments["N"] is None: name = arguments["NAME"] or Default.vm n = arguments["N"] or 1 elif arguments["NAME"].isdigit(): n = arguments["NAME"] name = Default.vm else: name = arguments["NAME"] or Default.vm n = arguments["N"] or 1 print("Ping:", name, str(n)) vm = dotdict(Vm.list(name=name, category=cloud, output="dict")["dict"]) ip = vm.floating_ip result = Shell.ping(host=ip, count=n) print(result) except Exception as e: Console.error(e.message, traceflag=False) elif arguments["console"]: try: name = arguments["NAME"] or Default.vm vm = dotdict(Vm.list(name=name, category=cloud, output="dict")["dict"]) cloud_provider = CloudProvider(cloud).provider vm_list = cloud_provider.list_console(vm.uuid) print(vm_list) msg = "info. OK." Console.ok(msg) except Exception as e: # Error.traceback(e) Console.error("Problem retrieving status of the VM", traceflag=False) elif arguments["status"]: try: cloud_provider = CloudProvider(cloud).provider vm_list = cloud_provider.list_vm(cloud) vms = [vm_list[i]["name"] for i in vm_list ] print ("V", vms) pattern = arguments["NAMES"] if pattern is not None: if "*" in pattern: print ("serach") names = search(vms, pattern) else: names = Parameter.expand() for i in vm_list: if vm_list[i]["name"] in names: print("{} {}".format(vm_list[i]["status"], vm_list[i]["name"])) else: print("{} {}".format(vm_list[0]["status"], vm_list[0]["name"])) except Exception as e: # Error.traceback(e) Console.error("Problem retrieving status of the VM", traceflag=True) elif arguments["wait"]: interval = arguments["--interval"] or 5 try: cloud_provider = CloudProvider(cloud).provider for i in range(1,10): vm_list = cloud_provider.list_vm(cloud) time.sleep(float(1)) d = {} for id in vm_list: vm = vm_list[id] d[vm["name"]] = vm["status"] print (d) print("{} {}".format(vm_list[0]["status"], vm_list[0]["name"])) if vm_list[0]["status"] in ['ACTIVE']: return except Exception as e: # Error.traceback(e) Console.error("Problem retrieving status of the VM", traceflag=True) elif arguments["info"]: try: cloud_provider = CloudProvider(cloud).provider vms = cloud_provider.list_vm(cloud) vm = vms[0] output_format = arguments["--format"] or "table" print(Printer.attribute(vm, output=output_format)) msg = "info. OK." Console.ok(msg) except Exception as e: # Error.traceback(e) Console.error("Problem retrieving status of the VM", traceflag=False) elif arguments["check"]: test = {} try: names = Parameter.expand(arguments["NAME"]) id = 0 for name in names: print("Not implemented: {}".format(name)) # TODO: check the status of the vms status = "active" # TODO: check if they have a floating ip # TODO: get ip floating_ip = "127.0.0.1" ip = True # ping # TODO: ping the machine with the shell command ping = True # check if one can login and run a command check = False try: r = Shell.execute("uname", "-a") # do a real check check = True except: check = False test[name] = { "id": id, "name": name, "status": status, "ip": ip, "ping": ping, "login": check } id += 1 pprint(test) print(Printer.write(test, order=["id", "name", "status", "ip", "ping", "login"], output="table", sort_keys=True)) msg = "not yet implemented. failed." Console.error(msg, traceflag=False) except Exception as e: # Error.traceback(e) Console.error("Problem retrieving status of the VM", traceflag=False) elif arguments["start"]: try: servers = Parameter.expand(arguments["NAMES"]) # If names not provided, take the last vm from DB. if len(servers) == 0: last_vm = Default.vm if last_vm is None: Console.error("No VM records in database. Please run vm refresh.", traceflag=False) return "" name = last_vm["name"] # print(name) servers = list() servers.append(name) group = arguments["--group"] force = arguments["--force"] # if default cloud not set, return error if not cloud: Console.error("Default cloud not set.", traceflag=False) return "" Vm.start(cloud=cloud, servers=servers) msg = "info. OK." Console.ok(msg) except Exception as e: # Error.traceback(e) Console.error("Problem starting instances", traceflag=False) elif arguments["stop"]: try: servers = Parameter.expand(arguments["NAMES"]) # If names not provided, take the last vm from DB. if servers is None or len(servers) == 0: last_vm = Default.vm if last_vm is None: Console.error("No VM records in database. Please run vm refresh.", traceflag=False) return "" name = last_vm["name"] # print(name) servers = list() servers.append(name) group = arguments["--group"] force = arguments["--force"] # if default cloud not set, return error if not cloud: Console.error("Default cloud not set.", traceflag=False) return "" Vm.stop(cloud=cloud, servers=servers) msg = "info. OK." Console.ok(msg) except Exception as e: # Error.traceback(e) Console.error("Problem stopping instances", traceflag=False) elif arguments["refresh"]: _refresh(cloud) elif arguments["delete"]: dryrun = arguments["--dryrun"] group = arguments["--group"] force = not arguments["--keep"] cloud = arguments["--cloud"] vms, servers = _get_vm_names() if servers in [None, []]: Console.error("No vms found.", traceflag=False) return "" for server in servers: if dryrun: Console.ok("Dryrun: delete {}".format(server)) else: Vm.delete(servers=[server], force=force) return "" elif arguments["ip"] and arguments["assign"]: if arguments["NAMES"] is None: names = [Default.vm] else: names = Parameter.expand(arguments["NAMES"]) for name in names: # ip = Network.get_floatingip(....) vm = dotdict(Vm.list(name=name, category=cloud, output="dict")["dict"]) if vm.floating_ip is None: Console.ok("Assign IP to {}".format(name)) try: floating_ip = Network.find_assign_floating_ip(cloudname=cloud, instance_id=name) Vm.refresh(cloud=cloud) if floating_ip is not None: print( "Floating IP assigned to {:} is {:}".format( name, floating_ip)) msg = "info. OK." Console.ok(msg) except Exception as e: Console.error("Problem assigning floating ips.", traceflag=False) else: Console.error("VM {} already has a floating ip: {}".format(name, vm.floating_ip), traceflag=False) elif arguments["ip"] and arguments["inventory"]: vms, names = _get_vm_names() if names in [None, []]: if str(Default.vm) in ['None', None]: Console.error("The default vm is not set.", traceflag=False) return "" else: names = [Default.vm] header = arguments["--header"] or "[servers]" filename = arguments["--file"] or "inventory.txt" try: vm_ips = [] for vm in vms: if vm["name"] in names: print (vm["name"]) vm_ips.append(vm["floating_ip"]) result = header + "\n" result += '\n'.join(vm_ips) Console.ok("Creating inventory file: {}".format(filename)) Console.ok(result) with open(filename, 'w') as f: f.write(result) except Exception as e: Console.error("Problem getting ip addresses for instance", traceflag=True) elif arguments["ip"] and arguments["show"]: if arguments["NAMES"] is None: if str(Default.vm) in ['None', None]: Console.error("The default vm is not set.", traceflag=False) return "" else: names = [Default.vm] else: names = Parameter.expand(arguments["NAMES"]) group = arguments["--group"] output_format = arguments["--format"] or "table" refresh = arguments["--refresh"] try: ips = Ip.list(cloud=arg.cloud, output=output_format, names=names) print(ips) except Exception as e: Console.error("Problem getting ip addresses for instance", traceflag=False) elif arguments["ssh"]: def _print(msg): if arg.verbose: Console.msg(msg) chameleon = "chameleon" in ConfigDict(filename="cloudmesh.yaml")["cloudmesh"]["clouds"][arg.cloud][ "cm_host"] if chameleon: arg.username = "******" elif arg.cloud == "azure": arg.username = ConfigDict(filename="cloudmesh.yaml")["cloudmesh"]["clouds"]["azure"]["default"]["username"] else: if arg.username is None: Console.error("Could not guess the username of the vm", traceflag=False) return arg.username = arguments["--username"] or Image.guess_username(arg.image) arg.command = arguments["--command"] data = dotdict({ 'name': arguments["NAME"] or Default.vm, 'username': arg.username, 'cloud': arg.cloud, 'command': arg.command }) _print("login {cloud}:{username}@{name}".format(**data)) vm = Vm.get(data.name, category=data.cloud) Vm.set_login_user(name=data.name, cloud=data.cloud, username=data.username) data.floating_ip = vm.floating_ip data.key = arguments["--key"] or Default.key _print(Printer.attribute(data)) ''' if vm.username is None: user_from_db = Vm.get_login_user(vm.name, vm.cloud) user_suggest = user_from_db or Default.user username = input("Username (Default: {}):".format(user_suggest)) or user_suggest Vm.set_login_user(name=data.name, cloud=cloud, username=data.username) ''' ip = arguments["--ip"] commands = arguments["--command"] ip_addresses = [] cloud_provider = CloudProvider(cloud).provider ip_addr = cloud_provider.get_ips(vm.name) ipaddr_dict = Vm.construct_ip_dict(ip_addr, cloud) for entry in ipaddr_dict: ip_addresses.append(ipaddr_dict[entry]["addr"]) if len(ip_addresses) > 0: if ip is not None: if ip not in ip_addresses: Console.error("IP Address specified does not match with the host.", traceflag=False) return "" else: _print("Determining IP Address to use with a ping test.") # This part assumes that the ping is allowed to the machine. for ipadd in ip_addresses: _print("Checking {:}...".format(ipadd)) try: # Evading ping test, as ping is not enabled for VMs on Azure cloud # socket.gethostbyaddr(ipadd) # ip will be set if above command is successful. ip = ipadd except socket.herror: _print("Cannot reach {:}.".format(ipadd)) if ip is None: _print("Unable to connect to the machine") return "" else: _print("IP to be used is: {:}".format(ip)) # # TODO: is this correctly implemented # if not cloud == 'azure': SecGroup.enable_ssh(cloud=cloud) if arg.verbose: Console.info("Connecting to Instance at IP:" + format(ip)) # Constructing the ssh command to connect to the machine. sshcommand = "ssh" if arg.key is not None: sshcommand += " -i {:}".format(arg.key) sshcommand += " -o StrictHostKeyChecking=no" sshcommand += " {:}@{:}".format(data.username, ip) if commands is not None: sshcommand += " \"{:}\"".format(commands) # print(sshcommand) os.system(sshcommand) else: Console.error("No Public IPs found for the instance", traceflag=False) elif arguments["list"]: # groups = Group.list(output="dict") arg = dotdict(arguments) arg.names = arguments["NAMES"] arg.group = arguments["--group"] if arg.group is None: arg.group = [] else: arg.group = Parameter.expand(arguments["--group"]) arg.refresh = arguments["--refresh"] or Default.refresh if arg.NAMES is not None: arg.names = Parameter.expand(arguments["NAMES"]) else: arg.names = ["all"] _format = arguments["--format"] or "table" if arguments["--active"]: clouds = active_clouds else: if arguments["--cloud"]: clouds = Parameter.expand(arguments["--cloud"]) else: clouds = [Default.cloud] try: d = ConfigDict("cloudmesh.yaml") for cloud in clouds: if arg.refresh: _refresh(cloud) Console.ok("Listing VMs on Cloud: {:}".format(cloud)) vms = Vm.list(category=cloud, output="raw") # print ("XXX", type(vms), vms) if vms is None: break result = [] if "all" in arg.names: if result is None: result = [] else: result = vms elif arg.group is not None and len(arg.group) > 0: for vm in vms: if vm["group"] in arg.group: result.append(vm) elif arg.names is not None and len(arg.names) > 0: for vm in vms: if vm["name"] in arg.names: result.append(vm) if len(result) > 0: # print(result) (order, header) = CloudProvider(cloud).get_attributes("vm") print(Printer.write(result, order=order, output=_format) ) else: Console.error("No data found with requested parameters.", traceflag=False) except Exception as e: # Error.traceback(e) Console.error("Problem listing all instances", traceflag=False) elif arguments["rename"]: try: oldnames = Parameter.expand(arguments["OLDNAMES"]) newnames = Parameter.expand(arguments["NEWNAMES"]) force = arguments["--force"] if oldnames is None or newnames is None: Console.error("Wrong VMs specified for rename", traceflag=False) elif len(oldnames) != len(newnames): Console.error("The number of VMs to be renamed is wrong", traceflat=False) else: for i in range(0, len(oldnames)): oldname = oldnames[i] newname = newnames[i] if arguments["--dryrun"]: Console.ok("Rename {} to {}".format(oldname, newname)) else: Vm.rename(cloud=cloud, oldname=oldname, newname=newname, force=force ) msg = "info. OK." Console.ok(msg) except Exception as e: # Error.traceback(e) Console.error("Problem deleting instances", traceflag=False) return ""
def get_floating_ip(cls, cloudname, floating_ip_or_id, output='table'): """ Method to get floating ip info :param cloudname: :param floating_ip_or_id: :return: floating ip info """ try: cloud_provider = CloudProvider(cloudname).provider result = None # check if argument is ip or uuid if cls.isIPAddr(ip_or_id=floating_ip_or_id): # get floating ip list floating_ips = cls.get_floating_ip_list(cloudname) for floating_ip in list(floating_ips): ip_addr = floating_ip["ip"] # if argument ip matches floating ip addr if ip_addr == floating_ip_or_id: result = floating_ip break else: # find by floating ip uuid result = cloud_provider.get_floating_ip(floating_ip_id=floating_ip_or_id) # Could not find floating IP from given args if result is None: return None instance_id = result["instance_id"] instance_name = None if instance_id is not None: # lookup instance_name from id instance_name = cls.find_instance_name(cloudname=cloudname, instance_id=instance_id) # add instance_name to dict result["instance_name"] = instance_name # add misc details to response result["cloud"] = cloudname result["user"] = cloud_provider.cloud_details["credentials"]["OS_USERNAME"] result["project"] = cloud_provider.cloud_details["credentials"]["OS_TENANT_NAME"] return Printer.attribute(result, header=[ "name", "value" ], output=output) except Exception: # auto detect floating-ip-id floating_ips = cls.get_floating_ip_list(cloudname) # for each floating-ip from list for floating_ip in list(floating_ips): if floating_ip["id"].startswith(floating_ip_or_id) or \ floating_ip["ip"].startswith(floating_ip_or_id): # confirm choice with user print("Did you mean floating-ip [{}] ? (y/n)".format(floating_ip["ip"])) choice = input().lower() # if yes, return dict if choice == 'y': return Printer.attribute(floating_ip, header=[ "name", "value" ], output=output) # Console.error(ex.message) return
def do_hpc(self, args, arguments): # noinspection PyPep8 """ :: Usage: hpc queue [--job=NAME][--cluster=CLUSTER][--format=FORMAT] hpc info [--cluster=CLUSTER][--format=FORMAT] hpc run list [ID] [--cluster=CLUSTER] hpc run output [ID] [--cluster=CLUSTER] hpc run rm [ID] [--cluster=CLUSTER] hpc run SCRIPT [--queue=QUEUE] [--t=TIME] [--N=nodes] [--name=NAME] [--cluster=CLUSTER][--dir=DIR][--group=GROUP][--format=FORMAT] hpc delete --job=NAME [--cluster=CLUSTER][--group=GROUP] hpc delete all [--cluster=CLUSTER][--group=GROUP][--format=FORMAT] hpc status [--job=name] [--cluster=CLUSTER] [--group=GROUP] hpc test --cluster=CLUSTER [--time=SECONDS] Options: --format=FORMAT the output format [default: table] Examples: Special notes if the group is specified only jobs from that group are considered. Otherwise the default group is used. If the group is set to None, all groups are used. cm hpc queue lists the details of the queues of the hpc cluster cm hpc queue --job=NAME lists the details of the job in the queue of the hpc cluster cm hpc info lists the details of the hpc cluster cm hpc run SCRIPT submits the script to the cluster. The script will be copied prior to execution into the home directory on the remote machine. If a DIR is specified it will be copied into that dir. The name of the script is either specified in the script itself, or if not the default naming scheme of cloudmesh is used using the same index incremented name as in vms fro clouds: cloudmes husername-index cm hpc delete all kills all jobs on the default hpc group cm hpc delete --job=NAME kills a job with a given name or job id cm default cluster=NAME sets the default hpc cluster cm hpc status returns the status of all jobs cm hpc status job=ID returns the status of the named job cm hpc test --cluster=CLUSTER --time=SECONDS submits a simple test job to the named cluster and returns if the job could be successfully executed. This is a blocking call and may take a long time to complete dependent on if the queuing system of that cluster is busy. It will only use one node/core and print the message #CLOUDMESH: Test ok in that is being looked for to identify if the test is successful. If time is used, the job is terminated after the time is elapsed. Examples: cm hpc queue cm hpc queue --job=xxx cm hpc info cm hpc delete --job=6 cm hpc delete all cm hpc status cm hpc status --job=6 cm hpc run uname cm hpc run ~/test.sh --cluster=india """ format = arguments['--format'] cluster = arguments['--cluster'] or Default.cluster arguments["CLUSTER"] = cluster if cluster is None: Console.error("Default cluster doesn't exist") return batch = BatchProvider(cluster) if arguments["queue"]: name = arguments['--job'] result = batch.queue(cluster, format=format, job=name) Console.msg(result) elif arguments["info"]: Console.msg(batch.info(cluster, format)) elif arguments['delete'] and arguments['all']: group = arguments['--group'] or Default.get(name='group') if group is None: Console.error('set default group using: default group=<value> --cloud=general') return Console.ok(batch.delete(cluster, None, group)) elif arguments["delete"]: job = arguments['--job'] Console.ok(batch.delete(cluster, job)) elif arguments["status"]: name = arguments['--job'] result = batch.queue(cluster, format=format, job=name) Console.msg(result) elif arguments["test"]: time_secs = arguments['--time'] if time_secs: time = '00:00:' + time_secs else: time = '00:00:10' # give a default time of 10 secs print(batch.test(cluster, time)) elif arguments["run"] and arguments["list"]: # hpc experiment list [--cluster=CLUSTER] if arguments["ID"]: print("# List of experiment {ID} on Cluster {CLUSTER}".format(**arguments)) result = Experiment.list(cluster, id=arguments["ID"], format="list") if result is not None: print("\n".join(result)) else: Console.error("Could not find experiment {ID} on {CLUSTER}".format(**arguments)) else: print("# List of experiments on Cluster {CLUSTER}".format(**arguments)) ids = Experiment.list(cluster, id=None, format="list") if ids is not None: print(", ".join([str(i) for i in ids])) else: Console.error("Could not find experiment {ID} on {CLUSTER}".format(**arguments)) elif arguments["run"] and arguments["rm"]: # hpc experiment list [--cluster=CLUSTER] if arguments["ID"]: force = yn_choice("Would you like to delete experiment {ID} on Cluster {CLUSTER}".format(**arguments)) if force: try: result = Experiment.rm(cluster, id=arguments["ID"]) Console.ok("Experiment {ID} on Cluster {CLUSTER} deleted".format(**arguments)) except: Console.error("Could not delete experiment {ID} on {CLUSTER}".format(**arguments)) else: result = Experiment.list(cluster, id=None, format="list") if result is not None: arguments['experiments'] = ", ".join([str(i) for i in result]) else: Console.error("Could not find experiment {ID} on {CLUSTER}".format(**arguments)) return "" force = yn_choice("Would you like to delete the experiments {experiments} on Cluster {CLUSTER}".format( **arguments)) if force: try: result = Experiment.rm(cluster, id=None) Console.ok("Experiments {experiments} on Cluster {CLUSTER} deleted".format(**arguments)) except: Console.error("Could delete the experiments on {CLUSTER}".format(**arguments)) return "" elif arguments["run"] and arguments["output"]: # hpc experiment list [--cluster=CLUSTER] if arguments["ID"]: print("# List of experiment {ID} on Cluster {CLUSTER}".format(**arguments)) result = Experiment.output(cluster, id=arguments["ID"], format="list") if result is not None: print("\n".join(result)) else: Console.error("Could not find experiment {ID} on {CLUSTER}".format(**arguments)) else: print("# List of experiments on Cluster {CLUSTER}".format(**arguments)) ids = Experiment.output(cluster, id=None, format="list") if ids is not None: print(", ".join([str(i) for i in ids])) else: Console.error("Could not find experiment {ID} on {CLUSTER}".format(**arguments)) elif arguments["run"]: queue = arguments['--queue'] or Default.get(name='queue') # if not queue: # Console.error('set default queue using: default queue=<value>') # return group = arguments['--group'] or Default.get(name='group') if group is None: Console.error('set default group using: default group=<value> --cloud=general') return script = arguments['SCRIPT'] arg_dict = { '-name': arguments['--name'], '-p': queue, '-t': arguments['--t'], '-N': arguments['--N'] } result = batch.run(cluster, group, script, **arg_dict) if isinstance(result, dict): print(Printer.attribute(result)) Console.ok("Experiment {count}: Started batch job {job_id} on {cluster}".format(**result)) else: Console.error(result) return ""
def do_vbox(argv): """ :: Usage: vbox version [--format=FORMAT] vbox image list [--format=FORMAT] vbox image find NAME vbox image add NAME vbox vm list [--format=FORMAT] [-v] vbox vm delete NAME vbox vm config NAME vbox vm ip NAME [--all] vbox create NAME ([--memory=MEMORY] [--image=IMAGE] [--script=SCRIPT] | list) vbox vm boot NAME ([--memory=MEMORY] [--image=IMAGE] [--port=PORT] [--script=SCRIPT] | list) vbox vm ssh NAME [-e COMMAND] """ arg = dotdict(docopt(do_vbox.__doc__, argv)) arg.format = arg["--format"] or "table" arg.verbose = arg["-v"] arg.all = arg["--all"] if arg.version: versions = { "vagrant": { "attribute": "Vagrant Version", "version": vagrant.version(), }, "cloudmesh-vbox": { "attribute":"cloudmesh vbox Version", "version": __version__ } } _LIST_PRINT(versions, arg.format) elif arg.image and arg.list: l = vagrant.image.list(verbose=arg.verbose) _LIST_PRINT(l, arg.format, order=["name", "provider", "date"]) elif arg.image and arg.add: l = vagrant.image.add(arg.NAME) print(l) elif arg.image and arg.find: l = vagrant.image.find(arg.NAME) print(l) elif arg.vm and arg.list: l = vagrant.vm.list() _LIST_PRINT(l, arg.format, order=["name", "state", "id", "provider", "directory"]) elif arg.create and arg.list: result = Shell.cat("{NAME}/Vagrantfile".format(**arg)) print (result) elif arg.create: d = defaults() arg.memory = arg["--memory"] or d.memory arg.image = arg["--image"] or d.image arg.script = arg["--script"] or d.script vagrant.vm.create( name=arg.NAME, memory=arg.memory, image=arg.image, script=arg.script) elif arg.config: # arg.NAME d = vagrant.vm.info(name=arg.NAME) result = Printer.attribute(d, output=arg.format) print (result) elif arg.ip: data = [] result = vagrant.vm.execute(arg.NAME, "ifconfig") if result is not None: lines = result.splitlines()[:-1] for line in lines: if "inet addr" in line: line = line.replace("inet addr", "ip") line = ' '.join(line.split()) _adresses = line.split(" ") address = {} for element in _adresses: attribute, value = element.split(":") address[attribute] = value data.append(address) if arg.all: d = {} i = 0 for e in data: d[str(i)] = e i = i + 1 result = Printer.attribute(d, output=arg.format) print(result) else: for element in data: ip = element['ip'] if ip == "127.0.0.1" or ip.startswith("10."): pass else: print (element['ip']) elif arg.boot: d = defaults() arg.memory = arg["--memory"] or d.memory arg.image = arg["--image"] or d.image arg.script = arg["--script"] or d.script arg.port = arg["--port"] or d.port vagrant.vm.boot( name=arg.NAME, memory=arg.memory, image=arg.image, script=arg.script, port=arg.port) elif arg.delete: result = vagrant.vm.delete(name=arg.NAME) print(result) elif arg.ssh: if arg.COMMAND is None: os.system("cd {NAME}; vagrant ssh {NAME}".format(**arg)) else: result = vagrant.vm.execute(arg.NAME, arg.COMMAND) if result is not None: lines = result.splitlines()[:-1] for line in lines: print (line) else: print ("use help")