class AddSwitch(object): """A utility class that handles adding a switch and retrieving corresponding machines associated with the switch. """ def __init__(self, server_url): print server_url, " ...." self._client = Client(server_url) def add_switch(self, queue, ip, snmp_community): """Add a switch with SNMP credentials and retrieve attached server machines. :param queue: The result holder for the machine details. :type queue: A Queue object(thread-safe). :param ip: The IP address of the switch. :type ip: string. :param snmp_community: The SNMP community string. :type snmp_community: string. """ status, resp = self._client.add_switch(ip, version="2c", community=snmp_community) if status > 409: queue.put((ip, (False, "Failed to add the switch (status=%d)" % status))) return if status == 409: # This is the case where the switch with the same IP already # exists in the system. We now try to update the switch # with the given credential. switch_id = resp['failedSwitch'] status, resp = self._client.update_switch(switch_id, version="2c", community=snmp_community) if status > 202: queue.put((ip, (False, "Failed to update the switch (status=%d)" % status))) return switch = resp['switch'] state = switch['state'] switch_id = switch['id'] # if the switch state is not in under_monitoring, # wait for the poll switch task while True: status, resp = self._client.get_switch(switch_id) if status > 400: queue.put((ip, (False, "Failed to get switch status"))) return switch = resp['switch'] state = switch['state'] if state == 'initialized' or state == 'repolling': time.sleep(5) else: break if state == 'under_monitoring': # get machines connected to the switch. status, response = self._client.get_machines(switch_id=switch_id) if status == 200: for machine in response['machines']: queue.put((ip, "mac=%s, vlan=%s, port=%s dbid=%d" % ( machine['mac'], machine['vlan'], machine['port'], machine['id']))) else: queue.put((ip, (False, "Failed to get machines %s" % response['status']))) else: queue.put((ip, (False, "Switch state is %s" % state)))
def __init__(self, server_url): print server_url, " ...." self._client = Client(server_url)
def _get_client(self): return Client(flags.OPTIONS.compass_server)
def _get_client(): """get apiclient object.""" return Client(flags.OPTIONS.compass_server)
PUBLIC_IP_END = '12.234.32.255' PUBLIC_NETMASK = '255.255.255.0' PUBLIC_NIC = 'eth1' PUBLIC_PROMISC = 1 STORAGE_IP_START = '172.16.100.100' STORAGE_IP_END = '172.16.100.255' STORAGE_NETMASK = '255.255.255.0' STORAGE_NIC = 'eth0' STORAGE_PROMISC = 0 HOME_PERCENTAGE = 40 TMP_PERCENTAGE = 10 VAR_PERCENTAGE = 15 ROLES_LIST = [[], ['os-single-controller']] # get apiclient object. client = Client(COMPASS_SERVER_URL) # get all switches. status, resp = client.get_switches() print 'get all switches status: %s resp: %s' % (status, resp) # add a switch. status, resp = client.add_switch(SWITCH_IP, version=SWITCH_SNMP_VERSION, community=SWITCH_SNMP_COMMUNITY) print 'add a switch status: %s resp: %s' % (status, resp) if status < 400: switch = resp['switch'] else:
'NTP_SERVER': '192.168.10.1', 'GATEWAY': '192.168.10.1', 'PROXY': 'http://192.168.10.1:3128', 'ROLES_LIST': 'os-dashboard', 'MACHINES_TO_ADD': '00:11:20:30:40:01', 'BUILD_TIMEOUT': 60 } for v in PRESET_VALUES: if v in os.environ.keys(): PRESET_VALUES[v] = os.environ.get(v) print (v + PRESET_VALUES[v] + " is set by env variables") else: print (PRESET_VALUES[v]) # get apiclient object. client = Client(COMPASS_SERVER_URL) # get all switches. status, resp = client.get_switches() print 'get all switches status: %s resp: %s' % (status, resp) # add a switch. status, resp = client.add_switch( SWITCH_IP, version=SWITCH_SNMP_VERSION, community=SWITCH_SNMP_COMMUNITY) print 'add a switch status: %s resp: %s' % (status, resp) if status < 400: switch = resp['switch']
) print ('all clusters/hosts are installed.') install_finished = True exit else: print ('there are some clusters/hosts in installing. \r\nsleep 10 seconds and retry') time.sleep(2.0) if __name__ == '__main__': COMPASS_SERVER_URL="http://10.1.0.12/api" COMPASS_USER_EMAIL="*****@*****.**" COMPASS_USER_PASSWORD="******" logsetting.init() client = Client(COMPASS_SERVER_URL) _login(client,COMPASS_USER_EMAIL,COMPASS_USER_PASSWORD) status, resp = client.list_hosts() #print '\r\nget all hosts status: %s resp\r\n' % (status) #print json.dumps(resp,indent=2) if status >= 400: msg = 'failed to get subnets' raise Exception(msg) host_mapping = {} for host in resp: host_mapping[host['hostname']] = host['id'] print '\r\nget hostmapping: %s \r\n' % (host_mapping) status, resp = client.list_clusters() #print '\r\nget all cluster status: %s resp: \r\n' % (status) #print json.dumps(resp,indent=2)
class AddSwitch(object): """A utility class. Handles adding a switch and retrieving corresponding machines associated with the switch. """ def __init__(self, server_url): print server_url, " ...." self._client = Client(server_url) def add_switch(self, queue, ip, snmp_community): """Add a switch with SNMP credentials. :param queue: The result holder for the machine details. :type queue: A Queue object(thread-safe). :param ip: The IP address of the switch. :type ip: string. :param snmp_community: The SNMP community string. :type snmp_community: string. """ status, resp = self._client.add_switch(ip, version="2c", community=snmp_community) if status > 409: queue.put( (ip, (False, "Failed to add the switch (status=%d)" % status))) return if status == 409: # This is the case where the switch with the same IP already # exists in the system. We now try to update the switch # with the given credential. switch_id = resp['failedSwitch'] status, resp = self._client.update_switch(switch_id, version="2c", community=snmp_community) if status > 202: queue.put( (ip, (False, "Failed to update the switch (status=%d)" % status))) return switch = resp['switch'] state = switch['state'] switch_id = switch['id'] # if the switch state is not in under_monitoring, # wait for the poll switch task while True: status, resp = self._client.get_switch(switch_id) if status > 400: queue.put((ip, (False, "Failed to get switch status"))) return switch = resp['switch'] state = switch['state'] if state == 'initialized' or state == 'repolling': time.sleep(5) else: break if state == 'under_monitoring': # get machines connected to the switch. status, response = self._client.get_machines(switch_id=switch_id) if status == 200: for machine in response['machines']: queue.put((ip, "mac=%s, vlan=%s, port=%s dbid=%d" % (machine['mac'], machine['vlan'], machine['port'], machine['id']))) else: queue.put( (ip, (False, "Failed to get machines %s" % response['status']))) else: queue.put((ip, (False, "Switch state is %s" % state)))