Skip to content
Permalink
Browse files

feat: add cron prune and refactor api

  • Loading branch information...
wei committed Mar 3, 2019
1 parent 1e52c0b commit adb12f08aa2fdf075cd7e8790eb97b8895a0c6b7
Showing with 243 additions and 341 deletions.
  1. +6 −1 .gitignore
  2. +0 −47 api/createContainer.py
  3. +106 −0 api/instantboxManager.py
  4. +0 −65 api/killContainer.py
  5. +0 −49 api/redisCli.py
  6. +0 −22 api/rmContainer.py
  7. +17 −6 docker-compose.yml
  8. +113 −149 inspire.py
  9. +1 −2 requirement.txt
@@ -1 +1,6 @@
.DS_Store
.DS_Store
__pycache__/
*.py[cod]
*$py.class

manifest.json

This file was deleted.

Oops, something went wrong.
@@ -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)

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
@@ -4,16 +4,12 @@ services:
server:
image: instantbox/instantbox:latest
container_name: instantbox_server
links:
- redis
volumes:
- /var/run/docker.sock:/var/run/docker.sock
environment:
- SERVERURL=

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

frontend:
image: instantbox/instantbox-frontend:latest
@@ -22,3 +18,18 @@ services:
- server
ports:
- 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:
Oops, something went wrong.

0 comments on commit adb12f0

Please sign in to comment.
You can’t perform that action at this time.