Ejemplo n.º 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)
Ejemplo n.º 2
0
    def reinstall_cluster(self, clusterid):
        """reinstall cluster."""
        session = database.current_session()
        cluster = session.query(Cluster).filter_by(id=clusterid).first()
        if not cluster:
            return

        if not cluster.state:
            cluster.state = ClusterState()

        cluster.state.state = 'INSTALLING'
        cluster.mutable = False
        cluster.state.progress = 0.0
        cluster.state.message = ''
        cluster.state.severity = 'INFO'
Ejemplo n.º 3
0
def trigger_install(clusterid):
    '''trigger installer to start install for a given cluster.

    Args:
        clusterid: int, the id of the cluster.

    Returns:
        None
    '''
    manager = ConfigManager()
    session = database.current_session()
    cluster = session.query(Cluster).filter_by(id=clusterid).first()
    if not cluster:
        logging.error('no cluster found for %s', clusterid)
        return

    adapter = cluster.adapter
    if not adapter:
        logging.error('no proper adapter found for cluster %s', cluster.id)
        return

    if not cluster.state:
        cluster.state = ClusterState()

    if cluster.state.state and cluster.state.state != 'UNINITIALIZED':
        logging.error('ignore installing cluster %s since the state is %s',
                      cluster.id, cluster.state)
        return

    cluster.state.state = 'INSTALLING'
    hostids = [host.id for host in cluster.hosts]
    update_hostids = []
    for host in cluster.hosts:
        if not host.state:
            host.state = HostState()
        elif host.state.state and host.state.state != 'UNINITIALIZED':
            logging.info('ignore installing host %s sinc the state is %s',
                         host.id, host.state)
            continue

        host.state.state = 'INSTALLING'
        update_hostids.append(host.id)

    manager.updateClusterAndHostConfigs(clusterid, hostids, update_hostids,
                                        adapter.os, adapter.target_system)
    manager.sync()
Ejemplo n.º 4
0
def trigger_install(clusterid):
    """Deploy a given cluster.

    :param clusterid: the id of the cluster to deploy.
    :type clusterid: int

    .. note::
        The function should be called in database session.
    """
    session = database.current_session()
    cluster = session.query(Cluster).filter_by(id=clusterid).first()
    if not cluster:
        logging.error('no cluster found for %s', clusterid)
        return

    adapter = cluster.adapter
    if not adapter:
        logging.error('no proper adapter found for cluster %s', cluster.id)
        return

    if not cluster.state:
        cluster.state = ClusterState()

    if cluster.state.state and cluster.state.state != 'UNINITIALIZED':
        logging.error('ignore installing cluster %s since the state is %s',
                      cluster.id, cluster.state)
        return

    cluster.state.state = 'INSTALLING'
    hostids = [host.id for host in cluster.hosts]
    update_hostids = []
    for host in cluster.hosts:
        if not host.state:
            host.state = HostState()
        elif host.state.state and host.state.state != 'UNINITIALIZED':
            logging.info('ignore installing host %s sinc the state is %s',
                         host.id, host.state)
            continue

        host.state.state = 'INSTALLING'
        update_hostids.append(host.id)

    manager = ConfigManager()
    manager.update_cluster_and_host_configs(clusterid, hostids, update_hostids,
                                            adapter.os, adapter.target_system)
    manager.sync()
Ejemplo n.º 5
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)