Exemplo n.º 1
0
    def define(self, clustername=None, **kwargs):
        """Define a cluster.

            kwargs are passed to Cluster

            :returns: a cluster
            :rtype: :class:`Cluster`
            """

        clustername = clustername or Default.generate_name(
            Names.CLUSTER_COUNTER)

        # remove None to defer default definitions to later
        for k in kwargs.keys():
            if kwargs[k] is None:
                del kwargs[k]

        try:
            spec = db.select(SPECIFICATION, name=clustername,
                             type='cluster')[0]
            spec.update(kwargs)
            db.updateObj(spec)
        except IndexError:
            spec = SPECIFICATION(clustername, 'cluster', kwargs)
            db.insert(spec)

        Default.set_specification(clustername)
        Console.ok('Defined cluster {}'.format(clustername))
Exemplo n.º 2
0
 def stop(cls, **kwargs):
     arg = dotdict(kwargs)
     cloud_provider = CloudProvider(arg.cloud).provider
     for server in kwargs["servers"]:
         cloud_provider.stop_vm(server)
         Console.ok("Machine {:} is being stopped on {:} Cloud...".format(
             server, cloud_provider.cloud))
Exemplo n.º 3
0
    def allocate(self, clustername=None):

        specname = clustername or Default.active_specification

        try:
            spec = db.select(SPECIFICATION, name=specname)[0]
        except IndexError:
            Console.error(
                'No specification with name={} found'.format(specname))
            return 1

        defns = spec.get()

        try:
            cluster = db.select(Cluster, name=spec.name, specId=spec.cm_id)[0]
        except IndexError:
            cluster = Cluster(name=spec.name, specId=spec.cm_id, **defns)

        Default.set_cluster(cluster.name)
        Console.ok('Cluster {} is now active'.format(cluster.name))

        cluster.create()
        Console.ok('Cluster {} created'.format(cluster.name))

        return cluster
Exemplo n.º 4
0
    def delete_all_rules(cls, secgroup):
        try:
            """
            rules = cls.cm_db.query(model.SECGROUPRULE).filter(
                model.SECGROUPRULE.groupid == secgroup.uuid
            ).all()
            """

            args = {
                "groupid": secgroup.uuid
            }
            rules = cls.cm_db.find("secgrouprule", output="object", **args)

            if rules is not None:
                for rule in rules:
                    cls.cm_db.delete(rule)
                    Console.ok("Rule [{} | {} | {} | {}] deleted"
                               .format(rule.fromPort, rule.toPort,
                                       rule.protocol, rule.cidr))
            else:
                pass
        except Exception as ex:
            Console.error(ex.message, ex)

        return
Exemplo n.º 5
0
    def do_banner(self, args, arguments):
        """
        ::

            Usage:
                banner [-c CHAR] [-n WIDTH] [-i INDENT] [-r COLOR] TEXT...

            Arguments:
                TEXT...   The text message from which to create the banner
                CHAR   The character for the frame.
                WIDTH  Width of the banner
                INDENT indentation of the banner
                COLOR  the color

            Options:
                -c CHAR   The character for the frame. [default: #]
                -n WIDTH  The width of the banner. [default: 70]
                -i INDENT  The width of the banner. [default: 0]
                -r COLOR  The color of the banner. [default: BLACK]

            Prints a banner form a one line text message.
        """
        Console.ok("banner")
        n = int(arguments['-n'])
        c = arguments['-c']
        i = int(arguments['-i'])
        color = arguments['-r'].upper()

        line = ' '.join(arguments['TEXT'])
        Console.cprint(color, "", i * " " + str((n - i) * c))
        Console.cprint(color, "", i * " " + c + " " + line)
        Console.cprint(color, "", i * " " + str((n - i) * c))

        return ""
Exemplo n.º 6
0
 def generate(self,
              key="india",
              host="india.futuresystems.org",
              username=None,
              force=False,
              verbose=False):
     data = {
         "host": host,
         "key": key,
         "username": username
     }
     if verbose and key in self.names():
         Console.error("{key} already in ~/.ssh/config".format(**data), traceflag=False)
         return ""
     else:
         entry = dedent("""
         Host {key}
             Hostname {host}
             User {username}
         """.format(**data))
     try:
         with open(self.filename, "a") as config_ssh:
             config_ssh.write(entry)
         config_ssh.close()
         self.load()
         if verbose:
             Console.ok("Added india to ~/.ssh/config")
     except Exception as e:
         if verbose:
             Console.error(e.message)
Exemplo n.º 7
0
    def undefine(self, specname=None, all=False):

        specs = set()

        if all:
            for spec in db.select(SPECIFICATION, type='cluster'):
                specs.add(spec)

        try:
            spec = db.select(SPECIFICATION,
                             type='cluster',
                             name=specname or Default.active_specification)[0]
            specs.add(spec)
        except IndexError:
            pass

        for spec in specs:

            try:
                cluster = db.select(Cluster, specId=spec.cm_id)[0]
                Console.warning('Cannot undefine allocated cluster {}.'.format(
                    cluster.name))
                Console.warning('Please delete the cluster first')
                continue
            except IndexError:
                pass

            db.delete_(SPECIFICATION, cm_id=spec.cm_id)
            Console.ok('Undefined specification {}'.format(spec.name))

        try:
            spec = db.select(SPECIFICATION, type='cluster')[0]
            Default.set_specification(spec.name)
        except IndexError:
            pass
