caterpillar/plugins/container.py

102 lines
2.9 KiB
Python
Raw Normal View History

2024-03-13 07:15:24 +00:00
#!/usr/bin/python3
#
# container.py
# Linux Container (e.g. Docker) plugin for Caterpillar Proxy
#
# Caterpillar Proxy - The simple and parasitic web proxy with SPAM filter
# Namyheon Go (Catswords Research) <gnh1201@gmail.com>
# https://github.com/gnh1201/caterpillar
# Created at: 2024-03-04
# Updated at: 2024-07-06
2024-03-13 07:15:24 +00:00
#
2024-03-06 06:46:45 +00:00
import docker
from base import Extension, Logger
logger = Logger("Container")
2024-03-06 07:08:27 +00:00
2024-03-06 06:46:45 +00:00
class Container(Extension):
def __init__(self):
self.type = "rpcmethod"
2024-03-06 07:09:24 +00:00
self.method = "container_init"
2024-07-14 09:46:02 +00:00
self.exported_methods = ["container_cteate", "container_start", "container_run", "container_stop", "container_pause", "container_unpause", "container_restart", "container_kill", "container_remove"]
2024-03-06 06:46:45 +00:00
2024-03-06 08:11:24 +00:00
# docker
self.client = docker.from_env()
2024-03-06 06:46:45 +00:00
def dispatch(self, type, id, params, conn):
logger.info("[*] Greeting! dispatch")
2024-07-11 10:02:08 +00:00
conn.send(b"Greeting! dispatch")
2024-03-06 06:46:45 +00:00
2024-07-14 09:46:02 +00:00
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()
2024-03-06 06:46:45 +00:00
def container_run(self, type, id, params, conn):
2024-07-11 10:02:08 +00:00
devices = params["devices"]
image = params["image"]
devices = params["devices"]
name = params["name"]
environment = params["environment"]
volumes = params["volumes"]
2024-03-06 08:18:36 +00:00
container = self.client.containers.run(
2024-03-06 08:34:47 +00:00
image,
devices=devices,
name=name,
volumes=volumes,
environment=environment,
2024-07-11 10:02:08 +00:00
detach=True,
2024-03-06 08:34:47 +00:00
)
2024-03-06 08:11:24 +00:00
container.logs()
logger.info("[*] Running...")
2024-07-14 09:46:02 +00:00
return b"[*] Running..."
2024-03-06 08:34:47 +00:00
2024-03-06 06:46:45 +00:00
def container_stop(self, type, id, params, conn):
2024-07-11 10:02:08 +00:00
name = params["name"]
2024-03-06 08:34:47 +00:00
container = self.client.containers.get(name)
2024-03-06 08:34:47 +00:00
container.stop()
logger.info("[*] Stopped")
2024-07-14 09:46:02 +00:00
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"