示例#1
0
    def do_echo(self, args, arguments):
        """
        ::

            Usage:
                echo  [-r COLOR] TEXT

            Arguments:
                TEXT   The text message to print
                COLOR  the color

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

            Prints a text in the given color
        """
        color = arguments["-r"] or "black"
        color = color.upper()
        text = arguments["TEXT"]
        if color is "black":
            Console.msg(text)
        else:
            Console.cprint(color, "", text)

        return ""
示例#2
0
    def do_quota(self, args, arguments):
        """
        ::

            Usage:
                quota list [--cloud=CLOUD] [--tenant=TENANT] [--format=FORMAT]

                Prints quota limit on a current project/tenant

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

            Examples:
                cm quota list
                cm quota list --cloud=india --format=csv

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

            if not cloud:
                Console.error("Default cloud doesn't exist")
                return
            tenant = arguments["--tenant"]
            output_format = arguments["--format"]
            list_quotas = Quota.list(cloud,
                                     tenant,
                                     output=output_format)
            Console.msg(list_quotas)
            return
示例#3
0
    def do_limits(self, args, arguments):
        """
        ::

            Usage:
                limits list [--cloud=CLOUD] [--tenant=TENANT] [--format=FORMAT]

                Current list data with limits on a selected project/tenant.
                The --tenant option can be used by admin only

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

            Examples:
                cm limits list
                cm limits list --cloud=juno --format=csv

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

            if not cloud:
                Console.error("cloud doesn't exist")
                return ""

            output_format = arguments["--format"]
            tenant = arguments["--tenant"]
            result = Limits.list(cloud,
                                 output=output_format,
                                 tenant=tenant)
            Console.msg(result)
            return ""
示例#4
0
    def do_quota(self, args, arguments):
        """
        ::

            Usage:
                quota list [--cloud=CLOUD] [--tenant=TENANT] [--format=FORMAT]

                Prints quota limit on a current project/tenant

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

            Examples:
                cm quota list
                cm quota list --cloud=india --format=csv

        """
        if arguments["list"]:
            cloud = arguments["--cloud"] or Default.cloud

            if not cloud:
                Console.error("Default cloud doesn't exist")
                return
            tenant = arguments["--tenant"]
            output_format = arguments["--format"]
            list_quotas = Quota.list(cloud, tenant, output=output_format)
            Console.msg(list_quotas)
            return
示例#5
0
 def run(cls, cloud, id):
     elements = cls.cm.find(kind="workflow", category='general', cm_id=id)
     Console.msg(elements)
     order = None
     Console.msg("Executing")
     header = None
     return elements
示例#6
0
 def run(cls,cloud,id):
     elements = cls.cm.find(kind="workflow", category='general', cm_id = id)
     Console.msg(elements)
     order = None
     Console.msg("Executing")
     header= None
     return elements
示例#7
0
    def do_limits(self, args, arguments):
        """
        ::

            Usage:
                limits list [--cloud=CLOUD] [--tenant=TENANT] [--format=FORMAT]

                Current list data with limits on a selected project/tenant.
                The --tenant option can be used by admin only

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

            Examples:
                cm limits list
                cm limits list --cloud=kilo --format=csv

        """
        if arguments["list"]:
            cloud = arguments["--cloud"] or Default.cloud

            if not cloud:
                Console.error("cloud doesn't exist")
                return ""

            output_format = arguments["--format"]
            tenant = arguments["--tenant"]
            result = Limits.list(cloud,
                                 output=output_format,
                                 tenant=tenant)
            Console.msg(result)
            return ""
示例#8
0
    def do_echo(self, args, arguments):
        """
        ::

            Usage:
                echo  [-r COLOR] TEXT

            Arguments:
                TEXT   The text message to print
                COLOR  the color

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

            Prints a text in the given color
        """
        color = arguments["-r"] or "black"
        color = color.upper()
        text = arguments["TEXT"]
        if color is "black":
            Console.msg(text)
        else:
            Console.cprint(color, "", text)

        return ""
示例#9
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",
            }
        }
        config = ConfigDict("cloudmesh.yaml")
        config["cloudmesh"]["clouds"][cloud] = d
        config.save()
示例#10
0
 def details(cls, cloud, id, live=False, format="table"):
     elements = cls.cm.find(kind="workflow", category='general' ,cm_id =id)
     Console.msg(elements)
     order = None
     header= None
     # Console.TODO("this method is not yet implemented")
     return Printer.write(elements,
                              order=order,
                              header=header,
                              output=format)
示例#11
0
 def refresh_vm(cls, cloudname):
     try:
         msg = "Refreshing database for cloud {:}.".format(cloudname)
         Console.msg(msg)
         if Vm.refresh(category=cloudname) is not None:
             Console.ok("Complete.")
         else:
             Console.error("{:} failed".format(msg))
     except Exception:
         Console.error("Problem running database refresh")
示例#12
0
 def details(cls, cloud, id, live=False, format="table"):
     elements = cls.cm.find(kind="workflow", category='general', cm_id=id)
     Console.msg(elements)
     order = None
     header = None
     # Console.TODO("this method is not yet implemented")
     return Printer.write(elements,
                          order=order,
                          header=header,
                          output=format)
示例#13
0
 def refresh_vm(cls, cloudname):
     try:
         msg = "Refreshing database for cloud {:}.".format(cloudname)
         Console.msg(msg)
         if Vm.refresh(cloud=cloudname) is not None:
             Console.ok("Complete.")
         else:
             Console.error("{:} failed".format(msg))
     except Exception:
         Console.error("Problem running database refresh")
示例#14
0
文件: sync.py 项目: arpiagariu/client
    def sync(cls, cloudname, localdir, remotedir, operation=None):
        """
        Syncs a local directory with a remote directory.
        Either from local to remote OR vice-versa
        :param cloudname:
        :param localdir:
        :param remotedir:
        :param operation: get/put
        :return:
        """
        # Get the operating system
        os_type = cls.operating_system()

        # fix the local dir path
        localdirpath = Config.path_expand(localdir)

        # check if local directory exists
        if not os.path.exists(localdirpath):
            if operation == "put":
                Console.error("The local directory [{}] does not exist."
                              .format(localdirpath))
                return None
            elif operation == "get":
                # for receiving, create local dir
                os.mkdir(localdirpath)
                Console.msg("Created local directory [{}] for sync."
                            .format(localdirpath))

        """
            rsync now works on windows machines as well.
            we install rsync (v5.4.1.20150827) on windows via chocolatey
            $ choco install rsync
        """

        host = cls.get_host(cloudname)
        if host is None:
            Console.error("Cloud [{}] not found in cloudmesh.yaml file."
                          .format(cloudname))
            return None
        else:
            args = None
            if operation == "put":
                args = [
                    "-r",
                    localdir,
                    host + ":" + remotedir
                ]
            elif operation == "get":
                args = [
                    "-r",
                    host + ":" + remotedir,
                    localdir
                ]
            # call rsync
            return Shell.rsync(*args)
示例#15
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()
示例#16
0
文件: sync.py 项目: sohiljain/client
    def sync(cls, cloudname, localdir, remotedir, operation=None):
        """
        Syncs a local directory with a remote directory.
        Either from local to remote OR vice-versa
        :param cloudname:
        :param localdir:
        :param remotedir:
        :param operation: get/put
        :return:
        """
        # Get the operating system
        os_type = cls.operating_system()

        # fix the local dir path
        localdirpath = Config.path_expand(localdir)

        # check if local directory exists
        if not os.path.exists(localdirpath):
            if operation == "put":
                Console.error(
                    "The local directory [{}] does not exist.".format(
                        localdirpath))
                return None
            elif operation == "get":
                # for receiving, create local dir
                os.mkdir(localdirpath)
                Console.msg("Created local directory [{}] for sync.".format(
                    localdirpath))
        """
            rsync now works on windows machines as well.
            we install rsync (v5.4.1.20150827) on windows via chocolatey
            $ choco install rsync
        """

        host = cls.get_host(cloudname)
        if host is None:
            Console.error(
                "Cloud [{}] not found in cloudmesh.yaml file.".format(
                    cloudname))
            return None
        else:
            args = None
            if operation == "put":
                args = ["-r", localdir, host + ":" + remotedir]
            elif operation == "get":
                args = ["-r", host + ":" + remotedir, localdir]
            # call rsync
            return Shell.rsync(*args)
    def delete_secgroup(self, name):
        ret = None
        groups = self.provider.security_groups.list()

        if groups is not None:
            for sec_group in groups:

                # delete the secgroup in the cloud
                if sec_group.name == name:
                    try:
                        ret = self.provider.security_groups.delete(sec_group)
                        Console.msg("Secgroup delete: {} {}".format(sec_group.name, sec_group.__dict__["id"]))
                    except Exception as e:
                        Console.error(e.message, traceflag=False)
                        Console.error("Secgroup delete: {}".format(sec_group.name), traceflag=False)

        else:
            print("Could not find security group [{}] in cloud [{}]"
                  .format(name, self.cloud))
        return ret
示例#18
0
    def upload(cls, cloud=None, group=None):
        if cloud is None or cloud == 'all':
            clouds = ConfigDict("cloudmesh.yaml")["cloudmesh"]["active"]
        else:
            clouds = [cloud]
        if group is None:
            rules = cls.list(output='dict')
            groups = set()
            for g in rules:
                r = rules[g]
                groups.add(r["group"])
            groups = list(groups)
        else:
            groups = [group]
        for cloud in clouds:
            Console.msg(
                "Uploading the groups/rules to cloud - {}...".format(cloud))
            for g in groups:
                cls.delete_all_rules_cloud(cloud, g)
                group = cls.get(name=g, cloud=cloud)
                group_cloud = cls.get_group_cloud(cloud, g)
                if not group_cloud:
                    cls.add_group_cloud(cloud, g)
                rules = cls.list_rules(group=g, output="dict")

                if rules:
                    for ruleid in rules:
                        rule = rules[ruleid]
                        rulename = rule["name"]
                        cls.add_rule_cloud(cloud, g, rulename)
                '''
                SecGroup.delete(category=c, group=g)
                uuid = SecGroup.create(category=c, group=g)
                for key in rules:
                    r = rules[key]
                    if r["group"] == g:
                        SecGroup.add_rule(c,uuid,r["fromPort"],r["toPort"] , r['protocol'],r['cidr'])
                # create group
                '''
            Console.msg("...done")
        Console.info("All completed")
示例#19
0
    def upload(cls, cloud=None, group=None):
        if cloud is None or cloud=='all':
            clouds = ConfigDict("cloudmesh.yaml")["cloudmesh"]["active"]
        else:
            clouds = [cloud]
        if group is None:
            rules = cls.list(output='dict')
            groups = set()
            for g in rules:
                r = rules[g]
                groups.add(r["group"])
            groups = list(groups)
        else:
            groups = [group]
        for cloud in clouds:
            Console.msg("Uploading the groups/rules to cloud - {}...".format(cloud))
            for g in groups:
                cls.delete_all_rules_cloud(cloud, g)
                group = cls.get(name=g, cloud=cloud)
                group_cloud = cls.get_group_cloud(cloud, g)
                if not group_cloud:
                    cls.add_group_cloud(cloud, g)
                rules = cls.list_rules(group=g, output="dict")

                if rules:
                    for ruleid in rules:
                        rule = rules[ruleid]
                        rulename = rule["name"]
                        cls.add_rule_cloud(cloud, g, rulename)
                '''
                SecGroup.delete(category=c, group=g)
                uuid = SecGroup.create(category=c, group=g)
                for key in rules:
                    r = rules[key]
                    if r["group"] == g:
                        SecGroup.add_rule(c,uuid,r["fromPort"],r["toPort"] , r['protocol'],r['cidr'])
                # create group
                '''
            Console.msg("...done")
        Console.info("All completed")
示例#20
0
    def do_usage(self, args, arguments):
        """
        ::

            Usage:
                usage list [--cloud=CLOUD] [--start=START] [--end=END] [--tenant=TENANT] [--format=FORMAT]

                Show usage data.

            Options:
               --format=FORMAT  the output format [default: table]
               --cloud=CLOUD    the cloud name
               --tenant=TENANT  the tenant name
               --start=START    Usage range start date ex 2012-01-20, default is: 4 weeks ago
               --end=END        Usage range end date, ex 2012-01-20, default is: tomorrow


            Examples:
                cm usage list

        """

        if arguments["list"]:
            cloud = arguments["--cloud"] or Default.get("cloud")

            if not cloud:
                Console.error("cloud doesn't exist")
                return ""
            output_format = arguments["--format"]
            start = arguments["--start"]
            end = arguments["--end"]
            tenant = arguments["--tenant"]
            usage = Usage.list(cloud,
                               start=start,
                               end=end,
                               tenant=tenant,
                               format=output_format)
            Console.msg(usage)
            return ""
示例#21
0
    def do_usage(self, args, arguments):
        """
        ::

            Usage:
                usage list [--cloud=CLOUD] [--start=START] [--end=END] [--tenant=TENANT] [--format=FORMAT]

                Show usage data.

            Options:
               --format=FORMAT  the output format [default: table]
               --cloud=CLOUD    the cloud name
               --tenant=TENANT  the tenant name
               --start=START    Usage range start date ex 2012-01-20, default is: 4 weeks ago
               --end=END        Usage range end date, ex 2012-01-20, default is: tomorrow


            Examples:
                cm usage list

        """

        if arguments["list"]:
            cloud = arguments["--cloud"] or Default.get("cloud")

            if not cloud:
                Console.error("cloud doesn't exist")
                return ""
            output_format = arguments["--format"]
            start = arguments["--start"]
            end = arguments["--end"]
            tenant = arguments["--tenant"]
            usage = Usage.list(cloud,
                               start=start,
                               end=end,
                               tenant=tenant,
                               format=output_format)
            Console.msg(usage)
            return ""
    def delete_secgroup(self, name):
        ret = None
        groups = self.provider.security_groups.list()

        if groups is not None:
            for sec_group in groups:

                # delete the secgroup in the cloud
                if sec_group.name == name:
                    try:
                        ret = self.provider.security_groups.delete(sec_group)
                        Console.msg("Secgroup delete: {} {}".format(
                            sec_group.name, sec_group.__dict__["id"]))
                    except Exception as e:
                        Console.error(e.message, traceflag=False)
                        Console.error("Secgroup delete: {}".format(
                            sec_group.name),
                                      traceflag=False)

        else:
            print("Could not find security group [{}] in cloud [{}]".format(
                name, self.cloud))
        return ret
