From 522e6c1167a4856b9ce04f3c64d1d2b0f6027895 Mon Sep 17 00:00:00 2001 From: Yichen Wang Date: Tue, 14 Jul 2015 21:31:17 -0700 Subject: [PATCH] Fix issues while parsing credential files from CLI Change-Id: I7a53a2cc87e015afbba11eb8d23bf2afbd1c0d7f --- scale/cfg.topo.yaml | 3 + scale/credentials.py | 5 +- .../kb_server/kb_server/controllers/config.py | 78 +++++++++---------- 3 files changed, 44 insertions(+), 42 deletions(-) diff --git a/scale/cfg.topo.yaml b/scale/cfg.topo.yaml index a5aa24d..73c6876 100644 --- a/scale/cfg.topo.yaml +++ b/scale/cfg.topo.yaml @@ -1,5 +1,8 @@ # Compute host topology file for running KloudBuster +# The compute host name must be exactly the same as shown from NOVA: +# i.e. "nova hypervisor-list" + servers_rack: hh23-5 diff --git a/scale/credentials.py b/scale/credentials.py index 1dff9aa..9908d02 100644 --- a/scale/credentials.py +++ b/scale/credentials.py @@ -48,11 +48,11 @@ class Credentials(object): def _init_with_openrc_(self, openrc_contents): export_re = re.compile('export OS_([A-Z_]*)="?(.*)') - for line in openrc_contents: + for line in openrc_contents.splitlines(): line = line.strip() mstr = export_re.match(line) if mstr: - # get rif of posible trailing double quote + # get rid of posible trailing double quote # the first one was removed by the re name = mstr.group(1) value = mstr.group(2) @@ -85,7 +85,6 @@ class Credentials(object): if openrc_file: if os.path.exists(openrc_file): self.openrc_contents = open(openrc_file).read() - self._init_with_openrc_(self.openrc_contents) else: LOG.error("rc file does not exist %s" % openrc_file) success = False diff --git a/scale/kb_server/kb_server/controllers/config.py b/scale/kb_server/kb_server/controllers/config.py index 1bd44b1..f0f8332 100644 --- a/scale/kb_server/kb_server/controllers/config.py +++ b/scale/kb_server/kb_server/controllers/config.py @@ -42,49 +42,49 @@ class ConfigController(object): @running_config.when(method='POST') def running_config_POST(self, args): try: + # Expectation: + # { + # 'credentials': {'tested_rc': '', 'passwd_tested': '', + # 'testing_rc': '', 'passwd_testing': ''}, + # 'kb_cfg': {}, + # 'topo_cfg': {} + # } user_config = eval(args) - except Exception: + + # Parsing credentials from application input + cred_config = user_config['credentials'] + cred_tested = Credentials(openrc_contents=cred_config['tested_rc'], + pwd=cred_config['passwd_tested']) + if ('testing_rc' in cred_config and + cred_config['testing_rc'] != cred_config['tested_rc']): + cred_testing = Credentials(openrc_contents=cred_config['testing_rc'], + pwd=cred_config['passwd_testing']) + else: + # Use the same openrc file for both cases + cred_testing = cred_tested + + # Parsing server and client configs from application input + # Save the public key into a temporary file + if 'public_key' in user_config['kb_cfg']: + pubkey_filename = '/tmp/kb_public_key.pub' + f = open(pubkey_filename, 'w') + f.write(user_config['kb_cfg']['public_key_file']) + f.close() + self.kb_config.config_scale['public_key_file'] = pubkey_filename + + alt_config = Configuration.from_string(user_config['kb_cfg']).configure() + self.kb_config.config_scale = self.kb_config.config_scale.merge(alt_config) + + # Parsing topology configs from application input + if 'topo_cfg' in user_config: + topo_cfg = Configuration.from_string(user_config['topo_cfg']).configure() + else: + topo_cfg = None + except Exception as e: response.status = 403 - response.text = "Error while parsing configurations!" + response.text = "Error while parsing configurations: %s" % e.message return response.text - # Expectation: - # { - # 'credentials': {'tested_rc': '', 'passwd_tested': '', - # 'testing_rc': '', 'passwd_testing': ''}, - # 'kb_cfg': {}, - # 'topo_cfg': {} - # } - - # Parsing credentials from application input - cred_config = user_config['credentials'] - cred_tested = Credentials(openrc_contents=cred_config['tested_rc'], - pwd=cred_config['passwd_tested']) - if ('testing_rc' in cred_config and cred_config['testing_rc'] != cred_config['tested_rc']): - cred_testing = Credentials(openrc_contents=cred_config['testing_rc'], - pwd=cred_config['passwd_testing']) - else: - # Use the same openrc file for both cases - cred_testing = cred_tested - - # Parsing server and client configs from application input - # Save the public key into a temporary file - if 'public_key' in user_config['kb_cfg']: - pubkey_filename = '/tmp/kb_public_key.pub' - f = open(pubkey_filename, 'w') - f.write(user_config['kb_cfg']['public_key_file']) - f.close() - self.kb_config.config_scale['public_key_file'] = pubkey_filename - - alt_config = Configuration.from_string(user_config['kb_cfg']).configure() - self.kb_config.config_scale = self.kb_config.config_scale.merge(alt_config) - - # Parsing topology configs from application input - if 'topo_cfg' in user_config: - topo_cfg = Configuration.from_string(user_config['topo_cfg']).configure() - else: - topo_cfg = None - self.kb_config.init_with_rest_api(cred_tested=cred_tested, cred_testing=cred_testing, topo_cfg=topo_cfg)