Merge "Using nova and heat client to create a stack for rubbos test suite"
authorJun Li <matthew.lijun@huawei.com>
Wed, 20 Apr 2016 01:38:46 +0000 (01:38 +0000)
committerGerrit Code Review <gerrit@172.30.200.206>
Wed, 20 Apr 2016 01:38:46 +0000 (01:38 +0000)
utils/dev_env/deploy/create_libvirt_vm.sh [new file with mode: 0755]
utils/dev_env/deploy/libvirt_template.xml [moved from utils/rubbos_dev_env_setup/libvirt_template.xml with 100% similarity]
utils/dev_env/deploy/mac_generator.sh [moved from utils/rubbos_dev_env_setup/mac_generator.sh with 87% similarity]
utils/dev_env/deploy/meta-data.template [moved from utils/rubbos_dev_env_setup/meta-data_template with 100% similarity]
utils/dev_env/deploy/p-agent-user-data.template [new file with mode: 0644]
utils/dev_env/deploy/p-master-user-data.template [new file with mode: 0644]
utils/dev_env/deploy/user-data.template [moved from utils/rubbos_dev_env_setup/user-data_template with 93% similarity]
utils/dev_env/paras.conf [new file with mode: 0644]
utils/rubbos_dev_env_setup/deploy.sh [deleted file]
utils/rubbos_dev_env_setup/env_config.sh [deleted file]

