Beispiel #1
0
    def _prepare_database(self, config_locals):
        """Prepare database."""
        with database.session() as session:
            adapters = {}
            for adapter_config in config_locals['ADAPTERS']:
                adapter = Adapter(**adapter_config)
                session.add(adapter)
                adapters[adapter_config['name']] = adapter

            roles = {}
            for role_config in config_locals['ROLES']:
                role = Role(**role_config)
                session.add(role)
                roles[role_config['name']] = role

            switches = {}
            for switch_config in config_locals['SWITCHES']:
                switch = Switch(**switch_config)
                session.add(switch)
                switches[switch_config['ip']] = switch

            machines = {}
            for switch_ip, machine_configs in (
                config_locals['MACHINES_BY_SWITCH'].items()
            ):
                for machine_config in machine_configs:
                    machine = Machine(**machine_config)
                    machines[machine_config['mac']] = machine
                    machine.switch = switches[switch_ip]
                    session.add(machine)

            clusters = {}
            for cluster_config in config_locals['CLUSTERS']:
                adapter_name = cluster_config['adapter']
                del cluster_config['adapter']
                cluster = Cluster(**cluster_config)
                clusters[cluster_config['name']] = cluster
                cluster.adapter = adapters[adapter_name]
                cluster.state = ClusterState(
                    state="INSTALLING", progress=0.0, message='')
                session.add(cluster)

            hosts = {}
            for cluster_name, host_configs in (
                config_locals['HOSTS_BY_CLUSTER'].items()
            ):
                for host_config in host_configs:
                    mac = host_config['mac']
                    del host_config['mac']
                    host = ClusterHost(**host_config)
                    hosts['%s.%s' % (
                        host_config['hostname'], cluster_name)] = host
                    host.machine = machines[mac]
                    host.cluster = clusters[cluster_name]
                    host.state = HostState(
                        state="INSTALLING", progress=0.0, message='')
                    session.add(host)
def prepare_database(config):
    with database.session() as session:
        adapters = {}
        for adapter_config in config["ADAPTERS"]:
            adapter = Adapter(**adapter_config)
            session.add(adapter)
            adapters[adapter_config["name"]] = adapter

        roles = {}
        for role_config in config["ROLES"]:
            role = Role(**role_config)
            session.add(role)
            roles[role_config["name"]] = role

        switches = {}
        for switch_config in config["SWITCHES"]:
            switch = Switch(**switch_config)
            session.add(switch)
            switches[switch_config["ip"]] = switch

        machines = {}
        for switch_ip, machine_configs in config["MACHINES_BY_SWITCH"].items():
            for machine_config in machine_configs:
                machine = Machine(**machine_config)
                machines[machine_config["mac"]] = machine
                machine.switch = switches[switch_ip]
                session.add(machine)

        clusters = {}
        for cluster_config in config["CLUSTERS"]:
            adapter_name = cluster_config["adapter"]
            del cluster_config["adapter"]
            cluster = Cluster(**cluster_config)
            clusters[cluster_config["name"]] = cluster
            cluster.adapter = adapters[adapter_name]
            cluster.state = ClusterState(state="INSTALLING", progress=0.0, message="")
            session.add(cluster)

        hosts = {}
        for cluster_name, host_configs in config["HOSTS_BY_CLUSTER"].items():
            for host_config in host_configs:
                mac = host_config["mac"]
                del host_config["mac"]
                host = ClusterHost(**host_config)
                hosts["%s.%s" % (host_config["hostname"], cluster_name)] = host
                host.machine = machines[mac]
                host.cluster = clusters[cluster_name]
                host.state = HostState(state="INSTALLING", progress=0.0, message="")
                session.add(host)
Beispiel #3
0
 def setUp(self):
     super(ClusterHostAPITest, self).setUp()
     self.test_config_data = {
         "networking": {
             "interfaces": {
                 "management": {
                     "ip": "192.168.1.1"}},
             "global": {}},
         "role": ""}
     # Insert a host into database for testing
     with database.session() as session:
         clusters_list = [Cluster(name='cluster_01'),
                          Cluster(name='cluster_02')]
         session.add_all(clusters_list)
         hosts_list = [ClusterHost(hostname='host_02', cluster_id=1),
                       ClusterHost(hostname='host_03', cluster_id=1),
                       ClusterHost(hostname='host_04', cluster_id=2)]
         host = ClusterHost(hostname='host_01', cluster_id=1)
         host.config_data = json.dumps(self.test_config_data)
         session.add(host)
         session.add_all(hosts_list)
