Ejemplo n.º 1
0
class Test_inventory:

    def setup(self):
        self.i = Inventory()
        banner("Info")
        self.i.info()

    def test_inventory(self):
        HEADING()

        for output in ['dict', 'yaml', 'csv', 'table']:
            banner(output)
            print(self.i.list(format=output))

        banner("changing values")
        self.i.add(host="i1", cluster="india", label="india")
        self.i.add(host="i2", cluster="india", label="gregor")
        self.i.add(host="d[1-4]", cluster="delta", label="delta")

        banner("saving")
        self.i.save()

        for output in ['dict', 'yaml', 'csv', 'table']:
            banner(output)
            print(self.i.list(format=output))

        banner("reading")
        n = Inventory()
        n.read()

        t = n.list('table')
        print(t)

        assert "gregor" in str(t)
        assert "+" in str(t)
Ejemplo n.º 2
0
    def test_key_gen(self):
        HEADING()
        inv_file = '~/.cloudmesh/config_test.yaml'
        inv = Inventory(inv_file)
        inv.add_directory(host='test_host1',
                          keyfile='~/.ssh/id_rsa.pub',
                          service='manager')
        inv.save()

        c = Configure(inventory=inv_file)
        priv_key, pub_key = c.generate_ssh_key(hostname='test_host1')

        u = c.build_user_data(name='test_host1')

        keys = readfile(filename='~/.ssh/id_rsa.pub').strip().split('\n')

        t1 = Userdata()\
         .with_authorized_keys(keys=keys)\
         .with_ssh_password_login(ssh_pwauth=False)\
         .with_locale()\
         .with_net_tools()\
         .with_hostname(hostname='test_host1')\
         .with_hosts(hosts=['127.0.0.1:test_host1'])\
         .with_packages(packages='avahi-daemon')\
         .with_runcmd(cmd=f'cat /boot/firmware/id_rsa.pub > /home/ubuntu/.ssh/id_rsa.pub')\
         .with_runcmd(cmd=f'cat /boot/firmware/id_rsa > /home/ubuntu/.ssh/id_rsa')\
         .with_fix_user_dir_owner(user='******')\
         .with_runcmd(cmd=f'chmod 600 /home/ubuntu/.ssh/id_rsa')\
         .with_runcmd(cmd=f'sudo rm /boot/firmware/id_rsa.pub')\
         .with_runcmd(cmd=f'sudo rm /boot/firmware/id_rsa')

        assert (t1.content == u.content)

        os.system("rm -f " + path_expand(inv_file))
Ejemplo n.º 3
0
    def test_build_networkdata(self):
        HEADING()
        inv_file = '~/.cloudmesh/config_test.yaml'
        inv = Inventory(inv_file)
        inv.add_directory(host='test_host1',
                          keyfile='~/.ssh/id_rsa.pub',
                          service='manager',
                          ip='10.1.1.11',
                          router='10.1.1.1',
                          dns=['8.8.8.8', '8.8.4.4'])
        inv.add_directory(host='test_host2',
                          service='worker',
                          ip='10.1.1.10',
                          router='10.1.1.1',
                          dns=['8.8.8.8', '8.8.4.4'])
        inv.save()

        c = Configure(inventory=inv_file)
        b1 = c.build_network_data(name='test_host1')
        b2 = c.build_network_data(name='test_host2')

        keys = readfile(filename='~/.ssh/id_rsa.pub').strip().split('\n')
        t1 = Networkdata()\
         .with_defaults()\
         .with_ip(ip='10.1.1.11')\
         .with_nameservers(nameservers=['8.8.8.8', '8.8.4.4'])\
         .with_gateway(gateway='10.1.1.1')

        t2 = Networkdata()\
         .with_defaults()\
         .with_ip(ip='10.1.1.10')\
         .with_nameservers(nameservers=['8.8.8.8', '8.8.4.4'])\
         .with_gateway(gateway='10.1.1.1')

        assert (t1.content == b1.content)
        assert (t2.content == b2.content)
        os.system("rm -f " + path_expand(inv_file))