diff --git a/utils/dev_env/deploy/create_libvirt_vm.sh b/utils/dev_env/deploy/create_libvirt_vm.sh
new file mode 100755 (executable)
index 0000000..45ba2b8
--- /dev/null
@@ -0,0 +1,237 @@
+#!/bin/bash
+##############################################################################
+# Copyright (c) 2016 Huawei Technologies Co.,Ltd 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
+##############################################################################
+
+
+##############################################
+#  Usage: ./deploy.sh paras_conf outout_dir
+##############################################
+SCRIPT_DIR=$(cd "$(dirname "$0")"; pwd)
+tool_dir=${0%/*}
+
+function print_usage()
+{
+    echo "Usage: ./deploy.sh paras_conf output_dir"
+}
+
+## sanity check
+if [ ! -f $tool_dir/mac_generator.sh ] || \
+    [ ! -f $tool_dir/libvirt_template.xml ] || \
+    [ ! -f $tool_dir/meta-data.template ] || \
+    [ ! -f $tool_dir/user-data.template ] || \
+    [ ! -f $tool_dir/p-master-user-data.template ] || \
+    [ ! -f $tool_dir/p-agent-user-data.template ]; then
+      echo "Lack some necessary files for this tool!"
+      echo "deploy.sh"
+      echo "mac_generator.sh"
+      echo "1 xml, and 4 template"
+      exit 1
+fi
+
+## Check input
+if [ $# != 2 ]; then
+    print_usage
+    exit 1
+fi
+if [ ! -f $1 ]; then
+    echo "Cannot find file: "$1
+    exit 1
+fi
+if [ -d $2 ]; then
+    echo "Ouput dir $2 exist!"
+    exit 1
+fi
+
+## Assign parameters
+host_names=""
+puppet_enable="true"
+master_host=""
+vm_mem=
+vm_cpu_cores=
+image_url=
+image_name=
+ipaddr_start=
+trusted_ssh_pub_keys_file=
+while read line
+do
+    line=(${line//=/ })
+    case ${line[0]} in
+        "host_names" )
+            host_names=${line[1]}
+        ;;
+        "puppet_enable" )
+            puppet_enable=${line[1]}
+        ;;
+        "master_host" )
+            master_host=${line[1]}
+        ;;
+        "vm_mem" )
+            vm_mem=${line[1]}
+        ;;
+        "vm_cpu_cores" )
+            vm_cpu_cores=${line[1]}
+        ;;
+        "image_url" )
+            image_url=${line[1]}
+        ;;
+        "image_name" )
+            image_name=${line[1]}
+        ;;
+        "ipaddr_start" )
+            ipaddr_start=${line[1]}
+        ;;
+        "trusted_ssh_pub_keys_file" )
+            trusted_ssh_pub_keys_file=${line[1]}
+        ;;
+    esac
+done < $1
+
+echo "puppet_enable="$puppet_enable
+
+# Check parameters in conf file
+if [ $puppet_enable == "true" ] ; then
+    if [ ${#master_host} == 0 ];then
+        echo "Should specify master_host!"
+        exit 1
+    else
+        result=$(echo ${host_names} | grep  "${master_host}")
+        if [ ${result} == "" ]; then
+            echo "Specified master_host is invalid!"
+            exit 1
+        fi
+    fi
+fi
+
+# Define and Prepare needed data
+mac_arr=
+hostname_arr=(${host_names//,/ })
+virt_num=${#hostname_arr[@]}
+ip_arr=()
+replaced_hosts=""
+replaced_ssh_keys=""
+output_dir=$2
+work_dir=
+host_vm_dir=
+cache_dir=
+function init(){
+    # Generate mac address
+    local mac_generator=${tool_dir}/mac_generator.sh
+    chmod +x $mac_generator
+    mac_str=$($mac_generator $virt_num)
+    mac_arr=($mac_str)
+
+    # Generate hosts info
+    local ip=""
+    i=0
+    for host in "${hostname_arr[@]}"; do
+        ip=${ipaddr_prefix}$((i+$ipaddr_idx))
+        ip_arr+=($ip)
+        # Note the format, especially the space
+        replaced_hosts=${replaced_hosts}"      "${ip}" "${host}"\n"
+        let i=i+1
+    done
+
+    # Generate ssh public keys
+    echo "## trusted_ssh_pub_keys_file --> "${trusted_ssh_pub_keys_file}
+    if [ ${trusted_ssh_pub_keys_file} != "" ] && [ -f ${trusted_ssh_pub_keys_file} ]; then
+        while read line
+        do
+            # Note the format, especially space
+            replaced_ssh_keys=${replaced_ssh_keys}"    - "${line}"\n"
+        done < ${trusted_ssh_pub_keys_file}
+        # delete last "\n" in replaced_ssh_keysi
+        replaced_ssh_keys=${replaced_ssh_keys%\\n}
+    fi
+
+    # Prepare needed folder and files
+    if [ ${output_dir:0-1} == "/" ]; then
+        output_dir=${output_dir%/*}
+    fi
+    output_file=$output_dir/hosts.info
+    mkdir -p $output_dir
+    touch $output_file
+    echo "## Output host_info file --> "$output_file
+
+    work_dir=$output_dir/work
+    host_vm_dir=$work_dir/vm
+    cache_dir=$work_dir/cache
+    mkdir -p $work_dir
+    mkdir -p $host_vm_dir
+    mkdir -p $cache_dir
+
+    # Cache img file
+    echo "## Cache img file"
+    curl --connect-timeout 10 -o ${cache_dir}/$image_name $image_url
+}
+
+# Bring up instances/vms
+sub_ip_arr=(${ipaddr_start//./ })
+ipaddr_prefix=${sub_ip_arr[0]}"."${sub_ip_arr[1]}"."${sub_ip_arr[2]}"."
+ipaddr_idx=${sub_ip_arr[3]}
+function bring_up() {
+    i=0
+    while (($i < $virt_num))
+    do
+        echo "Bring up a vm, hostname: ${hostname_arr[$i]}, ip: ${ip_arr[$i]}, mac: ${mac_arr[$i]}"
+        vm_dir=$host_vm_dir/${hostname_arr[$i]}
+        mkdir -p $vm_dir
+        cp ${cache_dir}/$image_name $vm_dir
+
+        sed -e "s/REPLACE_IPADDR/${ip_arr[$i]}/g" \
+            -e "s/REPLACE_GATEWAY/${ipaddr_prefix}1/g" \
+            -e "s/REPLACE_HOSTNAME/${hostname_arr[$i]}/g" \
+            ${tool_dir}/meta-data.template > ${cache_dir}/meta-data
+
+        if [ ${puppet_enable} == "true" ]; then
+            # Use puppet user data
+            echo "hostname: "${hostname_arr[$i]}
+            if [ ${hostname_arr[$i]} == ${master_host} ]; then
+                cp ${tool_dir}/p-master-user-data.template ${cache_dir}/user-data.template
+            else
+                cp ${tool_dir}/p-agent-user-data.template ${cache_dir}/user-data.templatate
+            fi
+            sed -e "s#REPLACED_TRUSTED_PUB_SSH_KEYS#${replaced_ssh_keys}#g" \
+                -e "s#REPLACED_HOSTS_INFO#${replaced_hosts}#g" \
+                -e "s/REPLACED_PUPPET_MASTER_SERVER/${master_host}/g" \
+                ${cache_dir}/user-data.template > ${cache_dir}/user-data
+        else
+           # Use common user data
+           echo "## Use common user-data.template"
+           cp ${tool_dir}/user-data.template ${cache_dir}/user-data
+        fi
+
+        genisoimage -output seed.iso -volid cidata -joliet -rock ${cache_dir}/user-data ${cache_dir}/meta-data
+        mv seed.iso ${vm_dir}/
+        # Create vm xml
+        sed -e "s/REPLACE_MEM/$vm_mem/g" \
+            -e "s/REPLACE_CPU/$vm_cpu_cores/g" \
+            -e "s/REPLACE_NAME/${hostname_arr[$i]}/g" \
+            -e "s#REPLACE_IMAGE#$vm_dir/disk.img#g" \
+            -e "s#REPLACE_SEED_IMAGE#$vm_dir/seed.iso#g" \
+            -e "s/REPLACE_MAC_ADDR/${mac_arr[$i]}/g" \
+            ${tool_dir}/libvirt_template.xml > ${vm_dir}/libvirt.xml
+
+        echo "${ip_arr[$i]} ${hostname_arr[$i]}" >> $output_file
+
+        echo "Will define xml from:"${vm_dir}"/libvirt.xml"
+        echo "start: "${hostname_arr[$i]}
+        sudo virsh define ${vm_dir}/libvirt.xml
+        sudo virsh start ${hostname_arr[$i]}
+        let i=i+1
+        rm -rf ${cache_dir}/meta-data ${cache_dir}/user-data
+    done
+}
+
+function clean(){
+    rm -rf ${cache_dir}
+}
+
+init
+bring_up
+clean
similarity index 87%
rename from utils/rubbos_dev_env_setup/mac_generator.sh
rename to utils/dev_env/deploy/mac_generator.sh
index ef78f5f..f8bc5e0 100755 (executable)
@@ -1,6 +1,6 @@
 #!/bin/bash
 ##############################################################################
-# Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
+# Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
 #
 # All rights reserved. This program and the accompanying materials
 # are made available under the terms of the Apache License, Version 2.0
@@ -8,8 +8,8 @@
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
 
-# this is a modified copy from compass4nfv/deploy/mac_generator.sh
 
+## This script is used in 'create_libvirt_vm.sh'
 function mac_address_part() {
     hex_number=$(printf '%02x' $RANDOM)
     number_length=${#hex_number}
@@ -33,4 +33,3 @@ for i in `seq $1`; do
 done
 
 echo ${machines}
-
diff --git a/utils/dev_env/deploy/p-agent-user-data.template b/utils/dev_env/deploy/p-agent-user-data.template
new file mode 100644 (file)
index 0000000..3d778f1
--- /dev/null
@@ -0,0 +1,40 @@
+#cloud-config
+
+users:
+  - name: ubuntu
+    ssh-authorized-keys:
+REPLACED_TRUSTED_PUB_SSH_KEYS
+    - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCwmx8/86Vc1yqqplMPkm/w6wB2r5Yl6CjON85Ko5SzPEpnysJwO5ygbMClQTbzCUztIpFeO8D6xdWlhPmhMEJIWtRh+Dx0swB+ByMraGTP92xxKo2ukWzYI6eu25LmJX+a1lmVPmewQq+6dPzIwgCZvwD8UVe5L+d0EP2rb3iniQVoBzksUmhL3N6jp2Lgpscfi1yXFgjFdLpDsCcFcM+Q6wQUC2+qGBYa5RxqercWzISSbvAsYwC7UepmjFpcj3W/WX2QcOCDIlAzeURvC8PbOdzCtgeMhuhH2499BcIUSzPMpjzvpRiHr5SeF/zrPeW+2Ei1MfHi1qrBCxS8JBqt bottlenecks@bottlenecks.opnfv.org
+    sudo: ['ALL=(ALL) NOPASSWD:ALL']
+    groups: sudo
+    shell: /bin/bash
+
+runcmd:
+  - restart ssh
+  - wget https://apt.puppetlabs.com/puppetlabs-release-trusty.deb
+  - sudo dpkg -i puppetlabs-release-trusty.deb
+  - sudo apt-get update
+  - sudo apt-get -y install puppet
+  - sed -i '/^factpath/a basemodulepath=\/etc\/puppet\/modules' /etc/puppet/puppet.conf
+  - sed -i '/^factpath/a server=REPLACED_PUPPET_MASTER_SERVER' /etc/puppet/puppet.conf
+  - sed -i '/^factpath/a runinterval=1d' /etc/puppet/puppet.conf
+  - sed -i '/^templatedir/d' /etc/puppet/puppet.conf
+  - sed -i 's/START=no/START=yes/g' /etc/default/puppet
+
+write_files:
+  - path: /etc/hosts
+    content: |
+      127.0.0.1 localhost
+
+      # The following lines are desirable for IPv6 capable hosts
+      ::1 ip6-localhost ip6-loopback
+      fe00::0 ip6-localnet
+      ff00::0 ip6-mcastprefix
+      ff02::1 ip6-allnodes
+      ff02::2 ip6-allrouters
+      ff02::3 ip6-allhosts
+
+      # hosts info for all vms
+REPLACED_HOSTS_INFO
+
+final_message: "The system with puppet is finally up, after $UPTIME seconds."
diff --git a/utils/dev_env/deploy/p-master-user-data.template b/utils/dev_env/deploy/p-master-user-data.template
new file mode 100644 (file)
index 0000000..446c969
--- /dev/null
@@ -0,0 +1,39 @@
+#cloud-config
+
+users:
+  - name: ubuntu
+    ssh-authorized-keys:
+REPLACED_TRUSTED_PUB_SSH_KEYS
+    - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCwmx8/86Vc1yqqplMPkm/w6wB2r5Yl6CjON85Ko5SzPEpnysJwO5ygbMClQTbzCUztIpFeO8D6xdWlhPmhMEJIWtRh+Dx0swB+ByMraGTP92xxKo2ukWzYI6eu25LmJX+a1lmVPmewQq+6dPzIwgCZvwD8UVe5L+d0EP2rb3iniQVoBzksUmhL3N6jp2Lgpscfi1yXFgjFdLpDsCcFcM+Q6wQUC2+qGBYa5RxqercWzISSbvAsYwC7UepmjFpcj3W/WX2QcOCDIlAzeURvC8PbOdzCtgeMhuhH2499BcIUSzPMpjzvpRiHr5SeF/zrPeW+2Ei1MfHi1qrBCxS8JBqt bottlenecks@bottlenecks.opnfv.org
+    sudo: ['ALL=(ALL) NOPASSWD:ALL']
+    groups: sudo
+    shell: /bin/bash
+
+runcmd:
+  - restart ssh
+  - wget https://apt.puppetlabs.com/puppetlabs-release-trusty.deb
+  - sudo dpkg -i puppetlabs-release-trusty.deb
+  - sudo apt-get update
+  - sudo apt-get -y install puppetserver
+  - sed -i '/^factpath/a basemodulepath=\/etc\/puppet\/modules' /etc/puppet/puppet.conf
+  - sed -i '/^factpath/a server=REPLACED_PUPPET_MASTER_SERVER' /etc/puppet/puppet.conf
+  - sed -i '/^factpath/a runinterval=1d' /etc/puppet/puppet.conf
+  - sed -i '/^templatedir/d' /etc/puppet/puppet.conf
+
+write_files:
+  - path: /etc/hosts
+    content: |
+      127.0.0.1 localhost
+
+      # The following lines are desirable for IPv6 capable hosts
+      ::1 ip6-localhost ip6-loopback
+      fe00::0 ip6-localnet
+      ff00::0 ip6-mcastprefix
+      ff02::1 ip6-allnodes
+      ff02::2 ip6-allrouters
+      ff02::3 ip6-allhosts
+
+      # hosts info for all vms
+REPLACED_HOSTS_INFO
+
+final_message: "The system with puppet is finally up, after $UPTIME seconds."
diff --git a/utils/dev_env/paras.conf b/utils/dev_env/paras.conf
new file mode 100644 (file)
index 0000000..1ebf104
--- /dev/null
@@ -0,0 +1,20 @@
+##############################################################################
+# Copyright (c) 2016 Huawei Technologies Co.,Ltd 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
+##############################################################################
+
+
+## This is a sample configuration file, as one input parameter for 'create_libvirt_vm.sh'
+host_names=test-master,test-agent
+puppet_enable=true
+master_host=test-master
+vm_mem=4096
+vm_cpu_cores=4
+image_url=file:///home/ubuntu/trusty-server-cloudimg-amd64-btnks.img
+image_name=disk.img
+ipaddr_start=192.168.122.201
+trusted_ssh_pub_keys_file=/home/ubuntu/trusted_ssh_pub_keys
diff --git a/utils/rubbos_dev_env_setup/deploy.sh b/utils/rubbos_dev_env_setup/deploy.sh
deleted file mode 100755 (executable)
index a4fbfb8..0000000
+++ /dev/null
@@ -1,106 +0,0 @@
-#!/bin/bash
-##############################################################################
-# Copyright (c) 2015 Huawei Technologies Co.,Ltd 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
-##############################################################################
-
-# this is a modified copy of compass4nfv/deploy/host_virtual.sh
-
-set -x
-
-SCRIPT_DIR=`cd ${BASH_SOURCE[0]%/*};pwd`
-WORK_DIR=${SCRIPT_DIR}/work
-
-mkdir -p $WORK_DIR
-
-source ./env_config.sh
-
-host_vm_dir=$WORK_DIR/vm
-
-
-function download_iso()
-{
-    mkdir -p ${WORK_DIR}/cache
-    curl --connect-timeout 10 -o ${WORK_DIR}/cache/$IMAGE_NAME $IMAGE_URL
-}
-
-
-function tear_down_machines() {
-    for i in $HOSTNAMES; do
-        echo "tear down machine:" $i
-        sudo virsh destroy $i
-        sudo virsh undefine $i
-        rm -rf $host_vm_dir/$i
-    done
-}
-
-
-function get_host_macs() {
-    local mac_generator=${SCRIPT_DIR}/mac_generator.sh
-    local machines=
-
-    chmod +x $mac_generator
-    mac_array=$($mac_generator $VIRT_NUMBER)
-    machines=$(echo $mac_array)
-
-    echo $machines
-}
-
-function launch_host_vms() {
-    mac_array=($(get_host_macs))
-
-    echo ${mac_array[2]}
-    echo ${mac_array[*]}
-
-    old_ifs=$IFS
-    IFS=,
-    tear_down_machines
-    echo "bringing up vms ${mac_array[*]}"
-    i=0
-    for host in $HOSTNAMES; do
-        echo "creating vm disk for instance $host" \
-             "ip ${IPADDR_PREFIX}$((i+11))" \
-             "mac ${mac_array[$i]}"
-        vm_dir=$host_vm_dir/$host
-        mkdir -p $vm_dir
-
-        cp ${WORK_DIR}/cache/$IMAGE_NAME $vm_dir
-
-        # create seed.iso
-        sed -e "s/REPLACE_IPADDR/${IPADDR_PREFIX}$((i+11))/g" \
-            -e "s/REPLACE_GATEWAY/${IPADDR_PREFIX}1/g" \
-            -e "s/REPLACE_HOSTNAME/${host}/g" \
-            meta-data_template \
-            > meta-data
-        cp user-data_template user-data
-        genisoimage  -output seed.iso -volid cidata -joliet -rock user-data meta-data
-        cp seed.iso $vm_dir
-
-        # create vm xml
-        sed -e "s/REPLACE_MEM/$VIRT_MEM/g" \
-            -e "s/REPLACE_CPU/$VIRT_CPUS/g" \
-            -e "s/REPLACE_NAME/$host/g" \
-            -e "s#REPLACE_IMAGE#$vm_dir/disk.img#g" \
-            -e "s#REPLACE_SEED_IMAGE#$vm_dir/seed.iso#g" \
-            -e "s/REPLACE_MAC_ADDR/${mac_array[$i]}/g" \
-            libvirt_template.xml \
-            > $vm_dir/libvirt.xml
-
-        sudo virsh define $vm_dir/libvirt.xml
-        sudo virsh start $host
-        let i=i+1
-    done
-    IFS=$old_ifs
-    rm -rf meta-data user-data seed.iso
-}
-
-
-download_iso
-launch_host_vms
-
-set +x
-
diff --git a/utils/rubbos_dev_env_setup/env_config.sh b/utils/rubbos_dev_env_setup/env_config.sh
deleted file mode 100644 (file)
index 326b9a5..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-export HOSTNAMES=${HOSTNAMES:-"control,httpd,mysql,tomcat,client1,client2,client3,client4,benchmark"}
-export VIRT_NUMBER=9
-export VIRT_MEM=4096
-export VIRT_CPUS=4
-export IMAGE_URL=${IMAGE_URL:-"file:///bottlenecks/trusty-server-cloudimg-amd64-disk1.img"}
-export IMAGE_NAME=disk.img
-export IPADDR_PREFIX="192.168.122."