def wait_until_cluster_ready(detailed_state_str="", **kwargs): """ Blocks until the controller cluster state is up or if a detailed_state_str was passed, then returns when the cluster reaches that state """ # uses site_name or config config = AviConfig.get_instance() ctrl_vm = config.get_vm_of_type('controller')[0].ip logger.debug('controller used in wait until cluster ready: %s' % ctrl_vm) rsp = None try: session = get_session() session.password = '******' session.reset_session() status_code, rsp = get('cluster', path='runtime') except Exception as e: fail('Cluster api runtime exception %s' % str(e)) if rsp and status_code == 200: # REVIEW do we need this logic implicitly checking status code still? cluster_state = rsp.get('cluster_state', {}) if ('CLUSTER_UP' in cluster_state.get('state', '') and not 'HA_NOT_READY' in cluster_state.get('state', '')): logger.info('Controller cluster is ready with cluster_state %s' % cluster_state) elif cluster_state.get('reason'): if (detailed_state_str and detailed_state_str in cluster_state.get('reason')): logger.info('Controller cluster is ready with %s' % detailed_state_str) else: fail('cluster state[%s]: %s' % (ctrl_vm, cluster_state.get('state', 'unknown'))) else: fail('cluster state[%s]: %s' % (ctrl_vm, cluster_state.get('state', 'unknown'))) elif rsp is None: fail('Cluster api runtime exception: no response.') else: fail('Cluster api runtime returned %d' % status_code)
def make_follower_ready_for_cluster(ctrl_vm, **kwargs): """ Resets Controller password to admin/admin """ config = AviConfig.get_instance() mode = config.get_mode() logger.debug("Current Default Mode %s" % mode) username = mode['user'] current_password = mode['password'] logger.info('Reset controller password for %s' % ctrl_vm.ip) try: config.switch_mode(password=ctrl_vm.password) session = create_session(ctrl_vm) config.switch_mode(session=session) # REVIEW password should be original default password reset_admin_user(username=username, password='******', old_password=ctrl_vm.password, **kwargs) except Exception as e: logger.debug("Trying with admin/admin") config.switch_mode(password='******') session = create_session(ctrl_vm) config.switch_mode(session=session) # REVIEW password shoulde original default password reset_admin_user(username=username, password='******', old_password='******', **kwargs) config.switch_mode(session=None, password=current_password)
def get_ip_addresses_assigned(self): import avi_objects.rest as rest from avi_objects.avi_config import AviConfig ip_list = [] config = AviConfig.get_instance() mode = config.get_mode() current_tenant = mode['tenant'] current_cloud = mode['cloud'] config.switch_mode(tenant='*', cloud=None) # Get Virtualservice IPs st, virtualservices = rest.get('virtualservice?page_size=1000') virtualservices = virtualservices['results'] for vs_name in virtualservices: vips_obj = vs_name.get('vip', []) for vip in vips_obj: if 'ip_address' in vip: ip_list.append(vip['ip_address']['addr']) if 'ip6_address' in vip: ip_list.append(vip['ip6_address']['addr']) # Get Pool Servers IPs st, pools = rest.get('pool?page_size=1000') pools = pools['results'] for pool in pools: servers = pool.get('servers', []) for server in servers: ip_list.append(server['ip']['addr']) config.switch_mode(tenant=current_tenant, cloud=current_cloud) logger.trace('Configured IP Addresses %s' % ip_list) return ip_list
def sdk_connect(self): """ Init the AZURE object from configuration params """ if not self.credentials: from avi_objects.avi_config import AviConfig config = AviConfig.get_instance() site_name = config.get_mode().get('site_name') self.cloud_name = self.configuration.get('name', 'Default-Cloud') credentials = get_credentials(self.cloud_type, self.cloud_name) if not self.subscription_id: self.subscription_id = self.configuration.get( 'subscription_id', None) if self.subscription_id: self.subscription_id = str(self.subscription_id) if not self.application_id: self.cc_user_info = config.testbed[site_name].pre_configuration[ 'CloudConnectorUser'][0] self.application_id = self.cc_user_info[ 'azure_serviceprincipal'].get('application_id', None) if not self.secret_key: self.secret_key = credentials.get('secret_key', None) if not self.tenant_id: self.tenant_id = self.cc_user_info['azure_serviceprincipal'].get( 'tenant_id', None) if not self.resource_group: self.resource_group = self.configuration.get( 'resource_group', None) if not self.vnet_id: self.vnet_id = self.configuration['network_info'][0].get( "virtual_network_id") if not self.subnet_uuid: self.subnet_uuid = self.configuration['network_info'][0].get( 'se_network_id') # # Below 4 params needed for setting up the testbed # if not self.subnet_id: # self.subnet_id = self.configuration.get("virtual_network_id") + '/subnets/' + self.configuration.get('se_network_id') # if not self.storage_account: # self.storage_account = get_testbed_variable(variable='storage_account') # if not self.storage_account: # self.storage_account_key = get_testbed_variable(variable='storage_account_key') # if not self.storage_account: # self.container_name = get_testbed_variable(variable='container_name') if (not self.subscription_id or not self.application_id or not self.application_id or not self.tenant_id or not self.resource_group): logger.info( 'Either or all of subscription_id, secret key, client id, tenant id, resource_group are empty' ) return logger.info("Initializing Azure with %s %s %s %s" % (self.subscription_id, self.tenant_id, self.resource_group, self.vnet_id)) self.azure_init()
def wait_until_n_cluster_nodes_ready(n=0, wait_time=MAX_TIME_FOR_CLUSTER_READY, removed_vm=None, **kwargs): if n == 0: n = len(infra_utils.get_vm_of_type('controller')) logger.info('Wait until n cluster nodes are ready [n=%s]' % str(n)) if not isinstance(removed_vm, list): removed_vm = [removed_vm] if n > 3: # In case the tb file has > 3 controllers defined. n = 3 prev_choice = [] config = AviConfig.get_instance() @aretry(retry=40, delay=30, maxtime=wait_time) def wait_until_n_cluster_nodes_ready_inner(): rsp = None try: st, rsp = get('cluster/runtime') except Exception as ex: fail('Cluster api runtime exception: %s' % ex) if rsp and st == 200: node_states = rsp.get('node_states', []) cluster_state = rsp.get('cluster_state', {}) cl_state = cluster_state.get('state', 'unknown') up_nodes = 0 for node in node_states: if node.get('state') == 'CLUSTER_ACTIVE': up_nodes += 1 if (up_nodes != n): logger.debug('Cluster (status:%s) expects %d active nodes ' 'but contains %d active nodes' % (cl_state, n, up_nodes)) elif (n == 1 and cl_state == 'CLUSTER_UP_NO_HA'): logger.info('Cluster is ready! Cluster state is %s' % cluster_state) return elif (n == 2 and cl_state == 'CLUSTER_UP_HA_COMPROMISED'): logger.info('Cluster is ready! Cluster state is %s' % cluster_state) return elif (n == 3 and cl_state == 'CLUSTER_UP_HA_ACTIVE'): logger.info('Cluster is ready! Cluster state is %s' % cluster_state) return fail('Cluster runtime response not as expected %s' % (rsp if rsp else 'None')) wait_until_n_cluster_nodes_ready_inner()
def get_and_delete_all_configs(skip_cloud=False, check_status_code=False, tenant_list=[], fix_url=True, **kwargs): move_all_se_to_group('Default-Group') session = get_session() config = AviConfig.get_instance() defaults = get('default-values').json() logger.info(defaults) tenant_resp = get('tenant').json() if not tenant_list: tenants = [] tenants = [str(entry['name']) for entry in tenant_resp.get('results', [])] else: tenants = tenant_list for _tenant in tenants: switch_mode(tenant=_tenant) for obj_type in reversed(obj_order): if (((obj_type == 'cloud' or obj_type == 'tenant') and skip_cloud) or (obj_type in ['sslcertificaterequest', 'staticroute'])): continue status_code, data = get(obj_type, check_status_code=check_status_code) if status_code > 400: continue for d in data['results']: if obj_type == 'cloud' and d['name'] == 'Default-Cloud': if d['vtype'] != 'CLOUD_NONE': logger.info('Update Default-Cloud from %s to no-access' % d['vtype']) if d.get('vcenter_configuration'): d.pop('vcenter_configuration') elif d.get('openstack_configuration'): d.pop('openstack_configuration') elif d.get('aws_configuration'): d.pop('aws_configuration') elif d.get('cloudstack_configuration'): d.pop('cloudstack_configuration') elif d.get('vca_configuration'): d.pop('vca_configuration') elif d.get('apic_configuration'): d.pop('apic_configuration') d['vtype'] = 'CLOUD_NONE' put('cloud', name=d['name'], data=json.dumps(d)) # review can we use uuid=d['uuid']? if obj_type in defaults.get('default', []) and \ d['uuid'] in defaults['default'][obj_type]: continue logger.info('Deleting: %s:%s' % (obj_type, d['name'])) if obj_type in ['sslcertificaterequest', 'sslkeyandcertificate_import']: delete('sslkeyandcertificate', name=d['name'], check_status_code=False) else: delete(obj_type, name=d['name'], check_status_code=False)
def set_sysadmin_public_key(enable_basic_auth=True, **kwargs): sysadmin_public_key_path = '/test/robot/new/lib/tools/id_sysadmin.pub' sysadmin_priv_key_path = '/test/robot/new/lib/tools/id_sysadmin' config = AviConfig.get_instance() controller_obj = config.get_vm_of_type('controller')[0].ip # first enable basic auth and then use REST API. if enable_basic_auth: enable_controller_basic_authentication() logger.info('Finish updating controller to allow basic auth') logger.info('-- set_sysadmin_public_key -- %s\n' % (controller_obj)) data = {} data['action'] = 'create' workspace = suite_vars.workspace key_path = workspace + sysadmin_public_key_path key_str = None try: with open(key_path, 'r') as f: key_str = f.read() except: raise Exception('Could not ready sysadmin pub key') data['key'] = key_str logger.info('Data after: %s---' % data) try: # Try with 'adminkey' config = AviConfig.get_instance() current_password = config.get_mode()['password'] config.switch_mode(password='******') r = post('adminkey', data=json.dumps(data)) except Exception as e: # Try with 'resetsysadminkey' logger.info("Got Exception %s with adminkey. Trying with resetsysadminkey" % e) r = post('resetsysadminkey', data=json.dumps(data)) config.switch_mode(password=current_password)
def reboot_clean(update_admin_info=True, **kwargs): post('cluster', path='reboot', data=json.dumps({'mode': 'REBOOT_CLEAN'})) asleep(msg="Sleep before cluster wait check", delay=120) wait_until_cluster_ready() set_sysadmin_public_key() set_systemconfiguration() set_ha_mode_best_effort() if update_admin_info: config = AviConfig.get_instance() mode = config.get_mode() logger.debug("Current Default Mode %s" % mode) username = mode['user'] password = '******' mode["password"] = '******' update_admin_user(username=username, password=password) switch_mode(**mode)
def avi_setup(request): suite_vars.initialize_vars() suite_vars.workspace = set_workspace() if not suite_vars.log_dir: suite_vars.log_dir = str(suite_vars.workspace) logger._init_file_handler(log_dir=suite_vars.log_dir) logger.setlevel(suite_vars.loglevel) config = AviConfig() initialize_testbed() old_cores = get_core_filenames() if old_cores: logger.info("Existing core files " + str(old_cores)) def fin(): new_cores = get_core_filenames() warn_if_new_cores(old_cores, new_cores) #TODO: Need to integrate eporting by e-mail and move the cores to bug folder if the run is on Jenkins request.addfinalizer(fin) return config
def enable_controller_basic_authentication(): config = AviConfig.get_instance() c_uri = config.get_vm_of_type('controller')[0].ip # REVIEW why isn't this using rest.post? def _log_out(session): logout_p = 'https://%s/logout' % c_uri rsp = session.post(logout_p, "{}", verify=False) if rsp.status_code >= 400: logger.info("ERROR when logout, return status %s" % rsp.status_code) logger.info('---change systemconfig to enable basic authentication') session = requests.Session() login_url = 'https://%s/login' % c_uri headers = {"Content-Type": "application/json"} session.headers.update(headers) system_path = 'https://%s/api/systemconfiguration' % c_uri login_success = False login_data = {'username': '******', 'password': '******'} rsp = session.post(login_url, json.dumps(login_data), headers=headers, verify=False) logger.info('Login API response with status %s' % rsp.status_code) csrftoken = requests.utils.dict_from_cookiejar(rsp.cookies).get('csrftoken', '') referer = 'https://%s/' % c_uri session.headers.update({"X-CSRFToken": csrftoken, "Referer": referer}) rsp = session.get(system_path, headers=headers, verify=False) data = rsp.json() if data.get('portal_configuration', {}).get('allow_basic_authentication'): logger.info('Controller already accepts basic auth') _log_out(session) return if 'portal_configuration' not in data: data['portal_configuration'] = {} data['portal_configuration']['allow_basic_authentication'] = True data['portal_configuration']['password_strength_check'] = False logger.info('Updating controller to allow basic auth') rsp = session.put(system_path, json.dumps(data), headers=headers, verify=False) _log_out(session)
def get_node_config_retries(**kwargs): config = AviConfig.get_instance() mode = config.get_mode() rsp = None try: st, rsp = get('cluster', check_status_code=False) except Exception as ex: fail("get_node_config: sdk-exception %s" % str(ex)) logger.info('get_node_config: Got cluster nodes %s' % str(rsp)) if re.search('Authentication credentials were not provided.', str(rsp)): fail('Controller %s is not running basic auth!', force=True) if re.search('Invalid username', str(rsp)): logger.info( 'get_node_config: invalid username/password admin/%s.Try admin/admin' % config.password) config.switch_mode(user='******', password='******') st, rsp = get('cluster', check_status_code=False) if st != 200: fail("Non 200 status code received %s %s" % (st, rsp)) config.switch_mode(**mode) return rsp['nodes']
def cleanup_client_and_servers(): config = AviConfig.get_instance() for vm in config.get_vm_of_type('client'): vm.killtcptest() vm.delete_netem_config() vm.cleanup_sub_ints() vm.flush_arp_cache_entries() for vm in config.get_vm_of_type('server'): vm.killtcptest() vm.delete_netem_config() vm.cleanup_sub_ints() vm.cleanup_server_context_nginx(False) vm.cleanup_server_context_apache(False) vm.flush_arp_cache_entries() mode = config.get_mode() site_name = mode['site_name'] cloud = mode['cloud'] cloud_obj = config.testbed[site_name].cloud_obj[cloud] cloud_obj.cleanup_all() # REVIEW from a hierarchical standpoint it makes more sense to do this from PoolModel from avi_objects.pool import ServerModel ServerModel.clear_servers()
def get_config(): """ API helps to get the config Object """ config = AviConfig.get_instance() return config