Merge pull request #39 from gnh1201/Container
Some checks failed
Ruff / ruff (push) Has been cancelled

add docker container lifecycle methods
This commit is contained in:
Namhyeon Go 2024-07-17 09:35:07 +09:00 committed by GitHub
commit 7139092c12
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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"