Esempio n. 1
0
def poll_switch(ip_addr, req_obj='mac', oper="SCAN"):
    """ Query switch and return expected result

        :param str ip_addr: switch ip address
        :param str req_obj: the object requested to query from switch
        :param str oper : the operation to query the switch(SCAN, GET, SET)
    """
    if not ip_addr:
        logging.error('No switch IP address is provided!')
        return

    #Retrieve vendor info from switch table
    session = database.current_session()
    switch = session.query(Switch).filter_by(ip=ip_addr).first()
    logging.info("pollswitch: %s", switch)
    if not switch:
        logging.error('no switch found for %s', ip_addr)
        return

    credential = switch.credential
    logging.error("pollswitch: credential %r", credential)
    vendor = switch.vendor
    hdmanager = HDManager()

    if not vendor or not hdmanager.is_valid_vendor(ip_addr,
                                                   credential, vendor):
        # No vendor found or vendor doesn't match queried switch.
        logging.debug('no vendor or vendor had been changed for switch %s',
                      switch)
        vendor = hdmanager.get_vendor(ip_addr, credential)
        logging.debug('[pollswitch] credential %r', credential)
        if not vendor:
            logging.error('no vendor found or match switch %s', switch)
            return
        switch.vendor = vendor

    # Start to poll switch's mac address.....
    logging.debug('hdmanager learn switch from %s %s %s %s %s',
                  ip_addr, credential, vendor, req_obj, oper)
    results = hdmanager.learn(ip_addr, credential, vendor, req_obj, oper)
    logging.info("pollswitch %s result: %s", switch, results)
    if not results:
        logging.error('no result learned from %s %s %s %s %s',
                      ip_addr, credential, vendor, req_obj, oper)
        return

    for entry in results:
        mac = entry['mac']
        machine = session.query(Machine).filter_by(mac=mac).first()
        if not machine:
            machine = Machine(mac=mac)
            machine.port = entry['port']
            machine.vlan = entry['vlan']
            machine.switch = switch

    logging.debug('update switch %s state to under monitoring', switch)
    switch.state = 'under_monitoring'
Esempio n. 2
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)
Esempio n. 3
0
 def update_switch_and_machines(self, switches, switch_machines):
     """update switches and machines."""
     session = database.current_session()
     session.query(Switch).delete(synchronize_session='fetch')
     session.query(Machine).delete(synchronize_session='fetch')
     for switch_data in switches:
         switch = Switch(**switch_data)
         logging.info('add switch %s', switch)
         session.add(switch)
         for machine_data in switch_machines.get(switch.ip, []):
             machine = Machine(**machine_data)
             logging.info('add machine %s under %s', machine, switch)
             machine.switch = switch
             session.add(machine)
Esempio n. 4
0
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)
 def update_switch_and_machines(
     self, switches, switch_machines
 ):
     """update switches and machines."""
     session = database.current_session()
     session.query(Switch).delete(synchronize_session='fetch')
     session.query(Machine).delete(synchronize_session='fetch')
     for switch_data in switches:
         switch = Switch(**switch_data)
         logging.info('add switch %s', switch)
         session.add(switch)
         for machine_data in switch_machines.get(switch.ip, []):
             machine = Machine(**machine_data)
             logging.info('add machine %s under %s', machine, switch)
             machine.switch = switch
             session.add(machine)
Esempio n. 6
0
def set_fake_switch_machine():
    """Set fake switches and machines.

    .. note::
       --fake_switches_vendor is the vendor name for all fake switches.
       the default value is 'huawei'
       --fake_switches_file is the filename which stores all fake switches
       and fake machines.
       each line in fake_switches_files presents one machine.
       the format of each line <switch_ip>,<switch_port>,<vlan>,<mac>.
    """
    # TODO(xiaodong): Move the main code to config manager.
    switch_ips = []
    switch_machines = {}
    vendor = flags.OPTIONS.fake_switches_vendor
    credential = {
        'version'    :  'v2c',
        'community'  :  'public',
    }
    if not _get_fake_switch_machines(switch_ips, switch_machines):
        return

    with database.session() as session:
        session.query(Switch).delete(synchronize_session='fetch')
        session.query(Machine).delete(synchronize_session='fetch')
        for switch_ip in switch_ips:
            logging.info('add switch %s', switch_ip)
            switch = Switch(ip=switch_ip, vendor_info=vendor,
                            credential=credential,
                            state='under_monitoring')
            logging.debug('add switch %s', switch_ip)
            session.add(switch)

            machines = switch_machines[switch_ip]
            for item in machines:
                logging.debug('add machine %s', item)
                machine = Machine(**item)
                machine.switch = switch

            session.add(machine)
