示例#1
0
 def __init__(self):
     self.data = {
         'notebook_dir': path_expand('~/notebook'),
         'profile_nbserver_dir': path_expand('~/.ipython/profile_nbserver'),
         'cert': '~/.ipython/profile_nbserver/mycert.pem',
         'cloudmesh': path_expand('~/.cloudmesh'),
         'pid': None
     }
示例#2
0
    def generate_from_dict(self, d, out_file):
        cloudmesh_yaml = path_expand(self.filename)
        t = cm_template(cloudmesh_yaml)

        result = t.replace(kind="dict", values=d)
        location = path_expand(out_file)
        yaml_file = open(location, 'w+')
        print(yaml.dump(result, default_flow_style=False), file=yaml_file)
        yaml_file.close()
        log.info("Written new yaml file in " + location)
示例#3
0
 def _create_dir(self, path=None):
     if path is not None:
         self.data['notebook_dir'] = path_expand(path)
     try:
         os.makedirs("{notebook_dir}".format(**self.data), 0700)
     except:
         pass
示例#4
0
文件: mq.py 项目: mnozary/cloudmesh
def set_rabbitmq_env():

    global RABBITMQ_SERVER

    location = path_expand("~/.cloudmesh/rabbitm")

    if sys.platform == "darwin":
        mkdir("-p", location)
        rabbit_env["RABBITMQ_MNESIA_BASE"] = location
        rabbit_env["RABBITMQ_LOG_BASE"] = location
        os.environ["RABBITMQ_MNESIA_BASE"] = location
        os.environ["RABBITMQ_LOG_BASE"] = location
        rabbit_env[
            "rabbitmq_server"] = "/usr/local/opt/rabbitmq/sbin/rabbitmq-server"
        rabbit_env["rabbitmqctl"] = "/usr/local/opt/rabbitmq/sbin/rabbitmqctl"
    elif sys.platform == "linux2":
        mkdir("-p", location)
        rabbit_env["RABBITMQ_MNESIA_BASE"] = location
        rabbit_env["RABBITMQ_LOG_BASE"] = location
        os.environ["RABBITMQ_MNESIA_BASE"] = location
        os.environ["RABBITMQ_LOG_BASE"] = location
        rabbit_env["rabbitmq_server"] = "/usr/sbin/rabbitmq-server"
        rabbit_env["rabbitmqctl"] = "/usr/sbin/rabbitmqctl"
    else:
        print "WARNING: cloudmesh rabbitmq user install not supported, " \
            "using system install"
示例#5
0
    def write(self, filename=None, format="dict", attribute_indent=attribute_indent):
        """write the dict"""

        if filename is not None:
            location = path_expand(filename)
        else:
            location = self['meta']['location']

        # with open('data.yml', 'w') as outfile:
            #    outfile.write( yaml.dump(data, default_flow_style=True) )

        # Make a backup
        self.make_a_copy(location)

        f = os.open(location, os.O_CREAT | os.O_TRUNC |
                    os.O_WRONLY, stat.S_IRUSR | stat.S_IWUSR)
        if format == "json":
            os.write(f, self.json())
        elif format in ['yml', 'yaml']:
            #d = dict(self)
            #os.write(f, yaml.dump(d, default_flow_style=False))
            os.write(f, ordered_dump(OrderedDict(self),
                                     Dumper=yaml.SafeDumper,
                                     default_flow_style=False,
                                     indent=attribute_indent))
        elif format == "print":
            os.write(f, custom_print(self, attribute_indent))
        else:
            os.write(f, self.dump())
        os.close(f)