Beispiel #4
0
 def setUp(self):
     super(ClusterHostAPITest, self).setUp()
     self.test_config_data = {
         "networking": {
             "interfaces": {
                 "management": {
                     "ip": "192.168.1.1"
                 }
             },
             "global": {}
         },
         "roles": ""
     }
     # Insert a host into database for testing
     with database.session() as session:
         clusters_list = [
             Cluster(name='cluster_01'),
             Cluster(name='cluster_02')
         ]
         session.add_all(clusters_list)
         hosts_list = [
             ClusterHost(hostname='host_02', cluster_id=1),
             ClusterHost(hostname='host_03', cluster_id=1),
             ClusterHost(hostname='host_04', cluster_id=2)
         ]
         host = ClusterHost(hostname='host_01', cluster_id=1)
         host.config_data = json.dumps(self.test_config_data)
         session.add(host)
         session.add_all(hosts_list)
Beispiel #5
0
    def setUp(self):
        super(ClusterHostAPITest, self).setUp()
        self.test_config_data = {
            "networking": {
                "interfaces": {
                    "management": {
                        "ip": "192.168.1.1"},
                    "tenant": {
                        "ip": "10.12.1.1"}
                },
                "global": {}},
            "roles": []}
        # Insert a host into database for testing
        with database.session() as session:
            clusters_list = [Cluster(name='cluster_01'),
                             Cluster(name='cluster_02')]
            session.add_all(clusters_list)

            switch = Switch(ip='192.168.1.1')
            session.add(switch)

            machines_list = [Machine(mac='00:27:88:0c:01', switch_id=1),
                             Machine(mac='00:27:88:0c:02', switch_id=1),
                             Machine(mac='00:27:88:0c:03', switch_id=1),
                             Machine(mac='00:27:88:0c:04', switch_id=1)]
            session.add_all(machines_list)
            
            host = ClusterHost(hostname='host_01', cluster_id=1, machine_id=1)
            host.config_data = json.dumps(self.test_config_data)
            session.add(host)

            hosts_list = [
                ClusterHost(hostname='host_02', cluster_id=1, machine_id=2),
                ClusterHost(hostname='host_03', cluster_id=1, machine_id=3),
                ClusterHost(hostname='host_04', cluster_id=2, machine_id=4)
            ]
            session.add_all(hosts_list)
