diff --git a/es_setup/cfg.py b/es_setup/cfg.py index 45357d9..291a08a 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__) @@ -168,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']: @@ -193,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']) @@ -227,14 +209,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 @@ -327,10 +312,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') @@ -368,15 +353,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) 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)