diff options
author | liyin <liyin11@huawei.com> | 2017-01-11 19:10:57 +0800 |
---|---|---|
committer | Ace Lee <liyin11@huawei.com> | 2017-01-12 08:36:38 +0000 |
commit | 9512f7cabad8cb1d7aaee22efe018c6539148dc2 (patch) | |
tree | e5fa5532cb7d8c00428999c3d09d64d466080004 /utils/env_prepare/stack_prepare.py | |
parent | e1a54d34819563946d1ff1ec673d1d261b826ad8 (diff) |
Bottlenecks stack environment prepare
JIRA: BOTTLENECK-124
This code is for Bottlenecks to have a common way to prepare stack
environment.
those action are divided into three part: fetch os file, source file
and adding ext-net to source file.
those function also need change other file like fetch_os_creds.sh.
And add some config to config file.
remove parser file logging function.
This code is relying on the patch:
Modify utils/ code into PEP8 style
Change-Id: I54405776b6dc3f5fb939e511c96963a9c1624938
Signed-off-by: liyin <liyin11@huawei.com>
Diffstat (limited to 'utils/env_prepare/stack_prepare.py')
-rw-r--r-- | utils/env_prepare/stack_prepare.py | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/utils/env_prepare/stack_prepare.py b/utils/env_prepare/stack_prepare.py new file mode 100644 index 00000000..37b523d1 --- /dev/null +++ b/utils/env_prepare/stack_prepare.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python +############################################################################## +# Copyright (c) 2017 Huawei Technologies Co.,Ltd and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## +import os +import subprocess +import errno +from utils.logger import Logger +from utils.parser import Parser as config +import utils.infra_setup.heat.manager as utils + +LOG = Logger(__name__).getLogger() + + +def _prepare_env_daemon(): + + installer_ip = os.environ.get('INSTALLER_IP', 'undefined') + installer_type = os.environ.get('INSTALLER_TYPE', 'undefined') + + rc_file = config.bottlenecks_config["rc_dir"] + + _get_remote_rc_file(rc_file, installer_ip, installer_type) + + _source_file(rc_file) + + _append_external_network(rc_file) + + # update the external_network + _source_file(rc_file) + + +def _get_remote_rc_file(rc_file, installer_ip, installer_type): + + RELENG_DIR = config.bottlenecks_config["releng_dir"] + OS_FETCH_SCRIPT = config.bottlenecks_config["fetch_os"] + os_fetch_script = os.path.join(RELENG_DIR, OS_FETCH_SCRIPT) + + try: + cmd = [os_fetch_script, '-d', rc_file, '-i', installer_type, + '-a', installer_ip] + p = subprocess.Popen(cmd, stdout=subprocess.PIPE) + p.communicate()[0] + + if p.returncode != 0: + LOG.debug('Failed to fetch credentials from installer') + except OSError as e: + if e.errno != errno.EEXIST: + raise + + +def _source_file(rc_file): + p = subprocess.Popen(". %s; env" % rc_file, stdout=subprocess.PIPE, + shell=True) + output = p.communicate()[0] + env = dict((line.split('=', 1) for line in output.splitlines())) + os.environ.update(env) + return env + + +def _append_external_network(rc_file): + neutron_client = utils._get_neutron_client() + networks = neutron_client.list_networks()['networks'] + try: + ext_network = next(n['name'] for n in networks if n['router:external']) + except StopIteration: + LOG.warning("Can't find external network") + else: + cmd = 'export EXTERNAL_NETWORK=%s' % ext_network + try: + with open(rc_file, 'a') as f: + f.write(cmd + '\n') + except OSError as e: + if e.errno != errno.EEXIST: + raise |