示例#23
0
    def do_key(self, args, arguments):
        """
        ::

           Usage:
             key  -h | --help
             key list --cloud=CLOUD
             key list --source=db [--format=FORMAT]
             key list --source=yaml [--format=FORMAT]
             key list --source=ssh [--dir=DIR] [--format=FORMAT]
             key list --source=git [--format=FORMAT] [--username=USERNAME]
             key list
             key load [--format=FORMAT]
             key add [NAME] [--source=FILENAME]
             key add [NAME] [--git]
             key add [NAME] [--ssh]
             key get NAME
             key default --select
             key delete (NAME | --select | --all)
             key delete NAME --cloud=CLOUD
             key upload [NAME] [--cloud=CLOUD]
             key upload [NAME] --active

           Manages the keys

           Arguments:

             CLOUD          The cloud
             NAME           The name of the key.
             SOURCE         db, ssh, all
             KEYNAME        The name of a key. For key upload it defaults to the default key name.
             FORMAT         The format of the output (table, json, yaml)
             FILENAME       The filename with full path in which the key
                            is located
             NAME_ON_CLOUD  Typically the name of the keypair on the cloud.

           Options:

              --dir=DIR                     the directory with keys [default: ~/.ssh]
              --format=FORMAT               the format of the output [default: table]
              --source=SOURCE               the source for the keys [default: db]
              --username=USERNAME           the source for the keys [default: none]
              --name=KEYNAME                The name of a key
              --all                         delete all keys
              --force                       delete the key form the cloud
              --name_on_cloud=NAME_ON_CLOUD Typically the name of the keypair on the cloud.

           Description:

           key list --source=git  [--username=USERNAME]

              lists all keys in git for the specified user. If the
              name is not specified it is read from cloudmesh.yaml

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

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

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

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

           key list [--format=FORMAT]

               list the keys in the giiven format: json, yaml,
               table. table is default

           key list

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

               
           key add [--name=keyname] FILENAME

               adds the key specifid by the filename to the key
               database

           key get NAME

               Retrieves the key indicated by the NAME parameter from database
               and prints its fingerprint.

           key default --select

                Select the default key interactively

           key delete NAME

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

           key rename NAME NEW

                renames the key from NAME to NEW.
                
        """
        # pprint(arguments)

        invalid_names = ['tbd', 'none', "", 'id_rsa']

        def _print_dict(d, header=None, format='table'):
            msg = Printer.write(d,
                                 order=["name",
                                        "comment",
                                        "uri",
                                        "fingerprint",
                                        "source"],
                                 output=format,
                                 sort_keys=True)
            if msg is None:
                Console.error("No keys found.", traceflag=False)
                return None
            else:
                return msg

        directory = Config.path_expand(arguments["--dir"])

        cloud = arguments["--cloud"] or Default.cloud

        if arguments['list']:
            _format = arguments['--format']
            _source = arguments['--source']
            _dir = arguments['--dir']

            if "--source" not in arguments and "--cloud" not in arguments:
                arguments["--source"] = 'db'


            if arguments['--cloud']:

                    #
                    # get key list from openstack cloud
                    #
                    #keys = Key.list(cloud, output=_format)

                    #keys = Key.list_on_cloud(cloud, live=True, format=_format)
                    keys = Key.get_from_cloud(cloud, live=True, format=_format)
                    if keys is None:
                        Console.ok("The Key list is empty")
                    else:
                        print(Printer.write(keys,
                            order=["name",
                                "fingerprint"],
                            output=_format or "table"))
                    return ""

            elif arguments['--source'] == 'ssh':

                try:
                    #sshm = SSHKeyManager()
                    d = Key.get_from_dir(directory, store=False)

                    #print("SSS", type(Key.__keys__))
                    #d = dict(Key.all())
                    #print(d)
                    print(Printer.write(d,
                                        order=["name",
                                               "comment",
                                               "uri",
                                               "fingerprint",
                                               "source"],
                                        output="table"))

                    # d = dict(sshm.__keys__)

                    # print(_print_dict(d, format=_format))
                    msg = "info. OK."
                    Console.ok(msg)
                    return ""
                except Exception as e:
                    Error.traceback(e)
                    Console.error("Problem listing keys from ssh")

            elif arguments['--source'] in ['cm', 'cloudmesh', 'yaml']:

                try:
                    #sshm = SSHKeyManager()
                    d = Key.get_from_yaml(load_order=directory, store=False)

                    print(Printer.write(d,
                                        order=["name",
                                               "comment",
                                               "uri",
                                               "fingerprint",
                                               "source"],
                                        output=_format))
                    return ""
                except Exception as e:
                    Error.traceback(e)
                    Console.error("Problem listing keys from `{:}`".format(arguments['--source']))

            elif arguments['--source'] in ['git']:

                username = arguments["--username"]
                # print(username)
                if username == 'none':
                    conf = ConfigDict("cloudmesh.yaml")
                    username = conf["cloudmesh.github.username"]

                #sshm = SSHKeyManager()
                try:

                    d = Key.get_from_git(username, store=False)

                    print(Printer.write(d,
                                        order=["name",
                                               "comment",
                                               "uri",
                                               "fingerprint",
                                               "source"],
                                        output=_format))

                    msg = "info. OK."
                    Console.ok(msg)
                except Exception as e:
                    Error.traceback(e)
                    Console.error("Problem listing git keys from database")
                    return ""

            elif arguments['--source'] == 'db':

                try:
                    #sshdb = SSHKeyDBManager()
                    d = Key.all(output='dict')

                    if d is not None or d != []:

                        print(Printer.write(d,
                                            order=["name",
                                                   "comment",
                                                   "uri",
                                                   "fingerprint",
                                                   "source"],
                                            output=_format))

                        # print(_print_dict(d, output=arguments['--format']))
                        msg = "info. OK."
                        Console.ok(msg)
                    else:
                        Console.error("No keys in the database")
                except Exception as e:
                    Error.traceback(e)
                    Console.error("Problem listing keys from database")



        elif arguments['get']:

            try:
                name = arguments['NAME']
                #sshdb = SSHKeyDBManager()
                d = Key.all(output="dict")

                for key in d:
                    if key["name"] == name:
                        print("{:}: {:}".format(key['name'], key['fingerprint']))
                        msg = "info. OK."
                        Console.ok(msg)
                        return ""
                    else:
                        pass
                Console.error("The key is not in the database")
            except Exception as e:
                Error.traceback(e)
                Console.error("The key is not in the database")

        # key add [NAME] [--source=FILENAME]
        #     key add [NAME] [--git]

        elif arguments['add'] and arguments["--git"]:

            # Console.error("This feature is not yet implemented", traceflag=False)
            # return ""

            print('git add')
            #sshdb = SSHKeyDBManager()

            data = dotdict(arguments)

            keyname = data.NAME

            #
            # get name
            #
            conf = ConfigDict("cloudmesh.yaml")
            data.username = conf["cloudmesh.github.username"]
            data.name = arguments['NAME'] or data.username

            #
            # get git username
            #
            data.username = ConfigDict("cloudmesh.yaml")["cloudmesh.github.username"]

            if str(data.name).lower() in invalid_names:
                Console.error("The github user name is not set in the yaml file", traceflag=False)
                return ""

            try:
                Console.msg("Retrieving github ssh keys for user {username}".format(**data))
                #sshm = SSHKeyManager()
                Key.get_from_git(data.username)
                d = Key.all()
                # pprint(d)
            except Exception as e:
                Console.error("Problem adding keys to git for user: {username}".format(**data))
                return ""

            for key in d:
                if key is not None:
                    key["name"] = key["name"].replace("-", "_")
                    key["source"] = "git"
                    key["user"] = data.name
                    try:
                        o = dict(key)
                        o['value'] = key["string"]
                        Key.add_from_dict(key)
                    except Exception as e:
                        Console.error("The key {name} with that finger print already exists".format(**key),
                                      traceflag=False)

        elif arguments['add'] and not arguments["--git"]:

            #     key add [NAME] [--source=FILENAME]
            #     key add [NAME] [--git]

            #sshdb = SSHKeyDBManager()

            data = dotdict()

            #
            # get name
            #
            conf = ConfigDict("cloudmesh.yaml")
            data.username = conf["cloudmesh.profile.user"]
            data.name = arguments['NAME'] or data.username

            data.filename = arguments['--source']
            if data.filename == "db" or data.filename is None:
                data.filename = Config.path_expand("~/.ssh/id_rsa.pub")

            if str(data.name).lower() in invalid_names:
                msg = ("Your choice of keyname {name} is insufficient. \n"
                       "You must be chosing a keyname that is distingct on all clouds. \n"
                       "Possible choices are your gmail name, your XSEDE name, or \n"
                       "some name that is uniqe. "
                       "Best is also to set this name in \n"
                       "cloudmesh.profile.user as "
                       "part of your \n~/cloudmesh/cloudmesh.yaml file.")
                Console.error(msg.format(**data), traceflag=False)
                return ""

            try:
                Key.add_from_path(data.filename,
                                    data.name,
                                    source="ssh",
                                    uri="file://" + data.filename)
                print("Key {name} successfully added to the database".format(**data))
                msg = "info. OK."
                Console.ok(msg)

            except ValueError as e:
                Console.error("A key with this fingerprint already exists".format(**data), traceflag=False)
                Console.msg("Please use check with: key list")

            return ""

        elif arguments['default']:

            # print("default")

            if arguments['--select']:
                keyname = None
                try:
                    #sshdb = SSHKeyDBManager()
                    select = Key.select()
                    if select != 'q':
                        keyname = select.split(':')[0]
                        print("Setting key: {:} as default.".format(keyname))
                        Default.key = keyname
                        msg = "info. OK."
                        Console.ok(msg)
                except Exception as e:
                    Error.traceback(e)
                    Console.error("Setting default for selected key {:} failed.".format(keyname))

            else:
                try:
                    #sshdb = SSHKeyDBManager()
                    d = Key.table_dict()

                    for i in d:
                        if d[i]["is_default"] == "True":
                            key = d[i]
                            print("{:}: {:}".format(key['name'], key['fingerprint']))
                            msg = "info. OK."
                            Console.ok(msg)
                            return ""
                        else:
                            pass
                    Console.error("The key is not in the database")
                except Exception as e:
                    Error.traceback(e)
                    Console.error("Problem retrieving default key.")

        elif arguments['delete'] and arguments["--cloud"]:

            key = dotdict({
                'cloud': arguments["--cloud"],
                'name': arguments["NAME"]
            })
            try:
                Key.delete(key.name, key.cloud)
                msg = "info. OK."
                Console.ok(msg)
            except:
                Console.error("Problem deleting the key {name} on the cloud {cloud}".format(**key))

        elif arguments['delete']:

            # key delete (NAME | --select| --all)

            data = dotdict({
                'all': arguments['--all'] or False,
                'select': arguments['--select'] or False,
                'name': arguments['NAME'] or False,
            })


            pprint(data)
            # BUG delete all is not properly implemented

            if data.all:
                Console.TODO("Delete --all is not yet implemented.")
                # Key.delete()
            elif data.select:
                key = Key.select()
                print (key)

            else: # name
                Key.delete(data.name)


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

        elif arguments['upload']:

            # pprint(arguments)

            try:
                #
                # get username
                #
                conf = ConfigDict("cloudmesh.yaml")
                username = conf["cloudmesh"]["profile"]["user"]
                if username in ['None', 'TBD']:
                    username = None

                #
                # get cloudnames
                #
                clouds = []

                if arguments["--active"]:
                    cloud = 'active'
                else:
                    cloud = arguments["--cloud"] or Default.cloud

                if cloud == "all":
                    config = ConfigDict("cloudmesh.yaml")
                    clouds = config["cloudmesh"]["clouds"]
                elif cloud == 'active':
                    config = ConfigDict("cloudmesh.yaml")
                    clouds = config["cloudmesh"]["active"]
                else:
                    clouds.append(cloud)

                #
                # get keyname
                #

                for cloud in clouds:
                    status = 0
                    #sshdb = SSHKeyDBManager()
                    #sshm = SSHKeyManager()
                    keys = Key.all()
                    for key in keys:

                        print("upload key {} -> {}".format(key["name"],
                                                           cloud))

                        try:
                            status = Key.add_key_to_cloud(
                                username,
                                key["name"],
                                cloud)

                        except Exception as e:
                            Console.error("problem")
                            if "already exists" in str(e):
                                print("key already exists. Skipping upload. OK.")
                        if status == 1:
                            print("Problem uploading key {} to {}. failed.".format(key["name"],
                                                                                   cloud))

            except Exception as e:
                Console.error("Problem adding key to cloud")