Beispiel #6
0
    def test_cluster_action(self):
        from sqlalchemy import func
        #Prepare testing data: create machines, clusters in database
        #The first three machines will belong to cluster_01, the last one
        #belongs to cluster_10
        with database.session() as session:
            machines = [Machine(mac='00:27:88:0c:01'),
                        Machine(mac='00:27:88:0c:02'),
                        Machine(mac='00:27:88:0c:03'),
                        Machine(mac='00:27:88:0c:04'),
                        Machine(mac='00:27:88:0c:05'),
                        Machine(mac='00:27:88:0c:06'),
                        Machine(mac='00:27:88:0c:07'),
                        Machine(mac='00:27:88:0c:08')]
            clusters = [Cluster(name='cluster_10')]
            session.add_all(machines)
            session.add_all(clusters)
            # add a host to machine '00:27:88:0c:04' to cluster_02
            host = ClusterHost(cluster_id=10, machine_id=4,
                               hostname='host_c2_01')
            session.add(host)

        # Do an action to a non-existing cluster
        url = '/clusters/1000/action'
        request = {'addHosts': [10, 20, 30]}
        rv = self.app.post(url, data=json.dumps(request))
        self.assertEqual(rv.status_code, 404)

        # Test 'addHosts' action on cluster_01
        # 1. add a host with  non-existing machine
        url = '/clusters/1/action'
        request = {'addHosts': [1, 1000, 1001]}
        rv = self.app.post(url, data=json.dumps(request))
        self.assertEqual(rv.status_code, 404)
        # ClusterHost table should not have any records.
        with database.session() as session:
            hosts_num = session.query(func.count(ClusterHost.id))\
                               .filter_by(cluster_id=1).scalar()
            self.assertEqual(hosts_num, 0)

        # 2. add a host with a installed machine
        request = {'addHosts': [1, 4]}
        rv = self.app.post(url, data=json.dumps(request))
        self.assertEqual(rv.status_code, 409)
        data = json.loads(rv.get_data())
        self.assertEqual(len(data['failedMachines']), 1)

        # 3. add hosts to cluster_01
        request = {'addHosts': [1, 2, 3]}
        rv = self.app.post(url, data=json.dumps(request))
        self.assertEqual(rv.status_code, 200)
        total_hosts = 0
        with database.session() as session:
            total_hosts = session.query(func.count(ClusterHost.id))\
                                 .filter_by(cluster_id=1).scalar()
            data = json.loads(rv.get_data())
            self.assertEqual(len(data['cluster_hosts']), total_hosts)
            self.assertEqual(total_hosts, 3)

        # 4. try to remove some hosts not existing and in different cluster
        request = {'removeHosts': [1, 2, 3, 1000, 1001]}
        rv = self.app.post(url, data=json.dumps(request))
        self.assertEqual(rv.status_code, 404)
        data = json.loads(rv.get_data())
        self.assertEqual(len(data['failedHosts']), 3)
        with database.session() as session:
            count = session.query(func.count(ClusterHost.id))\
                           .filter_by(cluster_id=1).scalar()
            self.assertEqual(count, 3)

        # 5. sucessfully remove requested hosts
        request = {'removeHosts': [2, 3]}
        rv = self.app.post(url, data=json.dumps(request))
        self.assertEqual(rv.status_code, 200)
        data = json.loads(rv.get_data())
        self.assertEqual(len(data['cluster_hosts']), 2)
        with database.session() as session:
            count = session.query(func.count(ClusterHost.id))\
                           .filter_by(cluster_id=1).scalar()
            self.assertEqual(count, 1)

        # 6. Test 'replaceAllHosts' action on cluster_01
        request = {'replaceAllHosts': [5, 6, 7]}
        rv = self.app.post(url, data=json.dumps(request))
        self.assertEqual(rv.status_code, 200)
        data = json.loads(rv.get_data())
        self.assertEqual(len(data['cluster_hosts']), 3)
        with database.session() as session:
            count = session.query(func.count(ClusterHost.id))\
                           .filter_by(cluster_id=1).scalar()
            self.assertEqual(count, 3)

        # 7. Test 'deploy' action on cluster_01
        request = {'deploy': []}
        rv = self.app.post(url, data=json.dumps(request))
        self.assertEqual(rv.status_code, 202)

        # 8. Test deploy cluster_01 the second time
        rv = self.app.post(url, data=json.dumps(request))
        self.assertEqual(rv.status_code, 400)

        # 9. Try to deploy cluster_02 which no host in
        url = '/clusters/2/action'
        with database.session() as session:
            session.query(ClusterHost).filter_by(cluster_id=2)\
                                      .delete(synchronize_session=False)
            host = session.query(ClusterHost).filter_by(cluster_id=2).first()

        rv = self.app.post(url, data=json.dumps(request))
        self.assertEqual(rv.status_code, 404)

        # 10. Try to add a new host to cluster_01 and deploy it
        with database.session() as session:
            cluster = session.query(Cluster).filter_by(id=1).first()
            cluster.mutable = True

            hosts = session.query(ClusterHost).filter_by(cluster_id=1).all()
            for host in hosts:
                host.mutable = True
        url = '/clusters/1/action'
        # add another machine as a new host into cluster_01
        request = json.dumps({"addHosts": [8]})
        rv = self.app.post(url, data=request)
        host_id = json.loads(rv.get_data())["cluster_hosts"][0]["id"]

        deploy_request = json.dumps({"deploy": [host_id]})
        rv = self.app.post(url, data=deploy_request)
        self.assertEqual(202, rv.status_code)
        expected_deploy_result = {
            "cluster": {
                "cluster_id": 1,
                "url": "/clusters/1/progress"
            },
            "hosts": [
                {"host_id": 5,
                 "url": "/cluster_hosts/5/progress"}
            ]
        }
        data = json.loads(rv.get_data())["deployment"]
        self.assertDictEqual(expected_deploy_result, data)