示例#6
0
    def write(self,
              filename=None,
              format="dict",
              attribute_indent=attribute_indent):
        """write the dict"""

        if filename is not None:
            location = path_expand(filename)
        else:
            location = self['meta']['location']

        # with open('data.yml', 'w') as outfile:
        #    outfile.write( yaml.dump(data, default_flow_style=True) )

        # Make a backup
        self.make_a_copy(location)

        f = os.open(location, os.O_CREAT | os.O_TRUNC | os.O_WRONLY,
                    stat.S_IRUSR | stat.S_IWUSR)
        if format == "json":
            os.write(f, self.json())
        elif format in ['yml', 'yaml']:
            #d = dict(self)
            #os.write(f, yaml.dump(d, default_flow_style=False))
            os.write(
                f,
                ordered_dump(OrderedDict(self),
                             Dumper=yaml.SafeDumper,
                             default_flow_style=False,
                             indent=attribute_indent))
        elif format == "print":
            os.write(f, custom_print(self, attribute_indent))
        else:
            os.write(f, self.dump())
        os.close(f)
示例#7
0
 def get_credential(self, cloud=None, expand=False):
     if expand:
         d = self.get("cloudmesh.clouds.{0}.credentials".format(cloud))
         for key in d:
             d[key] = path_expand(str(d[key]))
         return d
     else:
         return self.cloud(key)['credentials']
示例#8
0
 def get_credential(self, cloud=None, expand=False):
     if expand:
         d = self.get("cloudmesh.clouds.{0}.credentials".format(cloud))
         for key in d:
             d[key] = path_expand(str(d[key]))
         return d
     else:
         return self.cloud(key)['credentials']
示例#9
0
 def test_expand(self):
     HEADING()
     result = self.config.get('cloudmesh.clouds.india')
     dir = result['credentials']['OS_CACERT']
     print(dir)
     assert dir.startswith("~")
     dir = path_expand(dir)
     print(dir)
     assert not dir.startswith("~")
示例#10
0
def cat(filename):
    """prints the contents of a file with the given name.

    :param filename: name of the file, which can include ~ and $
                     environment variables
    :type: string
    """
    location = path_expand(filename)
    banner(filename)
    with open(location, 'r') as f:
        print(f.read())
示例#11
0
def cat(filename):
    """prints the contents of a file with the given name.

    :param filename: name of the file, which can include ~ and $
                     environment variables
    :type: string
    """
    location = path_expand(filename)
    banner(filename)
    with open(location, 'r') as f:
        print(f.read())
示例#12
0
 def userkeys(self, attribute=None, expand=True):
     if attribute is None:
         return self['cloudmesh']['keys']
     else:
         if attribute == 'default':
             name = self['cloudmesh']['keys']['default']
             value = self['cloudmesh']['keys']['keylist'][name]
         else:
             value = self['cloudmesh']['keys']['keylist'][attribute]
         if expand:
             value = path_expand(value)
         return value
示例#13
0
 def userkeys(self, attribute=None, expand=True):
     if attribute is None:
         return self['cloudmesh']['keys']
     else:
         if attribute == 'default':
             name = self['cloudmesh']['keys']['default']
             value = self['cloudmesh']['keys']['keylist'][name]
         else:
             value = self['cloudmesh']['keys']['keylist'][attribute]
         if expand:
             value = path_expand(value)
         return value
示例#14
0
def wipe():
    """wipes out all traces from mongo"""
    kill()
    config = cm_config_server().get("cloudmesh.server.mongo")

    path = path_expand(config["path"])

    banner("{0}".format(path))
    local("mkdir -p {0}".format(path))
    result = str(ls(path))
    banner(path, "-")
    print result
    print 70 * "-"
    if result != "":
        if yn_choice("deleting the directory", default="n"):
            local("rm -rf {0}".format(path))
            local("mkdir -p {0}".format(path))
            banner("{0}".format(path))
            local("ls {0}".format(path))
示例#15
0
def backup_name(filename):
    """
    :param filename: given a filename creates a backupname of the form
                     filename.back.1. If the filename already exists
                     the number will be increasd as  much as needed so
                     the file does not exist in the given location.
                     The filename can consists a path and is expanded
                     with ~ and environment variables.
    :type filename: string
    :rtype: string
    """
    location = path_expand(filename)
    n = 0
    found = True
    while found:
        n = n + 1
        backup = "{0}.bak.{1}".format(location, n)
        found = os.path.isfile(backup)
    return backup
