本文整理汇总了Python中cloudmesh_client.common.ConfigDict.ConfigDict类的典型用法代码示例。如果您正苦于以下问题:Python ConfigDict类的具体用法?Python ConfigDict怎么用?Python ConfigDict使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ConfigDict类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: entry
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)
示例2: from_file
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))
示例3: set_username
def set_username(cls, username):
"""
Method that sets the username in yaml.
:param username:
:return:
"""
config = ConfigDict("cloudmesh.yaml")
config['cloudmesh']['profile']['user'] = username
config.save()
示例4: ec2
def ec2(cls, cloud, zipfile):
def sanitize(name):
return name.replace(".zip", "").replace("@", "_")
def find_exports(filename):
with open(filename, "r") as f:
content = f.read()
data = {}
for line in content.split("\n"):
if line.startswith("export "):
line = line.replace("export ", "")
attribute, value = line.split("=", 1)
value = value.replace("${NOVA_KEY_DIR}/", "")
# remove comments
data[attribute] = value.split("#")[0].strip()
return data
base = sanitize(os.path.basename(zipfile))
dest = sanitize(os.path.join(
path_expand("~"),
".cloudmesh",
"clouds",
cloud,
os.path.basename(zipfile)))
Console.msg("Unzip file {} -> {}".format(zipfile, dest))
r = Shell.unzip(zipfile, dest)
rcfile = os.path.join(dest, "ec2rc.sh")
data = find_exports(rcfile)
data["DEST"] = dest
data["CLOUD"] = cloud
d = {
"cm_heading": "{CLOUD}, EC2".format(**data),
"cm_host": None,
"cm_label": "{CLOUD}_ec2".format(**data),
"cm_type": "ec2",
"cm_type_version": "ec2",
"credentials": {
"EC2_ACCESS_KEY": "{EC2_ACCESS_KEY}".format(**data),
"EC2_SECRET_KEY": "{EC2_SECRET_KEY}".format(**data),
"keyname": "TBD_not_used",
"userid": "TBD_not_used",
"EC2_URL": "{EC2_URL}".format(**data),
"EC2_USER_ID": "{EC2_USER_ID}",
"EC2_PRIVATE_KEY": "{DEST}/pk.pem".format(**data),
"EC2_CERT": "{DEST}/cert.pem".format(**data),
"NOVA_CERT": "{DEST}/cacert.pem".format(**data),
"EUCALYPTUS_CERT": "{DEST}/cacert.pem".format(**data),
},
"default": {
"flavor": "m1.small",
"image": "None",
}
}
config = ConfigDict("cloudmesh.yaml")
config["cloudmesh"]["clouds"][cloud] = d
config.save()
示例5: test_002_set
def test_002_set(self):
"""testing to set a value in the dict"""
HEADING()
shutil.copy(self.etc_yaml, self.tmp_yaml)
d = ConfigDict("cloudmesh.yaml", load_order=[self.tmp_dir], verbose=True)
d["cloudmesh"]["profile"]["firstname"] = "Gregor"
d.save()
d = ConfigDict("cloudmesh.yaml", load_order=[self.tmp_dir], verbose=True)
assert d["cloudmesh"]["profile"]["firstname"] == "Gregor"
示例6: fill_out_form
def fill_out_form(cls, filename):
"""
edits profile and clouds from cloudmesh.yaml
:param filename:
:type filename: string
:return:
"""
Console.ok("Filling out form")
print(filename)
config = ConfigDict(filename)
#
# edit profile
#
profile = config["cloudmesh"]["profile"]
keys = list(profile.keys())
# TODO: test this and delete this comment
# get input that works in python 2 and 3
# replaced by
# from builtins import input
# input = None
# try:
# input = input
# except NameError:
# pass
for key in keys:
if profile[key] == "TBD":
result = input("Please enter {:}[{:}]:".format(key, profile[key])) or profile[key]
profile[key] = result
config["cloudmesh"]["profile"] = profile
config.save()
# edit clouds
clouds = config["cloudmesh"]["clouds"]
for cloud in list(clouds.keys()):
print("Editing the credentials for cloud", cloud)
credentials = clouds[cloud]["credentials"]
for key in credentials:
if key not in ["OS_VERSION", "OS_AUTH_URL"] and credentials[key] == "TBD":
result = input("Please enter {:}[{:}]:".format(key, credentials[key])) or credentials[key]
credentials[key] = result
config["cloudmesh"]["clouds"][cloud]["credentials"] = credentials
config.save()
示例7: do_color
def do_color(self, args, arguments):
"""
::
Usage:
color FLAG
Arguments:
FLAG color mode flag ON/OFF
Description:
Global switch for the console color mode.
One can switch the color mode on/off with
cm color mode ON
cm color mode OFF
By default, the color mode is ON
Examples:
color mode ON
color mode OFF
"""
# default mode is ON
color_mode = True
flag = arguments["FLAG"].lower()
if flag in ["on", "true"]:
color_mode = True
Console.set_theme(color=True)
elif flag in ["off", "false"]:
color_mode = False
Console.set_theme(color=False)
else:
Console.error("Invalid Flag")
return
# Update the cloudmesh.yaml file
config = ConfigDict("cloudmesh.yaml")
config["cloudmesh"]["system"]["console_color"] = color_mode
config.save()
Console.color = color_mode
Console.ok("Color {:}".format(str(color_mode)))
return ""
示例8: get_apikey
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)
示例9: __init__
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)
示例10: read_rc_file
def read_rc_file(cls, host, openrc, force=False):
"""
:param host: the host name
:type host: string
:param openrc: the file name
:type openrc: string
:return:
"""
openrc = Config.path_expand(openrc)
# check if file exists
if not os.path.isfile(openrc):
Console.error("File not found.")
return
with open(openrc, 'r') as f:
lines = f.read().split("\n")
config = ConfigDict("cloudmesh.yaml")
credentials = {}
for line in lines:
if line.strip().startswith("export "):
line = line.replace("export ", "")
key, value = line.split("=", 1)
credentials[key] = value
if host not in config["cloudmesh.clouds"] or force:
config["cloudmesh"]["clouds"][host] = {
"credentials": credentials,
"cm_heading": "TBD",
"cm_host": "TBD",
"cm_label": "TBD",
"cm_type": "TBD",
"cm_type_version": "TBD"}
config.save()
return config["cloudmesh"]["clouds"][host]["credentials"]
示例11: get_apikey
def get_apikey():
user = raw_input("Comet Nucleus Usename: ")
password = getpass.getpass()
keyurl = "https://comet-nucleus.sdsc.edu/nucleus/getkey"
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"]["auth_provider"] = 'apikey'
config.data["cloudmesh"]["comet"]["apikey"]["api_key"] = api_key
config.data["cloudmesh"]["comet"]["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)
示例12: do_comet
#.........这里部分代码省略.........
else:
Console.error(
"some issue while logging off. Maybe comet not reachable")
elif arguments["docs"]:
Comet.docs()
elif arguments["info"]:
Console.error("not yet implemented")
elif arguments["add"]:
print ("add the cluster")
elif arguments["start"]:
cluster_id = arguments["ID"]
print("start", cluster_id)
Cluster.start(cluster_id)
elif arguments["stop"]:
cluster_id = arguments["ID"]
print("stop", cluster_id)
Cluster.stop(cluster_id)
elif arguments["ll"]:
"""
if arguments["init"]:
print ("Initializing the comet configuration file...")
config = ConfigDict("cloudmesh.yaml")
# for unit testing only.
cometConf = config["cloudmesh.comet"]
endpoints = []
# print (cometConf.keys())
if "endpoints" in cometConf.keys():
endpoints = cometConf["endpoints"].keys()
if len(endpoints) < 1:
Console.error("No service endpoints available. "
"Please check the config template",
traceflag=False)
return ""
if "username" in cometConf.keys():
default_username = cometConf['username']
# print (default_username)
if 'TBD' == default_username:
set_default_user = \
input("Set a default username (RETURN to skip): ")
if set_default_user:
config.data["cloudmesh"]["comet"]["username"] = \
set_default_user
config.save()
Console.ok("Comet default username set!")
if "active" in cometConf.keys():
active_endpoint = cometConf['active']
set_active_endpoint = \
input("Set the active service endpoint to use. "
"The availalbe endpoints are - %s [%s]: "
% ("/".join(endpoints),
active_endpoint)
)
if set_active_endpoint:
if set_active_endpoint in endpoints:
示例13: __init__
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)
示例14: do_register
#.........这里部分代码省略.........
displays the host details in json format
register remote CLOUD
registers a remote cloud and copies the openrc file
specified in the credentials of the cloudmesh.yaml
register CLOUD --dir
Copies the entire directory from the cloud and puts it in
~/.cloudmesh/clouds/host
For kilo, The directory would be copied to
~/.cloudmesh/clouds/kilo
register env [--provider=PROVIDER] [HOSTNAME]
Reads env OS_* variables and registers a new cloud in yaml,
interactively. Default PROVIDER is openstack and HOSTNAME
is localhost.
register user [USERNAME]
Sets the user in yaml with the value provided.
"""
# from pprint import pprint
# pprint(arguments)
def _get_config_yaml_file(arguments):
filename = arguments["--yaml"] or "cloudmesh.yaml"
filename = Config.find_file(filename)
return filename
def exists(filename):
return os.path.isfile(filename)
def export(host, output):
config = ConfigDict("cloudmesh.yaml")
credentials = dict(
config["cloudmesh"]["clouds"][host]["credentials"])
if not arguments["--password"]:
credentials["OS_PASSWORD"] = "********"
if output is None:
for attribute, value in credentials.items():
print("export {}={}".format(attribute, value))
elif output == "table":
print(Printer.attribute(credentials))
else:
print(Printer.write(credentials, output=output))
# TODO: bug csv does not work
if arguments["info"]:
filename = _get_config_yaml_file(arguments)
if os.path.isfile(filename):
Console.ok("File '{}' exists. ok.".format(filename))
Console.ok("The yaml file contains the following templates:")
d = CloudRegister.list(filename,
Default.cloud,
info=False,
output="table")
print(d)
else:
Console.error("File {} does not exist".format(filename))
示例15: do_comet
#.........这里部分代码省略.........
else:
Console.error(
"some issue while logging off. Maybe comet not reachable")
elif arguments["docs"]:
Comet.docs()
elif arguments["info"]:
Console.error("not yet implemented")
elif arguments["add"]:
print ("add the cluster")
elif arguments["start"]:
cluster_id = arguments["ID"]
print("start", cluster_id)
Cluster.start(cluster_id)
elif arguments["stop"]:
cluster_id = arguments["ID"]
print("stop", cluster_id)
Cluster.stop(cluster_id)
elif arguments["ll"]:
"""
if arguments["init"]:
print ("Initializing the comet configuration file...")
config = ConfigDict("cloudmesh.yaml")
# for unit testing only.
cometConf = config["cloudmesh.comet"]
endpoints = []
# print (cometConf.keys())
if "endpoints" in cometConf.keys():
endpoints = cometConf["endpoints"].keys()
if len(endpoints) < 1:
Console.error("No service endpoints available."\
" Please check the config template")
return ""
if "username" in cometConf.keys():
default_username = cometConf['username']
# print (default_username)
if 'TBD' == default_username:
set_default_user = \
input("Set a default username (RETURN to skip): ")
if set_default_user:
config.data["cloudmesh"]["comet"]["username"] = \
set_default_user
config.save()
Console.ok("Comet default username set!")
if "active" in cometConf.keys():
active_endpoint = cometConf['active']
set_active_endpoint = \
input("Set the active service endpoint to use. "\
"The availalbe endpoints are - %s [%s]: "\
% ("/".join(endpoints),
active_endpoint)
)
if set_active_endpoint:
if set_active_endpoint in endpoints:
config.data["cloudmesh"]["comet"]["active"] = \