Exemple #1
0
def get_nova_credentials(kind="yaml", cloud=None):

    d = {}
    if kind in ["env"]:
        d['version'] = '2'
        d['username'] = os.environ['OS_USERNAME']
        d['api_key'] = os.environ['OS_PASSWORD']
        d['auth_url'] = os.environ['OS_AUTH_URL']
        d['project_id'] = os.environ['OS_TENANT_NAME']
        d['cacert'] = path_expand(os.environ['OS_CACERT'])
    elif kind in ["yaml"]:
        if cloud is None:
            raise Exception("cloud not specified")
        config = dict(ConfigDict(filename="~/.cloudmesh/cloudmesh.yaml")["cloudmesh"]["clouds"][cloud])
        cred = dict(config["credentials"])
        d['version'] = '2'
        d['username'] = cred['OS_USERNAME']
        d['api_key'] = cred['OS_PASSWORD']
        d['auth_url'] = cred['OS_AUTH_URL']
        d['project_id'] = cred['OS_TENANT_NAME']
        if 'OS_CACERT' in cred:
            d['cacert'] = path_expand(cred['OS_CACERT'])
    else:
        raise Exception ("unsupported kind: " + kind)
    return d
Exemple #2
0
def get_nova_credentials(kind="yaml", cloud=None):

    d = {}
    if kind in ["env"]:
        d['version'] = '2'
        d['username'] = os.environ['OS_USERNAME']
        d['api_key'] = os.environ['OS_PASSWORD']
        d['auth_url'] = os.environ['OS_AUTH_URL']
        d['project_id'] = os.environ['OS_TENANT_NAME']
        d['cacert'] = path_expand(os.environ['OS_CACERT'])
    elif kind in ["yaml"]:
        if cloud is None:
            raise Exception("cloud not specified")
        config = dict(
            ConfigDict(filename="~/.cloudmesh/cloudmesh.yaml")["cloudmesh"]
            ["clouds"][cloud])
        cred = dict(config["credentials"])
        d['version'] = '2'
        d['username'] = cred['OS_USERNAME']
        d['api_key'] = cred['OS_PASSWORD']
        d['auth_url'] = cred['OS_AUTH_URL']
        d['project_id'] = cred['OS_TENANT_NAME']
        if 'OS_CACERT' in cred:
            d['cacert'] = path_expand(cred['OS_CACERT'])
    else:
        raise Exception("unsupported kind: " + kind)
    return d
Exemple #3
0
def create_cloudmesh_yaml(filename):
    if not os.path.exists(filename):
        path = os.path.dirname(filename)
        if not os.path.isdir(path):
            Shell.mkdir(path)
        etc_path = os.path.dirname(cloudmesh_client.__file__)
        etc_file = os.path.join(etc_path, "etc", "cloudmesh.yaml")
        to_dir = path_expand("~/.cloudmesh")
        shutil.copy(etc_file, to_dir)
        os.system("chmod -R go-rwx " + path_expand("~/.cloudmesh"))
        Console.ok("~/.cloudmesh/cloudmesh.yaml created")
Exemple #4
0
def create_cloudmesh_yaml(filename):
    if not os.path.exists(filename):
        path = os.path.dirname(filename)
        if not os.path.isdir(path):
            Shell.mkdir(path)
        etc_path = os.path.dirname(cloudmesh_client.__file__)
        etc_file = os.path.join(etc_path, "etc", "cloudmesh.yaml")
        to_dir = path_expand("~/.cloudmesh")
        shutil.copy(etc_file, to_dir)
        os.system("chmod -R go-rwx " + path_expand("~/.cloudmesh"))
        Console.ok("~/.cloudmesh/cloudmesh.yaml created")
Exemple #5
0
    def set_level(log_level):
        """
        sets th eloglevel in teh database and the loglevel file from
        cloudmesh.yaml
        :param log_level: the loglevel
        :return:
        """
        # TODO: BUG: This seems inconsistent with our use as it mixes db and
        # cloudmesh.yaml.
        level = log_level.upper()

        Default.set(key=LogUtil.LOG_LEVEL_KEY,
                    value=log_level,
                    category=LogUtil.category)

        # get log level obj
        log_level_obj = LogUtil.get_level_obj(log_level)

        # Read the ConfigDict
        config = ConfigDict("cloudmesh.yaml")
        log_file = config["cloudmesh"]["logging"]["file"]

        # Set the logger config
        logging.basicConfig(format=LogUtil.FORMAT,
                            level=log_level_obj,
                            filename=path_expand(log_file))

        LOGGER.info("Set log level to: " + log_level)
        return "Ok."
Exemple #6
0
    def write(self, filename=None, output="dict"):
        """
        This method writes the dict into various outout formats. This includes a dict,
        json, and yaml
        :param filename: the file in which the dict is written
        :param output: is a string that is either "dict", "json", "yaml"
        :param attribute_indent: character indentation of nested attributes in
        """
        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 output == "json":
            os.write(f, self.json())
        elif output 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 output == "print":
            os.write(f, custom_print(self, attribute_indent))
        else:
            os.write(f, self.dump())
        os.close(f)
Exemple #7
0
 def do_shell_exec(self, args):
     # just ignore arguments and pass on args
     command = path_expand(args)
     try:
         os.system(command)
     except Exception as e:
         print (e)
Exemple #8
0
    def write(self, filename=None, output="dict"):
        """
        This method writes the dict into various outout formats. This includes a dict,
        json, and yaml
        :param filename: the file in which the dict is written
        :param output: is a string that is either "dict", "json", "yaml"
        :param attribute_indent: character indentation of nested attributes in
        """
        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 output == "json":
            os.write(f, self.json())
        elif output 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 output == "print":
            os.write(f, custom_print(self, attribute_indent))
        else:
            os.write(f, self.dump())
        os.close(f)
Exemple #9
0
    def do_reset(self, args, arguments):
        """
        ::

          Usage:
              reset

        Description:

            DANGER: This method erases the database.


        Examples:
            clean

        """
        filename = path_expand("~/.cloudmesh/cloudmesh.db")
        if os.path.exists(filename):
            os.remove(filename)
        Console.ok("Database reset")
        r = self.do_quit(None)
        Console.error(
            "Quitting the shell does not yet work. please exit the shell now.")

        return ""
Exemple #10
0
 def do_shell_exec(self, args):
     # just ignore arguments and pass on args
     command = path_expand(args)
     try:
         os.system(command)
     except Exception as e:
         print(e)
