def do_check(self, args, arguments): """ :: Usage: check --cloud=CLOUD check checks some elementary setting for cloudmesh Options: --format=FORMAT the output format [default: table] --cloud=CLOUD the cloud name Examples: cm check cm check --cloud=kilo """ cloud = arguments["--cloud"] or Default.cloud if cloud is None: Console.error("Default cloud doesn't exist") print(locals()) Console.TODO("This command is not implemented yet", traceflag=False) Console.ok("{:} ok".format(cloud)) return ""
def do_echo(self, args, arguments): """ :: Usage: echo [-r COLOR] TEXT Arguments: TEXT The text message to print COLOR the color Options: -r COLOR The color of the text. [default: BLACK] Prints a text in the given color """ color = arguments["-r"] or "black" color = color.upper() text = arguments["TEXT"] if color is "black": Console.msg(text) else: Console.cprint(color, "", text) return ""
def set(cls, key, value, category='general', user=None, type='str'): """ sets the default value for a given category :param key: the dictionary key of the value to store it at. :param value: the value :param user: the username to store this default value at. :return: """ try: o = cls.get(name=key, category=category) if o is not None: cls.cm.update(kind=cls.__kind__, provider=cls.__provider__, filter={'name': key}, update={'value': value, 'type': type, 'user': user, 'category': category}) else: t = cls.cm.table(provider=cls.__provider__, kind=cls.__kind__) o = t(name=key, value=value, type=type, user=user, category=category) cls.cm.add(o) cls.cm.save() except Exception as e: Console.error("problem setting key value {}={}".format(key, value), traceflag=False)
def list(cls, **kwargs): """ This method lists all VMs of the cloud :param cloud: the cloud name """ try: if "name_or_id" in kwargs and kwargs["name_or_id"] is not None: if cls.isUuid(kwargs["name_or_id"]): elements = cls.cm.find("vm", cloud=kwargs["cloud"], uuid=kwargs["name_or_id"]) else: elements = cls.cm.find("vm", cloud=kwargs["cloud"], label=kwargs["name_or_id"]) else: elements = cls.cm.find("vm", cloud=kwargs["cloud"]) # print(elements) # order = ['id', 'uuid', 'name', 'cloud'] (order, header) = CloudProvider(kwargs["cloud"]).get_attributes("vm") # order = None if "name_or_id" in kwargs and kwargs["name_or_id"] is not None: return attribute_printer(elements.values()[0], output=kwargs["output_format"]) else: return dict_printer(elements, order=order, output=kwargs["output_format"]) except Exception as ex: Console.error(ex.message, ex)
def init(self, stackname='bds', activate=True, name=None, username=None, branch=None, overrides=None, playbooks=None, ips=None, force=False, update=False): factory = ProjectFactory() if stackname == 'bds': factory.use_bds() else: raise NotImplementedError(stackname) factory\ .set_project_name(name)\ .set_user_name(os.getenv('USER') if username is '$USER' else username)\ .set_branch(branch)\ .set_ips(ips)\ .set_overrides(overrides)\ .set_playbooks(playbooks)\ .activate(activate)\ .set_force(force=force)\ .set_update(update) project = factory() Console.info('Created project {}'.format(project.name))
def sync_metadata(self): path = os.path.join(self.path, '.cloudmesh_metadata') Console.debug_msg('Saving {} to {}'.format(self.__class__.__name__, path)) y = yaml.dump(self.__dict__, default_flow_style=False) with open(path, 'w') as fd: fd.write(y)
def do_reset(self, args, arguments): """ :: Usage: reset Description: DANGER: This method erases the database and quits the shell. Examples: clean """ filename = path_expand("~/.cloudmesh/cloudmesh.db") if os.path.exists(filename): os.remove(filename) Console.ok("Database reset") #r = self.do_register("profile") #r = self.do_info("") os._exit(0) # r = self.do_quit(None) # Console.error( # "Quitting the shell does not yet work. please exit the shell now.") return ""
def kill_tunnel(): pid = Comet.find_tunnel() if pid is None: Console.error("No tunnel to comet found") else: Console.ok("Killing the tunnel to comet") os.kill(pid, signal.SIGTERM)
def set(cls, key, value, user=None, type='str'): """ sets the default value for a given category :param key: the dictionary key of the value to store it at. :param value: the value :param user: the username to store this default value at. :return: """ try: o = cls.get(name=key) if o is not None: cls.cm.update(kind=cls.__kind__, provider=cls.__provider__, filter={'name': key}, update={ 'value': value, 'type': type }) else: t = cls.cm.table(provider=cls.__provider__, kind=cls.__kind__) o = t(name=key, value=value, type=type) cls.cm.add(o) cls.cm.save() except Exception as e: Console.error("problem setting key value {}={}".format(key, value), traceflag=False) Console.error(e.message)
def get_apikey(endpoint): config = ConfigDict("cloudmesh.yaml") cometConf = config["cloudmesh.comet"] defaultUser = cometConf["username"] user = input("Comet Nucleus Usename [%s]: " \ % defaultUser) if not user: user = defaultUser password = getpass.getpass() keyurl = "%s/getkey" % cometConf["endpoints"][endpoint][ "nucleus_base_url"] headers = {"ACCEPT": "application/json"} r = requests.get(keyurl, headers=headers, auth=HTTPBasicAuth(user, password)) if r.status_code == 200: keyobj = r.json() api_key = keyobj["key_name"] api_secret = keyobj["key"] config = ConfigDict("cloudmesh.yaml") config.data["cloudmesh"]["comet"]["endpoints"]\ [endpoint]["auth_provider"] = 'apikey' config.data["cloudmesh"]["comet"]["endpoints"]\ [endpoint]["apikey"]["api_key"] = api_key config.data["cloudmesh"]["comet"]["endpoints"]\ [endpoint]["apikey"]["api_secret"] = api_secret config.save() Console.ok("api key retrieval and set was successful!") else: Console.error("Error getting api key. " \ "Please check your username/password", traceflag=False)
def __repr__(self): try: return ("<{}> id={} name={} category={}: dict={}".format(self.kind, self.cm_id, self.name, self.category, self.__dict__)) except: Console.error("could not print object") return None
def table(cls, provider=None, kind=None, name=None): """ :param category: :param kind: :return: the table class based on a given table name. In case the table does not exist an exception is thrown """ t = None if name is not None: for t in cls.tables: if t.__tablename__ == name: return t if provider is None and kind is not None: t = cls.get_table_from_kind(kind) return t if provider is None and kind is None: Console.error("No Kind specified") return None for t in cls.tables: if t.__kind__ == kind and t.__provider__ == provider: return t Console.error("No table found for name={}, provider={}, kind={}".format(name, provider, kind))
def list_rules(cls, group=None, output='table'): """ This method gets the security group rules from the cloudmesh database :param uuid: :return: """ try: if group is None: rules = cls.cm.find(kind="secgrouprule") else: args = {"group": group} rules = cls.cm.find(kind="secgrouprule", **args) # check if rules exist if rules is None: return "No rules for security group={} in the database. Try cm secgroup refresh.".format( group) # return table return (Printer.write(rules, order=[ "user", "group", "category", "name", "fromPort", "toPort", "protocol", "cidr" ], output=output)) except Exception as ex: Console.error("Listing Security group rules") return None
def do_verbose(self, args, arguments): """ Usage: verbose (True | False) verbose NOTE: NOT YET IMPLEMENTED. If it sets to True, a command will be printed before execution. In the interactive mode, you may want to set it to False. When you use scripts, we recommend to set it to True. The default is set to False If verbose is specified without parameter the flag is toggled. """ # if args == '': # self.echo = not self.echo # else: # self.echo = arguments['True'] Console.error("verbose NOT YET IMPLEMENTED") return ""
def define(self, clustername=None, **kwargs): """Define a cluster. kwargs are passed to Cluster :returns: a cluster :rtype: :class:`Cluster` """ clustername = clustername or Default.generate_name(Names.CLUSTER_COUNTER) # remove None to defer default definitions to later for k in kwargs.keys(): if kwargs[k] is None: del kwargs[k] try: spec = db.select(SPECIFICATION, name=clustername, type='cluster')[0] spec.update(kwargs) db.updateObj(spec) except IndexError: spec = SPECIFICATION(clustername, 'cluster', kwargs) db.insert(spec) Default.set_specification(clustername) Console.ok('Defined cluster {}'.format(clustername))
def do_akey(self, args, arguments): """ :: Usage: akey akey list akey add --name=key-name --pub=pub-key-path --cert=certificate-file-path --pfx=pfx-file-path """ # pprint("Akey command pressed") # pprint(args) # pprint(arguments) if arguments['list']: print("Key list time") elif arguments['add']: Console.info("Azure key addition invoked") if arguments['--name']: print("name:" + arguments['--name']) key_name = arguments['--name'] if arguments['--cert']: print("cert:" + arguments['--cert']) certificate_path = arguments['--cert'] if arguments['--pub']: print("pub:" + arguments['--pub']) key_path = arguments['--pub'] if arguments['--pfx']: print("pfx:" + arguments['--pfx']) pfx_path = arguments['--pfx'] Key.add_azure_key_to_db(key_name, key_path, certificate_path, pfx_path) return ""
def run(cls,cloud,id): elements = cls.cm.find(kind="workflow", category='general', cm_id = id) Console.msg(elements) order = None Console.msg("Executing") header= None return elements
def list(cls, project, cloud="general"): """ This method queries the database to fetch list of secgroups filtered by cloud, tenant. :param project: :param cloud: :return: """ # noinspection PyUnreachableCode try: """ elements = cls.cm_db.query(model.SECGROUP).filter( model.SECGROUP.cloud == cloud, model.SECGROUP.project == project ).all() d = cls.toDict(elements) """ nova_client = CloudProvider.set(cloud) os_result = nova_client.security_groups.list() d = SecGroup.convert_list_to_dict(os_result) return dict_printer(d, order=["Id", "Name", "Description"], output="table") except Exception as ex: Console.error(ex.message, ex)
def get_apikey(endpoint): config = ConfigDict("cloudmesh.yaml") cometConf = config["cloudmesh.comet"] defaultUser = cometConf["username"] user = input("Comet Nucleus Usename [%s]: " \ % defaultUser) if not user: user = defaultUser password = getpass.getpass() keyurl = "%s/getkey" % cometConf["endpoints"][endpoint]["nucleus_base_url"] headers = {"ACCEPT": "application/json"} r = requests.get(keyurl, headers=headers, auth=HTTPBasicAuth(user, password)) if r.status_code == 200: keyobj = r.json() api_key = keyobj["key_name"] api_secret = keyobj["key"] config = ConfigDict("cloudmesh.yaml") config.data["cloudmesh"]["comet"]["endpoints"]\ [endpoint]["auth_provider"] = 'apikey' config.data["cloudmesh"]["comet"]["endpoints"]\ [endpoint]["apikey"]["api_key"] = api_key config.data["cloudmesh"]["comet"]["endpoints"]\ [endpoint]["apikey"]["api_secret"] = api_secret config.save() Console.ok("api key retrieval and set was successful!") else: Console.error("Error getting api key. " \ "Please check your username/password", traceflag=False)
def do_cloud(self, args, arguments): """ :: Usage: cloud list [--cloud=CLOUD] [--format=FORMAT] cloud activate CLOUD cloud deactivate CLOUD cloud info CLOUD managing the admins test test test test Arguments: KEY the name of the admin VALUE the value to set the key to Options: --cloud=CLOUD the name of the cloud [default: general] --format=FORMAT the output format [default: table] Description: Cloudmesh contains a cloudmesh.yaml file that contains templates for multiple clouds that you may or may not have access to. Hence it is useful to activate and deacivate clouds you like to use in other commands. To activate a cloud a user can simply use the activate command followed by the name of the cloud to be activated. To find out which clouds are available you can use the list command that will provide you with some basic information. As default it will print a table. Thus the commands:: cloud activate india cloud deactivate aws Will result in +----------------------+--------+-------------------+ | Cloud name | Active | Type | +----------------------+--------+-------------------+ | india | True | Openstack | +----------------------+--------+-------------------+ | aws | False | AWS | +----------------------+--------+-------------------+ To get ore information about the cloud you can use the command cloud info CLOUD It will call internally also the command uses in register See also: register """ # pprint(arguments) cloud = arguments["--cloud"] or Default.get_cloud() output_format = arguments["--format"] Console.error("TODO: Command not yet implemented.") pass
def var_replacer(self, line, c='$'): vars = self.var_finder(line, c=c) for v in vars["normal"]: value = str(Var.get(v)) line = line.replace(c + v, value) # replace in line the variable $v with value for v in vars["os"]: name = v.replace('os.', '') if name in os.environ: value = os.environ[name] line = line.replace(c + v, value) else: Console.error("can not find environment variable {}".format( v)) if c + v in line: value = os.environ(v) # replace in line the variable $v with value for v in vars["dot"]: try: config = ConfigDict("cloudmesh.yaml") print(config["cloudmesh.profile"]) value = config[v] line = line.replace(c + v, value) except Exception as e: Console.error("can not find variable {} in cloudmesh.yaml".format(value)) return line
def delete_rule(cls, cloud, secgroup, from_port, to_port, protocol, cidr): try: args = { "group": secgroup["uuid"], "fromPort": from_port, "toPort": to_port, "protocol": protocol, "cidr": cidr } rule = cls.cm.find(kind="secgrouprule", output="object", scope="first", **args) if rule is not None: # get the nova client for cloud cloud_provider = CloudProvider(cloud).provider # delete the rule from the cloud cloud_provider.delete_secgroup_rule(rule.uuid) # delete the local db record cls.cm.delete(rule) return "Rule [{fromPort} | {toPort} | {protocol} | {cidr}] deleted" \ .format(**args) else: return None except Exception as ex: Console.error("delete rule") return
def get(cls, name=None, cloud="general"): """ This method queries the database to fetch secgroup with given name filtered by cloud. :param name: :param cloud: :return: """ try: args = { "name": name, 'scope': 'fisrt', 'kind': "secgroup", "output": "object", } if cloud is not None and cloud is not 'general': args["category"] = cloud secgroup = cls.cm.find(**args) if secgroup is None: return None else: return secgroup[0] except Exception as ex: Console.error("get secgroup") return None
def do_who(self, args, arguments): """ :: Usage: who hostname """ try: logon = who.logon() if logon is False: Console.error("Could not logon") return except: Console.error("Could not logon") # pprint (arguments) output_format = arguments["--format"] or "table" if arguments["status"]: pass ValueError("NOT yet implemented") return ""
def do_reset(self, args, arguments): """ :: Usage: reset Description: DANGER: This method erases the database. Examples: clean """ filename = path_expand("~/.cloudmesh/cloudmesh.db") if os.path.exists(filename): os.remove(filename) Console.ok("Database reset") r = self.do_quit(None) Console.error( "Quitting the shell does not yet work. please exit the shell now.") return ""
def list_image(self, cloudname, **kwargs): # return self.list(self.provider.list_images, cloudnames, kwargs) Console.info("In list_images of libcloud") images = self.provider.list_images() self._print(images) image_dict = self._to_dict(images) return image_dict
def do_quota(self, args, arguments): """ :: Usage: quota list [--cloud=CLOUD] [--tenant=TENANT] [--format=FORMAT] Prints quota limit on a current project/tenant Options: --format=FORMAT the output format [default: table] --cloud=CLOUD the cloud name --tenant=TENANT the tenant id Examples: cm quota list cm quota list --cloud=india --format=csv """ if arguments["list"]: cloud = arguments["--cloud"] or Default.get_cloud() if not cloud: Console.error("Default cloud doesn't exist") return tenant = arguments["--tenant"] output_format = arguments["--format"] list_quotas = Quota.list(cloud, tenant, output=output_format) Console.msg(list_quotas) return
def list_vm(self, cloudname, **kwargs): # return self.list(self.provider.list_nodes, cloudnames, kwargs) Console.info("In list_vm") nodes = self.provider.list_nodes() self._print(nodes) vm_dict = self._to_dict(nodes) return vm_dict
def list(cls, name, live=False, format="table"): """ This method lists all workflows of the cloud :param cloud: the cloud name """ # Console.TODO("this method is not yet implemented") # return try: elements = cls.cm.find(kind="workflow", category='general') # pprint(elements) # (order, header) = CloudProvider(cloud).get_attributes("workflow") order = None header= None # Console.msg(elements) return Printer.write(elements, order=order, header=header, output=format) except Exception as ex: Console.error(ex.message)
def get_info(cls, cloud="kilo", name=None, output="table"): """ Method to get info about a group :param cloud: :param name: :param output: :return: """ try: cloud = cloud or Default.get("cloud") args = { "name": name, "cloud": cloud } # group = cls.get(name=name, cloud=cloud) group = cls.cm.find("group", output="object", **args).first() if group is not None: d = cls.to_dict(group) # Transform the dict to show multiple rows per vm newdict = Group.transform_dict(d) else: return None return dict_printer(newdict, order=cls.order, output=output) except Exception as ex: Console.error(ex.message, ex)
def entry(cls, name): banner("Register {}".format(name)) name = str(name) etc_config = ConfigDict("cloudmesh.yaml", etc=True) config = ConfigDict("cloudmesh.yaml") clouds = config["cloudmesh.clouds"] clusters = config["cloudmesh.hpc.clusters"] if name in clouds: name = "cloudmesh.clouds.{}.credentials".format(name) elif name in clusters: name = "cloudmesh.hpc.clusters.{}.credentials".format(name) elif not name.startswith("cloudmesh."): name = "cloudmesh." + name try: etc = etc_config[name] yaml = config[name] # walk yaml for key in etc: if etc[key] == "TBD": result = input("Enter {:} ({:}): ".format(key, yaml[key])) if result != '': yaml[key] = result config.save() except Exception as e: Console.error("Could not find {} in the yaml file".format(name), traceflag=False)
def list(cls, filename, cloud, info=False, output='table'): """ lists clouds from cloudmesh.yaml file :param filename: :type filename: string :return: """ config = ConfigDict("cloudmesh.yaml") clouds = config["cloudmesh"]["clouds"] if info: Console.ok("Cloudmesh configuration file: {}".format(filename)) print("") d = {} for i, key in enumerate(clouds.keys()): d[i] = { "id": i, "cloud": key, "iaas": config["cloudmesh"]["clouds"][key]["cm_type"], "version": config["cloudmesh"]["clouds"][key]["cm_type_version"] or "", # "active": "*" if key in config["cloudmesh"]["active"] else "", "active": config["cloudmesh"]["active"].index(key) + 1 if key in config["cloudmesh"]["active"] else "", "default": "*" if key == cloud else "" } return Printer.Printer.write(d, order=['id', 'default', 'cloud', 'iaas', 'version', 'active'], output=output)
def list_floating_ip(cls, cloudname): """ Method to list floating ips :param cloudname: :return: floating ip list """ try: floating_ips = cls.get_floating_ip_list(cloudname) for floating_ip in floating_ips.values(): # Get instance_id associated with instance instance_id = floating_ip["instance_id"] if instance_id is not None: try: instance_name = cls.find_instance_name(cloudname=cloudname, instance_id=instance_id) # Assign it to the dict floating_ip["instance_name"] = instance_name except Exception as ex: Console.error(ex.message) continue else: # If no instance associated, keep None floating_ip["instance_name"] = None (order, header) = CloudProvider(cloudname).get_attributes("floating_ip") return dict_printer(floating_ips, order=order, header=header) except Exception as ex: Console.error(ex.message, ex) return
def do_check(self, args, arguments): """ :: Usage: check --cloud=CLOUD check checks some elementary setting for cloudmesh Options: --format=FORMAT the output format [default: table] --cloud=CLOUD the cloud name Examples: cm check cm check --cloud=kilo """ cloud = arguments["--cloud"] or Default.get_cloud() if cloud is None: Console.error("Default cloud doesn't exist") print(locals()) Console.ok("{:} ok".format(cloud)) return ""
def details(self, kind, cloud, id, format="table"): from cloudmesh_client.db.CloudmeshDatabase import CloudmeshDatabase try: cm = CloudmeshDatabase() if kind not in self.kind: raise ValueError('{} not defined'.format(kind)) elements = None for idkey in ["name", "uuid", "id"]: s = {idkey: id} try: elements = cm.find(kind, cloud=cloud, **s) except: pass if len(elements) > 0: break if len(elements) == 0: return None if format == "table": element = elements.values()[0] return attribute_printer(element) else: return dict_printer(elements, output=format) except Exception as ex: Console.error(ex.message, ex)
def delete_secgroup(cls, label, cloud, tenant): try: # Find the secgroup from the cloud nova_client = CloudProvider.set(cloud) sec_group = nova_client.security_groups.find(name=label) if not sec_group: return None # delete the secgroup in the cloud nova_client.security_groups.delete(sec_group) # perform local db deletion sec_group = cls.get(label, tenant, cloud) if sec_group: # Delete all rules for group cls.delete_all_rules(sec_group) cls.cm_db.delete(sec_group) return "Security Group [{}] for cloud [{}], & tenant [{}] deleted".format(label, cloud, tenant) else: return None except Exception as ex: Console.error(ex.message, ex) return
def list_flavor(self, cloudname, **kwargs): # return self.list(self.provider.list_sizes, cloudnames, kwargs) Console.info("In list_flavor of libcloud") sizes = self.provider.list_sizes() self._print(sizes) sizes_dict = self._to_dict(sizes) return sizes_dict
def get(cls, **kwargs): """ This method queries the database to fetch group(s) with given name filtered by cloud. :param name: :param cloud: :return: """ query = dict(kwargs) if 'output' in kwargs: for key, value in kwargs.iteritems(): if value is None: query[key] = "None" del query['output'] try: group = cls.cm.find_by_name("group", **query) if group is not None \ and "output" in kwargs: d = {"0": group} group = dict_printer(d) return group except Exception as ex: Console.error(ex.message, ex)
def do_image(self, args, arguments): """ :: Usage: image refresh [--cloud=CLOUD] image list [ID] [--cloud=CLOUD] [--format=FORMAT] [--refresh] This lists out the images present for a cloud Options: --format=FORMAT the output format [default: table] --cloud=CLOUD the cloud name --refresh live data taken from the cloud Examples: cm image refresh cm image list cm image list --format=csv cm image list 58c9552c-8d93-42c0-9dea-5f48d90a3188 --refresh """ cloud = arguments["--cloud"] or Default.cloud if cloud is None: Console.error("Default cloud doesn't exist") return if arguments["refresh"] or Default.refresh: msg = "Refresh image for cloud {:}.".format(cloud) if Image.refresh(cloud): Console.ok("{:} ok.".format(msg)) else: Console.error("{:} failed.".format(msg)) return "" if arguments["list"]: id = arguments['ID'] live = arguments['--refresh'] output_format = arguments["--format"] counter = 0 result = None while counter < 2: if id is None: result = Image.list(cloud, output_format) else: result = Image.details(cloud, id, live, output_format) if counter == 0 and result is None: if not Image.refresh(cloud): msg = "Refresh image for cloud {:}.".format(cloud) Console.error("{:} failed.".format(msg)) counter += 1 if result is None: Console.error("No image(s) found. Failed.") else: print(result) return ""
def delete_secgroup(cls, label, cloud): try: # Find the secgroup from the cloud cloud_provider = CloudProvider(cloud).provider result = cloud_provider.delete_secgroup(label) return result except Exception as ex: Console.error(ex.message, ex)
def _delete_floating_ip(cls, cloudname, floating_ip): result = Network.delete_floating_ip(cloudname=cloudname, floating_ip_or_id=floating_ip) if result is not None: Console.ok(result) else: Console.error("Failed to delete floating IP address!")
def list_key(self, cloudname, **kwargs): Console.info("In list_key") keys = self.provider.list_key_pairs() #Console.info(keys) #self._print(keys) keys_dict = self._to_dict(keys) #print (keys_dict) return keys_dict
def load(cls, path): filename = os.path.join(path, '.cloudmesh_metadata') Console.debug_msg('Loading {} to {}'.format(cls.__name__, filename)) with open(filename) as fd: d = yaml.load(fd) stack = cls(dest=path, **d) stack._env = d['_env'] return stack
def delete_secgroup(cls, name=None, cloud=None): try: # Find the secgroup from the cloud cloud_provider = CloudProvider(cloud).provider result = cloud_provider.delete_secgroup(name) return result except Exception as ex: Console.error("delete group")
def list(cls, names, usort=False, format="table"): """ This method lists all vcs of the cloud :param cloud: the cloud name """ Console.error("NOT YET IMPLEMENTED") return None
def deploy(self, force=False): if not self.is_deployed or force: self.stack.deploy(**self.deployparams) else: Console.info('Already deployed') self.is_deployed = True
def load(cls, filename): config = ConfigDict(filename=filename)["cloudmesh"] clouds = config["clouds"] # FINDING DEFAULTS FOR CLOUDS for cloud in clouds: db = { "image": cls.get(name="image", category=cloud), "flavor": cls.get(name="flavor", category=cloud), } defaults = clouds[cloud]["default"] for attribute in ["image", "flavor"]: value = db[attribute] if attribute in defaults: value = db[attribute] or defaults[attribute] empty = cls.get(name=attribute, category=cloud) if empty is None: cls.set(attribute, value, category=cloud) # FINDING DEFAUlTS FOR KEYS # keys: # default: id_rsa # keylist: # id_rsa: ~/.ssh/id_rsa.pub # key_db = SSHKeyDBManager() name_key = cls.key # # SET DEFAULT KEYS # if "keys" in config: keys = config["keys"] name = keys["default"] if name in keys["keylist"]: value = name_key or keys["keylist"][name] # key_db.add(value, keyname=name) # Check if the key is already set exist_key = cls.key # Set the key only if there is no existing value in the DB. if exist_key is None: cls.set_key(name) else: if cls.key is not None and cls.user is not None: pass elif cls.key is None and cls.user is not None: cls.key = cls.user else: Console.error( "Please define a key first, e.g.: cm key add --ssh <keyname>", traceflag=False)
def execute(cls, cmd, arguments="", shell=False, cwd=None, traceflag=True, witherror=True): """Run Shell command :param cmd: command to run :param arguments: we dont know yet :param capture: if true returns the output :return: """ # print "--------------" result = None terminal = cls.terminal_type() # print cls.command os_command = [cmd] if terminal in ['linux', 'windows']: os_command = [cmd] elif 'cygwin' in terminal: if not cls.command_exists(cmd): print("ERROR: the command could not be found", cmd) return else: os_command = [cls.command[cls.operating_system()][cmd]] if isinstance(arguments, list): os_command = os_command + arguments elif isinstance(arguments, tuple): os_command = os_command + list(arguments) elif isinstance(arguments, str): os_command = os_command + arguments.split() else: print("ERROR: Wrong parameter type", type(arguments)) if cwd is None: cwd = os.getcwd() try: if shell: result = subprocess.check_output(os_command, stderr=subprocess.STDOUT, shell=True, cwd=cwd) else: result = subprocess.check_output( os_command, # shell=True, stderr=subprocess.STDOUT, cwd=cwd) except: if witherror: Console.error("problem executing subprocess", traceflag=traceflag) if result is not None: result = result.strip().decode() return result
def from_file(cls, filename): """ Replaces the TBD in cloudmesh.yaml with the contents present in FILEPATH's FILE :param filename: :return: """ if not os.path.isfile(os.path.expanduser(filename)): Console.error("{} doesn't exist".format(filename)) return # BUG should use path separator path, filename = filename.rsplit("/", 1) # Config file to be read from from_config_file = ConfigDict(filename, [path]) config = ConfigDict("cloudmesh.yaml") # Merging profile profile = config["cloudmesh"]["profile"] for profile_key in list(profile.keys()): if profile[profile_key] == "TBD": profile[profile_key] = \ from_config_file["cloudmesh"]["profile"][profile_key] config.save() # Merging clouds clouds = config["cloudmesh"]["clouds"] for cloud in list(clouds.keys()): cloud_element = clouds[cloud] for key in list(cloud_element.keys()): if cloud_element[key] == "TBD": cloud_element[key] = \ from_config_file["cloudmesh"]["clouds"][cloud][key] config["cloudmesh"]["clouds"][cloud] = cloud_element credentials = clouds[cloud]["credentials"] for key in credentials: if credentials[key] == "TBD": credentials[key] = \ from_config_file["cloudmesh"]["clouds"][cloud][ "credentials"][key] config["cloudmesh"]["clouds"][cloud]["credentials"] = credentials defaults = clouds[cloud]["default"] for key in defaults: if defaults[key] == "TBD": defaults[key] = \ from_config_file["cloudmesh"]["clouds"][cloud][ "default"][ key] config["cloudmesh"]["clouds"][cloud]["default"] = defaults config.save() Console.ok( "Overwritten the TBD of cloudmesh.yaml with {} contents".format( filename))
def set_loglevel(cls, level): level = level or 'debug' level = level.lower() if level in ['debug', 'info', 'warnin', 'error', 'critical']: cls.set("loglevel", level) else: Console.error("unkown logging level. Setting to debug.", traceflag=False) cls.set("loglevel", 'debug')
def list(self): """List the known deployments :returns: :rtype: """ Console.error('hadoop.list not implemented') raise NotImplementedError()
def details(cls, cloud, id, live=False, format="table"): elements = cls.cm.find(kind="workflow", category='general', cm_id=id) Console.msg(elements) order = None header = None # Console.TODO("this method is not yet implemented") return Printer.write(elements, order=order, header=header, output=format)
def get_layout(id, user): if id is None: myjobs = ids(user) else: myjobs = [id] if len(myjobs) > 1: print(jobs, len(myjobs)) Console.error("More than one swarm cluster running, please specify ID") n = nodes(user, myjobs[0])[0] all_nodes = Parameter.expand(n)
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 create_cloudmesh_yaml(filename): if not os.path.exists(filename): path = os.path.dirname(filename) if not os.path.isdir(path): Shell.mkdir(path) etc_path = os.path.dirname(cloudmesh_client.__file__) etc_file = os.path.join(etc_path, "etc", "cloudmesh.yaml") to_dir = path_expand("~/.cloudmesh") shutil.copy(etc_file, to_dir) os.system("chmod -R go-rwx " + path_expand("~/.cloudmesh")) Console.ok("~/.cloudmesh/cloudmesh.yaml created")