Exemplo n.º 8
0
    def check_yaml_for_completeness(cls, filename):
        """
        outputs how many values has to be fixed in cloudmesh.yaml file

        :param filename: the file name
        :type filename: string
        :return:
        """
        if filename is None:
            filename = "cloudmesh.yaml"

        config = ConfigDict(filename)

        content = config.yaml

        Console.ok("Checking the yaml file")
        count = 0
        output = []
        for line in content.split("\n"):
            if "TBD" in line:
                output.append(textwrap.dedent(line))
                count += 1
        if count > 0:
            Console.error("The file has {:} values to be fixed".format(count))
            print("")
            for line in output:
                Console.error("  " + line, prefix=False)
Exemplo n.º 9
0
    def list(cls, filename, info=False, output='table'):
        """
        lists clouds from cloudmesh.yaml file

        :param filename:
        :type filename: string
        :return:
        """
        config = ConfigDict("cloudmesh.yaml")
        clouds = config["cloudmesh"]["clouds"]
        if info:
            Console.ok("Cloudmesh configuration file: {}".format(filename))
            print("")
        d = {}
        for i, key in enumerate(clouds.keys()):
            d[i] = {
                "id":
                i,
                "cloud":
                key,
                "iaas":
                config["cloudmesh"]["clouds"][key]["cm_type"],
                "version":
                config["cloudmesh"]["clouds"][key]["cm_type_version"] or "N/A"
            }
        return Printer.dict_printer(d,
                                    order=['id', 'cloud', 'iaas', 'version'],
                                    output=output)
Exemplo n.º 10
0
    def get_apikey(endpoint):
        config = ConfigDict("cloudmesh.yaml")
        cometConf = config["cloudmesh.comet"]
        defaultUser = cometConf["username"]
        user = input("Comet Nucleus Usename [%s]: " \
                         % defaultUser)
        if not user:
            user = defaultUser
        password = getpass.getpass()
        keyurl = "%s/getkey" % cometConf["endpoints"][endpoint]["nucleus_base_url"]
        headers = {"ACCEPT": "application/json"}
        r = requests.get(keyurl, headers=headers, auth=HTTPBasicAuth(user, password))
        if r.status_code == 200:
            keyobj = r.json()
            api_key = keyobj["key_name"]
            api_secret = keyobj["key"]
            config = ConfigDict("cloudmesh.yaml")
            config.data["cloudmesh"]["comet"]["endpoints"]\
                        [endpoint]["auth_provider"] = 'apikey'
            config.data["cloudmesh"]["comet"]["endpoints"]\
                        [endpoint]["apikey"]["api_key"] = api_key
            config.data["cloudmesh"]["comet"]["endpoints"]\
                        [endpoint]["apikey"]["api_secret"] = api_secret

            config.save()
            Console.ok("api key retrieval and set was successful!")
        else:
            Console.error("Error getting api key. " \
                          "Please check your username/password", traceflag=False)
Exemplo n.º 11
0
 def kill_tunnel():
     pid = Comet.find_tunnel()
     if pid is None:
         Console.error("No tunnel to comet found")
     else:
         Console.ok("Killing the tunnel to comet")
         os.kill(pid, signal.SIGTERM)
Exemplo n.º 12
0
 def kill_tunnel():
     pid = Comet.find_tunnel()
     if pid is None:
         Console.error("No tunnel to comet found")
     else:
         Console.ok("Killing the tunnel to comet")
         os.kill(pid, signal.SIGTERM)
Exemplo n.º 13
0
    def do_exec(self, filename):
        """
        ::

            Usage:
               exec FILENAME

            executes the commands in the file. See also the script command.

            Arguments:
              FILENAME   The name of the file
        """
        if not filename:
            Console.error("the command requires a filename as parameter")
            return

        if os.path.exists(filename):
            with open(filename, "r") as f:
                for line in f:
                    if self.context.echo:
                        Console.ok("cm> {:}".format(str(line)))
                    self.precmd(line)
                    stop = self.onecmd(line)
                    self.postcmd(stop, line)
        else:
            Console.error('file "{:}" does not exist.'.format(filename))
            sys.exit()
Exemplo n.º 14
0
    def get_apikey(endpoint):
        config = ConfigDict("cloudmesh.yaml")
        cometConf = config["cloudmesh.comet"]
        defaultUser = cometConf["username"]

        user = input("Comet nucleus username [%s]: " \
                     % defaultUser)
        if not user:
            user = defaultUser
        password = getpass.getpass()
        keyurl = "%s/getkey" % cometConf["endpoints"][endpoint]["nucleus_base_url"]
        headers = {"ACCEPT": "application/json"}
        r = requests.get(keyurl, headers=headers, auth=HTTPBasicAuth(user, password))
        if r.status_code == 200:
            keyobj = r.json()
            api_key = keyobj["key_name"]
            api_secret = keyobj["key"]
            config = ConfigDict("cloudmesh.yaml")
            config.data["cloudmesh"]["comet"]["endpoints"] \
                [endpoint]["auth_provider"] = 'apikey'
            config.data["cloudmesh"]["comet"]["endpoints"] \
                [endpoint]["apikey"]["api_key"] = api_key
            config.data["cloudmesh"]["comet"]["endpoints"] \
                [endpoint]["apikey"]["api_secret"] = api_secret

            config.save()
            Console.ok("api key retrieval and set was successful!")
        else:
            Console.error("Error getting api key. "
                          "Please check your username/password", traceflag=False)
Exemplo n.º 15
0
    def undefine(self, specname=None, all=False):

        specs = set()

        if all:
            for spec in db.select(SPECIFICATION, type='cluster'):
                specs.add(spec)


        try:
            spec = db.select(SPECIFICATION, type='cluster', name=specname or Default.active_specification)[0]
            specs.add(spec)
        except IndexError:
            pass

        for spec in specs:

            try:
                cluster = db.select(Cluster, specId=spec.cm_id)[0]
                Console.warning('Cannot undefine allocated cluster {}.'.format(cluster.name))
                Console.warning('Please delete the cluster first')
                continue
            except IndexError:
                pass

            db.delete_(SPECIFICATION, cm_id = spec.cm_id)
            Console.ok('Undefined specification {}'.format(spec.name))

        try:
            spec = db.select(SPECIFICATION, type='cluster')[0]
            Default.set_specification(spec.name)
        except IndexError:
            pass
