From: Markos Chandras <mchandras@suse.de>
Date: Mon, 25 Sep 2017 19:40:11 +0000 (+0100)
Subject: xci: scripts: Fix script to run on Jenkins CI
X-Git-Tag: 6.0.0~295^2
X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=commitdiff_plain;h=9f085a075309738af1f73a7617f604bd2af1604c;p=releng-xci.git

xci: scripts: Fix script to run on Jenkins CI

A couple of tweaks are necessary to get it working with Jenkins
- Jenkins jobs could contain the 'xci' string so make the regexp
more accurate.
- Rename VMs to use a more accurate name
- Fix ssh public key location
- Create a fresh /etc/hosts since distro may not have one.
- Set hostname on VM

Change-Id: I332a424bc8b2de98d7b326c192996b7b12c79dd7
Signed-off-by: Markos Chandras <mchandras@suse.de>
---

diff --git a/xci/scripts/vm/build-dib-os.sh b/xci/scripts/vm/build-dib-os.sh
index 47d0a905..78eaff2d 100755
--- a/xci/scripts/vm/build-dib-os.sh
+++ b/xci/scripts/vm/build-dib-os.sh
@@ -13,8 +13,10 @@ set -e
 lsb_release -i | grep -q -i ubuntu || { echo "This script only works on Ubuntu distros"; exit 1; }
 
 declare -A flavors=( ["ubuntu-minimal"]="xenial" ["opensuse-minimal"]="42.3" ["centos-minimal"]="7" )
-elements="vm simple-init devuser growroot openssh-server"
+declare -r elements="vm simple-init devuser growroot openssh-server"
 declare -r one_distro=${1}
+declare -r BASE_PATH=$(dirname $(readlink -f $0) | sed "s@/xci/.*@@")
+
 if [[ -n ${one_distro} ]]; then
 	case ${one_distro} in
 		centos|ubuntu|opensuse) : ;;
@@ -26,7 +28,7 @@ fi
 echo "Configuring devuser..."
 export DIB_DEV_USER_USERNAME=devuser
 export DIB_DEV_USER_PWDLESS_SUDO=1
-export DIB_DEV_USER_AUTHORIZED_KEYS=$HOME/.ssh/id_rsa_for_dib.pub
+export DIB_DEV_USER_AUTHORIZED_KEYS=${BASE_PATH}/xci/scripts/vm/id_rsa_for_dib.pub
 export DIB_DEV_USER_PASSWORD=linux
 
 echo "Installing base dependencies..."
diff --git a/xci/scripts/vm/start-new-vm.sh b/xci/scripts/vm/start-new-vm.sh
index 5101fc9f..c374071c 100755
--- a/xci/scripts/vm/start-new-vm.sh
+++ b/xci/scripts/vm/start-new-vm.sh
@@ -30,23 +30,24 @@ declare -r CPU=host
 declare -r NCPUS=24
 declare -r MEMORY=49152
 declare -r DISK=500
-declare -r NAME=${1}
+declare -r NAME=${1}_xci_vm
+declare -r OS=${1}
 declare -r NETWORK="jenkins-test"
-declare -r BASE_PATH=$(dirname $(readlink -f $0) | sed "s@/xci.*@@")
+declare -r BASE_PATH=$(dirname $(readlink -f $0) | sed "s@/xci/.*@@")
 
 echo "Preparing new virtual machine '${NAME}'..."
 
 # NOTE(hwoarang) This should be removed when we move the dib images to a central place
-echo "Building '${NAME}' image (tail build.log for progress and failures)..."
-$BASE_PATH/xci/scripts/vm/build-dib-os.sh ${NAME} > build.log 2>&1
+echo "Building '${OS}' image (tail build.log for progress and failures)..."
+$BASE_PATH/xci/scripts/vm/build-dib-os.sh ${OS} > build.log 2>&1
 
-[[ ! -e ${1}.qcow2 ]] && echo "${1}.qcow2 not found! This should never happen!" && exit 1
+[[ ! -e ${OS}.qcow2 ]] && echo "${OS}.qcow2 not found! This should never happen!" && exit 1
 
 sudo apt-get install -y -q=3 virt-manager qemu-kvm libvirt-bin qemu-utils
 sudo systemctl -q start libvirtd
 