示例#24
0
    def do_network(self, args, arguments):
        """
        ::

            Usage:
                network get fixed [ip] [--cloud=CLOUD] FIXED_IP
                network get floating [ip] [--cloud=CLOUD] FLOATING_IP_ID
                network reserve fixed [ip] [--cloud=CLOUD] FIXED_IP
                network unreserve fixed [ip] [--cloud=CLOUD] FIXED_IP
                network associate floating [ip] [--cloud=CLOUD] [--group=GROUP] [--instance=INS_ID_OR_NAME] [FLOATING_IP]
                network disassociate floating [ip] [--cloud=CLOUD] [--group=GROUP] [--instance=INS_ID_OR_NAME] [FLOATING_IP]
                network create floating [ip] [--cloud=CLOUD] [--pool=FLOATING_IP_POOL]
                network delete floating [ip] [--cloud=CLOUD] FLOATING_IP
                network list floating pool [--cloud=CLOUD]
                network list floating [ip] [--cloud=CLOUD] [--instance=INS_ID_OR_NAME] [IP_OR_ID]
                network -h | --help

            Options:
                -h                          help message
                --cloud=CLOUD               Name of the IaaS cloud e.g. india_openstack_grizzly.
                --group=GROUP               Name of the group in Cloudmesh
                --pool=FLOATING_IP_POOL     Name of Floating IP Pool
                --instance=INS_ID_OR_NAME   ID or Name of the vm instance

            Arguments:
                IP_OR_ID        IP Address or ID of IP Address
                FIXED_IP        Fixed IP Address, e.g. 10.1.5.2
                FLOATING_IP     Floating IP Address, e.g. 192.1.66.8
                FLOATING_IP_ID  ID associated with Floating IP, e.g. 185c5195-e824-4e7b-8581-703abec4bc01

            Examples:
                $ network get fixed ip --cloud=india 10.1.2.5
                $ network get fixed --cloud=india 10.1.2.5
                $ network get floating ip --cloud=india 185c5195-e824-4e7b-8581-703abec4bc01
                $ network get floating --cloud=india 185c5195-e824-4e7b-8581-703abec4bc01
                $ network reserve fixed ip --cloud=india 10.1.2.5
                $ network reserve fixed --cloud=india 10.1.2.5
                $ network unreserve fixed ip --cloud=india 10.1.2.5
                $ network unreserve fixed --cloud=india 10.1.2.5
                $ network associate floating ip --cloud=india --instance=albert-001 192.1.66.8
                $ network associate floating --cloud=india --instance=albert-001
                $ network associate floating --cloud=india --group=albert_group
                $ network disassociate floating ip --cloud=india --instance=albert-001 192.1.66.8
                $ network disassociate floating --cloud=india --instance=albert-001 192.1.66.8
                $ network create floating ip --cloud=india --pool=albert-f01
                $ network create floating --cloud=india --pool=albert-f01
                $ network delete floating ip --cloud=india 192.1.66.8
                $ network delete floating --cloud=india 192.1.66.8
                $ network list floating ip --cloud=india
                $ network list floating --cloud=india
                $ network list floating --cloud=india 192.1.66.8
                $ network list floating --cloud=india --instance=323c5195-7yy34-4e7b-8581-703abec4b
                $ network list floating pool --cloud=india

        """
        # pprint(arguments)
        # Get the cloud parameter OR read default
        cloudname = arguments["--cloud"] or Default.get_cloud()

        if cloudname is None:
            Console.error("Default cloud has not been set!"
                          "Please use the following to set it:\n"
                          "cm default cloud=CLOUDNAME\n"
                          "or provide it via the --cloud=CLOUDNAME argument.")
            return ""

        # Fixed IP info
        if arguments["get"] \
                and arguments["fixed"]:
            fixed_ip = arguments["FIXED_IP"]
            result = Network.get_fixed_ip(cloudname,
                                          fixed_ip_addr=fixed_ip)
            Console.msg(result)

        # Floating IP info
        elif arguments["get"] \
                and arguments["floating"]:
            floating_ip_id = arguments["FLOATING_IP_ID"]
            result = Network.get_floating_ip(cloudname,
                                             floating_ip_or_id=floating_ip_id)
            Console.msg(result)

        # Reserve a fixed ip
        elif arguments["reserve"] \
                and arguments["fixed"]:
            fixed_ip = arguments["FIXED_IP"]
            result = Network.reserve_fixed_ip(cloudname=cloudname,
                                              fixed_ip_addr=fixed_ip)
            if result is not None:
                Console.ok("Reserve fixed ip address [{}] complete.".format(fixed_ip))

        # Un-Reserve a fixed ip
        elif arguments["unreserve"] \
                and arguments["fixed"]:
            fixed_ip = arguments["FIXED_IP"]
            result = Network.unreserve_fixed_ip(cloudname=cloudname,
                                                fixed_ip_addr=fixed_ip)
            if result is not None:
                Console.ok("Un-Reserve fixed ip address [{}] complete.".format(fixed_ip))

        # Associate floating IP
        elif arguments["associate"] \
                and arguments["floating"]:

            # Get all command-line arguments
            group_name = arguments["--group"]
            instance_id = arguments["--instance"]
            floating_ip = arguments["FLOATING_IP"]

            # group supplied
            if group_name is not None:
                """
                Group name has been provided.
                Assign floating IPs to all vms in the group
                and return
                """
                # Get the group information
                group = Group.get_info(name=group_name,
                                       cloud=cloudname,
                                       output="json")
                if group is not None:
                    # Convert from str to json
                    group = json.loads(group)
                    # For each vm in the group
                    # Create and assign a floating IP
                    for item in group:
                        instance_id = group[item]["value"]
                        # Get the instance dict
                        instance_dict = Network.get_instance_dict(cloudname=cloudname,
                                                                  instance_id=instance_id)
                        # Instance not found
                        if instance_dict is None:
                            Console.error("Instance [{}] not found in the cloudmesh database!"
                                          .format(instance_id))
                            return ""

                        # Get the instance name
                        instance_name = instance_dict["name"]
                        floating_ip = Network.create_assign_floating_ip(cloudname=cloudname,
                                                                        instance_name=instance_name)
                        if floating_ip is not None:
                            Console.ok("Created and assigned Floating IP [{}] to instance [{}]."
                                       .format(floating_ip, instance_name))
                            # Refresh VM in db
                            self.refresh_vm(cloudname)
                else:
                    Console.error("No group [{}] in the Cloudmesh database."
                                  .format(group_name))
                    return ""

            # floating-ip not supplied, instance-id supplied
            elif floating_ip is None and instance_id is not None:
                """
                Floating IP has not been provided, instance-id provided.
                Generate one from the pool, and assign to vm
                and return
                """
                instance_dict = Network.get_instance_dict(cloudname=cloudname,
                                                          instance_id=instance_id)
                # Instance not found
                if instance_dict is None:
                    Console.error("Instance [{}] not found in the cloudmesh database!"
                                  .format(instance_id))
                    return ""

                instance_name = instance_dict["name"]
                floating_ip = Network.create_assign_floating_ip(cloudname=cloudname,
                                                                instance_name=instance_name)
                if floating_ip is not None:
                    Console.ok("Created and assigned Floating IP [{}] to instance [{}]."
                               .format(floating_ip, instance_name))

            # instance-id & floating-ip supplied
            elif instance_id is not None:
                """
                Floating IP & Instance ID have been provided
                Associate the IP to the instance
                and return
                """
                instance_dict = Network.get_instance_dict(cloudname=cloudname,
                                                          instance_id=instance_id)
                # Instance not found
                if instance_dict is None:
                    Console.error("Instance [{}] not found in the cloudmesh database!"
                                  .format(instance_id))
                    return ""

                instance_name = instance_dict["name"]
                result = Network.associate_floating_ip(cloudname=cloudname,
                                                       instance_name=instance_name,
                                                       floating_ip=floating_ip)
                if result is not None:
                    Console.ok("Associated Floating IP [{}] to instance [{}]."
                               .format(floating_ip, instance_name))

            # Invalid parameters
            else:
                Console.error("Please provide at least one of [--group] OR [--instance] parameters.\n"
                              "You can also provide [FLOATING_IP] AND [--instance] parameters.\n"
                              "See 'cm network --help' for more info.")
                return ""

            # Refresh VM in db
            self.refresh_vm(cloudname)

        elif arguments["disassociate"] \
                and arguments["floating"]:

            # Get all command-line arguments
            group_name = arguments["--group"]
            instance_id = arguments["--instance"]
            floating_ip = arguments["FLOATING_IP"]

            # group supplied
            if group_name is not None:
                """
                Group name has been provided.
                Remove floating IPs of all vms in the group
                and return
                """
                # Get the group information
                group = Group.get_info(name=group_name,
                                       cloud=cloudname,
                                       output="json")
                if group is not None:
                    # Convert from str to json
                    group = json.loads(group)
                    # For each vm in the group
                    # Create and assign a floating IP
                    for item in group:
                        instance_id = group[item]["value"]
                        # Get the instance dict
                        instance_dict = Network.get_instance_dict(cloudname=cloudname,
                                                                  instance_id=instance_id)
                        # Instance not found
                        if instance_dict is None:
                            Console.error("Instance [{}] not found in the cloudmesh database!"
                                          .format(instance_id))
                            return ""

                        # Get the instance name
                        instance_name = instance_dict["name"]
                        floating_ip = instance_dict["floating_ip"]

                        # Floating ip argument invalid
                        if floating_ip is None:
                            Console.error("Instance[{}] does not have a floating_ip."
                                          .format(instance_name))
                            return ""

                        result = Network.disassociate_floating_ip(cloudname=cloudname,
                                                                  instance_name=instance_name,
                                                                  floating_ip=floating_ip)
                        if result is not None:
                            Console.ok("Disassociated Floating IP [{}] from instance [{}]."
                                       .format(floating_ip, instance_name))
                else:
                    Console.error("No group [{}] in the Cloudmesh database."
                                  .format(group_name))
                    return ""

            # floating-ip not supplied, instance-id supplied
            elif floating_ip is None and instance_id is not None:
                """
                Floating IP has not been provided, instance-id provided.
                Remove floating ip allocated to vm
                and return
                """
                instance_dict = Network.get_instance_dict(cloudname=cloudname,
                                                          instance_id=instance_id)
                # Instance not found
                if instance_dict is None:
                    Console.error("Instance [{}] not found in the cloudmesh database!"
                                  .format(instance_id))
                    return ""

                instance_name = instance_dict["name"]
                floating_ip = instance_dict["floating_ip"]

                # Floating ip argument invalid
                if floating_ip is None:
                    Console.error("Instance[{}] does not have a floating_ip."
                                  .format(instance_name))
                    return ""

                result = Network.disassociate_floating_ip(cloudname=cloudname,
                                                          instance_name=instance_name,
                                                          floating_ip=floating_ip)
                if result is not None:
                    Console.ok("Disassociated Floating IP [{}] from instance [{}]."
                               .format(floating_ip, instance_name))

            # instance-id & floating-ip supplied
            elif instance_id is not None:
                """
                Floating IP & Instance ID have been provided
                Remove the IP from the instance
                and return
                """
                instance_dict = Network.get_instance_dict(cloudname=cloudname,
                                                          instance_id=instance_id)
                # Instance not found
                if instance_dict is None:
                    Console.error("Instance [{}] not found in the cloudmesh database!"
                                  .format(instance_id))
                    return ""

                instance_name = instance_dict["name"]
                _floating_ip = instance_dict["floating_ip"]

                # Floating ip argument invalid
                if _floating_ip != floating_ip:
                    Console.error("Invalid floating_ip [{}] for instance [{}]."
                                  .format(floating_ip, instance_name))
                    return ""

                result = Network.disassociate_floating_ip(cloudname=cloudname,
                                                          instance_name=instance_name,
                                                          floating_ip=floating_ip)
                if result is not None:
                    Console.ok("Disassociated Floating IP [{}] from instance [{}]."
                               .format(floating_ip, instance_name))

            # Invalid parameters
            else:
                Console.error("Please provide at least one of [--group] OR [--instance] parameters.\n"
                              "You can also provide [FLOATING_IP] AND [--instance] parameters.\n"
                              "See 'cm network --help' for more info.")
                return ""

            # Refresh VM in db
            self.refresh_vm(cloudname)

        # Create new floating ip under floating pool
        elif arguments["create"] \
                and arguments["floating"]:
            floating_pool = arguments["--pool"]
            result = Network.create_floating_ip(cloudname=cloudname,
                                                floating_pool=floating_pool)
            if result is not None:
                Console.ok("Created new floating IP [{}]".format(result))
            else:
                Console.error("Failed to create floating IP! Please check arguments.")

        # Delete a floating ip address
        elif arguments["delete"] \
                and arguments["floating"]:
            floating_ip = arguments["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!")

        # Floating IP Pool List
        elif arguments["list"] \
                and arguments["floating"] \
                and arguments["pool"]:
            result = Network.list_floating_ip_pool(cloudname)
            Console.msg(result)

        # Floating IP list [or info]
        elif arguments["list"] \
                and arguments["floating"]:

            ip_or_id = arguments["IP_OR_ID"]
            instance_id = arguments["--instance"]

            # Refresh VM in db
            self.refresh_vm(cloudname)

            # If instance id is supplied
            if instance_id is not None:
                instance_dict = Network.get_instance_dict(cloudname=cloudname,
                                                          instance_id=instance_id)
                # Instance not found
                if instance_dict is None:
                    Console.error("Instance [{}] not found in the cloudmesh database!"
                                  .format(instance_id))
                    return ""

                # Read the floating_ip from the dict
                ip_or_id = instance_dict["floating_ip"]

                if ip_or_id is None:
                    Console.error("Instance with ID [{}] does not have a floating IP address!"
                                  .format(instance_id))
                    return ""

            # If the floating ip or associated ID is supplied
            if ip_or_id is not None:
                result = Network.get_floating_ip(cloudname,
                                                 floating_ip_or_id=ip_or_id)

                if result is not None:
                    Console.msg(result)
                else:
                    Console.error("Floating IP not found! Please check your arguments.")
                    return ""
            # Retrieve the full list
            else:
                result = Network.list_floating_ip(cloudname)
                Console.msg(result)

        return ""
示例#25
0
    def sync(cls, cloudname, localdir, remotedir, operation=None):
        """
        Syncs a local directory with a remote directory.
        Either from local to remote OR vice-versa
        :param cloudname:
        :param localdir:
        :param remotedir:
        :param operation: get/put
        :return:
        """
        # Get the operating system
        os_type = cls.operating_system()

        # fix the local dir path
        localdir = Config.path_expand(localdir)

        # check if local directory exists
        if not os.path.exists(localdir):
            if operation == "put":
                Console.error("The local directory [{}] does not exist."
                              .format(localdir))
                return None
            elif operation == "get":
                # for receiving, create local dir
                os.mkdir(localdir)
                Console.msg("Created local directory [{}] for sync."
                            .format(localdir))

        # sync entire local directory
        elif os.path.isdir(localdir):
            if operation == "put":
                localdir += "/*"
            elif operation == "get":
                localdir += "/"

        # for windows use pscp
        # rsync has issues with latest win10
        if 'windows' in os_type:
            ppk_file = ''
            while ppk_file == '':
                ppk_file = raw_input("Please enter putty private key(ppk) "
                                     "file path: ")
                # expand the path
                ppk_file = Config.path_expand(ppk_file)
                pass

            host = cls.get_host(cloudname)
            if host is None:
                Console.error("Cloud [{}] not found in cloudmesh.yaml file."
                              .format(cloudname))
                return None
            else:
                # Get the hostname and user of remote host
                hostname = cls.get_hostname(host)
                user = cls.get_hostuser(host)

                # Console.msg("Syncing local dir [{}] with remote host [{}]"
                #            .format(localdir, user + "@" + hostname))
                args = None
                if operation == "put":
                    # Construct the arguments
                    # local dir comes first (send)
                    args = [
                        "-i",
                        ppk_file,
                        localdir,
                        user + "@" + hostname + ":" + remotedir
                    ]
                elif operation == "get":
                    # Construct the arguments
                    # remote dir comes first (receive)
                    args = [
                        "-i",
                        ppk_file,
                        user + "@" + hostname + ":" + remotedir,
                        localdir
                    ]

                try:
                    # Convert command to string
                    cmd = " ".join(["pscp"] + args)
                    result = os.system(cmd)
                    if result != 0:
                        Console.error("Something went wrong. Please try again later.")
                        return None
                    else:
                        return "Success."
                except Exception as ex:
                    print(ex, ex.message)

        # for linux/mac machines use rsync
        else:
            host = cls.get_host(cloudname)
            if host is None:
                Console.error("Cloud [{}] not found in cloudmesh.yaml file."
                              .format(cloudname))
                return None
            else:
                args = None
                if operation == "put":
                    args = [
                        localdir,
                        host + ":" + remotedir
                    ]
                elif operation == "get":
                    args = [
                        host + ":" + remotedir,
                        localdir
                    ]
                # call rsync
                return Shell.rsync(*args)