Esempio n. 7
0
    def test_get_machine_by_id(self):
        # Test get a machine by id
        # Prepare testing data
        with database.session() as session:
            machine = Machine(mac='00:27:88:0c:a6',
                              port='1',
                              vlan='1',
                              switch_id=1)
            session.add(machine)

        # machine id exists in Machine table
        url = '/machines/1'
        rv = self.app.get(url)
        self.assertEqual(rv.status_code, 200)

        # machine id doesn't exist
        url = '/machines/1000'
        rv = self.app.get(url)
        self.assertEqual(rv.status_code, 404)
Esempio n. 8
0
    def test_get_machineList(self):
        #Prepare testing data
        with database.session() as session:
            machines = [
                Machine(mac='00:27:88:0c:01', port='1', vlan='1', switch_id=1),
                Machine(mac='00:27:88:0c:02', port='2', vlan='1', switch_id=1),
                Machine(mac='00:27:88:0c:03', port='3', vlan='1', switch_id=1),
                Machine(mac='00:27:88:0c:04', port='3', vlan='1', switch_id=2),
                Machine(mac='00:27:88:0c:05', port='4', vlan='2', switch_id=2),
                Machine(mac='00:27:88:0c:06', port='5', vlan='3', switch_id=3)
            ]
            session.add_all(machines)

        testList = [{
            'url': '/machines',
            'expected': 6
        }, {
            'url': '/machines?limit=3',
            'expected': 3
        }, {
            'url': '/machines?limit=50',
            'expected': 6
        }, {
            'url': '/machines?switchId=1&vladId=1&port=2',
            'expected': 1
        }, {
            'url': '/machines?switchId=1&vladId=1&limit=2',
            'expected': 2
        }, {
            'url': '/machines?switchId=4',
            'expected': 0
        }]

        for test in testList:
            url = test['url']
            expected = test['expected']
            rv = self.app.get(url)
            data = json.loads(rv.get_data())
            count = len(data['machines'])
            self.assertEqual(rv.status_code, 200)
            self.assertEqual(count, expected)
