From 21f51692c48de1dc93784d32877c2e859581b89c Mon Sep 17 00:00:00 2001 From: "M.Ravi" Date: Mon, 28 Aug 2017 11:40:59 +0200 Subject: [PATCH] Improved save_ssh_key celery task --- requirements.txt | 3 ++- utils/tasks.py | 34 ++++++++++++++++++---------------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/requirements.txt b/requirements.txt index 33bd51fa..639ac881 100644 --- a/requirements.txt +++ b/requirements.txt @@ -96,4 +96,5 @@ pyflakes==1.5.0 billiard==3.5.0.3 amqp==2.2.1 vine==1.1.4 -file:///home/test/projects/Nico/cdist-pcoder/cdist/#egg=cdist-web +#git+https://github.com/pcoder/cdist.git#egg=cdist-web +file:///home/mravi/gitprojects/cdist-pcoder/cdist/#egg=cdist-web diff --git a/utils/tasks.py b/utils/tasks.py index 692e9e42..7e015df7 100644 --- a/utils/tasks.py +++ b/utils/tasks.py @@ -1,31 +1,33 @@ -from dynamicweb.celery import app -from celery.utils.log import get_task_logger -from django.conf import settings -from cdist.integration import configure_hosts_simple import cdist import tempfile -import pathlib +from cdist.integration import configure_hosts_simple +from celery.utils.log import get_task_logger +from django.conf import settings + +from dynamicweb.celery import app logger = get_task_logger(__name__) @app.task(bind=True, max_retries=settings.CELERY_MAX_RETRIES) -def save_ssh_key(hosts, keys): +def save_ssh_key(self, hosts, keys): """ Saves ssh key into the VMs of a user using cdist :param hosts: A list of hosts to be configured :param keys: A list of keys to be added """ - # Generate manifest to be used for configuring the hosts + return_value = True with tempfile.NamedTemporaryFile() as tmp_manifest: - tmp_manifest.writelines(['__ssh_authorized_keys root \\', + # Generate manifest to be used for configuring the hosts + tmp_manifest.writelines([b'__ssh_authorized_keys root \\', ' --key "{keys}"'.format( - keys='\n'.join(keys))]) - - f = pathlib.Path(tmp_manifest.name) - configure_hosts_simple(hosts, - tmp_manifest.name, - verbose=cdist.argparse.VERBOSE_TRACE) - - + keys='\n'.join(keys)).encode('utf-8')]) + try: + configure_hosts_simple(hosts, + tmp_manifest.name, + verbose=cdist.argparse.VERBOSE_TRACE) + except Exception as cdist_exception: + logger.error(cdist_exception) + return_value = False + return return_value