示例#26
0
    def do_cloud(self, args, arguments):
        """
        ::

          Usage:
              cloud list [--cloud=CLOUD] [--format=FORMAT]
              cloud logon CLOUD
              cloud logout CLOUD
              cloud activate CLOUD
              cloud deactivate CLOUD
              cloud info CLOUD

          managing the admins test test test test

          Arguments:
            KEY    the name of the admin
            VALUE  the value to set the key to

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

          Description:
             Cloudmesh contains a cloudmesh.yaml file that contains
             templates for multiple clouds that you may or may not have
             access to. Hence it is useful to activate and deactivate clouds
             you like to use in other commands.

             To activate a cloud a user can simply use the activate
             command followed by the name of the cloud to be
             activated. To find out which clouds are available you can
             use the list command that will provide you with some
             basic information. As default it will print a table. Thus
             the commands::

               cloud activate india
               cloud deactivate aws

             Will result in

                +----------------------+--------+-------------------+
                | Cloud name           | Active | Type              |
                +----------------------+--------+-------------------+
                | india                | True   | Openstack         |
                +----------------------+--------+-------------------+
                | aws                  | False  | AWS               |
                +----------------------+--------+-------------------+

             To get ore information about the cloud you can use the command

                cloud info CLOUD

             It will call internally also the command uses in register

          See also:
             register
        """

        cloudname = arguments["--cloud"] or Default.cloud

        if arguments["logon"]:
            cloudname = arguments["CLOUD"]
            provider = CloudProvider(cloudname).provider
            provider.logon(cloudname)
            Console.ok("Logged into cloud: " + cloudname)

        elif arguments["logout"]:
            cloudname = arguments["CLOUD"]
            provider = CloudProvider(cloudname).provider
            provider.logout(cloudname)
            Console.ok("Logged out of cloud: " + cloudname)

        elif arguments["activate"]:
            cloudname = arguments["CLOUD"]
            provider = CloudProvider(cloudname).provider
            provider.activate(cloudname)
            Console.ok("Activated cloud: " + cloudname)

        elif arguments["deactivate"]:
            cloudname = arguments["CLOUD"]
            provider = CloudProvider(cloudname).provider
            provider.deactivate(cloudname)
            Console.ok("Deactivated cloud: " + cloudname)

        elif arguments["list"]:
            provider = CloudProvider(cloudname).provider
            clouds = provider.list_clouds()
            default = Default.cloud
            active = ConfigDict("cloudmesh.yaml")["cloudmesh"]["active"]
            key = Default.key

            def yes(state):
                if state:
                    return "*"
                else:
                    return " "

            for index in clouds:
                cloud = clouds[index]
                cloud["active"] = yes(cloud["cloud"] in active)
                cloud["default"] = yes(cloud["cloud"] == default)
                cloud["key"] = key
                cloud["id"] = index

            (order, header) = CloudProvider(cloudname).get_attributes("clouds")

            Console.msg(Printer.write(clouds, order=order, header=header))
        return ""
示例#27
0
    def do_workflow(self, args, arguments):
        """
        ::

            Usage:
                workflow refresh [--cloud=CLOUD] [-v]
                workflow list [ID] [NAME] [--cloud=CLOUD] [--format=FORMAT] [--refresh] [-v]
                workflow add NAME LOCATION
                workflow delete ID
                workflow status [NAMES]
                workflow show ID
                workflow save NAME WORKFLOWSTR
                workflow run NAME
                workflow service start
                workflow service stop
                This lists out the workflows 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 workflow refresh
                cm workflow list
                cm workflow list --format=csv
                cm workflow show 58c9552c-8d93-42c0-9dea-5f48d90a3188 --refresh
                cm workflow run workflow1

        """

        arg = dotdict(arguments)
        if arg.NAMES is not None:
            arg.names = Parameter.expand(arg.NAMES)
        else:
            arg.names = None
        pprint(arg)

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

        if arguments["-v"]:
            print("Cloud: {}".format(cloud))

        if arguments["refresh"] or Default.refresh:
            msg = "Refresh workflow for cloud {:}.".format(cloud)
            if Workflow.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"]

            result = None
            if id is None:
                result = Workflow.list(cloud, output_format)
            else:
                result = Workflow.details(cloud, id, live, output_format)

            if result is None:
                Console.error("No workflow(s) found. Failed.")
            else:
                print(result)
            return ""

        elif arguments["show"]:
            workflow_id = arguments["ID"]
            Console.ok("I executed show")
            # Console.msg(workflow_id)
            if workflow_id is None:
                Console.msg("Please enter a Workflow Id to execute workflow")
            else:
                result = Workflow.run(cloud, workflow_id)
                # print (result)
                if result is None:
                    Console.msg(
                        "Use workflow list to view existing workflows or do workflow save to save a new one"
                    )
                else:
                    # Console.msg(result)
                    # Console.msg(result[0])
                    Console.msg(result[0]['workflow_str'])
                    # entry_point(result[0]['workflow_str'])
                    Console.msg("All Set to execute")

        elif arguments["save"]:
            workflow_name = arguments["NAME"]
            workflow_str = arguments["WORKFLOWSTR"]

            result = Workflow.save(cloud=cloud,
                                   name=workflow_name,
                                   str=workflow_str)
            if result is not None:
                Console.ok(result)
            else:
                Console.error("Failed to save workflow!")
        elif arguments["run"]:
            Console.msg("Execute Run")
            workflow_name = arguments["NAME"]
            # cm_id = arguments["ID"]

            result = Workflow.run(cloud, name=workflow_name)

        elif arguments["delete"]:
            workflow_id = arguments["ID"]

            result = Workflow.delete(cloud, workflow_id)
            if result is not None:
                Console.msg(result)
            else:
                Console.error("Failed to save workflow!")
        elif arguments["service"] and arguments["start"]:
            # Console.msg()
            # mkdir -pf ~/.cloudmesh/workflow
            pass
示例#28
0
 def _print(msg):
     if arg.verbose:
         Console.msg(msg)
示例#29
0
    def do_network(self, args, arguments):
        """
        ::

            Usage:
                network get fixed [ip] [--cloud=CLOUD] FIXED_IP
                network get floating [ip] [--cloud=CLOUD] FLOATING_IP_ID
                network reserve fixed [ip] [--cloud=CLOUD] FIXED_IP
                network unreserve fixed [ip] [--cloud=CLOUD] FIXED_IP
                network associate floating [ip] [--cloud=CLOUD] [--group=GROUP]
                                           [--instance=INS_ID_OR_NAME] [FLOATING_IP]
                network disassociate floating [ip] [--cloud=CLOUD] [--group=GROUP]
                                              [--instance=INS_ID_OR_NAME] [FLOATING_IP]
                network create floating [ip] [--cloud=CLOUD] [--pool=FLOATING_IP_POOL]
                network delete floating [ip] [--cloud=CLOUD] [--unused] [FLOATING_IP]
                network list floating pool [--cloud=CLOUD]
                network list floating [ip] [--cloud=CLOUD] [--unused] [--instance=INS_ID_OR_NAME] [IP_OR_ID]
                network create cluster --group=demo_group
                network -h | --help

            Options:
                -h                          help message
                --unused                    unused floating ips
                --cloud=CLOUD               Name of the IaaS cloud e.g. india_openstack_grizzly.
                --group=GROUP               Name of the group in Cloudmesh
                --pool=FLOATING_IP_POOL     Name of Floating IP Pool
                --instance=INS_ID_OR_NAME   ID or Name of the vm instance

            Arguments:
                IP_OR_ID        IP Address or ID of IP Address
                FIXED_IP        Fixed IP Address, e.g. 10.1.5.2
                FLOATING_IP     Floating IP Address, e.g. 192.1.66.8
                FLOATING_IP_ID  ID associated with Floating IP, e.g. 185c5195-e824-4e7b-8581-703abec4bc01

            Examples:
                network get fixed ip --cloud=india 10.1.2.5
                network get fixed --cloud=india 10.1.2.5
                network get floating ip --cloud=india 185c5195-e824-4e7b-8581-703abec4bc01
                network get floating --cloud=india 185c5195-e824-4e7b-8581-703abec4bc01
                network reserve fixed ip --cloud=india 10.1.2.5
                network reserve fixed --cloud=india 10.1.2.5
                network unreserve fixed ip --cloud=india 10.1.2.5
                network unreserve fixed --cloud=india 10.1.2.5
                network associate floating ip --cloud=india --instance=albert-001 192.1.66.8
                network associate floating --cloud=india --instance=albert-001
                network associate floating --cloud=india --group=albert_group
                network disassociate floating ip --cloud=india --instance=albert-001 192.1.66.8
                network disassociate floating --cloud=india --instance=albert-001 192.1.66.8
                network create floating ip --cloud=india --pool=albert-f01
                network create floating --cloud=india --pool=albert-f01
                network delete floating ip --cloud=india 192.1.66.8 192.1.66.9
                network delete floating --cloud=india 192.1.66.8 192.1.66.9
                network list floating ip --cloud=india
                network list floating --cloud=india
                network list floating --cloud=india --unused
                network list floating --cloud=india 192.1.66.8
                network list floating --cloud=india --instance=323c5195-7yy34-4e7b-8581-703abec4b
                network list floating pool --cloud=india
                network create cluster --group=demo_group

        """
        # pprint(arguments)
        # Get the cloud parameter OR read default
        cloudname = arguments["--cloud"] or Default.cloud

        if cloudname is None:
            Console.error("Default cloud has not been set!"
                          "Please use the following to set it:\n"
                          "cm default cloud=CLOUDNAME\n"
                          "or provide it via the --cloud=CLOUDNAME argument.")
            return ""

        # Fixed IP info
        if arguments["get"] \
                and arguments["fixed"]:
            fixed_ip = arguments["FIXED_IP"]
            result = Network.get_fixed_ip(cloudname,
                                          fixed_ip_addr=fixed_ip)
            Console.msg(result)

        # Floating IP info
        elif arguments["get"] \
                and arguments["floating"]:
            floating_ip_id = arguments["FLOATING_IP_ID"]
            result = Network.get_floating_ip(cloudname,
                                             floating_ip_or_id=floating_ip_id)
            Console.msg(result)

        # Reserve a fixed ip
        elif arguments["reserve"] \
                and arguments["fixed"]:
            fixed_ip = arguments["FIXED_IP"]
            result = Network.reserve_fixed_ip(cloudname=cloudname,
                                              fixed_ip_addr=fixed_ip)
            if result is not None:
                Console.ok("Reserve fixed ip address {} complete.".format(fixed_ip))

        # Un-Reserve a fixed ip
        elif arguments["unreserve"] \
                and arguments["fixed"]:
            fixed_ip = arguments["FIXED_IP"]
            result = Network.unreserve_fixed_ip(cloudname=cloudname,
                                                fixed_ip_addr=fixed_ip)
            if result is not None:
                Console.ok("Un-Reserve fixed ip address {} complete.".format(fixed_ip))

        # Associate floating IP
        elif arguments["associate"] \
                and arguments["floating"]:

            # Get all command-line arguments
            group_name = arguments["--group"]
            instance_id = arguments["--instance"]
            floating_ip = arguments["FLOATING_IP"]

            # group supplied
            if group_name is not None:
                """
                Group name has been provided.
                Assign floating IPs to all vms in the group
                and return
                """
                # Get the group information
                group = Group.get_info(name=group_name,
                                       category=cloudname,
                                       output="json")
                if group is not None:
                    # Convert from str to json
                    group = json.loads(group)
                    # For each vm in the group
                    # Create and assign a floating IP
                    for item in group:
                        instance_id = group[item]["value"]
                        floating_ip = Network.find_assign_floating_ip(cloudname=cloudname,
                                                                      instance_id=instance_id)
                        if floating_ip is not None:
                            Console.ok("Created and assigned Floating IP {} to instance {}."
                                       .format(floating_ip, instance_id))
                            # Refresh VM in db
                            self.refresh_vm(cloudname)
                else:
                    Console.error("No group {} in the Cloudmesh database."
                                  .format(group_name))
                    return ""

            # floating-ip not supplied, instance-id supplied
            elif not floating_ip and instance_id is not None:
                """
                Floating IP has not been provided, instance-id provided.
                Generate one from the pool, and assign to vm
                and return
                """
                floating_ip = Network.find_assign_floating_ip(cloudname=cloudname,
                                                              instance_id=instance_id)
                if floating_ip is not None:
                    Console.ok("Associated floating IP {} to instance {}."
                               .format(floating_ip, instance_id))

            # instance-id & floating-ip supplied
            elif instance_id is not None:
                """
                Floating IP & Instance ID have been provided
                Associate the IP to the instance
                and return
                """
                Network.find_assign_floating_ip(cloudname=cloudname,
                                                instance_id=instance_id,
                                                floating_ip=floating_ip[0])

            # Invalid parameters
            else:
                Console.error("Please provide at least one of [--group] OR [--instance] parameters.\n"
                              "You can also provide [FLOATING_IP] AND [--instance] parameters.\n"
                              "See 'cm network --help' for more info.")
                return ""

            # Refresh VM in db
            self.refresh_vm(cloudname)

        elif arguments["disassociate"] \
                and arguments["floating"]:

            # Get all command-line arguments
            group_name = arguments["--group"]
            instance_id = arguments["--instance"]
            floating_ip = arguments["FLOATING_IP"]

            # group supplied
            if group_name is not None:
                """
                Group name has been provided.
                Remove floating IPs of all vms in the group
                and return
                """
                # Get the group information
                group = Group.get_info(name=group_name,
                                       category=cloudname,
                                       output="json")
                if group is not None:
                    # Convert from str to json
                    group = json.loads(group)
                    # For each vm in the group
                    # Create and assign a floating IP
                    for item in group:
                        instance_id = group[item]["value"]
                        # Get the instance dict
                        instance_dict = Network.get_instance_dict(cloudname=cloudname,
                                                                  instance_id=instance_id)
                        # Instance not found
                        if instance_dict is None:
                            Console.error("Instance {} not found in the cloudmesh database!"
                                          .format(instance_id))
                            return ""

                        # Get the instance name
                        instance_name = instance_dict["name"]
                        floating_ip = instance_dict["floating_ip"]

                        # Floating ip argument invalid
                        if floating_ip is None:
                            Console.error("Instance{} does not have a floating_ip."
                                          .format(instance_name))
                            return ""

                        result = Network.disassociate_floating_ip(cloudname=cloudname,
                                                                  instance_name=instance_name,
                                                                  floating_ip=floating_ip)
                        if result is not None:
                            Console.ok("Disassociated Floating IP {} from instance {}."
                                       .format(floating_ip, instance_name))
                else:
                    Console.error("No group {} in the Cloudmesh database."
                                  .format(group_name))
                    return ""

            # floating-ip not supplied, instance-id supplied
            elif len(floating_ip) == 0 and instance_id is not None:
                """
                Floating IP has not been provided, instance-id provided.
                Remove floating ip allocated to vm
                and return
                """
                instance_dict = Network.get_instance_dict(cloudname=cloudname,
                                                          instance_id=instance_id)
                # Instance not found
                if instance_dict is None:
                    Console.error("Instance {} not found in the cloudmesh database!"
                                  .format(instance_id))
                    return ""

                instance_name = instance_dict["name"]
                floating_ip = instance_dict["floating_ip"]

                # Floating ip argument invalid
                if floating_ip is None:
                    Console.error("Instance{} does not have a floating_ip."
                                  .format(instance_name))
                    return ""

                result = Network.disassociate_floating_ip(cloudname=cloudname,
                                                          instance_name=instance_name,
                                                          floating_ip=floating_ip)
                if result is not None:
                    Console.ok("Disassociated Floating IP {} from instance {}."
                               .format(floating_ip, instance_name))

            # instance-id & floating-ip supplied
            elif instance_id is not None:
                """
                Floating IP & Instance ID have been provided
                Remove the IP from the instance
                and return
                """
                instance_dict = Network.get_instance_dict(cloudname=cloudname,
                                                          instance_id=instance_id)
                floating_ip = floating_ip[0]

                # Instance not found
                if instance_dict is None:
                    Console.error("Instance {} not found in the cloudmesh database!"
                                  .format(instance_id))
                    return ""

                instance_name = instance_dict["name"]
                _floating_ip = instance_dict["floating_ip"]

                # Floating ip argument invalid
                if _floating_ip != floating_ip:
                    Console.error("Invalid floating_ip {} for instance {}."
                                  .format(floating_ip, instance_name))
                    return ""

                result = Network.disassociate_floating_ip(cloudname=cloudname,
                                                          instance_name=instance_name,
                                                          floating_ip=floating_ip)
                if result is not None:
                    Console.ok("Disassociated Floating IP {} from instance {}."
                               .format(floating_ip, instance_name))

            # Invalid parameters
            else:
                Console.error("Please provide at least one of [--group] OR [--instance] parameters.\n"
                              "You can also provide [FLOATING_IP] AND [--instance] parameters.\n"
                              "See 'cm network --help' for more info.")
                return ""

            # Refresh VM in db
            self.refresh_vm(cloudname)

        # Create new floating ip under floating pool
        elif arguments["create"] \
                and arguments["floating"]:
            floating_pool = arguments["--pool"]
            result = Network.create_floating_ip(cloudname=cloudname,
                                                floating_pool=floating_pool)
            if result is not None:
                Console.ok("Created new floating IP {}".format(result))
            else:
                Console.error("Failed to create floating IP! Please check arguments.")

        # Delete a floating ip address
        elif arguments["delete"] \
                and arguments["floating"]:

            # delete all unused floating ips
            if arguments["--unused"]:
                unused_floating_ips = Network.get_unused_floating_ip_list(cloudname=cloudname)
                if unused_floating_ips:
                    for floating_ip in unused_floating_ips:
                        self._delete_floating_ip(cloudname=cloudname,
                                                 floating_ip=floating_ip["id"])
                else:
                    Console.msg("No unused floating ips exist at this moment. Ok.")
                return ""

            # delete specified floating ips
            floating_ips = Parameter.expand(arguments["FLOATING_IP"])

            for floating_ip in floating_ips:
                self._delete_floating_ip(cloudname=cloudname,
                                         floating_ip=floating_ip)

        # Floating IP Pool List
        elif arguments["list"] \
                and arguments["floating"] \
                and arguments["pool"]:
            result = Network.list_floating_ip_pool(cloudname)
            Console.msg(result)

        # Floating IP list [or info]
        elif arguments["list"] \
                and arguments["floating"]:

            ip_or_id = arguments["IP_OR_ID"]
            instance_id = arguments["--instance"]

            # List unused floating addr
            if arguments["--unused"]:
                result = Network.list_unused_floating_ip(cloudname=cloudname)
                Console.msg(result)
                return ""

            # Refresh VM in db
            self.refresh_vm(cloudname)

            # If instance id is supplied
            if instance_id is not None:
                instance_dict = Network.get_instance_dict(cloudname=cloudname,
                                                          instance_id=instance_id)
                # Instance not found
                if instance_dict is None:
                    Console.error("Instance {} not found in the cloudmesh database!"
                                  .format(instance_id))
                    return ""

                # Read the floating_ip from the dict
                ip_or_id = instance_dict["floating_ip"]

                if ip_or_id is None:
                    Console.error("Instance with ID {} does not have a floating IP address!"
                                  .format(instance_id))
                    return ""

            # If the floating ip or associated ID is supplied
            if ip_or_id is not None:
                result = Network.get_floating_ip(cloudname,
                                                 floating_ip_or_id=ip_or_id)

                if result is not None:
                    Console.msg(result)
                else:
                    Console.error("Floating IP not found! Please check your arguments.")
                    return ""
            # Retrieve the full list
            else:
                result = Network.list_floating_ip(cloudname)
                Console.msg(result)

        # Create a virtual cluster
        elif arguments["cluster"] and \
                arguments["create"]:

            group_name = arguments["--group"] or \
                         Default.get(name="group", category=cloudname)

            # Get the group information
            group = Group.get_info(name=group_name,
                                   category=cloudname,
                                   output="json")
            if group is not None:
                # Convert from str to json
                group = json.loads(group)

                # var contains pub key of all vms
                public_keys = ""
                login_users = []
                login_ips = []

                # For each vm in the group
                # Create and assign a floating IP
                for item in group:
                    instance_id = group[item]["value"]
                    # Get the instance dict
                    instance_dict = Network.get_instance_dict(cloudname=cloudname,
                                                              instance_id=instance_id)
                    # Instance not found
                    if instance_dict is None:
                        Console.error("Instance {} not found in the cloudmesh database!"
                                      .format(instance_id))
                        return ""

                    # Get the instance name
                    instance_name = instance_dict["name"]
                    floating_ip = instance_dict["floating_ip"]

                    # If vm does not have floating ip, then create
                    if floating_ip is None:
                        floating_ip = Network.create_assign_floating_ip(cloudname=cloudname,
                                                                        instance_name=instance_name)
                        if floating_ip is not None:
                            Console.ok("Created and assigned Floating IP {} to instance {}."
                                       .format(floating_ip, instance_name))
                            # Refresh VM in db
                            self.refresh_vm(cloudname)

                    # Get the login user for this machine
                    user = input("Enter the login user for VM {} : ".format(instance_name))
                    passphrase = getpass.getpass("Enter the passphrase key on VM {} : ".format(instance_name))

                    # create list for second iteration
                    login_users.append(user)
                    login_ips.append(floating_ip)

                    login_args = [
                        user + "@" + floating_ip,
                    ]

                    keygen_args = [
                        "ssh-keygen -t rsa -f ~/.ssh/id_rsa -N " + passphrase
                    ]

                    cat_pubkey_args = [
                        "cat ~/.ssh/id_rsa.pub"
                    ]

                    generate_keypair = login_args + keygen_args
                    result = Shell.ssh(*generate_keypair)

                    # print("***** Keygen *****")
                    # print(result)

                    cat_public_key = login_args + cat_pubkey_args
                    result = Shell.ssh(*cat_public_key)
                    public_keys += "\n" + result

                    # print("***** id_rsa.pub *****")
                    # print(result)

                # print("***** public keys *****")
                # print(public_keys)

                for user, ip in zip(login_users, login_ips):
                    arguments = [
                        user + "@" + ip,
                        "echo '" + public_keys + "' >> ~/.ssh/authorized_keys"
                    ]

                    # copy the public key contents to auth_keys
                    result = Shell.ssh(*arguments)

                Console.ok("Virtual cluster creation successfull.")
            else:
                Console.error("No group {} in the Cloudmesh database."
                              .format(group_name))
                return ""

        return ""
