Пример #1
0
###############################################################
# pytest -v --capture=no tests/1_local/test_shell_universal.py
# pytest -v  tests/1_local/test_shell_universal.py
# pytest -v --capture=no  tests/1_local/test_shell_universal.py::TestUniversal::<METHODNAME>
###############################################################
import pytest
from cloudmesh.common.Benchmark import Benchmark
from cloudmesh.common.Shell import Shell
from cloudmesh.common.util import HEADING

Benchmark.debug()

cloud = locals

shell = Shell()


@pytest.mark.incremental
class TestUniversal:
    def test_terminal_type(self):
        HEADING()
        print(shell.terminal_type())

    def test_run(self):
        HEADING()
        Benchmark.Start()
        r = Shell.run("cms help")
        Benchmark.Stop()
        print(r)
        assert len(r) > 0
Пример #2
0
    def do_cluster(self, args, arguments):
        """
		::

		Usage:
			cluster test
			cluster build --id=ID LABEL
			cluster create --cloud=CLOUD --n=N LABEL
			cluster add --id="[ID]" --all LABEL
			cluster remove --id="[ID]" --all LABEL
			cluster terminate --all LABEL
			cluster info [--verbose=V] [LABEL]

		This command allows you to create and interact with an available cluster of machines.

		Arguments:
			ID		An existing machine ID to be reserved for the cluster.
			LABEL		The label for the cluster.
			CLOUD		Cloud platform to initialize VMs.
			N			Number of instances to request.
			V			Verbosity level.
			
		Options:
			--id      Specify string containing list of IDs, comma delimited format "id1,id2,...,idx".
			--cloud	Specify cloud platform {AWS, Azure, Openstack}.
			--n		Specify number of VMs to initialize.
			--all		OPTIONAL.  Overrides --id, will pass all machines as an argument.
			--verbose OPTIONAL.  Provides verbosity level for info.

		Description:

			cluster build --id=ID LABEL

				Groups together existing machines and reserves them for cluster use.  Pass a comma-delimited list of machine ID's as a string.
				Pass --all to associate all available machines to cluster.
				
			cluster create --cloud=CLOUD --n=N LABEL
				
				Automatically requests VMs from the cloud service requested.
			
			cluster add --id="[ID]" --all LABEL

				Adds given machine IDs to cluster.  Pass --all to associate all available machines to cluster.

			cluster remove --id="[ID]" LABEL
			
				Removes given machine IDs from cluster.  Pass --all to disassociate all machines from cluster.

			cluster terminate --all LABEL

				Terminates all instances associated with the cluster, wipes cluster data.  If --all is passed, terminates all running clusters.

			cluster info --all --verbose=v [LABEL]

				Retrieves cluster data and machine data associatred with cluster.  Verbosity level 1 provides high-level cluster information
				and list of machines associated.  Verbosity level 2 provides cluster information, machine information and status.  Verbosity 
				level 3 provides all available information.

		"""
        map_parameters(arguments, 'id', 'label', 'cloud', 'n', 'v', 'all',
                       'verbose')

        # inv = Inventory()
        # inv.read()

        config = Config()
        user = config["cloudmesh.profile.user"]
        s = Shell()
        cmdb = CmDatabase()

        if arguments.test:
            cmdb = CmDatabase()
            virtual_clusters = cmdb.collection("cluster-virtual")
            print(*[index for index in virtual_clusters.list_indexes()])

        if arguments.build:
            ids, label = arguments.id, arguments.LABEL

            # Builds and stores a cluster connected to existing machine ids
            machines = ids.split(",")
            cluster_machines = []
            for i, machine in enumerate(machines):
                cluster_machines.append({
                    f"{machine}_{i}": {
                        "type": "cloud",
                        "cloud": None,
                        "status": "available",
                        "deployment": None
                    }
                })
            print(f"Adding the following machines to cluster-cloud {label}: ")
            VERBOSE(cluster_machines)
            collection = cmdb.collection("cluster-cloud")
            collection.insert_one({label: cluster_machines})

        # # TODO Revise to update to correct mongo create/update
        # cmdb.UPDATE(clusters)

        if arguments.create:
            n, label, cloud = arguments.n, arguments.label, arguments.cloud
            ids = [f"label_{i}" for i in range(n)].join(",")
            starting = [
                s.run(f"cms vm boot --name={id} --cloud={cloud}") for id in ids
            ]
            s.run(f"cms cluster build --id={ids} {label}")
            print(f"Starting {starting}")

        elif arguments.add:
            pass

        elif arguments.remove:
            pass

        elif arguments.terminate:
            pass

        elif arguments.info:
            v, label = arguments.verbose or arguments.v or None, arguments.LABEL or None
            if label: print(f"Searching for {label}")
            virtual_clusters, cloud_clusters = cmdb.collection(
                "cluster-virtual"), cmdb.collection("cluster-cloud")
            output = {
                'virtual': [i for i in virtual_clusters.find(label)],
                'cloud': [i for i in cloud_clusters.find(label)]
            }

            print(output)
        return ""
