Example #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.cloud
        if cloud is None:
            Console.error("Default cloud doesn't exist")
            return

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

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

            counter = 0

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

            if result is None:
                Console.error("No image(s) found. Failed.")
            else:
                print(result)
            return ""
Example #2
0
def cloudmesh_images(request, cloud=None):
    banner("images")
    if cloud is None:
        cloud = Default.cloud
    # TODO: make the cloudname a parameter
    data = Image.list(cloud, format='dict')
    print (json.dumps(data, indent=4))
    # TODO set proper columns
    order = [
        'cm_id',
        'name',
        'category',
        'minDisk',
        'minRam',
        'os_image_size',
        'progress',
        'project',
        'status',
    ]
    return (dict_table(request,
                       title="Cloudmesh Images {}".format(cloud),
                       data=data,
                       order=order))
Example #3
0
def cloudmesh_images(request, cloud=None):
    banner("images")
    if cloud is None:
        cloud = Default.get_cloud()
    # TODO: make the cloudname a parameter
    data = Image.list(cloud, format='dict')
    print json.dumps(data, indent=4)
    # TODO set proper columns
    order = [
        'id',
        'name',
        'cloud',
        'minDisk',
        'minRam',
        'os_image_size',
        'progress',
        'project',
        'status',
    ]
    return (dict_table(request,
                       title="Cloudmesh Images {}".format(cloud),
                       data=data,
                       order=order))
Example #4
0
def cloudmesh_refresh_db(request, action=None, cloud=None):

    if action is None:
        action = ['image']
    else:
        action = [action]

    print("******DEBUG*********")
    print("ACTION IS:",action)
    print("******DEBUG*********")

    if cloud is None:
        cloud = Default.cloud
    # TODO: make the cloudname a parameter
    data = Image.refresh(cloud)

    print("REFRESH IMAGE DATA",data)

    data = Image.list(cloud, format='dict')
    print(json.dumps(data, indent=4))
    # TODO set proper columns
    order = [
        'cm_id',
        'name',
        'category',
        'minDisk',
        'minRam',
        'os_image_size',
        'progress',
        'project',
        'status',
    ]

    return (dict_table(request,
                       title="Cloudmesh Images {}".format(cloud),
                       data=data,
                       order=order))
Example #5
0
    def do_list(self, args, arguments):
        """
        ::

            Usage:
                list [--cloud=CLOUD] [--format=FORMAT] [--user=USER] [--tenant=TENANT] default
                list [--cloud=CLOUD] [--format=FORMAT] [--user=USER] [--tenant=TENANT] vm
                list [--cloud=CLOUD] [--format=FORMAT] [--user=USER] [--tenant=TENANT] flavor
                list [--cloud=CLOUD] [--format=FORMAT] [--user=USER] [--tenant=TENANT] image

            List the items stored in the database

            Options:
                --cloud=CLOUD    the name of the cloud
                --format=FORMAT  the output format
                --tenant=TENANT     Name of the tenant, e.g. fg82.

            Description:
                List command prints the values stored in the database
                for [default/vm/flavor/image].
                Result can be filtered based on the cloud, user & tenant arguments.
                If these arguments are not specified, it reads the default

            Examples:
                $ list --cloud india default
                $ list --cloud india --format table flavor
                $ list --cloud india --user albert --tenant fg82 flavor
        """

        # pprint(arguments)

        # Method to get the kind from args
        #
        # TODO: the kind are defined in the provider,
        # TODO: keep the kind lower case
        # why is there a reason to make the gind upper case
        def get_kind():
            for k in ["vm", "image", "flavor", "default"]:
                if arguments[k]:
                    # kinds are all uppercase in model.py
                    return k.upper()
            return "help"

        # Read commandline arguments
        output_format = arguments['--format']
        cloud = arguments['--cloud'] or Default.cloud
        user = arguments['--user']
        tenant = arguments['--tenant']

        # If format is not specified, read default
        if output_format is None:
            output_format = Default.get(name="format") or "table"

        # If cloud is not specified, get default
        if cloud is None:
            cloud = Default.get(name="cloud") or ConfigDict(
                filename="cloudmesh.yaml")["cloudmesh"]["active"][0]

        # If user is not specified, get default
        if user is None:
            user = Default.get(name="user")

        # If tenant is not specified, get default
        if tenant is None:
            tenant = Default.get(name="tenant")

        # Get the kind
        kind = get_kind()
        header = None
        order = None

        # print help message
        if kind == 'help':
            Console.ok("Print help!")
            return ""

        # Prepare the order & header based on kind
        # TODO: use lower case so we have a convention thats easy to follow
        # TODO: add quota
        # TODO: add limits
        # TODO: add usage

        #
        # TODO: BUG: use the get attribute from the provider.
        # TODO:: call the cm xyz list functions and do not
        # reimplement this here
        # possibly introduce List.py

        result = None
        if kind == 'FLAVOR':
            result = Flavor.list(cloud, format=output_format)
        elif kind == 'DEFAULT':
            result = Default.list(order=order, output=output_format)
        elif kind == 'IMAGE':
            result = Image.list(cloud, format=output_format)
        elif kind == 'VM':
            result = Vm.list(cloud=cloud, output_format=output_format)

        if result:
            print(result)
        else:
            Console.error("No {}s found in the database.".format(kind.lower()))

        return ""