示例#30
0
    def do_hpc(self, args, arguments):
        # noinspection PyPep8
        """
                ::

                    Usage:
                        hpc queue [--job=NAME][--cluster=CLUSTER][--format=FORMAT]
                        hpc info [--cluster=CLUSTER][--format=FORMAT]
                        hpc run SCRIPT [--queue=QUEUE] [--t=TIME] [--N=nodes] [--name=NAME] [--cluster=CLUSTER][--dir=DIR][--group=GROUP][--format=FORMAT]
                        hpc delete --job=NAME [--cluster=CLUSTER][--group=GROUP]
                        hpc delete all [--cluster=CLUSTER][--group=GROUP][--format=FORMAT]
                        hpc status [--job=name] [--cluster=CLUSTER] [--group=GROUP]
                        hpc test --cluster=CLUSTER [--time=SECONDS]

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

                    Examples:

                        Special notes

                           if the group is specified only jobs from that group are
                           considered. Otherwise the default group is used. If the
                           group is set to None, all groups are used.

                        cm hpc queue
                            lists the details of the queues of the hpc cluster

                        cm hpc queue --name=NAME
                            lists the details of the job in the queue of the hpc cluster

                        cm hpc info
                            lists the details of the hpc cluster

                        cm hpc run SCRIPT
                            submits the script to the cluster. The script will be
                            copied prior to execution into the home directory on the
                            remote machine. If a DIR is specified it will be copied
                            into that dir.
                            The name of the script is either specified in the script
                            itself, or if not the default nameing scheme of
                            cloudmesh is used using the same index incremented name
                            as in vms fro clouds: cloudmeshusername-index

                        cm hpc delete all
                            kills all jobs on the default hpc cluster

                        cm hpc delete all -cluster=all
                            kills all jobs on all clusters

                        cm hpc delete --job=NAME
                            kills a job with a given name or job id

                        cm hpc default cluster=NAME
                            sets the default hpc cluster

                        cm hpc status
                            returns the status of all jobs

                        cm hpc status job=ID
                            returns the status of the named job

                        cm hpc test --cluster=CLUSTER --time=SECONDS
                            submits a simple test job to the named cluster and returns
                            if the job could be successfully executed. This is a
                            blocking call and may take a long time to complete
                            dependent on if the queuing system of that cluster is
                            busy. It will only use one node/core and print the message

                            #CLOUDMESH: Test ok

                            in that is being looked for to identify if the test is
                            successful. If time is used, the job is terminated
                            after the time is elapsed.

                    Examples:
                        cm hpc queue
                        cm hpc queue --job=xxx
                        cm hpc info
                        cm hpc delete --job=6
                        cm hpc run uname
                """

        format = arguments['--format']
        cluster = arguments['--cluster'] or Default.get_cluster()

        print ("CCC", cluster)

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

        batch = BatchProvider(cluster)

        if arguments["queue"]:
            name = arguments['--job']
            result = batch.queue(cluster, format=format, job=name)
            Console.msg(result)

        elif arguments["info"]:
            Console.msg(batch.info(cluster, format))

        elif arguments["delete"]:
            job = arguments['--job']
            Console.ok(batch.kill(cluster, job))

        elif arguments["status"]:
            name = arguments['--job']
            result = batch.queue(cluster, format=format, job=name)
            Console.msg(result)

        elif arguments["run"]:
            queue = arguments['--queue'] or Default.get('queue')
            # if not queue:
            #    Console.error('set default queue using: default queue=<value>')
            #    return

            script = arguments['SCRIPT']
            arg_dict = {
                '-name': arguments['--name'],
                '-p': queue,
                '-t': arguments['--t'],
                '-N': arguments['--N']
            }

            result = batch.run(cluster, script, **arg_dict)
            Console.ok("Started batch job {id} on {cluster}".format(**result))

        elif arguments["test"]:
            time_secs = arguments['--time']
            if time_secs:
                time = '00:00:' + time_secs
            else:
                time = '00:00:10'  # give a  default time of 10 secs
            print(batch.test(cluster, time))

        return ""