示例#16
0
def backup_name(filename):
    """
    :param filename: given a filename creates a backupname of the form
                     filename.back.1. If the filename already exists
                     the number will be increasd as  much as needed so
                     the file does not exist in the given location.
                     The filename can consists a path and is expanded
                     with ~ and environment variables.
    :type filename: string
    :rtype: string
    """
    location = path_expand(filename)
    n = 0
    found = True
    while found:
        n = n + 1
        backup = "{0}.bak.{1}".format(location, n)
        found = os.path.isfile(backup)
    return backup
示例#17
0
def start(auth=True):
    '''
    start the mongod service in the location as specified in
    cloudmesh_server.yaml
    '''
    banner("Starting mongod")
    config = cm_config_server().get("cloudmesh.server.mongo")

    path = path_expand(config["path"])
    port = config["port"]

    if not os.path.exists(path):
        print "Creating mongodb directory in", path
        local("mkdir -p {0}".format(path))

    with settings(warn_only=True):
        with hide('output', 'running', 'warnings'):
            lines = local(
                "ps -ax |grep '[m]ongod.*port {0}'".format(port), capture=True)\
                .split("\n")

    if lines != ['']:
        pid = lines[0].split(" ")[0]
        print "NO ACTION: mongo already running in pid {0} for port {1}"\
            .format(pid, port)
    else:
        print "ACTION: Starting mongod"
        print
        print "NOTE: the preparation of mongo may take a few minutes"
        print "      please do not interrupt this program."
        print
        print "      Please be patient!"
        print

        with_auth = ""
        if isyes(auth):
            with_auth = "--auth"

        local('mongod {2} --bind_ip 127.0.0.1 '
              '--fork --dbpath {0} '
              '--logpath {0}/mongodb.log '
              '--port {1}'.format(path, port, with_auth))
示例#18
0
def boot(auth=True):

    # wipe mongo
    wipe()

    time.sleep(1)
    PROGRESS.next()
    # start mongo without auth
    start(auth=False)

    time.sleep(5)
    PROGRESS.next()
    if isyes(auth):

        # create users
        admin()

        time.sleep(2)
        PROGRESS.next()
        # restart with auth
        kill()

        time.sleep(10)
        PROGRESS.next()
        start(auth=auth)

        time.sleep(1)
        PROGRESS.next()
    config = cm_config_server().get("cloudmesh.server.mongo")
    path = path_expand(config["path"])
    banner(path)
    print ls(path)
    banner("PROCESS")
    with settings(warn_only=True):
        local("ps -ax | fgrep mongo")
    PROGRESS.next()
示例#19
0
文件: util.py 项目: mnozary/cloudmesh
def read_key(filename):
    key = {}
    key['filename'] = path_expand(filename)
    key['string'] = open(filename, "r").read()
    key['fingerprint'] = get_fingerprint(key['string'])
    return key
示例#20
0
def read_key(filename):
    key = {}
    key['filename'] = path_expand(filename)
    key['string'] = open(filename, "r").read()
    key['fingerprint'] = get_fingerprint(key['string'])
    return key