Exemplo n.º 16
0
 def generate(self,
              key="india",
              host="india.futuresystems.org",
              username=None,
              force=False,
              verbose=False):
     data = {"host": host, "key": key, "username": username}
     if verbose and key in self.names():
         Console.error("{key} already in ~/.ssh/config".format(**data),
                       traceflag=False)
         return ""
     else:
         entry = dedent("""
         Host {key}
             Hostname {host}
             User {username}
         """.format(**data))
     try:
         with open(self.filename, "a") as config_ssh:
             config_ssh.write(entry)
         config_ssh.close()
         self.load()
         if verbose:
             Console.ok("Added india to ~/.ssh/config")
     except Exception as e:
         if verbose:
             Console.error(e.message)
Exemplo n.º 17
0
 def stop(cls, **kwargs):
     arg = dotdict(kwargs)
     cloud_provider = CloudProvider(arg.cloud).provider
     for server in kwargs["servers"]:
         cloud_provider.stop_vm(server)
         Console.ok("Machine {:} is being stopped on {:} Cloud..."
                    .format(server, cloud_provider.cloud))
Exemplo n.º 18
0
    def do_exec(self, filename):
        """
        ::

            Usage:
               exec FILENAME

            executes the commands in the file. See also the script command.

            Arguments:
              FILENAME   The name of the file
        """
        if not filename:
            Console.error("the command requires a filename as parameter")
            return

        if os.path.exists(filename):
            with open(filename, "r") as f:
                for line in f:
                    if self.context.echo:
                        Console.ok("cm> {:}".format(str(line)))
                    self.precmd(line)
                    stop = self.onecmd(line)
                    self.postcmd(stop, line)
        else:
            Console.error('file "{:}" does not exist.'.format(filename))
            sys.exit()
Exemplo n.º 19
0
    def list(cls, filename, info=False, output='table'):
        """
        lists clouds from cloudmesh.yaml file

        :param filename:
        :type filename: string
        :return:
        """
        config = ConfigDict("cloudmesh.yaml")
        clouds = config["cloudmesh"]["clouds"]
        if info:
            Console.ok("Cloudmesh configuration file: {}".format(filename))
            print("")
        d = {}
        for i, key in enumerate(clouds.keys()):
            d[i] = {
                "id": i,
                "cloud": key,
                "iaas": config["cloudmesh"]["clouds"][key]["cm_type"],
                "version":
                    config["cloudmesh"]["clouds"][key][
                        "cm_type_version"] or "N/A"
            }
        return Printer.dict_printer(d,
                                    order=['id',
                                           'cloud',
                                           'iaas',
                                           'version'],
                                    output=output)
Exemplo n.º 20
0
    def list(cls, filename, cloud, info=False, output='table'):
        """
        lists clouds from cloudmesh.yaml file

        :param filename:
        :type filename: string
        :return:
        """
        config = ConfigDict("cloudmesh.yaml")
        clouds = config["cloudmesh"]["clouds"]
        if info:
            Console.ok("Cloudmesh configuration file: {}".format(filename))
            print("")
        d = {}
        for i, key in enumerate(clouds.keys()):
            d[i] = {
                "id": i,
                "cloud": key,
                "iaas": config["cloudmesh"]["clouds"][key]["cm_type"],
                "version":
                    config["cloudmesh"]["clouds"][key]["cm_type_version"] or "",
                # "active": "*" if key in config["cloudmesh"]["active"] else "",
                "active": config["cloudmesh"]["active"].index(key) + 1 if key in config["cloudmesh"]["active"] else "",
                "default": "*" if key == cloud else ""
            }
        return Printer.Printer.write(d,
                                     order=['id',
                                            'default',
                                            'cloud',
                                            'iaas',
                                            'version',
                                            'active'],
                                     output=output)
Exemplo n.º 21
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 ""
Exemplo n.º 22
0
    def check_yaml_for_completeness(cls, filename):
        """
        outputs how many values has to be fixed in cloudmesh.yaml file

        :param filename: the file name
        :type filename: string
        :return:
        """
        if filename is None:
            filename = "cloudmesh.yaml"

        config = ConfigDict(filename)

        content = config.yaml

        Console.ok("Checking the yaml file")
        count = 0
        output = []
        for line in content.split("\n"):
            if "TBD" in line:
                output.append(textwrap.dedent(line))
                count += 1
        if count > 0:
            Console.error("The file has {:} values to be fixed".format(count))
            print("")
            for line in output:
                Console.error("  " + line, prefix=False)
Exemplo n.º 23
0
    def do_open(self, args, arguments):
        """
        ::

            Usage:
                    open FILENAME

            ARGUMENTS:
                FILENAME  the file to open in the cwd if . is
                          specified. If file in in cwd
                          you must specify it with ./FILENAME

            Opens the given URL in a browser window.
        """
        filename = arguments['FILENAME']
        filename = self._expand_filename(filename)
        Console.ok("open {0}".format(filename))

        if not (filename.startswith("file:") or filename.startswith("http:")):
            try:
                with open(filename):
                    pass
                filename += "file://"
            except:
                Console.error(
                    "unsupported browser format in file {0}".format(filename))
                return ""

        try:
            webbrowser.open("%s" % filename)
        except:
            Console.error(
                "can not open browser with file {0}".format(filename))
