Not passing index to cdist configure

This commit is contained in:
M.Ravi 2017-08-31 18:57:05 +02:00
parent 75a778e65f
commit ab31ad37d3
3 changed files with 9 additions and 46 deletions

View file

@ -7,7 +7,6 @@ from oca.exceptions import OpenNebulaException
from oca.pool import WrongNameError, WrongIdError from oca.pool import WrongNameError, WrongIdError
from hosting.models import HostingOrder from hosting.models import HostingOrder
from utils.cdist_utils import CdistUtilts
from utils.models import CustomUser from utils.models import CustomUser
from utils.tasks import save_ssh_key from utils.tasks import save_ssh_key
from .exceptions import KeyExistsError, UserExistsError, UserCredentialError from .exceptions import KeyExistsError, UserExistsError, UserCredentialError
@ -554,9 +553,7 @@ class OpenNebulaManager():
hosts = self.get_all_hosts() hosts = self.get_all_hosts()
if len(hosts) > 0 and len(keys) > 0: if len(hosts) > 0 and len(keys) > 0:
save_ssh_key.apply_async( save_ssh_key.apply_async((hosts, keys), countdown=countdown)
(hosts, keys, CdistUtilts.get_cdist_index()),
countdown=countdown)
else: else:
logger.debug("Keys and hosts are empty, so not managing any keys") logger.debug("Keys and hosts are empty, so not managing any keys")

View file

@ -1,26 +0,0 @@
import cdist.integration as cdist_integration
class CdistUtilts():
@staticmethod
def get_cdist_index():
"""
Returns the next available instance index.
This is useful while making simultaneous configurations of
the same host.
:return: the next available index
"""
cdist_instance_index = cdist_integration.instance_index
cdist_index = next(cdist_instance_index)
return cdist_index
@staticmethod
def free_cdist_index(cdist_index):
"""
Frees up the index that was used during configuring a host
using cdist.
:param cdist_index: The index to be freed
:return:
"""
cdist_instance_index = cdist_integration.instance_index
cdist_instance_index.free(cdist_index)

View file

@ -6,7 +6,6 @@ from django.conf import settings
from django.core.mail import EmailMessage from django.core.mail import EmailMessage
from dynamicweb.celery import app from dynamicweb.celery import app
from utils.cdist_utils import CdistUtilts
logger = get_task_logger(__name__) logger = get_task_logger(__name__)
@ -26,7 +25,7 @@ def send_plain_email_task(self, email_data):
@app.task(bind=True, max_retries=settings.CELERY_MAX_RETRIES) @app.task(bind=True, max_retries=settings.CELERY_MAX_RETRIES)
def save_ssh_key(self, hosts, keys, index): def save_ssh_key(self, hosts, keys):
""" """
Saves ssh key into the VMs of a user using cdist Saves ssh key into the VMs of a user using cdist
@ -36,21 +35,16 @@ def save_ssh_key(self, hosts, keys, index):
'value': 'sha-.....', # public key as string 'value': 'sha-.....', # public key as string
'state': True # whether key is to be added or 'state': True # whether key is to be added or
} # removed } # removed
:param index: An integer that uniquely identifies simultaneous cdist
configurations being run on a host
""" """
logger.debug("""Running save_ssh_key task for logger.debug("""Running save_ssh_key task for
Hosts: {hosts_str} Hosts: {hosts_str}
Keys: {keys_str} Keys: {keys_str}""".format(hosts_str=", ".join(hosts),
index: {index}""".format(hosts_str=", ".join(hosts),
keys_str=", ".join([ keys_str=", ".join([
"{value}->{state}".format( "{value}->{state}".format(
value=key.get('value'), value=key.get('value'),
state=str( state=str(
key.get('state'))) key.get('state')))
for key in keys]), for key in keys]))
index=index)
) )
return_value = True return_value = True
with tempfile.NamedTemporaryFile(delete=True) as tmp_manifest: with tempfile.NamedTemporaryFile(delete=True) as tmp_manifest:
@ -67,10 +61,8 @@ def save_ssh_key(self, hosts, keys, index):
try: try:
configure_hosts_simple(hosts, configure_hosts_simple(hosts,
tmp_manifest.name, tmp_manifest.name,
index=index,
verbose=cdist.argparse.VERBOSE_TRACE) verbose=cdist.argparse.VERBOSE_TRACE)
except Exception as cdist_exception: except Exception as cdist_exception:
logger.error(cdist_exception) logger.error(cdist_exception)
return_value = False return_value = False
CdistUtilts.free_cdist_index(index)
return return_value return return_value