def test_007(self): """ set default variable :return: """ HEADING() name = "myvar" value = "myvalue" cloud = "mycloud" Default.set(name, value, cloud) assert Default.get(name, cloud) == value self._check(value)
def get(prefix=None, idx=None, user=None): """Return a vm name to use next time. prefix or index can be given to update a vm name (optional) Args: prefix (str, optional): the name of prefix idx (int, str, optional): the index to increment. This can be a digit or arithmetic e.g. +5 or -3 can be used """ user = user or getpass.getuser() prefix = prefix or user if type(idx) is not int: idx = int(idx) Default.set('index', idx) return "%{:}_%{:}".format()
def __init__(self, context): cmd.Cmd.__init__(self) self.command_topics = {} self.register_topics() self.context = context if self.context.debug: print("init CloudmeshConsole") self.prompt = 'ghost> ' self.banner = textwrap.dedent(""" +==========================================================+ . _ .-') ('-. .-') _ . . ( '.( OO )_ _( OO) ( OO) ) . . .-----. .-'),-----. ,--. ,--.)(,------./ '._ . . ' .--./ ( OO' .-. '| `.' | | .---'|'--...__) . . | |('-. / | | | || | | | '--. .--' . . /_) |OO )\_) | |\| || |'.'| | (| '--. | | . . || |`-'| \ | | | || | | | | .--' | | . . (_' '--'\ `' '-' '| | | | | `---. | | . . `-----' `-----' `--' `--' `------' `--' . +==========================================================+ Comet Ghost Shell """) # KeyCommands.__init__(self, context) # # set default cloud and default group if they do not exist # use the first cloud in cloudmesh.yaml as default # value = Default.get('cloud', 'general') if value is None: filename = path_expand("~/.cloudmesh/cloudmesh.yaml") clouds = ConfigDict(filename=filename)["cloudmesh"]["clouds"] cloud = clouds.keys()[0] Default.set('cloud', cloud, 'general') value = Default.get('default', 'general') if value is None: Default.set('default', 'default', 'general') for c in CloudmeshConsole.__bases__[1:]: # noinspection PyArgumentList c.__init__(self, context)
def __init__(self, context): cmd.Cmd.__init__(self) self.command_topics = {} self.register_topics() self.context = context if self.context.debug: print("init CloudmeshConsole") self.prompt = 'cm> ' self.banner = textwrap.dedent(""" +=======================================================+ . ____ _ _ _ . . / ___| | ___ _ _ __| |_ __ ___ ___ ___| |__ . . | | | |/ _ \| | | |/ _` | '_ ` _ \ / _ \/ __| '_ \ . . | |___| | (_) | |_| | (_| | | | | | | __/\__ \ | | | . . \____|_|\___/ \__,_|\__,_|_| |_| |_|\___||___/_| |_| . +=======================================================+ Cloudmesh Shell """) # KeyCommands.__init__(self, context) # # set default cloud and default group if they do not exist # use the first cloud in cloudmesh.yaml as default # filename = path_expand("~/.cloudmesh/cloudmesh.yaml") create_cloudmesh_yaml(filename) # sys,exit(1) value = Default.get('cloud', cloud='general') if value is None: clouds = ConfigDict(filename=filename)["cloudmesh"]["clouds"] cloud = clouds.keys()[0] Default.set('cloud', cloud, cloud='general') value = Default.get('default', cloud='general') if value is None: Default.set('default', 'default', cloud='general') cluster = 'india' # hardcode a value if not defined value = Default.get('cluster', cloud='general') if value is None: try: hosts = ssh_config().names() if hosts is not None: cluster = hosts[0] except: pass # use the hardcoded cluster else: cluster = value Default.set('cluster', cluster, cloud='general') # # Read cloud details from yaml file # filename = 'cloudmesh.yaml' config = ConfigDict(filename=filename)["cloudmesh"] clouds = config["clouds"] defaults = {'clouds': {}, 'key': {}} for cloud in clouds: if "default" in config['clouds'][cloud]: defaults['clouds'][cloud] = config["clouds"][cloud]['default'] if "default" in config["keys"]: defaults["keys"] = config["keys"]["default"] else: defaults['key'] = None for cloud in defaults["clouds"]: for default, value in defaults["clouds"][cloud].iteritems(): Default.set(default, value, cloud=cloud) for c in CloudmeshConsole.__bases__[1:]: # noinspection PyArgumentList c.__init__(self, context)
def do_default(self, args, arguments): """ :: Usage: default list [--cloud=CLOUD] [--format=FORMAT] [--all] default delete KEY [--cloud=CLOUD] default KEY [--cloud=CLOUD] default KEY=VALUE [--cloud=CLOUD] Arguments: KEY the name of the default VALUE the value to set the key to Options: --cloud=CLOUD the name of the cloud --format=FORMAT the output format [default: table] --all lists all the default values Description: Cloudmesh has the ability to manage easily multiple clouds. One of the key concepts to make the list of such clouds easier is the introduction of defaults for each cloud or globally. Hence it is possible to set default images, flavors for each cloud, and also the default cloud. The default command is used to set and list the default values. These defaults are used in other commands if they are not overwritten by a command parameter. The current default values can by listed with --all option:( if you have a default cloud specified. You can also add a cloud parameter to apply the command to a specific cloud) default list A default can be set with default KEY=VALUE To look up a default value you can say default KEY A default can be deleted with default delete KEY Examples: default list --all default list --cloud=general default image=xyz default image=abc --cloud=kilo default image default image --cloud=kilo default delete image default delete image --cloud=kilo """ # pprint(arguments) """ For these keys, the 'cloud' column in db will always be 'general'. """ general_keys = ["cloud", "cluster", "queue"] """ If the default cloud has been set (eg. default cloud=xxx), then subsequent defaults for any key (eg. default image=yyy), will have 'cloud' column in db as the default cloud that was set. (eg. image=yyy for cloud=xxx). """ if arguments["KEY"] in general_keys: cloud = "general" else: cloud = arguments["--cloud"] or Default.get("cloud", "general") or "general" if arguments["list"]: output_format = arguments["--format"] if arguments['--all'] or arguments["--cloud"] is None: cloud = None result = Default.list(cloud=cloud, format=output_format) if result is None: Console.error("No default values found") else: print(result) return "" elif arguments["delete"]: key = arguments["KEY"] result = Default.delete(key, cloud) if result is None: Console.error("Key {} not present".format(key)) else: Console.ok("Deleted key {} for cloud {}. ok.".format(key, cloud)) return "" elif "=" in arguments["KEY"]: key, value = arguments["KEY"].split("=") if key in general_keys: cloud = "general" Default.set(key, value, cloud) Console.ok( "set in defaults {}={}. ok.".format(key, value)) return "" elif arguments["KEY"]: key = arguments["KEY"] result = Default.get(key, cloud) if result is None: Console.error("No default values found") else: Console.ok("Default value for {} is {}".format(key, result)) return ""
def do_select(self, args, arguments): """ :: Usage: select image [CLOUD] select flavor [CLOUD] select cloud [CLOUD] select key [CLOUD] selects interactively the default values Arguments: CLOUD the name of the cloud Options: """ # pprint(arguments) cloud = arguments["CLOUD"] or Default.get_cloud() if arguments["image"]: try: image_dict = Image.list(cloud, format="dict") image_names = list() for image in image_dict.values(): image_names.append(image["name"]) number = menu_return_num(title="Select an Image", menu_list=image_names, tries=10, with_display=True) if number == "q": pass else: image = image_names[number] print("Selected image " + image) Default.set("image", image, cloud=cloud) except: print("ERROR: could not set image.") elif arguments["flavor"]: try: flavor_dict = Flavor.list(cloud, format="dict") flavor_names = list() for flavor in flavor_dict.values(): flavor_names.append(flavor["name"]) number = menu_return_num(title="Select a Flavor", menu_list=flavor_names, tries=10, with_display=True) if number == "q": pass else: flavor = flavor_names[number] print("Selected flavor " + flavor) Default.set("flavor", flavor, cloud=cloud) except: print("ERROR: could not set flavor.") elif arguments["cloud"]: try: config = ConfigDict("cloudmesh.yaml") clouds = config["cloudmesh"]["clouds"] for key in clouds.keys(): Console.ok(" " + key) number = menu_return_num(title="Select a cloud", menu_list=clouds.keys(), tries=10, with_display=True) if number == "q": pass else: cloud = clouds.keys()[number] print("Selected cloud " + cloud) Default.set("cloud", cloud, "general") except: print("ERROR: could not set cloud.") elif arguments["key"]: try: db = SSHKeyDBManager() key_dict = db.table_dict() key_names = list() for key in key_dict.values(): key_names.append(key["name"]) number = menu_return_num(title="Select a Key", menu_list=key_names, tries=10, with_display=True) if number == "q": pass else: key = key_names[number] print("Selected key " + key) # TODO Fix default key setting in key DB # db.set_default(key) Default.set("key", key, cloud=cloud) except: print("ERROR: could not set key") return ""
print (e) print (keyname) print (filename) Console.error("Problem adding the key `{}` from file `{}`".format(keyname, filename)) elif arguments['default']: # print("default") if arguments['KEYNAME']: keyname = None try: keyname = arguments['KEYNAME'] sshdb = SSHKeyDBManager() sshdb.set_default(keyname) Default.set("key", keyname, "general") print("Key {:} set as default".format(keyname)) msg = "info. OK." Console.ok(msg) except Exception, e: import traceback print(traceback.format_exc()) print (e) Console.error("Setting default for key {:} failed.".format(keyname)) elif arguments['--select']: keyname = None try: sshdb = SSHKeyDBManager() select = sshdb.select() if select != 'q':
def do_select(self, args, arguments): """ :: Usage: select image [CLOUD] select flavor [CLOUD] select cloud [CLOUD] select key [CLOUD] selects interactively the default values Arguments: CLOUD the name of the cloud Options: """ pprint(arguments) cloud = arguments["CLOUD"] if arguments["image"]: pass elif arguments["flavor"]: pass elif arguments["cloud"]: config = ConfigDict("cloudmesh.yaml") clouds = config["cloudmesh"]["clouds"] for key in clouds.keys(): Console.ok(" " + key) number = menu_return_num(title="Select a cloud", menu_list=clouds.keys(), tries=10, with_display=True) if number == "q": pass else: cloud = clouds.keys()[number] print("Selected cloud " + cloud) Default.set("cloud", cloud, "general") elif arguments["key"]: db = SSHKeyDBManager() d = db.table_dict() try: element = dict_choice(d) keyname = element["name"] print("Set default key to ", keyname) db.set_default(keyname) Default.set("key", keyname, "general") except: print("ERROR: could not set key") return ""
def do_default(self, args, arguments): """ :: Usage: default default list [--cloud=CLOUD] [--format=FORMAT] [--all] default delete KEY [--cloud=CLOUD] default KEY [--cloud=CLOUD] default KEY=VALUE [--cloud=CLOUD] Arguments: KEY the name of the default VALUE the value to set the key to Options: --cloud=CLOUD the name of the cloud --format=FORMAT the output format. Values include table, json, csv, yaml. [default: table] --all lists all the default values Description: Cloudmesh has the ability to manage easily multiple clouds. One of the key concepts to manage multiple clouds is to use defaults for the cloud, the images, flavors, and other values. The default command is used to manage such default values. These defaults are used in other commands if they are not overwritten by a command parameter. The current default values can by listed with default list --all Via the default command you can list, set, get and delete default values. You can list the defaults with default list A default can be set with default KEY=VALUE To look up a default value you can say default KEY A default can be deleted with default delete KEY To be specific to a cloud you can specify the name of the cloud with the --cloud=CLOUD option. The list command can print the information in various formats iv specified. Examples: default lists the default for the current default cloud default list --all lists all default values default list --cloud=kilo lists the defaults for the cloud with the name kilo default image=xyz sets the default image for the default cloud to xyz default image=abc --cloud=kilo sets the default image for the cloud kilo to xyz default image list the default image of the default cloud default image --cloud=kilo list the default image of the cloud kilo default delete image deletes the value for the default image in the default cloud default delete image --cloud=kilo deletes the value for the default image in the cloud kilo """ # pprint(arguments) """ For these keys, the 'cloud' column in db will always be 'general'. """ general_keys = ["cloud", "cluster", "queue"] """ If the default cloud has been set (eg. default cloud=xxx), then subsequent defaults for any key (eg. default image=yyy), will have 'cloud' column in db as the default cloud that was set. (eg. image=yyy for cloud=xxx). """ if arguments["KEY"] in general_keys: cloud = "general" elif args == '': cloud = "general" arguments["--cloud"] = cloud arguments["list"] = True order=['name', 'value'] output_format = arguments["--format"] result = Default.list(cloud=cloud, order=order, format=output_format) print (result) return "" else: cloud = arguments["--cloud"] or Default.get("cloud", "general") or "general" if arguments["list"]: output_format = arguments["--format"] if arguments['--all'] or arguments["--cloud"] is None: cloud = None result = Default.list(cloud=cloud, format=output_format) if result is None: Console.error("No default values found") else: print(result) return "" elif arguments["delete"]: key = arguments["KEY"] result = Default.delete(key, cloud) if result is None: Console.error("Key {} not present".format(key)) else: Console.ok("Deleted key {} for cloud {}. ok.".format(key, cloud)) return "" elif "=" in arguments["KEY"]: key, value = arguments["KEY"].split("=") if key in general_keys: cloud = "general" Default.set(key, value, cloud) Console.ok( "set in defaults {}={}. ok.".format(key, value)) return "" elif arguments["KEY"]: key = arguments["KEY"] result = Default.get(key, cloud) if result is None: Console.error("No default values found") else: Console.ok("Default value for {} is {}".format(key, result)) return ""