Skip to content

Commit

Permalink
feat: add cron prune and refactor api
Browse files Browse the repository at this point in the history
  • Loading branch information
wei committed Mar 3, 2019
1 parent 1e52c0b commit adb12f0
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 341 deletions.
7 changes: 6 additions & 1 deletion .gitignore
@@ -1 +1,6 @@
.DS_Store .DS_Store
__pycache__/
*.py[cod]
*$py.class

manifest.json
47 changes: 0 additions & 47 deletions api/createContainer.py

This file was deleted.

106 changes: 106 additions & 0 deletions api/instantboxManager.py
@@ -0,0 +1,106 @@
import docker
import random
import string
import time
import json


class InstantboxManager(object):
CONTAINER_PREFIX = 'instantbox_managed_'
TIMEOUT_LABEL = 'org.instantbox.variables.EXPIRATION_TIMESTAMP'
OS_LIST = None

def __init__(self):
self.client = docker.from_env()

try:
with open('manifest.json', 'r') as os_manifest:
self.OS_LIST = json.load(os_manifest)
except Exception:
pass

if self.OS_LIST is None:
raise Exception(
'Could not load manifest.json. ' +
'Download it from https://get.instantbox.org/manifest.json'
)

self.AVAILABLE_OS_LIST = []
for os in self.OS_LIST:
for ver in os['subList']:
self.AVAILABLE_OS_LIST.append(ver['osCode'])

def is_create_container(self,
mem,
cpu,
os_name,
os_timeout,
open_port=None):
if open_port is None:
port_dict = {'1588/tcp': None}
else:
port_dict = {'1588/tcp': None, '{}/tcp'.format(open_port): None}

container_name = self.generateContainerName()
try:
self.client.containers.run(
image=os_name,
cpu_period=100000,
cpu_quota=int('%s0000' % cpu),
mem_limit='%sm' % mem,
name=container_name,
ports=port_dict,
restart_policy={'Name': 'always'},
labels={self.TIMEOUT_LABEL: str.format('{:.0f}', os_timeout)},
tty=True,
detach=True,
)
except Exception:
return None
else:
return container_name

def get_container_ports(self, container_name):
try:
ports = self.client.containers.get(
container_name).attrs['NetworkSettings']['Ports']
return {
port: mapped_ports[0]['HostPort']
for port, mapped_ports in ports.items()
}
except Exception:
return None

def remove_timeout_containers(self):
for container in self.client.containers.list():
if container.name.startswith(self.CONTAINER_PREFIX):
timeout = container.labels.get(self.TIMEOUT_LABEL)
if timeout is not None and float(timeout) < time.time():
self.is_rm_container(container.name)

def is_rm_container(self, container_id) -> bool:
try:
container = self.client.containers.get(container_id)
except docker.errors.NotFound:
return True
else:
if container.name.startswith(self.CONTAINER_PREFIX):
container.remove(force=True)
return True

def is_os_available(self, osCode=None) -> bool:
return osCode is not None and osCode in self.AVAILABLE_OS_LIST

def generateContainerName(self) -> str:
return self.CONTAINER_PREFIX + ''.join(
random.sample(string.ascii_letters + string.digits, 16))


if __name__ == '__main__':
test = InstantboxManager()
container_name = test.is_create_container('512', 1,
'instantbox/ubuntu:latest',
time.time())
test.get_container_ports(container_name)
test.remove_timeout_containers()
test.is_rm_container(container_name)
65 changes: 0 additions & 65 deletions api/killContainer.py

This file was deleted.

49 changes: 0 additions & 49 deletions api/redisCli.py

This file was deleted.

22 changes: 0 additions & 22 deletions api/rmContainer.py

This file was deleted.

23 changes: 17 additions & 6 deletions docker-compose.yml
Expand Up @@ -4,16 +4,12 @@ services:
server: server:
image: instantbox/instantbox:latest image: instantbox/instantbox:latest
container_name: instantbox_server container_name: instantbox_server
links:
- redis
volumes: volumes:
- /var/run/docker.sock:/var/run/docker.sock - /var/run/docker.sock:/var/run/docker.sock
environment: environment:
- SERVERURL= - SERVERURL=

networks:
redis: - instantbox_net
image: redis:latest
container_name: instantbox_redis


frontend: frontend:
image: instantbox/instantbox-frontend:latest image: instantbox/instantbox-frontend:latest
Expand All @@ -22,3 +18,18 @@ services:
- server - server
ports: ports:
- 8888:80 - 8888:80
networks:
- instantbox_net

cron:
image: xordiv/docker-alpine-cron
container_name: instantbox_cron
links:
- frontend
environment:
- CRON_STRINGS=* * * * * wget -qO /dev/null http://frontend/api/v2/superinspire/prune
networks:
- instantbox_net

networks:
instantbox_net:

0 comments on commit adb12f0

Please sign in to comment.