Exemplo n.º 24
0
    def do_banner(self, args, arguments):
        """
        ::

            Usage:
                banner [-c CHAR] [-n WIDTH] [-i INDENT] [-r COLOR] TEXT

            Arguments:
                TEXT   The text message from which to create the banner
                CHAR   The character for the frame.
                WIDTH  Width of the banner
                INDENT indentation of the banner
                COLOR  the color

            Options:
                -c CHAR   The character for the frame. [default: #]
                -n WIDTH  The width of the banner. [default: 70]
                -i INDENT  The width of the banner. [default: 0]
                -r COLOR  The color of the banner. [default: BLACK]

            Prints a banner form a one line text message.
        """
        Console.ok("banner")
        n = int(arguments['-n'])
        c = arguments['-c']
        i = int(arguments['-i'])
        color = arguments['-r'].upper()

        Console.cprint(color, "", i * " " + str((n - i) * c))
        Console.cprint(color, "", i * " " + c + " " + arguments['TEXT'])
        Console.cprint(color, "", i * " " + str((n - i) * c))

        return ""
Exemplo n.º 25
0
    def do_check(self, args, arguments):
        """
        ::

            Usage:
                check --cloud=CLOUD
                check

                checks some elementary setting for cloudmesh

            Options:
               --format=FORMAT  the output format [default: table]
               --cloud=CLOUD    the cloud name

            Examples:
                cm check
                cm check --cloud=kilo

        """
        cloud = arguments["--cloud"] or Default.get_cloud()

        if cloud is None:
            Console.error("Default cloud doesn't exist")

        print(locals())

        Console.ok("{:} ok".format(cloud))

        return ""
Exemplo n.º 26
0
    def define(self, clustername=None, **kwargs):
            """Define a cluster.

            kwargs are passed to Cluster

            :returns: a cluster
            :rtype: :class:`Cluster`
            """

            clustername = clustername or Default.generate_name(Names.CLUSTER_COUNTER)

            # remove None to defer default definitions to later
            for k in kwargs.keys():
                if kwargs[k] is None:
                    del kwargs[k]

            try:
                spec = db.select(SPECIFICATION, name=clustername, type='cluster')[0]
                spec.update(kwargs)
                db.updateObj(spec)
            except IndexError:
                spec = SPECIFICATION(clustername, 'cluster', kwargs)
                db.insert(spec)

            Default.set_specification(clustername)
            Console.ok('Defined cluster {}'.format(clustername))
Exemplo n.º 27
0
    def do_echo(self, args, arguments):
        """
        ::

            Usage:
                echo  [-r COLOR] TEXT

            Arguments:
                TEXT   The text message from which to create the banner
                COLOR  the color

            Options:
                -r COLOR  The color of the banner. [default: BLACK]

            Prints a banner form a one line text message.
        """
        Console.ok("banner")
        n = int(arguments['-n'])
        c = arguments['-c']
        i = int(arguments['-i'])
        color = arguments['-r'].upper()

        Console.cprint(color, "", i * " " + str((n - i) * c))
        Console.cprint(color, "", i * " " + c + " " + arguments['TEXT'])
        Console.cprint(color, "", i * " " + str((n - i) * c))

        return ""
Exemplo n.º 28
0
    def list(cls, filename, cloud, info=False, output='table'):
        """
        lists clouds from cloudmesh.yaml file

        :param filename:
        :type filename: string
        :return:
        """
        config = ConfigDict("cloudmesh.yaml")
        clouds = config["cloudmesh"]["clouds"]
        if info:
            Console.ok("Cloudmesh configuration file: {}".format(filename))
            print("")
        d = {}
        for i, key in enumerate(clouds.keys()):
            d[i] = {
                "id":
                i,
                "cloud":
                key,
                "iaas":
                config["cloudmesh"]["clouds"][key]["cm_type"],
                "version":
                config["cloudmesh"]["clouds"][key]["cm_type_version"] or "",
                # "active": "*" if key in config["cloudmesh"]["active"] else "",
                "active":
                config["cloudmesh"]["active"].index(key) +
                1 if key in config["cloudmesh"]["active"] else "",
                "default":
                "*" if key == cloud else ""
            }
        return Printer.Printer.write(
            d,
            order=['id', 'default', 'cloud', 'iaas', 'version', 'active'],
            output=output)
Exemplo n.º 29
0
    def define(self, name=None, **kwargs):
        """Define a hadoop stack.

            """

        stackname = name or Default.generate_name(Names.STACK_COUNTER)

        # remove None to defer default definitions to latter
        for k in kwargs.keys():
            if kwargs[k] is None:
                del kwargs[k]

        kwargs['local_path'] = os.path.join(
            os.path.expanduser('~/.cloudmesh/stacks'), stackname)

        try:
            spec = db.select(SPECIFICATION, name=stackname, type='stack')[0]
            spec.update(kwargs)
            db.updateObj(spec)
        except IndexError:
            spec = SPECIFICATION(stackname, 'stack', kwargs)
            db.insert(spec)

        Default.set_stack(stackname)
        Console.ok('Defined stack {}'.format(stackname))
