def do_etcd_gc(): fname = 'do_etcd_gc' kube_client = get_kube_client() while True: LOG.info('{}: start'.format(fname)) key = '/paas/applications' value = etcd_client.read(key) namespace_list = [] for sub_item in value._children: namespace_list.append(os.path.basename(sub_item['key'])) for namespace in namespace_list: pod_names = {} pod_list = kube_client.GetPods(namespace).Items for pod in pod_list: pod_names['/paas/applications/{}/pods/{}'.format( namespace, pod.Name)] = None key = '/paas/applications/{}/pods'.format(namespace) try: value = etcd_client.read(key) for sub_item in value._children: if sub_item['key'] not in pod_names: etcd_client.delete(sub_item['key']) LOG.info('{} is delete from ETCD'.format( sub_item['key'])) except Exception, e: LOG.error('{}: {}'.format(fname, e)) LOG.info('{}: done'.format(fname)) greenthread.sleep(60)
def get(self, name): try: node = get_node(name) response_data = node.dump_as_dict() response_data['kind'] = 'Node' response = flask.make_response(json.dumps(response_data)) response.headers['Access-Control-Allow-Origin'] = '*' return response except NodeNotFound as e: response_json = { 'kind': 'Status', 'code': 404, 'message': 'Can not find the node' } response = flask.make_response(json.dumps(response_json)) response.headers['Access-Control-Allow-Origin'] = '*' return response except Exception as e: LOG.error(e) response_data = { 'kind': 'Status', 'code': 500, 'message': 'internal server error' } response = flask.make_response(json.dumps(response_data)) response.headers['Access-Control-Allow-Origin'] = '*' return response
def _validate_config(subset, superset, whitelist_keys): def get_err_config(subset, superset): result = {} for k, v in subset.items(): if k not in whitelist_keys: if k not in superset: result.update({k: v}) elif v is not None and superset[k] is not None: if not isinstance(v, type(superset[k])): result.update({k: v}) continue if isinstance(v, dict): res = get_err_config(v, superset[k]) if res: result.update({k: res}) if not result: return None return result err_cfg = get_err_config(subset, superset) if err_cfg: err_msg = 'The provided configuration has unknown options or values with invalid type: '\ + str(err_cfg) LOG.error(err_msg) raise Exception(err_msg)
def sanitize_az_host(self, host_list, az_host): ''' host_list: list of hosts as retrieved from openstack (can be empty) az_host: either a host or a az:host string if a host, will check host is in the list, find the corresponding az and return az:host if az:host is passed will check the host is in the list and az matches if host_list is empty, will return the configured az if there is no az passed ''' if ':' in az_host: # no host_list, return as is (no check) if not host_list: return az_host # if there is a host_list, extract and verify the az and host az_host_list = az_host.split(':') zone = az_host_list[0] host = az_host_list[1] for hyp in host_list: if hyp.host == host: if hyp.zone == zone: # matches return az_host # else continue - another zone with same host name? # no match LOG.error('No match for availability zone and host ' + az_host) return None else: return self.auto_fill_az(host_list, az_host)
def get_controller_info(ssh_access, net, res_col, retry_count): if not ssh_access: return LOG.info('Fetching OpenStack deployment details...') sshcon = sshutils.SSH(ssh_access, connect_retry_count=retry_count) if sshcon is None: LOG.error('Cannot connect to the controller node') return res = {} res['distro'] = sshcon.get_host_os_version() res['openstack_version'] = sshcon.check_openstack_version() res['cpu_info'] = sshcon.get_cpu_info() if net: l2type = res_col.get_result('l2agent_type') encap = res_col.get_result('encapsulation') if l2type: if encap: res['nic_name'] = sshcon.get_nic_name(l2type, encap, net.internal_iface_dict) res['l2agent_version'] = sshcon.get_l2agent_version(l2type) # print results CONLOG.info(res_col.ppr.pformat(res)) FILELOG.info(json.dumps(res, sort_keys=True)) res_col.add_properties(res)
def savestatstofile(filename="serverstats.bin", servers=[]): if not config.getboolean("serverstats", "savehistory"): return t = time.time() try: try: import cPickle as pickle except: import pickle except ImportError: LOG.error("error while loading the pickle module...") return try: f = open(filename, 'rb') oldstats = pickle.load(f) f.close() except IOError: oldstats = {} oldstats[t] = servers try: f = open(filename, 'wb') pickle.dump(oldstats, f) f.close() except IoError: LOG.error("error while saving history file!")
def jsonloads(jsonstr): try: return json.loads(jsonstr) except ValueError: errmsg = "xCAT response data is not in JSON format" LOG.error(errmsg) raise ZVMException(msg=errmsg)
def add_mdisk(self, instance_name, diskpool, vdev, size, fmt=None): """Add a 3390 mdisk for a z/VM user. NOTE: No read, write and multi password specified, and access mode default as 'MR'. """ disk_type = CONF.zvm_diskpool_type if (disk_type == 'ECKD'): action = '--add3390' elif (disk_type == 'FBA'): action = '--add9336' else: errmsg = ("Disk type %s is not supported.") % disk_type LOG.error(errmsg) raise ZVMException(msg=errmsg) if fmt: body = [ " ".join([ action, diskpool, vdev, size, "MR", "''", "''", "''", fmt ]) ] else: body = [" ".join([action, diskpool, vdev, size])] url = zvmutils.get_xcat_url().chvm('/' + instance_name) zvmutils.xcat_request("PUT", url, body)
def create_userid(self, instance_name, cpu, memory, image_name): """Create z/VM userid into user directory for a z/VM instance.""" LOG.debug("Creating the z/VM user entry for instance %s" % instance_name) kwprofile = 'profile=%s' % const.ZVM_USER_PROFILE body = [ kwprofile, 'password=%s' % CONF.zvm_user_default_password, 'cpu=%i' % cpu, 'memory=%im' % memory, 'privilege=%s' % const.ZVM_USER_DEFAULT_PRIVILEGE, 'ipl=%s' % CONF.zvm_user_root_vdev, 'imagename=%s' % image_name ] url = zvmutils.get_xcat_url().mkvm('/' + instance_name) try: zvmutils.xcat_request("POST", url, body) size = CONF.root_disk_units # Add root disk and set ipl self.add_mdisk(instance_name, CONF.zvm_diskpool, CONF.zvm_user_root_vdev, size) self.set_ipl(instance_name, CONF.zvm_user_root_vdev) except Exception as err: msg = ("Failed to create z/VM userid: %s") % err LOG.error(msg) raise ZVMException(msg=err)
def process_tvsou(cid, date, retry=3): import api api, ts = api.GET_API() api = api[0].encode("utf-8") ts = ts[0].encode("utf-8") url = 'https://www.tvsou.com' + api sub_data = {'date': date, 'channelid': cid, 't': ts} print url, sub_data data = None while retry > 0: data = post(url, sub_data, ctype='json') if not data: LOG.error('[%s]No data, try again.' % cid) retry -= 1 else: break if not data: return None datas = list() print data data = json_load(data) for obj in data.get('list'): datas.append({ 'time': timestamp_to_time(obj.get('playtimes')), 'program_name': obj.get('title') }) return datas
def reset_irq_affinity(self, uuid, irqs=None, msi_irqs=None): """Reset irq affinity for instance The instance has already been deleted or related PCI not used by it anymore. """ if irqs or msi_irqs: # reset irq affinity for specified irqs _irqs = irqs _msi_irqs = msi_irqs elif uuid in self.inst_dict: # reset all irq affinity for deleted instance _irqs = self.inst_dict[uuid][0] _msi_irqs = self.inst_dict[uuid][1] else: LOG.debug("No pci affinity need to be reset for instance=%s!" % uuid) return try: with open('/proc/irq/default_smp_affinity') as f: cpulist = f.readline().strip() LOG.debug("default smp affinity bitmap:%s" % cpulist) for x in [_irqs, _msi_irqs]: if len(x) > 0: pci_utils.set_irq_affinity(True, x, cpulist) except Exception as e: LOG.error("Failed to reset smp affinity! error=%s" % e) LOG.info("Reset smp affinity done for instance=%s!" % uuid)
def get_application(application_name, is_summary=False): if not is_summary: session = requests.session() url = 'http://{}:8080/api/v1/namespaces/{}/pods'.format( settings.K8S_IP, application_name) reply = session.get(url) pod_list_json = reply.json()['items'] url = 'http://{}:8080/api/v1/namespaces/{}/replicationcontrollers'.format( settings.K8S_IP, application_name) reply = session.get(url) rc_list_json = reply.json()['items'] url = 'http://{}:8080/api/v1/namespaces/{}/services'.format( settings.K8S_IP, application_name) reply = session.get(url) svc_list_json = reply.json()['items'] try: stack = heat_client.get_stack(application_name) stack_json = stack.to_dict() if not is_summary: paas_app = PaasApplication( application_name, stack_json, heat_client.get_resource_list(application_name), rc_list_json, pod_list_json, svc_list_json) else: paas_app = PaasApplication(application_name, stack_json, None, None, None, None) return paas_app except Exception, e: LOG.error(e) LOG.error(traceback.format_exc()) return None
def main(argv): app = Flask(__name__) restful_api = Api(app) from log import LOG from settings import settings try: save_pid() load_unversioned_api(restful_api) load_v1_api(restful_api) except Exception as e: if settings.DEBUG: traceback.print_exc() LOG.error(str(e)) LOG.error('exit 1') sys.exit(1) LOG.info('Started') app.run(host=settings.BINDING_ADDR, port=settings.PORT, debug=settings.DEBUG, threaded=settings.USE_THREAD, use_reloader=settings.USE_RELOADER)
def read_uri(self, uri): regex_pattern = URI_REGEX.format(self.route_prefix) if not re.match(regex_pattern, uri): LOG.error('URI does not match expected format. %s', uri) raise BadRequest return self.read(int(uri.split('/')[-1]))
def measure_flow(self, label, target_ip): label = self.add_location(label) FlowPrinter.print_desc(label) # results for this flow as a dict perf_output = self.client.run_client(label, target_ip, self.server, bandwidth=self.config.vm_bandwidth, az_to=self.server.az) if self.config.keep_first_flow_and_exit: CONLOG.info(self.rescol.ppr.pformat(perf_output)) FILELOG.info(json.dumps(perf_output, sort_keys=True)) LOG.info('Stopping execution after first flow, cleanup all VMs/networks manually') sys.exit(0) if self.config.stop_on_error: # check if there is any error in the results results_list = perf_output['results'] for res_dict in results_list: if 'error' in res_dict: LOG.error('Stopping execution on error, cleanup all VMs/networks manually') CONLOG.info(self.rescol.ppr.pformat(perf_output)) FILELOG.info(json.dumps(perf_output, sort_keys=True)) sys.exit(2) self.rescol.add_flow_result(perf_output) CONLOG.info(self.rescol.ppr.pformat(perf_output)) FILELOG.info(json.dumps(perf_output, sort_keys=True))
def check_component(app_name, component_json, app_controller): fname = 'check_component' ''' If current component's status is OK, return True. If current component's status is not OK, return False ''' if component_json['replicas'] != len(component_json['pods']): return False if component_json['replicas'] == 1: return True pod_need_killed = None is_component_ready = True for pod in component_json['pods']: if not pod['is_ready'] or not pod['is_running']: is_component_ready = False break try: if float(pod['mem_usage']) / float( pod['max_mem_limit'] ) * 100 >= app_controller['memory_threshold']: pod_need_killed = pod except Exception, e: LOG.error('{}: {}'.format(fname, e))
def run(self, json_msg, *args, **kwargs): msg = related.from_json(json_msg, EmailMessage) try: self.email_client.send(mail_msg=msg) except NotificationError: LOG.error('Failed to send email to %s in the background', msg.to_addr)
def process_main(): """Entry function for PCI Interrupt Affinity Agent""" LOG.info("Enter PCIInterruptAffinity Agent") try: signal.signal(signal.SIGTSTP, process_signal_handler) openstack_enabled = sysconfig.get('openstack', 'openstack_enabled') if openstack_enabled == 'true': novaClient.open_libvirt_connect() audit_srv = audits_initialize() rabbit_client = start_rabbitmq_client() while stay_on: time.sleep(1) except KeyboardInterrupt: LOG.info("keyboard Interrupt received.") pass except Exception as e: LOG.info("%s" % e) sys.exit(200) finally: LOG.error("proces_main finalized!!!") if openstack_enabled == 'true': novaClient.close_libvirt_connect() audit_srv.tg.stop() rabbit_client.stop()
def _close(self): if self.ovsdb: try: self.ovsdb.close() except Exception as e: LOG.error(str(e)) self.ovsdb = None
def create_server(self, vmname, image, flavor, key_name, nic, sec_group, avail_zone=None, user_data=None, config_drive=None, files=None, retry_count=10, volume=None): if sec_group: security_groups = [sec_group["name"]] else: security_groups = None # Also attach the created security group for the test vol_map = None if volume: vol_map = [{ "source_type": "volume", "boot_index": "0", "uuid": volume.id, "destination_type": "volume" }] image = None instance = self.novaclient.servers.create( name=vmname, image=image, block_device_mapping_v2=vol_map, flavor=flavor, key_name=key_name, nics=nic, availability_zone=avail_zone, userdata=user_data, config_drive=config_drive, files=files, security_groups=security_groups) if not instance: return None # Verify that the instance gets into the ACTIVE state for retry_attempt in range(retry_count): instance = self.novaclient.servers.get(instance.id) if instance.status == 'ACTIVE': return instance if instance.status == 'ERROR': LOG.error('Instance creation error: %s', instance.fault['message']) break LOG.debug("[%s] VM status=%s, retrying %s of %s...", vmname, instance.status, (retry_attempt + 1), retry_count) time.sleep(2) # instance not in ACTIVE state LOG.error('Instance failed status=%s', instance.status) self.delete_server(instance) return None
def set_irqs_affinity_by_pci_address(pci_addr, extra_spec=None, numa_topology=None): """Set cpu affinity for list of PCI IRQs with a VF's pci address, Restrict cpuset to the numa node of the PCI. Return list Raises PciDeviceNotFoundById in case the pci device is not found, or when there is an underlying problem getting associated irqs. :param pci_addr: PCI address :param extra_spec: extra_spec :param numa_topology: instance numa topology :return: irqs, msi_irqs, numa_node, cpulist """ irqs = set() msi_irqs = set() numa_node = None cpulist = '' if numa_topology is None: return (irqs, msi_irqs, numa_node, cpulist) # Get the irqs associated with pci addr _irqs, _msi_irqs = get_irqs_by_pci_address(pci_addr) LOG.debug("pci: %s, irqs: %s, msi_irqs: %s" % (pci_addr, _irqs, _msi_irqs)) # Obtain physical numa_node for this pci addr numa_path = "/sys/bus/pci/devices/%s/numa_node" % (pci_addr) try: with open(numa_path) as f: numa_node = [int(x) for x in f.readline().split()][0] except Exception as e: LOG.error( 'set_irqs_affinity_by_pci_address: ' 'pci_addr=%(A)s: numa_path=%(P)s; error=%(E)s', { 'A': pci_addr, 'P': numa_path, 'E': e }) raise Exception("PciDeviceNotFoundById id = %r" % pci_addr) # Skip irq configuration if there is no associated numa node if numa_node is None or numa_node < 0: return (irqs, msi_irqs, numa_node, cpulist) # Determine the pinned cpuset where irqs are to be affined cpuset, cpulist = get_pci_irqs_pinned_cpuset(extra_spec, numa_topology, numa_node) LOG.debug("cpuset where irqs are to be affined:%s or %s" % (cpuset, cpulist)) # Skip irq configuration if there are no pinned cpus if not cpuset: return (irqs, msi_irqs, numa_node, cpulist) # Set IRQ affinity, but do not treat errors as fatal. irqs = set_irq_affinity(False, _irqs, cpulist) msi_irqs = set_irq_affinity(False, _msi_irqs, cpulist) return (irqs, msi_irqs, numa_node, cpulist)
def get_bdw_kbps(bdw, bdw_unit): if not bdw_unit: # bits/sec return bdw / 1000 if bdw_unit in MULTIPLIERS: return int(bdw * MULTIPLIERS[bdw_unit]) LOG.error('Error: unknown multiplier: ' + bdw_unit) return bdw
def delete(self, obj_typ, id=None, obj=None): if not (id or obj): LOG.error('Give either net_id or net_obj') if obj: id = obj.get('id') return self.sendjson('delete', '%(obj_typ)s/%(id)s' % {'obj_typ': obj_typ, 'id': id})
def _send(self, info): if not self.ovsdb: self._connection() try: self.ovsdb.send(json.dumps(info)) except Exception as e: LOG.error(e.message()) self._close()
def delete_flavor(self, flavor): try: flavor.delete() LOG.info('Flavor %s deleted.' % flavor.name) return True except novaclient.exceptions: LOG.error('Failed deleting flavor %s.' % flavor.name) return False
def expect_invalid_xcat_resp_data(data=''): """Catch exceptions when using xCAT response data.""" try: yield except (ValueError, TypeError, IndexError, AttributeError, KeyError) as err: LOG.error('Parse %s encounter error', data) raise ZVMException(msg=err)
def get_az_host_list(self): avail_list = [] host_list = [] try: host_list = self.novaclient.services.list() except novaclient.exceptions.Forbidden: LOG.warning('Operation Forbidden: could not retrieve list of hosts' ' (likely no permission)') # the user has specified a list of 1 or 2 hypervisors to use if self.config.hypervisors: for hyp in self.config.hypervisors: hyp = self.sanitize_az_host(host_list, hyp) if hyp: avail_list.append(hyp) else: return [] # if the user did not specify an az, insert the configured az if ':' not in hyp: if self.config.availability_zone: hyp = self.normalize_az_host(None, hyp) else: return [] # pick first 2 matches at most if len(avail_list) == 2: break LOG.info('Using hypervisors ' + ', '.join(avail_list)) else: for host in host_list: # this host must be a compute node if host.binary != 'nova-compute' or host.state != 'up': continue candidate = None if self.config.availability_zone: if host.zone == self.config.availability_zone: candidate = self.normalize_az_host(None, host.host) else: candidate = self.normalize_az_host(host.zone, host.host) if candidate: avail_list.append(candidate) # pick first 2 matches at most if len(avail_list) == 2: break # if empty we insert the configured az if not avail_list: if not self.config.availability_zone: LOG.error('Availability_zone must be configured') elif host_list: LOG.error( 'No host matching the selection for availability zone: ' + self.config.availability_zone) avail_list = [] else: avail_list = [self.config.availability_zone] return avail_list
def func(engine): if not engine.running(): LOG.error("Engine not running! Not executing action") return 0 engine._update_game_lock.acquire() done_lines = f(engine) engine.print_game() engine._update_game_lock.release() return done_lines
def main(): data1 = run_gdtv() data2 = run_tvsou() result = dict(data1.items() + data2.items()) for key, val in result.items(): if not val: LOG.error('[%s]channel clawl failed.' % key) continue save_program(key, val) sys.exit(0)
def delete(self, obj_typ, id=None, obj=None): if not (id or obj): LOG.error('Give either net_id or net_obj') if obj: id = obj.get('id') return self.sendjson( 'delete', '%(obj_typ)s/%(id)s' % { 'obj_typ': obj_typ, 'id': id })
def save(self): try: with open(self.file, 'w', encoding='utf-8') as fp: for anno in self.annos: anno_str = '{} {} {} {} {}\n'.format( anno['label'], anno['xmin'], anno['ymin'], anno['xmax'], anno['ymax']) fp.write(anno_str) except: LOG.error("annotation file {} non-existent".format(self.file))
def __get_image_digest(self, image_name, tag): try: url = 'http://{}/v2/{}/manifests/{}'.format(self.server, image_name, tag) r = requests.get(url) if r.status_code == 404: raise HttpException(404) return r.headers['Docker-Content-Digest'] except Exception as e: LOG.error(str(e)) raise e
def decode_size_list(argname, size_list): try: pkt_sizes = size_list.split(',') for i in xrange(len(pkt_sizes)): pkt_sizes[i] = int(pkt_sizes[i]) except ValueError: LOG.error('Invalid %s parameter. A valid input must be ' 'integers seperated by comma.' % argname) sys.exit(1) return pkt_sizes
def delete_image(self, glance_client, img_name): try: LOG.log("Deleting image %s...", img_name) img = self.find_image(glance_client, img_name) glance_client.images.delete(img.id) except Exception: LOG.error("Failed to delete the image %s.", img_name) return False return True
def save_to_db(self, cfg): '''Save results to MongoDB database.''' LOG.info("Saving results to MongoDB database...") post_id = pns_mongo.\ pns_add_test_result_to_mongod(cfg.vmtp_mongod_ip, cfg.vmtp_mongod_port, cfg.vmtp_db, cfg.vmtp_collection, self.results) if post_id is None: LOG.error("Failed to add result to DB")
def test_native_tp(nhosts, ifname, config): FlowPrinter.print_desc('Native Host to Host throughput') result_list = [] server_host = nhosts[0] server = PerfInstance('Host-' + server_host.host + '-Server', config, server=True) if not server.setup_ssh(server_host): server.display('SSH failed, check IP or make sure public key is configured') else: server.display('SSH connected') server.create() # if inter-node-only requested we avoid running the client on the # same node as the server - but only if there is at least another # IP provided if config.inter_node_only and len(nhosts) > 1: # remove the first element of the list nhosts.pop(0) # IP address clients should connect to, check if the user # has passed a server listen interface name if ifname: # use the IP address configured on given interface server_ip = server.get_interface_ip(ifname) if not server_ip: LOG.error('Cannot get IP address for interface ' + ifname) else: server.display('Clients will use server IP address %s (%s)' % (server_ip, ifname)) else: # use same as ssh IP server_ip = server_host.host if server_ip: # start client side, 1 per host provided for client_host in nhosts: client = PerfInstance('Host-' + client_host.host + '-Client', config) if not client.setup_ssh(client_host): client.display('SSH failed, check IP or make sure public key is configured') else: client.buginf('SSH connected') client.create() if client_host == server_host: desc = 'Native intra-host' else: desc = 'Native inter-host' res = client.run_client(desc, server_ip, server, bandwidth=config.vm_bandwidth) result_list.append(res) client.dispose() server.dispose() return result_list
def put_file_to_host(self, from_path, to_path): ''' A wrapper api on top of paramiko scp module, to scp a local file to the remote. ''' sshcon = self._get_client() scpcon = scp.SCPClient(sshcon.get_transport()) try: scpcon.put(from_path, remote_path=to_path) except scp.SCPException as exp: LOG.error("Send failed: [%s]", exp) return 0 return 1
def get_file_from_host(self, from_path, to_path): ''' A wrapper api on top of paramiko scp module, to scp a remote file to the local. ''' sshcon = self._get_client() scpcon = scp.SCPClient(sshcon.get_transport()) try: scpcon.get(from_path, to_path) except scp.SCPException as exp: LOG.error("Receive failed: [%s]", exp) return 0 return 1
def savetofile(self, filename): """ Save the grf database from a file @type filename: string @param filename: the filename to save to """ if not self.canSaveLoad or not self.listchanged or not config.getboolean("serverstats", "savenewgrfs"): return import pickle try: f = open(filename, 'wb') pickle.dump(self.__database, f, 1) f.close() except IOError: LOG.error("error while saving newgrf cache file!")
def http_retriable_request(verb, url, headers={}, authenticate=False, params={}): """ Sends an HTTP request, with automatic retrying in case of HTTP Errors 500 or ConnectionErrors _http_retriable_request('POST', 'http://cc.cloudcomplab.ch:8888/app/', headers={'Content-Type': 'text/occi', [...]} , authenticate=True) :param verb: [POST|PUT|GET|DELETE] HTTP keyword :param url: The URL to use. :param headers: Headers of the request :param kwargs: May contain authenticate=True parameter, which is used to make requests requiring authentication, e.g. CC requests :return: result of the request """ LOG.debug(verb + ' on ' + url + ' with headers ' + headers.__repr__()) auth = () if authenticate: user = CONFIG.get('cloud_controller', 'user') pwd = CONFIG.get('cloud_controller', 'pwd') auth = (user, pwd) if verb in ['POST', 'DELETE', 'GET', 'PUT']: try: r = None if verb == 'POST': if authenticate: r = requests.post(url, headers=headers, auth=auth, params=params) else: r = requests.post(url, headers=headers, params=params) elif verb == 'DELETE': if authenticate: r = requests.delete(url, headers=headers, auth=auth, params=params) else: r = requests.delete(url, headers=headers, params=params) elif verb == 'GET': if authenticate: r = requests.get(url, headers=headers, auth=auth, params=params) else: r = requests.get(url, headers=headers, params=params) elif verb == 'PUT': if authenticate: r = requests.put(url, headers=headers, auth=auth, params=params) else: r = requests.put(url, headers=headers, params=params) r.raise_for_status() return r except requests.HTTPError as err: LOG.error('HTTP Error: should do something more here!' + err.message) raise err
def dispatch(self): """ Dispatch an event to all dispatchers Calls every dispatcher in dispatchTo, if it's a string, it uses getattr, else, it tries to execute """ for to in self.dispatchTo: if not type(to) == str: to(self) else: try: function = getattr(self, to) except AttributeError: LOG.error("Unknown dispatcher in %s: %s" % (self.__class__, to)) else: function()
def get_ssh_access(opt_name, opt_value, config): '''Allocate a HostSshAccess instance to the option value Check that a password is provided or the key pair in the config file is valid. If invalid exit with proper error message ''' if not opt_value: return None host_access = sshutils.SSHAccess(opt_value) host_access.private_key_file = config.private_key_file host_access.public_key_file = config.public_key_file if host_access.error: LOG.error('Error for --' + (opt_name + ':' + host_access.error)) sys.exit(2) return host_access
def __wrapper(*args, **kwargs): timer = 0 while(True): try: if timer <= times: result = f(*args, **kwargs) return result except Exception, e: LOG.debug('Do DB action Exception: %s' % traceback.format_exc()) if timer < times: timer += 1 time.sleep(interval) LOG.error('Start to retry to do db action, TIME: %s' % timer) continue else: LOG.error('Do DB Exception: %s' % traceback.format_exc()) raise e
def run(self): error_flag = False try: self.setup() self.measure_vm_flows() except KeyboardInterrupt: traceback.format_exc() except (VmtpException, sshutils.SSHError, ClientException, Exception): LOG.error(traceback.format_exc()) error_flag = True if self.config.stop_on_error and error_flag: LOG.error('Stopping execution on error, cleanup all VMs/networks manually') sys.exit(2) else: self.teardown()
def retry_if_http_error(exception): """ Defines which type of exceptions allow for a retry of the request :param exception: the raised exception :return: True if retrying the request is possible """ error = False if isinstance(exception, requests.HTTPError): if exception.response.status_code == 503: LOG.info('Requesting retry: response code: ' + str(exception.response.status_code)) LOG.error('Exception: ' + exception.__repr__()) error = True elif isinstance(exception, requests.ConnectionError): LOG.info('Requesting retry: ConnectionError') LOG.error('Exception: ' + exception.__repr__()) error = True return error
def check(self, directory, files): # {{{ super(CheckerGap, self).check(directory, files) result = True nexts = map(lambda x,y: (x,y), sorted(files), sorted(files[1:])) for n in nexts: if not n[1]: # end of list break a = int(n[0].split(".")[0]) b = int(n[1].split(".")[0]) if abs(a-b) != 1: LOG.error("Gap between '%s' and '%s'" % (n[0], n[1])) result = False return result
def ping_check(self, target_ip, ping_count=2, pass_threshold=80): '''helper function to ping from one host to an IP address, for a given count and pass_threshold; Steps: ssh to the host and then ping to the target IP then match the output and verify that the loss% is less than the pass_threshold% Return 1 if the criteria passes Return 0, if it fails ''' cmd = "ping -c " + str(ping_count) + " " + str(target_ip) (_, cmd_output, _) = self.execute(cmd) match = re.search(r'(\d*)% packet loss', cmd_output) pkt_loss = match.group(1) if int(pkt_loss) < int(pass_threshold): return 1 else: LOG.error('Ping to %s failed: %s', target_ip, cmd_output) return 0
def loadfromfile(self, filename): """ Load the grf database from a file @type filename: string @param filename: the filename to load from """ try: import pickle except ImportError: LOG.error("error while loading the pickle module...") self.__database = {} self.canSaveLoad = False return try: f = open(filename, 'rb') self.__database = pickle.load(f) f.close() except IOError: LOG.error("error while opening newgrf cache file!") self.__database = {}
def get_host_os_version(self): ''' Identify the host distribution/relase. ''' os_release_file = "/etc/os-release" sys_release_file = "/etc/system-release" name = "" version = "" if self.stat(os_release_file): data = self.read_remote_file(os_release_file) if data is None: LOG.error("Failed to read file %s", os_release_file) return None for line in data.splitlines(): mobj = re.match(r'NAME=(.*)', line) if mobj: name = mobj.group(1).strip("\"") mobj = re.match(r'VERSION_ID=(.*)', line) if mobj: version = mobj.group(1).strip("\"") os_name = name + " " + version return os_name if self.stat(sys_release_file): data = self.read_remote_file(sys_release_file) if data is None: LOG.error("Failed to read file %s", sys_release_file) return None for line in data.splitlines(): mobj = re.match(r'Red Hat.*', line) if mobj: return mobj.group(0) return None
def updateStats(self): self.last = time.time() if not config.getboolean('stats', 'enable'): return LOG.debug("updating stats...") tstart = time.time() fn = config.get("stats", "cachefilename") obj = [] firstSave = False try: f = open(fn, 'rb') obj = pickle.load(f) f.close() except IOError: firstSave=True value = [ self.client.getGameInfo(encode_grfs=True, short=not firstSave), self.client.getCompanyInfo().companies, tstart ] obj.append(value) try: f = open(fn, 'wb') #if you use python < 2.3 use this line: #pickle.dump(obj, f) pickle.dump(obj, f, 1) f.close() except IOError: LOG.error("error while saving stats cache file!") tdiff = time.time() - tstart fs = float(os.path.getsize(fn)) / float(1024) LOG.debug("stats updated in %0.5f seconds. File is %.2fKB big (%d lines)"%(tdiff, fs, len(obj)))
def sendjson(self, method, urlpath, obj=None): """Send json to the OpenDaylight controller.""" headers = {'Content-Type': 'application/json'} data = jsonutils.dumps(obj, indent=2) if obj else None url = '/'.join([self.url, urlpath]) LOG.debug("Sending METHOD (%(method)s) URL (%(url)s) JSON (%(obj)s)" % {'method': method, 'url': url, 'obj': obj}) r = requests.request(method, url=url, headers=headers, data=data, auth=self.auth, timeout=self.timeout) try: r.raise_for_status() except Exception as ex: LOG.error("Error Sending METHOD (%(method)s) URL (%(url)s)" "JSON (%(obj)s) return: %(r)s ex: %(ex)s rtext: " "%(rtext)s" % {'method': method, 'url': url, 'obj': obj, 'r': r, 'ex': ex, 'rtext': r.text}) return r try: return json.loads(r.content) except Exception: LOG.debug("%s" % r) return
def merge_opts_to_configs(opts): default_cfg_file = resource_string(__name__, "cfg.default.yaml") # read the default configuration file and possibly an override config file # the precedence order is as follows: # $HOME/.vmtp.yaml if exists # -c <file> from command line if provided # cfg.default.yaml config = config_loads(default_cfg_file) local_cfg = os.path.expanduser('~/.vmtp.yaml') if os.path.isfile(local_cfg): config = config_load(local_cfg, config) if opts.config: config = config_load(opts.config, config) if opts.show_config: print default_cfg_file sys.exit(0) if opts.version: print(__version__) sys.exit(0) config.debug = opts.debug config.stop_on_error = opts.stop_on_error config.keep_first_flow_and_exit = opts.keep_first_flow_and_exit config.inter_node_only = opts.inter_node_only config.same_network_only = opts.same_network_only if config.public_key_file and not os.path.isfile(config.public_key_file): LOG.warning('Invalid public_key_file:' + config.public_key_file) config.public_key_file = None if config.private_key_file and not os.path.isfile(config.private_key_file): LOG.warning('Invalid private_key_file:' + config.private_key_file) config.private_key_file = None # direct: use SR-IOV ports for all the test VMs if opts.vnic_type not in [None, 'direct', 'macvtap', 'normal']: LOG.error('Invalid vnic-type: ' + opts.vnic_type) sys.exit(1) config.vnic_type = opts.vnic_type config.hypervisors = opts.hypervisors if opts.availability_zone: config.availability_zone = opts.availability_zone # time to run each perf test in seconds if opts.time: config.time = int(opts.time) else: config.time = 10 if opts.json: config.json_file = opts.json else: config.json_file = None # Initialize the external host access config.ext_host = get_ssh_access('external-host', opts.ext_host, config) ################################################### # VM Image URL ################################################### if opts.vm_image_url: config.vm_image_url = opts.vm_image_url ################################################### # MongoDB Server connection info. ################################################### if opts.mongod_server: config.vmtp_mongod_ip = opts.mongod_server else: config.vmtp_mongod_ip = None if 'vmtp_mongod_port' not in config: # Set MongoDB default port if not set. config.vmtp_mongod_port = 27017 # the bandwidth limit for VMs if opts.vm_bandwidth: opts.vm_bandwidth = opts.vm_bandwidth.upper().strip() ex_unit = 'KMG'.find(opts.vm_bandwidth[-1]) try: if ex_unit == -1: raise ValueError val = int(opts.vm_bandwidth[0:-1]) except ValueError: LOG.error('Invalid --bandwidth parameter. A valid input must ' 'specify only one unit (K|M|G).') sys.exit(1) config.vm_bandwidth = int(val * (10 ** (ex_unit * 3))) # the pkt size for TCP, UDP and ICMP if opts.tcp_pkt_sizes: config.tcp_pkt_sizes = decode_size_list('--tcpbuf', opts.tcp_pkt_sizes) if opts.udp_pkt_sizes: config.udp_pkt_sizes = decode_size_list('--udpbuf', opts.udp_pkt_sizes) if opts.icmp_pkt_sizes: config.icmp_pkt_sizes = decode_size_list('--icmp_pkt_sizes', opts.icmp_pkt_sizes) if opts.reuse_network_name: config.reuse_network_name = opts.reuse_network_name if opts.os_dataplane_network: config.os_dataplane_network = opts.os_dataplane_network config.config_drive = opts.config_drive config.no_floatingip = opts.no_floatingip config.no_dhcp = opts.no_dhcp config.delete_image_after_run = opts.delete_image_after_run ##################################################### # Set Ganglia server ip and port if the monitoring (-m) # option is enabled. ##################################################### config.gmond_svr_ip = None config.gmond_svr_port = None if opts.monitor: # Add the default gmond port if not present if ':' not in opts.monitor: opts.monitor += ':8649' mobj = re.match(r'(\d+\.\d+\.\d+\.\d+):(\d+)', opts.monitor) if mobj: config.gmond_svr_ip = mobj.group(1) config.gmond_svr_port = mobj.group(2) LOG.info("Ganglia monitoring enabled (%s:%s)", config.gmond_svr_ip, config.gmond_svr_port) config.time = 30 else: LOG.warning('Invalid --monitor syntax: ' + opts.monitor) ################################################### # Once we parse the config files, normalize # the paths so that all paths are absolute paths. ################################################### normalize_paths(config) # Check the tp-tool name config.protocols = opts.protocols.upper() if 'M' in config.protocols or opts.multicast_addr: # nuttcp required for multicast opts.tp_tool = 'nuttcp' config.tp_tool = nuttcp_tool.NuttcpTool # If M provided, but not multicast_addr, use default (231.1.1.1) config.multicast_addr = opts.multicast_addr if opts.multicast_addr else "231.1.1.1" # If --multicast_addr provided, ensure 'M' is in protocols. if 'M' not in config.protocols: config.protocols += 'M' elif 'T' in config.protocols or 'U' in config.protocols: if opts.tp_tool.lower() == 'nuttcp': config.tp_tool = nuttcp_tool.NuttcpTool elif opts.tp_tool.lower() == 'iperf': config.tp_tool = iperf_tool.IperfTool else: LOG.warning('Invalid transport tool: ' + opts.tp_tool) sys.exit(1) else: config.tp_tool = None return config
def setup(self): # This is a template host access that will be used for all instances # (the only specific field specific to each instance is the host IP) # For test VM access, we never use password and always need a key pair self.instance_access = sshutils.SSHAccess() self.instance_access.username = self.config.ssh_vm_username # if the configuration does not have a # key pair specified, we check if the user has a personal key pair # if no key pair is configured or usable, a temporary key pair will be created if self.config.public_key_file and self.config.private_key_file: self.instance_access.public_key_file = self.config.public_key_file self.instance_access.private_key_file = self.config.private_key_file else: pub_key = os.path.expanduser('~/.ssh/id_rsa.pub') priv_key = os.path.expanduser('~/.ssh/id_rsa') if os.path.isfile(pub_key) and os.path.isfile(priv_key): self.instance_access.public_key_file = pub_key self.instance_access.private_key_file = priv_key else: LOG.error('Default keypair ~/.ssh/id_rsa[.pub] does not exist. Please ' 'either create one in your home directory, or specify your ' 'keypair information in the config file before running VMTP.') sys.exit(1) if self.config.debug and self.instance_access.public_key_file: LOG.info('VM public key: ' + self.instance_access.public_key_file) LOG.info('VM private key: ' + self.instance_access.private_key_file) # If we need to reuse existing vms just return without setup if not self.config.reuse_existing_vm: creds = self.cred.get_credentials() if self.cred.rc_identity_api_version == 3: auth = keystone_v3.Password(**creds) else: auth = keystone_v2.Password(**creds) sess = session.Session(auth=auth, verify=self.cred.rc_cacert) # Create the nova and neutron instances nova_client = novaclient.Client('2', session=sess) neutron = neutronclient.Client('2.0', session=sess) self.comp = compute.Compute(nova_client, self.config) # Add the appropriate public key to openstack self.comp.init_key_pair(self.config.public_key_name, self.instance_access) self.image_instance = self.comp.find_image(self.config.image_name) if self.image_instance is None: if self.config.vm_image_url != "": LOG.info('%s: image for VM not found, trying to upload it ...', self.config.image_name) keystoneclient.Client(self.cred.rc_identity_api_version, session=sess, auth_url=creds['auth_url']) self.glance_client = glanceclient.Client('1', session=sess) self.comp.upload_image_via_url( self.glance_client, self.config.image_name, self.config.vm_image_url) self.image_instance = self.comp.find_image(self.config.image_name) self.image_uploaded = True else: # Exit the pogram LOG.error('%s: image to launch VM not found. ABORTING.', self.config.image_name) sys.exit(1) self.assert_true(self.image_instance) LOG.info('Found image %s to launch VM, will continue', self.config.image_name) self.flavor_type = self.comp.find_flavor(self.config.flavor_type) self.net = network.Network(neutron, self.config) self.rescol.add_property('l2agent_type', self.net.l2agent_type) LOG.info("OpenStack agent: " + self.net.l2agent_type) try: network_type = self.net.vm_int_net[0]['provider:network_type'] LOG.info("OpenStack network type: " + network_type) self.rescol.add_property('encapsulation', network_type) except KeyError as exp: network_type = 'Unknown' LOG.info("Provider network type not found: ", str(exp)) # Create a new security group for the test self.sec_group = self.comp.security_group_create() if not self.sec_group: raise VmtpException("Security group creation failed") if self.config.reuse_existing_vm: self.server.internal_ip = self.config.vm_server_internal_ip self.client.internal_ip = self.config.vm_client_internal_ip if self.config.vm_server_external_ip: self.server.ssh_access.host = self.config.vm_server_external_ip else: self.server.ssh_access.host = self.config.vm_server_internal_ip if self.config.vm_client_external_ip: self.client.ssh_access.host = self.config.vm_client_external_ip else: self.client.ssh_access.host = self.config.vm_client_internal_ip return # this is the standard way of running the test # NICs to be used for the VM if self.config.reuse_network_name: # VM needs to connect to existing management and new data network # Reset the management network name int_net_name = list(self.config.internal_network_name) int_net_name[0] = self.config.reuse_network_name self.config.internal_network_name = int_net_name else: # Make sure we have an external network and an external router # self.assert_true(self.net.ext_net) # self.assert_true(self.net.ext_router) self.assert_true(self.net.vm_int_net) # Get hosts for the availability zone to use # avail_list = self.comp.list_hypervisor(config.availability_zone) avail_list = self.comp.get_az_host_list() if not avail_list: sys.exit(5) # compute the list of client vm placements to run # the first host is always where the server runs server_az = avail_list[0] if len(avail_list) > 1: # 2 hosts are known if self.config.inter_node_only: # in this case we do not want the client to run on the same host # as the server avail_list.pop(0) self.client_az_list = avail_list self.server = PerfInstance(self.config.vm_name_server, self.config, self.comp, self.net, server=True) self.server.display('Creating server VM...') self.create_instance(self.server, server_az, self.net.vm_int_net[0])
def __init__(self, openrc_file, pwd, no_env): self.rc_password = None self.rc_username = None self.rc_tenant_name = None self.rc_auth_url = None self.rc_cacert = False self.rc_region_name = None self.rc_project_name = None self.rc_project_domain_id = None self.rc_user_domain_id = None self.rc_identity_api_version = 2 success = True if openrc_file: if os.path.exists(openrc_file): export_re = re.compile('export OS_([A-Z_]*)="?(.*)') for line in open(openrc_file): mstr = export_re.match(line.strip()) if mstr: # get rif of posible trailing double quote # the first one was removed by the re name, value = mstr.group(1), mstr.group(2) if value.endswith('"'): value = value[:-1] # get rid of password assignment # echo "Please enter your OpenStack Password: "******"CACERT": self.rc_cacert = value elif name == "REGION_NAME": self.rc_region_name = value elif name == "PASSWORD" and not pwd: pwd = value elif name == "PROJECT_NAME": self.rc_project_name = value elif name == "PROJECT_DOMAIN_ID" or name == "PROJECT_DOMAIN_NAME": self.rc_project_domain_id = value elif name == "USER_DOMAIN_ID" or name == "USER_DOMAIN_ID": self.rc_user_domain_id = value else: LOG.error('Error: rc file does not exist %s', openrc_file) success = False elif not no_env: # no openrc file passed - we assume the variables have been # sourced by the calling shell # just check that they are present if 'OS_IDENTITY_API_VERSION' in os.environ: self.rc_identity_api_version = int(os.environ['OS_IDENTITY_API_VERSION']) if self.rc_identity_api_version == 2: for varname in ['OS_USERNAME', 'OS_AUTH_URL', 'OS_TENANT_NAME']: if varname not in os.environ: LOG.warning('%s is missing', varname) success = False if success: self.rc_username = os.environ['OS_USERNAME'] self.rc_auth_url = os.environ['OS_AUTH_URL'] self.rc_tenant_name = os.environ['OS_TENANT_NAME'] if 'OS_REGION_NAME' in os.environ: self.rc_region_name = os.environ['OS_REGION_NAME'] elif self.rc_identity_api_version == 3: for varname in ['OS_USERNAME', 'OS_AUTH_URL', 'OS_PROJECT_NAME', 'OS_PROJECT_DOMAIN_ID', 'OS_USER_DOMAIN_ID']: if varname not in os.environ: LOG.warning('%s is missing', varname) success = False if success: self.rc_username = os.environ['OS_USERNAME'] self.rc_auth_url = os.environ['OS_AUTH_URL'] self.rc_project_name = os.environ['OS_PROJECT_NAME'] self.rc_project_domain_id = os.environ['OS_PROJECT_DOMAIN_ID'] self.rc_user_domain_id = os.environ['OS_USER_DOMAIN_ID'] if 'OS_CACERT' in os.environ: self.rc_cacert = os.environ['OS_CACERT'] # always override with CLI argument if provided if pwd: self.rc_password = pwd # if password not know, check from env variable elif self.rc_auth_url and not self.rc_password and success: if 'OS_PASSWORD' in os.environ and not no_env: self.rc_password = os.environ['OS_PASSWORD'] else: # interactively ask for password self.rc_password = getpass.getpass( 'Please enter your OpenStack Password: ') if not self.rc_password: self.rc_password = ""