Example #6
0
    def do_select(self, args, arguments):
        """
        ::

          Usage:
              select image [CLOUD]
              select flavor [CLOUD]
              select cloud [CLOUD]
              select key [CLOUD]

          selects interactively the default values

          Arguments:

            CLOUD    the name of the cloud

          Options:

        """
        # pprint(arguments)
        cloud = arguments["CLOUD"] or Default.get_cloud()
        if arguments["image"]:
            try:
                image_dict = Image.list(cloud, format="dict")

                image_names = list()
                for image in image_dict.values():
                    image_names.append(image["name"])

                number = menu_return_num(title="Select an Image",
                                         menu_list=image_names,
                                         tries=10,
                                         with_display=True)

                if number == "q":
                    pass
                else:
                    image = image_names[number]
                    print("Selected image " + image)
                    Default.set("image", image, cloud=cloud)
            except:
                print("ERROR: could not set image.")

        elif arguments["flavor"]:
            try:
                flavor_dict = Flavor.list(cloud, format="dict")

                flavor_names = list()
                for flavor in flavor_dict.values():
                    flavor_names.append(flavor["name"])

                number = menu_return_num(title="Select a Flavor",
                                         menu_list=flavor_names,
                                         tries=10,
                                         with_display=True)

                if number == "q":
                    pass
                else:
                    flavor = flavor_names[number]
                    print("Selected flavor " + flavor)
                    Default.set("flavor", flavor, cloud=cloud)
            except:
                print("ERROR: could not set flavor.")

        elif arguments["cloud"]:
            try:
                config = ConfigDict("cloudmesh.yaml")
                clouds = config["cloudmesh"]["clouds"]

                for key in clouds.keys():
                    Console.ok("  " + key)

                number = menu_return_num(title="Select a cloud",
                                         menu_list=clouds.keys(),
                                         tries=10,
                                         with_display=True)
                if number == "q":
                    pass
                else:
                    cloud = clouds.keys()[number]
                    print("Selected cloud " + cloud)
                    Default.set("cloud", cloud, "general")
            except:
                print("ERROR: could not set cloud.")

        elif arguments["key"]:
            try:
                db = SSHKeyDBManager()

                key_dict = db.table_dict()

                key_names = list()
                for key in key_dict.values():
                    key_names.append(key["name"])

                number = menu_return_num(title="Select a Key",
                                         menu_list=key_names,
                                         tries=10,
                                         with_display=True)

                if number == "q":
                    pass
                else:
                    key = key_names[number]
                    print("Selected key " + key)

                    # TODO Fix default key setting in key DB
                    # db.set_default(key)

                    Default.set("key", key, cloud=cloud)
            except:
                print("ERROR: could not set key")

        return ""
Example #7
0
    def do_select(self, args, arguments):
        """
        ::

          Usage:
              select image [CLOUD] [--refresh]
              select flavor [CLOUD] [--refresh]
              select cloud [CLOUD]
              select key [CLOUD]

          selects interactively the default values

          Arguments:

            CLOUD    the name of the cloud

          Options:

            --refresh   refreshes the data before displaying it
                        from the cloud

        """
        # pprint(arguments)
        cloud = arguments["CLOUD"] or Default.cloud
        if arguments["image"]:
            try:
                refresh = arguments['--refresh'] or Default.refresh
                if refresh:
                    Image.refresh(cloud)

                image_dict = Image.list(cloud, format="dict")

                image_names = list()
                for image in list(image_dict.values()):
                    image_names.append(image["name"])

                number = menu_return_num(title="Select an Image",
                                         menu_list=image_names,
                                         tries=10,
                                         with_display=True)

                if number == "q":
                    pass
                else:
                    image = image_names[number]
                    print("Selected image " + image)
                    Default.set("image", image, category=cloud)
            except:
                print("ERROR: could not set image.")

        elif arguments["flavor"]:
            try:
                refresh = arguments['--refresh'] or Default.refresh
                if refresh:
                    Flavor.refresh(cloud)

                flavor_dict = Flavor.list(cloud, format="dict")

                flavor_names = list()
                for flavor in list(flavor_dict.values()):
                    flavor_names.append(flavor["name"])

                number = menu_return_num(title="Select a Flavor",
                                         menu_list=flavor_names,
                                         tries=10,
                                         with_display=True)

                if number == "q":
                    pass
                else:
                    flavor = flavor_names[number]
                    print("Selected flavor " + flavor)
                    Default.set("flavor", flavor, category=cloud)
            except:
                print("ERROR: could not set flavor.")

        elif arguments["cloud"]:
            try:
                config = ConfigDict("cloudmesh.yaml")
                clouds = config["cloudmesh"]["clouds"]

                for key in clouds:
                    Console.ok("  " + key)

                number = menu_return_num(title="Select a cloud",
                                         menu_list=list(clouds),
                                         tries=10,
                                         with_display=True)
                if number == "q":
                    pass
                else:
                    cloud = list(clouds)[number]
                    print("Selected cloud " + cloud)
                    Default.set("cloud", cloud, "general")
            except:
                print("ERROR: could not set cloud.")

        elif arguments["key"]:
            try:
                #db = SSHKeyDBManager()

                key_dict = Key.all(output='dict')

                key_names = list()
                for key in key_dict.values():
                    key_names.append(key["name"])

                number = menu_return_num(title="Select a Key",
                                         menu_list=key_names,
                                         tries=10,
                                         with_display=True)

                if number == "q":
                    pass
                else:
                    key = key_names[number]
                    print("Selected key " + key)

                    # TODO Fix default key setting in key DB
                    # db.set_default(key)

                    Default.set("key", key, category=cloud)
            except:
                print("ERROR: could not set key")

        return ""