示例#31
0
    def do_hpc(self, args, arguments):
        # noinspection PyPep8
        """
                ::

                    Usage:
                        hpc queue [--job=NAME][--cluster=CLUSTER][--format=FORMAT]
                        hpc info [--cluster=CLUSTER][--format=FORMAT]
                        hpc run list [ID] [--cluster=CLUSTER]
                        hpc run output [ID] [--cluster=CLUSTER]
                        hpc run rm [ID] [--cluster=CLUSTER]
                        hpc run SCRIPT [--queue=QUEUE] [--t=TIME] [--N=nodes] [--name=NAME] [--cluster=CLUSTER][--dir=DIR][--group=GROUP][--format=FORMAT]
                        hpc delete --job=NAME [--cluster=CLUSTER][--group=GROUP]
                        hpc delete all [--cluster=CLUSTER][--group=GROUP][--format=FORMAT]
                        hpc status [--job=name] [--cluster=CLUSTER] [--group=GROUP]
                        hpc test --cluster=CLUSTER [--time=SECONDS]

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

                    Examples:

                        Special notes

                           if the group is specified only jobs from that group are
                           considered. Otherwise the default group is used. If the
                           group is set to None, all groups are used.

                        cm hpc queue
                            lists the details of the queues of the hpc cluster

                        cm hpc queue --job=NAME
                            lists the details of the job in the queue of the hpc cluster

                        cm hpc info
                            lists the details of the hpc cluster

                        cm hpc run SCRIPT
                            submits the script to the cluster. The script will be
                            copied prior to execution into the home directory on the
                            remote machine. If a DIR is specified it will be copied
                            into that dir.
                            The name of the script is either specified in the script
                            itself, or if not the default naming scheme of
                            cloudmesh is used using the same index incremented name
                            as in vms fro clouds: cloudmes husername-index

                        cm hpc delete all
                            kills all jobs on the default hpc group

                        cm hpc delete --job=NAME
                            kills a job with a given name or job id

                        cm default cluster=NAME
                            sets the default hpc cluster

                        cm hpc status
                            returns the status of all jobs

                        cm hpc status job=ID
                            returns the status of the named job

                        cm hpc test --cluster=CLUSTER --time=SECONDS
                            submits a simple test job to the named cluster and returns
                            if the job could be successfully executed. This is a
                            blocking call and may take a long time to complete
                            dependent on if the queuing system of that cluster is
                            busy. It will only use one node/core and print the message

                            #CLOUDMESH: Test ok

                            in that is being looked for to identify if the test is
                            successful. If time is used, the job is terminated
                            after the time is elapsed.

                    Examples:
                        cm hpc queue
                        cm hpc queue --job=xxx
                        cm hpc info
                        cm hpc delete --job=6
                        cm hpc delete all
                        cm hpc status
                        cm hpc status --job=6
                        cm hpc run uname
                        cm hpc run ~/test.sh --cluster=india
                """

        format = arguments['--format']
        cluster = arguments['--cluster'] or Default.get_cluster()
        arguments["CLUSTER"] = cluster

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

        batch = BatchProvider(cluster)

        if arguments["queue"]:
            name = arguments['--job']
            result = batch.queue(cluster, format=format, job=name)
            Console.msg(result)

        elif arguments["info"]:
            Console.msg(batch.info(cluster, format))

        elif arguments['delete'] and arguments['all']:
            group = arguments['--group'] or Default.get('group')
            if group is None:
                Console.error('set default group using: default group=<value> --cloud=general')
                return
            Console.ok(batch.delete(cluster, None, group))

        elif arguments["delete"]:
            job = arguments['--job']
            Console.ok(batch.delete(cluster, job))

        elif arguments["status"]:
            name = arguments['--job']
            result = batch.queue(cluster, format=format, job=name)
            Console.msg(result)

        elif arguments["test"]:
            time_secs = arguments['--time']
            if time_secs:
                time = '00:00:' + time_secs
            else:
                time = '00:00:10'  # give a  default time of 10 secs
            print(batch.test(cluster, time))

        elif arguments["run"] and arguments["list"]:
            # hpc experiment list [--cluster=CLUSTER]
            if arguments["ID"]:
                print ("# List of experiment {ID} on Cluster {CLUSTER}".format(**arguments))
                result = Experiment.list(cluster, id=arguments["ID"], format="list")
                if result is not None:
                    print ("\n".join(result))
                else:
                    Console.error("Could not find experiment {ID} on {CLUSTER}".format(**arguments))
            else:
                print ("# List of experiments on Cluster {CLUSTER}".format(**arguments))
                ids = Experiment.list(cluster, id=None, format="list")
                if ids is not None:
                    print (", ".join([str(i) for i in ids]))
                else:
                    Console.error("Could not find experiment {ID} on {CLUSTER}".format(**arguments))

        elif arguments["run"] and arguments["rm"]:
            # hpc experiment list [--cluster=CLUSTER]
            if arguments["ID"]:

                force = yn_choice("Would you like to delete experiment {ID} on Cluster {CLUSTER}".format(**arguments))
                if force:
                    try:
                        result = Experiment.rm(cluster, id=arguments["ID"])
                        Console.ok("Experiment {ID} on Cluster {CLUSTER} deleted".format(**arguments))
                    except:
                        Console.error("Could not delete experiment {ID} on {CLUSTER}".format(**arguments))
            else:
                result = Experiment.list(cluster, id=None, format="list")
                if result is not None:
                    arguments['experiments'] = ", ".join([str(i) for i in result])
                else:
                    Console.error("Could not find experiment {ID} on {CLUSTER}".format(**arguments))
                    return ""

                force = yn_choice("Would you like to delete the experiments {experiments} on Cluster {CLUSTER}".format(
                    **arguments))
                if force:
                    try:
                        result = Experiment.rm(cluster, id=None)
                        Console.ok("Experiments {experiments} on Cluster {CLUSTER} deleted".format(**arguments))
                    except:
                        Console.error("Could delete the experiments on {CLUSTER}".format(**arguments))
                return ""

        elif arguments["run"] and arguments["output"]:
            # hpc experiment list [--cluster=CLUSTER]
            if arguments["ID"]:
                print ("# List of experiment {ID} on Cluster {CLUSTER}".format(**arguments))
                result = Experiment.output(cluster, id=arguments["ID"], format="list")
                if result is not None:
                    print ("\n".join(result))
                else:
                    Console.error("Could not find experiment {ID} on {CLUSTER}".format(**arguments))
            else:
                print ("# List of experiments on Cluster {CLUSTER}".format(**arguments))
                ids = Experiment.output(cluster, id=None, format="list")
                if ids is not None:
                    print (", ".join([str(i) for i in ids]))
                else:
                    Console.error("Could not find experiment {ID} on {CLUSTER}".format(**arguments))

        elif arguments["run"]:
            queue = arguments['--queue'] or Default.get('queue')
            # if not queue:
            #    Console.error('set default queue using: default queue=<value>')
            #    return
            group = arguments['--group'] or Default.get('group')
            if group is None:
                Console.error('set default group using: default group=<value> --cloud=general')
                return

            script = arguments['SCRIPT']
            arg_dict = {
                '-name': arguments['--name'],
                '-p': queue,
                '-t': arguments['--t'],
                '-N': arguments['--N']
            }

            result = batch.run(cluster, group, script, **arg_dict)
            if isinstance(result, dict):
                print (attribute_printer(result))
                Console.ok("Experiment {count}: Started batch job {job_id} on {cluster}".format(**result))
            else:
                Console.error(result)

        return ""
示例#32
0
    def do_workflow(self, args, arguments):
        """
        ::

            Usage:
                workflow refresh [--cloud=CLOUD] [-v]
                workflow list [ID] [NAME] [--cloud=CLOUD] [--format=FORMAT] [--refresh] [-v]
                workflow add NAME LOCATION
                workflow delete ID
                workflow status [NAMES]
                workflow show ID
                workflow save NAME WORKFLOWSTR
                workflow run NAME
                workflow service start
                workflow service stop
                This lists out the workflows 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 workflow refresh
                cm workflow list
                cm workflow list --format=csv
                cm workflow show 58c9552c-8d93-42c0-9dea-5f48d90a3188 --refresh
                cm workflow run workflow1

        """


        arg = dotdict(arguments)
        if arg.NAMES is not None:
            arg.names = Parameter.expand(arg.NAMES)
        else:
            arg.names = None
        pprint (arg)



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

        if arguments["-v"]:
            print("Cloud: {}".format(cloud))

        if arguments["refresh"] or Default.refresh:
            msg = "Refresh workflow for cloud {:}.".format(cloud)
            if Workflow.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"]

            result = None
            if id is None:
                result = Workflow.list(cloud, output_format)
            else:
                result = Workflow.details(cloud, id, live, output_format)

            if result is None:
                Console.error("No workflow(s) found. Failed.")
            else:
                print(result)
            return ""

        elif arguments["show"]:
            workflow_id = arguments["ID"]
            Console.ok("I executed show")
            # Console.msg(workflow_id)
            if workflow_id is None:
                Console.msg("Please enter a Workflow Id to execute workflow")
            else:
                result = Workflow.run(cloud,workflow_id)
                # print (result)
                if result is None:
                    Console.msg("Use workflow list to view existing workflows or do workflow save to save a new one")
                else:
                    # Console.msg(result)
                    # Console.msg(result[0])
                    Console.msg(result[0]['workflow_str'])
                    # entry_point(result[0]['workflow_str'])
                    Console.msg("All Set to execute")

        elif arguments["save"]:
            workflow_name = arguments["NAME"]
            workflow_str = arguments["WORKFLOWSTR"]

            result = Workflow.save(cloud=cloud,
                                   name=workflow_name,
                                   str=workflow_str)
            if result is not None:
                Console.ok(result)
            else:
                Console.error("Failed to save workflow!")
        elif arguments["run"]:
            Console.msg("Execute Run")
            workflow_name = arguments["NAME"]
            # cm_id = arguments["ID"]

            result = Workflow.run(cloud,name=workflow_name)

        elif arguments["delete"]:
            workflow_id = arguments["ID"]

            result = Workflow.delete(cloud,workflow_id)
            if result is not None:
                Console.msg(result)
            else:
                Console.error("Failed to save workflow!")
        elif arguments["service"] and arguments["start"]:
            # Console.msg()
            # mkdir -pf ~/.cloudmesh/workflow
            pass
示例#33
0
    def do_hpc(self, args, arguments):
        # noinspection PyPep8
        """
                ::

                    Usage:
                        hpc queue [--job=NAME][--cluster=CLUSTER][--format=FORMAT]
                        hpc info [--cluster=CLUSTER][--format=FORMAT]
                        hpc run list [ID] [--cluster=CLUSTER]
                        hpc run output [ID] [--cluster=CLUSTER]
                        hpc run rm [ID] [--cluster=CLUSTER]
                        hpc run SCRIPT [--queue=QUEUE] [--t=TIME] [--N=nodes] [--name=NAME] [--cluster=CLUSTER][--dir=DIR][--group=GROUP][--format=FORMAT]
                        hpc delete --job=NAME [--cluster=CLUSTER][--group=GROUP]
                        hpc delete all [--cluster=CLUSTER][--group=GROUP][--format=FORMAT]
                        hpc status [--job=name] [--cluster=CLUSTER] [--group=GROUP]
                        hpc test --cluster=CLUSTER [--time=SECONDS]

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

                    Examples:

                        Special notes

                           if the group is specified only jobs from that group are
                           considered. Otherwise the default group is used. If the
                           group is set to None, all groups are used.

                        cm hpc queue
                            lists the details of the queues of the hpc cluster

                        cm hpc queue --job=NAME
                            lists the details of the job in the queue of the hpc cluster

                        cm hpc info
                            lists the details of the hpc cluster

                        cm hpc run SCRIPT
                            submits the script to the cluster. The script will be
                            copied prior to execution into the home directory on the
                            remote machine. If a DIR is specified it will be copied
                            into that dir.
                            The name of the script is either specified in the script
                            itself, or if not the default naming scheme of
                            cloudmesh is used using the same index incremented name
                            as in vms fro clouds: cloudmes husername-index

                        cm hpc delete all
                            kills all jobs on the default hpc group

                        cm hpc delete --job=NAME
                            kills a job with a given name or job id

                        cm default cluster=NAME
                            sets the default hpc cluster

                        cm hpc status
                            returns the status of all jobs

                        cm hpc status job=ID
                            returns the status of the named job

                        cm hpc test --cluster=CLUSTER --time=SECONDS
                            submits a simple test job to the named cluster and returns
                            if the job could be successfully executed. This is a
                            blocking call and may take a long time to complete
                            dependent on if the queuing system of that cluster is
                            busy. It will only use one node/core and print the message

                            #CLOUDMESH: Test ok

                            in that is being looked for to identify if the test is
                            successful. If time is used, the job is terminated
                            after the time is elapsed.

                    Examples:
                        cm hpc queue
                        cm hpc queue --job=xxx
                        cm hpc info
                        cm hpc delete --job=6
                        cm hpc delete all
                        cm hpc status
                        cm hpc status --job=6
                        cm hpc run uname
                        cm hpc run ~/test.sh --cluster=india
                """

        format = arguments['--format']
        cluster = arguments['--cluster'] or Default.cluster
        arguments["CLUSTER"] = cluster

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

        batch = BatchProvider(cluster)

        if arguments["queue"]:
            name = arguments['--job']
            result = batch.queue(cluster, format=format, job=name)
            Console.msg(result)

        elif arguments["info"]:
            Console.msg(batch.info(cluster, format))

        elif arguments['delete'] and arguments['all']:
            group = arguments['--group'] or Default.get(name='group')
            if group is None:
                Console.error('set default group using: default group=<value> --cloud=general')
                return
            Console.ok(batch.delete(cluster, None, group))

        elif arguments["delete"]:
            job = arguments['--job']
            Console.ok(batch.delete(cluster, job))

        elif arguments["status"]:
            name = arguments['--job']
            result = batch.queue(cluster, format=format, job=name)
            Console.msg(result)

        elif arguments["test"]:
            time_secs = arguments['--time']
            if time_secs:
                time = '00:00:' + time_secs
            else:
                time = '00:00:10'  # give a  default time of 10 secs
            print(batch.test(cluster, time))

        elif arguments["run"] and arguments["list"]:
            # hpc experiment list [--cluster=CLUSTER]
            if arguments["ID"]:
                print("# List of experiment {ID} on Cluster {CLUSTER}".format(**arguments))
                result = Experiment.list(cluster, id=arguments["ID"], format="list")
                if result is not None:
                    print("\n".join(result))
                else:
                    Console.error("Could not find experiment {ID} on {CLUSTER}".format(**arguments))
            else:
                print("# List of experiments on Cluster {CLUSTER}".format(**arguments))
                ids = Experiment.list(cluster, id=None, format="list")
                if ids is not None:
                    print(", ".join([str(i) for i in ids]))
                else:
                    Console.error("Could not find experiment {ID} on {CLUSTER}".format(**arguments))

        elif arguments["run"] and arguments["rm"]:
            # hpc experiment list [--cluster=CLUSTER]
            if arguments["ID"]:

                force = yn_choice("Would you like to delete experiment {ID} on Cluster {CLUSTER}".format(**arguments))
                if force:
                    try:
                        result = Experiment.rm(cluster, id=arguments["ID"])
                        Console.ok("Experiment {ID} on Cluster {CLUSTER} deleted".format(**arguments))
                    except:
                        Console.error("Could not delete experiment {ID} on {CLUSTER}".format(**arguments))
            else:
                result = Experiment.list(cluster, id=None, format="list")
                if result is not None:
                    arguments['experiments'] = ", ".join([str(i) for i in result])
                else:
                    Console.error("Could not find experiment {ID} on {CLUSTER}".format(**arguments))
                    return ""

                force = yn_choice("Would you like to delete the experiments {experiments} on Cluster {CLUSTER}".format(
                    **arguments))
                if force:
                    try:
                        result = Experiment.rm(cluster, id=None)
                        Console.ok("Experiments {experiments} on Cluster {CLUSTER} deleted".format(**arguments))
                    except:
                        Console.error("Could delete the experiments on {CLUSTER}".format(**arguments))
                return ""

        elif arguments["run"] and arguments["output"]:
            # hpc experiment list [--cluster=CLUSTER]
            if arguments["ID"]:
                print("# List of experiment {ID} on Cluster {CLUSTER}".format(**arguments))
                result = Experiment.output(cluster, id=arguments["ID"], format="list")
                if result is not None:
                    print("\n".join(result))
                else:
                    Console.error("Could not find experiment {ID} on {CLUSTER}".format(**arguments))
            else:
                print("# List of experiments on Cluster {CLUSTER}".format(**arguments))
                ids = Experiment.output(cluster, id=None, format="list")
                if ids is not None:
                    print(", ".join([str(i) for i in ids]))
                else:
                    Console.error("Could not find experiment {ID} on {CLUSTER}".format(**arguments))

        elif arguments["run"]:
            queue = arguments['--queue'] or Default.get(name='queue')
            # if not queue:
            #    Console.error('set default queue using: default queue=<value>')
            #    return
            group = arguments['--group'] or Default.get(name='group')
            if group is None:
                Console.error('set default group using: default group=<value> --cloud=general')
                return

            script = arguments['SCRIPT']
            arg_dict = {
                '-name': arguments['--name'],
                '-p': queue,
                '-t': arguments['--t'],
                '-N': arguments['--N']
            }

            result = batch.run(cluster, group, script, **arg_dict)
            if isinstance(result, dict):
                print(Printer.attribute(result))
                Console.ok("Experiment {count}: Started batch job {job_id} on {cluster}".format(**result))
            else:
                Console.error(result)

        return ""