Esempio n. 9
0
def poll_switch(ip_addr, req_obj='mac', oper="SCAN"):
    """Query switch and return expected result

    .. note::
       When polling switch succeeds, for each mac it got from polling switch,
       A Machine record associated with the switch is added to the database.

    :param ip_addr: switch ip address.
    :type ip_addr: str
    :param req_obj: the object requested to query from switch.
    :type req_obj: str
    :param oper: the operation to query the switch.
    :type oper: str, should be one of ['SCAN', 'GET', 'SET']

    .. note::
       The function should be called out of database session scope.

    """
    under_monitoring = 'under_monitoring'
    unreachable = 'unreachable'

    if not ip_addr:
        logging.error('No switch IP address is provided!')
        return

    with database.session() as session:
        #Retrieve vendor info from switch table
        switch = session.query(Switch).filter_by(ip=ip_addr).first()
        logging.info("pollswitch: %s", switch)
        if not switch:
            logging.error('no switch found for %s', ip_addr)
            return

        credential = switch.credential
        logging.info("pollswitch: credential %r", credential)
        vendor = switch.vendor
        prev_state = switch.state
        hdmanager = HDManager()

        vendor, vstate, err_msg = hdmanager.get_vendor(ip_addr, credential)
        if not vendor:
            switch.state = vstate
            switch.err_msg = err_msg
            logging.info("*****error_msg: %s****", switch.err_msg)
            logging.error('no vendor found or match switch %s', switch)
            return

        switch.vendor = vendor

        # Start to poll switch's mac address.....
        logging.debug('hdmanager learn switch from %s %s %s %s %s',
                      ip_addr, credential, vendor, req_obj, oper)
        results = []

        try:
            results = hdmanager.learn(
                ip_addr, credential, vendor, req_obj, oper)
        except Exception as error:
            logging.exception(error)
            switch.state = unreachable
            switch.err_msg = "SNMP walk for querying MAC addresses timedout"
            return

        logging.info("pollswitch %s result: %s", switch, results)
        if not results:
            logging.error('no result learned from %s %s %s %s %s',
                          ip_addr, credential, vendor, req_obj, oper)
            return

        switch_id = switch.id
        filter_ports = session.query(
            SwitchConfig.filter_port
        ).filter(
            SwitchConfig.ip == Switch.ip
        ).filter(
            Switch.id == switch_id
        ).all()
        logging.info("***********filter posts are %s********", filter_ports)
        if filter_ports:
            #Get all ports from tuples into list
            filter_ports = [i[0] for i in filter_ports]

        for entry in results:
            mac = entry['mac']
            port = entry['port']
            vlan = entry['vlan']
            if port in filter_ports:
                continue

            machine = session.query(Machine).filter_by(
                mac=mac, port=port, switch_id=switch_id).first()
            if not machine:
                machine = Machine(mac=mac, port=port, vlan=vlan)
                session.add(machine)
                machine.switch = switch

        logging.debug('update switch %s state to under monitoring', switch)
        if prev_state != under_monitoring:
            #Update error message in db
            switch.err_msg = ""

        switch.state = under_monitoring
Esempio n. 10
0
def poll_switch(ip_addr, req_obj='mac', oper="SCAN"):
    """Query switch and return expected result

    .. note::
       When polling switch succeeds, for each mac it got from polling switch,
       A Machine record associated with the switch is added to the database.

    :param ip_addr: switch ip address.
    :type ip_addr: str
    :param req_obj: the object requested to query from switch.
    :type req_obj: str
    :param oper: the operation to query the switch.
    :type oper: str, should be one of ['SCAN', 'GET', 'SET']

    .. note::
       The function should be called out of database session scope.

    """
    under_monitoring = 'under_monitoring'
    unreachable = 'unreachable'

    if not ip_addr:
        logging.error('No switch IP address is provided!')
        return

    with database.session() as session:
        #Retrieve vendor info from switch table
        switch = session.query(Switch).filter_by(ip=ip_addr).first()
        logging.info("pollswitch: %s", switch)
        if not switch:
            logging.error('no switch found for %s', ip_addr)
            return

        credential = switch.credential
        logging.info("pollswitch: credential %r", credential)
        vendor = switch.vendor
        prev_state = switch.state
        hdmanager = HDManager()

        vendor, vstate, err_msg = hdmanager.get_vendor(ip_addr, credential)
        if not vendor:
            switch.state = vstate
            switch.err_msg = err_msg
            logging.info("*****error_msg: %s****", switch.err_msg)
            logging.error('no vendor found or match switch %s', switch)
            return

        switch.vendor = vendor

        # Start to poll switch's mac address.....
        logging.debug('hdmanager learn switch from %s %s %s %s %s', ip_addr,
                      credential, vendor, req_obj, oper)
        results = []

        try:
            results = hdmanager.learn(ip_addr, credential, vendor, req_obj,
                                      oper)
        except Exception as error:
            logging.exception(error)
            switch.state = unreachable
            switch.err_msg = "SNMP walk for querying MAC addresses timedout"
            return

        logging.info("pollswitch %s result: %s", switch, results)
        if not results:
            logging.error('no result learned from %s %s %s %s %s', ip_addr,
                          credential, vendor, req_obj, oper)
            return

        switch_id = switch.id
        filter_ports = session.query(SwitchConfig.filter_port).filter(
            SwitchConfig.ip == Switch.ip).filter(Switch.id == switch_id).all()
        logging.info("***********filter posts are %s********", filter_ports)
        if filter_ports:
            #Get all ports from tuples into list
            filter_ports = [i[0] for i in filter_ports]

        for entry in results:
            mac = entry['mac']
            port = entry['port']
            vlan = entry['vlan']
            if port in filter_ports:
                continue

            machine = session.query(Machine).filter_by(
                mac=mac, switch_id=switch_id).first()
            if not machine:
                machine = Machine(mac=mac,
                                  port=port,
                                  vlan=vlan,
                                  switch_id=switch_id)
                session.add(machine)
            else:
                machine.port = port
                machine.vlan = vlan

        logging.debug('update switch %s state to under monitoring', switch)
        if prev_state != under_monitoring:
            #Update error message in db
            switch.err_msg = ""

        switch.state = under_monitoring
