def start(self): try: mongod = Shell.which("mongod") command = [ mongod, "--dbpath", str(self.db_path), "--port", str(self.port), "--fork", "--logpath", str(self.log_file), "--bind_ip", "127.0.0.1" ] print(" ".join(command)) #a = subprocess.call(command) os.system(" ".join(command)) os.system ("ps aux | fgrep mongod |fgrep -v fgrep") Console.ok("MongoDB has been deployed") self.info() except Exception, e: Console.error("we had a problem starting the mongo daemon") print(e) Console.error("MongoDB has stopped") # TODO remove the exit in final code, for debugging only sys.exit()
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 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 do_open(self, args, arguments): """ :: Usage: open FILENAME ARGUMENTS: FILENAME the file to open in the cwd if . is specified. If file in in cwd you must specify it with ./FILENAME Opens the given URL in a browser window. """ filename = arguments['FILENAME'] filename = self._expand_filename(filename) Console.ok("open {0}".format(filename)) if not (filename.startswith("file:") or filename.startswith("http:")): try: with open(filename): pass filename += "file://" except: Console.error( "unsupported browser format in file {0}".format(filename)) return try: webbrowser.open("%s" % filename) except: Console.error( "can not open browser with file {0}".format(filename))
def do_open(self, args, arguments): """ :: Usage: open FILENAME ARGUMENTS: FILENAME the file to open in the cwd if . is specified. If file in in cwd you must specify it with ./FILENAME Opens the given URL in a browser window. """ filename = arguments['FILENAME'] filename = self._expand_filename(filename) Console.ok("open {0}".format(filename)) if not (filename.startswith("file:") or filename.startswith("http:")): try: with open(filename): pass filename += "file://" except: Console.error("unsupported browser format in file {0}".format(filename)) return try: webbrowser.open("%s" % filename) except: Console.error("can not open browser with file {0}".format(filename))
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 _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 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 stop(self): try: #command = ["mongod", "--shutdown"] command = ["killall", "mongod"] print(" ".join(command)) os.system(" ".join(command)) Console.ok("MongoDB has been shutdown") #os.system("mongod --shutdown") except Exception, e: Console.error("we had a problem shutting the mongo daemon down") print(e)
def ip_assign(cls, name_or_id, cloud): """ :param name_or_id: name or id of the machine :type name: string :param cloud: cloud name :type cloud: string :return: """ Console.ok('ip_assign {} {}'.format(name_or_id, 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 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 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 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 login(cls, name, user, ip, cloud, key, commands): """ TODO :param name: :param user: :param ip: :param cloud: :param key: :param commands: :return: """ Console.ok('login {} {} {} {} {} {}'.format(name, user, ip, cloud, key, commands)) raise NotImplemented("Not implemented yet")
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)
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 do_timer(self, args, arguments): """ :: Usage: timer on timer off timer list timer start NAME timer stop NAME timer resume NAME timer reset [NAME] Description (NOT IMPLEMENTED YET): timer on | off switches timers on and off not yet implemented. If the timer is on each command will be timed and its time is printed after the command. Please note that background command times are not added. timer list list all timers timer start NAME starts the timer with the name. A start resets the timer to 0. timer stop NAME stops the timer timer resume NAME resumes the timer timer reset NAME resets the named timer to 0. If no name is specified all timers are reset Implementation note: we have a stopwatch in cloudmesh, that we could copy into cmd3 """ # print(arguments) # print("args", args) args = args.lower() if args in ("on", "off"): self.with_timers = (args == "on") Console.ok("Timers are now: {0}".format(args)) if args == 'list': self.list_timers() else: self.do_timer.__doc__
def do_timer(self, args, arguments): """ :: Usage: timer on timer off timer list timer start NAME timer stop NAME timer resume NAME timer reset [NAME] Description (NOT IMPLEMENTED YET): timer on | off switches timers on and off not yet implemented. If the timer is on each command will be timed and its time is printed after the command. Please note that background command times are not added. timer list list all timers timer start NAME starts the timer with the name. A start resets the timer to 0. timer stop NAME stops the timer timer resume NAME resumes the timer timer reset NAME resets the named timer to 0. If no name is specified all timers are reset Implementation note: we have a stopwatch in cloudmesh, that we could copy into cmd3 """ # print arguments # print "args", args args = args.lower() if args in ("on", "off"): self.with_timers = (args == "on") Console.ok ("Timers are now: {0}".format(args)) if args == 'list': self.list_timers() else: self.do_timer.__doc__
def __init__(self, quiet=False): self.quiet = quiet try: self.filename = config_file("/cmd3.yaml") if not self.quiet: Console.ok("Reading " + self.filename + ". ok.") except: Console.error("problem loading cmd3.yaml file") try: self.config = ConfigDict(filename=self.filename) # print(self.config) except Exception as e: Console.error("problem with ConfigDict") print(e)
def __init__(self, quiet=False): self.quiet = quiet try: self.filename = config_file("/cmd3.yaml") if not self.quiet: Console.ok("Reading " + self.filename + ". ok.") except: Console.error("problem loading cmd3.yaml file") try: self.config = ConfigDict(filename=self.filename) # print(self.config) except Exception, e: Console.error("problem with ConfigDict") print(e)
def do_setup(self, arg, arguments): """ :: Usage: setup init [--force] Copies a cmd3.yaml file into ~/.cloudmesh/cmd3.yaml """ if arguments["init"]: Console.ok("Initialize cmd3.yaml file") from cmd3.yaml_setup import create_cmd3_yaml_file force = arguments["--force"] create_cmd3_yaml_file(force=force)
def list(cls, cloud, group, refresh, output_format, columns, detail): """ TODO :param cloud: cloud name :param group: the group name of server :param refresh: :param output_format: :param columns: :param detail: :return: """ Console.ok('list {} {} {} {} {} {}'.format(cloud, group, refresh, output_format, columns, detail)) raise NotImplemented("Not implemented yet")
def delete(self, plugin, dryrun=False): if not plugin.endswith(".plugins"): plugin = plugin + ".plugins" modules = self.config["cmd3"]["modules"] if plugin not in modules: Console.error("plugin {:} is already deactivated.".format(plugin)) else: self.config["cmd3"]["modules"].remove(plugin) if not self.quiet: Console.ok("Deleting plugin " + plugin) if not dryrun: self.config.write(self.filename, output="yaml") else: print(self.config.yaml())
def _default_cloud(self): self._start_cm_user() defaults_data = self.user_obj.info(self.username)['defaults'] if self.arguments['VALUE']: value = self.arguments['VALUE'] if (value in defaults_data['activeclouds'] and value in defaults_data['registered_clouds']): defaults_data['cloud'] = value self.user_obj.set_defaults(self.username, defaults_data) Console.ok("set '{0}' as default cloud".format(value)) else: Console.warning("To set a default cloud, it must be registered and " "active, to register and activate a CLOUD: cloud on [CLOUD]") else: if "cloud" in defaults_data: print(defaults_data['cloud']) else: print("default cloud not set")
def do_color(self, args, arguments): """ :: Usage: color on color off color Turns the shell color printing on or off Description: color on switched the color on color off switches the color off color without parameters prints a test to display the various colored mesages. It is intended as a test to see if your terminal supports colors. """ if arguments['on']: key = "cloudmesh.shell.color" value = True self.cm_config._update(key, value) self.cm_config.write(output="yaml") Console.color = True print("color on.") elif arguments['off']: key = "cloudmesh.shell.color" value = False self.cm_config._update(key, value) self.cm_config.write(output="yaml") Console.color = False print("color off.") else: print("Color:", Console.color) Console.warning("Warning") Console.error("Error") Console.info("Info") Console.msg("Msg") Console.ok("Success")
def do_color(self, args, arguments): """ :: Usage: color on color off color Turns the shell color printing on or off Description: color on switched the color on color off switches the color off color without parameters prints a test to display the various colored mesages. It is intended as a test to see if your terminal supports colors. """ if arguments['on']: key = "cloudmesh.shell.color" value = True self.cm_config._update(key, value) self.cm_config.write(output="yaml") Console.color = True print ("color on.") elif arguments['off']: key = "cloudmesh.shell.color" value = False self.cm_config._update(key, value) self.cm_config.write(output="yaml") Console.color = False print ("color off.") else: print("Color:", Console.color) Console.warning("Warning") Console.error("Error") Console.info("Info") Console.msg("Msg") Console.ok("Success")
def ip_show(cls, name_or_id, group, cloud, output_format, refresh): """ TODO shows the ip of a vm :param name_or_id: name or id of the machine :type name_or_id: list? :param group: the group name of server :type group: string :param cloud: cloud name :type cloud: string :param output_format: output format :type output_format: string :param refresh: :type refresh: bool? :return: """ Console.ok('ip_show {} {} {} {} {}'.format(name_or_id, group, cloud, output_format, refresh)) raise NotImplemented("Not implemented yet")
def do_wikicount(self, args, arguments): """ :: Usage: wikicount build_cluster NAME [--count=N] [--ln=S] [--cloud=S] [--flavor=S] [--image=S] wikicount decomission_cluster NAME wikicount install wikicount install_mongodb Arguments: NAME Name of the wikicount cluster group Options: --count=N number of nodes to create --ln=S login name --cloud=S cloud to use --flavor=S flavor to use --image=S image to use """ pprint(arguments) if arguments['build_cluster']: Console.ok("I want to build a cluster") name = arguments['NAME'] count = arguments['--count'] or 3 ln = arguments['--ln'] cloud = arguments['--cloud'] flavor = arguments['--flavor'] image = arguments['--image'] command_wikicount.build_cluster(name, count) elif arguments['decomission_cluster']: Console.ok("I want to decomission a cluster") name = arguments['NAME'] command_wikicount.decomission_cluster(name) elif arguments['install']: Console.ok("Initializing environment") command_wikicount.install() elif arguments['install_mongodb']: Console.ok("Installing mongodb") command_wikicount.install_mongodb() elif arguments["NAME"] is None: Console.error("Please specify a name for the cluster") else: name = arguments["NAME"] Console.info("trying to reach {0}".format(name)) status = command_wikicount.status(name) if status: Console.info("machine " + name + " has been found. ok.") else: Console.error("machine " + name + " not reachable. error.") pass
def create_cmd3_yaml_file(force=False, verbose=True): def print_error(kind, path): if verbose: Console.error("the {0} {1} already exists".format(kind, path)) Console.msg("") Console.msg("If you like to reinstall it, " "please remove the file first") Console.msg("") if verbose: banner("create cmd3.yaml") cmd3_yaml = path_expand("~/.cloudmesh/cmd3.yaml") if force or not os.path.isfile(cmd3_yaml): Shell.mkdir(path_expand("~/.cloudmesh")) import cmd3 content = pkg_resources.resource_string(cmd3.__name__, "etc/cmd3.yaml") Console.ok("Generate yaml file") with open(cmd3_yaml, "w") as cmd3_file: cmd3_file.write(content) else: print_error('file', cmd3_yaml) if verbose: banner("create cmd3_template") # # # copy tree filename = '~/.cloudmesh/etc/cmd3_template' if os.path.isdir(path_expand(filename)): print_error('directory', filename) else: import glob import shutil import cmd3.etc.cmd3_template f1 = cmd3.etc.cmd3_template.__file__ cmd3_etc_path = os.path.dirname(f1) pattern = os.path.join(cmd3_etc_path, '*') for src in glob.glob(pattern): if os.path.isfile(src): continue shutil.copytree(src, path_expand(filename))
def _default_cloud(self): self._start_cm_user() defaults_data = self.user_obj.info(self.username)['defaults'] if self.arguments['VALUE']: value = self.arguments['VALUE'] if (value in defaults_data['activeclouds'] and value in defaults_data['registered_clouds']): defaults_data['cloud'] = value self.user_obj.set_defaults(self.username, defaults_data) Console.ok("set '{0}' as default cloud".format(value)) else: Console.warning( "To set a default cloud, it must be registered and " "active, to register and activate a CLOUD: cloud on [CLOUD]" ) else: if "cloud" in defaults_data: print(defaults_data['cloud']) else: print("default cloud not set")
def do_loglevel(self, args, arguments): """ :: Usage: loglevel loglevel critical loglevel error loglevel warning loglevel info loglevel debug Shows current log level or changes it. loglevel - shows current log level critical - shows log message in critical level error - shows log message in error level including critical warning - shows log message in warning level including error info - shows log message in info level including warning debug - shows log message in debug level including info """ if arguments['debug']: self.loglevel = "DEBUG" elif arguments['error']: self.loglevel = "ERROR" elif arguments['warning']: self.loglevel = "WARNING" elif arguments['info']: self.loglevel = "INFO" elif arguments['critical']: self.loglevel = "CRITICAL" else: Console.ok("Log level: {0}".format(self.loglevel)) return Console.ok ("Log level: {0} is set".format(self.loglevel)) filename = path_expand("~/.cloudmesh/cmd3.yaml") config = ConfigDict(filename=filename) config["cmd3"]["properties"]["loglevel"] = self.loglevel config.write(filename=filename, output="yaml", attribute_indent=" ")
def do_loglevel(self, args, arguments): """ :: Usage: loglevel loglevel critical loglevel error loglevel warning loglevel info loglevel debug Shows current log level or changes it. loglevel - shows current log level critical - shows log message in critical level error - shows log message in error level including critical warning - shows log message in warning level including error info - shows log message in info level including warning debug - shows log message in debug level including info """ if arguments['debug']: self.loglevel = "DEBUG" elif arguments['error']: self.loglevel = "ERROR" elif arguments['warning']: self.loglevel = "WARNING" elif arguments['info']: self.loglevel = "INFO" elif arguments['critical']: self.loglevel = "CRITICAL" else: Console.ok("Log level: {0}".format(self.loglevel)) return Console.ok("Log level: {0} is set".format(self.loglevel)) filename = path_expand("~/.cloudmesh/cmd3.yaml") config = ConfigDict(filename=filename) config["cmd3"]["properties"]["loglevel"] = self.loglevel config.write(filename=filename, output="yaml", attribute_indent=" ")
def do_info(self, arg, arguments): """ :: Usage: info [--all] Options: --all -a more extensive information Prints some internal information about the shell """ if arguments["--all"]: Console.ok(70 * "-") Console.ok('DIR') Console.ok(70 * "-") for element in dir(self): Console.ok(str(element)) Console.ok(70 * "-") self.print_info()
def do_admin(self, args, arguments): """ :: Usage: admin password reset admin password Options: Description: admin password reset Reset portal password """ pprint(arguments) if arguments['password'] and arguments['reset']: Console.ok('password reset ...') self.password = getpass("Password:"******"~/.cloudmesh/cmd3.yaml") config = ConfigDict(filename=filename) config["cmd3"]["properties"]["password"] = self.password config.write(filename=filename, output="yaml") Console.ok("Resetting password. ok.") elif arguments['password']: if yn_choice("Would you like to print the password?"): filename = path_expand("~/.cloudmesh/cmd3.yaml") config = ConfigDict(filename=filename) try: self.password = config["cmd3"]["properties"]["password"] except Exception: Console.error("could not find the password. Please set it.") return Console.msg("Password: {:}".format(self.password))
def __destroy_node(node): """ deletes a Virtual Machine :param node: node to be deleted :type node: Node :return: """ try: while True: answer = "" if not force: answer = raw_input( "Would you like to delete {:}? y/n".format( node.name)) if answer.lower() == 'y' or answer.lower( ) == 'yes' or force: break elif answer.lower() != 'n' and answer.lower() != 'no': Console.ok("Invalid option") else: Console.ok("Operation aborted") return driver.destroy_node(node) Console.ok("Virtual Machine {:} deleted".format(node.name)) except Exception as e: Console.error( "Could not delete Virtual Machine {:}. {:}".format( node.name, e.message))
def do_admin(self, args, arguments): """ :: Usage: admin password reset admin password Options: Description: admin password reset Reset portal password """ pprint(arguments) if arguments["password"] and arguments["reset"]: Console.ok("password reset ...") self.password = getpass("Password:"******"~/.cloudmesh/cmd3.yaml") config = ConfigDict(filename=filename) config["cmd3"]["properties"]["password"] = self.password config.write(filename=filename, output="yaml") Console.ok("Resetting password. ok.") elif arguments["password"]: if yn_choice("Would you like to print the password?"): filename = path_expand("~/.cloudmesh/cmd3.yaml") config = ConfigDict(filename=filename) try: self.password = config["cmd3"]["properties"]["password"] except Exception: Console.error("could not find the password. Please set it.") return Console.msg("Password: {:}".format(self.password))
def __destroy_node(node): """ deletes a Virtual Machine :param node: node to be deleted :type node: Node :return: """ try: while True: answer = "" if not force: answer = raw_input("Would you like to delete {:}? y/n".format(node.name)) if answer.lower() == 'y' or answer.lower() == 'yes' or force: break elif answer.lower() != 'n' and answer.lower() != 'no': Console.ok("Invalid option") else: Console.ok("Operation aborted") return driver.destroy_node(node) Console.ok("Virtual Machine {:} deleted".format(node.name)) except Exception, e: Console.error("Could not delete Virtual Machine {:}. {:}".format(node.name, e.message))
def start(self): """ starts the database service :return: the pid """ if self.isup(): _pid = self.pid() Console.error("A mongod process on port {:} is already running with pid {:}".format(self.port, _pid)) return else: Console.ok("STARTING") try: mongod = Shell.which("mongod") command = [ mongod, "--dbpath", str(self.db_path), "--port", str(self.port), "--fork", "--logpath", str(self.log_file), "--bind_ip", "127.0.0.1" ] print(" ".join(command)) # a = subprocess.call(command) os.system(" ".join(command)) self.ps() Console.ok("MongoDB has been deployed") self.info() return None # implement the return of the pid for the process. # store the pid in self.pid except Exception, e: Console.error("we had a problem starting the mongo daemon") print(e) Console.error("MongoDB has stopped") # TODO remove the exit in final code, for debugging only sys.exit()
def do_var(self, arg, arguments): """ Usage: var list var delete NAMES var NAME=VALUE var NAME Arguments: NAME Name of the variable NAMES Names of the variable separated by spaces VALUE VALUE to be assigned special vars date and time are defined """ if arguments['list'] or arg == '' or arg is None: self._list_variables() return elif arguments['NAME=VALUE'] and "=" in arguments["NAME=VALUE"]: (variable, value) = arg.split('=', 1) if value == "time" or value == "now": value = datetime.datetime.now().strftime("%H:%M:%S") elif value == "date": value = datetime.datetime.now().strftime("%Y-%m-%d") self._add_variable(variable, value) return elif arguments['NAME=VALUE'] and "=" in arguments["NAME=VALUE"]: try: v = arguments['NAME=VALUE'] Console.ok(str(self.variables[v])) except: Console.error('variable {:} not defined'.format( arguments['NAME=VALUE'])) elif arg.startswith('delete'): variable = arg.split(' ')[1] self._delete_variable(variable) return
def do_var(self, arg, arguments): """ Usage: var list var delete NAMES var NAME=VALUE var NAME Arguments: NAME Name of the variable NAMES Names of the variable separated by spaces VALUE VALUE to be assigned special vars date and time are defined """ if arguments['list'] or arg == '' or arg is None: self._list_variables() return elif arguments['NAME=VALUE'] and "=" in arguments["NAME=VALUE"]: (variable, value) = arg.split('=', 1) if value == "time" or value == "now": value = datetime.datetime.now().strftime("%H:%M:%S") elif value == "date": value = datetime.datetime.now().strftime("%Y-%m-%d") self._add_variable(variable, value) return elif arguments['NAME=VALUE'] and "=" in arguments["NAME=VALUE"]: try: v = arguments['NAME=VALUE'] Console.ok(str(self.variables[v])) except: Console.error('variable {:} not defined'.format(arguments['NAME=VALUE'])) elif arg.startswith('delete'): variable = arg.split(' ')[1] self._delete_variable(variable) return
def do_exec(self, filename): """ :: Usage: exec FILENAME executes the commands in the file. See also the script command. Arguments: FILENAME The name of the file """ if not filename: Console.error("the command requires a filename as parameter") return if os.path.exists(filename): with open(filename, "r") as f: for line in f: Console.ok("> {:}".format(str(line))) self.onecmd(line) else: Console.error('file "{:}" does not exist.'.format(filename)) sys.exit()
def do_version(self, args): """ Usage: version Prints out the version number """ Console.ok("cmd3: {:}".format(str(cmd3.__version__))) Console.ok("cloudmesh_base: {:}".format(str( cloudmesh_base.__version__))) python_version, pip_version = get_python() Console.ok("python: {:}".format(str(python_version))) Console.ok("pip: {:}".format(str(pip_version))) check_python()
def activate(self): """method to activate all activation methods in the shell and its plugins. """ d = dir(self) self.plugins = [] for key in d: if key.startswith("shell_activate_"): if self.echo: Console.ok("Shell Activate: {0}".format(key)) self.plugins.append(key) for key in d: if key.startswith("activate_"): if self.echo: Console.ok("Activate: {0}".format(key)) self.plugins.append(key) for key in self.plugins: if self.echo: Console.ok("> {0}".format(key.replace("_", " ", 1))) exec ("self.%s()" % key)
def activate(self): """method to activate all activation methods in the shell and its plugins. """ d = dir(self) self.plugins = [] for key in d: if key.startswith("shell_activate_"): if self.echo: Console.ok("Shell Activate: {0}".format(key)) self.plugins.append(key) for key in d: if key.startswith("activate_"): if self.echo: Console.ok("Activate: {0}".format(key)) self.plugins.append(key) for key in self.plugins: if self.echo: Console.ok("> {0}".format(key.replace("_", " ", 1))) exec("self.%s()" % key)
def info(self): """ prints some elementary information about the server """ Console.ok("Mongo parameters") if self.db_path: Console.ok(" dbpath: {:}".format(self.db_path)) if self.port: Console.ok(" port: {:}".format(self.port)) if self.log_file: Console.ok(" logfile: {:}".format(self.log_file)) if self.dbname: Console.ok(" dbname: {:}".format(self.dbname)) try: Console.ok(" pid: {:}".format(self.pid())) except: pass try: db = self.connect() Console.ok(" Objects: {:}".format(len(db))) except: pass
p_format = None shell_commands_dict_output(username, d, print_format=p_format, table_format="key_list", indexed=True) elif arguments["create"]: try: GroupManage.create_group(name) except Exception, err: Console.error(str(err)) return Console.ok("group {0} created".format(name)) elif arguments['remove'] and not arguments['item']: try: GroupManage.delete_group(name) except Exception, err: Console.error(str(err)) return Console.ok("group {0} removed".format(name)) elif arguments["add"] and arguments['item']: try: add_item_to_group(username, name, type, value, refresh=True) except Exception, err: Console.error(str(err))
class cm_shell_launcher: """opt_example class""" _id = "t_stacks" # id for stack in cm_mongo def activate_cm_shell_launcher(self): self.register_command_topic('cloud', 'launcher') 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: log.error('set a default cloud with openstack. "stack" works on' ' openstack platform only') return None @command def do_launcher(self, args, arguments): """ :: Usage: launcher start MENU launcher stop STACK_NAME launcher list launcher show STACK_NAME launcher menu [--column=COLUMN] [--format=FORMAT] launcher import [FILEPATH] [--force] launcher export FILEPATH launcher help | -h An orchestration tool with Chef Cookbooks Arguments: MENU Name of a cookbook STACK_NAME Name of a launcher FILEPATH Filepath COLUMN column name to display FORMAT display format (json, table) help Prints this message Options: -v verbose mode """ log.info(arguments) self.cm_mongo = cm_mongo() self.cm_config = cm_config() self.cm_user = cm_user() self._id = "t_stacks" if arguments["help"] or arguments["-h"]: print(self.do_launcher.__doc__) elif arguments['show'] and arguments['STACK_NAME']: print("NOT IMPLEMENTED") return elif arguments['menu']: userid = self.cm_config.username() launchers = self.cm_mongo.launcher_get(userid) if launchers.count() == 0: Console.warning( "no launcher in database, please import launcher first" "(launcher import [FILEPATH] [--force])") return else: d = {} for launcher in launchers: d[launcher['cm_launcher']] = launcher if "_id" in d[launcher['cm_launcher']]: del d[launcher['cm_launcher']]['_id'] columns = None if arguments['--column']: if arguments['--column'] != "all": columns = [ x.strip() for x in arguments['--column'].split(',') ] else: columns = ['name', 'description'] if arguments['--format']: if arguments['--format'] not in ['table', 'json', 'csv']: Console.error("please select printing format ", "among table, json and csv") return else: p_format = arguments['--format'] else: p_format = None shell_commands_dict_output(userid, d, print_format=p_format, firstheader="launcher", header=columns # vertical_table=True ) elif arguments['list']: userid = self.cm_config.username() self.cm_mongo.activate(userid) self.cm_mongo.refresh(cm_user_id=userid, types=[self._id]) stacks = self.cm_mongo.stacks(cm_user_id=userid) launchers = self.filter_launcher(stacks, { "search": "contain", "key": "stack_name", "value": "launcher" }) log.debug(launchers) d = {} for k0, v0 in launchers.iteritems(): for k1, v1 in launchers[k0].iteritems(): d[v1['id']] = v1 columns = [ 'stack_name', 'description', 'stack_status', 'creation_time', 'cm_cloud' ] if arguments['--column'] and arguments['--column'] != "all": columns = [x.strip() for x in arguments['--column'].split(',')] if arguments['--format']: if arguments['--format'] not in ['table', 'json', 'csv']: Console.error( "please select printing format among table, json and csv" ) return else: p_format = arguments['--format'] else: p_format = None shell_commands_dict_output(userid, d, print_format=p_format, firstheader="launcher_id", header=columns # vertical_table=True ) elif arguments['start'] and arguments['MENU']: userid = self.cm_config.username() def_cloud = self.get_cloud_name(userid) self.cm_mongo.activate(userid) config_launcher = cm_config_launcher() userinfo = self.cm_user.info(userid) if "key" in userinfo["defaults"]: key = userinfo["defaults"]["key"] elif len(userinfo["keys"]["keylist"].keys()) > 0: key = userinfo["keys"]["keylist"].keys()[0] if key: keycontent = userinfo["keys"]["keylist"][key] if keycontent.startswith('key '): keycontent = keycontent[4:] cm_keys_mongo(userid).check_register_key( userid, def_cloud, key, keycontent) keynamenew = _keyname_sanitation(userid, key) else: Console.warning("No sshkey found. Please Upload one") cookbook = arguments['MENU'] s_name = "launcher-{0}-{1}-{2}".format(userid, cookbook, get_rand_string()) dummy = "123456789" # doing nothing. just for test try: t_url = \ config_launcher['cloudmesh']['launcher']['default']['template'] except: # If key is missing (KeyError), new cloudmesh_launcher.yaml # needs to be copied to ~/.cloudmesh t_url = \ "https://raw.githubusercontent.com/cloudmesh/cloudmesh/master/heat-templates/centos6/launcher/launcher.yaml" param = { 'KeyName': keynamenew, 'Cookbook': cookbook, 'dummy': dummy } # test for openmpi, hadoop if cookbook[:6] == "hadoop" or cookbook[:7] == "openmpi": privatekey, publickey = generate_keypair() try: t_url = \ config_launcher['cloudmesh']['launcher']['recipes'][cookbook]['template'] except: # If key is missing (KeyError), new cloudmesh_launcher.yaml # needs to be copied to ~/.cloudmesh t_url = \ ("https://raw.githubusercontent.com/cloudmesh/cloudmesh/master/heat-templates/ubuntu-14.04/" + str(cookbook) + "-cluster/" + str(cookbook) + "-cluster.yaml") param = { 'KeyName': keynamenew, 'PublicKeyString': publickey, 'PrivateKeyString': privatekey } if cookbook[:9] == "hadoop2.7": param["UserName"] = userid log.debug(def_cloud, userid, s_name, t_url, param) res = self.cm_mongo.stack_create(cloud=def_cloud, cm_user_id=userid, servername=s_name, template_url=t_url, parameters=param) log.debug(res) if 'error' in res: print(res['error']['message']) return res elif arguments['stop'] and arguments['STACK_NAME']: userid = self.cm_config.username() def_cloud = self.get_cloud_name(userid) s_id = arguments['STACK_NAME'] self.cm_mongo.activate(userid) res = self.cm_mongo.stack_delete(cloud=def_cloud, cm_user_id=userid, server=s_id) log.debug(res) return res elif arguments['import']: filepath = config_file("/cloudmesh_launcher.yaml") if arguments['FILEPATH']: filepath = arguments['FILEPATH'] try: filename = path_expand(filepath) fileconfig = ConfigDict(filename=filename) except Exception, err: Console.error( "error while loading '{0}', please check".format(filepath)) print(traceback.format_exc()) print(sys.exc_info()[0]) return try: recipes_dict = fileconfig.get("cloudmesh", "launcher", "recipies") except: Console.error("error while loading recipies from the file") # print recipes_dict userid = self.cm_config.username() launcher_names = [] launchers = self.cm_mongo.launcher_get(userid) for launcher in launchers: launcher_names.append(launcher['cm_launcher'].encode("ascii")) for key in recipes_dict: if key in launcher_names: if arguments['--force']: self.cm_mongo.launcher_remove(userid, key) self.cm_mongo.launcher_import(recipes_dict[key], key, userid) print("launcher '{0}' overwritten.".format(key)) else: print("ERROR: launcher '{0}' exists, " "please remove it first, or use " "'--force' when adding".format(key)) else: self.cm_mongo.launcher_import(recipes_dict[key], key, userid) print("launcher '{0}' added.".format(key)) elif arguments['export']: userid = self.cm_config.username() launchers = self.cm_mongo.launcher_get(userid) if launchers.count() == 0: Console.warning("no launcher in database, " "please import launcher first" "(launcher import [FILEPATH] [--force])") else: d = {} for launcher in launchers: key = launcher['cm_launcher'] d[key] = launcher if "_id" in d[key]: del d[key]['_id'] if "cm_launcher" in d[key]: del d[key]['cm_launcher'] if "cm_kind" in d[key]: del d[key]['cm_kind'] if "cm_user_id" in d[key]: del d[key]['cm_user_id'] d = dict_uni_to_ascii(d) d = { "meta": { "yaml_version": "2.1", "kind": "launcher" }, "cloudmesh": { "launcher": { "recipies": d } } } pprint(d) print("exporting to {0}...".format(arguments['FILEPATH'])) try: filename = path_expand(arguments['FILEPATH']) stream = file(filename, 'w') ordered_dump(d, stream=stream) Console.ok("done") except Exception, err: Console.error("failed exporting to {0}".format( arguments['FILEPATH'])) print(traceback.format_exc()) print(sys.exc_info()[0])
else: p_format = None shell_commands_dict_output(username, d, print_format=p_format, table_format="key_list", indexed=True) elif arguments["create"]: try: GroupManage.create_group(name) except Exception, err: Console.error(str(err)) return Console.ok("group {0} created".format(name)) elif arguments['remove'] and not arguments['item']: try: GroupManage.delete_group(name) except Exception, err: Console.error(str(err)) return Console.ok("group {0} removed".format(name)) elif arguments["add"] and arguments['item']: try: add_item_to_group(username, name, type_, value, refresh=True) except Exception, err: Console.error(str(err)) return