Exemple #11
0
 def key_add(self, name, filename, cloud=None):
     keyfile = path_expand(filename)
     if self.cloud_type(cloud) == "openstack":
         with open(os.path.expanduser(filename), 'r') as public_key:
           try:
              self.client[cloud].keypairs.create(name=name, public_key=public_key.read())
           except exceptions.Conflict, e:
              print ("key already exists: {0}".format(str(e)))
Exemple #12
0
    def _set_filename(self, filename):
        """
        Sets the filename to be used.

        :param filename: the filename
        """
        self['filename'] = filename
        self['location'] = path_expand(self["filename"])
Exemple #13
0
    def _set_filename(self, filename):
        """
        Sets the filename to be used.

        :param filename: the filename
        """
        self['filename'] = filename
        self['location'] = path_expand(self["filename"])
Exemple #14
0
 def key_add(self, name, filename, cloud=None):
     keyfile = path_expand(filename)
     if self.cloud_type(cloud) == "openstack":
         with open(os.path.expanduser(filename), 'r') as public_key:
             try:
                 self.client[cloud].keypairs.create(
                     name=name, public_key=public_key.read())
             except exceptions.Conflict, e:
                 print("key already exists: {0}".format(str(e)))
Exemple #15
0
def read_yaml_config(filename, check=True, osreplace=True, exit=True):
    """
    reads in a yaml file from the specified filename. If check is set to true
    the code will fail if the file does not exist. However if it is set to
    false and the file does not exist, None is returned.

    :param filename: the file name
    :param check: if True fails if the file does not exist,
                  if False and the file does not exist return will be None
    """
    location = filename
    if location is not None:
        location = path_expand(location)

    if not os.path.exists(location) and not check:
        return None

    if check and os.path.exists(location):

        # test for tab in yaml file
        if check_file_for_tabs(location):
            log.error("The file {0} contains tabs. yaml "
                      "Files are not allowed to contain tabs".format(location))
            sys.exit()
        result = None
        try:

            if osreplace:
                result = open(location, 'r').read()
                t = Template(result)
                result = t.substitute(os.environ)

                # data = yaml.safe_load(result)
                data = ordered_load(result, yaml.SafeLoader)
            else:
                f = open(location, "r")

                # data = yaml.safe_load(f)

                data = ordered_load(result, yaml.SafeLoader)
                f.close()

            return data
        except Exception as e:
            log.error(
                "The file {0} fails with a yaml read error".format(filename))
            log.error(str(e))
            print(traceback.format_exc())
            sys.exit()

    else:
        log.error("The file {0} does not exist.".format(filename))
        if exit:
            sys.exit()

    return None
Exemple #16
0
def read_yaml_config(filename, check=True, osreplace=True, exit=True):
    """
    reads in a yaml file from the specified filename. If check is set to true
    the code will fail if the file does not exist. However if it is set to
    false and the file does not exist, None is returned.

    :param filename: the file name
    :param check: if True fails if the file does not exist,
                  if False and the file does not exist return will be None
    """
    location = filename
    if location is not None:
        location = path_expand(location)

    if not os.path.exists(location) and not check:
        return None

    if check and os.path.exists(location):

        # test for tab in yaml file
        if check_file_for_tabs(location):
            log.error("The file {0} contains tabs. yaml "
                      "Files are not allowed to contain tabs".format(location))
            sys.exit()
        result = None
        try:

            if osreplace:
                result = open(location, 'r').read()
                t = Template(result)
                result = t.substitute(os.environ)

                # data = yaml.safe_load(result)
                data = ordered_load(result, yaml.SafeLoader)
            else:
                f = open(location, "r")

                # data = yaml.safe_load(f)

                data = ordered_load(result, yaml.SafeLoader)
                f.close()

            return data
        except Exception as e:
            log.error(
                "The file {0} fails with a yaml read error".format(filename))
            Error.traceback(e)
            sys.exit()

    else:
        log.error("The file {0} does not exist.".format(filename))
        if exit:
            sys.exit()

    return None
Exemple #17
0
    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",
            }
        }
        from pprint import pprint
        config = ConfigDict("cloudmesh.yaml")
        config["cloudmesh"]["clouds"][cloud] = d
        config.save()
Exemple #18
0
def cloudmesh_launcher_table(request):
    launcher_config = ConfigDict(path_expand("~/.cloudmesh/cloudmesh_launcher.yaml"))

    context = {
        'recipies': launcher_config["cloudmesh.launcher.recipes"],
        'title': '<div><i class="fa fa-rocket"></i> Cloudmesh Launcher List </div>'
    }

    return render(request,
                  'cloudmesh_portal/launcher/mesh_launch_table.jinja',
                  context)
Exemple #19
0
def cloudmesh_launcher_table(request):
    launcher_config = ConfigDict(
        path_expand("~/.cloudmesh/cloudmesh_launcher.yaml"))

    context = {
        'recipies': launcher_config["cloudmesh.launcher.recipes"],
        'title':
        '<div><i class="fa fa-rocket"></i> Cloudmesh Launcher List </div>'
    }

    return render(request, 'cloudmesh_portal/launcher/mesh_launch_table.jinja',
                  context)
Exemple #20
0
    def do_shell(self, args, arguments):
        """
        Usage:
           shell ARGUMENTS...

        Description:
            Executes a shell command
        """
        # just ignore arguments and pass on args
        command = path_expand(args)
        try:
            os.system(command)
        except Exception as e:
            print (e)
Exemple #21
0
def hpc_list(request):
    clusters = ConfigDict(
        path_expand("~/.cloudmesh/cloudmesh.yaml"))["cloudmesh.hpc.clusters"]
    print clusters
    data = {}
    for cluster in clusters:
        data[cluster] = {"cluster": cluster, "test": "test"}
    order = ["cluster"]
    context = {
        "data": data,
        "title": "Clusters",
        "order": order,
    }
    return render(request, 'cloudmesh_portal/hpc/hpc_table.jinja', context)
Exemple #22
0
    def do_shell(self, args, arguments):
        """
        Usage:
           shell ARGUMENTS...

        Description:
            Executes a shell command
        """
        # just ignore arguments and pass on args
        command = path_expand(args)
        try:
            os.system(command)
        except Exception as e:
            print(e)
