def _default_format(self): self._start_cm_user() defaults_data = self.user_obj.info(self.username)['defaults'] if self.arguments['VALUE']: allowed_formats = ['table', 'json', 'csv'] if self.arguments['VALUE'] not in allowed_formats: Console.warning("allowed formats are {0}".format( str(allowed_formats))) return else: defaults_data['shell_print_format'] = self.arguments['VALUE'] self.user_obj.set_defaults(self.username, defaults_data) Console.ok("set '{0}' as default print format".format( self.arguments['VALUE'])) else: format = None try: format = defaults_data['shell_print_format'] except: pass if format not in [None, 'none']: print(defaults_data['shell_print_format']) else: defaults_data['shell_print_format'] = "table" self.user_obj.set_defaults(self.username, defaults_data) defaults_data = self.user_obj.info(self.username) print(defaults_data['shell_print_format'])
def get_working_cloud_name(self): ''' get the name of a cloud to be work on, if CLOUD not given, will pick the slected cloud, is --all, will return a list of active clouds ''' self.cloudmanage._connect_to_mongo() activeclouds = None try: activeclouds = self.cloudmanage.mongo.active_clouds(self.username) except: pass if self.arguments['--all']: if activeclouds is None: print("no active cloud, please activate a cloud by 'cloud on [CLOUD]'") return False return activeclouds else: if self.arguments['CLOUD']: name = self.arguments['CLOUD'] else: name = self.cloudmanage.get_selected_cloud(self.username) if self.cloudmanage.get_clouds(self.username, getone=True, cloudname=name) is None: Console.error( "no cloud information of '{0}' in database".format(name)) return False if name not in activeclouds: Console.warning( "cloud '{0}' is not active, to activate a cloud: cloud on [CLOUD]".format(name)) return False return [name]
def create_user_from_file(cls, file_path): try: filename = path_expand(file_path) file_config = ConfigDict(filename=filename) except: Console.error("Could not load file, please check filename and its path") return try: user_config = file_config.get("cloudmesh", "user") user_name = user_config['username'] user = User() update_document(user, user_config) except: Console.error("Could not get user information from yaml file, " "please check you yaml file, users information must be " "under 'cloudmesh' -> 'users' -> user1...") return try: if cls.check_exists(user_name) is False: cls.add(user) Console.info("User created in the database.") else: Console.error("User with user name " + user_name + " already exists.") return except: Console.error("User creation in database failed, " + str(sys.exc_info())) return
def display(cls, user_dicts=None, user_name=None): if bool(user_dicts): values = [] table = Texttable(max_width=180) for entry in user_dicts: items = [] headers = [] for key, value in entry.iteritems(): if key == "projects": project_entry = "" if value: for itm in value: user_project = Project.objects(id=ObjectId(itm.get('$oid'))).only('title', 'project_id').first() project_entry = project_entry + user_project.title + ", " items.append(project_entry) elif key == "roles": role_entry = "" if value: for itm in value: role_entry = role_entry+itm + ", " role_entry = role_entry.rstrip(', ') items.append(role_entry) else: items.append(value) headers.append(key.replace('_', ' ').title()) values.append(items) table.add_row(items) table.header(headers) print table.draw() else: if user_name: Console.error("No user in the system with name '{0}'".format(user_name))
def display_json(cls, project_dict=None, project_id=None): if bool(project_dict): # pprint.pprint(user_json) print json.dumps(project_dict, indent=4) else: if project_id: Console.error("No project in the system with name '{0}'".format(project_id))
def load_plugins(classprefix, plugin_list): """ loads the plugins specified in the list :param classprefix: the class prefix :param plugin_list: the list of plugins """ # classprefix "cmd3.plugins." plugins = [] import_object = {} # log.info(str(list)) for plugin in plugin_list: if cygwin: plugin = path_into_cygpath(plugin) # print ("PPPPP", classprefix, plugin) try: import_object[plugin] = __import__( classprefix + "." + plugin, globals(), locals(), [plugin], -1) # print ("TTT", import_object[plugin], type(import_object[plugin])) load_module = "cls = import_object['{0}'].{1}".format(plugin, plugin) # print ("LLL", load_module) exec(load_module) plugins.append(cls) except Exception, e: # if echo: Console.error("loading module {0} {1}".format(str(plugin), str(classprefix))) Console.error(70 * "=") print(e) Console.error(70 * "=") print(traceback.format_exc()) Console.error(70 * "-") print(sys.exc_info()[0]) Console.error(70 * "-")
def do_spark(self, args, arguments): """ :: Usage: spark NAME spark deploy HOST tests via ping if the host ith the give NAME is reachable Arguments: NAME Name of the machine to test Options: -v verbose mode """ pprint(arguments) if arguments["deploy"]: Console.ok("I want to deploy") host = arguments["HOST"] print(host) elif arguments["NAME"] is None: Console.error("Please specify a host name") else: host = arguments["NAME"] Console.info("trying to reach {0}".format(host)) status = command_spark.status(host) if status: Console.info("machine " + host + " has been found. ok.") else: Console.error("machine " + host + " not reachable. error.") pass
def add(self, job): """ job is a dictionary. One of its attributes is 'name'. The element is inserted into the db with the id 'name' :param job: the job to be added :return: """ matchingJobs = self.find_jobs("job_name", job["job_name"]) # A job with this job name already exists if matchingJobs.count() != 0: Console.error("A job with this job name already exists in the database") # A job with this job name does not exist so insert it else: # Set the ID of this job to be the job name job["_id"] = job["job_name"] # Set the update time update = str(datetime.datetime.now()) job["update_time"] = update # Save the job object if self.database is not None: db_job_object = self.jobs.save(job) return db_job_object
def run(self): banner("Setup the cloudmesh management yaml files ") yamlfiles = ['cloudmesh_user.yaml', 'cloudmesh_project.yaml'] dir_path = path_expand("~/.cloudmesh/{0}".format("accounts")) if not os.path.exists(dir_path): Shell.mkdir(dir_path) for yamlfile in yamlfiles: filename = path_expand("~/.cloudmesh/{0}/{1}".format( "accounts", yamlfile)) if os.path.isfile(filename): Console.error( "File {0} already exists. If you like to reinstall it, please remove the file" .format(yamlfile)) else: Console.info("Copying file: {0} -> {1} ".format( path_expand("etc/{0}/{1}".format("accounts", yamlfile)), filename)) shutil.copy( "etc/{0}/{1}".format("accounts", yamlfile), path_expand("~/.cloudmesh/{0}/{1}".format( "accounts", yamlfile)))
def info(self): # TODO: implement self. dbath, self.port, self.logpath Console.ok("Mongo parameters") Console.ok(" dbpath: {:}".format(self.db_path)) Console.ok(" port: {:}".format(self.port)) Console.ok(" logfile: {:}".format(self.log_file)) Console.ok(" dbname: {:}".format(self.dbname))
def set_password(cls, user_name, passwd): pass_hash = sha256_crypt.encrypt(passwd) try: User.objects(username=user_name).update_one(set__password=pass_hash) Console.info("User password updated.") except: Console.error("Oops! Something went wrong while trying to set user password")
def get_working_cloud_name(self): ''' get the name of a cloud to work on, if CLOUD not given, will pick the selected or default cloud ''' cloudname = None cloudobj = CloudManage() mongo = cm_mongo() if self.arguments['--cloud']: cloud = cloudobj.get_clouds( self.username, getone=True, cloudname=self.arguments['--cloud']) if cloud is None: Console.error( "could not find cloud '{0}'".format(self.arguments['--cloud'])) return False else: cloudname = self.arguments['--cloud'] else: cloudname = cloudobj.get_selected_cloud(self.username) if cloudname not in mongo.active_clouds(self.username): Console.warning( "cloud '{0}' is not active, to activate a cloud: cloud on [CLOUD]".format(cloudname)) return False else: return cloudname
def _default_format(self): self._start_cm_user() defaults_data = self.user_obj.info(self.username)['defaults'] if self.arguments['VALUE']: allowed_formats = ['table', 'json', 'csv'] if self.arguments['VALUE'] not in allowed_formats: Console.warning("allowed formats are {0}".format(str(allowed_formats))) return else: defaults_data['shell_print_format'] = self.arguments['VALUE'] self.user_obj.set_defaults(self.username, defaults_data) Console.ok("set '{0}' as default print format".format(self.arguments['VALUE'])) else: format = None try: format = defaults_data['shell_print_format'] except: pass if format not in [None, 'none']: print(defaults_data['shell_print_format']) else: defaults_data['shell_print_format'] = "table" self.user_obj.set_defaults(self.username, defaults_data) defaults_data = self.user_obj.info(self.username) print(defaults_data['shell_print_format'])
def _print_default(self, attr=None): to_print = self.get_defaults() columns = None if attr: columns = [attr] elif self.arguments['--column'] and self.arguments['--column'] != "all": columns = [x.strip() for x in self.arguments['--column'].split(',')] # ---------------------------------- # flexible input for index, item in enumerate(columns): if item in ['format']: columns[index] = "shell_print_format" # ---------------------------------- if self.arguments['--format']: if self.arguments['--format'] not in ['table', 'json', 'csv']: Console.error("please select printing format among table, json and csv") return else: p_format = self.arguments['--format'] else: p_format = None # if p_format == 'table' or p_format is None: # print(row_table(to_print, order=None, labels=["Default", "Value"])) # else: shell_commands_dict_output(self.username, to_print, print_format=p_format, header=columns, oneitem=True, vertical_table=True)
def set_user_status(cls, user_name, status): if user_name: try: User.objects(username=user_name).update_one(set__status=status) except: Console.error("Oops! Something went wrong while trying to amend user status") else: Console.error("Please specify the user to be amended")
def connect(self): client = MongoClient('localhost', self.port) self.database = client["jobsdb"] self.jobs = self.database["jobs"] self.id = self.database["id"] # manages the counter for the job Console.info("Connecting to the Mongo Database")
def add_review(cls, project_id, username, review): if review: review = Review.objects(project_id=project_id).first() review_text = username + " - " + review Review.objects().update_one(push__reviews=review_text) else: Console.error("Please provide a review text.") pass
def get_cloud_name(self, cm_user_id): """Returns a default cloud name if exists """ try: return self.cm_user.get_defaults(cm_user_id)['cloud'] except KeyError: Console.error('Please set a default cloud.') return None
def run_script(self, filename): Console.ok('Running Script {0}'.format(filename)) f = open(filename, "r") for line in f: line = self.precmd(line) line = self.onecmd(line) # line = self.postcmd(line) f.close()
def add_review(cls, project_id, username, review): if review: review = Review.objects(project_id=project_id).first() review_text = username+" - "+review Review.objects().update_one(push__reviews=review_text) else: Console.error("Please provide a review text.") pass
def _list_project(self): self.cloudmanage._connect_to_mongo() selected_project = None try: selected_project = self.cloudmanage.mongo.db_defaults.find_one( {'cm_user_id': self.username + "OIO"})['project'] except Exception, NoneType: Console.warning("could not find selected project in the database")
def set_project_status(cls, project_id, status): if project_id: try: Project.objects(project_id=project_id).update_one(set__status=status) except: Console.error("Oops! Something went wrong while trying to amend project status") else: Console.error("Please specify the project to be amended")
def update_job_end_time(self, job_id, end_time=str(datetime.datetime.now())): if self.database is not None: self.jobs.update({"_id": job_id}, {"$set": {"end_time": end_time}}, upsert=False) else: Console.error("Please connect to the database first") return -1
def deploy(self): """ creates the directories if they do not exist """ try: r = Shell.mkdir(self.db_path) except Exception, e: Console.error("Problem creating the database directory {:}".format(self.db_path)) print(e)
def _start_cm_user(self): if not self.started_cm_user: try: self.user_obj = cm_user() except: Console.error("There is a problem with " "cm_user object initialization") return self.started_cm_user = True
def approve_project(cls, project_id=None, text=None): if project_id and text: current_status = Projects.get_project_status(project_id) if current_status != "active": Projects.amend_project_status(project_id, "active") cls.add_review(project_id, "user", text) else: Console.error("Please specify a project id to be approved and a message.") pass
def do_edit(self, arg, arguments): """ :: Usage: edit FILENAME Edits the file with the given name Arguments: FILENAME the file to edit """ def _create_file(filename): if not os.path.exists(filename): file(filename, 'w+').close() def _edit(prefix, editors, filename): for editor in editors: if os.path.exists(editor): _create_file(filename) os.system("{:} {:} {:}".format(prefix, editor, filename)) return True return False filename = arg what = platform.system().lower() prefix = "" print(what) if 'darwin' in what: editors = ["/Applications/Aquamacs.app", "/Applications/Emacs.app"] prefix = "open -a " elif "linux" in what: editors = [ "/usr/bin/emacs", "/usr/bin/vi", "/usr/bin/vim", "/usr/bin/nano" ] elif "windows" in what: editors = ["emacs", "vi", "vim", "nano", "notepad", "notepad++"] else: Console.error("Please contact the developers to add an " "editor for your platform") return if not _edit(prefix, editors, filename): Console.error("Could not find working editor in {0}".format( str(editors)))
def clear(cls): """ Removes all elements form the mongo db that are users """ try: for user in User.objects: user.delete() Console.info("Users cleared from the database.") except: Console.error("Oops! Something went wrong while trying to clear the users from database")
def approve_project(cls, project_id=None, text=None): if project_id and text: current_status = Projects.get_project_status(project_id) if current_status != "active": Projects.amend_project_status(project_id, "active") cls.add_review(project_id, "user", text) else: Console.error( "Please specify a project id to be approved and a message.") pass
def new(instance, args): # instance.new.__doc__ = doc try: arguments = docopt(doc, help=True, argv=args) func(instance, args, arguments) # func.__doc__ = doc except SystemExit: if args not in ('-h', '--help'): Console.error("Could not execute the command.") print(doc)
def list_committee(cls, project_id=None): if project_id is None: req_fields = ["reviewers"] committee_json = Committee.objects.only(*req_fields).to_json() committee_dict = json.loads(committee_json) if committee_dict: cls.display(committee_dict) else: Console.info("No committees found in the database.") pass
def display(cls, committee_dict=None): if bool(committee_dict): values = [] for entry in committee_dict: items = [] headers = [] for key, value in entry.iteritems(): if key == "project_id": entry = "" if value: items.append(value) headers.append("Project ID") project = Project.objects(project_id=value).first() entry += project.title # for item in value: # project = Project.objects.get(id=ObjectId(item.get('$oid'))) # entry += project.title # entry = entry.strip(',') items.append(entry) headers.append("Project Name") elif key == "reviewers": entry = "" if value: for item in value: user = User.objects.get( id=ObjectId(item.get('$oid'))) if user.username != "super": entry += user.firstname + " " + user.lastname + ", " entry = entry.strip(', ') if entry: items.append(entry) else: items.append("No reviewers yet.") else: items.append("No reviewers yet.") headers.append(key.replace('_', ' ').title()) values.append(items) # Re-order the columns as per our requirement # header order 1, 2, 0 => project name, project_id, reviewers header_order = [0] headers = [headers[i] for i in header_order] new_values = [[x[0]] for x in values] table_fmt = "fancy_grid" table = tabulate(new_values, headers, table_fmt) separator = '' try: seperator = table.split("\n")[1].replace("|", "+") except: separator = "-" * 50 print separator print table print separator else: Console.error("No Committees to display.")
def do_limits(self, args, arguments): """ :: Usage: limits [CLOUD] [--format=json] limits help | -h Current usage data with limits on a selected project (tenant) Arguments: CLOUD Cloud name to see the usage help Prints this message Options: -v verbose mode """ self.cm_mongo = cm_mongo() self.cm_config = cm_config() self.cm_user = cm_user() if arguments["help"] or arguments["-h"]: print (self.do_limits.__doc__) else: userid = self.cm_config.username() self.cm_mongo.activate(userid) cloudid = arguments["CLOUD"] if cloudid is None: cloudid = self.get_cloud_name(userid) # if an id is still not found print error if cloudid is None: Console.error('Please set a default cloud.') return usage_with_limits = self.cm_mongo.usage_with_limits(cloudid, userid) if arguments["--format"] is None: print(row_table(usage_with_limits, order=None, labels=[ "Limits", "(Used/Max)" ])) elif 'json' in arguments["--format"]: print(json.dumps(usage_with_limits, indent=4)) else: Console.error('Quota is not supported.') return usage_with_limits
def display(cls, committee_dict=None): if bool(committee_dict): values = [] for entry in committee_dict: items = [] headers = [] for key, value in entry.iteritems(): if key == "project_id": entry = "" if value: items.append(value) headers.append("Project ID") project = Project.objects(project_id=value).first() entry += project.title # for item in value: # project = Project.objects.get(id=ObjectId(item.get('$oid'))) # entry += project.title # entry = entry.strip(',') items.append(entry) headers.append("Project Name") elif key == "reviewers": entry = "" if value: for item in value: user = User.objects.get(id=ObjectId(item.get('$oid'))) if user.username != "super": entry += user.firstname + " " + user.lastname + ", " entry = entry.strip(', ') if entry: items.append(entry) else: items.append("No reviewers yet.") else: items.append("No reviewers yet.") headers.append(key.replace('_', ' ').title()) values.append(items) # Re-order the columns as per our requirement # header order 1, 2, 0 => project name, project_id, reviewers header_order = [0] headers = [headers[i] for i in header_order] new_values = [[x[0]] for x in values] table_fmt = "fancy_grid" table = tabulate(new_values, headers, table_fmt) separator = '' try: seperator = table.split("\n")[1].replace("|", "+") except: separator = "-" * 50 print separator print table print separator else: Console.error("No Committees to display.")
def ip_assign(cls, name, cloud): """ :param name: name or id of the machine :type name: string :param cloud: cloud name :type cloud: string :return: """ Console.ok('ip_assign {} {}'.format(name, cloud)) raise NotImplemented("Not implemented yet")
def set_debug(self, on): filename = path_expand("~/.cloudmesh/cmd3.yaml") config = ConfigDict(filename=filename) if type(on) == bool: self.debug = on else: self.debug = on.lower() in ['on', 'true'] config["cmd3"]["properties"]["debug"] = self.debug Console.ok("Debug mode is {:}".format(self.debug)) config.write(filename=filename, output="yaml", attribute_indent=" ")
def do_limits(self, args, arguments): """ :: Usage: limits [CLOUD] [--format=json] limits help | -h Current usage data with limits on a selected project (tenant) Arguments: CLOUD Cloud name to see the usage help Prints this message Options: -v verbose mode """ self.cm_mongo = cm_mongo() self.cm_config = cm_config() self.cm_user = cm_user() if arguments["help"] or arguments["-h"]: print(self.do_limits.__doc__) else: userid = self.cm_config.username() self.cm_mongo.activate(userid) cloudid = arguments["CLOUD"] if cloudid is None: cloudid = self.get_cloud_name(userid) # if an id is still not found print error if cloudid is None: Console.error('Please set a default cloud.') return usage_with_limits = self.cm_mongo.usage_with_limits( cloudid, userid) if arguments["--format"] is None: print( row_table(usage_with_limits, order=None, labels=["Limits", "(Used/Max)"])) elif 'json' in arguments["--format"]: print(json.dumps(usage_with_limits, indent=4)) else: Console.error('Quota is not supported.') return usage_with_limits
def do_use(self, arg): """ :: USAGE: use list lists the available scopes use add SCOPE adds a scope <scope> use delete SCOPE removes the <scope> use without parameters allows an interactive selection DESCRIPTION Often we have to type in a command multiple times. To save us typng the name of the command, we have defined a simple scope that can be activated with the use command ARGUMENTS: list list the available scopes add add a scope with a name delete delete a named scope use activate a scope """ if arg == 'list': self._list_scope() return elif arg.startswith('add'): new_scope = arg.split(' ')[1] self._add_scope(new_scope) return elif arg.startswith('delete'): # delete does not work which_scope = arg.split(' ')[1] self._delete_scope(which_scope) return elif arg == "cm" or arg == "/": self.active_scope = "" elif arg in self.scopes: self.active_scope = arg else: self.active_scope = self.select([""] + self.scopes, 'Which scope? ') if self.active_scope == "": Console.ok("Switched scope to: cm") self.prompt = self.active_scope + 'cm> ' else: Console.ok("Switched scope to: {0}".format(self.active_scope)) self.prompt = self.active_scope + '> '
def list_projects(cls, user_name=None): required_fields = ["username", "firstname", "lastname", "projects"] try: if user_name: user_json = User.objects.only(*required_fields).to_json() user_dict = json.loads(user_json) if user_dict: cls.display(user_dict, user_name) else: Console.info("No user details available in the database.") except: Console.error("Please provide a username.")
def __init__(self, arguments): self.arguments = arguments # print (self.arguments) try: self.config = cm_config() except: Console.error("There is a problem with the " "configuration yaml files") self.username = self.config['cloudmesh']['profile']['username'] self.started_cm_user = False self.user_obj = None
def print_info(self): """prints some info that the user may find useful""" d = dir(self) self.plugins = [] for key in d: if key.startswith("info_"): self.plugins.append(key) for key in self.plugins: if self.echo: Console.ok("> {0}".format(key.replace("_", " ", 1))) exec("self.%s()" % key)
def list_users_json(cls): # req_fields = ["username", "title", "firstname", "lastname", # "email", "phone", "url", "citizenship", # "institution", "institutionrole", "department", # "advisor", "address", "status", "projects"] req_fields = ["username", "firstname", "lastname", "email", "phone", "institution", "institutionrole", "advisor", "address", "status", "projects"] try: user_json = User.objects().to_json() return user_json except: Console.error("Oops.. Something went wrong in the list users method " + sys.exc_info()[0])
def generate_users(n): """ Generates n random users in an array containing dicts for users :param n: number of users :type n: integer :rtype: array of dicts """ users.clear() for i in range(0, n): data = random_user() users.add(data) Console.info(str(n) + " users generated.")
def stop(self): """ stops the server """ try: process_id = self.pid() command = ["kill", "-9", str(process_id)] print(" ".join(command)) os.system(" ".join(command)) Console.ok("MongoDB has been shutdown") except Exception, e: Console.error("we had a problem shutting the mongo daemon down") print(e)