Ejemplo n.º 4
0
    def test_build_user_data(self):
        HEADING()
        inv_file = '~/.cloudmesh/config_test.yaml'
        inv = Inventory(inv_file)
        inv.add_directory(host='test_host1',
                          keyfile='~/.ssh/id_rsa.pub',
                          service='manager')
        inv.add_directory(host='test_host2', service='worker')
        inv.save()

        c = Configure(inventory=inv_file)
        b1 = c.build_user_data(name='test_host1')
        b2 = c.build_user_data(name='test_host2')

        keys = readfile(filename='~/.ssh/id_rsa.pub').strip().split('\n')
        t1 = Userdata()\
         .with_authorized_keys(keys=keys)\
         .with_ssh_password_login(ssh_pwauth=False)\
         .with_locale()\
         .with_net_tools()\
         .with_hostname(hostname='test_host1')\
         .with_hosts(hosts=['127.0.0.1:test_host1'])\
         .with_packages(packages='avahi-daemon')

        t2 = Userdata()\
         .with_default_user()\
         .with_ssh_password_login()\
         .with_locale()\
         .with_net_tools()\
         .with_hostname(hostname='test_host2')\
         .with_hosts(hosts=['127.0.0.1:test_host2'])\
         .with_packages(packages='avahi-daemon')

        assert (t1.content == b1.content)
        assert (t2.content == b2.content)
        os.system("rm -f " + path_expand(inv_file))
