Esempio n. 1
0
 def entry():
     name = Name()
     print(name)
     d = {"cmid": str(name), "cmcounter": name.counter, "name": "albert"}
     name.incr()
     pprint(d)
     return d
Esempio n. 2
0
 def test_name_incr(self):
     n = Name()
     Benchmark.Start()
     n.incr()
     Benchmark.Stop()
     print(n)
     assert str(n) == "gregor-vm-2"
Esempio n. 3
0
class TestName:

    def print_images(self):
        images = self.p.images()
        print(Printer.flatwrite(images,
                                sort_keys=("name"),
                                order=["name", "provider", "version"],
                                header=["Name", "Provider", "Version"]))

    def next_name(self):
        self.name_generator.incr()
        self.new_name = str(self.name_generator)
        return self.new_name

    def setup(self):
        banner("setup", c="-")
        self.user = Config()["cloudmesh.profile.user"]
        self.name_generator = Name(
            experiment="exp",
            group="grp",
            user=self.user,
            kind="vm",
            counter=1)

        self.name = str(self.name_generator)
        self.name_generator.incr()
        self.new_name = str(self.name_generator)
        self.p = Provider(name="vbox")

    def test_01_list_images(self):
        HEADING()
        self.print_images()

#    def test_03_delete_image(self):
#        HEADING()
#        name = "hashicorp/precise64"
#        try:
#            images = self.p.delete_image(name)
#            print ("delete", name)
#        except:
#            print ("image", name, "nor found")
#        self.print_images()

