def _print_dict(d, header=None): print_format = arguments['--format'] if print_format == "json": return json.dumps(d, indent=4) elif print_format == "yaml": return yaml.dump(d, default_flow_style=False) else: return two_column_table(keys, header)
def do_ssh(self, args, arguments): """ :: Usage: ssh list [--format=json|yaml] ssh register NAME COMMANDS ssh NAME conducts a ssh login into a machine while using a set of registered commands under the name of the machine. Arguments: NAME Name of the machine to log in list Lists the machines that are registered and the commands to login to them register Register the commands to a name COMMANDS The list of commands executed when issuing a name Options: -v verbose mode """ pprint(arguments) if arguments["list"]: if 'json' == arguments["--format"]: print (print_format_dict(self.ssh_machines, kind='json')) elif 'yaml' == arguments["--format"]: print (print_format_dict(self.ssh_machines, kind='yaml')) else: print (two_column_table(self.ssh_machines, header=['Machines', 'Commands'])) elif arguments["register"]: Console.error("NOT YET IMPLEMENTED") else: machine = arguments["NAME"] if machine in self.ssh_machines: commands = self.ssh_machines[machine] print (commands) Console.info ("login to " + machine) os.system(commands) else: Console.error("machine " + machine + " not found") # shell_command_open_ssh(arguments) pass
def do_keys(self, args, arguments): """ Usage: keys info [--json] [NAME][--yaml][--mongo] keys mode MODENAME keys default NAME [--yaml][--mongo] keys add NAME KEY [--yaml][--mongo] keys delete NAME [--yaml][--mongo] keys persist Manages the keys Arguments: NAME The name of a key MODENAME This is used to specify the mode name. Mode name can be either 'yaml' or 'mongo' KEY This is the actual key that has to added Options: -v --verbose verbose mode -j --json json output -y --yaml forcefully use yaml mode -m --mongo forcefully use mongo mode Description: keys info Prints list of keys. NAME of the key can be specified keys mode MODENAME Used to change default mode. Valid MODENAMES are yaml(default) and mongo mode. keys default NAME Used to set a key from the key-list as the default key keys add NAME KEY adding/updating keys. Please provide filename and not the actual key keys delete NAME deletes a key. In yaml mode it can delete only keys that are not persisted keys persist Saves the temporary yaml data structure to mongo """ if arguments["mode"]: if arguments["MODENAME"] == "yaml": self.use_yaml = True print "SUCCESS: Set mode to yaml" return elif arguments["MODENAME"] == "mongo": self.use_yaml = False print "SUCCESS: Set mode to mongo" return else: print "ERROR: Wrong MODENAME. only valid modes are 'mongo' and 'yaml'" return if arguments["--yaml"] and arguments["--mongo"]: print "ERROR: you can specify only one mode" return elif arguments["--yaml"]: self.use_yaml = True elif arguments["--mongo"]: self.use_yaml = False if self.use_yaml: print "Mode: yaml" if not self.keys_loaded: self._load_keys() key_container = self.keys else: print "Mode: mongo" if not self.mongo_loaded: self._load_mongo() if not self.keys_loaded_mongo: self._load_keys_mongo() key_container = self.keys_mongo if arguments["default"] and arguments["NAME"]: if arguments["NAME"] in key_container.names(): key_container.setdefault(arguments["NAME"]) # Update mongo db defaults with new default key print 'The default key is set to: ', arguments['NAME'] else: print "ERROR: Specified key is not registered." return if arguments["add"]: print "Attempting to add key..." key_container.__setitem__(arguments["NAME"],arguments["KEY"]) return if arguments["delete"]: print "Attempting to delete key. {0}".format(arguments["NAME"]) if self.use_yaml: print "WARNING: This will only remove the keys that have not been written to the databse\ already when 'keys persist' is called. If your key is already in the database, \ you should use mongo mode\n" key_container.delete(arguments["NAME"]) return if arguments["persist"]: if not self.mongo_loaded: self._load_mongo() if not self.keys_loaded_mongo: self._load_keys_mongo() key_mongo = self.keys_mongo key_yaml = self.keys names = key_yaml.names() for name in names: key_mongo.__setitem__(name, key_yaml._getvalue(name),key_type="string") key_mongo.setdefault(key_yaml.get_default_key()) return if (arguments["info"]) or (args==""): if arguments["--json"]: if arguments["NAME"] is None: name = "keys" else: name = arguments["NAME"] try: print json.dumps(key_container[name], indent=4) except: print "ERROR: Something went wrong in looking up keys. Did u give the right name??" return else: mykeys = {} header = ["Default", "Fingerprint"] try: mykeys["default"] = key_container.get_default_key() except: mykeys["default"] = "default is not set, please set it" for name in key_container.names(): mykeys[name] = key_container.fingerprint(name) print two_column_table(mykeys) return
self.cm_user.add_completed_projects(self.username, project) self._load_projects() msg = '{0} project is in a completed project(s)'.format(project) log.info(msg) print(msg) return else: # log.info ("project info for all") if arguments["--format"] == "json": a = json.loads(self.projects.dump()) print(print_format_dict(a, kind='json')) return else: a = json.loads(self.projects.dump()) print(two_column_table(a)) ''' print print "Project Information" print "-------------------" print if self.projects.names("default") is not "" and not []: print "%10s:" % "default", self.projects.names("default") else: print "%10s:" % "default ", \ "default is not set, please set it" if len(self.projects.names("active")) > 0: print "%10s:" % "projects", \ ', '.join(self.projects.names("active"))
def do_defaults(self, args, arguments): """ Usage: defaults clean defaults load defaults [list] [--json] defaults set variable value NOTIMPLEMENTED defaults variable NOTIMPLEMENTED defaults format (json|table) NOTIMPLEMENTED This manages the defaults associated with the user. You can load, list and clean defaults associated with a user and a cloud. The default parameters include index, prefix, flavor and image. Arguments: CLOUD The name of Cloud - this has to be implemented Options: -j --json json output Description: defaults set a hallo sets the variable a to the value hallo NOT YET IMPLEMENTED defaults a returns the value of the variable NOT YET IMPLEMENTED default format json default format table sets the default format how returns are printed. if set to json json is returned, if set to table a pretty table is printed NOT YET IMPLEMENTED """ if arguments["clean"]: self.defDict = {} return if arguments["load"]: if not self.default_loaded: self.createDefaultDict() self.default_loaded = True return if (arguments["list"] or (args == '') or (args == '--json')): self.do_defaults("load") if arguments["--json"]: print json.dumps(self.defDict, indent=4) return else: print two_column_table(self.defDict) return return
def do_keys(self, args, arguments): """ Usage: keys info [--json] [NAME][--yaml][--mongo] keys mode MODENAME keys default NAME [--yaml][--mongo] keys add NAME KEY [--yaml][--mongo] keys delete NAME [--yaml][--mongo] keys persist Manages the keys Arguments: NAME The name of a key MODENAME This is used to specify the mode name. Mode name can be either 'yaml' or 'mongo' KEY This is the actual key that has to added Options: -v --verbose verbose mode -j --json json output -y --yaml forcefully use yaml mode -m --mongo forcefully use mongo mode Description: keys info Prints list of keys. NAME of the key can be specified keys mode MODENAME Used to change default mode. Valid MODENAMES are yaml(default) and mongo mode. keys default NAME Used to set a key from the key-list as the default key keys add NAME KEY adding/updating keys. Please provide filename and not the actual key keys delete NAME deletes a key. In yaml mode it can delete only keys that are not persisted keys persist Saves the temporary yaml data structure to mongo """ if arguments["mode"]: if arguments["MODENAME"] == "yaml": self.use_yaml = True print "SUCCESS: Set mode to yaml" return elif arguments["MODENAME"] == "mongo": self.use_yaml = False print "SUCCESS: Set mode to mongo" return else: print "ERROR: Wrong MODENAME. only valid modes are 'mongo' and 'yaml'" return if arguments["--yaml"] and arguments["--mongo"]: print "ERROR: you can specify only one mode" return elif arguments["--yaml"]: self.use_yaml = True elif arguments["--mongo"]: self.use_yaml = False if self.use_yaml: print "Mode: yaml" if not self.keys_loaded: self._load_keys() key_container = self.keys else: print "Mode: mongo" if not self.mongo_loaded: self._load_mongo() if not self.keys_loaded_mongo: self._load_keys_mongo() key_container = self.keys_mongo if arguments["default"] and arguments["NAME"]: if arguments["NAME"] in key_container.names(): key_container.setdefault(arguments["NAME"]) # Update mongo db defaults with new default key print 'The default key is set to: ', arguments['NAME'] else: print "ERROR: Specified key is not registered." return if arguments["add"]: print "Attempting to add key..." key_container.__setitem__(arguments["NAME"], arguments["KEY"]) return if arguments["delete"]: print "Attempting to delete key. {0}".format(arguments["NAME"]) if self.use_yaml: print "WARNING: This will only remove the keys that have not been written to the databse\ already when 'keys persist' is called. If your key is already in the database, \ you should use mongo mode\n" key_container.delete(arguments["NAME"]) return if arguments["persist"]: if not self.mongo_loaded: self._load_mongo() if not self.keys_loaded_mongo: self._load_keys_mongo() key_mongo = self.keys_mongo key_yaml = self.keys names = key_yaml.names() for name in names: key_mongo.__setitem__(name, key_yaml._getvalue(name), key_type="string") key_mongo.setdefault(key_yaml.get_default_key()) return if (arguments["info"]) or (args == ""): if arguments["--json"]: if arguments["NAME"] is None: name = "keys" else: name = arguments["NAME"] try: print json.dumps(key_container[name], indent=4) except: print "ERROR: Something went wrong in looking up keys. Did u give the right name??" return else: mykeys = {} header = ["Default", "Fingerprint"] try: mykeys["default"] = key_container.get_default_key() except: mykeys["default"] = "default is not set, please set it" for name in key_container.names(): mykeys[name] = key_container.fingerprint(name) print two_column_table(mykeys) return