Exemple #23
0
    def replace_vars(self, line):

        self.update_time()

        newline = line

        variables = Var.list(format="dict")

        if len(variables) is not None:
            for v in variables:
                name = variables[v]["name"]
                value = variables[v]["value"]
                newline = newline.replace("$" + name, value)

        # for v in os.environ:
        #    newline = newline.replace("$" + v.name, os.environ[v])
        newline = path_expand(newline)
        return newline
Exemple #24
0
    def replace_vars(self, line):

        self.update_time()

        newline = line

        variables = Var.list(format="dict")

        if len(variables) is not None:
            for v in variables:
                name = variables[v]["name"]
                value = variables[v]["value"]
                newline = newline.replace("$" + name, value)

        # for v in os.environ:
        #    newline = newline.replace("$" + v.name, os.environ[v])
        newline = path_expand(newline)
        return newline
Exemple #25
0
    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)
Exemple #26
0
 def mkdir(cls, newdir):
     """works the way a good mkdir should :)
     - already exists, silently complete
     - regular file in the way, raise an exception
     - parent directory(ies) does not exist, make them as well
     """
     """http://code.activestate.com/recipes/82465-a-friendly-mkdir/"""
     _newdir = path_expand(newdir)
     if os.path.isdir(_newdir):
         pass
     elif os.path.isfile(_newdir):
         raise OSError("a file with the same name as the desired "
                       "dir, '%s', already exists." % _newdir)
     else:
         head, tail = os.path.split(_newdir)
         if head and not os.path.isdir(head):
             os.mkdir(head)
         if tail:
             os.mkdir(_newdir)
Exemple #27
0
 def mkdir(cls, newdir):
     """works the way a good mkdir should :)
     - already exists, silently complete
     - regular file in the way, raise an exception
     - parent directory(ies) does not exist, make them as well
     """
     """http://code.activestate.com/recipes/82465-a-friendly-mkdir/"""
     _newdir = path_expand(newdir)
     if os.path.isdir(_newdir):
         pass
     elif os.path.isfile(_newdir):
         raise OSError("a file with the same name as the desired "
                       "dir, '%s', already exists." % _newdir)
     else:
         head, tail = os.path.split(_newdir)
         if head and not os.path.isdir(head):
             os.mkdir(head)
         if tail:
             os.mkdir(_newdir)
Exemple #28
0
def cloudmesh_launcher_start(request):
    parameters = dict(request.POST)
    for key in parameters:
        try:
            parameters[key] = parameters[key][0]
        except:
            pass
    if 'csrfmiddlewaretoken' in parameters:
        del parameters['csrfmiddlewaretoken']

    response = 'error'
    if parameters["name"]:
        name = parameters["name"]

        launcher_config = ConfigDict(
            path_expand("~/.cloudmesh/cloudmesh_launcher.yaml"))
        recipe = dict(launcher_config["cloudmesh.launcher.recipes"])[name]

        print(json.dumps(recipe, indent=4))

        response = "error"

        if recipe["script"]["type"] in ["sh", "shell"]:
            script = recipe["script"]["value"].format(**parameters)
            print(script)
            launcher = Launcher("shell")
            print(type(launcher))
            response = launcher.run(script=script)
            parameters["script"] = script

    else:
        parameters = "error"

    context = {
        'title': '<div><i class="fa fa-rocket"></i> Cloudmesh Launcher</div>',
        "response": response,
        "parameters": parameters,
    }

    return render(request,
                  'cloudmesh_portal/launcher/mesh_launch_response.jinja',
                  context)
Exemple #29
0
    def unzip(source_filename, dest_dir):
        """
        unzips a file into the destination directory
        :param dest_dir: the destination directory
        :return:
        """

        with zipfile.ZipFile(source_filename) as zf:
            for member in zf.infolist():
                # Path traversal defense copied from
                # http://hg.python.org/cpython/file/tip/Lib/http/server.py#l789
                words = member.filename.split('/')
                path = path_expand(dest_dir)
                for word in words[:-1]:
                    drive, word = os.path.splitdrive(word)
                    head, word = os.path.split(word)
                    if word in (os.curdir, os.pardir, ''):
                        continue
                    path = os.path.join(path, word)
                zf.extract(member, path)
Exemple #30
0
def cloudmesh_launcher(request):
    if request.method == 'POST':
        print "HHHHHH", request.form.keys()
        for key in request.form.keys():
            print key, ":", request.form[key]
    else:
        print "HEY JUDE"

    launcher_config = ConfigDict(path_expand("~/.cloudmesh/cloudmesh_launcher.yaml"))

    context = {
        'recipies': launcher_config["cloudmesh.launcher.recipes"],
        'title': '<div><i class="fa fa-rocket"></i> Cloudmesh Launcher </div>'
    }

    pprint(context)

    return render(request,
                  'cloudmesh_portal/launcher/mesh_launch.jinja',
                  context)
Exemple #31
0
    def unzip(source_filename, dest_dir):
        """
        unzips a file into the destination directory
        :param dest_dir: the destination directory
        :return:
        """

        with zipfile.ZipFile(source_filename) as zf:
            for member in zf.infolist():
                # Path traversal defense copied from
                # http://hg.python.org/cpython/file/tip/Lib/http/server.py#l789
                words = member.filename.split('/')
                path = path_expand(dest_dir)
                for word in words[:-1]:
                    drive, word = os.path.splitdrive(word)
                    head, word = os.path.split(word)
                    if word in (os.curdir, os.pardir, ''):
                        continue
                    path = os.path.join(path, word)
                zf.extract(member, path)
Exemple #32
0
def cloudmesh_launcher(request):
    if request.method == 'POST':
        print "HHHHHH", request.form.keys()
        for key in request.form.keys():
            print key, ":", request.form[key]
    else:
        print "HEY JUDE"

    launcher_config = ConfigDict(
        path_expand("~/.cloudmesh/cloudmesh_launcher.yaml"))

    context = {
        'recipies': launcher_config["cloudmesh.launcher.recipes"],
        'title': '<div><i class="fa fa-rocket"></i> Cloudmesh Launcher </div>'
    }

    pprint(context)

    return render(request, 'cloudmesh_portal/launcher/mesh_launch.jinja',
                  context)