Exemplo n.º 30
0
    def merge(cls, _nameA, _nameB, mergeName):
        """
        Method to merge two groups into
            one group
        :param _nameA:
        :param _nameB:
        :param mergeName:
        :return:
        """
        try:
            args_a = {
                "name": _nameA
            }
            args_b = {
                "name": _nameB
            }

            # groupA = cls.cm.find_by_name(model.GROUP, _nameA)
            # groupB = cls.cm.find_by_name(model.GROUP, _nameB)

            groupA = cls.cm.find("group", output="object", **args_a).first()
            groupB = cls.cm.find("group", output="object", **args_b).first()

            if groupA is not None \
                    and groupB is not None:
                id_str_a = groupA.value
                id_str_b = groupB.value
                merge_str = id_str_a + ',' + id_str_b

                # Copy default parameters
                user = groupA.user
                cloud = groupA.cloud

                """
                mergeGroup = model.GROUP(
                    mergeName,
                    merge_str,
                    user=user,
                    cloud=cloud
                )
                cls.cm.add(mergeGroup)
                """

                mergeGroup = cls.cm.db_obj_dict("group",
                                                name=mergeName,
                                                value=merge_str,
                                                user=user,
                                                cloud=cloud)
                cls.cm.add_obj(mergeGroup)
                cls.cm.save()

                Console.ok(
                    "Merge of group [{}] & [{}] to group [{}] successful!"
                    .format(_nameA, _nameB, mergeName))
            else:
                Console.error("Your groups [{}] and/or [{}] do not exist!"
                              .format(_nameA, _nameB))
        except Exception as ex:
            Console.error(ex.message, ex)
Exemplo n.º 31
0
    def do_image(self, args, arguments):
        """
        ::

            Usage:
                image refresh [--cloud=CLOUD]
                image list [ID] [--cloud=CLOUD] [--format=FORMAT] [--refresh]

                This lists out the images present for a cloud

            Options:
               --format=FORMAT  the output format [default: table]
               --cloud=CLOUD    the cloud name
               --refresh        live data taken from the cloud

            Examples:
                cm image refresh
                cm image list
                cm image list --format=csv
                cm image list 58c9552c-8d93-42c0-9dea-5f48d90a3188 --refresh

        """
        cloud = arguments["--cloud"] or Default.cloud
        if cloud is None:
            Console.error("Default cloud doesn't exist")
            return

        if arguments["refresh"] or Default.refresh:
            msg = "Refresh image for cloud {:}.".format(cloud)
            if Image.refresh(cloud):
                Console.ok("{:} ok.".format(msg))
            else:
                Console.error("{:} failed.".format(msg))
                return ""

        if arguments["list"]:
            id = arguments['ID']
            live = arguments['--refresh']
            output_format = arguments["--format"]

            counter = 0

            result = None
            while counter < 2:
                if id is None:
                    result = Image.list(cloud, output_format)
                else:
                    result = Image.details(cloud, id, live, output_format)
                if counter == 0 and result is None:
                    if not Image.refresh(cloud):
                        msg = "Refresh image for cloud {:}.".format(cloud)
                        Console.error("{:} failed.".format(msg))
                counter += 1

            if result is None:
                Console.error("No image(s) found. Failed.")
            else:
                print(result)
            return ""
Exemplo n.º 32
0
    def do_flavor(self, args, arguments):
        """
        ::

            Usage:
                flavor refresh [--cloud=CLOUD]
                flavor list [ID] [--cloud=CLOUD] [--format=FORMAT] [--refresh]

                This lists out the flavors present for a cloud

            Options:
               --format=FORMAT  the output format [default: table]
               --cloud=CLOUD    the cloud name
               --refresh        refreshes the data before displaying it
                                from the cloud

            Examples:
                cm flavor refresh
                cm flavor list
                cm flavor list --format=csv
                cm flavor show 58c9552c-8d93-42c0-9dea-5f48d90a3188 --refresh

        """

        cloud = arguments["--cloud"] or Default.get_cloud()
        if cloud is None:
            Console.error("Default cloud doesn't exist")
            return

        if arguments["refresh"]:
            msg = "Refresh flavor for cloud {:}.".format(cloud)
            if Flavor.refresh(cloud):
                Console.ok("{:} ok".format(msg))
            else:
                Console.error("{:} failed".format(msg))
            return ""

        if arguments["list"]:
            cluster_id = arguments['ID']
            refresh = arguments['--refresh']
            output_format = arguments["--format"]

            if cluster_id is None:
                result = Flavor.list(cloud, output_format)
            else:
                result = Flavor.details(cloud, cluster_id, refresh, output_format)

            if result is None:
                #
                # outo refresh
                #
                Console.error("No flavor(s) found. Failed")
                # Flavor.refresh(cloud)
                # Console.ok("Refreshing flavor(s). ok.")

            else:

                print(result)
            return ""
Exemplo n.º 33
0
    def _delete_floating_ip(cls, cloudname, floating_ip):
        result = Network.delete_floating_ip(cloudname=cloudname,
                                            floating_ip_or_id=floating_ip)

        if result is not None:
            Console.ok(result)
        else:
            Console.error("Failed to delete floating IP address!")
Exemplo n.º 34
0
    def _delete_floating_ip(cls, cloudname, floating_ip):
        result = Network.delete_floating_ip(cloudname=cloudname,
                                            floating_ip_or_id=floating_ip)

        if result is not None:
            Console.ok(result)
        else:
            Console.error("Failed to delete floating IP address!")
Exemplo n.º 35
0
    def from_file(cls, filename):
        """
        Replaces the TBD in cloudmesh.yaml with the contents present in FILEPATH's FILE
        :param filename:
        :return:
        """
        if not os.path.isfile(os.path.expanduser(filename)):
            Console.error("{} doesn't exist".format(filename))
            return

        # BUG should use path separator
        path, filename = filename.rsplit("/", 1)
        # Config file to be read from

        from_config_file = ConfigDict(filename, [path])

        config = ConfigDict("cloudmesh.yaml")

        # Merging profile
        profile = config["cloudmesh"]["profile"]
        for profile_key in list(profile.keys()):
            if profile[profile_key] == "TBD":
                profile[profile_key] = \
                    from_config_file["cloudmesh"]["profile"][profile_key]
        config.save()

        # Merging clouds
        clouds = config["cloudmesh"]["clouds"]
        for cloud in list(clouds.keys()):
            cloud_element = clouds[cloud]
            for key in list(cloud_element.keys()):
                if cloud_element[key] == "TBD":
                    cloud_element[key] = \
                        from_config_file["cloudmesh"]["clouds"][cloud][key]
            config["cloudmesh"]["clouds"][cloud] = cloud_element

            credentials = clouds[cloud]["credentials"]
            for key in credentials:
                if credentials[key] == "TBD":
                    credentials[key] = \
                        from_config_file["cloudmesh"]["clouds"][cloud][
                            "credentials"][key]
            config["cloudmesh"]["clouds"][cloud]["credentials"] = credentials

            defaults = clouds[cloud]["default"]
            for key in defaults:
                if defaults[key] == "TBD":
                    defaults[key] = \
                        from_config_file["cloudmesh"]["clouds"][cloud][
                            "default"][
                            key]
            config["cloudmesh"]["clouds"][cloud]["default"] = defaults
        config.save()

        Console.ok(
            "Overwritten the TBD of cloudmesh.yaml with {} contents".format(
                filename))
