Skip to content

Commit

Permalink
Merge pull request #39 from gnh1201/Container
Browse files Browse the repository at this point in the history
add docker container lifecycle methods
  • Loading branch information
gnh1201 authored Jul 17, 2024
2 parents 67dc16d + 13494e2 commit 7139092
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 deletions plugins/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -30,6 +30,16 @@ def dispatch(self, type, id, params, conn):
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"]
Expand All @@ -47,8 +57,8 @@ def container_run(self, type, id, params, conn):
detach=True,
)
container.logs()

logger.info("[*] Running...")
return b"[*] Running..."

def container_stop(self, type, id, params, conn):
name = params["name"]
Expand All @@ -57,3 +67,36 @@ def container_stop(self, type, id, params, conn):
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"

0 comments on commit 7139092

Please sign in to comment.