From 5f96594a85fbf2d2587406eee6adeafd902773b4 Mon Sep 17 00:00:00 2001 From: apporc Date: Fri, 19 Sep 2014 14:16:03 +0800 Subject: [PATCH 1/3] Don't force hostname change Signed-off-by: apporc --- es_setup/cfg.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/es_setup/cfg.py b/es_setup/cfg.py index 45357d9..6473194 100644 --- a/es_setup/cfg.py +++ b/es_setup/cfg.py @@ -227,14 +227,17 @@ def ask_user(user_conf): if set_host.lower() == 'yes': txt = 'Input the FQDN hostname you want to use for this host: ' user_conf['hostname'] = utils.ask_user(txt, check=utils.check_hostname) - else: - user_conf['hostname'] = open(HOSTFILE, 'r').read().strip() def validation(user_conf): - utils.valid_print('hostname', user_conf['hostname']) + if 'hostname' in user_conf.keys(): + utils.valid_print('hostname', user_conf['hostname']) def run(user_conf): - open(HOSTFILE, 'w').write(user_conf['hostname'] + '\n') + if 'hostname' in user_conf.keys(): + open(HOSTFILE, 'w').write(user_conf['hostname'] + '\n') + else: + # Get hostname from /etc/hostname if user has set it manually. + user_conf['hostname'] = open(HOSTFILE, 'r').read().strip() ec = ESCFG('setup hostname of this host') ec.ask_user = ask_user From eeeee462f5d2e967ebf61ff2ec0596a41507fbbb Mon Sep 17 00:00:00 2001 From: apporc Date: Fri, 19 Sep 2014 14:32:15 +0800 Subject: [PATCH 2/3] Save answer file using shutil.copyfile Signed-off-by: apporc --- es_setup/cfg.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/es_setup/cfg.py b/es_setup/cfg.py index 6473194..9cb9708 100644 --- a/es_setup/cfg.py +++ b/es_setup/cfg.py @@ -4,6 +4,8 @@ import getpass import commands import os +import shutil +from os.path import expanduser LOG = logging.getLogger(__name__) @@ -330,10 +332,10 @@ def cinder_create(user_conf): (user_conf['os_cinder_dev'])) def packstack(user_conf): - TMP_ANSWER_FILE = '/tmp/eayunstack.answer' - ANSWER_FILE = '.eayunstack.answer' + ANSWER_FILE = '/tmp/eayunstack.answer' + ANSWER_SAVE = os.path.join(expanduser("~"), '.es-setup.answer') # Generate answer file with packstack. - (status, out) = commands.getstatusoutput('/usr/bin/packstack --gen-answer-file=%s' % TMP_ANSWER_FILE) + (status, out) = commands.getstatusoutput('/usr/bin/packstack --gen-answer-file=%s' % ANSWER_FILE) if status != 0: LOG.warn(out) raise RuntimeError('Failed to generate answer file') @@ -371,15 +373,12 @@ def packstack(user_conf): for option in configs: # Update options (status, out) = commands.getstatusoutput('/usr/bin/openstack-config --set %s general %s %s' - % (TMP_ANSWER_FILE, option, configs[option])) + % (ANSWER_FILE, option, configs[option])) if status != 0: LOG.warn(out) raise RuntimeError('Failed to update option %s in answer file' % option) # Save answer file - os.chdir(os.environ['HOME']) - if os.path.exists(ANSWER_FILE): - os.unlink(ANSWER_FILE) - os.link(TMP_ANSWER_FILE, ANSWER_FILE) + shutil.copyfile(ANSWER_FILE, ANSWER_SAVE) # Invoke packstack, currently not hide the output from packstack. LOG.info('Starting openstack deployment') os.system('/usr/bin/packstack --answer-file=%s' % ANSWER_FILE) From a6c28df95f6775409648471f8ff60c6c607c0623 Mon Sep 17 00:00:00 2001 From: apporc Date: Fri, 19 Sep 2014 15:00:19 +0800 Subject: [PATCH 3/3] integrate code about systemctl in service_operate func Signed-off-by: apporc --- es_setup/cfg.py | 28 ++++------------------------ es_setup/utils.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/es_setup/cfg.py b/es_setup/cfg.py index 9cb9708..291a08a 100644 --- a/es_setup/cfg.py +++ b/es_setup/cfg.py @@ -170,18 +170,8 @@ def write_cfg(role): with file(CFG_FILE % user_conf[role + '_nic'], 'w') as f: f.write(CFG_FMT % tuple(CFG_VAL)) - LOG.info('Checking NetworkManager service') - (status, out) = commands.getstatusoutput( - 'systemctl is-active NetworkManager.service') - if out == 'active': - LOG.info('Stop NetworkManager service') - commands.getstatusoutput('systemctl stop NetworkManager.service') - - (status, out) = commands.getstatusoutput( - 'systemctl is-enabled NetworkManager.service') - if out == 'enabled': - LOG.info('Disable NetworkManager service') - commands.getstatusoutput('systemctl disable NetworkManager.service') + LOG.info('Disabling NetworkManager service') + utils.service_operate('NetworkManager', start=False) LOG.info('Write network config file') if 'cfg_mgt' in user_conf.keys() and user_conf['cfg_mgt']: @@ -195,18 +185,8 @@ def write_cfg(role): if 'ntp_server' not in user_conf.keys(): - LOG.info('Checking ntpd service') - (_, out) = commands.getstatusoutput( - 'systemctl is-active ntpd.service') - if out != 'active': - LOG.info('Starting ntpd service') - commands.getstatusoutput('systemctl start ntpd.service') - - (_, out) = commands.getstatusoutput( - 'systemctl is-enabled ntpd.service') - if out != 'enabled': - LOG.info('Enabling ntpd service') - commands.getstatusoutput('systemctl enable ntpd.service') + LOG.info('Enabling ntpd service') + utils.service_operate('ntpd', start=True) # After ntpd server started, set ntp server to the controller node. user_conf['ntp_server'] = utils.get_ipaddr(user_conf['mgt_nic']) diff --git a/es_setup/utils.py b/es_setup/utils.py index d84784b..d4f33f6 100644 --- a/es_setup/utils.py +++ b/es_setup/utils.py @@ -3,6 +3,7 @@ import fcntl import struct import string +import commands LOG = logging.getLogger(__name__) @@ -143,3 +144,22 @@ def get_ipaddr(ifname): except IOError: return None return socket.inet_ntoa(info) + + +def service_operate(service, start=None): + # service is an available service in systemctl + # start&enable the service if start is True, else stop/disable it. + if start is None: + return + (_, out) = commands.getstatusoutput( + 'systemctl is-active %s.service' % service) + if start and out != 'active': + commands.getstatusoutput('systemctl start %s.service' % service) + elif not start and out == 'active': + commands.getstatusoutput('systemctl stop %s.service' % service) + (_, out) = commands.getstatusoutput( + 'systemctl is-enabled %s.service' % service) + if start and out != 'enabled': + commands.getstatusoutput('systemctl enable %s.service' % service) + elif not start and out == 'enabled': + commands.getstatusoutput('systemctl disable %s.service' % service)