2 ##############################################################################
3 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
12 ##############################################
13 # Usage: ./deploy.sh paras_conf outout_dir
14 ##############################################
15 SCRIPT_DIR=$(cd "$(dirname "$0")"; pwd)
18 function print_usage()
20 echo "Usage: ./deploy.sh paras_conf output_dir"
24 if [ ! -f $tool_dir/mac_generator.sh ] || \
25 [ ! -f $tool_dir/libvirt_template.xml ] || \
26 [ ! -f $tool_dir/meta-data.template ] || \
27 [ ! -f $tool_dir/user-data.template ] || \
28 [ ! -f $tool_dir/p-master-user-data.template ] || \
29 [ ! -f $tool_dir/p-agent-user-data.template ]; then
30 echo "Lack some necessary files for this tool!"
32 echo "mac_generator.sh"
33 echo "1 xml, and 4 template"
43 echo "Cannot find file: "$1
47 echo "Ouput dir $2 exist!"
60 trusted_ssh_pub_keys_file=
69 puppet_enable=${line[1]}
72 master_host=${line[1]}
78 vm_cpu_cores=${line[1]}
87 ipaddr_start=${line[1]}
89 "trusted_ssh_pub_keys_file" )
90 trusted_ssh_pub_keys_file=${line[1]}
95 echo "puppet_enable="$puppet_enable
97 # Check parameters in conf file
98 if [ $puppet_enable == "true" ] ; then
99 if [ ${#master_host} == 0 ];then
100 echo "Should specify master_host!"
103 result=$(echo ${host_names} | grep "${master_host}")
104 if [ ${result} == "" ]; then
105 echo "Specified master_host is invalid!"
111 # Define and Prepare needed data
113 hostname_arr=(${host_names//,/ })
114 virt_num=${#hostname_arr[@]}
123 # Generate mac address
124 local mac_generator=${tool_dir}/mac_generator.sh
125 chmod +x $mac_generator
126 mac_str=$($mac_generator $virt_num)
129 # Generate hosts info
132 for host in "${hostname_arr[@]}"; do
133 ip=${ipaddr_prefix}$((i+$ipaddr_idx))
135 # Note the format, especially the space
136 replaced_hosts=${replaced_hosts}" "${ip}" "${host}"\n"
140 # Generate ssh public keys
141 echo "## trusted_ssh_pub_keys_file --> "${trusted_ssh_pub_keys_file}
142 if [ ${trusted_ssh_pub_keys_file} != "" ] && [ -f ${trusted_ssh_pub_keys_file} ]; then
145 # Note the format, especially space
146 replaced_ssh_keys=${replaced_ssh_keys}" - "${line}"\n"
147 done < ${trusted_ssh_pub_keys_file}
148 # delete last "\n" in replaced_ssh_keysi
149 replaced_ssh_keys=${replaced_ssh_keys%\\n}
152 # Prepare needed folder and files
153 if [ ${output_dir:0-1} == "/" ]; then
154 output_dir=${output_dir%/*}
156 output_file=$output_dir/hosts.info
159 echo "## Output host_info file --> "$output_file
161 work_dir=$output_dir/work
162 host_vm_dir=$work_dir/vm
163 cache_dir=$work_dir/cache
165 mkdir -p $host_vm_dir
169 echo "## Cache img file"
170 curl --connect-timeout 10 -o ${cache_dir}/$image_name $image_url
173 # Bring up instances/vms
174 sub_ip_arr=(${ipaddr_start//./ })
175 ipaddr_prefix=${sub_ip_arr[0]}"."${sub_ip_arr[1]}"."${sub_ip_arr[2]}"."
176 ipaddr_idx=${sub_ip_arr[3]}
177 function bring_up() {
179 while (($i < $virt_num))
181 echo "Bring up a vm, hostname: ${hostname_arr[$i]}, ip: ${ip_arr[$i]}, mac: ${mac_arr[$i]}"
182 vm_dir=$host_vm_dir/${hostname_arr[$i]}
184 cp ${cache_dir}/$image_name $vm_dir
186 sed -e "s/REPLACE_IPADDR/${ip_arr[$i]}/g" \
187 -e "s/REPLACE_GATEWAY/${ipaddr_prefix}1/g" \
188 -e "s/REPLACE_HOSTNAME/${hostname_arr[$i]}/g" \
189 ${tool_dir}/meta-data.template > ${cache_dir}/meta-data
191 if [ ${puppet_enable} == "true" ]; then
192 # Use puppet user data
193 echo "hostname: "${hostname_arr[$i]}
194 if [ ${hostname_arr[$i]} == ${master_host} ]; then
195 cp ${tool_dir}/p-master-user-data.template ${cache_dir}/user-data.template
197 cp ${tool_dir}/p-agent-user-data.template ${cache_dir}/user-data.templatate
199 sed -e "s#REPLACED_TRUSTED_PUB_SSH_KEYS#${replaced_ssh_keys}#g" \
200 -e "s#REPLACED_HOSTS_INFO#${replaced_hosts}#g" \
201 -e "s/REPLACED_PUPPET_MASTER_SERVER/${master_host}/g" \
202 ${cache_dir}/user-data.template > ${cache_dir}/user-data
204 # Use common user data
205 echo "## Use common user-data.template"
206 cp ${tool_dir}/user-data.template ${cache_dir}/user-data
209 genisoimage -output seed.iso -volid cidata -joliet -rock ${cache_dir}/user-data ${cache_dir}/meta-data
210 mv seed.iso ${vm_dir}/
212 sed -e "s/REPLACE_MEM/$vm_mem/g" \
213 -e "s/REPLACE_CPU/$vm_cpu_cores/g" \
214 -e "s/REPLACE_NAME/${hostname_arr[$i]}/g" \
215 -e "s#REPLACE_IMAGE#$vm_dir/disk.img#g" \
216 -e "s#REPLACE_SEED_IMAGE#$vm_dir/seed.iso#g" \
217 -e "s/REPLACE_MAC_ADDR/${mac_arr[$i]}/g" \
218 ${tool_dir}/libvirt_template.xml > ${vm_dir}/libvirt.xml
220 echo "${ip_arr[$i]} ${hostname_arr[$i]}" >> $output_file
222 echo "Will define xml from:"${vm_dir}"/libvirt.xml"
223 echo "start: "${hostname_arr[$i]}
224 sudo virsh define ${vm_dir}/libvirt.xml
225 sudo virsh start ${hostname_arr[$i]}
227 rm -rf ${cache_dir}/meta-data ${cache_dir}/user-data