-echo "Resizing disk image '${NAME}' to ${DISK}G..."
-qemu-img resize ${NAME}.qcow2 ${DISK}G
+echo "Resizing disk image '${OS}' to ${DISK}G..."
+qemu-img resize ${OS}.qcow2 ${DISK}G
 
 echo "Creating new network '${NETWORK}' if it does not exist already..."
 if ! sudo virsh net-list --name | grep -q ${NETWORK}; then
@@ -77,7 +78,7 @@ sudo virsh undefine ${NAME} || true
 
 echo "Installing virtual machine '${NAME}'..."
 sudo virt-install -n ${NAME} --memory ${MEMORY} --vcpus ${NCPUS} --cpu ${CPU} \
-	--import --disk=${NAME}.qcow2 --network network=${NETWORK} \
+	--import --disk=${OS}.qcow2 --network network=${NETWORK} \
 	--graphics none --hvm --noautoconsole
 
 _retries=30
@@ -98,7 +99,7 @@ done
 chmod 600 ${BASE_PATH}/xci/scripts/vm/id_rsa_for_dib*
 # Remove it from known_hosts
 ssh-keygen -R $_ip || true
-ssh-keygen -R ${NAME}_xci_vm || true
+ssh-keygen -R ${NAME} || true
 
 declare -r vm_ssh="ssh -o StrictHostKeyChecking=no -i ${BASE_PATH}/xci/scripts/vm/id_rsa_for_dib -l devuser"
 
@@ -107,7 +108,7 @@ _ssh_exit=0
 
 echo "Verifying operational status..."
 while [[ $_retries -ne 0 ]]; do
-	if eval $vm_ssh $_ip "sudo cat /etc/os-release" 2>/dev/null; then
+	if eval $vm_ssh $_ip "sudo cat /etc/os-release"; then
 		_ssh_exit=$?
 		break;
 	else
@@ -121,8 +122,8 @@ done
 echo "Congratulations! Your shiny new '${NAME}' virtual machine is fully operational! Enjoy!"
 
 echo "Adding ${NAME}_xci_vm entry to /etc/hosts"
-sudo sed -i "/.*${NAME}_xci_vm.*/d" /etc/hosts
-sudo bash -c "echo '${_ip} ${NAME}_xci_vm' >> /etc/hosts"
+sudo sed -i "/.*${NAME}.*/d" /etc/hosts
+sudo bash -c "echo '${_ip} ${NAME}' >> /etc/hosts"
 
 echo "Dropping a minimal .ssh/config file"
 cat > $HOME/.ssh/config<<EOF
@@ -141,21 +142,36 @@ ProxyCommand ssh -l devuser \$(echo %h | sed 's/_opnfv//') 'nc 192.168.122.2 %p'
 EOF
 
 echo "Preparing test environment..."
+# *_xci_vm hostname is invalid. Letst just use distro name
+$vm_ssh $_ip "sudo hostname ${NAME/_xci*}"
 # Start with good dns
 $vm_ssh $_ip 'sudo bash -c "echo nameserver 8.8.8.8 > /etc/resolv.conf"'
 $vm_ssh $_ip 'sudo bash -c "echo nameserver 8.8.4.4 >> /etc/resolv.conf"'
+cat > ${BASE_PATH}/vm_hosts.txt <<EOF
+127.0.0.1 localhost ${NAME/_xci*}
+::1 localhost ipv6-localhost ipv6-loopback
+fe00::0 ipv6-localnet
+fe00::1 ipv6-allnodes
+fe00::2 ipv6-allrouters
+ff00::3 ipv6-allhosts
+$_ip ${NAME/_xci*}
+EOF
+
 # Need to copy releng-xci to the vm so we can execute stuff
 do_copy() {
 	rsync -a \
 		--exclude "${NAME}*" \
 		--exclude "build.log" \
-		-e "$vm_ssh" ${BASE_PATH} $_ip:~/
+		-e "$vm_ssh" ${BASE_PATH}/* $_ip:~/releng-xci/
 }
 
 do_copy
+rm ${BASE_PATH}/vm_hosts.txt
+
 # Copy keypair
 $vm_ssh $_ip "cp --preserve=all ~/releng-xci/xci/scripts/vm/id_rsa_for_dib /home/devuser/.ssh/id_rsa"
 $vm_ssh $_ip "cp --preserve=all ~/releng-xci/xci/scripts/vm/id_rsa_for_dib.pub /home/devuser/.ssh/id_rsa.pub"
+$vm_ssh $_ip "sudo mv /home/devuser/releng-xci/vm_hosts.txt /etc/hosts"
 
 set +e