def __init__(self, authurl, username, password, container, object_size, objects, cache=None, read_only=False): self.container = container self.object_size = object_size self.objects = objects self.pos = 0 self.locked = False self.meta = dict() self.read_only = read_only self.bytes_in = 0 self.bytes_out = 0 self.cache = cache if self.cache is None: self.cache = Cache(1024**2 // self.object_size) self.cli = client.Connection(authurl, username, password)
def run(self): if os.path.isfile(self.args.pidfile): self.log.error("%s found: is the server already running?" % self.args.pidfile) return 1 stores = dict() for container, values in self.conf.items(): auth = dict( authurl=self.args.authurl, user=values['username'], key=values['password'], ) if self.args.keystone: try: from keystoneclient.v2_0 import client as _check_for_ksclient except ImportError: sys.exit( "auth 2.0 (keystone) requires python-keystoneclient") else: self.log.debug("using auth 2.0 (keystone)") if self.args.keystone_separator not in values['username']: self.log.error("%s: separator not found in %r, skipping" % (container, values['username'])) continue keystone_auth = values['username'].split( self.args.keystone_separator, 1) auth['tenant_name'], auth['user'] = keystone_auth auth['auth_version'] = '2.0' auth['os_options'] = dict( service_type=self.args.keystone_service, endpoint_type=self.args.keystone_endpoint, region_name=self.args.keystone_region, ) self.log.debug("os_options: %r" % auth['os_options']) cli = client.Connection(**auth) try: headers, _ = cli.get_container(container) except (socket.error, client.ClientException) as ex: if getattr(ex, 'http_status', None) == 404: self.log.warning("%s doesn't exist, skipping" % container) continue else: self.log.error("%s: %r, skipping" % (container, ex.msg)) continue self.log.debug(headers) meta = getMeta(headers) if not meta: self.log.warning("%s doesn't appear to be setup, skipping" % container) continue self.log.debug("Meta: %s" % meta) try: object_size = int(meta['object-size']) objects = int(meta['objects']) except ValueError as ex: self.log.error("%s doesn't appear to be correct: %s" % (container, ex)) return 1 if meta['version'] != disk_version: self.log.warning("Version mismatch %s != %s in %s" % (meta['version'], disk_version, container)) stores[container] = SwiftStorage( auth, container, object_size, objects, Cache(int(self.args.cache_limit * 1024**2 / object_size)), values['read-only'].lower() in ('1', 'yes', 'true', 'on'), ) addr = (self.args.bind_address, self.args.bind_port) server = Server(addr, stores) if not self.args.foreground: try: if os.fork() != 0: os._exit(0) except OSError as ex: self.log.error("Failed to daemonize: %s" % ex) return 1 os.setsid() fd = os.open(os.devnull, os.O_RDWR) os.dup2(fd, sys.stdin.fileno()) os.dup2(fd, sys.stdout.fileno()) os.dup2(fd, sys.stderr.fileno()) self.log.info("Starting to serve on %s:%s" % (addr[0], addr[1])) try: fd = os.open(self.args.pidfile, (os.O_CREAT | os.O_EXCL | os.O_WRONLY), 0o644) except OSError as ex: self.log.error("Failed to create the pidfile: %s" % ex) return 1 with os.fdopen(fd, "w") as pidfile_handle: pidfile_handle.write("%s\n" % os.getpid()) pidfile_handle.flush() server.serve_forever() os.remove(self.args.pidfile) # unlock the storages before exit server.unlock_all() self.log.info("Exiting...") return 0
def setUp(self): from swiftnbd.cache import Cache self.cache = Cache(10)
def run(self): if os.path.isfile(self.args.pidfile): self.log.error("%s found: is the server already running?" % self.args.pidfile) return 1 stores = dict() for container, values in self.conf.items(): cli = client.Connection(values['authurl'], values['username'], values['password']) try: headers, _ = cli.get_container(container) except (socket.error, client.ClientException) as ex: if getattr(ex, 'http_status', None) == 404: self.log.warning("%s doesn't exist, skipping" % container) continue else: self.log.error(ex) return 1 self.log.debug(headers) meta = getMeta(headers) if not meta: self.log.warning("%s doesn't appear to be setup, skipping" % container) continue self.log.debug("Meta: %s" % meta) try: object_size = int(meta['object-size']) objects = int(meta['objects']) except ValueError as ex: self.log.error("%s doesn't appear to be correct: %s" % (container, ex)) return 1 if meta['version'] != disk_version: self.log.warning("Version mismatch %s != %s in %s" % (meta['version'], disk_version, container)) stores[container] = SwiftStorage( values['authurl'], values['username'], values['password'], container, object_size, objects, Cache(int(self.args.cache_limit * 1024**2 / object_size)), values['read-only'].lower() in ('1', 'yes', 'true', 'on'), ) addr = (self.args.bind_address, self.args.bind_port) server = Server(addr, stores) if not self.args.foreground: try: if os.fork() != 0: os._exit(0) except OSError as ex: self.log.error("Failed to daemonize: %s" % ex) return 1 os.setsid() fd = os.open(os.devnull, os.O_RDWR) os.dup2(fd, sys.stdin.fileno()) os.dup2(fd, sys.stdout.fileno()) os.dup2(fd, sys.stderr.fileno()) self.log.info("Starting to serve on %s:%s" % (addr[0], addr[1])) try: fd = os.open(self.args.pidfile, (os.O_CREAT | os.O_EXCL | os.O_WRONLY), 0o644) except OSError as ex: self.log.error("Failed to create the pidfile: %s" % ex) return 1 with os.fdopen(fd, "w") as pidfile_handle: pidfile_handle.write("%s\n" % os.getpid()) pidfile_handle.flush() server.serve_forever() os.remove(self.args.pidfile) # unlock the storages before exit server.unlock_all() self.log.info("Exiting...") return 0