From 568f38dd6794c052d3109a35003d84842b9c4eac Mon Sep 17 00:00:00 2001 From: PCoder Date: Mon, 28 Aug 2017 12:25:39 +0530 Subject: [PATCH] Added utils/tasks.py --- utils/tasks.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 utils/tasks.py diff --git a/utils/tasks.py b/utils/tasks.py new file mode 100644 index 00000000..692e9e42 --- /dev/null +++ b/utils/tasks.py @@ -0,0 +1,31 @@ +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 + +logger = get_task_logger(__name__) + + +@app.task(bind=True, max_retries=settings.CELERY_MAX_RETRIES) +def save_ssh_key(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 + with tempfile.NamedTemporaryFile() as tmp_manifest: + tmp_manifest.writelines(['__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) + +