Exemple #33
0
def cloudmesh_launcher_start(request):
    parameters = dict(request.POST)
    for key in parameters:
        try:
            parameters[key] = parameters[key][0]
        except:
            pass
    if 'csrfmiddlewaretoken' in parameters:
        del parameters['csrfmiddlewaretoken']

    response = 'error'
    if parameters["name"]:
        name = parameters["name"]

        launcher_config = ConfigDict(path_expand("~/.cloudmesh/cloudmesh_launcher.yaml"))
        recipe = dict(launcher_config["cloudmesh.launcher.recipes"])[name]

        print(json.dumps(recipe, indent=4))

        response = "error"

        if recipe["script"]["type"] in ["sh", "shell"]:
            script = recipe["script"]["value"].format(**parameters)
            print (script)
            launcher = Launcher("shell")
            print (type(launcher))
            response = launcher.run(script=script)
            parameters["script"] = script

    else:
        parameters = "error"

    context = {
        'title': '<div><i class="fa fa-rocket"></i> Cloudmesh Launcher</div>',
        "response": response,
        "parameters": parameters,
    }

    return render(request,
                  'cloudmesh_portal/launcher/mesh_launch_response.jinja',
                  context)
Exemple #34
0
    def check_file_for_tabs(cls, filename, verbose=True):
        """identifies if the file contains tabs and returns True if it
        does. It also prints the location of the lines and columns. If
        verbose is set to False, the location is not printed.
        :param filename: the filename
        :type filename: str
        :rtype: True if there are tabs in the file
        """
        filename = path_expand(filename)
        file_contains_tabs = False
        with file(filename) as f:
            lines = f.read().split("\n")

        line_no = 1
        for line in lines:
            if "\t" in line:
                file_contains_tabs = True
                location = [
                    i for i in range(len(line)) if line.startswith('\t', i)]
                if verbose:
                    print("Tab found in line", line_no, "and column(s)",
                          location)
            line_no += 1
        return file_contains_tabs
Exemple #35
0
    def check_file_for_tabs(cls, filename, verbose=True):
        """identifies if the file contains tabs and returns True if it
        does. It also prints the location of the lines and columns. If
        verbose is set to False, the location is not printed.
        :param filename: the filename
        :type filename: str
        :rtype: True if there are tabs in the file
        """
        filename = path_expand(filename)
        file_contains_tabs = False
        with file(filename) as f:
            lines = f.read().split("\n")

        line_no = 1
        for line in lines:
            if "\t" in line:
                file_contains_tabs = True
                location = [
                    i for i in range(len(line)) if line.startswith('\t', i)]
                if verbose:
                    print("Tab found in line", line_no, "and column(s)",
                          location)
            line_no += 1
        return file_contains_tabs
Exemple #36
0
from cloudmesh_client.util import path_expand
from cloudmesh_client.common.Shell import Shell
import os

__config_dir_prefix__ = os.path.join("~", ".cloudmesh")

__config_dir__ = path_expand(__config_dir_prefix__)


def config_file(filename):
    """
    The location of the config file: ~/.cloudmesh/filename. ~ will be expanded
    :param filename: the filename
    """
    return os.path.join(__config_dir__, filename)


def config_file_raw(filename):
    """
    The location of the config file: ~/.cloudmesh/filename. ~ will NOT be expanded
    :param filename: the filename
    """
    return os.path.join(__config_dir_prefix__, filename)


def config_file_prefix():
    """
    The prefix of the configuration file location
    """
    return __config_dir_prefix__
Exemple #37
0
from cloudmesh_client.common.StopWatch import StopWatch


def create_cloudmesh_yaml(filename):
    if not os.path.exists(filename):
        path = os.path.dirname(filename)
        if not os.path.isdir(path):
            Shell.mkdir(path)
        etc_path = os.path.dirname(cloudmesh_client.__file__)
        etc_file = os.path.join(etc_path, "etc", "cloudmesh.yaml")
        to_dir = path_expand("~/.cloudmesh")
        shutil.copy(etc_file, to_dir)
        Console.ok("~/.cloudmesh/cloudmesh.yaml created")


filename = path_expand("~/.cloudmesh/cloudmesh.yaml")
create_cloudmesh_yaml(filename)
os.system("chmod -R go-rwx " + path_expand("~/.cloudmesh"))


class CloudmeshContext(object):
    def __init__(self, **kwargs):
        self.__dict__ = kwargs


PluginCommandClasses = type(
    'CommandProxyClass',
    tuple(PluginCommand.__subclasses__()),
    {})

Exemple #38
0
def setup_yaml():
    filename = path_expand("~/.cloudmesh/cloudmesh.yaml")
    create_cloudmesh_yaml(filename)
