config /etc/hosts in target hosts

Change-Id: I9421d7e75f7582f52082c46329a8aa801ad510f3
Signed-off-by: luyao <lu.yao135@zte.com.cn>
This commit is contained in:
luyao 2016-07-29 16:42:16 +08:00 committed by Yao Lu
parent 4b9656f843
commit 4d89a5f5cd
2 changed files with 98 additions and 3 deletions

View File

@ -16,6 +16,8 @@
"""
/install endpoint for zenic API
"""
import os
import subprocess
import copy
from oslo_log import log as logging
from webob.exc import HTTPBadRequest
@ -189,3 +191,68 @@ def get_roles_and_hosts_list(req, cluster_id):
hosts_list.append(host_cfg)
roles_id_list.add(role['id'])
return (roles_id_list, hosts_id_list, hosts_list)
def run_scrip(script, ip=None, password=None, msg=None):
try:
_run_scrip(script, ip, password)
except:
msg1 = 'Error occurred during running scripts.'
message = msg1 + msg if msg else msg1
LOG.error(message)
raise HTTPForbidden(explanation=message)
else:
LOG.info('Running scripts successfully!')
def _run_scrip(script, ip=None, password=None):
mask_list = []
repl_list = [("'", "'\\''")]
script = "\n".join(script)
_PIPE = subprocess.PIPE
if ip:
cmd = ["sshpass", "-p", "%s" % password,
"ssh", "-o StrictHostKeyChecking=no",
"%s" % ip, "bash -x"]
else:
cmd = ["bash", "-x"]
environ = os.environ
environ['LANG'] = 'en_US.UTF8'
obj = subprocess.Popen(cmd, stdin=_PIPE, stdout=_PIPE, stderr=_PIPE,
close_fds=True, shell=False, env=environ)
script = "function t(){ exit $? ; } \n trap t ERR \n" + script
out, err = obj.communicate(script)
masked_out = mask_string(out, mask_list, repl_list)
masked_err = mask_string(err, mask_list, repl_list)
if obj.returncode:
pattern = (r'^ssh\:')
if re.search(pattern, err):
LOG.error(_("Network error occured when run script."))
raise exception.NetworkError(masked_err, stdout=out, stderr=err)
else:
msg = ('Failed to run remote script, stdout: %s\nstderr: %s' %
(masked_out, masked_err))
LOG.error(msg)
raise exception.ScriptRuntimeError(msg, stdout=out, stderr=err)
return obj.returncode, out
def mask_string(unmasked, mask_list=None, replace_list=None):
"""
Replaces words from mask_list with MASK in unmasked string.
If words are needed to be transformed before masking, transformation
could be describe in replace list. For example [("'","'\\''")]
replaces all ' characters with '\\''.
"""
mask_list = mask_list or []
replace_list = replace_list or []
masked = unmasked
for word in sorted(mask_list, lambda x, y: len(y) - len(x)):
if not word:
continue
for before, after in replace_list:
word = word.replace(before, after)
masked = masked.replace(word, STR_MASK)
return masked

View File

@ -196,6 +196,8 @@ def get_cluster_kolla_config(req, cluster_id):
pub_macname_list = []
openstack_version = '2.0.3'
docker_namespace = 'kolla'
host_name_ip = {}
host_name_ip_list = []
docker_registry_ip = _get_local_ip()
docker_registry = docker_registry_ip + ':4000'
cluster_networks = daisy_cmn.get_cluster_networks_detail(req, cluster_id)
@ -213,11 +215,15 @@ def get_cluster_kolla_config(req, cluster_id):
deploy_host_cfg = kolla_cmn.get_deploy_node_cfg(
req, host_detail, cluster_networks)
mgt_ip = deploy_host_cfg['mgtip']
host_name_ip = {
deploy_host_cfg['host_name']: deploy_host_cfg['mgtip']}
controller_ip_list.append(mgt_ip)
mgt_macname = deploy_host_cfg['mgt_macname']
pub_macname = deploy_host_cfg['pub_macname']
mgt_macname_list.append(mgt_macname)
pub_macname_list.append(pub_macname)
if host_name_ip not in host_name_ip_list:
host_name_ip_list.append(host_name_ip)
if len(set(mgt_macname_list)) != 1 or \
len(set(pub_macname_list)) != 1:
msg = (_("hosts interface name of public and \
@ -241,10 +247,14 @@ def get_cluster_kolla_config(req, cluster_id):
deploy_host_cfg = kolla_cmn.get_deploy_node_cfg(
req, host_detail, cluster_networks)
mgt_ip = deploy_host_cfg['mgtip']
host_name_ip = {
deploy_host_cfg['host_name']: deploy_host_cfg['mgtip']}
computer_ip_list.append(mgt_ip)
if host_name_ip not in host_name_ip_list:
host_name_ip_list.append(host_name_ip)
kolla_config.update({'Computer_ips': computer_ip_list})
mgt_ip_list = set(controller_ip_list + computer_ip_list)
return (kolla_config, mgt_ip_list)
return (kolla_config, mgt_ip_list, host_name_ip_list)
def generate_kolla_config_file(cluster_id, kolla_config):
@ -255,6 +265,23 @@ def generate_kolla_config_file(cluster_id, kolla_config):
config.add_role_to_inventory(kolla_file, kolla_config)
def config_nodes_hosts(host_name_ip_list, host_ip):
config_scripts = []
hosts_file = "/etc/hosts"
for name_ip in host_name_ip_list:
config_scripts.append("linenumber=`grep -n '%s$' %s | "
"awk -F ':' '{print $1}'` && "
"[ ! -z $linenumber ] && "
"sed -i ${linenumber}d %s" %
(name_ip.keys()[0],
hosts_file, hosts_file))
config_scripts.append("echo '%s %s' >> %s" % (name_ip.values()[0],
name_ip.keys()[0],
hosts_file))
kolla_cmn.run_scrip(config_scripts, host_ip, "ossdbg1",
msg='Failed to config /etc/hosts on %s' % host_ip)
class KOLLAInstallTask(Thread):
"""
Class for kolla install openstack.
@ -301,8 +328,8 @@ class KOLLAInstallTask(Thread):
% self.cluster_id))
def _run(self):
(kolla_config, self.mgt_ip_list) = get_cluster_kolla_config(
self.req, self.cluster_id)
(kolla_config, self.mgt_ip_list, host_name_ip_list) = \
get_cluster_kolla_config(self.req, self.cluster_id)
if not self.mgt_ip_list:
msg = _("there is no host in cluster %s") % self.cluster_id
raise exception.ThreadBinException(msg)
@ -322,6 +349,7 @@ class KOLLAInstallTask(Thread):
with open(self.log_file, "w+") as fp:
for host in hosts_list:
host_ip = host['mgtip']
config_nodes_hosts(host_name_ip_list, host_ip)
cmd = 'sshpass -p ossdbg1 ssh -o StrictHostKeyChecking=no %s \
"if [ ! -d %s ];then mkdir %s;fi" ' % \
(host_ip, self.host_prepare_file, self.host_prepare_file)