def vnc_process(self): while True: try: event = self.q.get() event_type = event['type'] kind = event['object'].get('kind') metadata = event['object']['metadata'] namespace = metadata.get('namespace') name = metadata.get('name') uid = metadata.get('uid') if kind == 'Pod': self.pod_mgr.process(event) elif kind == 'Service': self.service_mgr.process(event) elif kind == 'Namespace': self.namespace_mgr.process(event) elif kind == 'NetworkPolicy': self.network_policy_mgr.process(event) elif kind == 'Endpoints': self.endpoints_mgr.process(event) elif kind == 'Ingress': self.ingress_mgr.process(event) else: print("%s - Event %s %s %s:%s:%s not handled" %(self._name, event_type, kind, namespace, name, uid)) self.logger.error("%s - Event %s %s %s:%s:%s not handled" %(self._name, event_type, kind, namespace, name, uid)) except Empty: gevent.sleep(0) except Exception as e: string_buf = StringIO() cgitb_hook(file=string_buf, format="text") err_msg = string_buf.getvalue() self.logger.error("%s - %s" %(self._name, err_msg))
def _vnc_subscribe_callback(self, oper_info): self._db_resync_done.wait() try: self.oper_info = oper_info self.vnc_subscribe_actions() except Exception: string_buf = cStringIO.StringIO() cgitb_hook(file=string_buf, format="text") self.logger.error(string_buf.getvalue()) self.msgbus_store_err_msg(string_buf.getvalue()) try: with open(self._args.trace_file, 'a') as err_file: err_file.write(string_buf.getvalue()) except IOError: pass finally: try: self.msgbus_trace_msg() except Exception: pass del self.oper_info del self.obj_type del self.obj_class del self.obj del self.dependency_tracker
def process(self): """Process available events.""" if not self.kube_api_stream_handle: self.logger.error("%s - Event handler not found. " "Cannot process its events." % self.name) return resp = self.kube_api_resp fp = resp.raw._fp.fp if fp is None: self.register_monitor() return try: line = next(self.kube_api_stream_handle) if not line: return except StopIteration: return except requests.exceptions.ChunkedEncodingError as e: self.logger.error("%s - %s" % (self.name, e)) return try: self.process_event(json.loads(line)) except ValueError: self.logger.error( "Invalid JSON data from response stream:%s" % line) except Exception as e: string_buf = StringIO() cgitb_hook(file=string_buf, format="text") err_msg = string_buf.getvalue() self.logger.error("%s - %s" % (self.name, err_msg))
def _allocate_floating_ip(lb, vmi, fip_pool, external_ip=None): fip_obj = FloatingIp(lb.name + str(external_ip) + "-externalIP", fip_pool) fip_obj.set_virtual_machine_interface(vmi_obj) if external_ip: if not(_check_ip_with_fip_pool(external_ip, fip_pool)): err_str = "external_ip " + external_ip + " not in fip_pool subnet" self.logger.error(err_str) return None fip_obj.set_floating_ip_address(external_ip) project = self._vnc_lib.project_read(id=lb.parent_uuid) fip_obj.set_project(project) try: self._vnc_lib.floating_ip_create(fip_obj) except RefsExistError as e: string_buf = StringIO() cgitb_hook(file=string_buf, format="text") err_msg = string_buf.getvalue() self.logger.error("%s" %(err_msg)) except: string_buf = StringIO() cgitb_hook(file=string_buf, format="text") err_msg = string_buf.getvalue() self.logger.error("%s" %(err_msg)) fip = FloatingIpKM.locate(fip_obj.uuid) self.logger.notice("floating ip allocated : %s for Service (%s)" % (fip.address, service_id)) return(fip.address)
def _link_vm_to_node(self, vm_obj, pod_node, node_ip): if node_ip is None: return vm = VirtualMachineKM.locate(vm_obj.uuid) if vm: vm.node_ip = node_ip vr_uuid = VirtualRouterKM.get_ip_addr_to_uuid(node_ip) if vr_uuid is None: self._logger.debug("%s - Vrouter %s Not Found for Pod %s" %(self._name, node_ip, vm_obj.uuid)) return try: vrouter_obj = self._vnc_lib.virtual_router_read(id=vr_uuid) except Exception as e: self._logger.debug("%s - Vrouter %s Not Found for Pod %s" %(self._name, node_ip, vm_obj.uuid)) string_buf = StringIO() cgitb_hook(file=string_buf, format="text") err_msg = string_buf.getvalue() self._logger.error("_link_vm_to_node: %s - %s" %(self._name, err_msg)) return self._vnc_lib.ref_update('virtual-router', vrouter_obj.uuid, 'virtual-machine', vm_obj.uuid, None, 'ADD') if vm: vm.virtual_router = vr_obj.uuid
def plugin_get_virtual_router(self, context, virtual_router): try: cfgdb = self._get_user_cfgdb(context) vr_info = cfgdb.virtual_router_read(virtual_router['id']) return vr_info except Exception as e: cgitb_hook(format="text") raise
def _add_detailed_traceback(self, exc_info): vnc_cgitb.enable(format='text') from cStringIO import StringIO tmp_file = StringIO() cgitb_hook(format="text", file=tmp_file, info=exc_info) tb_str = tmp_file.getvalue() tmp_file.close() self.addDetail('detailed-traceback', content.text_content(tb_str))
def plugin_create_sec_group_rule(self, context, sg_rule): """ Security group rule create request """ try: cfgdb = self._get_user_cfgdb(context) sg_rule_info = cfgdb.security_group_rule_create(sg_rule['resource']) return sg_rule_info except Exception as e: cgitb_hook(format="text") raise
def plugin_get_router(self, context, router): """ Router get request """ try: cfgdb = self._get_user_cfgdb(context) router_info = cfgdb.router_read(router['id']) return router_info except Exception as e: cgitb_hook(format="text") raise
def plugin_create_policy(self, context, policy): """ Policy create request """ try: cfgdb = self._get_user_cfgdb(context) pol_info = cfgdb.policy_create(policy['resource']) return pol_info except Exception as e: cgitb_hook(format="text") raise
def plugin_delete_sec_group(self, context, sg): """ Security group delete request """ try: cfgdb = self._get_user_cfgdb(context) cfgdb.security_group_delete(context, sg['id']) LOG.debug("plugin_delete_sec_group(): " + pformat(sg['id'])) except Exception as e: cgitb_hook(format="text") raise
def plugin_delete_router(self, context, router): """ Router delete request """ try: cfgdb = self._get_user_cfgdb(context) cfgdb.router_delete(router['id']) LOG.debug("plugin_delete_router(): " + pformat(router['id'])) except Exception as e: cgitb_hook(format="text") raise
def log_exception(self): string_buf = cStringIO.StringIO() cgitb_hook(file=string_buf, format="text") self.logger.error(string_buf.getvalue()) self.msgbus_store_err_msg(string_buf.getvalue()) if not self._trace_file: return try: with open(self._trace_file, 'a') as err_file: err_file.write(string_buf.getvalue()) except IOError: pass
def plugin_create_port(self, context, port): """ Port create request """ try: cfgdb = self._get_user_cfgdb(context) net_info = cfgdb.port_create(context, port['resource']) return net_info except Exception as e: cgitb_hook(format="text") raise
def plugin_create_ipam(self, context, ipam): """ IPAM create request """ try: cfgdb = self._get_user_cfgdb(context) ipam_info = cfgdb.ipam_create(ipam['resource']) return ipam_info except Exception as e: cgitb_hook(format="text") raise
def plugin_create_subnet(self, context, subnet): """ Subnet create request """ try: cfgdb = self._get_user_cfgdb(context) subnet_info = cfgdb.subnet_create(subnet['resource']) return self._make_subnet_dict(subnet_info) except Exception as e: cgitb_hook(format="text") raise
def plugin_delete_subnet(self, context, subnet): """ Subnet delete request """ try: cfgdb = self._get_user_cfgdb(context) cfgdb.subnet_delete(subnet['id']) LOG.debug("plugin_delete_subnet(): " + pformat(subnet['id'])) except Exception as e: cgitb_hook(format="text") raise
def plugin_delete_network(self, context, network): """ Network delete request """ try: cfgdb = self._get_user_cfgdb(context) cfgdb.network_delete(network['id']) LOG.debug("plugin_delete_network(): " + pformat(network['id'])) except Exception as e: cgitb_hook(format="text") raise
def plugin_create_network(self, context, network): """ Network create request """ try: cfgdb = self._get_user_cfgdb(context) net_info = cfgdb.network_create(network['resource']) return net_info except Exception as e: cgitb_hook(format="text") raise
def plugin_create_svc_instance(self, context, svc_instance): """ Service instance create request """ try: cfgdb = self._get_user_cfgdb(context) si_info = cfgdb.svc_instance_create(svc_instance['resource']) return si_info except Exception as e: cgitb_hook(format="text") raise
def plugin_create_router(self, context, router): """ Router create request """ try: cfgdb = self._get_user_cfgdb(context) router_info = cfgdb.router_create(router['resource']) return router_info except Exception as e: cgitb_hook(format="text") raise
def plugin_delete_ipam(self, context, ipam): """ IPAM delete request """ try: cfgdb = self._get_user_cfgdb(context) cfgdb.ipam_delete(ipam['id']) LOG.debug("plugin_delete_ipam(): " + pformat(ipam['id'])) except Exception as e: cgitb_hook(format="text") raise
def plugin_get_ipam(self, context, ipam): """ IPAM get request """ try: cfgdb = self._get_user_cfgdb(context) ipam_info = cfgdb.ipam_read(ipam['id']) return ipam_info except Exception as e: cgitb_hook(format="text") raise
def plugin_get_floatingip(self, context, floatingip): """ Floating IP get request """ try: cfgdb = self._get_user_cfgdb(context) fip_info = cfgdb.floatingip_read(floatingip['id']) return fip_info except Exception as e: cgitb_hook(format="text") raise
def plugin_get_policy(self, context, policy): """ Policy get request """ try: cfgdb = self._get_user_cfgdb(context) pol_info = cfgdb.policy_read(policy['id']) return pol_info except Exception as e: cgitb_hook(format="text") raise
def plugin_create_floatingip(self, context, floatingip): """ Floating IP create request """ try: cfgdb = self._get_user_cfgdb(context) net_info = cfgdb.floatingip_create(context, floatingip['resource']) return net_info except Exception as e: cgitb_hook(format="text") raise
def vnc_timer(self): try: self.network_policy_mgr.network_policy_timer() self.ingress_mgr.ingress_timer() self.service_mgr.service_timer() self.pod_mgr.pod_timer() self.namespace_mgr.namespace_timer() except Exception as e: string_buf = StringIO() cgitb_hook(file=string_buf, format="text") err_msg = string_buf.getvalue() self.logger.error("vnc_timer: %s - %s" %(self._name, err_msg))
def plugin_get_sec_group(self, context, sg): """ Security group get request """ try: cfgdb = self._get_user_cfgdb(context) sg_info = cfgdb.security_group_read(sg['id']) return sg_info except Exception as e: cgitb_hook(format="text") raise
def plugin_delete_port(self, context, port): """ Port delete request """ try: cfgdb = self._get_user_cfgdb(context) cfgdb.port_delete(port['id']) LOG.debug("plugin_delete_port(): " + pformat(port['id'])) except Exception as e: cgitb_hook(format="text") raise
def vnc_timer(self): try: self.network_policy_mgr.network_policy_timer() self.ingress_mgr.ingress_timer() self.service_mgr.service_timer() self.pod_mgr.pod_timer() self.namespace_mgr.namespace_timer() except Exception as e: string_buf = StringIO() cgitb_hook(file=string_buf, format="text") err_msg = string_buf.getvalue() self.logger.error("vnc_timer: %s - %s" % (self._name, err_msg))
def plugin_get_port(self, context, port): """ Port get request """ try: cfgdb = self._get_user_cfgdb(context) port_info = cfgdb.port_read(port['id']) return port_info except Exception as e: cgitb_hook(format="text") raise
def plugin_delete_floatingip(self, context, floatingip): """ Floating IP delete request """ try: cfgdb = self._get_user_cfgdb(context) cfgdb.floatingip_delete(floatingip['id']) LOG.debug("plugin_delete_floatingip(): " + pformat(floatingip['id'])) except Exception as e: cgitb_hook(format="text") raise
def handler_trap_exception(*args, **kwargs): try: response = handler(*args, **kwargs) return response except Exception as e: # don't log details of bottle.abort i.e handled error cases if not isinstance(e, bottle.HTTPError): string_buf = StringIO() cgitb_hook(file=string_buf, format="text",) err_msg = string_buf.getvalue() self._logger.error(err_msg) raise
def plugin_update_route_table(self, context, route_table): """ Route table update request """ try: cfgdb = self._get_user_cfgdb(context) rt_info = cfgdb.route_table_update(route_table['id'], route_table['resource']) return rt_info except Exception as e: cgitb_hook(format="text") raise
def plugin_delete_svc_instance(self, context, svc_instance): """ Service instance delete request """ try: cfgdb = self._get_user_cfgdb(context) cfgdb.svc_instance_delete(svc_instance['id']) LOG.debug("plugin_delete_svc_instance(): " + pformat(svc_instance['id'])) except Exception as e: cgitb_hook(format="text") raise
def _get_floating_ip(self, name, ns_name, proj_obj, external_ip=None, vmi_obj=None, specified_fip_pool_fq_name_str=None): fip_pool_fq_name = None if specified_fip_pool_fq_name_str != None: fip_pool_fq_name = get_fip_pool_fq_name_from_dict_string(specified_fip_pool_fq_name_str) if fip_pool_fq_name is None: ns = self._get_namespace(ns_name) fip_pool_fq_name = ns.get_annotated_ns_fip_pool_fq_name() if fip_pool_fq_name is None: if not vnc_kube_config.is_public_fip_pool_configured(): return None try: fip_pool_fq_name = get_fip_pool_fq_name_from_dict_string( self._args.public_fip_pool) except Exception as e: string_buf = StringIO() cgitb_hook(file=string_buf, format="text") err_msg = string_buf.getvalue() self._logger.error("%s - %s" %(self._name, err_msg)) return None if vmi_obj: fip_refs = vmi_obj.get_floating_ip_back_refs() for ref in fip_refs or []: fip = FloatingIpKM.get(ref['uuid']) if fip and fip.fq_name[:-1] == fip_pool_fq_name: return fip else: break fip_pool = self._get_fip_pool_obj(fip_pool_fq_name) if fip_pool is None: return None fip_uuid = str(uuid.uuid4()) fip_name = VncCommon.make_name(name, fip_uuid) fip_obj = FloatingIp(fip_name, fip_pool) fip_obj.uuid = fip_uuid fip_obj.set_project(proj_obj) if vmi_obj: fip_obj.set_virtual_machine_interface(vmi_obj) if external_ip: fip_obj.floating_ip_address = external_ip try: self._vnc_lib.floating_ip_create(fip_obj) fip = FloatingIpKM.locate(fip_obj.uuid) except Exception as e: string_buf = StringIO() cgitb_hook(file=string_buf, format="text") err_msg = string_buf.getvalue() self._logger.error("%s - %s" %(self._name, err_msg)) return None return fip
def plugin_get_sec_group_rules(self, context, sg_rule): """ Security group rules get request """ filters = sg_rule['filters'] try: cfgdb = self._get_user_cfgdb(context) sg_rules_info = cfgdb.security_group_rule_list(context, filters) return json.dumps(sg_rules_info) except Exception as e: cgitb_hook(format="text") raise
def plugin_get_routers(self, context, router): """ Routers get request """ filters = router['filters'] try: cfgdb = self._get_user_cfgdb(context) routers_info = cfgdb.router_list(context, filters) return json.dumps(routers_info) except Exception as e: cgitb_hook(format="text") raise
def plugin_get_ipams(self, context, ipam): """ IPAM get request """ filters = ipam['filters'] try: cfgdb = self._get_user_cfgdb(context) ipams_info = cfgdb.ipam_list(context, filters) return json.dumps(ipams_info) except Exception as e: cgitb_hook(format="text") raise