xci: scripts: start-new-vm.sh: Do not destroy user's configuration file 47/44447/5
authorMarkos Chandras <mchandras@suse.de>
Fri, 6 Oct 2017 09:29:28 +0000 (10:29 +0100)
committerMarkos Chandras <mchandras@suse.de>
Sat, 7 Oct 2017 15:08:16 +0000 (16:08 +0100)
As we move towards making this script useful for developers, we
shouldn't destroy the existing ssh configuration file but rather
create a new one just for XCI. This also move the destruction of
previous instances sooner, even before building the OS image so we
can free resources as soon as possible. Finally, this also uses the
hostname to ssh to the new VM instead of its IP to ensure that
everything is setup up properly on the physical host.

Change-Id: I185befd8943fdb8ebcdb635aefb4d4b03473258b
Signed-off-by: Markos Chandras <mchandras@suse.de>
xci/scripts/vm/start-new-vm.sh

index a1f4080..5273a77 100755 (executable)
@@ -82,6 +82,10 @@ declare -r XCI_CACHE_DIR=${HOME}/.cache/opnfv_xci_deploy
 
 echo "Preparing new virtual machine '${VM_NAME}'..."
 
+echo "Destroying previous '${VM_NAME}' instances..."
+sudo virsh destroy ${VM_NAME} || true
+sudo virsh undefine ${VM_NAME} || true
+
 source /etc/os-release
 echo "Installing host (${ID,,}) dependencies..."
 # check we can run sudo
@@ -172,10 +176,6 @@ fi
 sudo virsh net-list --autostart | grep -q ${NETWORK} || sudo virsh net-autostart ${NETWORK}
 sudo virsh net-list --inactive | grep -q ${NETWORK} && sudo virsh net-start ${NETWORK}
 
-echo "Destroying previous instances if necessary..."
-sudo virsh destroy ${VM_NAME} || true
-sudo virsh undefine ${VM_NAME} || true
-
 echo "Installing virtual machine '${VM_NAME}'..."
 sudo virt-install -n ${VM_NAME} --memory ${MEMORY} --vcpus ${NCPUS} --cpu ${CPU} \
        --import --disk=${OS_IMAGE_FILE},cache=unsafe --network network=${NETWORK} \
@@ -201,7 +201,8 @@ chmod 600 ${BASE_PATH}/xci/scripts/vm/id_rsa_for_dib*
 ssh-keygen -R $_ip || true
 ssh-keygen -R ${VM_NAME} || true
 
-declare -r vm_ssh="ssh -o StrictHostKeyChecking=no -i ${BASE_PATH}/xci/scripts/vm/id_rsa_for_dib -l devuser"
+# Initial ssh command until we setup everything
+vm_ssh="ssh -o StrictHostKeyChecking=no -i ${BASE_PATH}/xci/scripts/vm/id_rsa_for_dib -l devuser"
 
 _retries=30
 _ssh_exit=0
@@ -221,12 +222,12 @@ done
 
 echo "Congratulations! Your shiny new '${VM_NAME}' virtual machine is fully operational! Enjoy!"
 
-echo "Adding ${VM_NAME}_xci_vm entry to /etc/hosts"
+echo "Adding ${VM_NAME} entry to /etc/hosts"
 sudo sed -i "/.*${VM_NAME}.*/d" /etc/hosts
 sudo bash -c "echo '${_ip} ${VM_NAME}' >> /etc/hosts"
 
 echo "Dropping a minimal .ssh/config file"
-cat > $HOME/.ssh/config<<EOF
+cat > $HOME/.ssh/xci-vm-config<<EOF
 Host *
 StrictHostKeyChecking no
 ServerAliveInterval 60
@@ -243,12 +244,15 @@ StrictHostKeyChecking no
 ProxyCommand ssh -l devuser \$(echo %h | sed 's/_opnfv//') 'nc 192.168.122.2 %p'
 EOF
 
+# Final ssh command which will also test the configuration file
+declare -r vm_ssh="ssh -F $HOME/.ssh/xci-vm-config"
+
 echo "Preparing test environment..."
 # *_xci_vm hostname is invalid. Letst just use distro name
-$vm_ssh $_ip "sudo hostname ${VM_NAME/_xci*}"
+$vm_ssh ${VM_NAME} "sudo hostname ${VM_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"'
+$vm_ssh ${VM_NAME} 'sudo bash -c "echo nameserver 8.8.8.8 > /etc/resolv.conf"'
+$vm_ssh ${VM_NAME} 'sudo bash -c "echo nameserver 8.8.4.4 >> /etc/resolv.conf"'
 cat > ${BASE_PATH}/vm_hosts.txt <<EOF
 127.0.0.1 localhost ${VM_NAME/_xci*}
 ::1 localhost ipv6-localhost ipv6-loopback
@@ -265,22 +269,22 @@ do_copy() {
                --exclude "${VM_NAME}*" \
                --exclude "${OS}*" \
                --exclude "build.log" \
-               -e "$vm_ssh" ${BASE_PATH}/ $_ip:~/releng-xci/
+               -e "$vm_ssh" ${BASE_PATH}/ ${VM_NAME}:~/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"
+$vm_ssh ${VM_NAME} "cp --preserve=all ~/releng-xci/xci/scripts/vm/id_rsa_for_dib /home/devuser/.ssh/id_rsa"
+$vm_ssh ${VM_NAME} "cp --preserve=all ~/releng-xci/xci/scripts/vm/id_rsa_for_dib.pub /home/devuser/.ssh/id_rsa.pub"
+$vm_ssh ${VM_NAME} "sudo mv /home/devuser/releng-xci/vm_hosts.txt /etc/hosts"
 
 set +e
 
 _has_test=true
 echo "Verifying test script exists..."
-$vm_ssh $_ip "bash -c 'stat ~/releng-xci/run_jenkins_test.sh'"
+$vm_ssh ${VM_NAME} "bash -c 'stat ~/releng-xci/run_jenkins_test.sh'"
 if [[ $? != 0 ]]; then
        echo "Failed to find a 'run_jenkins_test.sh' script..."
        if ${DEFAULT_XCI_TEST}; then
@@ -300,7 +304,7 @@ fi
 
 if ${_has_test}; then
        echo "Running test..."
-       $vm_ssh $_ip "bash ~/releng-xci/run_jenkins_test.sh"
+       $vm_ssh ${VM_NAME} "bash ~/releng-xci/run_jenkins_test.sh"
        xci_error=$?
 else
        echo "No jenkins test was found. The virtual machine will remain idle!"