示例#34
0
    def do_network(self, args, arguments):
        """
        ::

            Usage:
                network get fixed [ip] [--cloud=CLOUD] FIXED_IP
                network get floating [ip] [--cloud=CLOUD] FLOATING_IP_ID
                network reserve fixed [ip] [--cloud=CLOUD] FIXED_IP
                network unreserve fixed [ip] [--cloud=CLOUD] FIXED_IP
                network associate floating [ip] [--cloud=CLOUD] [--group=GROUP]
                                           [--instance=INS_ID_OR_NAME] [FLOATING_IP]
                network disassociate floating [ip] [--cloud=CLOUD] [--group=GROUP]
                                              [--instance=INS_ID_OR_NAME] [FLOATING_IP]
                network create floating [ip] [--cloud=CLOUD] [--pool=FLOATING_IP_POOL]
                network delete floating [ip] [--cloud=CLOUD] [--unused] [FLOATING_IP]
                network list floating pool [--cloud=CLOUD]
                network list floating [ip] [--cloud=CLOUD] [--unused] [--instance=INS_ID_OR_NAME] [IP_OR_ID]
                network create cluster --group=demo_group
                network -h | --help

            Options:
                -h                          help message
                --unused                    unused floating ips
                --cloud=CLOUD               Name of the IaaS cloud e.g. india_openstack_grizzly.
                --group=GROUP               Name of the group in Cloudmesh
                --pool=FLOATING_IP_POOL     Name of Floating IP Pool
                --instance=INS_ID_OR_NAME   ID or Name of the vm instance

            Arguments:
                IP_OR_ID        IP Address or ID of IP Address
                FIXED_IP        Fixed IP Address, e.g. 10.1.5.2
                FLOATING_IP     Floating IP Address, e.g. 192.1.66.8
                FLOATING_IP_ID  ID associated with Floating IP, e.g. 185c5195-e824-4e7b-8581-703abec4bc01

            Examples:
                network get fixed ip --cloud=india 10.1.2.5
                network get fixed --cloud=india 10.1.2.5
                network get floating ip --cloud=india 185c5195-e824-4e7b-8581-703abec4bc01
                network get floating --cloud=india 185c5195-e824-4e7b-8581-703abec4bc01
                network reserve fixed ip --cloud=india 10.1.2.5
                network reserve fixed --cloud=india 10.1.2.5
                network unreserve fixed ip --cloud=india 10.1.2.5
                network unreserve fixed --cloud=india 10.1.2.5
                network associate floating ip --cloud=india --instance=albert-001 192.1.66.8
                network associate floating --cloud=india --instance=albert-001
                network associate floating --cloud=india --group=albert_group
                network disassociate floating ip --cloud=india --instance=albert-001 192.1.66.8
                network disassociate floating --cloud=india --instance=albert-001 192.1.66.8
                network create floating ip --cloud=india --pool=albert-f01
                network create floating --cloud=india --pool=albert-f01
                network delete floating ip --cloud=india 192.1.66.8 192.1.66.9
                network delete floating --cloud=india 192.1.66.8 192.1.66.9
                network list floating ip --cloud=india
                network list floating --cloud=india
                network list floating --cloud=india --unused
                network list floating --cloud=india 192.1.66.8
                network list floating --cloud=india --instance=323c5195-7yy34-4e7b-8581-703abec4b
                network list floating pool --cloud=india
                network create cluster --group=demo_group

        """
        # pprint(arguments)
        # Get the cloud parameter OR read default
        cloudname = arguments["--cloud"] or Default.cloud

        if cloudname is None:
            Console.error("Default cloud has not been set!"
                          "Please use the following to set it:\n"
                          "cm default cloud=CLOUDNAME\n"
                          "or provide it via the --cloud=CLOUDNAME argument.")
            return ""

        # Fixed IP info
        if arguments["get"] \
                and arguments["fixed"]:
            fixed_ip = arguments["FIXED_IP"]
            result = Network.get_fixed_ip(cloudname, fixed_ip_addr=fixed_ip)
            Console.msg(result)

        # Floating IP info
        elif arguments["get"] \
                and arguments["floating"]:
            floating_ip_id = arguments["FLOATING_IP_ID"]
            result = Network.get_floating_ip(cloudname,
                                             floating_ip_or_id=floating_ip_id)
            Console.msg(result)

        # Reserve a fixed ip
        elif arguments["reserve"] \
                and arguments["fixed"]:
            fixed_ip = arguments["FIXED_IP"]
            result = Network.reserve_fixed_ip(cloudname=cloudname,
                                              fixed_ip_addr=fixed_ip)
            if result is not None:
                Console.ok(
                    "Reserve fixed ip address {} complete.".format(fixed_ip))

        # Un-Reserve a fixed ip
        elif arguments["unreserve"] \
                and arguments["fixed"]:
            fixed_ip = arguments["FIXED_IP"]
            result = Network.unreserve_fixed_ip(cloudname=cloudname,
                                                fixed_ip_addr=fixed_ip)
            if result is not None:
                Console.ok("Un-Reserve fixed ip address {} complete.".format(
                    fixed_ip))

        # Associate floating IP
        elif arguments["associate"] \
                and arguments["floating"]:

            # Get all command-line arguments
            group_name = arguments["--group"]
            instance_id = arguments["--instance"]
            floating_ip = arguments["FLOATING_IP"]

            # group supplied
            if group_name is not None:
                """
                Group name has been provided.
                Assign floating IPs to all vms in the group
                and return
                """
                # Get the group information
                group = Group.get_info(name=group_name,
                                       category=cloudname,
                                       output="json")
                if group is not None:
                    # Convert from str to json
                    group = json.loads(group)
                    # For each vm in the group
                    # Create and assign a floating IP
                    for item in group:
                        instance_id = group[item]["value"]
                        floating_ip = Network.find_assign_floating_ip(
                            cloudname=cloudname, instance_id=instance_id)
                        if floating_ip is not None:
                            Console.ok(
                                "Created and assigned Floating IP {} to instance {}."
                                .format(floating_ip, instance_id))
                            # Refresh VM in db
                            self.refresh_vm(cloudname)
                else:
                    Console.error(
                        "No group {} in the Cloudmesh database.".format(
                            group_name))
                    return ""

            # floating-ip not supplied, instance-id supplied
            elif not floating_ip and instance_id is not None:
                """
                Floating IP has not been provided, instance-id provided.
                Generate one from the pool, and assign to vm
                and return
                """
                floating_ip = Network.find_assign_floating_ip(
                    cloudname=cloudname, instance_id=instance_id)
                if floating_ip is not None:
                    Console.ok(
                        "Associated floating IP {} to instance {}.".format(
                            floating_ip, instance_id))

            # instance-id & floating-ip supplied
            elif instance_id is not None:
                """
                Floating IP & Instance ID have been provided
                Associate the IP to the instance
                and return
                """
                Network.find_assign_floating_ip(cloudname=cloudname,
                                                instance_id=instance_id,
                                                floating_ip=floating_ip[0])

            # Invalid parameters
            else:
                Console.error(
                    "Please provide at least one of [--group] OR [--instance] parameters.\n"
                    "You can also provide [FLOATING_IP] AND [--instance] parameters.\n"
                    "See 'cm network --help' for more info.")
                return ""

            # Refresh VM in db
            self.refresh_vm(cloudname)

        elif arguments["disassociate"] \
                and arguments["floating"]:

            # Get all command-line arguments
            group_name = arguments["--group"]
            instance_id = arguments["--instance"]
            floating_ip = arguments["FLOATING_IP"]

            # group supplied
            if group_name is not None:
                """
                Group name has been provided.
                Remove floating IPs of all vms in the group
                and return
                """
                # Get the group information
                group = Group.get_info(name=group_name,
                                       category=cloudname,
                                       output="json")
                if group is not None:
                    # Convert from str to json
                    group = json.loads(group)
                    # For each vm in the group
                    # Create and assign a floating IP
                    for item in group:
                        instance_id = group[item]["value"]
                        # Get the instance dict
                        instance_dict = Network.get_instance_dict(
                            cloudname=cloudname, instance_id=instance_id)
                        # Instance not found
                        if instance_dict is None:
                            Console.error(
                                "Instance {} not found in the cloudmesh database!"
                                .format(instance_id))
                            return ""

                        # Get the instance name
                        instance_name = instance_dict["name"]
                        floating_ip = instance_dict["floating_ip"]

                        # Floating ip argument invalid
                        if floating_ip is None:
                            Console.error(
                                "Instance{} does not have a floating_ip.".
                                format(instance_name))
                            return ""

                        result = Network.disassociate_floating_ip(
                            cloudname=cloudname,
                            instance_name=instance_name,
                            floating_ip=floating_ip)
                        if result is not None:
                            Console.ok(
                                "Disassociated Floating IP {} from instance {}."
                                .format(floating_ip, instance_name))
                else:
                    Console.error(
                        "No group {} in the Cloudmesh database.".format(
                            group_name))
                    return ""

            # floating-ip not supplied, instance-id supplied
            elif len(floating_ip) == 0 and instance_id is not None:
                """
                Floating IP has not been provided, instance-id provided.
                Remove floating ip allocated to vm
                and return
                """
                instance_dict = Network.get_instance_dict(
                    cloudname=cloudname, instance_id=instance_id)
                # Instance not found
                if instance_dict is None:
                    Console.error(
                        "Instance {} not found in the cloudmesh database!".
                        format(instance_id))
                    return ""

                instance_name = instance_dict["name"]
                floating_ip = instance_dict["floating_ip"]

                # Floating ip argument invalid
                if floating_ip is None:
                    Console.error(
                        "Instance{} does not have a floating_ip.".format(
                            instance_name))
                    return ""

                result = Network.disassociate_floating_ip(
                    cloudname=cloudname,
                    instance_name=instance_name,
                    floating_ip=floating_ip)
                if result is not None:
                    Console.ok(
                        "Disassociated Floating IP {} from instance {}.".
                        format(floating_ip, instance_name))

            # instance-id & floating-ip supplied
            elif instance_id is not None:
                """
                Floating IP & Instance ID have been provided
                Remove the IP from the instance
                and return
                """
                instance_dict = Network.get_instance_dict(
                    cloudname=cloudname, instance_id=instance_id)
                floating_ip = floating_ip[0]

                # Instance not found
                if instance_dict is None:
                    Console.error(
                        "Instance {} not found in the cloudmesh database!".
                        format(instance_id))
                    return ""

                instance_name = instance_dict["name"]
                _floating_ip = instance_dict["floating_ip"]

                # Floating ip argument invalid
                if _floating_ip != floating_ip:
                    Console.error(
                        "Invalid floating_ip {} for instance {}.".format(
                            floating_ip, instance_name))
                    return ""

                result = Network.disassociate_floating_ip(
                    cloudname=cloudname,
                    instance_name=instance_name,
                    floating_ip=floating_ip)
                if result is not None:
                    Console.ok(
                        "Disassociated Floating IP {} from instance {}.".
                        format(floating_ip, instance_name))

            # Invalid parameters
            else:
                Console.error(
                    "Please provide at least one of [--group] OR [--instance] parameters.\n"
                    "You can also provide [FLOATING_IP] AND [--instance] parameters.\n"
                    "See 'cm network --help' for more info.")
                return ""

            # Refresh VM in db
            self.refresh_vm(cloudname)

        # Create new floating ip under floating pool
        elif arguments["create"] \
                and arguments["floating"]:
            floating_pool = arguments["--pool"]
            result = Network.create_floating_ip(cloudname=cloudname,
                                                floating_pool=floating_pool)
            if result is not None:
                Console.ok("Created new floating IP {}".format(result))
            else:
                Console.error(
                    "Failed to create floating IP! Please check arguments.")

        # Delete a floating ip address
        elif arguments["delete"] \
                and arguments["floating"]:

            # delete all unused floating ips
            if arguments["--unused"]:
                unused_floating_ips = Network.get_unused_floating_ip_list(
                    cloudname=cloudname)
                if unused_floating_ips:
                    for floating_ip in unused_floating_ips:
                        self._delete_floating_ip(cloudname=cloudname,
                                                 floating_ip=floating_ip["id"])
                else:
                    Console.msg(
                        "No unused floating ips exist at this moment. Ok.")
                return ""

            # delete specified floating ips
            floating_ips = Parameter.expand(arguments["FLOATING_IP"])

            for floating_ip in floating_ips:
                self._delete_floating_ip(cloudname=cloudname,
                                         floating_ip=floating_ip)

        # Floating IP Pool List
        elif arguments["list"] \
                and arguments["floating"] \
                and arguments["pool"]:
            result = Network.list_floating_ip_pool(cloudname)
            Console.msg(result)

        # Floating IP list [or info]
        elif arguments["list"] \
                and arguments["floating"]:

            ip_or_id = arguments["IP_OR_ID"]
            instance_id = arguments["--instance"]

            # List unused floating addr
            if arguments["--unused"]:
                result = Network.list_unused_floating_ip(cloudname=cloudname)
                Console.msg(result)
                return ""

            # Refresh VM in db
            self.refresh_vm(cloudname)

            # If instance id is supplied
            if instance_id is not None:
                instance_dict = Network.get_instance_dict(
                    cloudname=cloudname, instance_id=instance_id)
                # Instance not found
                if instance_dict is None:
                    Console.error(
                        "Instance {} not found in the cloudmesh database!".
                        format(instance_id))
                    return ""

                # Read the floating_ip from the dict
                ip_or_id = instance_dict["floating_ip"]

                if ip_or_id is None:
                    Console.error(
                        "Instance with ID {} does not have a floating IP address!"
                        .format(instance_id))
                    return ""

            # If the floating ip or associated ID is supplied
            if ip_or_id is not None:
                result = Network.get_floating_ip(cloudname,
                                                 floating_ip_or_id=ip_or_id)

                if result is not None:
                    Console.msg(result)
                else:
                    Console.error(
                        "Floating IP not found! Please check your arguments.")
                    return ""
            # Retrieve the full list
            else:
                result = Network.list_floating_ip(cloudname)
                Console.msg(result)

        # Create a virtual cluster
        elif arguments["cluster"] and \
                arguments["create"]:

            group_name = arguments["--group"] or \
                         Default.get(name="group", category=cloudname)

            # Get the group information
            group = Group.get_info(name=group_name,
                                   category=cloudname,
                                   output="json")
            if group is not None:
                # Convert from str to json
                group = json.loads(group)

                # var contains pub key of all vms
                public_keys = ""
                login_users = []
                login_ips = []

                # For each vm in the group
                # Create and assign a floating IP
                for item in group:
                    instance_id = group[item]["value"]
                    # Get the instance dict
                    instance_dict = Network.get_instance_dict(
                        cloudname=cloudname, instance_id=instance_id)
                    # Instance not found
                    if instance_dict is None:
                        Console.error(
                            "Instance {} not found in the cloudmesh database!".
                            format(instance_id))
                        return ""

                    # Get the instance name
                    instance_name = instance_dict["name"]
                    floating_ip = instance_dict["floating_ip"]

                    # If vm does not have floating ip, then create
                    if floating_ip is None:
                        floating_ip = Network.create_assign_floating_ip(
                            cloudname=cloudname, instance_name=instance_name)
                        if floating_ip is not None:
                            Console.ok(
                                "Created and assigned Floating IP {} to instance {}."
                                .format(floating_ip, instance_name))
                            # Refresh VM in db
                            self.refresh_vm(cloudname)

                    # Get the login user for this machine
                    user = input("Enter the login user for VM {} : ".format(
                        instance_name))
                    passphrase = getpass.getpass(
                        "Enter the passphrase key on VM {} : ".format(
                            instance_name))

                    # create list for second iteration
                    login_users.append(user)
                    login_ips.append(floating_ip)

                    login_args = [
                        user + "@" + floating_ip,
                    ]

                    keygen_args = [
                        "ssh-keygen -t rsa -f ~/.ssh/id_rsa -N " + passphrase
                    ]

                    cat_pubkey_args = ["cat ~/.ssh/id_rsa.pub"]

                    generate_keypair = login_args + keygen_args
                    result = Shell.ssh(*generate_keypair)

                    # print("***** Keygen *****")
                    # print(result)

                    cat_public_key = login_args + cat_pubkey_args
                    result = Shell.ssh(*cat_public_key)
                    public_keys += "\n" + result

                    # print("***** id_rsa.pub *****")
                    # print(result)

                # print("***** public keys *****")
                # print(public_keys)

                for user, ip in zip(login_users, login_ips):
                    arguments = [
                        user + "@" + ip,
                        "echo '" + public_keys + "' >> ~/.ssh/authorized_keys"
                    ]

                    # copy the public key contents to auth_keys
                    result = Shell.ssh(*arguments)

                Console.ok("Virtual cluster creation successfull.")
            else:
                Console.error("No group {} in the Cloudmesh database.".format(
                    group_name))
                return ""

        return ""