Пример #3
0
#!/usr/bin/env python3

# Josh Goodman sp20-516-220 E.Cloudmesh.Common.4

from cloudmesh.common.Shell import Shell

if __name__ == "__main__":
    my_shell = Shell()
    lines = my_shell.grep('Josh', __file__)
    print(f"The following lines match 'Josh' in {__file__}:\n\n{lines}")



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

		  Usage:
		  		cluster
				cluster create --provider=PROVIDER --deploy=FILE NAME
				cluster add --name=NAME NAME
				cluster remove --name=NAME NAME
				cluster deploy --name=NAME FILE
				cluster kill NAME
				cluster list
				cluster info NAME


		  This command allows you to create and interact with an available cluster.

		  Arguments:
			  NAME   	A name/id of a cluster or machine
			  PROVIDER	One of {Nomad, Kubernetes}
			  FILE		Jobfile for given provider

		  Options:
			  --name    	specify name
			  --provider	specify provider
			  --deploy		specify application to deploy (jobfile)

		"""
        map_parameters(arguments, 'name', 'provider', 'deploy')

        clusters = {}

        vm_boot = """
		/bin/bash cms vm boot \
			--name=amirjankar-hadoop \
			--output=json \
			--n=3
		"""
        if arguments.create:
            Console.info(f"Creating cluster {arguments.NAME}...")
            if not arguments.NAME:
                raise ValueError("Enter a name for the cluster.")
            name = arguments.NAME
            clusters[name] = {
                'created_at':
                datetime.datetime.now().strftime("%Y-%m-%D %H:%M:%S"),
                'machines': []
            }
            VERBOSE(clusters)

        elif arguments.add:
            Console.info(
                f"Attempting to add {arguments.NAME} from {arguments.name}")
            cluster_name, machine_name = arguments.name, arguments.NAME
            if cluster_name not in clusters.keys():
                VERBOSE(clusters)
                raise ValueError(
                    f"{cluster_name} doesn't exist. Create cluster with cms cluster create."
                )

            if machine_name in clusters[cluster_name]['machines']:
                VERBOSE(clusters)
                raise ValueError(f"{machine_name} already in {cluster_name}")

            clusters[cluster_name]['machines'].append(machine_name)
            VERBOSE(f"Successfully added {machine_name} to {cluster_name}.")

        elif arguments.remove:
            Console.info(
                f"Attempting to remove {arguments.NAME} from {arguments.name}")

        elif arguments.deploy:
            Console.info(
                f"Attempting to deploy {arguments.FILE} from {arguments.name}")

        elif arguments.kill:
            Console.info(f"Attempting to kill {arguments.NAME}")
            Shell()
        elif arguments.list:
            VERBOSE(clusters)

        elif arguments.info:
            Console.info(f"Retriving info for cluster {arguments.NAME}")

        return ""