async def create(cls, from_image=None, from_src=None, repo=None, tag=None): if (from_image is None and from_src is None or from_image is not None and from_src is not None): raise ValueError("Specify either from_image or from_src") q = {} if from_image is not None: q['fromImage'] = from_image if from_src is not None: q['fromSrc'] = from_src if repo is not None: q['repo'] = repo if tag is not None: q['tag'] = tag req = cls.client.post( build_url(PREFIX, 'create', **q) ) async with req as res: if res.status != 200: raise await status_error(res) # Wait till full response is available data = (await res.text()).splitlines() if data: # Check last status, make sure it has no error last_status = json.loads(data[-1]) if 'error' in last_status: raise APIError(last_status['error']) return cls(from_image)
async def exec_create(self, *cmd, attach_stdin=False, attach_stdout=True, attach_stderr=True, tty=False, detach_keys='ctrl-c'): data = { 'Cmd': [str(arg) for arg in cmd], 'AttachStdin': attach_stdin, 'AttachStdout': attach_stdout, 'AttachStderr': attach_stderr, 'Tty': tty, 'DetachKeys': detach_keys } req = self.client.post( build_url(PREFIX, self.id, 'exec'), headers={ CONTENT_TYPE: APPLICATION_JSON }, data=json.dumps(data) ) async with req as res: if res.status != 201: raise await status_error(res) raw = await(res.json()) return self.registry.ExecInstance(raw['Id'], raw=raw)
async def list(cls, all=None, labels=None, dangling=False, filters=None, filter=None): f = {} for label, val in (labels or {}).items(): f['label'] = f.get('label', []) + [ '%s=%s' % (label, val) if val else label ] if dangling: f['dangling'] = ['true'] filters = dict(f, **(filters or {})) q = {} if filters: q['filters'] = filters if filter: q['filter'] = filter if all is not None: q['all'] = '1' if all else '0' req = cls.client.get(build_url(PREFIX, 'json', **q)) async with req as res: if res.status != 200: raise await status_error(res) return [ cls(raw['Id'], raw=raw) for raw in await res.json() ]
async def remove(self, remove_volumes=False, force=False): q = { 'v': '1' if remove_volumes else '0', 'force': '1' if force else '0' } req = self.client.delete(build_url(PREFIX, self.id, **q)) async with req as res: if res.status != 204: raise await status_error(res)
async def remove(self, no_prune=False, force=False): q = { 'noprune': '1' if no_prune else '0', 'force': '1' if force else '0' } req = self.client.delete(build_url(PREFIX, self.id, **q)) async with req as res: if res.status != 200: raise await status_error(res)
async def create(cls, config): validate(config, VOLUME_CONFIG) req = cls.client.post( build_url(PREFIX, "create"), headers={CONTENT_TYPE: APPLICATION_JSON}, data=json.dumps(config) ) async with req as res: if res.status != 201: raise await status_error(res) raw = await (res.json()) return cls(raw["Name"], raw=raw)
async def disconnect(self, container): req = self.client.post( build_url(PREFIX, self.id, 'disconnect'), headers={ CONTENT_TYPE: APPLICATION_JSON }, data=json.dumps({ 'container': container.id }) ) async with req as res: if res.status != 200: raise await status_error(res)
def exec_start(self, detach=False, tty=False): data = { 'Detach': detach, 'Tty': tty } req = self.client.post( build_url('exec', self.id, 'start'), headers={ CONTENT_TYPE: APPLICATION_JSON }, data=json.dumps(data) ) return self.registry.ExecStream(req, tty=tty)
async def list(cls, dangling=False, filters=None): f = {} if dangling: f["dangling"] = True filters = dict(f, **(filters or {})) q = {} if filters: q["filters"] = filters req = cls.client.get(build_url(PREFIX, **q)) async with req as res: if res.status != 200: raise await status_error(res) return [cls(raw["Name"], raw=raw) for raw in ((await res.json()).get("Volumes", None) or [])]
async def list(cls, id=None, name=None, filters=None): f = {} if id is not None: f['id'] = or_filter(id) if name is not None: f['name'] = or_filter(name) filters = dict(f, **(filters or {})) q = {} if filters: q['filters'] = filters req = cls.client.get(build_url(PREFIX, **q)) async with req as res: if res.status != 200: raise await status_error(res) return [ cls(raw['ID'], raw=raw) for raw in await res.json() ]
async def create(cls, config, name=None): validate(config, CONTAINER_CONFIG) q = {} if name is not None: q['name'] = name req = cls.client.post( build_url(PREFIX, 'create', **q), headers={ CONTENT_TYPE: APPLICATION_JSON }, data=json.dumps(config) ) async with req as res: if res.status != 201: raise await status_error(res) raw = await(res.json()) return cls(raw['Id'], raw=raw)
async def update(self, config, version=None): validate(config, SERVICE_CONFIG) if version is None: try: version = self.data.version.index except AttributeError: raise ValueError("version") q = { 'version': version } req = self.client.post( build_url(PREFIX, self.id, 'update', **q), headers={ CONTENT_TYPE: APPLICATION_JSON }, data=json.dumps(config) ) async with req as res: if res.status != 200: raise await status_error(res)
async def kill(self, signal=None): req = self.client.post(build_url(PREFIX, self.id, 'kill')) async with req as res: if res.status != 204: raise await status_error(res)
async def unpause(self): req = self.client.post(build_url(PREFIX, self.id, 'unpause')) async with req as res: if res.status != 204: raise await status_error(res)
async def restart(self, timeout=None): req = self.client.post(build_url(PREFIX, self.id, 'restart')) async with req as res: if res.status != 204: raise await status_error(res)
async def inspect(self): req = self.client.get(build_url(PREFIX, self.id, 'json')) async with req as res: if res.status != 200: raise await status_error(res) return DataMapping(await res.json())
async def remove(self): req = self.client.delete(build_url(PREFIX, self.id)) async with req as res: if res.status != 200: raise await status_error(res)