Ejemplo n.º 5
0
    def do_inventory(self, args, arguments):
        """
        ::

          Usage:
              inventory add cluster NAMES
              inventory add NAMES [--label=LABEL]
                                  [--services=SERVICES]
                                  [--project=PROJECT]
                                  [--owners=OWNERS]
                                  [--comment=COMMENT]
                                  [--inventory=INVENTORY]
                                  [--cluster=CLUSTER]
                                  [--ip=IP]
                                  [--service=SERVICE]
                                  [--tag=TAG]
                                  [--keyfile=KEYFILE]
                                  [--router=ROUTER]
                                  [--locale=LOCALE]
                                  [--timezone=TIMEZONE]
              inventory create TAG [--hostnames=NAMES]
                                   [--ip=IP]
                                   [--inventory=INVENTORY]
                                   [--keyfile=KEYFILE]
              inventory set NAMES ATTRIBUTE to VALUES [--inventory=INVENTORY] [--listvalue]
              inventory delete NAMES [--inventory=INVENTORY]
              inventory clone NAMES from SOURCE [--inventory=INVENTORY]
              inventory list [NAMES] [--format=FORMAT] [--columns=COLUMNS] [--inventory=INVENTORY]
              inventory info [--inventory=INVENTORY]
              inventory remove --inventory=INVENTORY

          Arguments:
            NAMES     Name of the resources (example i[10-20])
            FORMAT    The format of the output is either txt,
                      yaml, dict, table [default: table].
            OWNERS    a comma separated list of owners for this resource
            LABEL     a unique label for this resource
            SERVICE   a string that identifies the service
            PROJECT   a string that identifies the project
            SOURCE    a single host name to clone from
            COMMENT   a comment

          Options:
             -v       verbose mode
             --keyfile=KEYFILE      Keyfile to assign [default: ~/.ssh/id_rsa.pub]

          Description:

                add -- adds a resource to the resource inventory
                list -- lists the resources in the given format
                delete -- deletes objects from the table
                clone -- copies the content of an existing object
                         and creates new once with it
                set   -- sets for the specified objects the attribute
                         to the given value or values. If multiple values
                         are used the values are assigned to the and
                         objects in order. See examples
                map   -- allows to set attributes on a set of objects
                         with a set of values

          Examples:

            cms inventory add x[0-3] --service=openstack
                adds hosts x0, x1, x2, x3 and puts the string
                openstack into the service column

            cms inventory list
                lists the repository

            cms inventory set x[3-4] temperature to 32
                sets for the resources x3, x4 the value of the
                temperature to 32

            cms inventory set x[7-8] ip to 128.0.0.[0-1]
                sets the value of x7 to 128.0.0.0
                sets the value of x8 to 128.0.0.1

            cms inventory set x1 services to bridge,kubernetes --listvalue
                sets the value of x1 to [bridge, kubernetes]
                The --listvalue option indicates the value set is a list

            cms inventory clone x[5-6] from x3
                clones the values for x5, x6 from x3

        """
        map_parameters(arguments, "columns", 'ip', 'hostnames', 'inventory',
                       'keyfile', 'listvalue')

        if arguments.info:

            if arguments.inventory is None:
                i = Inventory()
            else:
                i = Inventory(f'~/.cloudmesh/{arguments.inventory}')
            i.read()
            i.info()

        elif arguments.remove and arguments.inventory:

            os.system("rm -f " +
                      path_expand(f'~/.cloudmesh/{arguments.inventory}'))

        elif arguments.NAMES is not None and arguments.list:

            hosts = Parameter.expand(arguments.NAMES)

            if arguments.inventory is None:
                i = Inventory()
            else:
                i = Inventory(f'~/.cloudmesh/{arguments.inventory}')
            i.read()
            d = dict(i.data)
            r = {}
            for key in d:
                if key in hosts:
                    r[key] = d[key]

            pprint(r)
            i.data = r

            if arguments["--columns"]:
                order = arguments["--columns"].split(",")
            else:
                order = i.order
            print(i.list(format="table", order=order))

        # elif arguments["set"]:
        #    hosts = hostlist.expand_hostlist(arguments.NAMES)
        #    i = inventory()
        #    i.read()
        #    element = {}

        #    for attribute in i.order:
        #        try:
        #            attribute = arguments["ATTRIBUTE"]
        #            value = arguments["VALUE"]
        #            if value is not None:
        #                element[attribute] = value
        #        except:
        #            pass
        #    element['host'] = arguments.NAMES
        #    i.add(**element)
        #    print (i.list(format="table"))

        elif arguments.create:
            tag = arguments.TAG

            hostnames = Parameter.expand(arguments.hostnames)
            manager, workers = Host.get_hostnames(hostnames)

            ips = Parameter.expand(arguments.ip)

            if len(ips) != len(hostnames):
                Console.error(
                    "The number of hosts does not match the number of ips")
                return

            keyfile = arguments.keyfile or '~/.ssh/id_rsa.pub'

            if arguments.inventory is None:
                arguments.inventory = "inventory.yaml"

            if manager is None:
                manager_ip = None
                worker_ips = ips
            else:
                manager_ip = ips[0]
                worker_ips = ips[1:]

            worker_hostnames = workers

            if arguments.inventory is None:
                i = Inventory()
            else:
                i = Inventory(f'~/.cloudmesh/{arguments.inventory}')
            i.read()

            inventory_name = arguments.inventory.split('.')[0]

            i.add(host=manager,
                  name=manager,
                  tag=tag,
                  cluster=inventory_name,
                  service='manager',
                  ip=manager_ip,
                  keyfile=keyfile,
                  status="inactive")

            for worker, ip in zip(worker_hostnames, worker_ips):
                i.add(host=worker,
                      name=worker,
                      tag=tag,
                      cluster=inventory_name,
                      service='worker',
                      ip=ip,
                      keyfile=keyfile,
                      status="inactive")  # noqa: E124

            i.save()
            Console.ok(
                f"Successfuly saved to ~/.cloudmesh/{arguments.inventory}")

        elif arguments.list:

            if arguments.inventory is None:
                i = Inventory()
            else:
                i = Inventory(f'~/.cloudmesh/{arguments.inventory}')
            i.read()
            if arguments["--columns"]:
                order = arguments["--columns"].split(",")
            else:
                order = i.order
            print(i.list(format="table", order=order))

        elif arguments.set:

            hosts = Parameter.expand(arguments.NAMES)
            values = Parameter.expand(arguments.VALUES)
            if len(values) == 1:
                values = values * len(hosts)
            attribute = arguments.ATTRIBUTE
            if not arguments.listvalue and len(hosts) != len(values):
                Console.error(
                    "Number of names {:} != number of values{:}".format(
                        len(hosts), len(values)))
            if arguments.inventory is None:
                i = Inventory()
            else:
                i = Inventory(f'~/.cloudmesh/{arguments.inventory}')
            i.read()

            for index in range(0, len(hosts)):
                host = hosts[index]
                value = values if arguments.listvalue else values[index]

                if not i.has_host(host):
                    i.add(host=host)

                i.set(host, attribute, value)
                # object = {'host': host,
                #           attribute: value}

                # i.add(**object)
                i.save()

            print(i.list(format="table"))

        elif arguments.add and arguments.cluster:
            names = Parameter.expand(arguments.NAMES)
            manager, workers = Host.get_hostnames(names)
            if manager is None:
                Console.error(
                    "A manager node is required. Numbers are not allowed in a manger name, "
                    "i.e. red is an acceptable manager name, red00 is not.")
                return
            if workers:
                worker_base_name = ''.join(
                    [i for i in workers[0] if not i.isdigit()])
            cluster_name = manager or worker_base_name
            inventory = path_expand(
                f'~/.cloudmesh/inventory-{cluster_name}.yaml')
            Inventory.build_default_inventory(filename=inventory,
                                              manager=manager,
                                              workers=workers)

        elif arguments.add:

            hosts = Parameter.expand(arguments.NAMES)
            if arguments.inventory is None:
                i = Inventory()
            else:
                i = Inventory(f'~/.cloudmesh/{arguments.inventory}')
            i.read()
            element = {}

            for attribute in i.order:
                try:
                    value = arguments["--" + attribute]
                    if value is not None:
                        element[attribute] = value
                except Exception as e:  # noqa: F841
                    pass
            element['host'] = arguments.NAMES
            element['status'] = 'inactive'
            i.add(**element)
            i.save()
            print(i.list(format="table"))

        elif arguments.delete:

            hosts = Parameter.expand(arguments.NAMES)
            if arguments.inventory is None:
                i = Inventory()
            else:
                i = Inventory(f'~/.cloudmesh/{arguments.inventory}')
            i.read()

            for host in hosts:
                i.delete(host)
            i.save()

        elif arguments.clone:

            hosts = Parameter.expand(arguments.NAMES)
            source = arguments.SOURCE

            if arguments.inventory is None:
                i = Inventory()
            else:
                i = Inventory(f'~/.cloudmesh/{arguments.inventory}')
            i.read()

            if source in i.data:

                for host in hosts:
                    i.data[host] = dict(i.data[source])
                i.save()
            else:
                Console.error("The source {:} does not exist".format(source))

        return ""