Exemple #39
0
    def __init__(self, context):
        cmd.Cmd.__init__(self)
        self.variables = {}
        self.command_topics = {}
        self.register_topics()
        self.context = context
        # TODO get loglevel from DB or yaml file, if not defined set to ERROR
        self.loglevel = "DEBUG"
        self._hist = []
        if self.context.debug:
            print("init CloudmeshConsole")

        self.prompt = 'cm> '
        self.doc_header = "Documented commands (type help <command>):"
        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)

        # Initialize Logging
        # LogUtil.initialize_logging()

        # sys,exit(1)

        value = Default.get('cloud', category='general')
        if value is None:
            clouds = ConfigDict(filename=filename)["cloudmesh"]["clouds"]
            cloud = clouds.keys()[0]
            Default.set('cloud', cloud, category='general')

        value = Default.get('default', category='general')
        if value is None:
            Default.set('default', 'default', category='general')

        cluster = 'kilo'  # hardcode a value if not defined
        value = Default.get('cluster', category='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, category='general')

        group = Default.get_group()
        if group is None:
            Default.set_group("default")

        Default.load("cloudmesh.yaml")

        on = Default.timer()

        group = Default.get_group()
        if group is None:
            Default.set_group("default")

        r = Default.get_refresh()
        if r is None:
            Default.set_refresh("on")

        """
        try:
            sshm = SSHKeyManager()
            m = sshm.get_from_yaml(
                load_order="~/.cloudmesh/cloudmesh.yaml")
            d = dict(m.__keys__)


            sshdb = SSHKeyDBManager()

            for keyname in m.__keys__:
                filename = m[keyname]["path"]
                try:
                    sshdb.add(filename,
                              keyname,
                              source="yaml",
                              uri="file://" + filename)
                except Exception as e:
                    pass
        except Exception as e:
            Console.error("Problem adding keys from yaml file")
        """

        for c in CloudmeshConsole.__bases__[1:]:
            # noinspection PyArgumentList
            c.__init__(self, context)
Exemple #40
0
    def do_register(self, args, arguments):
        """
        ::

          Usage:
              register info
              register new
              register clean [--force]
              register list ssh [--format=FORMAT]
              register list [--yaml=FILENAME][--info][--format=FORMAT]
              register cat [--yaml=FILENAME]
              register edit [--yaml=FILENAME]
              register export HOST [--password] [--format=FORMAT]
              register source HOST
              register merge FILEPATH
              register form [--yaml=FILENAME]
              register check [--yaml=FILENAME]
              register test [--yaml=FILENAME]
              register json HOST
              register remote [CLOUD] [--force]
              register env [--provider=PROVIDER]
              register profile --username=[USERNAME]
              register yaml ENTRY
              register CLOUD [--force]
              register CLOUD [--dir=DIR]
              register ec2 CLOUD EC2ZIP

          managing the registered clouds in the cloudmesh.yaml file.
          It looks for it in the current directory, and than in
          ~/.cloudmesh.  If the file with the cloudmesh.yaml name is
          there it will use it.  If neither location has one a new
          file will be created in ~/.cloudmesh/cloudmesh.yaml. Some
          defaults will be provided.  However you will still need to
          fill it out with valid entries.

          Arguments:

            HOST   the host name
            USER   the user name
            FILEPATH the path of the file
            CLOUD the cloud name
            PROVIDER the provider or type of cloud [Default: openstack]
            USERNAME  Username that would be registered in yaml. Defaults to OS username.

          Options:

            --provider=PROVIDER     Provider to be used for cloud. Values are:
                                    openstack, azure, ec2.
            --version=VERSION       Version of the openstack cloud.
            --openrc=OPENRC         The location of the openrc file
            --password              Prints the password
            --force                 ignore interactive questions and execute
                                    the action

          Description:

              register info
                  It looks out for the cloudmesh.yaml file in the current
                  directory, and then in ~/.cloudmesh

              register list [--yaml=FILENAME] [--name] [--info]
                  lists the clouds specified in the cloudmesh.yaml file. If
                  info is specified it also prints the location of the yaml
                  file.

              register list ssh
                  lists hosts from ~/.ssh/config

              register cat [--yaml=FILENAME]
                  outputs the cloudmesh.yaml file

              register edit [--yaml=FILENAME]
                  edits the cloudmesh.yaml file

              register export HOST [--format=FORMAT]

                    prints the contents of an openrc.sh file based on the
                    information found in the cloudmesh.yaml file.

              register remote CLOUD [--force]

                    reads the Openstack OPENRC file from a remote host that
                    is described in cloudmesh.yaml file. We assume that
                    the file has already a template for this host. If
                    not it can be created from other examples before
                    you run this command.

                    It uses the OS_OPENRC variable to locate the file and
                    copy it onto your computer.

              register merge FILENAME
                  Replaces the TBD in cloudmesh.yaml with the contents
                  present in the named file

              register form [--yaml=FILENAME]
                  interactively fills out the form wherever we find TBD.

              register check [--yaml=FILENAME]
                  checks the yaml file for completness

              register test [--yaml=FILENAME]
                  checks the yaml file and executes tests to check if
                  we can use the cloud. TODO: maybe this should be in
                  a test command

              register json host
                  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 username [USERNAME]
                  Sets the username 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(attribute_printer(credentials))
            else:
                print(dict_printer(credentials, output=output))
                # TODO: bug csv does not work
            return ""

        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,
                                       info=False,
                                       output="table")
                print(d)

            else:
                Console.error("File {} does not exist".format(filename))

            return ""

        elif arguments["new"]:

            import shutil
            import cloudmesh_client.etc
            print(cloudmesh_client.etc.__file__)
            filename = os.path.join(
                os.path.dirname(cloudmesh_client.etc.__file__),
                "cloudmesh.yaml")
            Console.ok(filename)
            yamlfile = path_expand("~/.cloudmesh/cloudmesh.yaml")
            shutil.copyfile(filename, yamlfile)
            print("copy ")
            print("From: ", filename)
            print("To:   ", yamlfile)

            # filename = _get_config_yaml_file(arguments)
            # if _exists(filename):
            #    Console.ok("File '{}' exists. ok.".format(filename))
            # else:
            #    Console.error("File {} does not exist".format(filename))
            return ""

        elif arguments["clean"]:

            filename = _get_config_yaml_file(arguments)
            force = arguments["--force"] or False
            if filename is not None:
                print(filename, force)
                if exists(filename):
                    print("Delete cloudmesh.yaml file:", filename)
                    if not force:
                        force = yn_choice("Would you like to delete the "
                                          "cloudmesh.yaml file")
                        print(force)
                    if force:
                        os.remove(filename)
                        Console.ok("Deleted the file " + filename + ". ok.")
                    else:
                        Console.ok("Please use Y to delete the file.")
                    pass
                else:
                    Console.error("File {} does not exist".format(filename))
            else:
                Console.error("No cloudmesh.yaml file found.")
            return ""

        elif arguments["cat"]:

            filename = _get_config_yaml_file(arguments)
            if exists(filename):
                with open(filename, 'r') as f:
                    lines = f.read().split("\n")
                print('\n'.join(lines))
            else:
                Console.error("File {} does not exist".format(filename))
            return ""

        elif arguments["edit"]:

            filename = _get_config_yaml_file(arguments)
            if exists(filename):
                try:
                    data = {"editor": os.environ["EDITOR"],
                            "filename": filename}
                    Console.ok("editing file " + filename)
                    os.system("{editor} {filename}".format(**data))
                except:
                    Console.error("No EDITOR variable set in shell.")
            else:
                Console.error("File {} does not exist".format(filename))
            return ""

        elif arguments['list'] and arguments['ssh']:
            output = arguments['--format'] or 'table'
            hosts = CloudRegister.list_ssh()
            print(print_list(hosts, output=output))
            return ""

        elif arguments['list']:

            filename = _get_config_yaml_file(arguments)
            info = arguments["--info"] or False
            output = arguments["--format"] or "table"

            if not filename:
                Console.error("File {} doesn't exist".format(filename))
            else:
                d = CloudRegister.list(filename,
                                       info=info,
                                       output=output)
                print(d)
            return ""

        elif arguments['check']:
            filename = _get_config_yaml_file(arguments)
            if not filename:
                Console.error("File {} doesn't exist".format(
                    arguments["--yaml"] or 'cloudmesh.yaml'))
            else:
                CloudRegister.check_yaml_for_completeness(filename)
            return ""

        elif arguments['merge']:
            filename = arguments['FILENAME']
            CloudRegister.from_file(filename)
            return

        elif arguments['test']:
            filename = _get_config_yaml_file(arguments)
            CloudRegister.test(filename)
            return ""

        elif arguments['form']:
            filename = _get_config_yaml_file(arguments)
            if not filename:
                Console.error("File {} doesn't exist".format(
                    arguments["--yaml"] or 'cloudmesh.yaml'))
            else:
                CloudRegister.fill_out_form(filename)
            return ""

        elif arguments['source']:

            host = arguments['HOST']
            config = ConfigDict("cloudmesh.yaml")
            credentials = dict(
                config["cloudmesh"]["clouds"][host]["credentials"])

            # unset

            variables = list(os.environ)
            for attribute in variables:
                if attribute.startswith("OS_"):
                    print("x ", attribute)
                    del os.environ[attribute]

            # set
            for attribute, value in credentials.items():
                os.putenv(attribute, value)
                print("+ ", attribute)
            export(host, "table")

            return ""

        elif arguments['export']:

            output = arguments['--format']
            host = arguments['HOST']

            variables = list(os.environ)
            for attribute in variables:
                if attribute.startswith("OS_"):
                    print("unset ", attribute)
                    del os.environ[attribute]
            export(host, output)

        elif arguments['json']:
            host = arguments['HOST']
            result = CloudRegister.get(host)
            if result:
                print(json.dumps(result, indent=4))
            else:
                print("Cloud {:} is not described in cloudmesh.yaml".format(
                    host))
            return ""

        elif arguments['remote']:

            force = arguments['--force']
            cloud = arguments['CLOUD']

            if cloud is None:
                clouds = ["kilo"]
            else:
                clouds = [cloud]

            for cloud in clouds:
                CloudRegister.remote(cloud, force)
                export(cloud, "table")

            config = ConfigDict("cloudmesh.yaml")
            if config["cloudmesh.profile.username"] == "TBD":
                name = config["cloudmesh.clouds.kilo.credentials.OS_USERNAME"]
                config["cloudmesh"]["profile"]["username"] = name
                config.save()
            else:
                print("KKK")
            return ""

        elif arguments['ec2']:

            cloud = arguments['CLOUD']
            zipfile = arguments['EC2ZIP']

            if cloud is None:
                clouds = ["kilo"]
            else:
                clouds = [cloud]

            for cloud in clouds:
                CloudRegister.ec2(cloud, zipfile)
                export(cloud, "table")

        elif arguments['env']:
            try:
                CloudRegister.from_environ(arguments['--provider'])
            except Exception as e:
                Error.traceback(e)
            return ""

        elif arguments['CLOUD']:
            if arguments['--dir']:
                cloud = arguments['CLOUD']
                directory = arguments['--dir']
                Console.ok(directory)
                CloudRegister.directory(cloud, directory)
            else:
                cloud = arguments['CLOUD']

                if cloud is None:
                    clouds = ["kilo"]
                else:
                    clouds = [cloud]

                for cloud in clouds:
                    CloudRegister.remote(cloud, True)
                    export(cloud, "table")
            return ""

        elif arguments['profile']:
            username = arguments["--username"] or getpass.getuser()
            CloudRegister.set_username(username)
            Console.ok("Username {} set successfully in the yaml settings.".format(username))
            return ""

        elif arguments['yaml']:
            name = arguments['ENTRY']
            Register.entry(name)
            return ""
        # if all fails do a simple list

        filename = _get_config_yaml_file(arguments)
        CloudRegister.list(filename)

        pass
Exemple #41
0
os_password = d["cloudmesh.clouds.chameleon.credentials.OS_PASSWORD"]
os_username = d["cloudmesh.clouds.chameleon.credentials.OS_USERNAME"]

print os_username, os_password

driver = webdriver.Firefox()

from pprint import pprint

pprint (driver.profile.__dict__)

profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", path_expand('~/.cloudmesh/'))
driver = webdriver.Firefox(firefox_profile=profile)

driver.get("https://openstack.tacc.chameleoncloud.org/dashboard/project/")
username = driver.find_element_by_name("username")
username.send_keys(os_username)

username = driver.find_element_by_name("password")
username.send_keys(os_password)

driver.find_element_by_id("loginBtn").click()

driver.get("https://openstack.tacc.chameleoncloud.org/dashboard/project/access_and_security/api_access/openrc/")


element = webdriver.WebDriverWait(driver, 10)
Exemple #42
0
 def diagram(self, name):
     with open('{}.diag'.format(path_expand(name)), 'w') as f:
         f.write(self.diag)
Exemple #43
0
    def __init__(self, context):
        cmd.Cmd.__init__(self)
        self.variables = {}
        self.command_topics = {}
        self.register_topics()
        self.context = context
        # TODO get loglevel from DB or yaml file, if not defined set to ERROR
        self.loglevel = "DEBUG"
        self._hist = []
        if self.context.debug:
            print("init CloudmeshConsole")

        self.prompt = 'cm> '
        self.doc_header = "Documented commands (type help <command>):"
        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")
        # moved to import cloudmesh_client

        # create_cloudmesh_yaml(filename)

        # Initialize Logging
        # LogUtil.initialize_logging()

        # sys,exit(1)

        value = Default.get('cloud', category='general')
        if value is None:
            clouds = ConfigDict(filename=filename)["cloudmesh"]["clouds"]
            cloud = list(clouds.keys())[0]
            Default.set('cloud', cloud, category='general')

        value = Default.get('default', category='general')
        if value is None:
            Default.set('default', 'default', category='general')

        cluster = 'kilo'  # hardcode a value if not defined
        value = Default.get('cluster', category='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, category='general')

        group = Default.get_group()
        if group is None:
            Default.set_group("default")

        Default.load("cloudmesh.yaml")

        on = Default.timer()

        group = Default.get_group()
        if group is None:
            Default.set_group("default")

        r = Default.get_refresh()
        if r is None:
            Default.set_refresh("on")
        """
        try:
            sshm = SSHKeyManager()
            m = sshm.get_from_yaml(
                load_order="~/.cloudmesh/cloudmesh.yaml")
            d = dict(m.__keys__)


            sshdb = SSHKeyDBManager()

            for keyname in m.__keys__:
                filename = m[keyname]["path"]
                try:
                    sshdb.add(filename,
                              keyname,
                              source="yaml",
                              uri="file://" + filename)
                except Exception as e:
                    pass
        except Exception as e:
            Console.error("Problem adding keys from yaml file")
        """

        for c in CloudmeshConsole.__bases__[1:]:
            # noinspection PyArgumentList
            c.__init__(self, context)
Exemple #44
0
 def view(self, name):
     cmd = ['open', "{}.svg".format(path_expand(name))]
     subprocess.Popen(cmd)
Exemple #45
0
os_password = d["cloudmesh.clouds.chameleon.credentials.OS_PASSWORD"]
os_username = d["cloudmesh.clouds.chameleon.credentials.OS_USERNAME"]

print os_username, os_password

driver = webdriver.Firefox()

from pprint import pprint

pprint(driver.profile.__dict__)

profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", path_expand('~/.cloudmesh/'))
driver = webdriver.Firefox(firefox_profile=profile)

driver.get("https://openstack.tacc.chameleoncloud.org/dashboard/project/")
username = driver.find_element_by_name("username")
username.send_keys(os_username)

username = driver.find_element_by_name("password")
username.send_keys(os_password)

driver.find_element_by_id("loginBtn").click()

driver.get(
    "https://openstack.tacc.chameleoncloud.org/dashboard/project/access_and_security/api_access/openrc/"
)
Exemple #46
0
from flask import Flask
from flask.ext.sandboy import Sandboy

from flask.ext.sqlalchemy import SQLAlchemy

from cloudmesh_client.db.model import DEFAULT, GROUP, KEY, RESERVATION

app = Flask(__name__)

from cloudmesh_client.util import path_expand
filename = "sqlite:///{}".format(path_expand("~/.cloudmesh/cloudmesh.db"))

print ("FFF", filename)
app.config['SQLALCHEMY_DATABASE_URI'] = filename



db = SQLAlchemy()
db.init_app(app)
with app.app_context():
    db.create_all()
sandboy = Sandboy(app, db, [DEFAULT, GROUP])
app.run(debug=True)
Exemple #47
0
from flask import Flask
from flask.ext.sandboy import Sandboy

from flask.ext.sqlalchemy import SQLAlchemy

from cloudmesh_client.db.model import DEFAULT, GROUP, KEY, RESERVATION

app = Flask(__name__)

from cloudmesh_client.util import path_expand
filename = "sqlite:///{}".format(path_expand("~/.cloudmesh/cloudmesh.db"))

print("FFF", filename)
app.config['SQLALCHEMY_DATABASE_URI'] = filename

db = SQLAlchemy()
db.init_app(app)
with app.app_context():
    db.create_all()
sandboy = Sandboy(app, db, [DEFAULT, GROUP])
app.run(debug=True)
Exemple #48
0
    def do_register(self, args, arguments):
        """
        ::

          Usage:
              register info
              register new
              register clean [--force]
              register list ssh [--format=FORMAT]
              register list [--yaml=FILENAME][--info][--format=FORMAT]
              register cat [--yaml=FILENAME]
              register edit [--yaml=FILENAME]
              register export HOST [--password] [--format=FORMAT]
              register source HOST
              register merge FILEPATH
              register form [--yaml=FILENAME]
              register check [--yaml=FILENAME]
              register test [--yaml=FILENAME]
              register json HOST
              register remote [CLOUD] [--force]
              register env [--provider=PROVIDER]
              register profile --username=[USERNAME]
              register yaml ENTRY
              register CLOUD [--force]
              register CLOUD [--dir=DIR]
              register ec2 CLOUD EC2ZIP

          managing the registered clouds in the cloudmesh.yaml file.
          It looks for it in the current directory, and than in
          ~/.cloudmesh.  If the file with the cloudmesh.yaml name is
          there it will use it.  If neither location has one a new
          file will be created in ~/.cloudmesh/cloudmesh.yaml. Some
          defaults will be provided.  However you will still need to
          fill it out with valid entries.

          Arguments:

            HOST   the host name
            USER   the user name
            FILEPATH the path of the file
            CLOUD the cloud name
            PROVIDER the provider or type of cloud [Default: openstack]
            USERNAME  Username that would be registered in yaml. Defaults to OS username.

          Options:

            --provider=PROVIDER     Provider to be used for cloud. Values are:
                                    openstack, azure, ec2.
            --version=VERSION       Version of the openstack cloud.
            --openrc=OPENRC         The location of the openrc file
            --password              Prints the password
            --force                 ignore interactive questions and execute
                                    the action

          Description:

              register info
                  It looks out for the cloudmesh.yaml file in the current
                  directory, and then in ~/.cloudmesh

              register list [--yaml=FILENAME] [--name] [--info]
                  lists the clouds specified in the cloudmesh.yaml file. If
                  info is specified it also prints the location of the yaml
                  file.

              register list ssh
                  lists hosts from ~/.ssh/config

              register cat [--yaml=FILENAME]
                  outputs the cloudmesh.yaml file

              register edit [--yaml=FILENAME]
                  edits the cloudmesh.yaml file

              register export HOST [--format=FORMAT]

                    prints the contents of an openrc.sh file based on the
                    information found in the cloudmesh.yaml file.

              register remote CLOUD [--force]

                    reads the Openstack OPENRC file from a remote host that
                    is described in cloudmesh.yaml file. We assume that
                    the file has already a template for this host. If
                    not it can be created from other examples before
                    you run this command.

                    It uses the OS_OPENRC variable to locate the file and
                    copy it onto your computer.

              register merge FILENAME
                  Replaces the TBD in cloudmesh.yaml with the contents
                  present in the named file

              register form [--yaml=FILENAME]
                  interactively fills out the form wherever we find TBD.

              register check [--yaml=FILENAME]
                  checks the yaml file for completness

              register test [--yaml=FILENAME]
                  checks the yaml file and executes tests to check if
                  we can use the cloud. TODO: maybe this should be in
                  a test command

              register json host
                  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 username [USERNAME]
                  Sets the username 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(attribute_printer(credentials))
            else:
                print(dict_printer(credentials, output=output))
                # TODO: bug csv does not work
            return ""

        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, info=False, output="table")
                print(d)

            else:
                Console.error("File {} does not exist".format(filename))

            return ""

        elif arguments["new"]:

            import shutil
            import cloudmesh_client.etc
            print(cloudmesh_client.etc.__file__)
            filename = os.path.join(
                os.path.dirname(cloudmesh_client.etc.__file__),
                "cloudmesh.yaml")
            Console.ok(filename)
            yamlfile = path_expand("~/.cloudmesh/cloudmesh.yaml")
            shutil.copyfile(filename, yamlfile)
            print("copy ")
            print("From: ", filename)
            print("To:   ", yamlfile)

            # filename = _get_config_yaml_file(arguments)
            # if _exists(filename):
            #    Console.ok("File '{}' exists. ok.".format(filename))
            # else:
            #    Console.error("File {} does not exist".format(filename))
            return ""

        elif arguments["clean"]:

            filename = _get_config_yaml_file(arguments)
            force = arguments["--force"] or False
            if filename is not None:
                print(filename, force)
                if exists(filename):
                    print("Delete cloudmesh.yaml file:", filename)
                    if not force:
                        force = yn_choice("Would you like to delete the "
                                          "cloudmesh.yaml file")
                        print(force)
                    if force:
                        os.remove(filename)
                        Console.ok("Deleted the file " + filename + ". ok.")
                    else:
                        Console.ok("Please use Y to delete the file.")
                    pass
                else:
                    Console.error("File {} does not exist".format(filename))
            else:
                Console.error("No cloudmesh.yaml file found.")
            return ""

        elif arguments["cat"]:

            filename = _get_config_yaml_file(arguments)
            if exists(filename):
                with open(filename, 'r') as f:
                    lines = f.read().split("\n")
                print('\n'.join(lines))
            else:
                Console.error("File {} does not exist".format(filename))
            return ""

        elif arguments["edit"]:

            filename = _get_config_yaml_file(arguments)
            if exists(filename):
                try:
                    data = {
                        "editor": os.environ["EDITOR"],
                        "filename": filename
                    }
                    Console.ok("editing file " + filename)
                    os.system("{editor} {filename}".format(**data))
                except:
                    Console.error("No EDITOR variable set in shell.")
            else:
                Console.error("File {} does not exist".format(filename))
            return ""

        elif arguments['list'] and arguments['ssh']:
            output = arguments['--format'] or 'table'
            hosts = CloudRegister.list_ssh()
            print(print_list(hosts, output=output))
            return ""

        elif arguments['list']:

            filename = _get_config_yaml_file(arguments)
            info = arguments["--info"] or False
            output = arguments["--format"] or "table"

            if not filename:
                Console.error("File {} doesn't exist".format(filename))
            else:
                d = CloudRegister.list(filename, info=info, output=output)
                print(d)
            return ""

        elif arguments['check']:
            filename = _get_config_yaml_file(arguments)
            if not filename:
                Console.error("File {} doesn't exist".format(
                    arguments["--yaml"] or 'cloudmesh.yaml'))
            else:
                CloudRegister.check_yaml_for_completeness(filename)
            return ""

        elif arguments['merge']:
            filename = arguments['FILENAME']
            CloudRegister.from_file(filename)
            return

        elif arguments['test']:
            filename = _get_config_yaml_file(arguments)
            CloudRegister.test(filename)
            return ""

        elif arguments['form']:
            filename = _get_config_yaml_file(arguments)
            if not filename:
                Console.error("File {} doesn't exist".format(
                    arguments["--yaml"] or 'cloudmesh.yaml'))
            else:
                CloudRegister.fill_out_form(filename)
            return ""

        elif arguments['source']:

            host = arguments['HOST']
            config = ConfigDict("cloudmesh.yaml")
            credentials = dict(
                config["cloudmesh"]["clouds"][host]["credentials"])

            # unset

            variables = list(os.environ)
            for attribute in variables:
                if attribute.startswith("OS_"):
                    print("x ", attribute)
                    del os.environ[attribute]

            # set
            for attribute, value in credentials.items():
                os.putenv(attribute, value)
                print("+ ", attribute)
            export(host, "table")

            return ""

        elif arguments['export']:

            output = arguments['--format']
            host = arguments['HOST']

            variables = list(os.environ)
            for attribute in variables:
                if attribute.startswith("OS_"):
                    print("unset ", attribute)
                    del os.environ[attribute]
            export(host, output)

        elif arguments['json']:
            host = arguments['HOST']
            result = CloudRegister.get(host)
            if result:
                print(json.dumps(result, indent=4))
            else:
                print("Cloud {:} is not described in cloudmesh.yaml".format(
                    host))
            return ""

        elif arguments['remote']:

            force = arguments['--force']
            cloud = arguments['CLOUD']

            if cloud is None:
                clouds = ["kilo"]
            else:
                clouds = [cloud]

            for cloud in clouds:
                CloudRegister.remote(cloud, force)
                export(cloud, "table")

            config = ConfigDict("cloudmesh.yaml")
            if config["cloudmesh.profile.username"] == "TBD":
                name = config["cloudmesh.clouds.kilo.credentials.OS_USERNAME"]
                config["cloudmesh"]["profile"]["username"] = name
                config.save()
            else:
                print("KKK")
            return ""

        elif arguments['ec2']:

            cloud = arguments['CLOUD']
            zipfile = arguments['EC2ZIP']

            if cloud is None:
                clouds = ["kilo"]
            else:
                clouds = [cloud]

            for cloud in clouds:
                CloudRegister.ec2(cloud, zipfile)
                export(cloud, "table")

        elif arguments['env']:
            try:
                CloudRegister.from_environ(arguments['--provider'])
            except Exception as e:
                Error.traceback(e)
            return ""

        elif arguments['CLOUD']:
            if arguments['--dir']:
                cloud = arguments['CLOUD']
                directory = arguments['--dir']
                Console.ok(directory)
                CloudRegister.directory(cloud, directory)
            else:
                cloud = arguments['CLOUD']

                if cloud is None:
                    clouds = ["kilo"]
                else:
                    clouds = [cloud]

                for cloud in clouds:
                    CloudRegister.remote(cloud, True)
                    export(cloud, "table")
            return ""

        elif arguments['profile']:
            username = arguments["--username"] or getpass.getuser()
            CloudRegister.set_username(username)
            Console.ok(
                "Username {} set successfully in the yaml settings.".format(
                    username))
            return ""

        elif arguments['yaml']:
            name = arguments['ENTRY']
            Register.entry(name)
            return ""
        # if all fails do a simple list

        filename = _get_config_yaml_file(arguments)
        CloudRegister.list(filename)

        pass
Exemple #49
0
def setup_yaml():
    filename = path_expand("~/.cloudmesh/cloudmesh.yaml")
    create_cloudmesh_yaml(filename)
Exemple #50
0
 def svg(self, name):
     self.diagram(name)
     cmd = ['rackdiag', "-T", "svg", "{}.diag".format(path_expand(name))]
     subprocess.Popen(cmd).wait()