def ldap_uris(ldap_uri=None): """Return all nodes registered in LDAP.""" if len(SERVICES) == 0: raise LdapConfigurationError(_('No SERVICES defined.')) # Build filter to find all Virtualization nodes filter_list = ["(univentionService=%s)" % service for service in SERVICES] if len(filter_list) > 1: filter = "(|%s)" % "".join(filter_list) else: filter = filter_list[0] # ensure that we should manage the host filter = '(&%s(|(!(univentionVirtualMachineManageableBy=*))(univentionVirtualMachineManageableBy=%s)))' % ( filter, HOST_FQDN ) logger.debug('Find servers to manage "%s"' % filter) lo, position = univention.admin.uldap.getMachineConnection(ldap_master=False) try: nodes = [] res = lo.search(filter) for dn, data in res: fqdn = ldap2fqdn(data) for service in SERVICES: if service in data['univentionService']: uri = SERVICES[service] % fqdn nodes.append(uri) logger.debug('Registered URIs: %s' % ', '.join(nodes)) return nodes except LDAPError, e: raise LdapConnectionError(_('Could not query "%(uri)s"'), uri=ldap_uri)
def storage_pools(node): """ Get 'protocol.Data_Pool' instance for all (active) pools. """ if node.conn is None: raise StorageError(_('Error listing pools at "%(uri)s": %(error)s'), uri=node.pd.uri, error='no connection') try: pools = [] for name in timeout(node.conn.listStoragePools)(): pool = get_pool_info(node, name) pools.append(pool) return pools except TimeoutError as ex: logger.warning( 'libvirt connection "%s" timeout: %s', node.pd.uri, ex, ) node.pd.last_try = time.time() return pools except libvirt.libvirtError as ex: logger.error(ex) raise StorageError( _('Error listing pools at "%(uri)s": %(error)s'), uri=node.uri, error=ex.get_error_message(), )
def DOMAIN_UNDEFINE(server, request): """Undefine a domain on a node.""" if not isinstance(request.uri, basestring): raise CommandError('DOMAIN_UNDEFINE', _('uri != string: %(uri)s'), uri=request.uri) if not isinstance(request.domain, basestring): raise CommandError('DOMAIN_UNDEFINE', _('domain != string: %(domain)s'), domain=request.domain) if request.volumes is not None and not isinstance( request.volumes, (list, tuple)): raise CommandError('DOMAIN_UNDEFINE', _('volumes != list or None: %(volumes)s'), volumes=request.volumes) if request.volumes is not None: for vol in request.volumes: if not isinstance(vol, basestring): raise CommandError('DOMAIN_UNDEFINE', _('volumes[] != string: %(volume)s'), volume=vol) logger.debug( 'DOMAIN_UNDEFINE %s#%s [%s]' % (request.uri, request.domain, ','.join(request.volumes))) else: logger.debug( 'DOMAIN_UNDEFINE %s#%s None (-> all volumes will be removed)' % (request.uri, request.domain)) try: node.domain_undefine(request.uri, request.domain, request.volumes) except node.NodeError as e: raise CommandError('DOMAIN_UNDEFINE', e)
def parse(buffer, offset=0): """Unpack packet from data.""" # important! Bug #44128: As we unpickle files this would lead to a AttributeError # in certain situations (e.g. two parallel threads) if the module isn't yet imported import univention.uvmm.node # noqa: F401 FORMAT = '!HHI' SIZE = struct.calcsize(FORMAT) if len(buffer) < offset + SIZE: return None ( v1, v2, length, ) = struct.unpack(FORMAT, buffer[offset:offset + SIZE]) if VERSION[0] != v1 or VERSION[1] > v2: raise PacketError(_('Incompatible version: %(major)d.%(minor)d'), major=v1, minor=v2) if len(buffer) < offset + SIZE + length: return None (data, ) = struct.unpack('%ds' % length, buffer[offset + SIZE:offset + SIZE + length]) try: s = StringIO(data) p = pickle.Unpickler(s) packet = p.load() except Exception as e: raise PacketError(_('Not a valid Packet: %(msg)s'), msg=str(e)) if not isinstance(packet, Packet): raise PacketError(_('Not a Packet: %(type)s'), type=type(packet)) else: return (SIZE + length, packet)
def cached(cachefile, func, exception=LdapConnectionError): """Cache result of function or return cached result on LdapConnectionException.""" try: result = func() file = open("%s.new" % (cachefile, ), "w") try: p = pickle.Pickler(file) p.dump(result) finally: file.close() try: os.remove("%s.old" % (cachefile, )) except OSError, e: if e.errno != errno.ENOENT: raise LdapError(_('Error removing %(file)s.old: %(msg)s'), file=cachefile, msg=e) try: os.rename("%s" % (cachefile, ), "%s.old" % (cachefile, )) except OSError, e: if e.errno != errno.ENOENT: raise LdapError(_('Error renaming %(file)s: %(msg)s'), file=cachefile, msg=e)
def parse(buffer, offset=0): """Unpack packet from data.""" FORMAT = '!HHI' SIZE = struct.calcsize(FORMAT) if len(buffer) < offset + SIZE: return None ( v1, v2, length, ) = struct.unpack(FORMAT, buffer[offset:offset + SIZE]) if VERSION[0] != v1 or VERSION[1] > v2: raise PacketError(_('Incompatible version: %(major)d.%(minor)d'), major=v1, minor=v2) if len(buffer) < offset + SIZE + length: return None (data, ) = struct.unpack('%ds' % length, buffer[offset + SIZE:offset + SIZE + length]) try: s = StringIO(data) p = pickle.Unpickler(s) packet = p.load() except Exception, e: raise PacketError(_('Not a valid Packet: %(msg)s'), msg=str(e))
def ldap_uris(ldap_uri=None): """Return all nodes registered in LDAP.""" if len(SERVICES) == 0: raise LdapConfigurationError(_('No SERVICES defined.')) # Build filter to find all Virtualization nodes filter_list = ["(univentionService=%s)" % service for service in SERVICES] if len(filter_list) > 1: filter = "(|%s)" % "".join(filter_list) else: filter = filter_list[0] # ensure that we should manage the host filter = '(&%s(|(!(univentionVirtualMachineManageableBy=*))(univentionVirtualMachineManageableBy=%s)))' % ( filter, HOST_FQDN) logger.debug('Find servers to manage "%s"' % filter) lo, position = univention.admin.uldap.getMachineConnection( ldap_master=False) try: nodes = [] res = lo.search(filter) for dn, data in res: fqdn = ldap2fqdn(data) for service in SERVICES: if service in data['univentionService']: uri = SERVICES[service] % fqdn nodes.append(uri) logger.debug('Registered URIs: %s' % ', '.join(nodes)) return nodes except LDAPError: raise LdapConnectionError(_('Could not query "%(uri)s"'), uri=ldap_uri)
def __getitem__(self, cmd): if cmd.startswith('_'): raise CommandError(cmd, _('Command "%(command)s" is restricted')) try: return getattr(self, cmd) except AttributeError: raise CommandError(cmd, _('Unknown command "%(command)s"'))
def __getitem__(self, cmd): if cmd.startswith('_'): raise CommandError(cmd, _('Command "%(command)s" is restricted')) try: return getattr(self, cmd) except AttributeError, e: raise CommandError(cmd, _('Unknown command "%(command)s'))
def close(self): """Close socket.""" try: self.sock.close() self.sock = None except socket.timeout as msg: raise ClientError(_('Timed out while closing socket.')) except socket.error as (errno, msg): raise ClientError(_('Error while closing socket: %(errno)d'), errno=errno)
def cached(cachefile, func, exception=LdapConnectionError): """Cache result of function or return cached result on LdapConnectionException.""" try: result = func() file = open("%s.new" % (cachefile, ), "w") try: p = pickle.Pickler(file) p.dump(result) finally: file.close() try: os.remove("%s.old" % (cachefile, )) except OSError as e: if e.errno != errno.ENOENT: raise LdapError(_('Error removing %(file)s.old: %(msg)s'), file=cachefile, msg=e) try: os.rename("%s" % (cachefile, ), "%s.old" % (cachefile, )) except OSError as e: if e.errno != errno.ENOENT: raise LdapError(_('Error renaming %(file)s: %(msg)s'), file=cachefile, msg=e) try: os.rename("%s.new" % (cachefile, ), "%s" % (cachefile, )) except OSError as e: if e.errno != errno.ENOENT: raise LdapError(_('Error renaming %(file)s.new: %(msg)s'), file=cachefile, msg=e) except IOError as e: # LdapError("Error writing %(file)s: %(msg)e", file=cachefile, msg=e) pass except exception as msg: logger.info('Using cached data "%s"' % (cachefile, )) try: file = open("%s" % (cachefile, ), "r") try: p = pickle.Unpickler(file) result = p.load() finally: file.close() except IOError as e: if e.errno != errno.ENOENT: raise exception(_('Error reading %(file)s: %(msg)s'), file=cachefile, msg=e) raise msg except EOFError: raise exception(_('Error reading incomplete %(file)s.'), file=cachefile) return result
def send(self, req): """Send request, wait for and return answer.""" packet = req.pack() try: self.sock.send(packet) return self.receive() except socket.timeout as msg: raise ClientError(_('Timed out while sending data.')) except socket.error as (errno, msg): raise ClientError(_("Could not send request: %(errno)d"), errno=errno)
def DOMAIN_SNAPSHOT_DELETE(server, request): """Delete snapshot of domain.""" if not isinstance(request.uri, basestring): raise CommandError('DOMAIN_SNAPSHOT_DELETE', _('uri != string: %(uri)s'), uri=request.uri) if not isinstance(request.domain, basestring): raise CommandError('DOMAIN_SNAPSHOT_DELETE', _('domain != string: %(domain)s'), domain=request.domain) if not isinstance(request.snapshot, basestring): raise CommandError('DOMAIN_SNAPSHOT_DELETE', _('snapshot != string: %(snapshot)s'), snapshot=request.snapshot) logger.debug('DOMAIN_SNAPSHOT_DELETE %s#%s %s' % (request.uri, request.domain, request.snapshot)) try: node.domain_snapshot_delete(request.uri, request.domain, request.snapshot) except node.NodeError, e: raise CommandError('DOMAIN_SNAPSHOT_DELETE', e)
def DOMAIN_STATE(server, request): """Change running state of domain on node.""" if not isinstance(request.uri, basestring): raise CommandError('DOMAIN_STATE', _('uri != string: %(uri)s'), uri=request.uri) if not isinstance(request.domain, basestring): raise CommandError('DOMAIN_STATE', _('domain != string: %(domain)s'), domain=request.domain) if not request.state in ('RUN', 'PAUSE', 'SHUTDOWN', 'RESTART', 'SUSPEND'): raise CommandError('DOMAIN_STATE', _('unsupported state: %(state)s'), state=request.state) logger.debug('DOMAIN_STATE %s#%s %s' % (request.uri, request.domain, request.state)) try: node.domain_state(request.uri, request.domain, request.state) except node.NodeError, e: raise CommandError('DOMAIN_STATE', e)
def NODE_FREQUENCY(server, request): """Set polling interval for node.""" try: hz = int(request.hz) except TypeError: raise CommandError('NODE_FREQUENCY', _('hz != int: %(hz)s'), hz=request.hz) if request.uri is not None and not isinstance(request.uri, basestring): raise CommandError('NODE_FREQUENCY', _('uri != string: %(uri)s'), uri=request.uri) logger.debug('NODE_FREQUENCY %d %s' % (hz,request.uri)) try: node.node_frequency(hz, request.uri) except node.NodeError, e: raise CommandError('NODE_FREQUENCY', e)
def DOMAIN_MIGRATE(server, request): """Migrate a domain from node to the target node.""" if not isinstance(request.uri, basestring): raise CommandError('DOMAIN_MIGRATE', _('uri != string: %(uri)s'), uri=request.uri) if not isinstance(request.domain, basestring): raise CommandError('DOMAIN_MIGRATE', _('domain != string: %(domain)s'), domain=request.domain) if not isinstance(request.target_uri, basestring): raise CommandError('DOMAIN_MIGRATE', _('target_uri != string: %(uri)s'), uri=request.target_uri) logger.debug('DOMAIN_MIGRATE %s#%s %s' % (request.uri, request.domain, request.target_uri)) try: node.domain_migrate(request.uri, request.domain, request.target_uri) except node.NodeError, e: raise CommandError('DOMAIN_MIGRATE', e)
def DOMAIN_RESTORE(server, request): """Restore defined domain.""" if not isinstance(request.uri, basestring): raise CommandError('DOMAIN_RESTORE', _('uri != string: %(uri)s'), uri=request.uri) if not isinstance(request.domain, basestring): raise CommandError('DOMAIN_RESTORE', _('domain != string: %(domain)s'), domain=request.domain) if not isinstance(request.statefile, basestring): raise CommandError('DOMAIN_RESTORE', _('statefile != string: %(file)s'), file=request.statefile) logger.debug('DOMAIN_RESTORE %s %s' % (request.uri, request.statefile)) try: node.domain_restore(request.uri, request.domain, request.statefile) except node.NodeError, e: raise CommandError('DOMAIN_RESTORE', e)
def NODE_LIST(server, request): """Return list of nodes in group.""" if not isinstance(request.group, basestring): raise CommandError('NODE_LIST', _('group != string: %(group)s'), group=request.group) if not isinstance(request.pattern, basestring): raise CommandError( 'NODE_LIST', _('pattern != string: %(pattern)s'), pattern = request.pattern ) logger.debug('NODE_LIST') try: res = protocol.Response_DUMP() res.data = node.node_list( request.group, request.pattern ) return res except node.NodeError, e: raise CommandError('NODE_LIST', e)
def __init__(self, socket_path, timeout=0): """Open new UNIX socket to socket_path.""" try: self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) if timeout > 0: self.sock.settimeout(timeout) self.sock.connect(socket_path) except socket.timeout as msg: raise ClientError( _('Timed out while opening local socket "%(path)s".'), path=socket_path) except socket.error as (errno, msg): raise ClientError(_('Could not open socket "%(path)s": %(errno)d'), path=socket_path, errno=errno)
def DOMAIN_DEFINE(server, request): """Define new domain on node.""" if not isinstance(request.uri, basestring): raise CommandError('DOMAIN_DEFINE', _('uri != string: %(uri)s'), uri=request.uri) if not isinstance(request.domain, protocol.Data_Domain): raise CommandError('DOMAIN_DEFINE', _('definition != Domain: %(domain)s'), domain=request.domain) logger.debug('DOMAIN_DEFINE %s %s' % (request.uri, request.domain)) try: uuid, warnings = node.domain_define(request.uri, request.domain) res = protocol.Response_DUMP() res.data = uuid res.messages = warnings return res except node.NodeError, e: raise CommandError('DOMAIN_DEFINE', e)
def DOMAIN_INFO( server, request ): """Return detailed information about a domain.""" if not isinstance( request.uri, basestring ): raise CommandError( 'DOMAIN_INFO', _( 'uri != string: %(uri)s' ), uri = request.uri ) if not isinstance( request.domain, basestring ): raise CommandError( 'DOMAIN_INFO', _( 'domain != string: %(domain)s' ), domain = request.domain ) logger.debug('DOMAIN_INFO %s %s' % ( request.uri, request.domain ) ) try: domain_info = node.domain_info( request.uri, request.domain ) res = protocol.Response_DUMP() res.data = domain_info return res except node.NodeError, e: raise CommandError('DOMAIN_INFO', e)
def DOMAIN_LIST( server, request ): """Return a list of available domains of a given node.""" if not isinstance( request.uri, basestring ): raise CommandError( 'DOMAIN_LIST', _( 'uri != string: %(uri)s' ), uri = request.uri ) if not isinstance( request.pattern, basestring ): raise CommandError( 'DOMAIN_LIST', _( 'pattern != string: %(pattern)s' ), pattern = request.pattern ) logger.debug('DOMAIN_LIST %s %s' % ( request.uri, request.pattern ) ) try: domains = node.domain_list( request.uri, request.pattern ) res = protocol.Response_DUMP() res.data = domains return res except node.NodeError, e: raise CommandError('DOMAIN_LIST', e)
def STORAGE_VOLUMES_DESTROY( server, request ): '''destroy all given volumes in a pool.''' if not isinstance( request.uri, basestring ): raise CommandError( 'STORAGE_VOLUMES_DESTROY' , _( 'uri != string: %(uri)s' ), uri = request.uri ) for vol in request.volumes: if not isinstance( vol, basestring ): raise CommandError( 'STORAGE_VOLUMES_DESTROY', _('volumes[] != string: %(volume)s'), volume = vol ) logger.debug('STORAGE_VOLUMES_DESTROY %s]' % request.uri ) try: n = node.node_query( request.uri ) storage.destroy_storage_volumes( n.conn, request.volumes, ignore_error = True ) res = protocol.Response_OK() return res except node.NodeError, e: raise CommandError( 'STORAGE_VOLUMES_DESTROY', e )
def ldap_modify(uuid): """Modify annotations for domain from LDAP.""" try: lo, position = univention.admin.uldap.getMachineConnection(ldap_master=True) base = "%s,%s" % (LDAP_INFO_RDN, position.getDn()) except (SERVER_DOWN, IOError ), e: raise LdapConnectionError(_('Could not open LDAP-Admin connection'))
def network_is_active( conn, name ): '''checks if the network with the given name ist currently active''' try: return name in conn.listNetworks() except libvirt.libvirtError, e: logger.error( e ) raise NetworkError( _( 'Error retrieving list of active networks: %(error)s' ), error = e.get_error_message() )
def handle(self, command): """ Handle authentication: If the connection is not yet authenticated, start PAM authentication and handle challenge-response through AUTHENTICATION packets. Returns None on success, Response_AUTHENTICATION on further negotiation and Response_ERROR on failure.""" if self.state == PamAuthenticator.AUTH_OK: return None if self.state == PamAuthenticator.AUTH_FAIL: return self.res self.challenge_pending.clear() if self.state == PamAuthenticator.AUTH_INIT: self.state = PamAuthenticator.AUTH_RUNNING self.thread.start() else: # self.state == PamAuthenticator.AUTH_RUNNING: if not isinstance(command, protocol.Request_AUTHENTICATION): logger.warn('Authentication protocol violated: %s' % (command,)) # terminate thread self.state = PamAuthenticator.AUTH_FAIL self.response = None self.response_pending.set() self.thread.join() self.res = protocol.Response_ERROR() self.res.translatable_text = _('Authentication protocol violated') return self.res self.response = command.response self.response_pending.set() self.challenge_pending.wait(PamAuthenticator.TIMEOUT) if self.res is None: self.thread.join() return self.res
def ldap_annotation(uuid): """Load annotations for domain from LDAP.""" try: lo, position = univention.admin.uldap.getMachineConnection( ldap_master=False) base = "%s,%s" % (LDAP_INFO_RDN, position.getDn()) except (SERVER_DOWN, IOError): raise LdapConnectionError(_('Could not open LDAP-Machine connection')) co = None dn = "%s=%s,%s" % (uvmm_info.mapping.mapName('uuid'), uuid, base) filter = "(objectclass=*)" logger.debug('Querying domain infos "%s"' % dn) try: res = univention.admin.modules.lookup(uvmm_info, co, lo, scope='base', base=dn, filter=filter, required=True, unique=True) record = res[0] return dict(record) except univention.admin.uexceptions.base: return {}
def __init__(self, host, port=2106, ssl_timeout=0, tcp_timeout=0): """Open new SSL encrypted TCP socket to host:port.""" try: ctx = SSL.Context(SSL.SSLv23_METHOD) ctx.set_options(SSL.OP_NO_SSLv2) #ctx.set_verify(SSL.VERIFY_PEER|SSL.VERIFY_FAIL_IF_NO_PEER_CERT, verify_cb) ctx.use_privatekey_file(UVMM_ClientSSLSocket.privatekey) ctx.use_certificate_file(UVMM_ClientSSLSocket.certificate) ctx.load_verify_locations(UVMM_ClientSSLSocket.cas) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) if tcp_timeout > 0: sock.settimeout(tcp_timeout) self.sock = SSL.Connection(ctx, sock) self.sock.connect((host, port)) if ssl_timeout > 0: import struct self.sock.setblocking(1) tv = struct.pack('ii', int(ssl_timeout), int(0)) self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVTIMEO, tv) except socket.timeout, msg: raise ClientError( _('Timed out while connecting to "%(host)s:%(port)d".'), host=host, port=port)
def close(self): """Close socket.""" try: self.sock.close() self.sock = None except socket.timeout, msg: raise ClientError(_('Timed out while closing socket.'))
def L_CLOUD_INSTANCE_TERMINATE(server, request): """Terminate a cloud instance""" logger.debug('L_CLOUD_INSTANCE_TERMINATE') if not isinstance(request.conn_name, basestring): raise CommandError('L_CLOUD_INSTANCE_TERMINATE', _('conn_name != string: %(conn_name)s'), conn_name=request.conn_name) if not isinstance(request.instance_id, basestring): raise CommandError('L_CLOUD_INSTANCE_TERMINATE', _('instance_id != string: %(instance_id)s'), instance_id=request.instance_id) try: cloudnode.cloudconnections.instance_terminate( request.conn_name, request.instance_id) except cloudnode.CloudConnectionError as e: raise CommandError('L_CLOUD_INSTANCE_TERMINATE', e)
def create_storage_pool(conn, path, pool_name='default'): """Create directory pool.""" # FIXME: support other types than dir xml = ''' <pool type="dir"> <name>%(pool)s</name> <target> <path>%(path)s</path> </target> </pool> ''' % { 'pool': xml_escape(pool_name), 'path': xml_escape(path), } try: pool = conn.storagePoolDefineXML(xml, 0) pool.setAutostart(True) pool.create(0) except libvirt.libvirtError as ex: logger.error(ex) raise StorageError( _('Error creating storage pool "%(pool)s": %(error)s'), pool=pool_name, error=ex.get_error_message(), )
def get_pool_info(node, name): """ Get 'protocol.Data_Pool' instance for named pool. """ try: pool = node.conn.storagePoolLookupByName(name) except libvirt.libvirtError as ex: if ex.get_error_code() == libvirt.VIR_ERR_NO_STORAGE_POOL: raise KeyError(name) logger.error(ex) raise StorageError( _('Error listing pools at "%(uri)s": %(error)s'), uri=node.pd.uri, error=ex.get_error_message(), ) xml = pool.XMLDesc(0) pool_tree = ET.fromstring(xml) res = Data_Pool() res.name = name res.uuid = pool_tree.findtext('uuid') res.capacity = int(pool_tree.findtext('capacity')) res.available = int(pool_tree.findtext('available')) res.path = pool_tree.find('target').findtext('path') res.active = pool.isActive() == 1 res.type = pool_tree.attrib['type'] # pool/@type return res
def L_CLOUD_INSTANCE_CREATE(server, request): """Create a new cloud instance""" logger.debug('L_CLOUD_INSTANCE_CREATE') if not isinstance(request.conn_name, basestring): raise CommandError('L_CLOUD_INSTANCE_CREATE', _('conn_name != string: %(conn_name)s'), conn_name=request.conn_name) if not isinstance(request.args, dict): raise CommandError('L_CLOUD_INSTANCE_CREATE', _('args != dict: %(args)s'), agrs=request.args) try: cloudnode.cloudconnections.instance_create(request.conn_name, request.args) except cloudnode.CloudConnectionError as e: raise CommandError('L_CLOUD_INSTANCE_CREATE', e)
def ldap_modify(uuid): """Modify annotations for domain from LDAP.""" try: lo, position = univention.admin.uldap.getMachineConnection( ldap_master=True) base = "%s,%s" % (LDAP_INFO_RDN, position.getDn()) except (SERVER_DOWN, IOError): raise LdapConnectionError(_('Could not open LDAP-Admin connection')) co = None dn = "%s=%s,%s" % (uvmm_info.mapping.mapName('uuid'), uuid, base) filter = "(objectclass=*)" logger.debug('Updating domain infos "%s"' % dn) try: res = univention.admin.modules.lookup(uvmm_info, co, lo, scope='base', base=dn, filter=filter, required=True, unique=True) record = res[0] record.open() record.commit = record.modify except univention.admin.uexceptions.base: position.setDn(base) record = uvmm_info.object(co, lo, position) record['uuid'] = uuid record['description'] = None record['contact'] = None record['profile'] = None record.commit = record.create return record
class Packet(object): """On-wire packet format.""" def __init__(self, **kw): """Create new packet.""" self._default() for k, v in kw.items(): if hasattr(self, k): setattr(self, k, v) else: raise AttributeError( "Packet '%s' has no attribute '%s'" % (str(self.__class__)[len(self.__class__.__module__) + 1:], k)) def __str__(self): res = ['Packet:'] for attr in dir(self): if not attr.startswith('_') and attr not in ('pack', 'parse'): res.append(' %s: %s' % (attr, str(getattr(self, attr)))) return '\n'.join(res) def pack(self): """Pack data for transfer.""" s = StringIO() p = pickle.Pickler(s) p.dump(self) data = s.getvalue() return struct.pack('!HHI', VERSION[0], VERSION[1], len(data)) + data @staticmethod def parse(buffer, offset=0): """Unpack packet from data.""" FORMAT = '!HHI' SIZE = struct.calcsize(FORMAT) if len(buffer) < offset + SIZE: return None ( v1, v2, length, ) = struct.unpack(FORMAT, buffer[offset:offset + SIZE]) if VERSION[0] != v1 or VERSION[1] > v2: raise PacketError(_('Incompatible version: %(major)d.%(minor)d'), major=v1, minor=v2) if len(buffer) < offset + SIZE + length: return None (data, ) = struct.unpack('%ds' % length, buffer[offset + SIZE:offset + SIZE + length]) try: s = StringIO(data) p = pickle.Unpickler(s) packet = p.load() except Exception, e: raise PacketError(_('Not a valid Packet: %(msg)s'), msg=str(e)) if not isinstance(packet, Packet): raise PacketError(_('Not a Packet: %(type)s'), type=type(packet)) else: return (SIZE + length, packet)
def send(self, req): """Send request, wait for and return answer.""" packet = req.pack() try: self.sock.send(packet) return self.receive() except socket.timeout, msg: raise ClientError(_('Timed out while sending data.'))
def NODE_LIST(server, request): """Return list of nodes in group.""" if not isinstance(request.group, basestring): raise CommandError('NODE_LIST', _('group != string: %(group)s'), group=request.group) if not isinstance(request.pattern, basestring): raise CommandError('NODE_LIST', _('pattern != string: %(pattern)s'), pattern=request.pattern) logger.debug('NODE_LIST') try: res = protocol.Response_DUMP() res.data = node.node_list(request.group, request.pattern) return res except node.NodeError as e: raise CommandError('NODE_LIST', e)
def NODE_FREQUENCY(server, request): """Set polling interval for node.""" try: hz = int(request.hz) except TypeError: raise CommandError('NODE_FREQUENCY', _('hz != int: %(hz)s'), hz=request.hz) if request.uri is not None and not isinstance(request.uri, basestring): raise CommandError('NODE_FREQUENCY', _('uri != string: %(uri)s'), uri=request.uri) logger.debug('NODE_FREQUENCY %d %s' % (hz, request.uri)) try: node.node_frequency(hz, request.uri) except node.NodeError as e: raise CommandError('NODE_FREQUENCY', e)
def ldap_modify(uuid): """Modify annotations for domain from LDAP.""" try: lo, position = univention.admin.uldap.getMachineConnection( ldap_master=True) base = "%s,%s" % (LDAP_INFO_RDN, position.getDn()) except (SERVER_DOWN, IOError), e: raise LdapConnectionError(_('Could not open LDAP-Admin connection'))
def STORAGE_VOLUME_USEDBY( server, request ): '''Return list of domains using the given volume.''' if not isinstance( request.volume, basestring ): raise CommandError( 'STORAGE_VOLUME_USEDBY' , _( 'volume != string: %(volume)s' ), volume = request.volume ) logger.debug('STORAGE_VOLUME_USEDBY %s]' % request.volume ) res = protocol.Response_DUMP() res.data = storage.storage_volume_usedby( node.nodes, request.volume ) return res
def __init__(self, host, port=2105, timeout=0): """Open new TCP socket to host:port.""" try: self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) if timeout > 0: self.sock.settimeout(timeout) self.sock.connect((host, port)) except socket.timeout, msg: raise ClientError(_('Timed out while connecting to "%(host)s:%(port)d".'), host=host, port=port)
def DOMAIN_UPDATE(server, request): """Trigger update of domain.""" if not isinstance(request.domain, basestring): raise CommandError( 'DOMAIN_UPDATE', _('domain != string: %(domain)s'), domain=request.domain) logger.debug('DOMAIN_UPDATE %s' % request.domain) try: node.domain_update(request.domain) except node.NodeError, e: raise CommandError('DOMAIN_UPDATE', e)
def __init__(self, socket_path, timeout=0): """Open new UNIX socket to socket_path.""" try: self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) if timeout > 0: self.sock.settimeout(timeout) self.sock.connect(socket_path) except socket.timeout, msg: raise ClientError(_('Timed out while opening local socket "%(path)s".'), path=socket_path)
def parse(buffer, offset=0): """Unpack packet from data.""" FORMAT = '!HHI' SIZE = struct.calcsize(FORMAT) if len(buffer) < offset + SIZE: return None (v1, v2, length,) = struct.unpack(FORMAT, buffer[offset:offset + SIZE]) if VERSION[0] != v1 or VERSION[1] > v2: raise PacketError(_('Incompatible version: %(major)d.%(minor)d'), major=v1, minor=v2) if len(buffer) < offset + SIZE + length: return None (data,) = struct.unpack('%ds' % length, buffer[offset + SIZE:offset + SIZE + length]) try: s = StringIO(data) p = pickle.Unpickler(s) packet = p.load() except Exception, e: raise PacketError(_('Not a valid Packet: %(msg)s'), msg=str(e))
def L_CLOUD_FREQUENCY(server, request): """Set polling interval for cloud connection""" try: freq = int(request.freq) except TypeError: raise CommandError('L_CLOUD_FREQUENCY', _('freq != int: %(freq)s'), freq=request.freq) if request.name is not None and not isinstance(request.name, basestring): raise CommandError('L_CLOUD_FREQUENCY', _('name != string: %(name)s'), name=request.name) logger.debug('L_CLOUD_FREQUENCY %d %s' % (freq, request.name)) try: cloudnode.cloudconnections.set_poll_frequency(freq, request.name) except cloudnode.CloudConnectionError as e: raise CommandError('L_CLOUD_FREQUENCY', e)
def L_CLOUD_INSTANCE_LIST(server, request): """ List instances in connected clouds """ logger.debug('L_CLOUD_INSTANCE_LIST') if not isinstance(request.conn_name, basestring): raise CommandError('L_CLOUD_INSTANCE_LIST', _('conn_name != string: %(conn_name)s'), conn_name=request.conn_name) if not isinstance(request.pattern, basestring): raise CommandError('L_CLOUD_INSTANCE_LIST', _('pattern != string: %(pattern)s'), pattern=request.pattern) try: res = protocol.Response_DUMP() res.data = cloudnode.cloudconnections.list_conn_instances( request.conn_name, request.pattern) return res except cloudnode.CloudConnectionError as e: raise CommandError('L_CLOUD_INSTANCE_LIST', e)
def NODE_REMOVE(server, request): """Remove node from watch list.""" if not isinstance(request.uri, basestring): raise CommandError('NODE_REMOVE', _('uri != string: %(uri)s'), uri=request.uri) logger.debug('NODE_REMOVE %s' % (request.uri,)) try: node.node_remove(request.uri) except node.NodeError, e: raise CommandError('NODE_REMOVE', e)
def STORAGE_VOLUME_USEDBY(server, request): '''Return list of domains using the given volume.''' if not isinstance(request.volume, basestring): raise CommandError('STORAGE_VOLUME_USEDBY', _('volume != string: %(volume)s'), volume=request.volume) logger.debug('STORAGE_VOLUME_USEDBY %s]' % request.volume) res = protocol.Response_DUMP() res.data = storage.storage_volume_usedby(node.nodes, request.volume) return res
def DOMAIN_UNDEFINE(server, request): """Undefine a domain on a node.""" if not isinstance(request.uri, basestring): raise CommandError('DOMAIN_UNDEFINE', _('uri != string: %(uri)s'), uri=request.uri) if not isinstance(request.domain, basestring): raise CommandError('DOMAIN_UNDEFINE', _('domain != string: %(domain)s'), domain=request.domain) if not request.volumes is None and not isinstance(request.volumes, (list, tuple)): raise CommandError('DOMAIN_UNDEFINE', _('volumes != list or None: %(volumes)s'), volumes=request.volumes) if not request.volumes is None: for vol in request.volumes: if not isinstance(vol, basestring): raise CommandError('DOMAIN_UNDEFINE', _('volumes[] != string: %(volume)s'), volume=vol) logger.debug('DOMAIN_UNDEFINE %s#%s [%s]' % (request.uri, request.domain, ','.join(request.volumes))) else: logger.debug('DOMAIN_UNDEFINE %s#%s None (-> all volumes will be removed)' % (request.uri, request.domain)) try: node.domain_undefine(request.uri, request.domain, request.volumes) except node.NodeError, e: raise CommandError('DOMAIN_UNDEFINE', e)
def DOMAIN_RESTORE(server, request): """Restore defined domain.""" if not isinstance(request.uri, basestring): raise CommandError('DOMAIN_RESTORE', _('uri != string: %(uri)s'), uri=request.uri) if not isinstance(request.domain, basestring): raise CommandError('DOMAIN_RESTORE', _('domain != string: %(domain)s'), domain=request.domain) if not isinstance(request.statefile, basestring): raise CommandError('DOMAIN_RESTORE', _('statefile != string: %(file)s'), file=request.statefile) logger.debug('DOMAIN_RESTORE %s %s' % (request.uri, request.statefile)) try: node.domain_restore(request.uri, request.domain, request.statefile) except node.NodeError as e: raise CommandError('DOMAIN_RESTORE', e)
def NODE_ADD(server, request): """Add node to watch list.""" if not isinstance(request.uri, basestring): raise CommandError('NODE_ADD', _('uri != string: %(uri)s'), uri=request.uri) logger.debug('NODE_ADD %s' % (request.uri,)) try: node.node_add(request.uri) except node.NodeError, e: raise CommandError('NODE_ADD', e)
def NODE_QUERY(server, request): """Get domain and storage-pool information from node.""" if not isinstance(request.uri, basestring): raise CommandError('NODE_QUERY', _('uri != string: %(uri)s'), uri=request.uri) logger.debug('NODE_QUERY %s' % (request.uri,)) try: local_data = node.node_query(request.uri) if local_data is None: raise CommandError('NODE_QUERY', _('unknown node %(uri)s'), uri=request.uri) pkg_data = copy.copy(local_data.pd) pkg_data.domains = [d.pd for d in local_data.domains.values()] res = protocol.Response_DUMP() res.data = pkg_data return res except node.NodeError, e: raise CommandError('NODE_QUERY', e)
def create_storage_volume(conn, domain, disk): """Create disk for domain.""" try: # BUG #19342: does not find volumes in sub-directories v = conn.storageVolLookupByPath(disk.source) logger.warning('Reusing existing volume "%s" for domain "%s"' % (disk.source, domain.name)) return v except libvirt.libvirtError, e: logger.info( 'create_storage_volume: libvirt error (%d): %s' % ( e.get_error_code(), str( e ) ) ) if not e.get_error_code() in ( libvirt.VIR_ERR_INVALID_STORAGE_VOL, libvirt.VIR_ERR_NO_STORAGE_VOL ): raise StorageError(_('Error locating storage volume "%(volume)s" for "%(domain)s": %(error)s'), volume=disk.source, domain=domain.name, error=e.get_error_message())
def cached(cachefile, func, exception=LdapConnectionError): """Cache result of function or return cached result on LdapConnectionException.""" try: result = func() file = open("%s.new" % (cachefile,), "w") try: p = pickle.Pickler(file) p.dump(result) finally: file.close() try: os.remove("%s.old" % (cachefile,)) except OSError, e: if e.errno != errno.ENOENT: raise LdapError(_('Error removing %(file)s.old: %(msg)s'), file=cachefile, msg=e) try: os.rename("%s" % (cachefile,), "%s.old" % (cachefile,)) except OSError, e: if e.errno != errno.ENOENT: raise LdapError(_('Error renaming %(file)s: %(msg)s'), file=cachefile, msg=e)
def get_storage_volumes(node, pool_name, type=None): """Get 'protocol.Disk' instance for all Storage Volumes in named pool of given type.""" if node.conn is None: raise StorageError(_('Error listing volumes at "%(uri)s": %(error)s'), uri=node.uri, error='no connection') volumes = [] try: pool = timeout(node.conn.storagePoolLookupByName)(pool_name) pool.refresh(0) except TimeoutError, e: logger.warning('libvirt connection "%s" timeout: %s', node.pd.uri, e) node.pd.last_try = time.time() return volumes
def STORAGE_VOLUMES( server, request ): '''List all volumes in a pool.''' if not isinstance( request.uri, basestring ): raise CommandError( 'STORAGE_VOLUMES' , _( 'uri != string: %(uri)s' ), uri = request.uri ) logger.debug('STORAGE_VOLUMES %s]' % request.uri ) try: node_stat = node.node_query(request.uri) volumes = storage.get_storage_volumes(node_stat, request.pool, request.type) res = protocol.Response_DUMP() res.data = volumes return res except node.NodeError, e: raise CommandError( 'STORAGE_VOLUMES', e )