118 lines
3.9 KiB
Python
118 lines
3.9 KiB
Python
# coding: utf-8
|
|
|
|
import sys
|
|
import json
|
|
import requests
|
|
from SWSCloudNode.settings import settings
|
|
|
|
|
|
class Node(object):
|
|
def tasks_get(self):
|
|
try:
|
|
response = requests.get(
|
|
'%s/server_api/tasks' % settings.get('server', 'endpoint'),
|
|
auth=(settings.get('server', 'id'), settings.get('server', 'secret')))
|
|
except Exception as e:
|
|
sys.exit('no connection with %s' % settings.get('server', 'endpoint'))
|
|
else:
|
|
return dict(status=response.status_code, results=response.json())
|
|
|
|
def task_status_update(self, task_id, status):
|
|
response = requests.put(
|
|
'%s/server_api/tasks/%s' % (
|
|
settings.get('server', 'endpoint'),
|
|
task_id
|
|
),
|
|
auth=(
|
|
settings.get('server', 'id'),
|
|
settings.get('server', 'secret'),
|
|
),
|
|
data={
|
|
"status": status
|
|
}
|
|
)
|
|
return response.json()
|
|
|
|
def report_container_stats(self, container_id, statistics):
|
|
response = requests.post(
|
|
'%s/server_api/containers/stats/%s' % (
|
|
settings.get('server', 'endpoint'),
|
|
container_id
|
|
),
|
|
auth=(settings.get('server', 'id'), settings.get('server', 'secret')),
|
|
data={
|
|
'status': json.dumps(statistics)
|
|
}
|
|
)
|
|
if response.status_code == 200:
|
|
return response.json()
|
|
return False
|
|
|
|
# TODO: подумать куда переместить
|
|
def container_config_create(self, container_id, link, ipv4, ipv6):
|
|
"""
|
|
|
|
:param container_id:
|
|
:param link:
|
|
:param ipv4:
|
|
:param ipv6:
|
|
:return:
|
|
"""
|
|
cfg = list()
|
|
cfg.append("lxc.network.type = veth")
|
|
cfg.append("lxc.network.flags = up")
|
|
cfg.append("lxc.network.name = eth0")
|
|
cfg.append("lxc.network.link = %s" % link)
|
|
|
|
if ipv4['ipv4']:
|
|
# cfg.append('lxc.network.ipv4 = %s/32' % ipv4['ipv4'])
|
|
cfg.append('lxc.network.ipv4 = %s' % ipv4['ipv4'])
|
|
cfg.append('lxc.network.ipv4.gateway = %s' % ipv4['ipv4_gateway'])
|
|
|
|
if 'ipv6' in ipv6 and 'ipv6_gateway' in ipv6:
|
|
# cfg.append('lxc.network.ipv6 = %s/64' % ipv6['ipv6'])
|
|
cfg.append('lxc.network.ipv6 = %s' % ipv6['ipv6'])
|
|
cfg.append('lxc.network.ipv6.gateway = %s', ipv6['ipv6_gateway'])
|
|
|
|
config_file = '/var/lib/gocloud/node/configs/%s.config' % container_id
|
|
|
|
cfg_file = open(config_file, 'w')
|
|
cfg_file.write('\n'.join(cfg))
|
|
cfg_file.write('\n')
|
|
cfg_file.close()
|
|
return True
|
|
|
|
def container_authkey_create(self, container_id, auth_key):
|
|
# create ssh_key.pub
|
|
authkey_file = '/var/lib/gocloud/node/auth-keys/%s.pub' % container_id
|
|
ak = open(authkey_file, 'w')
|
|
ak.write(auth_key)
|
|
ak.write('\n')
|
|
ak.close()
|
|
return True
|
|
|
|
|
|
class StatisticsReporter(object):
|
|
@staticmethod
|
|
def send_vm_statistics(vm_id, data):
|
|
response = requests.post(
|
|
'%s/stats/v1/compute/vms/%s' % (
|
|
settings.get('statistics', 'endpoint'), vm_id),
|
|
# TODO: node auth
|
|
# auth=(settings.get('server', 'id'), settings.get('server', 'secret')),
|
|
json=data)
|
|
if response.status_code == 200:
|
|
return response.json()
|
|
return False
|
|
|
|
@staticmethod
|
|
def send_containers_statistics(vm_id, data):
|
|
response = requests.post(
|
|
'%s/stats/v1/compute/containers/%s' % (
|
|
settings.get('statistics', 'endpoint'), vm_id),
|
|
# TODO: node auth
|
|
# auth=(settings.get('server', 'id'), settings.get('server', 'secret')),
|
|
json=data)
|
|
if response.status_code == 200:
|
|
return response.json()
|
|
return False
|