From 0d09e2b7301defd1f408d17373cbf991a922c693 Mon Sep 17 00:00:00 2001 From: Alex Yang Date: Wed, 26 Apr 2017 14:14:43 +0800 Subject: Implement the deployment script with python 1. deploy.py: control the work flow of the deployment 2. daisy_server.py: maintain the ssh connection with daisy server and execute ssh commands 3. environment.py: create/find/delete the nodes/vms, install operating system and openstack on nodes 4. libvirt_utils.py: deal with the vm templates and call virsh commands 5. utils.py: some common functions such as file/directory/bash operation Change-Id: I1caa4b0b3118665e15410e8f02bcb6473e5a530b Signed-off-by: Alex Yang --- deploy/utils.py | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 deploy/utils.py (limited to 'deploy/utils.py') diff --git a/deploy/utils.py b/deploy/utils.py new file mode 100644 index 00000000..0c5b1370 --- /dev/null +++ b/deploy/utils.py @@ -0,0 +1,138 @@ +############################################################################## +# Copyright (c) 2017 ZTE Corporation 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 commands +from configobj import ConfigObj +import os +import logging +import subprocess +import sys + + +path_join = os.path.join +CWD = os.getcwd() +WORKSPACE = os.path.normpath(path_join(os.path.dirname(__file__), '..')) +BASE = CWD + + +def get_logger(): + logger = logging.getLogger(__name__) + logger.setLevel(logging.DEBUG) + formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S') + handler = logging.StreamHandler(sys.stdout) + handler.setFormatter(formatter) + logger.addHandler(handler) + return logger + + +LOG = get_logger() +LD = LOG.debug +LI = LOG.info +LW = LOG.warn +LE = LOG.error + + +def save_log_to_file(log_file): + with open(log_file, 'w+'): + pass + + formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S') + handler = logging.FileHandler(log_file, mode='w') + handler.setFormatter(formatter) + LOG.addHandler(handler) + + +def err_exit(message): + LE('%s\n' % message) + sys.exit(1) + + +def log_bar(message, log_func=LI): + bar = '=' * len(message) + log_func(bar) + log_func(message) + log_func(bar) + + +def check_sudo_privilege(): + uid = os.getuid() + if uid != 0: + err_exit('You need run this script with sudo privilege') + + +def check_file_exists(file_path): + if not os.path.dirname(file_path): + file_path = os.path.normpath(path_join(BASE, file_path)) + if not os.access(file_path, os.R_OK): + err_exit('File %s not found\n' % file_path) + + +def make_file_executable(file_path): + if not os.path.isdir(file_path): + file_path = os.path.normpath(path_join(BASE, file_path)) + if not os.access(file_path, os.R_OK): + err_exit('File %s not found\n' % file_path) + if not os.access(file_path, os.X_OK): + LW('File %s is not executable, chmod it and continue' % file_path) + status, output = commands.getstatusoutput('chmod +x %s' % file_path) + if status: + err_exit('Cannot change the file mode of %s' % file_path) + + +def confirm_dir_exists(dir_path): + if not os.path.isdir(dir_path): + LI('Creating directory %s' % dir_path) + os.makedirs(dir_path) + + +def update_config(conf_file, key, value, section='DEFAULT'): + LI('Update_config [ %s : %s ] to file: %s' % (key, value, conf_file)) + config = ConfigObj(conf_file) + config[section][key] = value + config.write() + + +def ipmi_reboot_node(host, user, passwd, boot_source=None): + prefix = 'ipmitool -I lanplus -H {host} -U {user} -P {passwd} -R 1 '.format( + host=host, user=user, passwd=passwd) + if boot_source: + cmd = prefix + 'chassis bootdev {boot_source}'.format(boot_source=boot_source) + LI('IMPI set node %s boot from %s' % (host, boot_source)) + status, output = commands.getstatusoutput(cmd) + if status: + err_exit('IPMI command failed: %s' % output) + + cmd = prefix + 'chassis power reset' + LI('IPMI reset node %s' % host) + status, output = commands.getstatusoutput(cmd) + if status: + err_exit('IPMI command failed: %s' % output) + + +def run_shell(cmd, check=False): + process = subprocess.Popen(cmd, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + shell=True) + while process.poll() is None: + LD(process.stdout.readline().strip()) + + response, stderr = process.communicate() + return_code = process.returncode + + if check: + if return_code > 0: + stderr = stderr.strip() + LE('Failed command: ' + str(cmd)) + LE('Command returned error: ' + str(stderr)) + err_exit('Command return code: ' + str(return_code)) + else: + LI('Successful command: ' + str(cmd)) + + return return_code -- cgit 1.2.3-korg