class Test_inventory:
    def setup(self):
        self.i = Inventory('~/.cloudmesh/test.yaml')
        self.i.info()

    def test_add(self):
        HEADING(txt="cms inventory add")
        cluster = "test_cluster"
        label = "test{j}"
        host = "red00{j}"
        ip = "10.1.1.{j}"
        dns = "8.8.8.8"

        # Adding one by one
        for j in range(1, 4):
            self.i.add(
                host=host.format(j=j),
                cluster=cluster,
                dns=dns,
                label=label.format(j=j),
                ip=ip.format(j=j),
            )

        for j in range(1, 4):
            host_name = host.format(j=j)
            assert self.i.has_host(host_name)
            entry = self.i.data[host_name]
            assert entry['cluster'] == cluster
            assert entry['ip'] == ip.format(j=j)
            assert entry['label'] == label.format(j=j)
            assert entry['dns'] == dns

        # Multi add
        self.i.add(host=host.format(j='[4-7]'),
                   cluster=cluster,
                   ip=ip.format(j='[4-7]'))

        for j in range(4, 8):
            host_name = host.format(j=j)
            assert self.i.has_host(host_name)
            entry = self.i.data[host_name]
            assert entry['cluster'] == cluster
            assert entry['ip'] == ip.format(j=j)

        self.i.save()

    def test_list(self):
        HEADING(txt="cms inventory list")

        # for output in ['dict', 'yaml', 'csv', 'table']: # bug in 'csv' print in cloudmesh-common
        for output in ['dict', 'yaml', 'table']:
            banner(output)
            print(self.i.list(format=output))

        t = str(self.i.list('table'))

        for order in self.i.order:
            assert order in t

    def test_set(self):
        HEADING()

        self.i.set("red002", "service", "worker")
        assert self.i.get("red002", "service") == "worker"
        self.i.save()

    def test_find(self):
        HEADING()
        assert len(self.i.find(host="red002")) > 0