示例#35
0
    def do_key(self, args, arguments):
        """
        ::

           Usage:
             key  -h | --help
             key list --cloud=CLOUD
             key list --source=db [--format=FORMAT]
             key list --source=yaml [--format=FORMAT]
             key list --source=ssh [--dir=DIR] [--format=FORMAT]
             key list --source=git [--format=FORMAT] [--username=USERNAME]
             key list
             key load [--format=FORMAT]
             key add [NAME] [--source=FILENAME]
             key add [NAME] [--git]
             key add [NAME] [--ssh]
             key get NAME
             key default --select
             key delete (NAME | --select | --all)
             key delete NAME --cloud=CLOUD
             key upload [NAME] [--cloud=CLOUD]
             key upload [NAME] --active

           Manages the keys

           Arguments:

             CLOUD          The cloud
             NAME           The name of the key.
             SOURCE         db, ssh, all
             KEYNAME        The name of a key. For key upload it defaults to the default key name.
             FORMAT         The format of the output (table, json, yaml)
             FILENAME       The filename with full path in which the key
                            is located
             NAME_ON_CLOUD  Typically the name of the keypair on the cloud.

           Options:

              --dir=DIR                     the directory with keys [default: ~/.ssh]
              --format=FORMAT               the format of the output [default: table]
              --source=SOURCE               the source for the keys [default: db]
              --username=USERNAME           the source for the keys [default: none]
              --name=KEYNAME                The name of a key
              --all                         delete all keys
              --force                       delete the key form the cloud
              --name_on_cloud=NAME_ON_CLOUD Typically the name of the keypair on the cloud.

           Description:

           key list --source=git  [--username=USERNAME]

              lists all keys in git for the specified user. If the
              name is not specified it is read from cloudmesh.yaml

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

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

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

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

           key list [--format=FORMAT]

               list the keys in the giiven format: json, yaml,
               table. table is default

           key list

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

               
           key add [--name=keyname] FILENAME

               adds the key specifid by the filename to the key
               database

           key get NAME

               Retrieves the key indicated by the NAME parameter from database
               and prints its fingerprint.

           key default --select

                Select the default key interactively

           key delete NAME

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

           key rename NAME NEW

                renames the key from NAME to NEW.
                
        """
        # pprint(arguments)

        invalid_names = ['tbd', 'none', "", 'id_rsa']

        def _print_dict(d, header=None, format='table'):
            msg = Printer.write(
                d,
                order=["name", "comment", "uri", "fingerprint", "source"],
                output=format,
                sort_keys=True)
            if msg is None:
                Console.error("No keys found.", traceflag=False)
                return None
            else:
                return msg

        directory = Config.path_expand(arguments["--dir"])

        cloud = arguments["--cloud"] or Default.cloud

        if arguments['list']:
            _format = arguments['--format']
            _source = arguments['--source']
            _dir = arguments['--dir']

            if "--source" not in arguments and "--cloud" not in arguments:
                arguments["--source"] = 'db'

            if arguments['--cloud']:

                #
                # get key list from openstack cloud
                #
                #keys = Key.list(cloud, output=_format)

                keys = Key.list_on_cloud(cloud, live=True, format=_format)
                if keys is None:
                    Console.ok("The Key list is empty")
                else:
                    print(keys)
                return ""

            elif arguments['--source'] == 'ssh':

                try:
                    #sshm = SSHKeyManager()
                    d = Key.get_from_dir(directory, store=False)

                    #print("SSS", type(Key.__keys__))
                    #d = dict(Key.all())
                    #print(d)
                    print(
                        Printer.write(d,
                                      order=[
                                          "name", "comment", "uri",
                                          "fingerprint", "source"
                                      ],
                                      output="table"))

                    # d = dict(sshm.__keys__)

                    # print(_print_dict(d, format=_format))
                    msg = "info. OK."
                    Console.ok(msg)
                    return ""
                except Exception as e:
                    Error.traceback(e)
                    Console.error("Problem listing keys from ssh")

            elif arguments['--source'] in ['cm', 'cloudmesh', 'yaml']:

                try:
                    #sshm = SSHKeyManager()
                    d = Key.get_from_yaml(load_order=directory, store=False)

                    print(
                        Printer.write(d,
                                      order=[
                                          "name", "comment", "uri",
                                          "fingerprint", "source"
                                      ],
                                      output=_format))
                    return ""
                except Exception as e:
                    Error.traceback(e)
                    Console.error("Problem listing keys from `{:}`".format(
                        arguments['--source']))

            elif arguments['--source'] in ['git']:

                username = arguments["--username"]
                # print(username)
                if username == 'none':
                    conf = ConfigDict("cloudmesh.yaml")
                    username = conf["cloudmesh.github.username"]

                #sshm = SSHKeyManager()
                try:

                    d = Key.get_from_git(username, store=False)

                    print(
                        Printer.write(d,
                                      order=[
                                          "name", "comment", "uri",
                                          "fingerprint", "source"
                                      ],
                                      output=_format))

                    msg = "info. OK."
                    Console.ok(msg)
                except Exception as e:
                    Error.traceback(e)
                    Console.error("Problem listing git keys from database")
                    return ""

            elif arguments['--source'] == 'db':

                try:
                    #sshdb = SSHKeyDBManager()
                    d = Key.all(output='dict')

                    if d is not None or d != []:

                        print(
                            Printer.write(d,
                                          order=[
                                              "name", "comment", "uri",
                                              "fingerprint", "source"
                                          ],
                                          output=_format))

                        # print(_print_dict(d, output=arguments['--format']))
                        msg = "info. OK."
                        Console.ok(msg)
                    else:
                        Console.error("No keys in the database")
                except Exception as e:
                    Error.traceback(e)
                    Console.error("Problem listing keys from database")

        elif arguments['get']:

            try:
                name = arguments['NAME']
                #sshdb = SSHKeyDBManager()
                d = Key.all(output="dict")

                for key in d:
                    if key["name"] == name:
                        print("{:}: {:}".format(key['name'],
                                                key['fingerprint']))
                        msg = "info. OK."
                        Console.ok(msg)
                        return ""
                    else:
                        pass
                Console.error("The key is not in the database")
            except Exception as e:
                Error.traceback(e)
                Console.error("The key is not in the database")

        # key add [NAME] [--source=FILENAME]
        #     key add [NAME] [--git]

        elif arguments['add'] and arguments["--git"]:

            # Console.error("This feature is not yet implemented", traceflag=False)
            # return ""

            print('git add')
            #sshdb = SSHKeyDBManager()

            data = dotdict(arguments)

            keyname = data.NAME

            #
            # get name
            #
            conf = ConfigDict("cloudmesh.yaml")
            data.username = conf["cloudmesh.github.username"]
            data.name = arguments['NAME'] or data.username

            #
            # get git username
            #
            data.username = ConfigDict(
                "cloudmesh.yaml")["cloudmesh.github.username"]

            if str(data.name).lower() in invalid_names:
                Console.error(
                    "The github user name is not set in the yaml file",
                    traceflag=False)
                return ""

            try:
                Console.msg(
                    "Retrieving github ssh keys for user {username}".format(
                        **data))
                #sshm = SSHKeyManager()
                Key.get_from_git(data.username)
                d = Key.all()
                # pprint(d)
            except Exception as e:
                Console.error(
                    "Problem adding keys to git for user: {username}".format(
                        **data))
                return ""

            for key in d:
                if key is not None:
                    key["name"] = key["name"].replace("-", "_")
                    key["source"] = "git"
                    key["user"] = data.name
                    try:
                        o = dict(key)
                        o['value'] = key["string"]
                        Key.add_from_dict(key)
                    except Exception as e:
                        Console.error(
                            "The key {name} with that finger print already exists"
                            .format(**key),
                            traceflag=False)

        elif arguments['add'] and not arguments["--git"]:

            #     key add [NAME] [--source=FILENAME]
            #     key add [NAME] [--git]

            #sshdb = SSHKeyDBManager()

            data = dotdict()

            #
            # get name
            #
            conf = ConfigDict("cloudmesh.yaml")
            data.username = conf["cloudmesh.profile.user"]
            data.name = arguments['NAME'] or data.username

            data.filename = arguments['--source']
            if data.filename == "db" or data.filename is None:
                data.filename = Config.path_expand("~/.ssh/id_rsa.pub")

            if str(data.name).lower() in invalid_names:
                msg = (
                    "Your choice of keyname {name} is insufficient. \n"
                    "You must be chosing a keyname that is distingct on all clouds. \n"
                    "Possible choices are your gmail name, your XSEDE name, or \n"
                    "some name that is uniqe. "
                    "Best is also to set this name in \n"
                    "cloudmesh.profile.user as "
                    "part of your \n~/cloudmesh/cloudmesh.yaml file.")
                Console.error(msg.format(**data), traceflag=False)
                return ""

            try:
                Key.add_from_path(data.filename,
                                  data.name,
                                  source="ssh",
                                  uri="file://" + data.filename)
                print("Key {name} successfully added to the database".format(
                    **data))
                msg = "info. OK."
                Console.ok(msg)

            except ValueError as e:
                Console.error(
                    "A key with this fingerprint already exists".format(
                        **data),
                    traceflag=False)
                Console.msg("Please use check with: key list")

            return ""

        elif arguments['default']:

            # print("default")

            if arguments['--select']:
                keyname = None
                try:
                    #sshdb = SSHKeyDBManager()
                    select = Key.select()
                    if select != 'q':
                        keyname = select.split(':')[0]
                        print("Setting key: {:} as default.".format(keyname))
                        Default.key = keyname
                        msg = "info. OK."
                        Console.ok(msg)
                except Exception as e:
                    Error.traceback(e)
                    Console.error(
                        "Setting default for selected key {:} failed.".format(
                            keyname))

            else:
                try:
                    #sshdb = SSHKeyDBManager()
                    d = Key.table_dict()

                    for i in d:
                        if d[i]["is_default"] == "True":
                            key = d[i]
                            print("{:}: {:}".format(key['name'],
                                                    key['fingerprint']))
                            msg = "info. OK."
                            Console.ok(msg)
                            return ""
                        else:
                            pass
                    Console.error("The key is not in the database")
                except Exception as e:
                    Error.traceback(e)
                    Console.error("Problem retrieving default key.")

        elif arguments['delete'] and arguments["--cloud"]:

            key = dotdict({
                'cloud': arguments["--cloud"],
                'name': arguments["NAME"]
            })
            try:
                Key.delete(key.name, key.cloud)
                msg = "info. OK."
                Console.ok(msg)
            except:
                Console.error(
                    "Problem deleting the key {name} on the cloud {cloud}".
                    format(**key))

        elif arguments['delete']:

            # key delete (NAME | --select| --all)

            data = dotdict({
                'all': arguments['--all'] or False,
                'select': arguments['--select'] or False,
                'name': arguments['NAME'] or False,
            })

            pprint(data)
            # BUG delete all is not properly implemented

            if data.all:
                Console.TODO("Delete --all is not yet implemented.")
                # Key.delete()
            elif data.select:
                key = Key.select()
                print(key)

            else:  # name
                Key.delete(data.name)

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

        elif arguments['upload']:

            # pprint(arguments)

            try:
                #
                # get username
                #
                conf = ConfigDict("cloudmesh.yaml")
                username = conf["cloudmesh"]["profile"]["user"]
                if username in ['None', 'TBD']:
                    username = None

                #
                # get cloudnames
                #
                clouds = []

                if arguments["--active"]:
                    cloud = 'active'
                else:
                    cloud = arguments["--cloud"] or Default.cloud

                if cloud == "all":
                    config = ConfigDict("cloudmesh.yaml")
                    clouds = config["cloudmesh"]["clouds"]
                elif cloud == 'active':
                    config = ConfigDict("cloudmesh.yaml")
                    clouds = config["cloudmesh"]["active"]
                else:
                    clouds.append(cloud)

                #
                # get keyname
                #

                for cloud in clouds:
                    status = 0
                    #sshdb = SSHKeyDBManager()
                    #sshm = SSHKeyManager()
                    keys = Key.all()
                    for key in keys:

                        print("upload key {} -> {}".format(key["name"], cloud))

                        try:
                            status = Key.add_key_to_cloud(
                                username, key["name"], cloud)

                        except Exception as e:
                            Console.error("problem")
                            if "already exists" in str(e):
                                print(
                                    "key already exists. Skipping upload. OK.")
                        if status == 1:
                            print("Problem uploading key {} to {}. failed.".
                                  format(key["name"], cloud))

            except Exception as e:
                Console.error("Problem adding key to cloud")