Exemplo n.º 36
0
    def from_file(cls, filename):
        """
        Replaces the TBD in cloudmesh.yaml with the contents present in FILEPATH's FILE
        :param filename:
        :return:
        """
        if not os.path.isfile(os.path.expanduser(filename)):
            Console.error("{} doesn't exist".format(filename))
            return

        # BUG should use path separator
        path, filename = filename.rsplit("/", 1)
        # Config file to be read from

        from_config_file = ConfigDict(filename, [path])

        config = ConfigDict("cloudmesh.yaml")

        # Merging profile
        profile = config["cloudmesh"]["profile"]
        for profile_key in list(profile.keys()):
            if profile[profile_key] == "TBD":
                profile[profile_key] = \
                    from_config_file["cloudmesh"]["profile"][profile_key]
        config.save()

        # Merging clouds
        clouds = config["cloudmesh"]["clouds"]
        for cloud in list(clouds.keys()):
            cloud_element = clouds[cloud]
            for key in list(cloud_element.keys()):
                if cloud_element[key] == "TBD":
                    cloud_element[key] = \
                        from_config_file["cloudmesh"]["clouds"][cloud][key]
            config["cloudmesh"]["clouds"][cloud] = cloud_element

            credentials = clouds[cloud]["credentials"]
            for key in credentials:
                if credentials[key] == "TBD":
                    credentials[key] = \
                        from_config_file["cloudmesh"]["clouds"][cloud][
                            "credentials"][key]
            config["cloudmesh"]["clouds"][cloud]["credentials"] = credentials

            defaults = clouds[cloud]["default"]
            for key in defaults:
                if defaults[key] == "TBD":
                    defaults[key] = \
                        from_config_file["cloudmesh"]["clouds"][cloud][
                            "default"][
                            key]
            config["cloudmesh"]["clouds"][cloud]["default"] = defaults
        config.save()

        Console.ok(
            "Overwritten the TBD of cloudmesh.yaml with {} contents".format(
                filename))
Exemplo n.º 37
0
 def refresh_vm(cls, cloudname):
     try:
         msg = "Refreshing database for cloud {:}.".format(cloudname)
         if Vm.refresh(cloud=cloudname) is not None:
             Console.ok("{:} OK.".format(msg))
         else:
             Console.error("{:} failed".format(msg))
     except Exception:
         Console.error("Problem running database refresh")
Exemplo n.º 38
0
 def refresh_vm(cls, cloudname):
     try:
         msg = "Refreshing database for cloud {:}.".format(cloudname)
         if Vm.refresh(cloud=cloudname) is not None:
             Console.ok("{:} OK.".format(msg))
         else:
             Console.error("{:} failed".format(msg))
     except Exception:
         Console.error("Problem running database refresh")
Exemplo n.º 39
0
 def _refresh_cloud(cloud):
     try:
         msg = "Refresh VMs for cloud {:}.".format(cloud)
         if Vm.refresh(cloud=cloud):
             Console.ok("{:} OK.".format(msg))
         else:
             Console.error("{:} failed".format(msg), traceflag=False)
     except Exception as e:
         Console.error("Problem running VM refresh", traceflag=False)
Exemplo n.º 40
0
    def do_loglevel(self, args, arguments):
        """
        ::

            Usage:
                loglevel set MODE [--cloud=CLOUD]
                loglevel get [--cloud=CLOUD]
                loglevel save [--cloud=CLOUD]

            Arguments:
                MODE    log level mode [DEBUG/INFO/WARNING/CRITICAL/ERROR]

            Options:
                --cloud=CLOUD    the name of the cloud

        Description:
            loglevel command sets the default logging level
            for a cloud.

        Examples:
            loglevel set DEBUG --cloud=kilo
                sets the default log level to DEBUG for kilo.

            loglevel get --cloud=kilo
                retreives the default log level for kilo cloud.

            loglevel save --cloud=kilo
                saves the log level preference to the db & yaml file.

        """
        # pprint(arguments)

        cloud = arguments["--cloud"] or Default.get_cloud()
        LOGGER.info("Cloud: " + cloud + ", Arguments: " + str(arguments))

        if arguments["set"]:
            try:
                log_level = arguments["MODE"]
                response = LogUtil.set_level(log_level=log_level,
                                             cloudname=cloud)

                if response is not None:
                    Console.ok(response)
            except Exception as ex:
                Console.error(ex.message, ex)

        elif arguments["get"]:
            try:
                log_level = LogUtil.get_level(cloudname=cloud)
                Console.ok("Current Log Level = " + log_level + ". Ok.")
            except Exception as ex:
                Console.error(ex.message, ex)

        elif arguments["save"]:
            LogUtil.save(cloudname=cloud)
            pass
