Commit 23ed986c by Bach Dániel

vm: add agent.update task

parent 3bcecbcd
...@@ -18,8 +18,10 @@ ...@@ -18,8 +18,10 @@
"""Common settings and globals.""" """Common settings and globals."""
# flake8: noqa # flake8: noqa
from os import environ from os import environ
from os.path import abspath, basename, dirname, join, normpath, isfile from os.path import (abspath, basename, dirname, join, normpath, isfile,
expanduser)
from sys import path from sys import path
from subprocess import check_output
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
...@@ -418,6 +420,16 @@ if get_env_variable('DJANGO_SAML', 'FALSE') == 'TRUE': ...@@ -418,6 +420,16 @@ if get_env_variable('DJANGO_SAML', 'FALSE') == 'TRUE':
LOGIN_REDIRECT_URL = "/" LOGIN_REDIRECT_URL = "/"
AGENT_DIR = get_env_variable(
'DJANGO_AGENT_DIR', join(unicode(expanduser("~")), 'agent'))
try:
git_env = {'GIT_DIR': join(AGENT_DIR, '.git')}
AGENT_VERSION = check_output(
('git', 'log', '-1', r'--pretty=format:%h', 'HEAD'), env=git_env)
except:
AGENT_VERSION = None
LOCALE_PATHS = (join(SITE_ROOT, 'locale'), ) LOCALE_PATHS = (join(SITE_ROOT, 'locale'), )
COMPANY_NAME = "BME IK 2014" COMPANY_NAME = "BME IK 2014"
SOUTH_MIGRATION_MODULES = { SOUTH_MIGRATION_MODULES = {
......
...@@ -51,3 +51,8 @@ def cleanup(vm): ...@@ -51,3 +51,8 @@ def cleanup(vm):
@celery.task(name='agent.start_access_server') @celery.task(name='agent.start_access_server')
def start_access_server(vm): def start_access_server(vm):
pass pass
@celery.task(name='agent.update')
def update(vm, data):
pass
...@@ -18,8 +18,13 @@ ...@@ -18,8 +18,13 @@
from manager.mancelery import celery from manager.mancelery import celery
from vm.tasks.agent_tasks import (restart_networking, change_password, from vm.tasks.agent_tasks import (restart_networking, change_password,
set_time, set_hostname, start_access_server, set_time, set_hostname, start_access_server,
cleanup) cleanup, update)
import time import time
from base64 import encodestring
from StringIO import StringIO
from tarfile import TarFile, TarInfo
from django.conf import settings
from celery.result import TimeoutError
def send_init_commands(instance, act, vm): def send_init_commands(instance, act, vm):
...@@ -38,22 +43,53 @@ def send_init_commands(instance, act, vm): ...@@ -38,22 +43,53 @@ def send_init_commands(instance, act, vm):
queue=queue, args=(vm, instance.primary_host.hostname)) queue=queue, args=(vm, instance.primary_host.hostname))
def create_agent_tar():
def exclude(tarinfo):
if tarinfo.name.startswith('./.git'):
return None
else:
return tarinfo
f = StringIO()
with TarFile.open(fileobj=f, mode='w|gz') as tar:
tar.add(settings.AGENT_DIR, arcname='.', filter=exclude)
version_fileobj = StringIO(settings.AGENT_VERSION)
version_info = TarInfo(name='version.txt')
version_info.size = len(version_fileobj.buf)
tar.addfile(version_info, version_fileobj)
return encodestring(f.getvalue()).replace('\n', '')
@celery.task @celery.task
def agent_started(vm): def agent_started(vm, version=None):
from vm.models import Instance, instance_activity, InstanceActivity from vm.models import Instance, instance_activity, InstanceActivity
instance = Instance.objects.get(id=int(vm.split('-')[-1])) instance = Instance.objects.get(id=int(vm.split('-')[-1]))
queue = instance.get_remote_queue_name("agent")
initialized = InstanceActivity.objects.filter( initialized = InstanceActivity.objects.filter(
instance=instance, activity_code='vm.Instance.agent').exists() instance=instance, activity_code='vm.Instance.agent.cleanup').exists()
with instance_activity(code_suffix='agent', instance=instance) as act: with instance_activity(code_suffix='agent', instance=instance) as act:
with act.sub_activity('starting'): with act.sub_activity('starting'):
pass pass
if version and version != settings.AGENT_VERSION:
try:
with act.sub_activity('update'):
update.apply_async(
queue=queue,
args=(vm, create_agent_tar())).get(timeout=10)
return
except TimeoutError:
pass
if not initialized: if not initialized:
send_init_commands(instance, act, vm) send_init_commands(instance, act, vm)
with act.sub_activity('start_access_server'): with act.sub_activity('start_access_server'):
queue = instance.get_remote_queue_name("agent") start_access_server.apply_async(queue=queue, args=(vm, ))
start_access_server.apply_async(
queue=queue, args=(vm, ))
@celery.task @celery.task
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment