Пример #1
0
    def do_image(self, args, arguments):
        """
        ::

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

                This lists out the images present for a cloud

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

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

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

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

        if arguments["list"]:
            id = arguments['ID']
            live = arguments['--refresh']
            output_format = arguments["--format"]
            if id is None:
                result = Image.list(cloud, output_format)
            else:
                result = Image.details(cloud, id, live, output_format)
            if result is None:
                Console.error("No image(s) found. Failed.")
            # Todo:
            # if database size = 0:
            #    Console.error("No images in the database, please refresh.")
            print(result)
            return ""
Пример #2
0
    def do_flavor(self, args, arguments):
        """
        ::

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

                This lists out the flavors present for a cloud

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

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

        """

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

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

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

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

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

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

            else:

                print(result)
            return ""
Пример #3
0
    def do_secgroup(self, args, arguments):
        """
        ::

            Usage:
                secgroup list [--cloud=CLOUD] [--tenant=TENANT]
                secgroup create [--cloud=CLOUD] [--tenant=TENANT] LABEL
                secgroup delete [--cloud=CLOUD] [--tenant=TENANT] LABEL
                secgroup rules-list [--cloud=CLOUD] [--tenant=TENANT] LABEL
                secgroup rules-add [--cloud=CLOUD] [--tenant=TENANT] LABEL FROMPORT TOPORT PROTOCOL CIDR
                secgroup rules-delete [--cloud=CLOUD] [--tenant=TENANT] LABEL FROMPORT TOPORT PROTOCOL CIDR
                secgroup refresh [--cloud=CLOUD]
                secgroup -h | --help
                secgroup --version

            Options:
                -h                  help message
                --cloud=CLOUD       Name of the IaaS cloud e.g. india_openstack_grizzly.
                --tenant=TENANT     Name of the tenant, e.g. fg82.

            Arguments:
                LABEL         The label/name of the security group
                FROMPORT      Staring port of the rule, e.g. 22
                TOPORT        Ending port of the rule, e.g. 22
                PROTOCOL      Protocol applied, e.g. TCP,UDP,ICMP
                CIDR          IP address range in CIDR format, e.g., 129.79.0.0/16

            Description:
                security_group command provides list/add/delete
                security_groups for a tenant of a cloud, as well as
                list/add/delete of rules for a security group from a
                specified cloud and tenant.


            Examples:
                $ secgroup list --cloud india --tenant fg82
                $ secgroup rules-list --cloud india --tenant fg82 default
                $ secgroup create --cloud india --tenant fg82 webservice
                $ secgroup rules-add --cloud india --tenant fg82 webservice 8080 8088 TCP "129.79.0.0/16"

        """
        # pprint(arguments)

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

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

        elif arguments["list"]:
            # if no arguments read default
            tenant = arguments["--tenant"] or Default.get("tenant")

            # If default not set, terminate
            if not cloud:
                Console.error("Default cloud not set!")
                return
            if not tenant:
                Console.error("Default tenant not set!")
                return ""

            result = SecGroup.list(project=tenant, cloudname=cloud)
            if result:
                print(result)
            else:
                Console.error("No Security Groups found in the cloudmesh database!")
            return ""

        elif arguments["create"]:
            # if no arguments read default
            tenant = arguments["--tenant"] or Default.get("tenant", cloud)
            label = arguments["LABEL"]

            # If default not set, terminate
            if not cloud:
                Console.error("Default cloud not set!")
                return
            if not tenant:
                Console.error("Default tenant not set!")
                return ""

            # Create returns uuid of created sec-group
            uuid = SecGroup.create(label, cloud, tenant)

            if uuid:
                Console.ok("Created a new security group [{}] with UUID [{}]".format(label, uuid))
            else:
                Console.error("Exiting!")
            return ""

        elif arguments["delete"]:
            # if no arguments read default
            tenant = arguments["--tenant"] or Default.get("tenant", cloud)
            label = arguments["LABEL"]

            # If default not set, terminate
            if not cloud:
                Console.error("Default cloud not set!")
                return ""
            if not tenant:
                Console.error("Default tenant not set!")
                return ""

            result = SecGroup.delete_secgroup(label, cloud, tenant)
            if result:
                print(result)
            else:
                Console.error("Security Group [{}, {}, {}] could not be " "deleted".format(label, cloud, tenant))

            return ""

        elif arguments["rules-delete"]:
            # if no arguments read default
            cloud = arguments["--cloud"] or Default.get_cloud()
            tenant = arguments["--tenant"] or Default.get("tenant", cloud)

            label = arguments["LABEL"]
            from_port = arguments["FROMPORT"]
            to_port = arguments["TOPORT"]
            protocol = arguments["PROTOCOL"]
            cidr = arguments["CIDR"]

            # If default not set, terminate
            if not cloud:
                Console.error("Default cloud not set!")
                return ""
            if not tenant:
                Console.error("Default tenant not set!")
                return ""

            # Get the security group
            sec_group = SecGroup.get(label, tenant, cloud)
            if sec_group:
                # Get the rules
                result = SecGroup.delete_rule(
                    cloudname=cloud,
                    secgroup=sec_group,
                    from_port=from_port,
                    to_port=to_port,
                    protocol=protocol,
                    cidr=cidr,
                )
                if result:
                    print(result)
                else:
                    Console.error(
                        "Rule [{} | {} | {} | {}] could not be deleted".format(from_port, to_port, protocol, cidr)
                    )

            return ""

        elif arguments["rules-list"]:
            # if no arguments read default
            tenant = arguments["--tenant"] or Default.get("tenant", cloud)
            label = arguments["LABEL"]

            # If default not set, terminate
            if not cloud:
                Console.error("Default cloud not set!")
                return ""
            if not tenant:
                Console.error("Default tenant not set!")
                return ""

            # Get the security group
            sec_group = SecGroup.get(label, tenant, cloud)
            if sec_group:
                # Get the rules
                result = SecGroup.get_rules(sec_group.uuid)
                print(result)
            else:
                Console.error(
                    "Security Group with label [{}], cloud [{}], and "
                    "tenant [{}] not found!".format(label, cloud, tenant)
                )
                return ""

        elif arguments["rules-add"]:
            # if no arguments read default
            tenant = arguments["--tenant"] or Default.get("tenant", cloud)

            label = arguments["LABEL"]
            from_port = arguments["FROMPORT"]
            to_port = arguments["TOPORT"]
            protocol = arguments["PROTOCOL"]
            cidr = arguments["CIDR"]

            # If default not set, terminate
            if not cloud:
                Console.error("Default cloud not set!")
                return ""
            if not tenant:
                Console.error("Default tenant not set!")
                return ""

            # Get the security group
            sec_group = SecGroup.get(label, tenant, cloud)
            if sec_group:
                # Add rules to the security group
                SecGroup.add_rule(
                    cloudname=cloud,
                    secgroup=sec_group,
                    from_port=from_port,
                    to_port=to_port,
                    protocol=protocol,
                    cidr=cidr,
                )
            else:
                Console.error(
                    "Security Group with label [{}], cloud [{}], and tenant [{"
                    "}] not found!".format(label, cloud, tenant)
                )
                return ""

        # TODO: Add Implementation
        elif arguments["--version"]:
            Console.ok("Version: ")

        return ""
Пример #4
0
            if commands is not None:
                sshcommand += " \"{:}\"".format(commands)

            # print(sshcommand)
            os.system(sshcommand)

        elif arguments["list"]:


            if arguments["--all"]:
                try:
                    _format = arguments["--format"] or "table"
                    d = ConfigDict("cloudmesh.yaml")
                    for cloud in d["cloudmesh"]["clouds"]:

                        if arguments["--refresh"] or Default.refresh():
                            _refresh()

                        print("Listing VMs on Cloud: {:}".format(cloud))
                        result = Vm.list(cloud=cloud, output_format=_format)
                        if result is not None:
                            print(result)
                        else:
                            print("Sorry. No data found with requested parameters in DB.")
                    msg = "info. OK."
                    Console.ok(msg)
                except Exception, e:
                    import traceback
                    print(traceback.format_exc())
                    print(e)
                    Console.error("Problem listing all instances")