#    def test_03_add_image(self):
#        HEADING()
#        name = "hashicorp/precise64"
#
#        images = self.p.add_image(name)
#        self.print_images()

    def test_04_list_vm(self):
        HEADING()

        name = self.next_name()

        print ("Name", name)

        vms = self.p.list()
        pprint (vms)
        '''
def main():
    Benchmark.debug()
    user = Config()["cloudmesh.profile.user"]

    name_generator = Name()
    name_generator.set(f"benchmark-{user}-vm-" + "{counter}")
    name_generator.incr()

    results = {}

    for provider in providers:
        results[provider] = {}

        for f_name in functionality_benchmark:
            results[provider][f_name] = []

            x = ComputeBenchmark(provider, f_name)

            try:
                results[provider][f_name].append(test_function(x))

            except Exception as e:
                print(e)

    #TODO use Tabulate writer to print
    print(results)

    with open('benchmark.txt', 'w') as f:
        for provider, functions in results.items():
            for function in functions.keys():
                for result in functions[function]:
                    f.write(f"{result.__str__()}\n")
Esempio n. 5
0
    def test_01_Name(self):
        HEADING()

        n = Name(experiment="exp",
                 group="grp",
                 user="******",
                 kind="vm",
                 counter=1)

        n.reset()
        assert n.counter == 1

        print(n)
        assert str(n) == "exp-grp-gregor-vm-1"

        pprint(n.dict())

        print(n.get("container"))
        print(n)
        assert str(n) == "exp-grp-gregor-container-1"

        n.incr()
        print(n)
        assert str(n) == "exp-grp-gregor-container-2"

        print(n.counter)
        assert n.counter == 2

        m = Name()

        pprint(n.dict())
        pprint(m.dict())
        print(m)
        assert str(n) == str(m)
Esempio n. 6
0
class DatabaseAddOld:
    """
    Save the method's output to a MongoDB collection
    if the output is a dict or list of dicts.

    Example:

        @DatabaseUpdate("test-collection")
        def foo(x):
            return {"test": "hello"}
    """

    def __init__(self, collection="cloudmesh", replace=False):
        self.database = CmDatabase()
        self.replace = replace
        self.collection = collection
        self.name = Name()

    def __call__(self, f):
        def wrapper(*args, **kwargs):
            result = f(*args, **kwargs)
            result["cmid"] = str(self.name)
            result["cmcounter"] = str(self.name.counter)
            result["created"] = result["modified"] = str(datetime.utcnow())
            self.name.incr()

            if result is not None:
                result["created"] = result["modified"] = str(datetime.utcnow())
                r = self.database.update(result, collection=self.collection,
                                         replace=self.replace)

            return result

        return wrapper
Esempio n. 7
0
 def test_name_incr(self):
     HEADING()
     n = Name()
     Benchmark.Start()
     n.incr()
     Benchmark.Stop()
     print(n)
     assert str(n) == f"{username}-vm-2"
Esempio n. 8
0
 def test_aws_aws_boot(self):
     HEADING()
     name = Name()
     name.incr()
     vm_name = str(name)
     provider = Provider(name=CLOUD)
     vm = provider.create(name=vm_name, image="ami-0c929bde1796e1484",
                          size="t2.micro")
     print(vm)
     print(provider.list())
Esempio n. 9
0
class TestName:
    os = "ubuntu"
    version = "18.04"

    def next_name(self):
        self.name_generator.incr()
        self.new_name = str(self.name_generator)
        return self.new_name

    def setup(self):
        banner("setup", c="-")
        self.config = Config()
        self.user = self.config["cloudmesh.profile.user"]
        self.config = self.config["cloudmesh.cloud.docker"]
        self.name_generator = Name(experiment="exp",
                                   group="grp",
                                   user=self.user,
                                   kind="vm",
                                   counter=1)

        self.name = str(self.name_generator)
        self.name_generator.incr()
        self.new_name = str(self.name_generator)
        self.p = Provider(name="docker")

    def test_01_config(self):
        HEADING()
        pprint(self.config)

    def test_02_version(self):
        HEADING()
        version = self.p.version()
        pprint(version)

        assert "18.09.2" == version["docker"]["version"]
        assert 6247962 <= int(version["docker"]["build"])
        assert "18.09.2" == version.docker.version
        assert 6247962 <= int(version.docker.build)

    '''
    def test_03_terminal(self):
        HEADING()
        t1 = Shell.terminal()
        t2 = Shell.terminal(command="pwd")
    '''

    def test_03_images(self):
        HEADING
        images = self.p.images()
        pprint(images)
        """
Esempio n. 10
0
        def create_name():
            """
            Create volume name if name is not specified

            :return: string
            """

            config = Config()

            n = VolumeName(user=config["cloudmesh.profile.user"],
                           kind="volume",
                           path=f"{config.location}/volume.yaml",
                           schema="{user}-volume-{counter}")
            n.incr()
            return n
Esempio n. 11
0
class TestName:
    def setup(self):
        print()
        self.user = Config()["cloudmesh"]["profile"]["user"]
        self.clouduser = '******'
        self.name_generator = Name(experiment="exp",
                                   group="grp",
                                   user=self.user,
                                   kind="vm",
                                   counter=1)

        self.name = str(self.name_generator)
        self.name_generator.incr()

        self.new_name = str(self.name_generator)

        variables = Variables()
        cloud = variables['cloud']

        self.p = Provider(name=cloud)

        self.secgroupname = "CM4TestSecGroup"
        self.secgrouprule = {
            "ip_protocol": "tcp",
            "from_port": 8080,
            "to_port": 8088,
            "ip_range": "129.79.0.0/16"
        }
        self.testnode = None
        print("\n")

    def test_01_list_flavors(self):
        HEADING()
        flavors = self.p.flavors()

    def test_02_list_images(self):
        HEADING()
        flavors = self.p.images()

    def test_03_list_keys(self):
        HEADING()
        self.keys = self.p.keys()

    def test_03_list_nodes(self):
        HEADING()
        self.keys = self.p.list()
class TestName:
    image_test = False
    vbox = '6.0.4'
    image_name = "generic/ubuntu1810"
    size = 1024
    cloud = "vagrant"

    def print_images(self):
        images = self.p.images()
        print(
            Printer.flatwrite(images,
                              sort_keys=["name"],
                              order=["name", "provider", "version"],
                              header=["Name", "Provider", "Version"]))

    def next_name(self):
        self.name_generator.incr()
        self.new_name = str(self.name_generator)
        return self.new_name

    def setup(self):
        banner("setup", c="-")
        self.user = Config()["cloudmesh.profile.user"]
        self.name_generator = Name(experiment="exp",
                                   group="grp",
                                   user=self.user,
                                   kind="vm",
                                   counter=1)

        self.name = str(self.name_generator)
        self.name_generator.incr()
        self.new_name = str(self.name_generator)
        self.p = Provider(name=self.cloud)

    def test_01_version(self):
        HEADING()
        r = self.p.version()
        pprint(r)
        assert self.vbox_version == r["virtualbox"]["extension"]["version"]
        assert self.vbox_version == r["virtualbox"]["version"]

    def test_02_list_os(self):
        HEADING()
        ostypes = self.p.list_os()
        print(
            Printer.write(ostypes,
                          order=[
                              "id", "64_bit", "description", "family_descr",
                              "family_id"
                          ],
                          header=[
                              "id", "64_bit", "description", "family_descr",
                              "family_id"
                          ]))

    def test_03_name(self):
        HEADING()
        print(self.name)
        assert self.name == "exp-grp-{user}-vm-1".format(user=self.user)

    def test_04_list_images(self):
        HEADING()
        self.print_images()

    def test_05_delete_image(self):
        HEADING()
        if self.image_test:
            name = "generic/ubuntu1810"
            try:
                images = self.p.delete_image(self.image_name)
                print("delete", self.image_name)
            except:
                print("image", self.image_name, "nor found")
            self.print_images()
        else:
            print("not executed as image_test is not True. ok")

    def test_06_add_image(self):
        HEADING()
        if self.image_test:
            images = self.p.add_image(self.image_name)
            print("I", images)
            self.print_images()
            assert images.status == 0
        else:
            print("not executed as image_test is not True. ok")

    def test_07_list_vm(self):
        HEADING()

        vms = self.p.info()
        pprint(vms)
        print(
            Printer.flatwrite(
                vms,
                order=[
                    "vagrant.name", "vbox.name", "vagrant.id",
                    "vagrant.provider", "vagrant.state", "vagrant.hostname"
                ],
                header=["name", "vbox", "id", "provider", "state",
                        "hostname"]))

    def test_10_create(self):
        HEADING()

        name = self.next_name()

        print("Name", name)

        self.p.create(
            name=self.name,
            image=self.image_name,
            size=self.size,
            # username as the keypair name based on
            # the key implementation logic
            ex_keyname=self.user,
            ex_security_groups=['default'])

        directory = Path(
            path_expand("~/.cloudmesh/vagrant/exp-grp-gregor-vm-1"))

        assert directory.is_dir()

        time.sleep(5)
        nodes = self.p.list()
        pprint(nodes)

        node = self.p.find(nodes, name=self.name)
        pprint(node)

        nodes = self.p.list(raw=False)
        for node in nodes:
            if node.name == self.name:
                self.testnode = node
                break

        assert node is not None
Esempio n. 13
0
class TestName:

    def setup(self):
        banner("setup", c="-")
        self.user = Config()["cloudmesh.profile.user"]
        self.clouduser = '******'
        self.name_generator = Name(
            schema=f"{self.user}-vm",
            counter=1)

        self.name = str(self.name_generator)
        self.name_generator.incr()

        self.new_name = str(self.name_generator)

        self.p = Provider(name=CLOUD)

        self.secgroupname = "CM4TestSecGroup"
        self.secgrouprule = {"ip_protocol": "tcp",
                             "from_port": 8080,
                             "to_port": 8088,
                             "ip_range": "129.79.0.0/16"}
        self.testnode = None

    def test_show_conf(self):
        HEADING()
        pprint(self.p.user)
        pprint(self.p.cloudtype)
        pprint(self.p.spec)

    def test_list_flavors(self):
        HEADING()
        flavors = self.p.flavors()
        # pprint(flavors)

        print(Printer.flatwrite(flavors,
                                sort_keys=[
                                    "name", "ram", "disk",
                                    "extra.numberOfCores",
                                    "price"],
                                order=["name", "ram", "disk",
                                       "extra.numberOfCores",
                                       "price"],
                                header=["Name", "ram (MB)", "disk (GB)",
                                        "Cores",
                                        "price"])
              )

        """
        {'bandwidth': 0,
          'cm': {'cloud': 'azure',
                 'created': '2019-04-02 18:22:54.929946',
                 'driver': 'openstack',
                 'kind': 'flavor',
                 'name': 'Standard_L4s'},
          'disk': 678.0,
          'extra': {'maxDataDiskCount': 16,
                    'numberOfCores': 4,
                    'osDiskSizeInMB': 1047552},
          'id': 'Standard_L4s',
          'name': 'Standard_L4s',
          'price': 0,
          'ram': 32768,
          'updated': '2019-04-02 18:22:54.929946'},
        """

    def test_list_vm(self):
        HEADING()
        vms = self.p.list()
        # pprint(vms)

        # TODO: bug th eprint function is not implemented
        # print (self.p.Print(vms, kind="vm"))

        # '''
        print(Printer.flatwrite(vms,
                                sort_keys=["name"],
                                order=["name",
                                       "state",
                                       "extra.properties.hardwareProfile.vmSize",
                                       "extra.properties.storageProfile.imageReference.sku",
                                       "extra.properties.storageProfile.osDisk.osType",
                                       "extra.properties.storageProfile.osDisk.diskSizeGB",
                                       "extra.properties.osProfile.adminUsername",
                                       "private_ips",
                                       "public_ips"],
                                header=["Name",
                                        "State",
                                        "vmSize",
                                        "Image",
                                        "OS Type",
                                        "Disk (GB)",
                                        "Admin User",
                                        "Private ips",
                                        "Public ips"])
              )
        # '''

    def test_list_images(self):
        HEADING()
        publishername = 'Canonical'
        offername = 'UbuntuServer'
        skuname = '18.04-LTS'
        images = self.p.images(raw=False,
                               publisher=publishername,
                               offer=offername,
                               sku=skuname)
        # pprint (images)

        print(Printer.flatwrite(images,
                                sort_keys=["id", "name"],
                                order=["name", "id", 'version'],
                                header=["Name", "id", 'Version'])
              )
        # TODO: bug th eprint function is not implemented
        # print (self.p.Print(images, kind="image"))

        """
        {'cloud': 'azure',
          'driver': 'azure_asm',
          'extra': {'affinity_group': '',
                    'category': 'Public',
                    'description': '<p>Visual Studio Community 2015 Update 3 is our '
                                   'free, full featured and extensible IDE for '
                                   'non-enterprise application development. This image '
                                   'contains Windows Server 2012 R2 with Visual Studio '
                                   'Community 2015 Update 3. It allows you to easily '
                                   'and quickly set up a development environment in '
                                   'Azure to build and test applications using Visual '
                                   'Studio.</p><p>This image was created from the '
                                   'latest bits available on 8/20/2018.</p><p>The '
                                   'Visual Studio software is preinstalled in this VM, '
                                   'but you must acquire a Visual Studio subscription '
                                   'separately which allows you to sign in to and run '
                                   'Visual Studio on this VM.</p>',
                    'location': 'Southeast Asia;Australia East;Australia '
                                'Southeast;Brazil South;Canada Central;North '
                                'Europe;West Europe;Central India;South India;West '
                                'India;Japan East;Japan West;UK South;UK West;Central '
                                'US;South Central US;West US 2;West Central US',
                    'media_link': '',
                    'os': 'Windows',
                    'vm_image': False},
          'id': '03f55de797f546a1b29d1b8d66be687a__VS-2015-Comm-VSU3-AzureSDK-29-WS2012R2-2018-08-20',
          'kind': 'image',
          'name': 'Visual Studio Community 2015 Update 3 with Azure SDK 2.9 on Windows '
                  'Server 2012 R2'},

        """

    # '''
    def test_create(self):
        HEADING()
        image = "Canonical:UbuntuServer:18.04-LTS:18.04.201903200"
        size = "Standard_B1s"
        resgroup = 'xxx'
        storage_account = 'xxx'
        blob_container = 'xxx'
        network = 'xxx'
        subnet = 'xxx'
        sshpubkey = 'ssh-rsa YOUR_KEY_STRING'
        self.p.create(name=self.name,
                      image=image,
                      size=size,
                      sshpubkey=sshpubkey,
                      resource_group=resgroup,
                      storage_account=storage_account,
                      blob_container=blob_container,
                      network=network,
                      subnet=subnet)
        time.sleep(5)
        nodes = self.p.list()
        node = self.p.find(nodes, name=self.name)
        pprint(node)

        nodes = self.p.list(raw=True)
        for node in nodes:
            if node.name == self.name:
                self.testnode = node
                break

        assert node is not None

    # '''

    def test_list_keys(self):
        HEADING()
        self.keys = self.p.keys()
        # pprint(self.keys)

        print(Printer.flatwrite(self.keys,
                                sort_keys=["name"],
                                order=["name", "fingerprint"],
                                header=["Name", "Fingerprint"])
              )

    def test_key_upload(self):
        HEADING()

        key = SSHkey()
        print(key.__dict__)

        self.p.key_upload(key)

        self.test_list_keys()

    def test_list_secgroups(self):
        HEADING()
        secgroups = self.p.list_secgroups()
        for secgroup in secgroups:
            print(secgroup["name"])
            rules = self.p.list_secgroup_rules(secgroup["name"])

            print(Printer.write(rules,
                                sort_keys=[
                                    "ip_protocol", "from_port", "to_port",
                                    "ip_range"],
                                order=["ip_protocol", "from_port", "to_port",
                                       "ip_range"],
                                header=["ip_protocol", "from_port", "to_port",
                                        "ip_range"])
                  )

    def test_secgroups_add(self):
        self.p.add_secgroup(self.secgroupname)
        self.test_list_secgroups()

    def test_secgroup_rules_add(self):
        rules = [self.secgrouprule]
        self.p.add_rules_to_secgroup(self.secgroupname, rules)
        self.test_list_secgroups()

    def test_secgroup_rules_remove(self):
        rules = [self.secgrouprule]
        self.p.remove_rules_from_secgroup(self.secgroupname, rules)
        self.test_list_secgroups()

    def test_secgroups_remove(self):
        self.p.remove_secgroup(self.secgroupname)
        self.test_list_secgroups()

    def test_publicIP_attach(self):
        HEADING()
        pubip = self.p.get_public_ip()
        pprint(pubip)
        nodes = self.p.list(raw=True)
        for node in nodes:
            if node.name == self.name:
                self.testnode = node
                break
        if self.testnode:
            print("attaching public IP...")
            self.p.attach_publicIP(self.testnode, pubip)
            time.sleep(5)
        self.test_list_vm()

    def test_publicIP_detach(self):
        print("detaching and removing public IP...")
        time.sleep(5)
        nodes = self.p.list(raw=True)
        for node in nodes:
            if node.name == self.name:
                self.testnode = node
                break
        ipaddr = self.testnode.public_ips[0]
        pubip = self.p.cloudman.ex_get_floating_ip(ipaddr)
        self.p.detach_publicIP(self.testnode, pubip)
        time.sleep(5)
        self.test_list_vm()

    # def test_11_printer(self):
    #    HEADING()
    #    nodes = self.p.list()

    #    print(Printer.write(nodes, order=["name", "image", "size"]))

    # def test_start(self):
    #    HEADING()
    #    self.p.start(name=self.name)

    # def test_12_list_vm(self):
    #    self.test_list_vm()

    def test_info(self):
        HEADING()
        self.p.info(name=self.name)

    def test_destroy(self):
        HEADING()
        self.p.destroy(names=self.name)
        nodes = self.p.list()
        node = self.p.find(nodes, name=self.name)

        pprint(node)

        assert node["extra"]["task_state"] == "deleting"

    def test_list_vm(self):
        self.test_list_vm()

    def test_vm_login(self):
        self.test_list_vm()
        self.test_10_create()
        # use the self.testnode for this test
        time.sleep(30)
        self.test_11_publicIP_attach()
        time.sleep(5)
        nodes = self.p.list(raw=True)
        for node in nodes:
            if node.name == self.name:
                self.testnode = node
                break
        # pprint (self.testnode)
        # pprint (self.testnode.public_ips)
        pubip = self.testnode.public_ips[0]

        COMMAND = "cat /etc/*release*"

        ssh = subprocess.Popen(
            ["ssh", "%s@%s" % (self.clouduser, pubip), COMMAND],
            shell=False,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
        result = ssh.stdout.readlines()
        if result == []:
            error = ssh.stderr.readlines()
            print("ERROR: %s" % error)
        else:
            print("RESULT:")
            for line in result:
                line = line.decode("utf-8")
                print(line.strip("\n"))

        self.test_destroy()
        self.test_list_vm()

    def test_benchmark(self):
        Benchmark.print(csv=True, sysinfo=False, tag=CLOUD)
Esempio n. 14
0
class TestName:
    def setup(self):
        banner("setup", c="-")
        self.user = Config()["cloudmesh"]["profile"]["user"]
        self.clouduser = '******'
        self.name_generator = Name(experiment="exp",
                                   group="grp",
                                   user=self.user,
                                   kind="vm",
                                   counter=1)

        self.name = str(self.name_generator)
        self.name_generator.incr()

        self.new_name = str(self.name_generator)

        self.p = Provider(name="azure")

        self.secgroupname = "CM4TestSecGroup"
        self.secgrouprule = {
            "ip_protocol": "tcp",
            "from_port": 8080,
            "to_port": 8088,
            "ip_range": "129.79.0.0/16"
        }
        self.testnode = None

    def test_00_show_conf(self):
        HEADING()
        pprint(self.p.user)
        pprint(self.p.cloudtype)
        pprint(self.p.spec)

    def test_03_list_flavors(self):
        HEADING()
        flavors = self.p.flavors()
        # pprint(flavors)

        print(
            Printer.flatwrite(
                flavors,
                sort_keys=[
                    "name", "ram", "disk", "extra.numberOfCores", "price"
                ],
                order=["name", "ram", "disk", "extra.numberOfCores", "price"],
                header=["Name", "ram (MB)", "disk (GB)", "Cores", "price"]))
        """
        {'bandwidth': 0,
          'cm': {'cloud': 'azure',
                 'created': '2019-04-02 18:22:54.929946',
                 'driver': 'openstack',
                 'kind': 'flavor',
                 'name': 'Standard_L4s'},
          'disk': 678.0,
          'extra': {'maxDataDiskCount': 16,
                    'numberOfCores': 4,
                    'osDiskSizeInMB': 1047552},
          'id': 'Standard_L4s',
          'name': 'Standard_L4s',
          'price': 0,
          'ram': 32768,
          'updated': '2019-04-02 18:22:54.929946'},
        """

    def test_04_list_vm(self):
        HEADING()
        vms = self.p.list()
        # pprint(vms)

        # '''
        print(
            Printer.flatwrite(
                vms,
                sort_keys=["name"],
                order=[
                    "name", "state", "extra.properties.hardwareProfile.vmSize",
                    "extra.properties.storageProfile.imageReference.sku",
                    "extra.properties.storageProfile.osDisk.osType",
                    "extra.properties.storageProfile.osDisk.diskSizeGB",
                    "extra.properties.osProfile.adminUsername", "private_ips",
                    "public_ips"
                ],
                header=[
                    "Name", "State", "vmSize", "Image", "OS Type", "Disk (GB)",
                    "Admin User", "Private ips", "Public ips"
                ]))
        # '''

    def test_17_list_images(self):
        HEADING()
        publishername = 'Canonical'
        offername = 'UbuntuServer'
        skuname = '18.04-LTS'
        images = self.p.images(raw=False,
                               publisher=publishername,
                               offer=offername,
                               sku=skuname)
        # pprint (images)

        print(
            Printer.flatwrite(images,
                              sort_keys=["id", "name"],
                              order=["name", "id", 'version'],
                              header=["Name", "id", 'Version']))
        """
        {'cloud': 'azure',
          'driver': 'azure_asm',
          'extra': {'affinity_group': '',
                    'category': 'Public',
                    'description': '<p>Visual Studio Community 2015 Update 3 is our '
                                   'free, full featured and extensible IDE for '
                                   'non-enterprise application development. This image '
                                   'contains Windows Server 2012 R2 with Visual Studio '
                                   'Community 2015 Update 3. It allows you to easily '
                                   'and quickly set up a development environment in '
                                   'Azure to build and test applications using Visual '
                                   'Studio.</p><p>This image was created from the '
                                   'latest bits available on 8/20/2018.</p><p>The '
                                   'Visual Studio software is preinstalled in this VM, '
                                   'but you must acquire a Visual Studio subscription '
                                   'separately which allows you to sign in to and run '
                                   'Visual Studio on this VM.</p>',
                    'location': 'Southeast Asia;Australia East;Australia '
                                'Southeast;Brazil South;Canada Central;North '
                                'Europe;West Europe;Central India;South India;West '
                                'India;Japan East;Japan West;UK South;UK West;Central '
                                'US;South Central US;West US 2;West Central US',
                    'media_link': '',
                    'os': 'Windows',
                    'vm_image': False},
          'id': '03f55de797f546a1b29d1b8d66be687a__VS-2015-Comm-VSU3-AzureSDK-29-WS2012R2-2018-08-20',
          'kind': 'image',
          'name': 'Visual Studio Community 2015 Update 3 with Azure SDK 2.9 on Windows '
                  'Server 2012 R2'},

        """

    # '''
    def test_10_create(self):
        HEADING()
        image = "Canonical:UbuntuServer:18.04-LTS:18.04.201903200"
        size = "Standard_B1s"
        resgroup = 'xxx'
        storage_account = 'xxx'
        blob_container = 'xxx'
        network = 'xxx'
        subnet = 'xxx'
        sshpubkey = 'ssh-rsa YOUR_KEY_STRING'
        self.p.create(name=self.name,
                      image=image,
                      size=size,
                      sshpubkey=sshpubkey,
                      resource_group=resgroup,
                      storage_account=storage_account,
                      blob_container=blob_container,
                      network=network,
                      subnet=subnet)
        time.sleep(5)
        nodes = self.p.list()
        node = self.p.find(nodes, name=self.name)
        pprint(node)

        nodes = self.p.list(raw=True)
        for node in nodes:
            if node.name == self.name:
                self.testnode = node
                break

        assert node is not None
class TestName:
    def setup(self):
        banner("setup", c="-")
        self.user = Config()["cloudmesh"]["profile"]["user"]
        self.clouduser = '******'
        self.name_generator = Name(experiment="exp",
                                   group="grp",
                                   user=self.user,
                                   kind="vm",
                                   counter=1)

        self.name = str(self.name_generator)
        self.name_generator.incr()

        self.new_name = str(self.name_generator)

        self.p = Provider(name="azure")

        self.secgroupname = "CM4TestSecGroup"
        self.secgrouprule = {
            "ip_protocol": "tcp",
            "from_port": 8080,
            "to_port": 8088,
            "ip_range": "129.79.0.0/16"
        }
        self.testnode = None

    def test_00_show_conf(self):
        HEADING()
        pprint(self.p.user)
        pprint(self.p.cloudtype)
        pprint(self.p.spec)

    def test_03_list_flavors(self):
        HEADING()
        flavors = self.p.flavors()
        #pprint(flavors)

        print(
            Printer.flatwrite(
                flavors,
                sort_keys=("name", "ram", "disk", "extra.numberOfCores",
                           "price"),
                order=["name", "ram", "disk", "extra.numberOfCores", "price"],
                header=["Name", "ram (MB)", "disk (GB)", "Cores", "price"]))
        """
        {'bandwidth': 0,
          'cm': {'cloud': 'azure',
                 'created': '2019-04-02 18:22:54.929946',
                 'driver': 'openstack',
                 'kind': 'flavor',
                 'name': 'Standard_L4s'},
          'disk': 678.0,
          'extra': {'maxDataDiskCount': 16,
                    'numberOfCores': 4,
                    'osDiskSizeInMB': 1047552},
          'id': 'Standard_L4s',
          'name': 'Standard_L4s',
          'price': 0,
          'ram': 32768,
          'updated': '2019-04-02 18:22:54.929946'},
        """

    def test_04_list_vm(self):
        HEADING()
        vms = self.p.list()
        #pprint(vms)

        #'''
        print(
            Printer.flatwrite(
                vms,
                sort_keys=("name"),
                order=[
                    "name", "state", "extra.properties.hardwareProfile.vmSize",
                    "extra.properties.storageProfile.imageReference.sku",
                    "extra.properties.storageProfile.osDisk.osType",
                    "extra.properties.storageProfile.osDisk.diskSizeGB",
                    "extra.properties.osProfile.adminUsername", "private_ips",
                    "public_ips"
                ],
                header=[
                    "Name", "State", "vmSize", "Image", "OS Type", "Disk (GB)",
                    "Admin User", "Private ips", "Public ips"
                ]))
Esempio n. 16
0
class TestName:
    def setup(self):
        banner("setup", c="-")
        self.user = Config()["cloudmesh"]["profile"]["user"]
        self.clouduser = '******'
        self.name_generator = Name(experiment="exp",
                                   group="grp",
                                   user=self.user,
                                   kind="vm",
                                   counter=1)

        self.name = str(self.name_generator)
        self.name_generator.incr()

        self.new_name = str(self.name_generator)

        variables = Variables()
        cloud = variables['cloud']

        self.p = Provider(name=cloud)

        self.secgroupname = "CM4TestSecGroup"
        self.secgrouprule = {
            "ip_protocol": "tcp",
            "from_port": 8080,
            "to_port": 8088,
            "ip_range": "129.79.0.0/16"
        }
        self.testnode = None

    def test_001_list_keys(self):
        HEADING()
        pprint(self.p.user)
        pprint(self.p.cloudtype)
        pprint(self.p.spec)

    def test_01_list_keys(self):
        HEADING()
        self.keys = self.p.keys()
        # pprint(self.keys)

        print(
            Printer.flatwrite(self.keys,
                              sort_keys=["name"],
                              order=["name", "fingerprint"],
                              header=["Name", "Fingerprint"]))

    def test_02_key_upload(self):
        HEADING()

        key = SSHkey()
        print(key.__dict__)

        self.p.key_upload(key)

        self.test_01_list_keys()

    def test_03_list_images(self):
        HEADING()
        images = self.p.images()
        # pprint(images)

        print(
            Printer.flatwrite(
                images,
                sort_keys=["name", "extra.minDisk"],
                order=["name", "extra.minDisk", "updated", "driver"],
                header=["Name", "MinDisk", "Updated", "Driver"]))

    def test_04_list_flavors(self):
        HEADING()
        flavors = self.p.flavors()
        # pprint (flavors)

        print(
            Printer.flatwrite(flavors,
                              sort_keys=["name", "vcpus", "disk"],
                              order=["name", "vcpus", "ram", "disk"],
                              header=["Name", "VCPUS", "RAM", "Disk"]))

    def test_04_list_vm(self):
        HEADING()
        vms = self.p.list()
        # pprint (vms)

        print(
            Printer.flatwrite(vms,
                              sort_keys=("name"),
                              order=[
                                  "name", "state", "extra.task_state",
                                  "extra.vm_state", "extra.userId",
                                  "extra.key_name", "private_ips", "public_ips"
                              ],
                              header=[
                                  "Name", "State", "Task state", "VM state",
                                  "User Id", "SSHKey", "Private ips",
                                  "Public ips"
                              ]))

    def test_05_list_secgroups(self):
        HEADING()
        secgroups = self.p.list_secgroups()
        for secgroup in secgroups:
            print(secgroup["name"])
            rules = self.p.list_secgroup_rules(secgroup["name"])
            print(
                Printer.write(
                    rules,
                    sort_keys=("ip_protocol", "from_port", "to_port",
                               "ip_range"),
                    order=["ip_protocol", "from_port", "to_port", "ip_range"],
                    header=["ip_protocol", "from_port", "to_port",
                            "ip_range"]))

    def test_06_secgroups_add(self):
        HEADING()
        self.p.add_secgroup(self.secgroupname)
        self.test_05_list_secgroups()

    def test_07_secgroup_rules_add(self):
        HEADING()
        rules = [self.secgrouprule]
        self.p.add_rules_to_secgroup(self.secgroupname, rules)
        self.test_05_list_secgroups()

    def test_08_secgroup_rules_remove(self):
        HEADING()
        rules = [self.secgrouprule]
        self.p.remove_rules_from_secgroup(self.secgroupname, rules)
        self.test_05_list_secgroups()

    def test_09_secgroups_remove(self):
        HEADING()
        self.p.remove_secgroup(self.secgroupname)
        self.test_05_list_secgroups()

    def test_10_create(self):
        HEADING()
        image = "CC-Ubuntu16.04"
        size = "m1.medium"
        self.p.create(
            name=self.name,
            image=image,
            size=size,
            # username as the keypair name based on
            # the key implementation logic
            ex_keyname=self.user,
            ex_security_groups=['default'])
        time.sleep(5)
        nodes = self.p.list()
        node = self.p.find(nodes, name=self.name)
        pprint(node)

        nodes = self.p.list(raw=True)
        for node in nodes:
            if node.name == self.name:
                self.testnode = node
                break

        assert node is not None

    def test_11_publicIP_attach(self):
        HEADING()
        pubip = self.p.get_publicIP()
        pprint(pubip)
        nodes = self.p.list(raw=True)
        for node in nodes:
            if node.name == self.name:
                self.testnode = node
                break
        if self.testnode:
            print("attaching public IP...")
            self.p.attach_publicIP(self.testnode, pubip)
            time.sleep(5)
        self.test_04_list_vm()

    def test_12_publicIP_detach(self):
        HEADING()
        print("detaching and removing public IP...")
        time.sleep(5)
        nodes = self.p.list(raw=True)
        for node in nodes:
            if node.name == self.name:
                self.testnode = node
                break
        ipaddr = self.testnode.public_ips[0]
        pubip = self.p.cloudman.ex_get_floating_ip(ipaddr)
        self.p.detach_publicIP(self.testnode, pubip)
        time.sleep(5)
        self.test_04_list_vm()

    # def test_11_printer(self):
    #    HEADING()
    #    nodes = self.p.list()

    #    print(Printer.write(nodes, order=["name", "image", "size"]))

    # def test_01_start(self):
    #    HEADING()
    #    self.p.start(name=self.name)

    # def test_12_list_vm(self):
    #    self.test_04_list_vm()

    def test_13_info(self):
        HEADING()
        self.p.info(name=self.name)

    def test_14_destroy(self):
        HEADING()
        self.p.destroy(names=self.name)
        nodes = self.p.list()
        node = self.p.find(nodes, name=self.name)

        pprint(node)

        assert node["extra"]["task_state"] == "deleting"

    def test_15_list_vm(self):
        HEADING()
        self.test_04_list_vm()

    def test_16_vm_login(self):
        HEADING()
        self.test_04_list_vm()
        self.test_10_create()
        # use the self.testnode for this test
        time.sleep(30)
        self.test_11_publicIP_attach()
        time.sleep(5)
        nodes = self.p.list(raw=True)
        for node in nodes:
            if node.name == self.name:
                self.testnode = node
                break
        # pprint (self.testnode)
        # pprint (self.testnode.public_ips)
        pubip = self.testnode.public_ips[0]

        command = "cat /etc/*release*"

        ssh = subprocess.Popen(
            ["ssh", "%s@%s" % (self.clouduser, pubip), command],
            shell=False,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
        result = ssh.stdout.readlines()
        if result == []:
            error = ssh.stderr.readlines()
            print("ERROR: %s" % error)
        else:
            print("RESULT:")
            for line in result:
                line = line.decode("utf-8")
                print(line.strip("\n"))

        self.test_14_destroy()
        self.test_04_list_vm()
Esempio n. 17
0
    def do_vm(self, args, arguments):
        """
        ::

            Usage:
                vm ping [NAMES] [--cloud=CLOUDS] [--count=N]
                vm check [NAMES] [--cloud=CLOUDS] [--username=USERNAME]
                vm status [NAMES] [--cloud=CLOUDS] [--output=OUTPUT]
                vm console [NAME] [--force]
                vm log [NAME] [--force]
                vm stop [NAMES]  [--dryrun]
                vm start [NAMES] [--dryrun]
                vm terminate [NAMES] [--cloud=CLOUD] [--dryrun]
                vm delete [NAMES] [--cloud=CLOUD] [--dryrun]
                vm refresh [--cloud=CLOUDS]
                vm list [NAMES]
                        [--cloud=CLOUDS]
                        [--output=OUTPUT]
                        [--refresh]
                vm boot [--n=COUNT]
                        [--name=VMNAMES]
                        [--cloud=CLOUD]
                        [--username=USERNAME]
                        [--image=IMAGE]
                        [--flavor=FLAVOR]
                        [--network=NETWORK]
                        [--public]
                        [--secgroup=SECGROUPs]
                        [--group=GROUPs]
                        [--key=KEY]
                        [--dryrun]
                        [-v]
                vm meta list [NAME]
                vm meta set [NAME] KEY=VALUE...
                vm meta delete [NAME] KEY...
                vm script [--name=NAMES]
                          [--username=USERNAME]
                          [--key=KEY]
                          [--dryrun]
                          [--dir=DESTINATION]
                          SCRIPT
                vm ip assign [NAMES]
                          [--cloud=CLOUD]
                vm ip show [NAMES]
                           [--group=GROUP]
                           [--cloud=CLOUD]
                           [--output=OUTPUT]
                           [--refresh]
                vm ip inventory [NAMES]
                vm ssh [NAMES]
                       [--username=USER]
                       [--quiet]
                       [--ip=IP]
                       [--key=KEY]
                       [--command=COMMAND]
                vm put SOURCE DESTINATION [NAMES]
                vm get SOURCE DESTINATION [NAMES]
                vm rename [OLDNAMES] [NEWNAMES] [--force] [--dryrun]
                vm wait [--cloud=CLOUD] [--interval=INTERVAL] [--timeout=TIMEOUT]
                vm info [--cloud=CLOUD]
                        [--output=OUTPUT]
                vm username USERNAME [NAMES] [--cloud=CLOUD]
                vm resize [NAMES] [--size=SIZE]

            Arguments:
                OUTPUT         the output format
                COMMAND        positional arguments, the commands you want to
                               execute on the server(e.g. ls -a) separated by ';',
                               you will get a return of executing result instead of login to
                               the server, note that type in -- is suggested before
                               you input the commands
                NAME           server name. By default it is set to the name of last vm from database.
                NAMES          server name. By default it is set to the name of last vm from database.
                KEYPAIR_NAME   Name of the vm keypair to be used to create VM. Note this is
                               not a path to key.
                NEWNAMES       New names of the VM while renaming.
                OLDNAMES       Old names of the VM while renaming.

            Options:
                -v             verbose, prints the dict at the end
                --output=OUTPUT   the output format
                -H --modify-knownhosts  Do not modify ~/.ssh/known_hosts file
                                      when ssh'ing into a machine
                --username=USERNAME   the username to login into the vm. If not
                                      specified it will be guessed
                                      from the image name and the cloud
                --ip=IP          give the public ip of the server
                --cloud=CLOUD    give a cloud to work on, if not given, selected
                                 or default cloud will be used
                --count=COUNT    give the number of servers to start
                --detail         for table, a brief version
                                 is used as default, use this flag to print
                                 detailed table
                --flavor=FLAVOR  give the name or id of the flavor
                --group=GROUP          give the group name of server
                --secgroup=SECGROUP    security group name for the server
                --image=IMAGE    give the name or id of the image
                --key=KEY        specify a key to use, input a string which
                                 is the full path to the private key file
                --keypair_name=KEYPAIR_NAME   Name of the vm keypair to
                                              be used to create VM.
                                              Note this is not a path to key.
                --user=USER      give the user name of the server that you want
                                 to use to login
                --name=NAME      give the name of the virtual machine
                --force          rename/ delete vms without user's confirmation
                --command=COMMAND
                                 specify the commands to be executed


            Description:
                commands used to boot, start or delete servers of a cloud

                vm default [options...]
                    Displays default parameters that are set for vm boot either
                    on the default cloud or the specified cloud.

                vm boot [options...]
                    Boots servers on a cloud, user may specify flavor, image
                    .etc, otherwise default values will be used, see how to set
                    default values of a cloud: cloud help

                vm start [options...]
                    Starts a suspended or stopped vm instance.

                vm stop [options...]
                    Stops a vm instance .

                vm delete [options...]

                    Delete servers of a cloud, user may delete a server by its
                    name or id, delete servers of a group or servers of a cloud,
                    give prefix and/or range to find servers by their names.
                    Or user may specify more options to narrow the search

                vm floating_ip_assign [options...]
                    assign a public ip to a VM of a cloud

                vm ip show [options...]
                    show the ips of VMs

                vm ssh [options...]
                    login to a server or execute commands on it

                vm list [options...]
                    same as command "list vm", please refer to it

                vm status [options...]
                    Retrieves status of last VM booted on cloud and displays it.

                vm refresh [--cloud=CLOUDS]
                    this command refreshes the data for virtual machines,
                    images and flavors for the specified clouds.

                vm ping [NAMES] [--cloud=CLOUDS] [--count=N] [--processors=PROCESSORS]
                     pings the specified virtual machines, while using at most N pings.
                     The ping is executed in parallel.
                     If names are specifies the ping is restricted to the given names in
                     parameter format. If clouds are specified, names that are not in
                     these clouds are ignored. If the name is set in the variables
                     this name is used.

                cms vm ssh --command=\"uname -a\"

                      executes the uname command on the last booted vm

                vm script [--name=NAMES]
                          [--username=USERNAME]
                          [--key=KEY]
                          [--dryrun]
                          [--dir=DESTINATION]
                          [--shell=SHELL]
                          SCRIPT

                   The script command copies a shell script to the specified vms
                   into the DESTINATION directory and than execute it. With
                   SHELL you can set the shell for executing the command,
                   this coudl even be a python interpreter. Examples for
                   SHELL are /bin/sh, /usr/bin/env python

                vm put SOURCE DESTINATION [NAMES]

                    puts the file defined by SOURCE into the DESINATION folder
                    on the specified machines. If the file exists it is
                    overwritten, so be careful.

                vm get SOURCE DESTINATION [NAMES]

                    gets  the file defined by SOURCE into the DESINATION folder
                    on the specified machines. The SOURCE is on the remote
                    machine. If one machine is specified, the SOURCE is the same
                    name as on the remote machine. If multiple machines are
                    specified, the name of the machine will be a prefix to the
                    filename. If the filenames exists, they will be overwritten,
                    so be careful.

            Tip:
                give the VM name, but in a hostlist style, which is very
                convenient when you need a range of VMs e.g. sample[1-3]
                => ['sample1', 'sample2', 'sample3']
                sample[1-3,18] => ['sample1', 'sample2', 'sample3', 'sample18']

            Quoting commands:
                cm vm login gregor-004 --command=\"uname -a\"

            Limitations:

                Azure: rename is not supported
        """

        map_parameters(arguments, 'active', 'cloud', 'command', 'dryrun',
                       'flavor', 'force', 'group'
                       'output', 'group', 'image', 'interval', 'timeout', 'ip',
                       'key', 'modify-knownhosts', 'n', 'name', 'public',
                       'quiet', 'secgroup', 'size', 'username', 'output',
                       'count', 'network', 'refresh')

        variables = Variables()
        database = CmDatabase()

        arguments.output = Parameter.find("output", arguments, variables,
                                          "table")

        arguments.refresh = Parameter.find_bool("refresh", arguments,
                                                variables)

        if (arguments.meta and arguments.list):

            name = arguments.NAME
            if arguments.NAME is None:
                name = variables['vm']
                if name is None:
                    Console.error("No vm specified")

            cloud = "chameleon"
            # cloud = Parameter.find(arguments, variables)
            print(f"vm metadata for {name} on {cloud}")

            provider = Provider(name=cloud)
            r = provider.get_server_metadata(name)
            print(r)

        elif arguments.meta and arguments.set:

            metadata = {}
            pairs = arguments['KEY=VALUE']
            for pair in pairs:
                key, value = pair.split("=", 1)
                metadata[key] = value

            name = arguments.NAME
            if arguments.NAME is None:
                name = variables['vm']
                if name is None:
                    Console.error("No vm specified")

            cloud = "chameleon"
            # cloud = Parameter.find(arguments, variables)
            print(f"cloud {cloud} {name}")

            provider = Provider(name=cloud)
            provider.set_server_metadata(name, **metadata)
            r = provider.get_server_metadata(name)

            pprint(r)

        elif arguments.meta and arguments.delete:

            metadata = {}
            keys = arguments['KEY']

            name = arguments.NAME
            if arguments.NAME is None:
                name = variables['vm']
                if name is None:
                    Console.error("No vm specified")

            cloud = "chameleon"
            # cloud = Parameter.find(arguments, variables)
            print(f"cloud {cloud} {name}")

            provider = Provider(name=cloud)

            for key in keys:
                provider.delete_server_metadata(name, key)

            r = provider.get_server_metadata(name)

            pprint(r)

        elif arguments.list and arguments.refresh:

            names = []

            clouds, names = Arguments.get_cloud_and_names(
                "list", arguments, variables)

            for cloud in clouds:
                print(f"cloud {cloud}")
                provider = Provider(name=cloud)
                vms = provider.list()

                provider.Print(vms, output=arguments.output, kind="vm")

                return ""

        elif arguments.list:

            names = []

            clouds, names = Arguments.get_cloud_and_names(
                "list", arguments, variables)

            try:

                for cloud in clouds:
                    print(f"List {cloud}")
                    p = Provider(cloud)
                    kind = p.kind

                    collection = "{cloud}-vm".format(cloud=cloud, kind=p.kind)
                    db = CmDatabase()
                    vms = db.find(collection=collection)

                    p.Print(vms, output=arguments.output, kind="vm")

            except Exception as e:
                Console.error("Error in listing ", traceflag=True)
                VERBOSE(e)

            return ""

        elif arguments.ping:
            """
            vm ping [NAMES] [--cloud=CLOUDS] [--count=N]
            """
            if arguments.NAMES:
                variables['vm'] = arguments.NAMES
            if arguments['--cloud']:
                variables['cloud'] = arguments['--cloud']
            clouds, names = Arguments.get_cloud_and_names(
                "status", arguments, variables)

            count = arguments.count
            if arguments.count:
                count = int(count)
            else:
                count = 1

            def get_ips():
                ips = []
                for cloud in clouds:
                    params = {}
                    # gets public ips from database
                    cursor = database.db[f'{cloud}-vm']
                    for name in names:
                        for node in cursor.find({'name': name}):
                            ips.append(node['ip_public'])
                    ips = list(set(ips))
                    pprint(ips)
                return ips

            ips = get_ips()
            if len(ips) == 0:
                Console.warning("no public ip found.")
                for cloud in clouds:
                    print(f"refresh for cloud {cloud}")
                    provider = Provider(name=cloud)
                    vms = provider.list()
                ips = get_ips()

            if len(ips) == 0:
                Console.error("No vms with public IPS found.")
                Console.error("  Make sure to use cms vm list --refresh")

            for ip in ips:
                result = Shell.ping(host=ip, count=count)
                banner(ip)
                print(result)
                print()

        elif arguments.check:

            raise NotImplementedError
            """
            vm check [NAMES] [--cloud=CLOUDS] [--username=USERNAME]
            """
            """
            
            THIS IS ALL WRONG AS PROVIDER DEPENDENT !!!
            
            if arguments.NAMES:
                variables['vm'] = arguments.NAMES
            if arguments['--cloud']:
                variables['cloud'] = arguments['--cloud']
            clouds, names = Arguments.get_cloud_and_names("status", arguments, variables)

            for cloud in clouds:
                provider = Provider(cloud)
                params = {}

                params['key'] = \
                    provider.p.spec["credentials"]['EC2_PRIVATE_KEY_FILE_PATH'] + \
                    provider.p.spec["credentials"]['EC2_PRIVATE_KEY_FILE_NAME']

                params['username'] = arguments['--username']  # or get from db

                processors = arguments['--processors']
                if processors:
                    params['processors'] = int(processors[0])

                # gets public ips from database
                public_ips = []
                cursor = database.db['{cloud}-vm']
                for name in names:
                    for node in cursor.find({'name': name}):
                        public_ips.append(node['public_ips'])
                public_ips = [y for x in public_ips for y in x]

                Host.check(hosts=public_ips, **params)
            """

        elif arguments.status:
            if arguments.NAMES:
                variables['vm'] = arguments.NAMES
            if arguments['--cloud']:
                variables['cloud'] = arguments['--cloud']
            clouds, names = Arguments.get_cloud_and_names(
                "status", arguments, variables)

            # gets status from database
            for cloud in clouds:
                provider = Provider(cloud)
                status = []
                cursor = database.db[f'{cloud}-vm']
                print(cloud)
                for name in names:
                    for node in cursor.find({'name': name}):
                        status.append(node)

                provider.Print(status, output=arguments.output, kind="status")
                return ""

        elif arguments.start:
            # TODO: not tested
            if arguments.NAMES:
                names = variables['vm'] = arguments.NAMES

            if arguments['--cloud']:
                variables['cloud'] = arguments['--cloud']
            clouds, names = Arguments.get_cloud_and_names(
                "stop", arguments, variables)

            cloud = clouds[0]
            print(cloud)
            print(names)

            for name in names:

                provider = Provider(cloud)

                if arguments['--dryrun']:
                    print(f"start node {name}")
                else:
                    vms = provider.start(name=name, cloud=cloud)

                    provider.Print(vms, output=arguments.output, kind="vm")

            return ""

        elif arguments.stop:
            # TODO: not tested

            if arguments.NAMES:
                variables['vm'] = arguments.NAMES
            if arguments['--cloud']:
                variables['cloud'] = arguments['--cloud']
            clouds, names = Arguments.get_cloud_and_names(
                "stop", arguments, variables)

            for cloud in clouds:
                params = {}
                provider = Provider(cloud)

                if arguments['--dryrun']:
                    Console.ok(f"Dryrun stop: "
                               f"        {cloud}\n"
                               f"        {names}"
                               f"        {provider}")
                else:
                    for name in names:
                        vms = provider.stop(name)

                    provider.Print(vms, output=arguments.output, kind="vm")

        elif arguments.terminate:
            # TODO: not tested

            if arguments.NAMES:
                variables['vm'] = arguments.NAMES
            if arguments['--cloud']:
                variables['cloud'] = arguments['--cloud']
            clouds, names = Arguments.get_cloud_and_names(
                "stop", arguments, variables)

            for cloud in clouds:
                params = {}
                provider = Provider(cloud)

                if arguments['--dryrun']:
                    Console.ok(f"Dryrun terminate: "
                               f"        {cloud}\n"
                               f"        {names}"
                               f"        {provider}")
                else:
                    for name in names:
                        vms = provider.destroy(name)

                    provider.Print(vms, output=arguments.output, kind="vm")

        elif arguments.delete:

            if arguments.NAMES:
                variables['vm'] = arguments.NAMES
            if arguments['--cloud']:
                variables['cloud'] = arguments['--cloud']
            clouds, names = Arguments.get_cloud_and_names(
                "stop", arguments, variables)

            if names is not None:
                pass
            elif clouds is not None:
                for cloud in clouds:
                    provider = Provider(cloud)
                    vms = provider.list()
                    for vm in vms:
                        r = provider.destroy(name=vm)
                return ""
            else:
                return ""

            for cloud in clouds:
                provider = Provider(cloud)
                vms = provider.list()
                for vm in vms:
                    name = vm["cm"]["name"]
                    if name in names:
                        r = provider.destroy(name=name)

        # TODO: username, secgroup
        elif arguments.boot:
            # not everything works
            """
                vm boot 
                        [--name=VMNAMES]
                        [--cloud=CLOUD]
                        [--username=USERNAME]
                        [--image=IMAGE]
                        [--flavor=FLAVOR]
                        [--network=NETWORK]
                        [--public]
                        [--secgroup=SECGROUP]
                        [--key=KEY]
                        [--group=GROUP]
                        [--dryrun]
            """
            # for name in names:
            #    node = p.create(name=name, size=flavor, image=image)

            # VERBOSE(arguments)
            parameters = dotdict()

            names = Parameter.expand(arguments.name)

            cloud = Parameter.find("cloud", arguments, variables.dict())
            defaults = Config()[f"cloudmesh.cloud.{cloud}.default"]
            groups = Parameter.find("group", arguments, variables.dict(),
                                    {"group": "default"})

            parameters = dotdict()

            # parameters.names = arguments.name

            parameters.group = groups
            for attribute in [
                    "image", "username", "flavor", "key", "network", "secgroup"
            ]:
                parameters[attribute] = Parameter.find(attribute, arguments,
                                                       variables.dict(),
                                                       defaults)

            if arguments.username is None:
                parameters.user = Image.guess_username(parameters.image)

            provider = Provider(name=cloud)

            parameters.secgroup = arguments.secgroup or "default"

            #
            # determine names
            #

            if names and arguments.n and len(names) > 1:
                Console.error(
                    f"When using --n={arguments.n}, you can only specify one name"
                )
                return ""
            # cases
            #

            # only name --name = "a[1,2]"
            # name and count # --name="a" --n=3, names must be of length 1
            # only count --n=2 names are read form var
            # nothing, just use one vm

            # determin names
            _names = []
            if not names:

                if not arguments.n:
                    count = 1
                else:
                    count = int(arguments.n)

                for i in range(0, count):
                    if names is None:
                        n = Name()
                        n.incr()
                        name = str(n)
                    else:
                        n = names[i]
                        name = str(n)
                    _names.append(name)
                names = _names

            elif len(names) == 1 and arguments.n:

                name = names[0]
                for i in range(0, int(arguments.n)):
                    _names.append(f"{name}-{i}")
                names = _names

            # pprint(parameters)

            for name in names:

                parameters.name = name
                if arguments['--dryrun']:
                    banner("boot")

                    pprint(parameters)

                    Console.ok(f"Dryrun boot {name}: \n"
                               f"        cloud={cloud}\n"
                               f"        names={names}\n"
                               f"        provider={provider}")
                    print()
                    for attribute in parameters:
                        value = parameters[attribute]
                        Console.ok(f"        {attribute}={value}")

                else:

                    # parameters.progress = len(parameters.names) < 2

                    try:
                        vms = provider.create(**parameters)
                    except TimeoutError:
                        Console.error(
                            f"Timeout during vm creation. There may be a problem with the cloud {cloud}"
                        )

                    except Exception as e:
                        Console.error("create problem", traceflag=True)
                        print(e)
                        return ""

                    variables['vm'] = str(n)
                    if arguments["-v"]:
                        banner("Details")
                        pprint(vms)

            # provider.Print(arguments.output, "vm", vms)

        elif arguments.info:
            """
            vm info [--cloud=CLOUD] [--output=OUTPUT]
            """
            print("info for the vm")

            cloud, names = Arguments.get_cloud_and_names(
                "info", arguments, variables)

            raise NotImplementedError

        elif arguments.rename:
            raise NotImplementedError
            # Not tested
            print("rename the vm")

            v = Variables()
            cloud = v["cloud"]

            p = Provider(cloud)

            try:
                oldnames = Parameter.expand(arguments["OLDNAMES"])
                newnames = Parameter.expand(arguments["NEWNAMES"])
                force = arguments["--force"]

                if oldnames is None or newnames is None:
                    Console.error("Wrong VMs specified for rename",
                                  traceflag=False)
                elif len(oldnames) != len(newnames):
                    Console.error("The number of VMs to be renamed is wrong",
                                  traceflag=False)
                else:
                    print(oldnames)
                    print(newnames)
                    for i in range(0, len(oldnames)):
                        oldname = oldnames[i]
                        newname = newnames[i]
                        if arguments["--dryrun"]:
                            Console.ok("Rename {} to {}".format(
                                oldname, newname))
                        else:
                            print(f"rename {oldname} -> {newname}")

                            p.rename(source=oldname, destination=newname)

                    msg = "info. OK."
                    Console.ok(msg)
            except Exception as e:
                Error.traceback(e)
                Console.error("Problem renaming instances", traceflag=True)

        elif arguments["ip"] and arguments["show"]:
            raise NotImplementedError

            print("show the ips")
            """
            vm ip show [NAMES]
                   [--group=GROUP]
                   [--cloud=CLOUD]
                   [--output=OUTPUT]
                   [--refresh]

            """

        elif arguments["ip"] and arguments["assign"]:
            raise NotImplementedError
            """
            vm ip assign [NAMES] [--cloud=CLOUD]
            """
            print("assign the public ip")

        elif arguments["ip"] and arguments["inventory"]:
            raise NotImplementedError
            """
            vm ip inventory [NAMES]

            """
            print("list ips that could be assigned")

        elif arguments.default:
            raise NotImplementedError

            print("sets defaults for the vm")

        elif arguments.script:
            raise NotImplementedError
            clouds, names = Arguments.get_cloud_and_names(
                "run", arguments, variables)
            username = arguments['--username']
            script = arguments.SCRIPT

            for cloud in clouds:
                provider = Provider(cloud)

                name_ips = {}
                cursor = database.db['{}-node'.format(cloud)]
                for name in names:
                    for node in cursor.find({'name': name}):
                        name_ips[name] = node['public_ips']

                if arguments['--dryrun']:
                    print("run script {} on vms: {}".format(script, names))
                else:
                    provider.ssh(name_ips, username=username, script=script)

        elif arguments.username:
            raise NotImplementedError
            """
            vm username USERNAME [NAMES] [--cloud=CLOUD]
            """
            print("sets the username for the vm")

        elif arguments.resize:
            raise NotImplementedError
            """
            vm resize [NAMES] [--size=SIZE]
            """
            pass

        elif arguments.ssh:
            """
            vm ssh [NAMES] [--username=USER]
                 [--quiet]
                 [--ip=IP]
                 [--key=KEY]
                 [--command=COMMAND]
            """

            # VERBOSE(arguments)
            clouds, names, command = Arguments.get_commands(
                "ssh", arguments, variables)

            # print (clouds)
            # print(names)
            # print (command)

            if arguments.command is None and len(names) > 1:
                Console.error("Interactive shell can only be done on one vm")
                return ""
            elif arguments.command is None and len(names) == 1:
                name = names[0]
                cloud = clouds[0]
                cm = CmDatabase()
                try:
                    vm = cm.find_name(name, "vm")[0]
                except IndexError:
                    Console.error(f"could not find vm {name}")
                    return ""
                # VERBOSE(vm)
                cloud = vm["cm"]["cloud"]
                provider = Provider(name=cloud)
                try:
                    provider.ssh(vm=vm)
                except KeyError:
                    vms = provider.list()

                    provider.Print(vms, output=arguments.output, kind="vm")

                    provider.ssh(vm=vm)
                return ""
            else:
                # command on all vms

                if clouds is None or names is None or command is None:
                    return ""
                else:
                    for cloud in clouds:
                        p = Provider(cloud)
                        for name in names:
                            cm = CmDatabase()
                            try:
                                vm = cm.find_name(name, "vm")[0]
                            except IndexError:
                                Console.error(f"could not find vm {name}")
                                continue
                            r = p.ssh(vm=vm, command=command)
                            print(r)
            return ""

        elif arguments.console:

            # why is this not vm
            clouds, names, command = Arguments.get_commands(
                "ssh", arguments, variables)

            print(clouds)
            print(names)
            print(command)

            for cloud in clouds:
                p = Provider(cloud)
                for name in names:
                    cm = CmDatabase()
                    try:
                        vm = cm.find_name(name, "vm")[0]
                    except IndexError:
                        Console.error(f"could not find vm {name}")
                        continue
                    r = p.console(vm=vm)
                    print(r)

            return ""

        elif arguments.log:

            # why is this not vm
            clouds, names, command = Arguments.get_commands(
                "ssh", arguments, variables)

            print(clouds)
            print(names)
            print(command)

            for cloud in clouds:
                p = Provider(cloud)
                for name in names:
                    cm = CmDatabase()
                    try:
                        vm = cm.find_name(name, "vm")[0]
                    except IndexError:
                        Console.error(f"could not find vm {name}")
                        continue
                    r = p.log(vm=vm)
                    print(r)

            return ""

        elif arguments.wait:
            """
            vm wait [--cloud=CLOUD] [--interval=INTERVAL] [--timeout=TIMEOUT]
            """

            # why is this not vm
            clouds, names, command = Arguments.get_commands(
                "ssh", arguments, variables)

            # print (clouds)
            # print (names)
            # print (command)

            for cloud in clouds:
                p = Provider(cloud)
                for name in names:
                    cm = CmDatabase()
                    try:
                        vm = cm.find_name(name, "vm")[0]
                    except IndexError:
                        Console.error(f"could not find vm {name}")
                        continue
                    r = p.wait(vm=vm,
                               interval=arguments.interval,
                               timeout=arguments.timeout)
                    if r:
                        Console.ok("Instance available for SSH")
                    else:
                        Console.error(
                            f"Instance unavailable after timeout of {arguments.timeout}"
                        )
                    # print(r)

            return ""

        elif arguments.put:
            """
            vm put SOURCE DESTINATION
            """
            clouds, names, command = Arguments.get_commands(
                "ssh", arguments, variables)

            key = variables['key']

            source = arguments['SOURCE']
            destination = arguments['DESTINATION']
            for cloud in clouds:
                p = Provider(name=cloud)
                cm = CmDatabase()
                for name in names:
                    try:
                        vms = cm.find_name(name, "vm")
                    except IndexError:
                        Console.error(f"could not find vm {name}")
                        return ""
                    # VERBOSE(vm)
                    for vm in vms:
                        try:
                            ip = vm['public_ips']
                        except:
                            try:
                                ip = p.get_public_ip(name=name)
                            except:
                                Console.error(
                                    f"could not find a public ip for vm {name}",
                                    traceflag=True)
                                return
                            Console.error(
                                f"could not find a public ip for vm {name}",
                                traceflag=True)
                            return

                        # get the username
                        try:
                            # username not in vm...guessing
                            imagename = list(
                                cm.collection(cloud + '-image').find(
                                    {'ImageId': vm['ImageId']}))[0]['name']
                            print(imagename)
                            user = Image.guess_username(image=imagename,
                                                        cloud=cloud)
                        except:
                            try:
                                user = vm['os_profile']['admin_username']
                            except:
                                Console.error(
                                    f"could not find a valid username for "
                                    f"{name}, try refreshing the image list",
                                    traceflag=True)
                                return
                            Console.error(
                                f"could not find a valid username for {name}, try refreshing the image list"
                            )
                            return

                        cmd = f'scp -i {key} {source} {user}@{ip}:{destination}'
                        print(cmd)
                        os.system(cmd)
            return ""
Esempio n. 18
0
class TestName:
    def setup(self):
        banner("setup", c="-")
        self.user = Config()["cloudmesh"]["profile"]["user"]
        self.key_path = path_expand(
            Config()["cloudmesh"]["profile"]["publickey"])
        f = open(self.key_path, 'r')
        self.key_val = f.read()

        self.clouduser = '******'
        self.name_generator = Name(experiment="exp",
                                   group="grp",
                                   user="******",
                                   kind="vm",
                                   counter=1)

        self.name = str(self.name_generator)
        self.name_generator.incr()

        self.new_name = str(self.name_generator)

        self.p = GCloudProvider(name="google")

        self.secgroupname = "CM4TestSecGroup"
        self.secgrouprule = {
            "ip_protocol": "tcp",
            "from_port": 8080,
            "to_port": 8088,
            "ip_range": "129.79.0.0/16"
        }
        self.testnode = None

    def test_01_list_keys(self):
        HEADING()
        print("List Key Method is not supported by google")

    def test_02_key_upload(self):
        HEADING()
        print("Upload Key method is not supported by google")

    def test_03_list_images(self):
        HEADING()
        images = self.p.images()
        print(
            Printer.flatwrite(images,
                              sort_keys=("name"),
                              order=["name", "id", "driver"],
                              header=["Name", "Id", "Driver"]))

    def test_04_list_flavors(self):
        HEADING()
        flavors = self.p.flavors()
        print(
            Printer.flatwrite(flavors,
                              sort_keys=("name", "disk"),
                              order=["name", "id", "ram", "disk"],
                              header=["Name", "Id", "RAM", "Disk"]))

    def test_04_list_vm(self):
        HEADING()
        vms = self.p.list()
        print(
            Printer.flatwrite(vms,
                              sort_keys=("name"),
                              order=[
                                  "name", "state", "extra.task_state",
                                  "extra.vm_state", "extra.userId",
                                  "extra.key_name", "private_ips", "public_ips"
                              ],
                              header=[
                                  "Name", "State", "Task state", "VM state",
                                  "User Id", "SSHKey", "Private ips",
                                  "Public ips"
                              ]))

    def test_05_list_secgroups(self):
        HEADING()
        print("List security group method is not supported by google")

    def test_06_secgroups_add(self):
        print("List add security groups method is not supported by google")

    def test_07_secgroup_rules_add(self):
        print(
            "List Add security group rules method is not supported by google")

    def test_08_secgroup_rules_remove(self):
        print("Remove security group rules method is not supported by google")

    def test_09_secgroups_remove(self):
        print("Remove security groups method is not supported by google")

    def test_10_create(self):
        HEADING()
        image = "ubuntu-minimal-1810-cosmic-v20190320"
        size = "n1-standard-4"
        location = "us-central1-a"
        self.p.create(name=self.name,
                      image=image,
                      size=size,
                      location=location)
        time.sleep(5)
        nodes = self.p.list()
        node = self.p.find(nodes, name=self.name)

        nodes = self.p.list(raw=True)
        for node in nodes:
            if node.name == self.name:
                self.testnode = node
                break

        assert node is not None

    def test_11_publicIP_attach(self):
        HEADING()
        print("Attach Public IP method is not supported by google")

    def test_12_publicIP_detach(self):
        print("Detach Public IP method is not supported by google")

    def test_13_info(self):
        HEADING()
        self.p.info(name=self.name)

    def test_14_destroy(self):
        HEADING()
        self.p.destroy(name=self.name)
        nodes = self.p.list()
        node = self.p.find(nodes, name=self.name)

        assert node is None

    def test_15_list_vm(self):
        self.test_04_list_vm()

    def test_16_vm_login(self):
        self.test_04_list_vm()
        self.test_10_create()
        time.sleep(30)
        time.sleep(5)
        nodes = self.p.list(raw=True)
        for node in nodes:
            if node.name == self.name:
                self.testnode = node
                break
        pubip = self.testnode.public_ips[0]

        command = "cat /etc/*release*"

        ssh = subprocess.Popen(
            ["ssh", "%s" % (pubip), "%s" % (command)],
            #["ssh", "%s@%s" % (self.clouduser, pubip), COMMAND],
            shell=False,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
        result = ssh.stdout.readlines()
        if result == []:
            error = ssh.stderr.readlines()
            print("ERROR: %s" % error)
        else:
            print("RESULT:")
            for line in result:
                line = line.decode("utf-8")
                print(line.strip("\n"))

        self.test_14_destroy()
        self.test_04_list_vm()
Esempio n. 19
0
class Provider(ComputeNodeABC):

    ProviderMapper = {
        "google": LibcloudProvider.GCE
    }

    def __init__(self, name=None, configuration="~/.cloudmesh/cloudmesh.yaml"):
        """
        Initializes the provider. The default parameters are read from the configutation
        file that is defined in yaml format.
        :param name: The name of the provider as defined in the yaml file
        :param configuration: The location of the yaml configuration filw
        """
        HEADING(c=".")
        conf = Config(configuration)["cloudmesh"]
        #self.user = conf["profile"]
        self.user = Config()["cloudmesh"]["profile"]["user"]
        self.spec = conf["cloud"][name]
        self.cloud = name
        cred = self.spec["credentials"]
        self.cloudtype = self.spec["cm"]["kind"]
        super().__init__(name, conf)
        self.name_generator = Name(
            experiment="exp",
            group="grp",
            user = "******",
            kind="vm",
            counter=1)

        self.name = str(self.name_generator)
        self.name_generator.incr()

        self.name = str(self.name_generator)
        self.key_path = path_expand(Config()["cloudmesh"]["profile"]["publickey"])
        f = open(self.key_path, 'r')
        self.key_val = f.read()
        self.testnode = None

       

        if self.cloudtype in Provider.ProviderMapper:

            self.driver = get_driver(
                Provider.ProviderMapper[self.cloudtype])

            if self.cloudtype == 'google':
                self.cloudman = self.driver(
                        cred["client_email"],
                        cred["path_to_json_file"],
                        project = cred["project"]
                        )    
        else:
            print("Specified provider not available")
            self.cloudman = None
        self.default_image = None
        self.default_size = None
        self.public_key_path = conf["profile"]["publickey"]

    def dict(self, elements, kind=None):
        """
        Libcloud returns an object or list of objects With the dict method
        this object is converted to a dict. Typically this method is used internally.
        :param elements: the elements
        :param kind: Kind is image, flavor, or node
        :return:
        """
        if elements is None:
            return None
        elif type(elements) == list:
            _elements = elements
        else:
            _elements = [elements]
        d = []
        for element in _elements:
            entry = element.__dict__
            entry["kind"] = kind
            entry["driver"] = self.cloudtype
            entry["cloud"] = self.cloud

            if kind == 'node':
                entry["updated"] = str(datetime.utcnow())

                if "created_at" in entry:
                    entry["created"] = str(entry["created_at"])
                    del entry["created_at"]
                else:
                    entry["created"] = entry["modified"]
            elif kind == 'flavor':
                entry["created"] = entry["updated"] = str(datetime.utcnow())
            elif kind == 'image':
                if self.cloudtype == 'openstack':
                    entry['created'] = entry['extra']['created']
                    entry['updated'] = entry['extra']['updated']
                else:
                    pass

            if "_uuid" in entry:
                del entry["_uuid"]

            d.append(entry)
        return d

    def find(self, elements, name=None):
        """
        finds an element in elements with the specified name
        :param elements: The elements
        :param name: The name to be found
        :return:
        """
        for element in elements:
            if element["name"] == name:
                return element
        return None

    def images(self, raw=False):
        """
        Lists the images on the cloud
        :param raw: If raw is set to True the lib cloud object is returened
                    otherwise a dict is returened.
        :return: dict or libcloud object
        """
        if self.cloudman:
            entries = self.cloudman.list_images()
            if raw:
                return entries
            else:
                return self.dict(entries, kind="image")

        return None

    def image(self, name=None):
        """
        Gets the image with a given nmae
        :param name: The name of the image
        :return: the dict of the image
        """
        return self.find(self.images(), name=name)

    def flavors(self, raw=False):
        """
        Lists the flavors on the cloud
        :param raw: If raw is set to True the lib cloud object is returened
                    otherwise a dict is returened.
        :return: dict or libcloud object
        """
        if self.cloudman:
            entries = self.cloudman.list_sizes()
            if raw:
                return entries
            else:
                return self.dict(entries, kind="flavor")
        return None

    def flavor(self, name=None):
        """
        Gest the flavor with a given name
        :param name: The aname of the flavor
        :return: The dict of the flavor
        """
        return self.find(self.flavors(), name=name)

    def start(self, name=None):
        """
        start a node. NOT YET IMPLEMENTED.
    
        :param name: the unique node name
        :return:  The dict representing the node
        """
        HEADING(c=".")
        names=name
        nodes = self.list(raw=True)
        for node in nodes:
            if node.name in names:
                self.cloudman.ex_start_node(node)

        return None

    def stop(self, name=None):
        """
        stops the node with the given name. NOT YET IMPLEMENTED.
    
        :param name:
        :return: The dict representing the node including updated status
        """
        HEADING(c=".")
        names=name
        nodes = self.list(raw=True)
        for node in nodes:
            if node.name == names:
                self.cloudman.ex_stop_node(node)

        return None

    def info(self, name=None):
        """
        Gets the information of a node with a given name
    
        :param name: The name of teh virtual machine
        :return: The dict representing the node including updated status
        """
        return self.find(self.list(), name=name)


    def list(self, raw=False):
        """
        Lists the vms on the cloud
        :param raw: If raw is set to True the lib cloud object is returened
                    otherwise a dict is returened.
        :return: dict or libcloud object
        """
        entries = self.cloudman.list_nodes()

        if raw:
            return entries
        else:
            return self.dict(entries,kind="vm")

    def destroy(self, name=None):
        """
        Destroys the node
        :param names: the name of the node
        :return: the dict of the node
        """

        #names = Parameter.expand(names)
        names=name
        nodes = self.list(raw=True)
        for node in nodes:
            if node.name in names:
                self.cloudman.destroy_node(node)
        # bug status shoudl change to destroyed
        return None


    def create(self, name=None, image=None, size=None, location=None,  timeout=360, **kwargs):
        """
        creates a named node
    
        :param name: the name of the node
        :param image: the image used
        :param size: the size of the image
        :param timeout: a timeout in seconds that is invoked in case the image
                        does not boot. The default is set to 3 minutes.
        :param kwargs: additional arguments HEADING(c=".")ed along at time of boot
        :return:
        """
        #self.image = self.spec["default"]["image"]
        #self.flavor = self.spec["default"]["size"]
        location = self.spec["credentials"]["datacenter"]

        images = self.images(raw=True)
        image_use = None
        flavors = self.flavors(raw=True)
        flavor_use = None
        
        for _image in images:
            if _image.name == image:
                image_use = _image
                break
        for _flavor in flavors:
            if _flavor.name == size:
                flavor_use = _flavor
                break

        metadata = {"items": [{"value": self.user+":"+self.key_val, "key": "ssh-keys"}]}
        node = self.cloudman.create_node(name=name, image=image_use,
                size=flavor_use, location=location,ex_metadata=metadata, **kwargs)
     
        return self.dict(node)

    def ssh(self, name=None, command=None):
        nodes = self.list(raw=True)
        for node in nodes:
            if node.name == name:
                self.testnode = node
                break
        pubip = self.testnode.public_ips[0]
        ssh = subprocess.Popen(
            ["ssh", "%s" % (pubip), "%s" % (command)],
            shell=False,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
        result = ssh.stdout.readlines()
        if result == []:
            error = ssh.stderr.readlines()
            print("ERROR: %s" % error)
        else:
            print("RESULT:")
            for line in result:
                line = line.decode("utf-8")
                print(line.strip("\n"))

    def resume(self, name=None):
        """
        resume the named node. NOT YET IMPLEMENTED.
    
        :param name: the name of the node
        :return: the dict of the node
        """
        HEADING(c=".")
        return None

    def suspend(self, name=None):
        """
        suspends the node with the given name. NOT YET IMPLEMENTED.

        :param name: the name of the node
        :return: The dict representing the node
        """
        HEADING(c=".")
        return None

    def rename(self, name=None, destination=None):
        """
        rename a node. NOT YET IMPLEMENTED.

        :param destination:
        :param name: the current name
        :return: the dict with the new name
        """
        # if destination is None, increase the name counter and use the new name
        HEADING(c=".")
        return None
Esempio n. 20
0
key = variables['key']

#
# cms set cloud=aws
#
cloud = variables.parameter('cloud')

print(f"Test run for {cloud}")

if cloud is None:
    raise ValueError("cloud is not set")

name_generator = Name()
name_generator.set(f"test-{user}-vm-" + "{counter}")
vm_name1 = str(name_generator)
name_generator.incr()
vm_name2 = str(name_generator)

volume_name_generator = Name()
volume_name_generator.set(f"test-{user}-volume-" + "{counter}")
volume_name1 = str(volume_name_generator)
volume_name_generator.incr()
volume_name2 = str(volume_name_generator)

provider = Provider(name=cloud)
current_vms = 0


@pytest.mark.incremental
class Test_provider_volume:
    def test_cms_vm_setup(self):
Esempio n. 21
0
class TestMongo:
    def setup(self):
        self.database = CmDatabase()

        self.name = Name(experiment="exp",
                         group="grp",
                         user="******",
                         kind="vm",
                         counter=1)

    def test_00_status(self):
        HEADING()

        #print(self.name)
        #print(self.name.counter)
        #print(self.name.id(counter=100))

        self.database.clear()

        r = self.database.find()
        pprint(r)

        assert len(r) == 0

    def test_01_status(self):
        HEADING()
        r = self.database.status()
        # pprint(r)
        assert "Connection refused" not in r

        d = {}
        for field in ['uptime', 'pid', 'version', 'host']:
            d[field] = r[field]

        print(Printer.attribute(d))

        assert d is not None

    def test_02_update(self):
        HEADING()

        entries = [{"name": "Gregor"}, {"name": "Laszewski"}]

        for entry in entries:
            entry["cmid"] = str(self.name)
            entry["cmcounter"] = self.name.counter
            self.name.incr()
        self.database.update(entries)

        r = self.database.find()

        pprint(r)
        assert len(r) == 2

    def test_03_update(self):
        HEADING()

        r = self.database.find(name="Gregor")
        pprint(r)

        assert r[0]['name'] == "Gregor"

    def test_04_update(self):
        HEADING()
        entries = [{
            "cmcounter": 1,
            "name": "gregor"
        }, {
            "cmcounter": 2,
            "name": "laszewski"
        }]
        pprint(entries)
        for entry in entries:
            counter = entry["cmcounter"]
            print("Counter:", counter)
            entry["cmid"] = self.name.id(counter=counter)
        self.database.update(entries, replace=False)
        r = self.database.find()
        pprint(r)

    def test_05_update(self):
        HEADING()
        r = self.database.find(name="gregor")
        pprint(r)
        assert r[0]["name"] == "gregor"

    def test_06_find_by_counter(self):
        HEADING()
        r = self.database.find_by_counter(1)
        pprint(r)
        assert r[0]["name"] == "gregor"

        r = self.database.find_by_counter(2)
        pprint(r)
        assert r[0]["name"] == "laszewski"

    def test_07_decorator_update(self):
        HEADING()

        @DatabaseUpdate(collection="cloudmesh")
        def entry():
            name = Name()
            print(name)
            print("OOOO", str(name), name.counter)
            d = {
                "cmid": str(name),
                "cmcounter": name.counter,
                "name": "albert"
            }
            name.incr()
            pprint(d)
            return d

        a = entry()

        r = self.database.find_by_counter(3)

        pprint(r)

    def test_08_decorator_add(self):
        HEADING()

        @DatabaseAdd(collection="cloudmesh")
        def entry():
            d = {"name": "zweistein"}
            return d

        a = entry()

        r = self.database.find()

        pprint(r)

        assert len(r) == 4

    def test_09_overwrite(self):
        HEADING()
        r = self.database.find(name="gregor")[0]
        pprint(r)
        r["color"] = "red"

        self.database.update([r], replace=True)

        r = self.database.find(color="red")

        pprint(r)

        assert len(r) == 1

    def test_10_fancy(self):
        HEADING()

        counter = 1

        n = Name(experiment="exp",
                 group="grp",
                 user="******",
                 kind="vm",
                 counter=counter)

        print(n)

        entries = [{
            "cmcounter": counter,
            "cmid": str(n),
            "name": "gregor",
            "phone": "android"
        }]
        self.database.update(entries, replace=True)

        r = self.database.find()

        pprint(r)

        assert len(r) == 4
Esempio n. 22
0
class TestName:
    def setup(self):
        banner("setup", c="-")
        self.user = Config()["cloudmesh"]["profile"]["user"]
        self.clouduser = '******'
        self.name_generator = Name(experiment="exp",
                                   group="grp",
                                   user=self.user,
                                   kind="vm",
                                   counter=1)

        self.name = str(self.name_generator)
        self.name_generator.incr()

        self.new_name = str(self.name_generator)

        self.p = Provider(name="aws")

        self.secgroupname = "CM4TestSecGroup"
        self.secgrouprule = {
            "ip_protocol": "tcp",
            "from_port": 8080,
            "to_port": 8088,
            "ip_range": "129.79.0.0/16"
        }
        self.testnode = None

    def test_001_list_keys(self):
        HEADING()
        pprint(self.p.user)
        pprint(self.p.cloudtype)
        pprint(self.p.spec)

    def test_01_list_keys(self):
        HEADING()
        self.keys = self.p.keys()
        #pprint(self.keys)

        print(
            Printer.flatwrite(self.keys,
                              sort_keys=("name"),
                              order=["name", "fingerprint"],
                              header=["Name", "Fingerprint"]))

    def test_02_key_upload(self):
        HEADING()

        key = SSHkey()
        print(key.__dict__)

        self.p.key_upload(key)

        self.test_01_list_keys()

    def test_03_list_flavors(self):
        HEADING()
        flavors = self.p.flavors()
        pprint(flavors)

        print(
            Printer.flatwrite(
                flavors,
                sort_keys=("name", "extra.vcpu", "extra.memory", "price"),
                order=[
                    "name", "extra.vcpu", "extra.memory", "extra.clockSpeed",
                    "price"
                ],
                header=["Name", "VCPUS", "RAM", "Speed", "Price"]))
        """
            {'bandwidth': None,
             'cloud': 'aws',
             'created': '2019-02-22 19:27:54.965053',
             'disk': 0,
             'driver': 'aws',
             'extra': {'clockSpeed': 'Up to 3.0 GHz',
                       'currentGeneration': 'Yes',
                       'ecu': 'Variable',
                       'instanceFamily': 'General purpose',
                       'instanceType': 't2.xlarge',
                       'memory': '16 GiB',
                       'networkPerformance': 'Moderate',
                       'normalizationSizeFactor': '8',
                       'physicalProcessor': 'Intel Xeon Family',
                       'processorArchitecture': '64-bit',
                       'processorFeatures': 'Intel AVX; Intel Turbo',
                       'servicecode': 'AmazonEC2',
                       'servicename': 'Amazon Elastic Compute Cloud',
                       'storage': 'EBS only',
                       'vcpu': '4'},
             'id': 't2.xlarge',
             'kind': 'flavor',
             'name': 't2.xlarge',
             'price': 0.188,
             'ram': 16384,
             'updated': '2019-02-22 19:27:54.965053'},
        """

    def test_04_list_vm(self):
        HEADING()
        vms = self.p.list()
        pprint(vms)

        print(
            Printer.flatwrite(vms,
                              sort_keys=("name"),
                              order=[
                                  "name", "state", "extra.task_state",
                                  "extra.vm_state", "extra.userId",
                                  "extra.key_name", "private_ips", "public_ips"
                              ],
                              header=[
                                  "Name", "State", "Task state", "VM state",
                                  "User Id", "SSHKey", "Private ips",
                                  "Public ips"
                              ]))
class Test_Compute_Database:
    def setup(self):
        print()
        self.user = Config()["cloudmesh"]["profile"]["user"]
        self.clouduser = '******'
        self.name_generator = Name(experiment="exp",
                                   group="grp",
                                   user=self.user,
                                   kind="vm",
                                   counter=1)

        self.name = str(self.name_generator)
        self.name_generator.incr()

        self.new_name = str(self.name_generator)

        variables = Variables()
        clouds = Parameter.expand(variables['cloud'])
        cloud = clouds[0]

        self.p = Provider(name=cloud)

        self.secgroupname = "CM4TestSecGroup"
        self.secgrouprule = {
            "ip_protocol": "tcp",
            "from_port": 8080,
            "to_port": 8088,
            "ip_range": "129.79.0.0/16"
        }
        self.testnode = None
        print("\n")

    def test_00_banner(self):
        StopWatch.start("banner")
        banner("START", color="RED")
        StopWatch.stop("banner")

    def test_01_list_flavors(self):
        HEADING()
        StopWatch.start("list flavors")
        flavors = self.p.flavors()
        StopWatch.stop("list flavors")

    def test_02_list_images(self):
        HEADING()
        StopWatch.start("list images")
        images = self.p.images()
        StopWatch.stop("list images")

    def test_03_list_nodes(self):
        HEADING()
        StopWatch.start("list nodes")
        nodes = self.p.list()
        StopWatch.stop("list nodes")

    def test_04_list_keys(self):
        HEADING()
        StopWatch.start("list keys")
        self.keys = self.p.keys()

        # pprint(self.keys)

        # print(Printer.flatwrite(self.keys,
        #                    sort_keys=["name"],
        #                    order=["name", "fingerprint"],
        #                    header=["Name", "Fingerprint"])
        #      )

        StopWatch.stop("list keys")

    def test_05_key_upload(self):
        HEADING()
        StopWatch.start("key upload")
        key = SSHkey()
        print(key.__dict__)

        self.p.key_upload(key)

        self.test_04_list_keys()
        StopWatch.stop("key upload")

    def test_06_list_images(self):
        HEADING()
        StopWatch.start("list images2")
        images = self.p.images()
        # pprint(images)
        sort_keys = self.p.p.output['image']['sort_keys']  # not pretty
        order = self.p.p.output['image']['order']  # not pretty
        header = self.p.p.output['image']['header']  # not pretty

        print(
            Printer.flatwrite(images,
                              sort_keys=sort_keys,
                              order=order,
                              header=header))
        StopWatch.stop("list images2")

    def test_07_list_vm(self):
        HEADING()
        StopWatch.start("list vm")
        vms = self.p.list()
        # pprint (vms)

        sort_keys = self.p.p.output['vm']['sort_keys']  # not pretty
        order = self.p.p.output['vm']['order']  # not pretty
        header = self.p.p.output['vm']['header']  # not pretty

        print(
            Printer.flatwrite(vms,
                              sort_keys=sort_keys,
                              order=order,
                              header=header))

        StopWatch.stop("list vm")

    def test_print(self):
        HEADING()
        StopWatch.benchmark()
Esempio n. 24
0
class TestName:

    def setup(self):
        banner("setup", c="-")
        self.user = Config()["cloudmesh.profile.user"]
        self.clouduser = '******'
        self.name_generator = Name(
            schema=f"{self.user}-vm",
            counter=1)

        self.name = str(self.name_generator)
        self.name_generator.incr()

        self.new_name = str(self.name_generator)

        self.p = Provider(name=CLOUD)

        self.secgroupname = "CM4TestSecGroup"
        self.secgrouprule = {"ip_protocol": "tcp",
                             "from_port": 8080,
                             "to_port": 8088,
                             "ip_range": "129.79.0.0/16"}
        self.testnode = None

    def test_list_keys(self):
        HEADING()
        pprint(self.p.user)
        pprint(self.p.cloudtype)
        pprint(self.p.spec)

    def test_list_keys(self):
        HEADING()
        self.keys = self.p.keys()
        # pprint(self.keys)

        print(Printer.flatwrite(self.keys,
                                sort_keys=["name"],
                                order=["name", "fingerprint"],
                                header=["Name", "Fingerprint"])
              )

    def test_key_upload(self):
        HEADING()

        key = SSHkey()
        print(key.__dict__)

        self.p.key_upload(key)

        self.test_01_list_keys()

    def test_list_flavors(self):
        HEADING()
        flavors = self.p.flavors()
        pprint(flavors)

        print(Printer.flatwrite(flavors,
                                sort_keys=[
                                    "name", "extra.vcpu", "extra.memory",
                                    "price"],
                                order=["name", "extra.vcpu", "extra.memory",
                                       "extra.clockSpeed", "price"],
                                header=["Name", "VCPUS", "RAM", "Speed",
                                        "Price"])
              )

        """
            {'bandwidth': None,
             'cloud': 'aws',
             'created': '2019-02-22 19:27:54.965053',
             'disk': 0,
             'driver': 'aws',
             'extra': {'clockSpeed': 'Up to 3.0 GHz',
                       'currentGeneration': 'Yes',
                       'ecu': 'Variable',
                       'instanceFamily': 'General purpose',
                       'instanceType': 't2.xlarge',
                       'memory': '16 GiB',
                       'networkPerformance': 'Moderate',
                       'normalizationSizeFactor': '8',
                       'physicalProcessor': 'Intel Xeon Family',
                       'processorArchitecture': '64-bit',
                       'processorFeatures': 'Intel AVX; Intel Turbo',
                       'servicecode': 'AmazonEC2',
                       'servicename': 'Amazon Elastic Compute Cloud',
                       'storage': 'EBS only',
                       'vcpu': '4'},
             'id': 't2.xlarge',
             'kind': 'flavor',
             'name': 't2.xlarge',
             'price': 0.188,
             'ram': 16384,
             'updated': '2019-02-22 19:27:54.965053'},
        """

    def test_list_vm(self):
        HEADING()
        vms = self.p.list()
        pprint(vms)

        print(Printer.flatwrite(vms,
                                sort_keys=["name"],
                                order=["name",
                                       "state",
                                       "extra.task_state",
                                       "extra.vm_state",
                                       "extra.userId",
                                       "extra.key_name",
                                       "private_ips",
                                       "public_ips"],
                                header=["Name",
                                        "State",
                                        "Task state",
                                        "VM state",
                                        "User Id",
                                        "SSHKey",
                                        "Private ips",
                                        "Public ips"])
              )

    def test_list_secgroups(self):
        HEADING()
        secgroups = self.p.list_secgroups()
        for secgroup in secgroups:
            print(secgroup["name"])
            rules = self.p.list_secgroup_rules(secgroup["name"])

            print(Printer.write(rules,
                                sort_keys=["ip_protocol", "from_port",
                                           "to_port", "ip_range"],
                                order=["ip_protocol", "from_port", "to_port",
                                       "ip_range"],
                                header=["ip_protocol", "from_port", "to_port",
                                        "ip_range"])
                  )

    def test_secgroups_add(self):
        self.p.add_secgroup(self.secgroupname)
        self.test_05_list_secgroups()

    def test_secgroup_rules_add(self):
        rules = [self.secgrouprule]
        self.p.add_rules_to_secgroup(self.secgroupname, rules)
        self.test_05_list_secgroups()

    def test_secgroup_rules_remove(self):
        rules = [self.secgrouprule]
        self.p.remove_rules_from_secgroup(self.secgroupname, rules)
        self.test_05_list_secgroups()

    def test_secgroups_remove(self):
        self.p.remove_secgroup(self.secgroupname)
        self.test_05_list_secgroups()

    def test_create(self):
        HEADING()
        image = "CC-Ubuntu16.04"
        size = "m1.medium"
        self.p.create(name=self.name,
                      image=image,
                      size=size,
                      # username as the keypair name based on
                      # the key implementation logic
                      ex_keyname=self.user,
                      ex_security_groups=['default'])
        time.sleep(5)
        nodes = self.p.list()
        node = self.p.find(nodes, name=self.name)
        pprint(node)

        nodes = self.p.list(raw=True)
        for node in nodes:
            if node.name == self.name:
                self.testnode = node
                break

        assert node is not None

    def test_publicip_attach(self):
        HEADING()
        pubip = self.p.get_public_ip()
        pprint(pubip)
        nodes = self.p.list(raw=True)
        for node in nodes:
            if node.name == self.name:
                self.testnode = node
                break
        if self.testnode:
            print("attaching public IP...")
            self.p.attach_publicIP(self.testnode, pubip)
            time.sleep(5)
        self.test_04_list_vm()

    def test_publicip_detach(self):
        print("detaching and removing public IP...")
        time.sleep(5)
        nodes = self.p.list(raw=True)
        for node in nodes:
            if node.name == self.name:
                self.testnode = node
                break
        ipaddr = self.testnode.public_ips[0]
        pubip = self.p.cloudman.ex_get_floating_ip(ipaddr)
        self.p.detach_publicIP(self.testnode, pubip)
        time.sleep(5)
        self.test_04_list_vm()

    # def test_11_printer(self):
    #    HEADING()
    #    nodes = self.p.list()

    #    print(Printer.write(nodes, order=["name", "image", "size"]))

    # def test_01_start(self):
    #    HEADING()
    #    self.p.start(name=self.name)

    # def test_12_list_vm(self):
    #    self.test_04_list_vm()

    def test_info(self):
        HEADING()
        self.p.info(name=self.name)

    def test_destroy(self):
        HEADING()
        self.p.destroy(names=self.name)
        nodes = self.p.list()
        node = self.p.find(nodes, name=self.name)

        pprint(node)

        assert node["extra"]["task_state"] == "deleting"

    def test_list_vm(self):
        self.test_04_list_vm()

    def test_vm_login(self):
        self.test_04_list_vm()
        self.test_10_create()
        # use the self.testnode for this test
        time.sleep(30)
        self.test_11_publicIP_attach()
        time.sleep(5)
        nodes = self.p.list(raw=True)
        for node in nodes:
            if node.name == self.name:
                self.testnode = node
                break
        # pprint (self.testnode)
        # pprint (self.testnode.public_ips)
        pubip = self.testnode.public_ips[0]

        COMMAND = "cat /etc/*release*"

        ssh = subprocess.Popen(
            ["ssh", "%s@%s" % (self.clouduser, pubip), COMMAND],
            shell=False,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
        result = ssh.stdout.readlines()
        if result == []:
            error = ssh.stderr.readlines()
            print("ERROR: %s" % error)
        else:
            print("RESULT:")
            for line in result:
                line = line.decode("utf-8")
                print(line.strip("\n"))

        self.test_14_destroy()
        self.test_04_list_vm()

    def test_benchmark(self):
        Benchmark.print(csv=True, sysinfo=False, tag=CLOUD)
class TestAzure:
    def setup(self):
        banner("setup", c="-")
        self.user = Config()["cloudmesh.profile.user"]
        self.name_generator = Name(schema=f"{self.user}-vm", counter=1)

        self.name = str(self.name_generator)
        self.name_generator.incr()

        self.new_name = str(self.name_generator)
        self.p = Provider(name=CLOUD)

    def test_resource_group(self):
        HEADING()
        Benchmark.Start()
        test_resource_group = self.p._get_resource_group()
        VERBOSE(test_resource_group, label='RESOURCE GROUP')
        Benchmark.Stop()

        assert test_resource_group is not None

    def test_list_images(self):
        HEADING()

        Benchmark.Start()
        test_images = self.p.images()
        Benchmark.Stop()
        VERBOSE(test_images, label='Azure List of Images')

        assert test_images is not None

    def test_list_flavors(self):
        HEADING()

        Benchmark.Start()
        test_flavors = self.p.flavors()
        Benchmark.Stop()
        VERBOSE(test_flavors, label='Azure List of Flavors')

        assert test_flavors is not None

    def test_create_vm(self):
        HEADING()

        Benchmark.Start()
        test_vm = self.p.create()
        Benchmark.Stop()
        VERBOSE(test_vm, label='Virtual Machine Created')

        assert test_vm is not None

    def test_set_server_metadata(self):
        HEADING()

        tags = 'This is my cloudmesh metadata Tag'

        Benchmark.Start()
        test_set_metadata = self.p.set_server_metadata(name=None, cm=tags)
        Benchmark.Stop()
        VERBOSE(test_set_metadata,
                label='Added Metadata to Virtual Machine Created')

        assert test_set_metadata is not None

    def test_get_server_metadata(self):
        HEADING()

        Benchmark.Start()
        test_get_metadata = self.p.get_server_metadata(self)
        Benchmark.Stop()
        VERBOSE(test_get_metadata, label='Get Metadata from Virtual Machine')

        assert test_get_metadata is not None

    def test_delete_server_metadata(self):
        HEADING()

        Benchmark.Start()
        test_delete_metadata = self.p.delete_server_metadata(None, 'cm')
        Benchmark.Stop()
        VERBOSE(test_delete_metadata,
                label='Metadata from Virtual Machine after deleting tag')

        assert test_delete_metadata is not None

    def test_list_security_groups(self):
        HEADING()

        Benchmark.Start()
        test_list_sec_groups = self.p.list_secgroups()
        Benchmark.Stop()
        VERBOSE(test_list_sec_groups, label='List Security Groups')

        assert test_list_sec_groups is not None

    def test_add_security_group(self):
        HEADING()

        Benchmark.Start()
        #
        # BUG secgroup must be named argument
        #
        test_add_sec_group = self.p.add_secgroup(SECGROUP, description=None)
        Benchmark.Stop()
        VERBOSE(test_add_sec_group, label='Add Security Group')

        assert test_add_sec_group is not None

    def test_add_security_rule(self):
        HEADING()

        Benchmark.Start()
        test_add_sec_rule = self.p.add_secgroup_rule(
            name='resource_name_security_rule',
            port=None,
            protocol=None,
            ip_range='3389:3390')
        Benchmark.Stop()
        VERBOSE(test_add_sec_rule, label='Add Security Rule')

        assert test_add_sec_rule is not None

    def test_list_security_group_rules(self):
        HEADING()

        Benchmark.Start()
        test_list_secgroup_rules = self.p.list_secgroup_rules(name=SECGROUP)
        Benchmark.Stop()
        VERBOSE(test_list_secgroup_rules, label='List Security Group Rules')

        assert test_list_secgroup_rules is not None

    def test_remove_security_rule(self):
        HEADING()

        Benchmark.Start()
        test_remove_sec_rule = self.p.remove_rules_from_secgroup(
            name=SECGROUP, rules='resource_name_security_rule')
        Benchmark.Stop()
        VERBOSE(test_remove_sec_rule, label='Remove Security Rule')

        assert test_remove_sec_rule is not None

    def test_remove_security_group(self):
        HEADING()

        Benchmark.Start()
        test_remove_sec_group = self.p.remove_secgroup(name=SECGROUP)
        Benchmark.Stop()
        VERBOSE(test_remove_sec_group, label='Remove Security Rule')

        assert test_remove_sec_group is None

    def test_upload_security_group(self):
        HEADING()

        # BUG: this seems wrong: cloudmesh_upload

        Benchmark.Start()
        test_upload_secgroup = self.p.upload_secgroup(name=SECGROUP_UPLOAD)
        Benchmark.Stop()
        VERBOSE(test_upload_secgroup, label='Upload Security Group')

        assert test_upload_secgroup is None

    def test_add_rules_to_security_group(self):
        HEADING()

        # BUG: this seems wrong: cloudmesh_upload

        Benchmark.Start()
        test_add_rules_to_secgroup = self.p.add_rules_to_secgroup(
            secgroupname=SECGROUP_UPLOAD,
            newrules='resource_name_security_rule_upload')
        Benchmark.Stop()
        VERBOSE(test_add_rules_to_secgroup,
                label='Add Rules to Security Group')

        assert test_add_rules_to_secgroup is None

    def test_start(self):
        HEADING()

        Benchmark.Start()
        start_vm = self.p.start()
        Benchmark.Stop()
        VERBOSE(start_vm, label='Starting Virtual Machine')

        assert start_vm is not None

    def test_info(self):
        HEADING()

        Benchmark.Start()
        info_vm = self.p.info(None, None)
        Benchmark.Stop()
        VERBOSE(info_vm, label='Get Virtual Machine Info')

        assert info_vm is not None

    def test_list_vms(self):
        HEADING()

        Benchmark.Start()
        list_vm = self.p.list()
        Benchmark.Stop()
        VERBOSE(list_vm, label='List Virtual Machines')

        assert list_vm is not None

    def test_reboot(self):
        HEADING()

        Benchmark.Start()
        reboot_vm = self.p.reboot()
        Benchmark.Stop()
        VERBOSE(reboot_vm, label='Rebooting Virtual Machine')

        assert reboot_vm is not None

    def test_suspend(self):
        HEADING()

        Benchmark.Start()
        suspend_vm = self.p.suspend()
        Benchmark.Stop()
        VERBOSE(suspend_vm, label='Suspend Virtual Machine')

        assert suspend_vm is not None

    def test_resume(self):
        HEADING()

        Benchmark.Start()
        resume_vm = self.p.resume()
        Benchmark.Stop()
        VERBOSE(resume_vm, label='Resume Virtual Machine')

        assert resume_vm is not None

    def test_stop(self):
        HEADING()

        Benchmark.Start()
        stop_vm = self.p.stop()
        Benchmark.Stop()
        VERBOSE(stop_vm, label='Stop Virtual Machine')

        assert stop_vm is not None

    def test_destroy(self):
        HEADING()

        Benchmark.Start()
        destroy_vm = self.p.destroy()
        Benchmark.Stop()
        VERBOSE(destroy_vm, label='Destroy Virtual Machine')

        assert destroy_vm is None

    def test_benchmark(self):
        Benchmark.print()
Esempio n. 26
0
class TestName:
    def setup(self):
        banner("setup", c="-")
        self.user = Config()["cloudmesh.profile.user"]
        self.key_path = path_expand(
            Config()["cloudmesh"]["profile"]["publickey"])
        f = open(self.key_path, 'r')
        self.key_val = f.read()

        self.clouduser = '******'
        self.name_generator = Name(schema=f"{self.user}-vm", counter=1)

        self.name = str(self.name_generator)
        self.name_generator.incr()

        self.new_name = str(self.name_generator)

        self.p = GCloudProvider(name="google")

        self.secgroupname = "CM4TestSecGroup"
        self.secgrouprule = {
            "ip_protocol": "tcp",
            "from_port": 8080,
            "to_port": 8088,
            "ip_range": "129.79.0.0/16"
        }
        self.testnode = None

    def test_list_keys(self):
        HEADING()
        print("List Key Method is not supported by google")

    def test_key_upload(self):
        HEADING()
        print("Upload Key method is not supported by google")

    def test_list_images(self):
        HEADING()
        images = self.p.images()
        print(
            Printer.flatwrite(images,
                              sort_keys=("name"),
                              order=["name", "id", "driver"],
                              header=["Name", "Id", "Driver"]))

    def test_list_flavors(self):
        HEADING()
        flavors = self.p.flavors()
        print(
            Printer.flatwrite(flavors,
                              sort_keys=("name", "disk"),
                              order=["name", "id", "ram", "disk"],
                              header=["Name", "Id", "RAM", "Disk"]))

    def test_list_vm(self):
        HEADING()
        vms = self.p.list()
        print(
            Printer.flatwrite(vms,
                              sort_keys=("name"),
                              order=[
                                  "name", "state", "extra.task_state",
                                  "extra.vm_state", "extra.userId",
                                  "extra.key_name", "private_ips", "public_ips"
                              ],
                              header=[
                                  "Name", "State", "Task state", "VM state",
                                  "User Id", "SSHKey", "Private ips",
                                  "Public ips"
                              ]))

    def test_list_secgroups(self):
        HEADING()
        print("List security group method is not supported by google")

    def test_secgroups_add(self):
        print("List add security groups method is not supported by google")

    def test_secgroup_rules_add(self):
        print(
            "List Add security group rules method is not supported by google")

    def test_secgroup_rules_remove(self):
        print("Remove security group rules method is not supported by google")

    def test_secgroups_remove(self):
        print("Remove security groups method is not supported by google")

    def test_create(self):
        HEADING()
        image = "ubuntu-minimal-1810-cosmic-v20190402"
        size = "n1-standard-4"
        location = "us-central1-a"
        self.p.create(name=self.name,
                      image=image,
                      size=size,
                      location=location)
        nodes = self.p.list()
        node = self.p.find(nodes, name=self.name)

        nodes = self.p.list(raw=True)
        for node in nodes:
            if node.name == self.name:
                self.testnode = node
                break

        assert node is not None

    def test_publicIP_attach(self):
        HEADING()
        print("Attach Public IP method is not supported by google")

    def test_publicIP_detach(self):
        print("Detach Public IP method is not supported by google")

    def test_info(self):
        HEADING()
        self.p.info(name=self.name)

    def test_stop(self):
        HEADING()
        try:
            self.p.stop(names=self.name)
            self.test_list_vm()
        except:
            self.test_list_vm()

    def test_list(self):
        HEADING()
        self.test_list_vm()

    def test_start(self):
        HEADING()
        self.p.start(names=self.name)
        self.test_list_vm()

    def test_destroy(self):
        HEADING()
        try:
            self.p.stop(names=self.name)
            self.test_list_vm()
        except:
            self.test_list_vm()
        self.p.destroy(names=self.name)
        nodes = self.p.list()
        node = self.p.find(nodes, name=self.name)

        assert node is None

    def test_vm_login(self):
        HEADING()
        self.test_list_vm()
        self.test_create()
        self.p.ssh(name=self.name, command="cat /etc/*release*")
        try:
            self.p.stop(names=self.name)
            self.test_list_vm()
        except:
            self.test_list_vm()

        self.p.destroy(names=self.name)
class TestDatabase:
    def setup(self):
        print()
        self.user = Config()["cloudmesh"]["profile"]["user"]
        self.clouduser = '******'
        self.name_generator = Name(experiment="exp",
                                   group="grp",
                                   user=self.user,
                                   kind="vm",
                                   counter=1)

        self.name = str(self.name_generator)
        self.name_generator.incr()

        self.new_name = str(self.name_generator)

        variables = Variables()
        clouds = Parameter.expand(variables['cloud'])
        cloud = clouds[0]

        self.p = Provider(name=cloud)

        self.secgroupname = "CM4TestSecGroup"
        self.secgrouprule = {
            "ip_protocol": "tcp",
            "from_port": 8080,
            "to_port": 8088,
            "ip_range": "129.79.0.0/16"
        }
        self.testnode = None
        print("\n")

    def test_00_banner(self):
        banner("START", color="RED")

    def test_01_list_flavors(self):
        HEADING()
        flavors = self.p.flavors()

    def test_02_list_images(self):
        HEADING()
        images = self.p.images()

    def test_03_list_nodes(self):
        HEADING()
        nodes = self.p.list()

    def test_04_list_keys(self):
        HEADING()
        self.keys = self.p.keys()

    # pprint(self.keys)

    #print(Printer.flatwrite(self.keys,
    #                    sort_keys=("name"),
    #                    order=["name", "fingerprint"],
    #                    header=["Name", "Fingerprint"])
    #      )

    def test_05_key_upload(self):
        HEADING()

        key = SSHkey()
        print(key.__dict__)

        self.p.key_upload(key)

        self.test_04_list_keys()

    def test_06_list_images(self):
        HEADING()
        images = self.p.images()
        #pprint(images)

        print(
            Printer.flatwrite(
                images,
                sort_keys=("name", "extra.minDisk"),
                order=["name", "extra.minDisk", "updated", "driver"],
                header=["Name", "MinDisk", "Updated", "Driver"]))

    def test_07_list_vm(self):
        HEADING()
        vms = self.p.list()
        #pprint (vms)

        print(
            Printer.flatwrite(vms,
                              sort_keys=("name"),
                              order=[
                                  "name", "state", "extra.task_state",
                                  "extra.vm_state", "extra.userId",
                                  "extra.key_name", "private_ips", "public_ips"
                              ],
                              header=[
                                  "Name", "State", "Task state", "VM state",
                                  "User Id", "SSHKey", "Private ips",
                                  "Public ips"
                              ]))
Esempio n. 28
0
class TestName:
    def setup(self):
        banner("setup", c="-")
        self.user = Config()["cloudmesh"]["profile"]["user"]
        self.clouduser = '******'
        self.name_generator = Name(experiment="exp",
                                   group="grp",
                                   user=self.user,
                                   kind="vm",
                                   counter=1)

        self.name = str(self.name_generator)
        self.name_generator.incr()

        self.new_name = str(self.name_generator)

        self.p = Provider(name="azure")

        self.secgroupname = "CM4TestSecGroup"
        self.secgrouprule = {
            "ip_protocol": "tcp",
            "from_port": 8080,
            "to_port": 8088,
            "ip_range": "129.79.0.0/16"
        }
        self.testnode = None

    def test_001_list_keys(self):
        HEADING()
        pprint(self.p.user)
        pprint(self.p.cloudtype)
        pprint(self.p.spec)

    def test_01_list_keys(self):
        HEADING()
        self.keys = self.p.keys()
        # pprint(self.keys)

        print(
            Printer.flatwrite(self.keys,
                              sort_keys=("name"),
                              order=["name", "fingerprint"],
                              header=["Name", "Fingerprint"]))

    def test_02_key_upload(self):
        HEADING()

        key = SSHkey()
        print(key.__dict__)

        self.p.key_upload(key)

        self.test_01_list_keys()

    def test_03_list_flavors(self):
        HEADING()
        flavors = self.p.flavors()
        pprint(flavors)

        print(
            Printer.flatwrite(
                flavors,
                sort_keys=("name", "extra.cores", "extra.max_data_disks",
                           "price"),
                order=["name", "extra.cores", "extra.max_data_disks", "price"],
                header=["Name", "VCPUS", "max_data_disks", "Price"]))
        """
        {'bandwidth': None,
          'cloud': 'azure',
          'created': '2019-02-22 21:15:09.897719',
          'disk': 127,
          'driver': 'azure_asm',
          'extra': {'cores': 16, 'max_data_disks': 32},
          'id': 'Standard_D14',
          'kind': 'flavor',
          'name': 'D14 Faster Compute Instance',
          'price': '1.6261',
          'ram': 114688,
          'updated': '2019-02-22 21:15:09.897719'}
        """

    def test_04_list_vm(self):
        HEADING()
        vms = self.p.list()
        pprint(vms)

        print(
            Printer.flatwrite(vms,
                              sort_keys=("name"),
                              order=[
                                  "name", "state", "extra.task_state",
                                  "extra.vm_state", "extra.userId",
                                  "extra.key_name", "private_ips", "public_ips"
                              ],
                              header=[
                                  "Name", "State", "Task state", "VM state",
                                  "User Id", "SSHKey", "Private ips",
                                  "Public ips"
                              ]))

    def test_17_list_images(self):
        HEADING()
        images = self.p.images()
        pprint(images[:10])

        print(
            Printer.flatwrite(images[:10],
                              sort_keys=("id", "name"),
                              order=["name", "id", 'extra.os'],
                              header=["Name", "id", 'extra.os']))
        """