示例#21
0
    def do_key(self, args, arguments):
        """
        Usage:
               key -h|--help
               key list [--source=SOURCE] [--dir=DIR] [--format=FORMAT]
               key add [--keyname=KEYNAME] FILENAME
               key default [KEYNAME]
               key delete KEYNAME

        Manages the keys

        Arguments:

          SOURCE         mongo, yaml, ssh
          KEYNAME        The name of a key
          FORMAT         The format of the output (table, json, yaml)
          FILENAME       The filename with full path in which the key is located

        Options:

           --dir=DIR            the directory with keys [default: ~/.ssh]
           --format=FORMAT      the format of the output [default: table]
           --source=SOURCE      the source for the keys [default: mongo]
           --keyname=KEYNAME    the name of the keys

        Description:


        key list --source=ssh  [--dir=DIR] [--format=FORMAT]

           lists all keys in the directory. If the directory is not
           specified the defualt will be ~/.ssh

        key list --source=yaml  [--dir=DIR] [--format=FORMAT]

           lists all keys in cloudmesh.yaml file in the specified directory.
            dir is by default ~/.cloudmesh

        key list [--format=FORMAT]

            list the keys in mongo

        key add [--keyname=keyname] FILENAME

            adds the key specifid by the filename to mongodb


        key list

             Prints list of keys. NAME of the key can be specified

        key default [NAME]

             Used to set a key from the key-list as the default key if NAME
             is given. Otherwise print the current default key

        key delete NAME

             deletes a key. In yaml mode it can delete only key that
             are not saved in mongo

        """

        # print arguments

        def _find_keys(directory):
            return [
                file for file in listdir(expanduser(directory))
                if file.lower().endswith(".pub")
            ]

        #
        # DIR (OK)
        #

        directory = path_expand(arguments["--dir"])
        source = arguments["--source"]

        if source not in ["ssh", "yaml", "mongo"]:
            print("ERROR: source is not defined")
            return

        #
        # PRINT DICT (OK)
        #

        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)

        #
        # PRINT SYSTEM (OK)
        #

        if arguments["list"] and source == "ssh":

            files = _find_keys(directory)

            keys = {}
            for key in files:
                keys[key] = directory + "/" + key
            print(_print_dict(keys, header=["Key", "Location"]))

            return

        #
        # PRINT YAML (OK)
        #

        if arguments["list"] and source == "yaml":

            key_store = cm_keys_yaml()
            keynames = key_store.names()
            keys = {}
            for key in keynames:
                keys[key] = get_fingerprint(key_store[key])

            print(_print_dict(keys, header=["Key", "Fingerprint"]))

            return

        #
        # FROM HERE ON BROKEN
        #

        if arguments["list"] and source == "mongo":

            username = cm_config().username()
            key_store = cm_keys_mongo(username)
            keynames = key_store.names()

            keys = {}
            for key in keynames:

                keys[key] = key_store[key]
                if keytype(keys[key]) == 'string':
                    keys[key] = get_fingerprint(keys[key])

            print(_print_dict(keys, header=["Key", "Fingerprint"]))

            return

        # self._load_keys_from_yaml()
        # self._load_mongo()
        # self._load_keys_mongo()

        if arguments["add"] and arguments["FILENAME"]:

            username = cm_config().username()
            key_store = cm_keys_mongo(username)
            filename = arguments["FILENAME"]

            # print filename
            # file existence check moved to the util function
            # if file does not exist it will return None
            keystring = get_key_from_file(filename)
            if keystring:
                keystring = keystring.strip()
                keysegments = key_parse(keystring)
                # key valid?
                if key_validate("string", keystring):
                    if arguments["--keyname"]:
                        keyname = arguments["--keyname"]
                    else:
                        # get keyname from the comment field of key
                        keyname = keysegments[2]
                        # in case a comment field is really missing
                        # use a timestamp for uniqueness
                        if keyname is None:
                            tstamp = datetime.now().strftime("%Y%m%d%H%M%S")
                            keyname = tstamp
                        # sanitize the key name for mongo use
                        else:
                            keyname = re.sub(r'@|-|\.', '_', keysegments[2])
                    key_store[keyname] = keystring
                else:
                    print("ERROR: INVALID key. Please verify!")
            else:
                print("ERROR: INVALID filename provided!")

            return
            """
            def func():
                if arguments["KEY"]:
                    key_store.__setitem__(
                        arguments["NAME"], arguments["KEY"])
                else:
                    files = _find_keys(directory)

                    result = menu_return_num(
                        title="Select a public key", menu_list=files, tries=3)
                    if result == 'q':
                        return
                    else:
                        key_store.__setitem__(arguments["NAME"],
                                                  "{0}/{1}".format(directory,files[result]))

            if arguments["NAME"] in key_store.names():
                if yn_choice("key {0} exists, update?"
                             .format(arguments["NAME"]), default='n'):
                    print "Updating key {0} ...".format(arguments["NAME"])
                    func()
                else:
                    return
            else:
                print "adding key {0} ...".format(arguments["NAME"])
                func()
            """

        if arguments["default"]:
            username = cm_config().username()
            key_store = cm_keys_mongo(username)
            # print key_store.names()
            # no name provided, will get current default key
            if not arguments["KEYNAME"]:
                defaultkey = key_store.default()
                print("Current default key is: {0}".format(defaultkey))
            # name provided, check if it exists in the db
            elif arguments["KEYNAME"] in key_store.names():
                key_store.setdefault(arguments["KEYNAME"])
                # Update mongo db defaults with new default key
                print('The default key was successfully set to: ',
                      arguments['KEYNAME'])
            else:
                print("ERROR: Specified key is not registered.")
            return

        if arguments["delete"]:
            username = cm_config().username()
            key_store = cm_keys_mongo(username)
            print("Attempting to delete key: {0}".format(arguments["KEYNAME"]))
            if self.use_yaml:

                print("WARNING: This will only remove the keys that"
                      "have not been written to the databse already when "
                      "'keys save' is"
                      "called. If your key is already in the database, "
                      "you should use mongo mode\n")

            key_store.delete(arguments["KEYNAME"])
            return

        # deprecating...
        """
示例#22
0
 def _set_filename(self, filename):
     self['filename'] = filename
     self['location'] = path_expand(self["filename"])
示例#23
0
 def _set_filename(self, filename):
     self['filename'] = filename
     self['location'] = path_expand(self["filename"])
示例#24
0
def init_shell_command(arguments):
    """
    ::

      Usage:
             init [--force] generate yaml
             init [--force] generate me
             init [--force] generate none
             init [--force] generate FILENAME
             init list [KIND] [--json]
             init list clouds [--file=FILENAME] [--json]
             init inspect --file=FILENAME
             init fill --file=FILENAME [VALUES]

      Initializes cloudmesh from a yaml file

      Arguments:
         generate   generates a yaml file
         yaml       specifies if a yaml file is used for generation
                    the file is located at me.yaml
         me         same as yaml

         none       specifies if a yaml file is used for generation
                    the file is located at CONFIG/etc/none.yaml
         FILENAME   The filename to be generated or from which to read
                    information.
         VALUES     yaml file with the velues to be sed in the FILENAME
         KIND       The kind of the yaml file.

      Options:
         --force  force mode does not ask. This may be dangerous as it
                  overwrites the CONFIG/cloudmesh.yaml file
         --file=FILENAME  The file
         --json   make the output format json
         -v       verbose mode


      Description:

        init list [KIND] [--json]
           list the versions and types of the yaml files in the
           CONFIG and CONFIG/etc directories.

        init list clouds [--file=FILENAME]
           Lists the available clouds in the configuration yaml file.

        init inspect --file=FILENAME
           print the variables in the yaml template
    """

    if arguments["inspect"]:
        filename = arguments['--file']
        if filename is None:
            filename = config_file('/cloudmesh.yaml')

        content = open(filename, 'r').read()

        t = cm_template(filename)
        sorted_vars = sorted(set(t.variables()))
        print("\n".join(sorted_vars))
        # banner("PARSER")
        # env = Environment()
        # ast = env.parse(content)
        # for v in meta.find_undeclared_variables(ast):
        #    print v
    if arguments["list"] and not arguments["clouds"]:
        dirs = [
            path_expand(config_dir + '/*.yaml'),
            path_expand(config_dir + '/etc/*.yaml')
        ]
        file_list = []
        for directory in dirs:
            file_list.extend(glob.glob(directory))
        vector = {}
        vector['kind'] = []
        vector['yaml_version'] = []
        vector['meta'] = []
        vector['filename'] = []
        for filename in file_list:
            values = {'kind': "-", 'yaml_version': "-", 'meta': "-"}
            head_of_file = find_meta(filename)
            values = {'kind': "-", 'yaml_version': "-", 'meta': "-"}
            for line in head_of_file:
                if ":" in line:
                    (attribute, value) = line.strip().split(":")
                    if attribute in ["kind", "yaml_version"]:
                        values[attribute] = value.strip()
                    if attribute in ["meta"]:
                        values[attribute] = "+"
            if arguments["KIND"] is None or \
               values['kind'] == arguments['KIND']:
                for attribute in values.keys():
                    vector[attribute].append(values[attribute])
                vector['filename'].append(filename)

        vector['Kind'] = vector.pop('kind')
        vector['Version'] = vector.pop('yaml_version')
        vector['Meta'] = vector.pop('meta')
        vector['Filename'] = vector.pop('filename')

        banner("Configuration Directory: {0}".format(config_dir), c="-")
        print(column_table(vector))

        # print filename, values["kind"], values["version"]

    if arguments["list"] and arguments["clouds"]:
        filename = arguments['--file']
        if filename is None:
            filename = config_file('/cloudmesh.yaml')
        config = cm_config(filename)

        data = {}
        data['Clouds'] = config.cloudnames()
        data['Labels'] = []
        data['Type'] = []
        data['Version'] = []
        for cloud_key in data['Clouds']:
            data['Labels'].append(config.cloud(cloud_key)['cm_label'])
            data['Type'].append(config.cloud(cloud_key)['cm_type'])
            data['Version'].append(config.cloud(cloud_key)['cm_type_version'])
        if arguments["--json"]:
            print(json.dumps(data, sort_keys=True, indent=4))
        else:
            print(column_table(data, ['Labels', 'Clouds', 'Type', 'Version']))

    if arguments["fill"]:

        filename_template = arguments['--file']
        if filename_template is None:
            filename_template = config_file('/etc/cloudmesh.yaml')
        filename_template = path_expand(filename_template)

        filename_values = arguments['VALUES']

        if filename_values is None:
            filename_values = config_file('/me.yaml')

        content = open(filename_template, 'r').read()

        t = cm_template(filename_template)
        sorted_vars = sorted(set(t.variables()))

        try:
            values = ConfigDict(filename=filename_values)
        except Exception, e:
            print("ERROR: There is an error in the yaml file", e)

        for cloud in values['clouds']:
            values['clouds'][cloud]['default'] = {}
            values['clouds'][cloud]['default']['image'] = None
            values['clouds'][cloud]['default']['flavor'] = None

        banner("%s -> %s" % (filename_values, filename_template))
        env = Environment(undefined=IgnoreUndefined)
        template = env.from_string(content)
        result = template.render(values)
        print(result)
示例#25
0
        result = template.render(values)
        print(result)

    if arguments["generate"]:
        filename_tmp = config_file('/cloudmesh-new.yaml')
        filename_out = config_file('/cloudmesh.yaml')
        filename_bak = backup_name(filename_out)
        filename_template = config_file("/etc/cloudmesh.yaml")
        if arguments["generate"] and (arguments["me"]):
            filename_values = config_file("/me.yaml")
        elif arguments["generate"] and (arguments["yaml"]):
            filename_values = config_file("/me.yaml")
        elif (arguments["generate"] and arguments["none"]):
            filename_values = config_file("/etc/me-none.yaml")
        elif arguments["FILENAME"] is not None:
            filename_values = path_expand(arguments["FILENAME"])
        # print me_filename
        # print etc_filename

        try:
            values = ConfigDict(filename=filename_values)
        except Exception, e:
            print("ERROR: There is an error in the yaml file", e)

        for cloud in values['clouds']:
            values['clouds'][cloud]['default'] = {}
            values['clouds'][cloud]['default']['image'] = None
            values['clouds'][cloud]['default']['flavor'] = None

        content = open(filename_template, 'r').read()
        env = Environment(undefined=IgnoreUndefined)
示例#26
0
 def _generate_from_dict(self, d):
     cloudmesh_yaml = path_expand(self.filename)
     try:
         result = self.replace(kind="dict", values=d)
     except Exception, e:
         print("EEEE", e)
示例#27
0
def get_key_from_file(filename):
    ret = None
    # existence check
    if os.path.isfile(os.path.expanduser(filename)):
        ret = open(path_expand(filename), "r").read()
    return ret
示例#28
0
    def __init__(self, filename):

        self.filename = path_expand(filename)
        self.content = open(self.filename, 'r').read()
示例#29
0
    def do_key(self, args, arguments):
        """
        Usage:
               key -h|--help
               key list [--source=SOURCE] [--dir=DIR] [--format=FORMAT]
               key add [--keyname=KEYNAME] FILENAME
               key default [KEYNAME]
               key delete KEYNAME

        Manages the keys

        Arguments:

          SOURCE         mongo, yaml, ssh
          KEYNAME        The name of a key
          FORMAT         The format of the output (table, json, yaml)
          FILENAME       The filename with full path in which the key is located

        Options:

           --dir=DIR            the directory with keys [default: ~/.ssh]
           --format=FORMAT      the format of the output [default: table]
           --source=SOURCE      the source for the keys [default: mongo]
           --keyname=KEYNAME    the name of the keys

        Description:


        key list --source=ssh  [--dir=DIR] [--format=FORMAT]

           lists all keys in the directory. If the directory is not
           specified the defualt will be ~/.ssh

        key list --source=yaml  [--dir=DIR] [--format=FORMAT]

           lists all keys in cloudmesh.yaml file in the specified directory.
            dir is by default ~/.cloudmesh

        key list [--format=FORMAT]

            list the keys in mongo

        key add [--keyname=keyname] FILENAME

            adds the key specifid by the filename to mongodb


        key list

             Prints list of keys. NAME of the key can be specified

        key default [NAME]

             Used to set a key from the key-list as the default key if NAME
             is given. Otherwise print the current default key

        key delete NAME

             deletes a key. In yaml mode it can delete only key that
             are not saved in mongo

        """
        # print arguments

        def _find_keys(directory):
            return [file for file in listdir(expanduser(directory))
                    if file.lower().endswith(".pub")]

        #
        # DIR (OK)
        #

        directory = path_expand(arguments["--dir"])
        source = arguments["--source"]

        if source not in ["ssh", "yaml", "mongo"]:
            print("ERROR: source is not defined")
            return

        #
        # PRINT DICT (OK)
        #

        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)

        #
        # PRINT SYSTEM (OK)
        #

        if arguments["list"] and source == "ssh":

            files = _find_keys(directory)

            keys = {}
            for key in files:
                keys[key] = directory + "/" + key
            print(_print_dict(keys, header=["Key", "Location"]))

            return

        #
        # PRINT YAML (OK)
        #

        if arguments["list"] and source == "yaml":

            key_store = cm_keys_yaml()
            keynames = key_store.names()
            keys = {}
            for key in keynames:
                keys[key] = get_fingerprint(key_store[key])

            print(_print_dict(keys, header=["Key", "Fingerprint"]))

            return

        #
        # FROM HERE ON BROKEN
        #

        if arguments["list"] and source == "mongo":

            username = cm_config().username()
            key_store = cm_keys_mongo(username)
            keynames = key_store.names()

            keys = {}
            for key in keynames:

                keys[key] = key_store[key]
                if keytype(keys[key]) == 'string':
                    keys[key] = get_fingerprint(keys[key])

            print(_print_dict(keys, header=["Key", "Fingerprint"]))

            return

        # self._load_keys_from_yaml()
        # self._load_mongo()
        # self._load_keys_mongo()

        if arguments["add"] and arguments["FILENAME"]:

            username = cm_config().username()
            key_store = cm_keys_mongo(username)
            filename = arguments["FILENAME"]

            # print filename
            # file existence check moved to the util function
            # if file does not exist it will return None
            keystring = get_key_from_file(filename)
            if keystring:
                keystring = keystring.strip()
                keysegments = key_parse(keystring)
                # key valid?
                if key_validate("string", keystring):
                    if arguments["--keyname"]:
                        keyname = arguments["--keyname"]
                    else:
                        # get keyname from the comment field of key
                        keyname = keysegments[2]
                        # in case a comment field is really missing
                        # use a timestamp for uniqueness
                        if keyname is None:
                            tstamp = datetime.now().strftime("%Y%m%d%H%M%S")
                            keyname = tstamp
                        # sanitize the key name for mongo use
                        else:
                            keyname = re.sub(r'@|-|\.', '_', keysegments[2])
                    key_store[keyname] = keystring
                else:
                    print("ERROR: INVALID key. Please verify!")
            else:
                print("ERROR: INVALID filename provided!")

            return

            """
            def func():
                if arguments["KEY"]:
                    key_store.__setitem__(
                        arguments["NAME"], arguments["KEY"])
                else:
                    files = _find_keys(directory)

                    result = menu_return_num(
                        title="Select a public key", menu_list=files, tries=3)
                    if result == 'q':
                        return
                    else:
                        key_store.__setitem__(arguments["NAME"],
                                                  "{0}/{1}".format(directory,files[result]))

            if arguments["NAME"] in key_store.names():
                if yn_choice("key {0} exists, update?"
                             .format(arguments["NAME"]), default='n'):
                    print "Updating key {0} ...".format(arguments["NAME"])
                    func()
                else:
                    return
            else:
                print "adding key {0} ...".format(arguments["NAME"])
                func()
            """

        if arguments["default"]:
            username = cm_config().username()
            key_store = cm_keys_mongo(username)
            # print key_store.names()
            # no name provided, will get current default key
            if not arguments["KEYNAME"]:
                defaultkey = key_store.default()
                print("Current default key is: {0}".format(defaultkey))
            # name provided, check if it exists in the db
            elif arguments["KEYNAME"] in key_store.names():
                key_store.setdefault(arguments["KEYNAME"])
                # Update mongo db defaults with new default key
                print('The default key was successfully set to: ', arguments['KEYNAME'])
            else:
                print("ERROR: Specified key is not registered.")
            return

        if arguments["delete"]:
            username = cm_config().username()
            key_store = cm_keys_mongo(username)
            print("Attempting to delete key: {0}".format(arguments["KEYNAME"]))
            if self.use_yaml:

                print("WARNING: This will only remove the keys that"
                      "have not been written to the databse already when "
                      "'keys save' is"
                      "called. If your key is already in the database, "
                      "you should use mongo mode\n")

            key_store.delete(arguments["KEYNAME"])
            return

        # deprecating...
        """
示例#30
0
def get_key_from_file(filename):
    ret = None
    # existence check
    if os.path.isfile(os.path.expanduser(filename)):
        ret = open(path_expand(filename), "r").read()
    return ret
示例#31
0
    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.user = cm_user()

        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)
            
            userinfo = self.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
            t_url = \
            "https://raw.githubusercontent.com/cloudmesh/cloudmesh/master/heat-templates/centos6/launcher/launcher.yaml"
            param = {'KeyName': keynamenew,
                     'Cookbook': cookbook,
                     'dummy': dummy}
            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))
示例#32
0
"""Common methods and classes used at the intall time."""
import sys
from cloudmesh_install.util import path_expand

__config_dir_prefix__ = "~/.cloudmesh"

__config_dir__ = path_expand(__config_dir_prefix__)


def config_file(filename):
    return __config_dir__ + filename


def config_file_raw(filename):
    return __config_dir_prefix__ + filename


def config_file_prefix():
    return __config_dir_prefix__