Beispiel #7
0
    def test_cluster_action(self):
        from sqlalchemy import func
        #Prepare testing data: create machines, clusters in database
        #The first three machines will belong to cluster_01, the last one
        #belongs to cluster_02
        with database.session() as session:
            machines = [
                Machine(mac='00:27:88:0c:01'),
                Machine(mac='00:27:88:0c:02'),
                Machine(mac='00:27:88:0c:03'),
                Machine(mac='00:27:88:0c:04')
            ]
            clusters = [Cluster(name='cluster_02')]
            session.add_all(machines)
            session.add_all(clusters)
            # add a host to machine '00:27:88:0c:04' to cluster_02
            host = ClusterHost(cluster_id=2,
                               machine_id=4,
                               hostname='host_c2_01')
            session.add(host)

        # Do an action to a non-existing cluster
        url = '/clusters/1000/action'
        request = {'addHosts': [10, 20, 30]}
        rv = self.app.post(url, data=json.dumps(request))
        self.assertEqual(rv.status_code, 404)

        # Test 'addHosts' action on cluster_01
        # 1. add a host with  non-existing machine
        url = '/clusters/1/action'
        request = {'addHosts': [1, 1000, 1001]}
        rv = self.app.post(url, data=json.dumps(request))
        self.assertEqual(rv.status_code, 404)
        # ClusterHost table should not have any records.
        with database.session() as session:
            hosts_num = session.query(func.count(ClusterHost.id))\
                               .filter_by(cluster_id=1).scalar()
            self.assertEqual(hosts_num, 0)

        # 2. add a host with a installed machine
        request = {'addHosts': [1, 4]}
        rv = self.app.post(url, data=json.dumps(request))
        self.assertEqual(rv.status_code, 409)
        data = json.loads(rv.get_data())
        self.assertEqual(len(data['failedMachines']), 1)

        # 3. add hosts to cluster_01
        request = {'addHosts': [1, 2, 3]}
        rv = self.app.post(url, data=json.dumps(request))
        self.assertEqual(rv.status_code, 200)
        data = json.loads(rv.get_data())
        self.assertEqual(len(data['cluster_hosts']), 3)

        # 4. try to remove some hosts which do not exists
        request = {'removeHosts': [1, 1000, 1001]}
        rv = self.app.post(url, data=json.dumps(request))
        self.assertEqual(rv.status_code, 404)
        data = json.loads(rv.get_data())
        self.assertEqual(len(data['failedHosts']), 2)

        # 5. sucessfully remove requested hosts
        request = {'removeHosts': [1, 2]}
        rv = self.app.post(url, data=json.dumps(request))
        self.assertEqual(rv.status_code, 200)
        data = json.loads(rv.get_data())
        self.assertEqual(len(data['cluster_hosts']), 2)

        # 6. Test 'replaceAllHosts' action on cluster_01
        request = {'replaceAllHosts': [1, 2, 3]}
        rv = self.app.post(url, data=json.dumps(request))
        self.assertEqual(rv.status_code, 200)
        data = json.loads(rv.get_data())
        self.assertEqual(len(data['cluster_hosts']), 3)

        # 7. Test 'deploy' action on cluster_01
        request = {'deploy': {}}
        rv = self.app.post(url, data=json.dumps(request))
        self.assertEqual(rv.status_code, 202)

        # 8. Test deploy cluster_01 the second time
        rv = self.app.post(url, data=json.dumps(request))
        self.assertEqual(rv.status_code, 400)

        # 9. Try to deploy cluster_02  which no host
        url = '/clusters/2/action'
        with database.session() as session:
            session.query(ClusterHost).filter_by(cluster_id=2)\
                                      .delete(synchronize_session=False)
            host = session.query(ClusterHost).filter_by(cluster_id=2).first()

        rv = self.app.post(url, data=json.dumps(request))
        self.assertEqual(rv.status_code, 404)
