def _setup_client(self, create=False): """ Setup a client connection. If create is True and the container doesn't exist, it is created. Sets username, password and authurl. Returns a client connection, metadata tuple or (None, None) on error. """ try: values = self.conf.get_container(self.args.container) except ValueError as ex: self.log.error(ex) return (None, None) self.authurl = values['authurl'] self.username = values['username'] self.password = values['password'] cli = client.Connection( values['authurl'], values['username'], values['password'], auth_version=values['auth_version'], tenant_name=values['tenant_name']) try: headers, _ = cli.get_container(self.args.container) except (socket.error, client.ClientException) as ex: if getattr(ex, 'http_status', None) == 404: if create: self.log.warning("%s doesn't exist, will be created" % self.args.container) return (cli, dict()) else: self.log.error("%s doesn't exist" % self.args.container) else: self.log.error(ex) return (None, None) self.log.debug(headers) meta = getMeta(headers) self.log.debug("Meta: %s" % meta) if not meta: self.log.error("%s hasn't been setup to be used with swiftnbd" % self.args.container) return (None, None) return (cli, meta)
def _setup_client(self, create=False): """ Setup a client connection. If create is True and the container doesn't exist, it is created. Sets username, password and authurl. Returns a client connection, metadata tuple or (None, None) on error. """ try: values = self.conf.get_container(self.args.container) except ValueError as ex: self.log.error(ex) return (None, None) self.authurl = values['authurl'] self.username = values['username'] self.password = values['password'] cli = client.Connection(self.authurl, self.username, self.password) try: headers, _ = cli.get_container(self.args.container) except (socket.error, client.ClientException) as ex: if getattr(ex, 'http_status', None) == 404: if create: self.log.warning("%s doesn't exist, will be created" % self.args.container) return (cli, dict()) else: self.log.error("%s doesn't exist" % self.args.container) else: self.log.error(ex) return (None, None) self.log.debug(headers) meta = getMeta(headers) self.log.debug("Meta: %s" % meta) if not meta: self.log.error("%s hasn't been setup to be used with swiftnbd" % self.args.container) return (None, None) return (cli, meta)
def do_list(self): self.log.debug("listing containers") if self.args.simple: out = print else: out = self.log.info prev_authurl = None for container, values in self.conf.items(): if prev_authurl or prev_authurl != values['authurl']: 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.error("%s doesn't exist (auth-url: %s)" % (container, values['authurl'])) else: self.log.error(ex) else: meta = getMeta(headers) self.log.debug("Meta: %s" % meta) if meta: lock = "unlocked" if not 'client' in meta else "locked by %s" % meta[ 'client'] out("%s objects=%s size=%s (version=%s, %s)" % ( container, meta['objects'], meta['object-size'], meta['version'], lock, )) else: out("%s is not a swiftnbd container" % container) prev_authurl = values['authurl'] return 0
def do_list(self): self.log.debug("listing containers") if self.args.simple: out = print else: out = self.log.info prev_authurl = None for container, values in self.conf.iteritems(): if prev_authurl or prev_authurl != values['authurl']: cli = client.Connection( values['authurl'], values['username'], values['password'], auth_version=values['auth_version'], tenant_name=values['tenant_name']) try: headers, _ = cli.get_container(container) except (socket.error, client.ClientException) as ex: if getattr(ex, 'http_status', None) == 404: self.log.error("%s doesn't exist (auth-url: %s)" % (container, values['authurl'])) else: self.log.error(ex) else: meta = getMeta(headers) self.log.debug("Meta: %s" % meta) if meta: lock = "unlocked" if not 'client' in meta else "locked by %s" % meta['client'] out("%s objects=%s size=%s (version=%s, %s)" % (container, meta['objects'], meta['object-size'], meta['version'], lock, )) else: out("%s is not a swiftnbd container" % container) prev_authurl = values['authurl'] return 0
def lock(self, client_id): """Set the storage as busy""" if self.locked: return try: headers, _ = self.cli.get_container(self.container) except (socket.error, client.ClientException) as ex: raise StorageError(errno.EIO, "Failed to lock: %s" % ex) self.meta = getMeta(headers) if self.meta.get('client'): raise StorageError(errno.EBUSY, "Already in use: %s" % self.meta['client']) self.meta['client'] = "%s@%i" % (client_id, time()) hdrs = setMeta(self.meta) try: self.cli.put_container(self.container, headers=hdrs) except (socket.error, client.ClientException) as ex: raise StorageError(errno.EIO, "Failed to lock: %s" % ex) self.locked = True
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 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
def _setup_client(self, create=False, container=None): """ Setup a client connection. If create is True and the container doesn't exist, it is created. Sets auth dictionary to create connections. Returns a client connection, metadata tuple or (None, None) on error. """ if container is None: container = self.args.container try: values = self.conf.get_container(container) except ValueError as ex: self.log.error(ex) return (None, None) 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" % (container, values['username'])) return (None, None) 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']) self.auth = auth cli = client.Connection(**auth) try: headers, _ = cli.get_container(container) except (socket.error, client.ClientException) as ex: if getattr(ex, 'http_status', None) == 404: if create: self.log.warning("%s doesn't exist, will be created" % container) return (cli, dict()) else: self.log.error("%s doesn't exist" % container) else: self.log.error(ex) return (None, None) self.log.debug(headers) meta = getMeta(headers) self.log.debug("Meta: %s" % meta) if not meta: self.log.error("%s hasn't been setup to be used with swiftnbd" % container) return (None, None) return (cli, meta)
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 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
def _setup_client(self, create=False, container=None): """ Setup a client connection. If create is True and the container doesn't exist, it is created. Sets auth dictionary to create connections. Returns a client connection, metadata tuple or (None, None) on error. """ if container is None: container = self.args.container try: values = self.conf.get_container(container) except ValueError as ex: self.log.error(ex) return (None, None) 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" % (container, values['username'])) return (None, None) 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']) self.auth = auth cli = client.Connection(**auth) try: headers, _ = cli.get_container(container) except (socket.error, client.ClientException) as ex: if getattr(ex, 'http_status', None) == 404: if create: self.log.warning("%s doesn't exist, will be created" % container) return (cli, dict()) else: self.log.error("%s doesn't exist" % container) else: self.log.error(ex) return (None, None) self.log.debug(headers) meta = getMeta(headers) self.log.debug("Meta: %s" % meta) if not meta: self.log.error("%s hasn't been setup to be used with swiftnbd" % container) return (None, None) return (cli, meta)