Exemplo n.º 41
0
 def _refresh():
     try:
         msg = "Refresh VMs for cloud {:}.".format(cloud)
         if Vm.refresh(cloud=cloud):
             Console.ok("{:} OK.".format(msg))
         else:
             Console.error("{:} failed".format(msg))
     except Exception as e:
         Error.traceback(e)
         Console.error("Problem running VM refresh")
Exemplo n.º 42
0
Arquivo: cm.py Projeto: atavism/client
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")
Exemplo n.º 43
0
 def _refresh():
     try:
         msg = "Refresh VMs for cloud {:}.".format(cloud)
         if Vm.refresh(cloud=cloud):
             Console.ok("{:} OK.".format(msg))
         else:
             Console.error("{:} failed".format(msg))
     except Exception as e:
         Error.traceback(e)
         Console.error("Problem running VM refresh")
Exemplo n.º 44
0
    def do_var(self, arg, arguments):
        """
        Usage:
            var list
            var delete NAMES
            var NAME=VALUE
            var NAME

        Arguments:
            NAME    Name of the variable
            NAMES   Names of the variable separated by spaces
            VALUE   VALUE to be assigned

        special vars date and time are defined
        """

        if arguments['list'] or arg == '' or arg is None:
            # self._list_variables()
            print(Var.list())
            return ""
        elif arguments['NAME=VALUE'] and "=" in arguments["NAME=VALUE"]:
            (variable, value) = arg.split('=', 1)
            if value == "time" or value == "now":
                value = datetime.datetime.now().strftime("%H:%M:%S")
            elif value == "date":
                value = datetime.datetime.now().strftime("%Y-%m-%d")
            elif value.startswith("default."):
                name = value.replace("default.", "")
                value = Default.get(name=name, category="general")
            elif "." in value:
                try:
                    config = ConfigDict("cloudmesh.yaml")
                    value = config[value]
                except Exception as e:
                    Console.error(
                        "can not find variable {} in cloudmesh.yaml".format(
                            value))
                    value = None
            # self._add_variable(variable, value)
            Var.set(variable, value)
            return ""
        elif arguments['NAME=VALUE'] and "=" not in arguments["NAME=VALUE"]:
            try:
                v = arguments['NAME=VALUE']
                # Console.ok(str(self.variables[v]))
                Console.ok(str(Var.get(v)))
            except:
                Console.error('variable {:} not defined'.format(
                    arguments['NAME=VALUE']))

        elif arg.startswith('delete'):
            variable = arg.split(' ')[1]
            Var.delete(variable)
            # self._delete_variable(variable)
            return ""
Exemplo n.º 45
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")
Exemplo n.º 46
0
def boot_from_args(arg):
    arg.username = arg.username or Image.guess_username(arg.image)
    is_name_provided = arg.name is not None

    arg.user = Default.user

    for index in range(0, arg.count):
        vm_details = dotdict({
            "cloud": arg.cloud,
            "name": Vm.generate_vm_name(arg.name, index),
            "image": arg.image,
            "flavor": arg.flavor,
            "key": arg.key,
            "secgroup": arg.secgroup,
            "group": arg.group,
            "username": arg.username,
            "user": arg.user
        })
        # correct the username
        vm_details.username = Image.guess_username_from_category(
            vm_details.cloud, vm_details.image, username=arg.username)
        try:

            if arg.dryrun:
                print(Printer.attribute(vm_details, output=arg.format))
                msg = "dryrun info. OK."
                Console.ok(msg)
            else:
                vm_id = Vm.boot(**vm_details)

                if vm_id is None:
                    msg = "info. failed."
                    Console.error(msg, traceflag=False)
                    return ""

                # set name and counter in defaults
                Default.set_vm(value=vm_details.name)
                if is_name_provided is False:
                    Default.incr_counter("name")

                # Add to group
                if vm_id is not None:
                    Group.add(name=vm_details.group,
                              species="vm",
                              member=vm_details.name,
                              category=vm_details.cloud)

                msg = "info. OK."
                Console.ok(msg)

        except Exception as e:
            Console.error(
                "Problem booting instance {name}".format(**vm_details),
                traceflag=False)
Exemplo n.º 47
0
def boot_from_args(arg):
    arg.username = arg.username or Image.guess_username(arg.image)
    is_name_provided = arg.name is not None

    arg.user = Default.user

    for index in range(0, arg.count):
        vm_details = dotdict({
            "cloud": arg.cloud,
            "name": Vm.get_vm_name(arg.name, index),
            "image": arg.image,
            "flavor": arg.flavor,
            "key": arg.key,
            "secgroup": arg.secgroup,
            "group": arg.group,
            "username": arg.username,
            "user": arg.user
        })
        # correct the username
        vm_details.username = Image.guess_username_from_category(
            vm_details.cloud,
            vm_details.image,
            username=arg.username)
        try:

            if arg.dryrun:
                print(Printer.attribute(vm_details, output=arg.format))
                msg = "dryrun info. OK."
                Console.ok(msg)
            else:
                vm_id = Vm.boot(**vm_details)

                if vm_id is None:
                    msg = "info. failed."
                    Console.error(msg, traceflag=False)
                    return ""

                # set name and counter in defaults
                Default.set_vm(value=vm_details.name)
                if is_name_provided is False:
                    Default.incr_counter("name")

                # Add to group
                if vm_id is not None:
                    Group.add(name=vm_details.group,
                              species="vm",
                              member=vm_details.name,
                              category=vm_details.cloud)

                msg = "info. OK."
                Console.ok(msg)

        except Exception as e:
            Console.error("Problem booting instance {name}".format(**vm_details), traceflag=False)