Beispiel #8
0
def setupDb():
    """setup database."""
    SECURITY_CONFIG = {
        "security": {
            "server_credentials": {
                "username": "******",
                "password": "******"},
            "service_credentials": {
                "username": "******",
                "password": "******"},
            "console_credentials": {
                "username": "******",
                "password": "******"}
        }
    }

    NET_CONFIG = {
        "networking": {
            "interfaces": {
                "management": {
                    "ip_start": "10.120.8.100",
                    "ip_end": "10.120.8.200",
                    "netmask": "255.255.255.0",
                    "gateway": "",
                    "nic": "eth0",
                    "promisc": 1
                },
                "tenant": {
                    "ip_start": "192.168.10.100",
                    "ip_end": "192.168.10.200",
                    "netmask": "255.255.255.0",
                    "gateway": "",
                    "nic": "eth1",
                    "promisc": 0
                },
                "public": {
                    "ip_start": "12.145.68.100",
                    "ip_end": "12.145.68.200",
                    "netmask": "255.255.255.0",
                    "gateway": "",
                    "nic": "eth2",
                    "promisc": 0
                },
                "storage": {
                    "ip_start": "172.29.8.100",
                    "ip_end": "172.29.8.200",
                    "netmask": "255.255.255.0",
                    "gateway": "",
                    "nic": "eth3",
                    "promisc": 0
                }
            },
            "global": {
                "nameservers": "8.8.8.8",
                "search_path": "ods.com",
                "gateway": "192.168.1.1",
                "proxy": "http://127.0.0.1:3128",
                "ntp_server": "127.0.0.1"
            }
        }
    }

    PAR_CONFIG = {
        "partition": "/home 20%;/tmp 10%;/var 30%;"
    }

    HOST_CONFIG = {
        "networking": {
            "interfaces": {
                "management": {
                    "ip": "%s"
                },
                "tenant": {
                    "ip": "%s"
                }
            }
        },
        "roles": ["base"]
    }

    print "Setting up DB ..."
    with database.session() as session:
            # populate switch_config
            switch_config = SwitchConfig(ip='192.168.1.10', filter_port='1')
            session.add(switch_config)

            # populate role table
            role = Role(name='compute', target_system='openstack')
            session.add(role)

            # Populate one adapter to DB
            adapter = Adapter(name='Centos_openstack', os='Centos',
                              target_system='openstack')
            session.add(adapter)

            #Populate switches info to DB
            switches = [Switch(ip="192.168.2.1",
                               credential={"version": "2c",
                                           "community": "public"},
                               vendor="huawei",
                               state="under_monitoring"),
                        Switch(ip="192.168.2.2",
                               credential={"version": "2c",
                                           "community": "public"},
                               vendor="huawei",
                               state="under_monitoring"),
                        Switch(ip="192.168.2.3",
                               credential={"version": "2c",
                                           "community": "public"},
                               vendor="huawei",
                               state="under_monitoring"),
                        Switch(ip="192.168.2.4",
                               credential={"version": "2c",
                                           "community": "public"},
                               vendor="huawei",
                               state="under_monitoring")]
            session.add_all(switches)

            # Populate machines info to DB
            machines = [
                Machine(mac='00:0c:27:88:0c:a1', port='1', vlan='1',
                        switch_id=1),
                Machine(mac='00:0c:27:88:0c:a2', port='2', vlan='1',
                        switch_id=1),
                Machine(mac='00:0c:27:88:0c:a3', port='3', vlan='1',
                        switch_id=1),
                Machine(mac='00:0c:27:88:0c:b1', port='1', vlan='1',
                        switch_id=2),
                Machine(mac='00:0c:27:88:0c:b2', port='2', vlan='1',
                        switch_id=2),
                Machine(mac='00:0c:27:88:0c:b3', port='3', vlan='1',
                        switch_id=2),
                Machine(mac='00:0c:27:88:0c:c1', port='1', vlan='1',
                        switch_id=3),
                Machine(mac='00:0c:27:88:0c:c2', port='2', vlan='1',
                        switch_id=3),
                Machine(mac='00:0c:27:88:0c:c3', port='3', vlan='1',
                        switch_id=3),
                Machine(mac='00:0c:27:88:0c:d1', port='1', vlan='1',
                        switch_id=4),
                Machine(mac='00:0c:27:88:0c:d2', port='2', vlan='1',
                        switch_id=4),
            ]

            session.add_all(machines)
            # Popluate clusters into DB
            """
            a. cluster #1: a new machine will be added to it.
            b. cluster #2: a failed machine needs to be re-deployed.
            c. cluster #3: a new cluster with 3 hosts will be deployed.
            """
            clusters_networking_config = [
                {"networking":
                    {"interfaces": {"management": {"ip_start": "10.120.1.100",
                                                   "ip_end": "10.120.1.200"},
                                    "tenant": {"ip_start": "192.168.1.100",
                                               "ip_end": "192.168.1.200"},
                                    "public": {"ip_start": "12.145.1.100",
                                               "ip_end": "12.145.1.200"},
                                    "storage": {"ip_start": "172.29.1.100",
                                                "ip_end": "172.29.1.200"}}}},
                {"networking":
                    {"interfaces": {"management": {"ip_start": "10.120.2.100",
                                                   "ip_end": "10.120.2.200"},
                                    "tenant": {"ip_start": "192.168.2.100",
                                               "ip_end": "192.168.2.200"},
                                    "public": {"ip_start": "12.145.2.100",
                                               "ip_end": "12.145.2.200"},
                                    "storage": {"ip_start": "172.29.2.100",
                                                "ip_end": "172.29.2.200"}}}}
            ]
            cluster_names = ['cluster_01', 'cluster_02']
            for name, networking_config in zip(cluster_names,
                                               clusters_networking_config):
                nconfig = copy.deepcopy(NET_CONFIG)
                util.merge_dict(nconfig, networking_config)
                c = Cluster(
                    name=name, adapter_id=1,
                    security_config=json.dumps(SECURITY_CONFIG['security']),
                    networking_config=json.dumps(nconfig['networking']),
                    partition_config=json.dumps(PAR_CONFIG['partition']))
                session.add(c)
            # Populate hosts to each cluster
            host_mips = ['10.120.1.100', '10.120.1.101', '10.120.1.102',
                         '10.120.2.100', '10.120.2.101', '10.120.2.102']
            host_tips = ['192.168.1.100', '192.168.1.101', '192.168.1.102',
                         '192.168.2.100', '192.168.2.101', '192.168.2.102']

            hosts_config = []
            for mip, tip in zip(host_mips, host_tips):
                config = copy.deepcopy(HOST_CONFIG)
                config['networking']['interfaces']['management']['ip'] = mip
                config['networking']['interfaces']['tenant']['ip'] = tip
                hosts_config.append(json.dumps(config))

            hosts = [
                ClusterHost(hostname='host_01', machine_id=1, cluster_id=1,
                            config_data=hosts_config[0]),
                ClusterHost(hostname='host_02', machine_id=2, cluster_id=1,
                            config_data=hosts_config[1]),
                ClusterHost(hostname='host_03', machine_id=3, cluster_id=1,
                            config_data=hosts_config[2]),
                ClusterHost(hostname='host_01', machine_id=4, cluster_id=2,
                            config_data=hosts_config[3]),
                ClusterHost(hostname='host_02', machine_id=5, cluster_id=2,
                            config_data=hosts_config[4]),
                ClusterHost(hostname='host_03', machine_id=6, cluster_id=2,
                            config_data=hosts_config[5])
            ]
            session.add_all(hosts)

            # Populate cluster state and host state
            cluster_states = [
                ClusterState(id=1, state="READY", progress=1.0,
                             message="Successfully!"),
                ClusterState(id=2, state="ERROR", progress=0.5,
                             message="Failed!")
            ]
            session.add_all(cluster_states)

            host_states = [
                HostState(id=1, state="READY", progress=1.0,
                          message="Successfully!"),
                HostState(id=2, state="READY", progress=1.0,
                          message="Successfully!"),
                HostState(id=3, state="READY", progress=1.0,
                          message="Successfully!"),
                HostState(id=4, state="ERROR", progress=0.5,
                          message="Failed!"),
                HostState(id=5, state="READY", progress=1.0,
                          message="Successfully!"),
                HostState(id=6, state="ERROR", progress=1.0,
                          message="Failed!")
            ]
            session.add_all(host_states)