Merge "Add Danube Document Framework"
[bottlenecks.git] / utils / dev_env / deploy / create_libvirt_vm.sh
1 #!/bin/bash
2 ##############################################################################
3 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
4 #
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 ##############################################################################
10
11
12 ##############################################
13 #  Usage: ./deploy.sh paras_conf outout_dir
14 ##############################################
15 SCRIPT_DIR=$(cd "$(dirname "$0")"; pwd)
16 tool_dir=${0%/*}
17
18 function print_usage()
19 {
20     echo "Usage: ./deploy.sh paras_conf output_dir"
21 }
22
23 ## sanity check
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!"
31       echo "deploy.sh"
32       echo "mac_generator.sh"
33       echo "1 xml, and 4 template"
34       exit 1
35 fi
36
37 ## Check input
38 if [ $# != 2 ]; then
39     print_usage
40     exit 1
41 fi
42 if [ ! -f $1 ]; then
43     echo "Cannot find file: "$1
44     exit 1
45 fi
46 if [ -d $2 ]; then
47     echo "Ouput dir $2 exist!"
48     exit 1
49 fi
50
51 ## Assign parameters
52 host_names=""
53 puppet_enable="true"
54 master_host=""
55 vm_mem=
56 vm_cpu_cores=
57 image_url=
58 image_name=
59 ipaddr_start=
60 trusted_ssh_pub_keys_file=
61 while read line
62 do
63     line=(${line//=/ })
64     case ${line[0]} in
65         "host_names" )
66             host_names=${line[1]}
67         ;;
68         "puppet_enable" )
69             puppet_enable=${line[1]}
70         ;;
71         "master_host" )
72             master_host=${line[1]}
73         ;;
74         "vm_mem" )
75             vm_mem=${line[1]}
76         ;;
77         "vm_cpu_cores" )
78             vm_cpu_cores=${line[1]}
79         ;;
80         "image_url" )
81             image_url=${line[1]}
82         ;;
83         "image_name" )
84             image_name=${line[1]}
85         ;;
86         "ipaddr_start" )
87             ipaddr_start=${line[1]}
88         ;;
89         "trusted_ssh_pub_keys_file" )
90             trusted_ssh_pub_keys_file=${line[1]}
91         ;;
92     esac
93 done < $1
94
95 echo "puppet_enable="$puppet_enable
96
97 # Check parameters in conf file
98 if [ $puppet_enable == "true" ] ; then
99     if [ ${#master_host} == 0 ];then
100         echo "Should specify master_host!"
101         exit 1
102     else
103         result=$(echo ${host_names} | grep  "${master_host}")
104         if [ ${result} == "" ]; then
105             echo "Specified master_host is invalid!"
106             exit 1
107         fi
108     fi
109 fi
110
111 # Define and Prepare needed data
112 mac_arr=
113 hostname_arr=(${host_names//,/ })
114 virt_num=${#hostname_arr[@]}
115 ip_arr=()
116 replaced_hosts=""
117 replaced_ssh_keys=""
118 output_dir=$2
119 work_dir=
120 host_vm_dir=
121 cache_dir=
122 function init(){
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)
127     mac_arr=($mac_str)
128
129     # Generate hosts info
130     local ip=""
131     i=0
132     for host in "${hostname_arr[@]}"; do
133         ip=${ipaddr_prefix}$((i+$ipaddr_idx))
134         ip_arr+=($ip)
135         # Note the format, especially the space
136         replaced_hosts=${replaced_hosts}"      "${ip}" "${host}"\n"
137         let i=i+1
138     done
139
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
143         while read line
144         do
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}
150     fi
151
152     # Prepare needed folder and files
153     if [ ${output_dir:0-1} == "/" ]; then
154         output_dir=${output_dir%/*}
155     fi
156     output_file=$output_dir/hosts.info
157     mkdir -p $output_dir
158     touch $output_file
159     echo "## Output host_info file --> "$output_file
160
161     work_dir=$output_dir/work
162     host_vm_dir=$work_dir/vm
163     cache_dir=$work_dir/cache
164     mkdir -p $work_dir
165     mkdir -p $host_vm_dir
166     mkdir -p $cache_dir
167
168     # Cache img file
169     echo "## Cache img file"
170     curl --connect-timeout 10 -o ${cache_dir}/$image_name $image_url
171 }
172
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() {
178     i=0
179     while (($i < $virt_num))
180     do
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]}
183         mkdir -p $vm_dir
184         cp ${cache_dir}/$image_name $vm_dir
185
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
190
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
196             else
197                 cp ${tool_dir}/p-agent-user-data.template ${cache_dir}/user-data.templatate
198             fi
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
203         else
204            # Use common user data
205            echo "## Use common user-data.template"
206            cp ${tool_dir}/user-data.template ${cache_dir}/user-data
207         fi
208
209         genisoimage -output seed.iso -volid cidata -joliet -rock ${cache_dir}/user-data ${cache_dir}/meta-data
210         mv seed.iso ${vm_dir}/
211         # Create vm xml
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
219
220         echo "${ip_arr[$i]} ${hostname_arr[$i]}" >> $output_file
221
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]}
226         let i=i+1
227         rm -rf ${cache_dir}/meta-data ${cache_dir}/user-data
228     done
229 }
230
231 function clean(){
232     rm -rf ${cache_dir}
233 }
234
235 init
236 bring_up
237 clean