Exemplo n.º 48
0
 def test(cls, filename):
     """
     TODO
     :param filename:
     :type filename: string
     :return:
     """
     config = ConfigDict("cloudmesh.yaml")
     print(config)
     Console.ok("register")
     print(filename)
     raise NotImplementedError("Not implemented")
Exemplo n.º 49
0
 def _refresh():
     try:
         msg = "Refresh VMs for cloud {:}.".format(cloud)
         if Vm.refresh(cloud=cloud):
             Console.ok("{:} OK.".format(msg))
         else:
             Console.error("{:} failed".format(msg))
     except Exception, e:
         import traceback
         print(traceback.format_exc())
         print(e)
         Console.error("Problem running VM refresh")
Exemplo n.º 50
0
 def test(cls, filename):
     """
     TODO
     :param filename:
     :type filename: string
     :return:
     """
     config = ConfigDict("cloudmesh.yaml")
     print(config)
     Console.ok("register")
     print(filename)
     raise NotImplementedError("Not implemented")
Exemplo n.º 51
0
    def do_var(self, arg, arguments):
        """
        Usage:
            var list
            var delete NAMES
            var NAME=VALUE
            var NAME

        Arguments:
            NAME    Name of the variable
            NAMES   Names of the variable separated by spaces
            VALUE   VALUE to be assigned

        special vars date and time are defined
        """

        if arguments['list'] or arg == '' or arg is None:
            # self._list_variables()
            print(Var.list())
            return ""
        elif arguments['NAME=VALUE'] and "=" in arguments["NAME=VALUE"]:
            (variable, value) = arg.split('=', 1)
            if value == "time" or value == "now":
                value = datetime.datetime.now().strftime("%H:%M:%S")
            elif value == "date":
                value = datetime.datetime.now().strftime("%Y-%m-%d")
            elif value.startswith("default."):
                name = value.replace("default.", "")
                value = Default.get(name=name, category="general")
            elif "." in value:
                try:
                    config = ConfigDict("cloudmesh.yaml")
                    value = config[value]
                except Exception as e:
                    Console.error("can not find variable {} in cloudmesh.yaml".format(value))
                    value = None
            # self._add_variable(variable, value)
            Var.set(variable, value)
            return ""
        elif arguments['NAME=VALUE'] and "=" not in arguments["NAME=VALUE"]:
            try:
                v = arguments['NAME=VALUE']
                # Console.ok(str(self.variables[v]))
                Console.ok(str(Var.get(v)))
            except:
                Console.error('variable {:} not defined'.format(arguments['NAME=VALUE']))

        elif arg.startswith('delete'):
            variable = arg.split(' ')[1]
            Var.delete(variable)
            # self._delete_variable(variable)
            return ""
Exemplo n.º 52
0
    def fill_out_form(cls, filename):
        """
        edits profile and clouds from cloudmesh.yaml
        :param filename:
        :type filename: string
        :return:
        """
        Console.ok("Filling out form")
        print(filename)
        config = ConfigDict(filename)
        #
        # edit profile
        #

        profile = config["cloudmesh"]["profile"]
        keys = list(profile.keys())

        # TODO: test this and delete this comment
        # get input that works in python 2 and 3

        # replaced by
        #   from builtins import input
        # input = None
        # try:
        #    input = input
        # except NameError:
        #    pass

        for key in keys:
            if profile[key] == "TBD":
                result = input("Please enter {:}[{:}]:".format(
                    key, profile[key])) or profile[key]
                profile[key] = result

        config["cloudmesh"]["profile"] = profile
        config.save()

        # edit clouds
        clouds = config["cloudmesh"]["clouds"]
        for cloud in list(clouds.keys()):
            print("Editing the credentials for cloud", cloud)
            credentials = clouds[cloud]["credentials"]

            for key in credentials:
                if key not in ["OS_VERSION", "OS_AUTH_URL"
                               ] and credentials[key] == "TBD":
                    result = input("Please enter {:}[{:}]:".format(
                        key, credentials[key])) or credentials[key]
                    credentials[key] = result
            config["cloudmesh"]["clouds"][cloud]["credentials"] = credentials
        config.save()
Exemplo n.º 53
0
    def copy(cls, _fromName, _toName):
        """
        Method to make copy of a group
        :param _fromName:
        :param _toName:
        :return:
        """

        try:
            from_args = {"name": _fromName}
            to_args = {"name": _toName}

            _fromGroup = cls.cm.find(kind="group",
                                     scope="all",
                                     output="dict",
                                     **from_args)
            _toGroup = cls.cm.find(kind="group",
                                   scope="all",
                                   output="dict",
                                   **to_args)

            # print ("A")
            # pprint (_fromGroup)
            # print ("B")
            # pprint(_toGroup)

            if _fromGroup is not None:

                for from_element in _fromGroup:
                    member = from_element["member"]
                    species = from_element["species"]
                    category = from_element["category"]
                    print("TTT", _toName)
                    cls.add(name=_toName,
                            species=species,
                            member=member,
                            category=category)
                cls.cm.save()
                Console.ok("Copy from group {} to group {}. ok.".format(
                    _fromName, _toName))

            else:
                Console.error(
                    "Group [{}] does not exist in the cloudmesh database!".
                    format(_fromName),
                    traceflag=False)
                return None

        except Exception as ex:
            Console.error(ex.message)
Exemplo n.º 54
0
    def delete_all_rules(cls, secgroup):
        try:

            args = {"groupid": secgroup.uuid}
            rules = cls.cm_db.find("secgrouprule", output="object", **args)

            if rules is not None:
                for rule in rules:
                    cls.cm_db.delete(rule)
                    Console.ok("Rule [{} | {} | {} | {}] deleted".format(
                        rule.fromPort, rule.toPort, rule.protocol, rule.cidr))
            else:
                pass
        except Exception as ex:
            Console.error(ex.message, ex)

        return