Esempio n. 11
0
def poll_switch(ip_addr, req_obj='mac', oper="SCAN"):
    """Query switch and return expected result

    .. note::
       When polling switch succeeds, for each mac it got from polling switch,
       A Machine record associated with the switch is added to the database.

    :param ip_addr: switch ip address.
    :type ip_addr: str
    :param req_obj: the object requested to query from switch.
    :type req_obj: str
    :param oper: the operation to query the switch.
    :type oper: str, should be one of ['SCAN', 'GET', 'SET']

    .. note::
       The function should be called inside database session scope.

    """
    if not ip_addr:
        logging.error('No switch IP address is provided!')
        return

    #Retrieve vendor info from switch table
    session = database.current_session()
    switch = session.query(Switch).filter_by(ip=ip_addr).first()
    logging.info("pollswitch: %s", switch)
    if not switch:
        logging.error('no switch found for %s', ip_addr)
        return

    credential = switch.credential
    logging.error("pollswitch: credential %r", credential)
    vendor = switch.vendor
    hdmanager = HDManager()

    if not vendor or not hdmanager.is_valid_vendor(ip_addr, credential,
                                                   vendor):
        # No vendor found or vendor doesn't match queried switch.
        logging.debug('no vendor or vendor had been changed for switch %s',
                      switch)
        vendor = hdmanager.get_vendor(ip_addr, credential)
        logging.debug('[pollswitch] credential %r', credential)
        if not vendor:
            logging.error('no vendor found or match switch %s', switch)
            return
        switch.vendor = vendor

    # Start to poll switch's mac address.....
    logging.debug('hdmanager learn switch from %s %s %s %s %s', ip_addr,
                  credential, vendor, req_obj, oper)
    results = hdmanager.learn(ip_addr, credential, vendor, req_obj, oper)
    logging.info("pollswitch %s result: %s", switch, results)
    if not results:
        logging.error('no result learned from %s %s %s %s %s', ip_addr,
                      credential, vendor, req_obj, oper)
        return

    for entry in results:
        mac = entry['mac']
        machine = session.query(Machine).filter_by(mac=mac).first()
        if not machine:
            machine = Machine(mac=mac)
            machine.port = entry['port']
            machine.vlan = entry['vlan']
            machine.switch = switch

    logging.debug('update switch %s state to under monitoring', switch)
    switch.state = 'under_monitoring'
