191 lines
7.2 KiB
Python
191 lines
7.2 KiB
Python
#!/usr/bin/env python
|
|
# coding: utf-8
|
|
|
|
import time
|
|
from SWSCloudNode.settings import settings
|
|
from SWSCloudNode.logger import logging
|
|
from SWSCloudNode.network import Detect
|
|
from SWSCloudNode import Node
|
|
from SWSCloudNode import Tasks
|
|
from SWSCloudNode import lxc, qemu
|
|
|
|
allowed_actions = [
|
|
'container_delete',
|
|
'container_create',
|
|
'container_start',
|
|
'container_stop',
|
|
'container_restart',
|
|
|
|
'vm_create',
|
|
'vm_delete',
|
|
'vm_start',
|
|
'vm_stop',
|
|
'vm_restart',
|
|
]
|
|
|
|
logging.debug("Application started")
|
|
|
|
while True:
|
|
time.sleep(settings.getfloat('node', 'sleep'))
|
|
|
|
nodeclient = Node()
|
|
task_data = Tasks().get_item()
|
|
|
|
if task_data is not None and 'task' in task_data:
|
|
if task_data['task']['task'] not in allowed_actions:
|
|
logging.critical("Task not allowed: %s" % task_data['task']['task'])
|
|
continue
|
|
|
|
task = task_data['task']
|
|
|
|
# container_create
|
|
if task['task'] == 'container_create':
|
|
# TODO: take from task
|
|
template = 'ubuntu'
|
|
|
|
# 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']
|
|
|
|
# автоматически определяем подходящий сетевой интерфейс исходя из имеющегося ipv4
|
|
interface = Detect().get_suitable_interface(ipv4)
|
|
# interface = settings.get('node', 'interface')
|
|
nodeclient.container_config_create(container_id, 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']:
|
|
nodeclient.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(settings.get('node', 'packages'))
|
|
|
|
# create container
|
|
lxc.lxc().create(
|
|
name=container_id,
|
|
config_file=container_config_file,
|
|
template='ubuntu',
|
|
backing_store=None,
|
|
template_options=' '.join([
|
|
param_auth_key,
|
|
'--user',
|
|
task['plain']['username'],
|
|
'--password',
|
|
task['plain']['password'],
|
|
param_packages]
|
|
)
|
|
# template_options=""" %s --user %s --password %s %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['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']
|
|
# TODO: if container doesn't exists then complete task and report about this fact
|
|
try:
|
|
lxc.lxc().destroy(container_id)
|
|
except Exception as e:
|
|
logging.warning(e)
|
|
pass
|
|
nodeclient.task_status_update(task['id'], 2)
|
|
|
|
# Virtual machines operations
|
|
# Create new virtual machine
|
|
if task.get('task') == 'vm_create':
|
|
nodeclient.task_status_update(task['id'], 1)
|
|
|
|
vm_id = task['plain']['vm_id']
|
|
|
|
# TODO: if container doesn't exists then complete task and report about this fact
|
|
p = task['plain']
|
|
|
|
# автоматически определяем подходящий сетевой интерфейс исходя из имеющегося ipv4
|
|
|
|
interface = Detect().get_suitable_interface(p['ipv4'])
|
|
if not interface:
|
|
interface = settings.get('node', 'interface')
|
|
|
|
try:
|
|
qemu.QEMU().create(
|
|
p['cores'], p['memory'], p['storage'], p['swap'], p['hostname'],
|
|
p['ipv4'], p['ipv4_gateway'], p['dns1'], p['dns2'], p['password'],
|
|
p['os_name'], p['os_suite'], interface,
|
|
p['vm_id'],
|
|
p['ssh_key'],
|
|
)
|
|
# Start virtual server after creation
|
|
qemu.QEMU().start(task['plain']['vm_id'])
|
|
except Exception as e:
|
|
# back to origin task status
|
|
nodeclient.task_status_update(task['id'], 0)
|
|
logging.warning(e)
|
|
else:
|
|
nodeclient.task_status_update(task['id'], 2)
|
|
# Start virtual machine
|
|
if task.get('task') == 'vm_start':
|
|
nodeclient.task_status_update(task['id'], 1)
|
|
qemu.QEMU().start(task['plain']['vm_id'])
|
|
nodeclient.task_status_update(task['id'], 2)
|
|
pass
|
|
# Restart virtual machine
|
|
if task.get('task') == 'vm_restart':
|
|
nodeclient.task_status_update(task['id'], 1)
|
|
qemu.QEMU().restart(task['plain']['vm_id'])
|
|
nodeclient.task_status_update(task['id'], 2)
|
|
pass
|
|
if task.get('task') == 'vm_stop':
|
|
nodeclient.task_status_update(task['id'], 1)
|
|
qemu.QEMU().stop(task['plain']['vm_id'])
|
|
nodeclient.task_status_update(task['id'], 2)
|
|
pass
|
|
if task.get('task') == 'vm_delete':
|
|
nodeclient.task_status_update(task['id'], 1)
|
|
qemu.QEMU().delete(task['plain']['vm_id'])
|
|
nodeclient.task_status_update(task['id'], 2)
|
|
pass
|
|
|
|
logging.debug("Application ended")
|