From 13494e285b8e98ad1cc3c624e3a5681b7033e710 Mon Sep 17 00:00:00 2001 From: tkgka Date: Sun, 14 Jul 2024 18:46:02 +0900 Subject: [PATCH] add docker container lifecycle methods --- plugins/container.py | 47 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/plugins/container.py b/plugins/container.py index fcb1e17..da14569 100644 --- a/plugins/container.py +++ b/plugins/container.py @@ -21,7 +21,7 @@ class Container(Extension): def __init__(self): self.type = "rpcmethod" self.method = "container_init" - self.exported_methods = ["container_run", "container_stop"] + self.exported_methods = ["container_cteate", "container_start", "container_run", "container_stop", "container_pause", "container_unpause", "container_restart", "container_kill", "container_remove"] # docker self.client = docker.from_env() @@ -30,6 +30,16 @@ class Container(Extension): logger.info("[*] Greeting! dispatch") conn.send(b"Greeting! dispatch") + def container_cteate(self, type, id, params, conn): + # todo: - + return b"[*] Created" + + def container_start(self, type, id, params, conn): + name = params['name'] + + container = self.client.containers.get(name) + container.start() + def container_run(self, type, id, params, conn): devices = params["devices"] image = params["image"] @@ -47,8 +57,8 @@ class Container(Extension): detach=True, ) container.logs() - logger.info("[*] Running...") + return b"[*] Running..." def container_stop(self, type, id, params, conn): name = params["name"] @@ -57,3 +67,36 @@ class Container(Extension): container.stop() logger.info("[*] Stopped") + return b"[*] Stopped" + + def container_pause(self, type, id, params, conn): + name = params['name'] + + container = self.client.containers.get(name) + container.pause() + return b"[*] Paused" + + def container_unpause(self, type, id, params, conn): + name = params['name'] + + container = self.client.containers.get(name) + container.unpause() + return b"[*] Unpaused" + + def container_restart(self, type, id, params, conn): + name = params['name'] + + container = self.client.containers.get(name) + container.restart() + return b"[*] Restarted" + + def container_kill(self, type, id, params, conn): + # TODO: - + return b"[*] Killed" + + def container_remove(self, type, id, params, conn): + name = params['name'] + + container = self.client.containers.get(name) + container.remove() + return b"[*] Removed" \ No newline at end of file