Esempio n. 12
0
def set_fake_switch_machine():
    """Set fake switches and machines for test."""
    with database.session() as session:
        credential = {
            'version': 'v2c',
            'community': 'public',
        }
        switches = [
            {
                'ip': '192.168.100.250'
            },
            {
                'ip': '192.168.100.251'
            },
            {
                'ip': '192.168.100.252'
            },
        ]
        session.query(Switch).delete()
        session.query(Machine).delete()
        ip_switch = {}
        for item in switches:
            logging.info('add switch %s', item)
            switch = Switch(ip=item['ip'],
                            vendor_info='huawei',
                            state='under_monitoring')
            switch.credential = credential
            session.add(switch)
            ip_switch[item['ip']] = switch
        session.flush()

        machines = [
            {
                'mac': '00:0c:29:32:76:85',
                'port': 50,
                'vlan': 1,
                'switch_ip': '192.168.100.250'
            },
            {
                'mac': '00:0c:29:fa:cb:72',
                'port': 51,
                'vlan': 1,
                'switch_ip': '192.168.100.250'
            },
            {
                'mac': '28:6e:d4:64:c7:4a',
                'port': 1,
                'vlan': 1,
                'switch_ip': '192.168.100.251'
            },
            {
                'mac': '28:6e:d4:64:c7:4c',
                'port': 2,
                'vlan': 1,
                'switch_ip': '192.168.100.251'
            },
            {
                'mac': '28:6e:d4:46:c4:25',
                'port': 40,
                'vlan': 1,
                'switch_ip': '192.168.100.252'
            },
            {
                'mac': '26:6e:d4:4d:c6:be',
                'port': 41,
                'vlan': 1,
                'switch_ip': '192.168.100.252'
            },
            {
                'mac': '28:6e:d4:62:da:38',
                'port': 42,
                'vlan': 1,
                'switch_ip': '192.168.100.252'
            },
            {
                'mac': '28:6e:d4:62:db:76',
                'port': 43,
                'vlan': 1,
                'switch_ip': '192.168.100.252'
            },
        ]

        for item in machines:
            logging.info('add machine %s', item)
            machine = Machine(mac=item['mac'],
                              port=item['port'],
                              vlan=item['vlan'],
                              switch_id=ip_switch[item['switch_ip']].id)
            session.add(machine)
Esempio n. 13
0
def poll_switch(ip_addr, req_obj='mac', oper="SCAN"):
    """Query switch and return expected result

    .. note::
       When polling switch succeeds, for each mac it got from polling switch,
       A Machine record associated with the switch is added to the database.

    :param ip_addr: switch ip address.
    :type ip_addr: str
    :param req_obj: the object requested to query from switch.
    :type req_obj: str
    :param oper: the operation to query the switch.
    :type oper: str, should be one of ['SCAN', 'GET', 'SET']

    .. note::
       The function should be called inside database session scope.

    """
    if not ip_addr:
        logging.error('No switch IP address is provided!')
        return

    #Retrieve vendor info from switch table
    session = database.current_session()
    switch = session.query(Switch).filter_by(ip=ip_addr).first()
    logging.info("pollswitch: %s", switch)
    if not switch:
        logging.error('no switch found for %s', ip_addr)
        return

    credential = switch.credential
    logging.error("pollswitch: credential %r", credential)
    vendor = switch.vendor
    hdmanager = HDManager()

    if not vendor or not hdmanager.is_valid_vendor(ip_addr,
                                                   credential, vendor):
        # No vendor found or vendor doesn't match queried switch.
        logging.debug('no vendor or vendor had been changed for switch %s',
                      switch)
        vendor = hdmanager.get_vendor(ip_addr, credential)
        logging.debug('[pollswitch] credential %r', credential)
        if not vendor:
            logging.error('no vendor found or match switch %s', switch)
            return
        switch.vendor = vendor

    # Start to poll switch's mac address.....
    logging.debug('hdmanager learn switch from %s %s %s %s %s',
                  ip_addr, credential, vendor, req_obj, oper)
    results = hdmanager.learn(ip_addr, credential, vendor, req_obj, oper)
    logging.info("pollswitch %s result: %s", switch, results)
    if not results:
        logging.error('no result learned from %s %s %s %s %s',
                      ip_addr, credential, vendor, req_obj, oper)
        return

    for entry in results:
        mac = entry['mac']
        machine = session.query(Machine).filter_by(mac=mac).first()
        if not machine:
            machine = Machine(mac=mac)
            machine.port = entry['port']
            machine.vlan = entry['vlan']
            machine.switch = switch

    logging.debug('update switch %s state to under monitoring', switch)
    switch.state = 'under_monitoring'
Esempio n. 14
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)
Esempio n. 15
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)