def __init__(self, maxpolicies): """ Create a management class for managing the system's policies. @param maxpolicies: The max. number of policies allowed on the system (currently '1') """ self.maxpolicies = maxpolicies self.policies = {} self.xsobjs = {} act_pol_name = self.get_hv_loaded_policy_name() initialize() ref = uuid.createString() try: self.xsobjs[ref] = ACMPolicy(name=act_pol_name, ref=ref) self.policies[ref] = (act_pol_name, xsconstants.ACM_POLICY_ID) except Exception, e: log.error("Could not find XML representation of policy '%s': " "%s" % (act_pol_name,e)) rc, errors, acmpol_def = ACMPolicy.force_default_policy() if rc == xsconstants.XSERR_SUCCESS: self.xsobjs[ref] = acmpol_def self.policies[ref] = (acmpol_def.get_name(), xsconstants.ACM_POLICY_ID) log.info("Switched to DEFAULT policy.")
def __init__(self, maxpolicies): """ Create a management class for managing the system's policies. @param maxpolicies: The max. number of policies allowed on the system (currently '1') """ self.maxpolicies = maxpolicies self.policies = {} self.xsobjs = {} act_pol_name = self.get_hv_loaded_policy_name() initialize() ref = uuid.createString() try: self.xsobjs[ref] = ACMPolicy(name=act_pol_name, ref=ref) self.policies[ref] = (act_pol_name, xsconstants.ACM_POLICY_ID) except Exception, e: log.error("Could not find XML representation of policy '%s': " "%s" % (act_pol_name, e)) rc, errors, acmpol_def = ACMPolicy.force_default_policy() if rc == xsconstants.XSERR_SUCCESS: self.xsobjs[ref] = acmpol_def self.policies[ref] = (acmpol_def.get_name(), xsconstants.ACM_POLICY_ID) log.info("Switched to DEFAULT policy.")
def recreate_active_pools(cls): """ Read active pool config from hypervisor and create pool instances. - Query pool ids and assigned CPUs from hypervisor. - Query additional information for any pool from xenstore. If an entry for a pool id is missing in xenstore, it will be recreated with a new uuid and generic name (this is an error case) - Create an XendCPUPool instance for any pool id Function have to be called after recreation of managed pools. """ log.debug('recreate_active_pools') for pool_rec in xc.cpupool_getinfo(): pool = pool_rec['cpupool'] # read pool data from xenstore path = XS_POOLROOT + "%s/" % pool uuid = xstransact.Read(path, 'uuid') if not uuid: # xenstore entry missing / invaild; create entry with new uuid uuid = genuuid.createString() name = "Pool-%s" % pool try: inst = XendCPUPool({'name_label': name}, uuid, False) inst.update_XS(pool) except PoolError, ex: # log error and skip domain log.error('cannot recreate pool %s; skipping (reason: %s)' \ % (name, ex)) else: (name, descr) = xstransact.Read(path, 'name', 'description') other_config = {} for key in xstransact.List(path + 'other_config'): other_config[key] = xstransact.Read(path + 'other_config/%s' % key) # check existance of pool instance inst = XendAPIStore.get(uuid, cls.getClass()) if inst: # update attributes of existing instance inst.name_label = name inst.name_description = descr inst.other_config = other_config else: # recreate instance try: inst = XendCPUPool( { 'name_label': name, 'name_description': descr, 'other_config': other_config, 'proposed_CPUs': pool_rec['cpulist'], 'ncpu': len(pool_rec['cpulist']), }, uuid, False) except PoolError, ex: # log error and skip domain log.error( 'cannot recreate pool %s; skipping (reason: %s)' \ % (name, ex))
def _loadConfig(servers, root, reload): if xoptions.get_xend_http_server(): servers.add(HttpServer(root, xoptions.get_xend_address(), xoptions.get_xend_port())) if xoptions.get_xend_unix_server(): path = xoptions.get_xend_unix_path() log.info('unix path=' + path) servers.add(UnixHttpServer(root, path)) api_cfg = xoptions.get_xen_api_server() if api_cfg: try: for server_cfg in api_cfg: # Parse the xen-api-server config ssl_key_file = None ssl_cert_file = None auth_method = XendAPI.AUTH_NONE hosts_allowed = None host_addr = server_cfg[0].split(':', 1) if len(host_addr) == 1: if host_addr[0].lower() == 'unix': use_tcp = False host = 'localhost' port = 0 else: use_tcp = True host = '' port = int(host_addr[0]) else: use_tcp = True host = str(host_addr[0]) port = int(host_addr[1]) if len(server_cfg) > 1: if server_cfg[1] in [XendAPI.AUTH_PAM, XendAPI.AUTH_NONE]: auth_method = server_cfg[1] if len(server_cfg) > 2 and len(server_cfg[2]): hosts_allowed = map(re.compile, server_cfg[2].split(' ')) if len(server_cfg) > 4: # SSL key and cert file ssl_key_file = server_cfg[3] ssl_cert_file = server_cfg[4] servers.add(XMLRPCServer(auth_method, True, use_tcp = use_tcp, ssl_key_file = ssl_key_file, ssl_cert_file = ssl_cert_file, host = host, port = port, path = XEN_API_SOCKET, hosts_allowed = hosts_allowed)) except (ValueError, TypeError), exn: log.exception('Xen API Server init failed') log.error('Xen-API server configuration %s is invalid.', api_cfg)
def op_receive(self, name, _): if self.transport: self.send_reply(["ready", name]) XendDomain.instance().domain_restore_fd( self.transport.sock.fileno()) else: log.error(name + ": no transport") raise XendError(name + ": no transport")
def recreate_active_pools(cls): """ Read active pool config from hypervisor and create pool instances. - Query pool ids and assigned CPUs from hypervisor. - Query additional information for any pool from xenstore. If an entry for a pool id is missing in xenstore, it will be recreated with a new uuid and generic name (this is an error case) - Create an XendCPUPool instance for any pool id Function have to be called after recreation of managed pools. """ log.debug('recreate_active_pools') for pool_rec in xc.cpupool_getinfo(): pool = pool_rec['cpupool'] # read pool data from xenstore path = XS_POOLROOT + "%s/" % pool uuid = xstransact.Read(path, 'uuid') if not uuid: # xenstore entry missing / invaild; create entry with new uuid uuid = genuuid.createString() name = "Pool-%s" % pool try: inst = XendCPUPool( { 'name_label' : name }, uuid, False ) inst.update_XS(pool) except PoolError, ex: # log error and skip domain log.error('cannot recreate pool %s; skipping (reason: %s)' \ % (name, ex)) else: (name, descr) = xstransact.Read(path, 'name', 'description') other_config = {} for key in xstransact.List(path + 'other_config'): other_config[key] = xstransact.Read( path + 'other_config/%s' % key) # check existance of pool instance inst = XendAPIStore.get(uuid, cls.getClass()) if inst: # update attributes of existing instance inst.name_label = name inst.name_description = descr inst.other_config = other_config else: # recreate instance try: inst = XendCPUPool( { 'name_label' : name, 'name_description' : descr, 'other_config' : other_config, 'proposed_CPUs' : pool_rec['cpulist'], 'ncpu' : len(pool_rec['cpulist']), }, uuid, False ) except PoolError, ex: # log error and skip domain log.error( 'cannot recreate pool %s; skipping (reason: %s)' \ % (name, ex))
def read_exact(fd, size, errmsg): buf = "" while size != 0: readstr = os.read(fd, size) if not len(readstr): log.error("read_exact: EOF trying to read %d (buf='%s')" % (size, buf)) raise XendError(errmsg) size = size - len(readstr) buf = buf + readstr return buf
def read_exact(fd, size, errmsg): buf = '' while size != 0: readstr = os.read(fd, size) if not len(readstr): log.error("read_exact: EOF trying to read %d (buf='%s')" % \ (size, buf)) raise XendError(errmsg) size = size - len(readstr) buf = buf + readstr return buf
def slurp(infile): while 1: line = infile.readline() if line == "": break else: line = line.strip() m = re.match(r"^ERROR: (.*)", line) if m is None: log.info('%s', line) else: log.error('%s', m.group(1))
def op_receive(self, name, _): if self.transport: self.send_reply(["ready", name]) try: XendDomain.instance().domain_restore_fd( self.transport.sock.fileno(), relocating=True) except: self.send_error() self.close() else: log.error(name + ": no transport") raise XendError(name + ": no transport")
def run(self): authmsg = (self.auth == XendAPI.AUTH_NONE and "; authentication has been disabled for this server." or ".") try: if self.use_tcp: using_ssl = self.ssl_key_file and self.ssl_cert_file log.info("Opening %s XML-RPC server on %s%d%s", using_ssl and 'HTTPS' or 'TCP', self.host and '%s:' % self.host or 'all interfaces, port ', self.port, authmsg) # #sync vms and hosts' status send message # from xen.xend import RunSend # RunSend.main() # # #the follow file was import by shixisheng_cxx # from xen.xend import P_DataCollect # P_DataCollect.main() if using_ssl: if not ssl_enabled: raise ValueError("pyOpenSSL not installed. " "Unable to start HTTPS XML-RPC server") self.server = SSLXMLRPCServer( (self.host, self.port), self.hosts_allowed, self.xenapi is not None, logRequests = False, ssl_key_file = self.ssl_key_file, ssl_cert_file = self.ssl_cert_file) else: self.server = TCPXMLRPCServer( (self.host, self.port), self.hosts_allowed, self.xenapi is not None, logRequests = False) else: log.info("Opening Unix domain socket XML-RPC server on %s%s", self.path, authmsg) self.server = UnixXMLRPCServer(self.path, self.hosts_allowed, self.xenapi is not None, logRequests = False) except socket.error, exn: log.error('Cannot start server: %s!', exn.args[1]) ready = True running = False return
def op_sslreceive(self, name, _): if self.transport: self.send_reply(["ready", name]) p2cread, p2cwrite = os.pipe() threading.Thread(target=connection.SSLSocketServerConnection.recv2fd, args=(self.transport.sock, p2cwrite)).start() try: XendDomain.instance().domain_restore_fd(p2cread, relocating=True) except: os.close(p2cread) os.close(p2cwrite) self.send_error() self.close() else: log.error(name + ": no transport") raise XendError(name + ": no transport")
def run(self): authmsg = (self.auth == XendAPI.AUTH_NONE and "; authentication has been disabled for this server." or ".") try: if self.use_tcp: using_ssl = self.ssl_key_file and self.ssl_cert_file log.info( "Opening %s XML-RPC server on %s%d%s", using_ssl and 'HTTPS' or 'TCP', self.host and '%s:' % self.host or 'all interfaces, port ', self.port, authmsg) if using_ssl: if not ssl_enabled: raise ValueError( "pyOpenSSL not installed. " "Unable to start HTTPS XML-RPC server") self.server = SSLXMLRPCServer( (self.host, self.port), self.hosts_allowed, self.xenapi is not None, logRequests=False, ssl_key_file=self.ssl_key_file, ssl_cert_file=self.ssl_cert_file) else: self.server = TCPXMLRPCServer((self.host, self.port), self.hosts_allowed, self.xenapi is not None, logRequests=False) else: log.info("Opening Unix domain socket XML-RPC server on %s%s", self.path, authmsg) self.server = UnixXMLRPCServer(self.path, self.hosts_allowed, self.xenapi is not None, logRequests=False) except socket.error, exn: log.error('Cannot start server: %s!', exn.args[1]) ready = True running = False return
def op_sslreceive(self, name, _): if self.transport: self.send_reply(["ready", name]) p2cread, p2cwrite = os.pipe() threading.Thread( target=connection.SSLSocketServerConnection.recv2fd, args=(self.transport.sock, p2cwrite)).start() try: XendDomain.instance().domain_restore_fd(p2cread, relocating=True) except: os.close(p2cread) os.close(p2cwrite) self.send_error() self.close() else: log.error(name + ": no transport") raise XendError(name + ": no transport")
def __acm_init(self): act_pol_name = self.get_hv_loaded_policy_name() initialize() ref = uuid.createString() try: self.xsobjs[ref] = ACMPolicy(name=act_pol_name, ref=ref) self.policies[ref] = (act_pol_name, xsconstants.ACM_POLICY_ID) self.xsobjs[ref].validate_enforced_policy_hash() except Exception, e: log.error("Could not find XML representation of policy '%s': " "%s" % (act_pol_name, e)) rc, errors, acmpol_def = ACMPolicy.force_default_policy(ref) if rc == xsconstants.XSERR_SUCCESS: self.xsobjs[ref] = acmpol_def self.policies[ref] = (acmpol_def.get_name(), xsconstants.ACM_POLICY_ID) log.info("Switched to DEFAULT policy.")
def __acm_init(self): act_pol_name = self.get_hv_loaded_policy_name() initialize() ref = uuid.createString() try: self.xsobjs[ref] = ACMPolicy(name=act_pol_name, ref=ref) self.policies[ref] = (act_pol_name, xsconstants.ACM_POLICY_ID) self.xsobjs[ref].validate_enforced_policy_hash() except Exception, e: log.error("Could not find XML representation of policy '%s': " "%s" % (act_pol_name,e)) rc, errors, acmpol_def = ACMPolicy.force_default_policy(ref) if rc == xsconstants.XSERR_SUCCESS: self.xsobjs[ref] = acmpol_def self.policies[ref] = (acmpol_def.get_name(), xsconstants.ACM_POLICY_ID) log.info("Switched to DEFAULT policy.")
def run(self): authmsg = self.auth == XendAPI.AUTH_NONE and "; authentication has been disabled for this server." or "." try: if self.use_tcp: using_ssl = self.ssl_key_file and self.ssl_cert_file log.info( "Opening %s XML-RPC server on %s%d%s", using_ssl and "HTTPS" or "TCP", self.host and "%s:" % self.host or "all interfaces, port ", self.port, authmsg, ) if using_ssl: if not ssl_enabled: raise ValueError("pyOpenSSL not installed. " "Unable to start HTTPS XML-RPC server") self.server = SSLXMLRPCServer( (self.host, self.port), self.hosts_allowed, self.xenapi is not None, logRequests=False, ssl_key_file=self.ssl_key_file, ssl_cert_file=self.ssl_cert_file, ) else: self.server = TCPXMLRPCServer( (self.host, self.port), self.hosts_allowed, self.xenapi is not None, logRequests=False ) else: log.info("Opening Unix domain socket XML-RPC server on %s%s", self.path, authmsg) self.server = UnixXMLRPCServer( self.path, self.hosts_allowed, self.xenapi is not None, logRequests=False ) except socket.error, exn: log.error("Cannot start server: %s!", exn.args[1]) ready = True running = False return
def start(self, status): # Running the network script will spawn another process, which takes # the status fd with it unless we set FD_CLOEXEC. Failing to do this # causes the read in SrvDaemon to hang even when we have written here. if status: fcntl.fcntl(status, fcntl.F_SETFD, fcntl.FD_CLOEXEC) # Prepare to catch SIGTERM (received when 'xend stop' is executed) # and call each server's cleanup if possible signal.signal(signal.SIGTERM, self.cleanup) signal.signal(signal.SIGHUP, self.reloadConfig) while True: threads = [] for server in self.servers: if server.ready: continue thread = Thread(target=server.run, name=server.__class__.__name__) thread.setDaemon(True) thread.start() threads.append(thread) # check for when all threads have initialized themselves and then # close the status pipe retryCount = 0 threads_left = True while threads_left: threads_left = False for server in self.servers: if not server.ready: threads_left = True break if threads_left: time.sleep(.5) retryCount += 1 if retryCount > 60: for server in self.servers: if not server.ready: log.error("Server " + server.__class__.__name__ + " did not initialise!") break if status: status.write('0') status.close() status = None # auto start pools before domains are started try: XendCPUPool.autostart_pools() except Exception, e: log.exception("Failed while autostarting pools") # Reaching this point means we can auto start domains try: xenddomain().autostart_domains() except Exception, e: log.exception("Failed while autostarting domains")
def start(self, status): # Running the network script will spawn another process, which takes # the status fd with it unless we set FD_CLOEXEC. Failing to do this # causes the read in SrvDaemon to hang even when we have written here. if status: fcntl.fcntl(status, fcntl.F_SETFD, fcntl.FD_CLOEXEC) # Prepare to catch SIGTERM (received when 'xend stop' is executed) # and call each server's cleanup if possible signal.signal(signal.SIGTERM, self.cleanup) signal.signal(signal.SIGHUP, self.reloadConfig) while True: threads = [] for server in self.servers: if server.ready: continue thread = Thread(target=server.run, name=server.__class__.__name__) thread.setDaemon(True) thread.start() threads.append(thread) # check for when all threads have initialized themselves and then # close the status pipe retryCount = 0 threads_left = True while threads_left: threads_left = False for server in self.servers: if not server.ready: threads_left = True break if threads_left: time.sleep(.5) retryCount += 1 if retryCount > 60: for server in self.servers: if not server.ready: log.error("Server " + server.__class__.__name__ + " did not initialise!") break if status: status.write('0') status.close() status = None # Reaching this point means we can auto start domains try: xenddomain().autostart_domains() except Exception, e: log.exception("Failed while autostarting domains") # loop to keep main thread alive until it receives a SIGTERM self.running = True while self.running: time.sleep(100000000) if self.reloadingConfig: log.info("Restarting all XML-RPC and Xen-API servers...") self.cleaningUp = False self.reloadingConfig = False xoptions.set_config() self.servers = [] _loadConfig(self, self.root, True) else: break
def get_info_from_sysfs(self): try: self.find_capability(0x11) except PciDeviceParseError, err: log.error("Caught '%s'" % err) return False
def _loadConfig(servers, root, reload): if xoptions.get_xend_http_server(): servers.add( HttpServer(root, xoptions.get_xend_address(), xoptions.get_xend_port())) if xoptions.get_xend_unix_server(): path = xoptions.get_xend_unix_path() log.info('unix path=' + path) servers.add(UnixHttpServer(root, path)) api_cfg = xoptions.get_xen_api_server() if api_cfg: try: for server_cfg in api_cfg: # Parse the xen-api-server config ssl_key_file = None ssl_cert_file = None auth_method = XendAPI.AUTH_NONE hosts_allowed = None host_addr = server_cfg[0].split(':', 1) if len(host_addr) == 1: if host_addr[0].lower() == 'unix': use_tcp = False host = 'localhost' port = 0 else: use_tcp = True host = '' port = int(host_addr[0]) else: use_tcp = True host = str(host_addr[0]) port = int(host_addr[1]) if len(server_cfg) > 1: if server_cfg[1] in [XendAPI.AUTH_PAM, XendAPI.AUTH_NONE]: auth_method = server_cfg[1] if len(server_cfg) > 2 and len(server_cfg[2]): hosts_allowed = map(re.compile, server_cfg[2].split(' ')) if len(server_cfg) > 4: # SSL key and cert file ssl_key_file = server_cfg[3] ssl_cert_file = server_cfg[4] servers.add( XMLRPCServer(auth_method, True, use_tcp=use_tcp, ssl_key_file=ssl_key_file, ssl_cert_file=ssl_cert_file, host=host, port=port, path=XEN_API_SOCKET, hosts_allowed=hosts_allowed)) except (ValueError, TypeError), exn: log.exception('Xen API Server init failed') log.error('Xen-API server configuration %s is invalid.', api_cfg)