Merge branch 'develop'
This commit is contained in:
commit
f683840980
4 changed files with 43 additions and 42 deletions
|
@ -71,7 +71,7 @@ class LXC(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
logging.debug("")
|
logging.debug("")
|
||||||
|
|
||||||
def list(self, status=None):
|
def list_containers(self, status=None):
|
||||||
"""
|
"""
|
||||||
:return: ['container_first', 'container_second']
|
:return: ['container_first', 'container_second']
|
||||||
"""
|
"""
|
||||||
|
@ -89,7 +89,7 @@ class LXC(object):
|
||||||
"""
|
"""
|
||||||
checks if a given container is defined or not
|
checks if a given container is defined or not
|
||||||
"""
|
"""
|
||||||
if name in self.list():
|
if name in self.list_containers():
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -100,7 +100,7 @@ class LXC(object):
|
||||||
if not self.exists(name):
|
if not self.exists(name):
|
||||||
raise ContainerNotExists("The container (%s) does not exist!" % name)
|
raise ContainerNotExists("The container (%s) does not exist!" % name)
|
||||||
|
|
||||||
if name in self.list("running"):
|
if name in self.list_containers("running"):
|
||||||
raise ContainerAlreadyRunning('The container %s is already started!' % name)
|
raise ContainerAlreadyRunning('The container %s is already started!' % name)
|
||||||
|
|
||||||
cmd = ['lxc-start', '-n', name, '-d']
|
cmd = ['lxc-start', '-n', name, '-d']
|
||||||
|
|
|
@ -7,7 +7,7 @@ from SWSCloudNode.logger import logging
|
||||||
from SWSCloudNode.network import Detect
|
from SWSCloudNode.network import Detect
|
||||||
from SWSCloudNode import Node
|
from SWSCloudNode import Node
|
||||||
from SWSCloudNode import Tasks
|
from SWSCloudNode import Tasks
|
||||||
from SWSCloudNode import lxc, qemu
|
from SWSCloudNode.compute import lxc, qemu
|
||||||
|
|
||||||
allowed_actions = [
|
allowed_actions = [
|
||||||
'container_delete',
|
'container_delete',
|
||||||
|
@ -28,7 +28,7 @@ logging.debug("Application started")
|
||||||
while True:
|
while True:
|
||||||
time.sleep(settings.getfloat('node', 'sleep'))
|
time.sleep(settings.getfloat('node', 'sleep'))
|
||||||
|
|
||||||
nodeclient = Node()
|
node_client = Node()
|
||||||
task_data = Tasks().get_item()
|
task_data = Tasks().get_item()
|
||||||
|
|
||||||
if task_data is not None and 'task' in task_data:
|
if task_data is not None and 'task' in task_data:
|
||||||
|
@ -44,7 +44,7 @@ while True:
|
||||||
template = 'ubuntu'
|
template = 'ubuntu'
|
||||||
|
|
||||||
# TODO: update task status to 1
|
# TODO: update task status to 1
|
||||||
nodeclient.task_status_update(task['id'], 1)
|
node_client.task_status_update(task['id'], 1)
|
||||||
|
|
||||||
container_id = task['plain']['container_id']
|
container_id = task['plain']['container_id']
|
||||||
ipv4 = dict()
|
ipv4 = dict()
|
||||||
|
@ -59,14 +59,14 @@ while True:
|
||||||
# автоматически определяем подходящий сетевой интерфейс исходя из имеющегося ipv4
|
# автоматически определяем подходящий сетевой интерфейс исходя из имеющегося ipv4
|
||||||
interface = Detect().get_suitable_interface(ipv4)
|
interface = Detect().get_suitable_interface(ipv4)
|
||||||
# interface = settings.get('node', 'interface')
|
# interface = settings.get('node', 'interface')
|
||||||
nodeclient.container_config_create(container_id, interface, ipv4, ipv6)
|
node_client.container_config_create(container_id, interface, ipv4, ipv6)
|
||||||
|
|
||||||
container_config_file = '/var/lib/gocloud/node/configs/%s.config' % container_id
|
container_config_file = '/var/lib/gocloud/node/configs/%s.config' % container_id
|
||||||
|
|
||||||
# create ssh_key.pub
|
# create ssh_key.pub
|
||||||
param_auth_key = ''
|
param_auth_key = ''
|
||||||
if 'ssh_key' in task['plain'] and task['plain']['ssh_key']:
|
if 'ssh_key' in task['plain'] and task['plain']['ssh_key']:
|
||||||
nodeclient.container_authkey_create(container_id, task['plain']['ssh_key'])
|
node_client.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_auth_key = '--auth-key /var/lib/gocloud/node/auth-keys/%s.pub' % container_id
|
||||||
|
|
||||||
param_packages = ''
|
param_packages = ''
|
||||||
|
@ -74,7 +74,7 @@ while True:
|
||||||
param_packages = '--packages %s' % ','.join(settings.get('node', 'packages'))
|
param_packages = '--packages %s' % ','.join(settings.get('node', 'packages'))
|
||||||
|
|
||||||
# create container
|
# create container
|
||||||
lxc.lxc().create(
|
lxc.LXC().create(
|
||||||
name=container_id,
|
name=container_id,
|
||||||
config_file=container_config_file,
|
config_file=container_config_file,
|
||||||
template='ubuntu',
|
template='ubuntu',
|
||||||
|
@ -94,49 +94,49 @@ while True:
|
||||||
# param_packages
|
# param_packages
|
||||||
# )
|
# )
|
||||||
)
|
)
|
||||||
lxc.lxc().start(container_id)
|
lxc.LXC().start(container_id)
|
||||||
|
|
||||||
# TODO: update task status to 2
|
# TODO: update task status to 2
|
||||||
nodeclient.task_status_update(task['id'], 2)
|
node_client.task_status_update(task['id'], 2)
|
||||||
|
|
||||||
# container_start
|
# container_start
|
||||||
if task['task'] == 'container_start':
|
if task['task'] == 'container_start':
|
||||||
nodeclient.task_status_update(task['id'], 1)
|
node_client.task_status_update(task['id'], 1)
|
||||||
container_id = task['plain']['container_id']
|
container_id = task['plain']['container_id']
|
||||||
lxc.lxc().start(container_id)
|
lxc.LXC().start(container_id)
|
||||||
nodeclient.task_status_update(task['id'], 2)
|
node_client.task_status_update(task['id'], 2)
|
||||||
|
|
||||||
# container_restart
|
# container_restart
|
||||||
if task['task'] == 'container_restart':
|
if task['task'] == 'container_restart':
|
||||||
nodeclient.task_status_update(task['id'], 1)
|
node_client.task_status_update(task['id'], 1)
|
||||||
container_id = task['plain']['container_id']
|
container_id = task['plain']['container_id']
|
||||||
lxc.lxc().stop(container_id)
|
lxc.LXC().stop(container_id)
|
||||||
lxc.lxc().start(container_id)
|
lxc.LXC().start(container_id)
|
||||||
nodeclient.task_status_update(task['task']['id'], 2)
|
node_client.task_status_update(task['task']['id'], 2)
|
||||||
|
|
||||||
# container_stop
|
# container_stop
|
||||||
if task['task'] == 'container_stop':
|
if task['task'] == 'container_stop':
|
||||||
nodeclient.task_status_update(task['id'], 1)
|
node_client.task_status_update(task['id'], 1)
|
||||||
container_id = task['plain']['container_id']
|
container_id = task['plain']['container_id']
|
||||||
lxc.lxc().stop(container_id)
|
lxc.LXC().stop(container_id)
|
||||||
nodeclient.task_status_update(task['id'], 2)
|
node_client.task_status_update(task['id'], 2)
|
||||||
|
|
||||||
# container_delete
|
# container_delete
|
||||||
if task['task'] == 'container_delete':
|
if task['task'] == 'container_delete':
|
||||||
nodeclient.task_status_update(task['id'], 1)
|
node_client.task_status_update(task['id'], 1)
|
||||||
container_id = task['plain']['container_id']
|
container_id = task['plain']['container_id']
|
||||||
# TODO: if container doesn't exists then complete task and report about this fact
|
# TODO: if container doesn't exists then complete task and report about this fact
|
||||||
try:
|
try:
|
||||||
lxc.lxc().destroy(container_id)
|
lxc.LXC().destroy(container_id)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.warning(e)
|
logging.warning(e)
|
||||||
pass
|
pass
|
||||||
nodeclient.task_status_update(task['id'], 2)
|
node_client.task_status_update(task['id'], 2)
|
||||||
|
|
||||||
# Virtual machines operations
|
# Virtual machines operations
|
||||||
# Create new virtual machine
|
# Create new virtual machine
|
||||||
if task.get('task') == 'vm_create':
|
if task.get('task') == 'vm_create':
|
||||||
nodeclient.task_status_update(task['id'], 1)
|
node_client.task_status_update(task['id'], 1)
|
||||||
|
|
||||||
vm_id = task['plain']['vm_id']
|
vm_id = task['plain']['vm_id']
|
||||||
|
|
||||||
|
@ -150,7 +150,7 @@ while True:
|
||||||
interface = settings.get('node', 'interface')
|
interface = settings.get('node', 'interface')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
qemu.QEMU().create(
|
qemu.Qemu().create(
|
||||||
p['cores'], p['memory'], p['storage'], p['swap'], p['hostname'],
|
p['cores'], p['memory'], p['storage'], p['swap'], p['hostname'],
|
||||||
p['ipv4'], p['ipv4_gateway'], p['dns1'], p['dns2'], p['password'],
|
p['ipv4'], p['ipv4_gateway'], p['dns1'], p['dns2'], p['password'],
|
||||||
p['os_name'], p['os_suite'], interface,
|
p['os_name'], p['os_suite'], interface,
|
||||||
|
@ -158,34 +158,34 @@ while True:
|
||||||
p['ssh_key'],
|
p['ssh_key'],
|
||||||
)
|
)
|
||||||
# Start virtual server after creation
|
# Start virtual server after creation
|
||||||
qemu.QEMU().start(task['plain']['vm_id'])
|
qemu.Qemu().start(task['plain']['vm_id'])
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# back to origin task status
|
# back to origin task status
|
||||||
nodeclient.task_status_update(task['id'], 0)
|
node_client.task_status_update(task['id'], 0)
|
||||||
logging.warning(e)
|
logging.warning(e)
|
||||||
else:
|
else:
|
||||||
nodeclient.task_status_update(task['id'], 2)
|
node_client.task_status_update(task['id'], 2)
|
||||||
# Start virtual machine
|
# Start virtual machine
|
||||||
if task.get('task') == 'vm_start':
|
if task.get('task') == 'vm_start':
|
||||||
nodeclient.task_status_update(task['id'], 1)
|
node_client.task_status_update(task['id'], 1)
|
||||||
qemu.QEMU().start(task['plain']['vm_id'])
|
qemu.Qemu().start(task['plain']['vm_id'])
|
||||||
nodeclient.task_status_update(task['id'], 2)
|
node_client.task_status_update(task['id'], 2)
|
||||||
pass
|
pass
|
||||||
# Restart virtual machine
|
# Restart virtual machine
|
||||||
if task.get('task') == 'vm_restart':
|
if task.get('task') == 'vm_restart':
|
||||||
nodeclient.task_status_update(task['id'], 1)
|
node_client.task_status_update(task['id'], 1)
|
||||||
qemu.QEMU().restart(task['plain']['vm_id'])
|
qemu.Qemu().restart(task['plain']['vm_id'])
|
||||||
nodeclient.task_status_update(task['id'], 2)
|
node_client.task_status_update(task['id'], 2)
|
||||||
pass
|
pass
|
||||||
if task.get('task') == 'vm_stop':
|
if task.get('task') == 'vm_stop':
|
||||||
nodeclient.task_status_update(task['id'], 1)
|
node_client.task_status_update(task['id'], 1)
|
||||||
qemu.QEMU().stop(task['plain']['vm_id'])
|
qemu.Qemu().stop(task['plain']['vm_id'])
|
||||||
nodeclient.task_status_update(task['id'], 2)
|
node_client.task_status_update(task['id'], 2)
|
||||||
pass
|
pass
|
||||||
if task.get('task') == 'vm_delete':
|
if task.get('task') == 'vm_delete':
|
||||||
nodeclient.task_status_update(task['id'], 1)
|
node_client.task_status_update(task['id'], 1)
|
||||||
qemu.QEMU().delete(task['plain']['vm_id'])
|
qemu.Qemu().delete(task['plain']['vm_id'])
|
||||||
nodeclient.task_status_update(task['id'], 2)
|
node_client.task_status_update(task['id'], 2)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
logging.debug("Application ended")
|
logging.debug("Application ended")
|
||||||
|
|
|
@ -11,7 +11,7 @@ cls_lxc = LXC()
|
||||||
cls_qemu = Qemu()
|
cls_qemu = Qemu()
|
||||||
cls_node = Node()
|
cls_node = Node()
|
||||||
|
|
||||||
for container in cls_lxc.list():
|
for container in cls_lxc.list_containers():
|
||||||
# print container
|
# print container
|
||||||
info = cls_lxc.info(container)
|
info = cls_lxc.info(container)
|
||||||
info['container_id'] = info['name']
|
info['container_id'] = info['name']
|
||||||
|
|
1
setup.py
1
setup.py
|
@ -9,6 +9,7 @@ setup(
|
||||||
author_email='vanzhiganov@ya.ru',
|
author_email='vanzhiganov@ya.ru',
|
||||||
packages=[
|
packages=[
|
||||||
'SWSCloudNode',
|
'SWSCloudNode',
|
||||||
|
'SWSCloudNode.compute',
|
||||||
'SWSCloudNode.compute.lxc',
|
'SWSCloudNode.compute.lxc',
|
||||||
'SWSCloudNode.compute.qemu',
|
'SWSCloudNode.compute.qemu',
|
||||||
],
|
],
|
||||||
|
|
Loading…
Add table
Reference in a new issue