Ejemplo n.º 7
0
    def do_inventory(self, args, arguments):
        """
        ::

          Usage:
              inventory add NAMES [--label=LABEL]
                                  [--service=SERVICES]
                                  [--project=PROJECT]
                                  [--owners=OWNERS]
                                  [--comment=COMMENT]
                                  [--cluster=CLUSTER]
                                  [--ip=IP]
              inventory set NAMES ATTRIBUTE to VALUES
              inventory delete NAMES
              inventory clone NAMES from SOURCE
              inventory list [NAMES] [--format=FORMAT] [--columns=COLUMNS]
              inventory info

          Arguments:

            NAMES     Name of the resources (example i[10-20])

            FORMAT    The format of the output is either txt,
                      yaml, dict, table [default: table].

            OWNERS    a comma separated list of owners for this resource

            LABEL     a unique label for this resource

            SERVICE   a string that identifies the service

            PROJECT   a string that identifies the project

            SOURCE    a single host name to clone from

            COMMENT   a comment

          Options:

             -v       verbose mode

          Description:

                add -- adds a resource to the resource inventory

                list -- lists the resources in the given format

                delete -- deletes objects from the table

                clone -- copies the content of an existing object
                         and creates new once with it

                set   -- sets for the specified objects the attribute
                         to the given value or values. If multiple values
                         are used the values are assigned to the and
                         objects in order. See examples

                map   -- allows to set attributes on a set of objects
                         with a set of values

          Examples:

            cms inventory add x[0-3] --service=openstack

                adds hosts x0, x1, x2, x3 and puts the string
                openstack into the service column

            cms inventory lists

                lists the repository

            cms inventory x[3-4] set temperature to 32

                sets for the resources x3, x4 the value of the
                temperature to 32

            cms inventory x[7-8] set ip 128.0.0.[0-1]

                sets the value of x7 to 128.0.0.0
                sets the value of x8 to 128.0.0.1

            cms inventory clone x[5-6] from x3

                clones the values for x5, x6 from x3

        """
        map_parameters(arguments, "columns")

        #
        # TODO: fix config reader, use cmd4
        #
        filename = Config(config_path="/cloudmesh_inventory.yaml")

        sorted_keys = True
        if arguments.info:

            i = Inventory()
            i.read()
            i.info()

        elif arguments.NAMES is not None and arguments.list:

            hosts = Parameter.expand(arguments.NAMES)

            print(hosts)
            i = Inventory()
            i.read()
            d = dict(i.data)
            r = {}
            for key in d:
                if key in hosts:
                    r[key] = d[key]

            pprint(r)
            i.data = r

            if arguments["--columns"]:
                order = arguments["--columns"].split(",")
            else:
                order = i.order
            print(i.list(format="table", order=order))

        # elif arguments["set"]:
        #    hosts = hostlist.expand_hostlist(arguments.NAMES)
        #    i = inventory()
        #    i.read()
        #    element = {}

        #    for attribute in i.order:
        #        try:
        #            attribute = arguments["ATTRIBUTE"]
        #            value = arguments["VALUE"]
        #            if value is not None:
        #                element[attribute] = value
        #        except:
        #            pass
        #    element['host'] = arguments.NAMES
        #    i.add(**element)
        #    print (i.list(format="table"))

        elif arguments.list:

            i = Inventory()
            i.read()
            if arguments["--columns"]:
                order = arguments["--columns"].split(",")
            else:
                order = i.order
            print(i.list(format="table", order=order))

        elif arguments.set:

            hosts = Parameter.expand(arguments.NAMES)
            values = Parameter.expand(arguments.VALUES)
            if len(values) == 1:
                values = values * len(hosts)
            print(hosts)
            print(values)
            attribute = arguments.ATTRIBUTE
            if len(hosts) != len(values):
                Console.error(
                    "Number of names {:} != number of values{:}".format(
                        len(hosts), len(values)))

            i = Inventory()
            i.read()

            for index in range(0, len(hosts)):
                host = hosts[index]
                value = values[index]

                object = {'host': host, attribute: value}

                i.add(**object)

            print(i.list(format="table"))

        elif arguments.add:

            hosts = Parameter.expand(arguments.NAMES)
            i = Inventory()
            i.read()
            element = {}

            for attribute in i.order:
                try:
                    value = arguments["--" + attribute]
                    if value is not None:
                        element[attribute] = value
                except:
                    pass
            element['host'] = arguments.NAMES
            i.add(**element)
            print(i.list(format="table"))

        elif arguments.delete:

            hosts = Parameter.expand(arguments.NAMES)
            i = Inventory()
            i.read()

            for host in hosts:
                del i.data[host]
            i.save()

        elif arguments.clone:

            hosts = Parameter.expand(arguments.NAMES)
            source = arguments.SOURCE

            i = Inventory()
            i.read()

            if source in i.data:

                for host in hosts:
                    i.data[host] = dict(i.data[source])
                i.save()
            else:
                Console.error("The source {:} does not exist".format(source))

        return ""