90 lines
3 KiB
Python
90 lines
3 KiB
Python
# coding: utf-8
|
|
|
|
import requests
|
|
import config
|
|
import ConfigParser
|
|
import lxc
|
|
import node
|
|
|
|
nodeclient = node.NodeClient()
|
|
tasks = nodeclient.tasks_get()
|
|
|
|
for task in tasks['results']:
|
|
# container_create
|
|
print task
|
|
print '---------------------'
|
|
|
|
if task['task'] == 'container_create':
|
|
# TODO: update task status to 1
|
|
nodeclient.task_status_update(task['id'], 1)
|
|
|
|
container_id = task['plain']['container_id']
|
|
ipv4 = dict()
|
|
ipv6 = dict()
|
|
if task['plain']['ipv4']:
|
|
ipv4['ipv4'] = task['plain']['ipv4']
|
|
ipv4['ipv4_gateway'] = task['plain']['ipv4_gateway']
|
|
if 'ipv6' in task['plain'] and task['plain']['ipv6'] != '' and 'ipv6_gateway' in task['plain']:
|
|
ipv6['ipv6'] = task['plain']['ipv6']
|
|
ipv6['ipv6_gateway'] = task['plain']['ipv6_gateway']
|
|
|
|
node.__container_config_create(container_id, config.interface, ipv4, ipv6)
|
|
|
|
container_config_file = '/var/lib/gocloud/node/configs/%s.config' % container_id
|
|
|
|
# create ssh_key.pub
|
|
param_auth_key = ''
|
|
if 'ssh_key' in task['plain'] and task['plain']['ssh_key'] != '':
|
|
node.__container_authkey_create(container_id, task['plain']['ssh_key'])
|
|
param_auth_key = '--auth-key /var/lib/gocloud/node/auth-keys/%s.pub' % container_id
|
|
|
|
param_packages = ''
|
|
if template == 'ubuntu':
|
|
param_packages = '--packages %s' % ','.join(config.packages['ubuntu'])
|
|
|
|
# create container
|
|
lxc.lxc().create(
|
|
container_id,
|
|
container_config_file,
|
|
'ubuntu',
|
|
None,
|
|
"%s --user %s --password %s" % (
|
|
param_auth_key,
|
|
task['plain']['username'],
|
|
task['plain']['password'],
|
|
param_packages
|
|
)
|
|
)
|
|
lxc.lxc().start(container_id)
|
|
|
|
# TODO: update task status to 2
|
|
nodeclient.task_status_update(task['id'], 2)
|
|
|
|
# container_start
|
|
if task['task'] == 'container_start':
|
|
nodeclient.task_status_update(task['id'], 1)
|
|
container_id = task['plain']['container_id']
|
|
lxc.lxc().start(container_id)
|
|
nodeclient.task_status_update(task['id'], 2)
|
|
|
|
# container_restart
|
|
if task['task'] == 'container_restart':
|
|
nodeclient.task_status_update(task['id'], 1)
|
|
container_id = task['plain']['container_id']
|
|
lxc.lxc().stop(container_id)
|
|
lxc.lxc().start(container_id)
|
|
nodeclient.task_status_update(task['id'], 2)
|
|
|
|
# container_stop
|
|
if task['task'] == 'container_stop':
|
|
nodeclient.task_status_update(task['id'], 1)
|
|
container_id = task['plain']['container_id']
|
|
lxc.lxc().stop(container_id)
|
|
nodeclient.task_status_update(task['id'], 2)
|
|
|
|
# container_delete
|
|
if task['task'] == 'container_delete':
|
|
nodeclient.task_status_update(task['id'], 1)
|
|
container_id = task['plain']['container_id']
|
|
lxc.lxc().destroy(container_id)
|
|
nodeclient.task_status_update(task['id'], 2)
|