diff options
Diffstat (limited to 'xci/scripts/vm/start-new-vm.sh')
-rwxr-xr-x | xci/scripts/vm/start-new-vm.sh | 87 |
1 files changed, 64 insertions, 23 deletions
diff --git a/xci/scripts/vm/start-new-vm.sh b/xci/scripts/vm/start-new-vm.sh index a1f4080e..9b5cdd8e 100755 --- a/xci/scripts/vm/start-new-vm.sh +++ b/xci/scripts/vm/start-new-vm.sh @@ -15,6 +15,10 @@ set -e # executed on a CI or not. export JENKINS_HOME="${JENKINS_HOME:-${HOME}}" +# Set this option to destroy the VM on failures. This is helpful when we +# don't want to preserve the VM for debugging purposes. +export XCI_KEEP_CLEAN_VM_ON_FAILURES=${XCI_KEEP_CLEAN_VM_ON_FAILURES:-true} + export DEFAULT_XCI_TEST=${DEFAULT_XCI_TEST:-false} # JIT Build of OS image to load on the clean VM export XCI_BUILD_CLEAN_VM_OS=${XCI_BUILD_CLEAN_VM_OS:-true} @@ -24,6 +28,15 @@ export XCI_UPDATE_CLEAN_VM_OS=${XCI_UPDATE_CLEAN_VM_OS:-false} grep -q -i ^Y$ /sys/module/kvm_intel/parameters/nested || { echo "Nested virtualization is not enabled but it's needed for XCI to work"; exit 1; } +destroy_vm_on_failures() { + local exit_err=${xci_error:-130} + if ! ${XCI_KEEP_CLEAN_VM_ON_FAILURES}; then + sudo virsh destroy ${VM_NAME}_xci_vm + sudo virsh undefine ${VM_NAME}_xci_vm + fi + exit $exit_err +} + usage() { echo """ $0 <distro> @@ -32,6 +45,22 @@ usage() { """ } +wait_for_pkg_mgr() { + local pkg_mgr=$1 + local _retries=30 + while [[ $_retries -gt 0 ]]; do + if pgrep -a $pkg_mgr &> /dev/null; then + echo "There is another $pkg_mgr process running... ($_retries retries left)" + sleep 30 + (( _retries = _retries - 1 )) + else + return 0 + fi + done + echo "$pkg_mgr still running... Maybe stuck?" + exit 1 +} + update_clean_vm_files() { local opnfv_url="http://artifacts.opnfv.org/releng/xci/images" local vm_cache=${XCI_CACHE_DIR}/clean_vm/images @@ -82,6 +111,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 @@ -95,13 +128,19 @@ if ! sudo -n "true"; then exit 1 fi case ${ID,,} in - *suse) sudo zypper -q -n in virt-manager qemu-kvm qemu-tools libvirt-daemon docker libvirt-client libvirt-daemon-driver-qemu iptables ebtables dnsmasq - ;; - centos) sudo yum install -q -y epel-release - sudo yum install -q -y in virt-manager qemu-kvm qemu-kvm-tools qemu-img libvirt-daemon-kvm docker iptables ebtables dnsmasq - ;; - ubuntu) sudo apt-get install -y -q=3 virt-manager qemu-kvm libvirt-bin qemu-utils docker.io docker iptables ebtables dnsmasq - ;; + *suse) + wait_for_pkg_mgr zypper + sudo zypper -q -n in virt-manager qemu-kvm qemu-tools libvirt-daemon docker libvirt-client libvirt-daemon-driver-qemu iptables ebtables dnsmasq + ;; + centos) + wait_for_pkg_mgr yum + sudo yum install -q -y epel-release + sudo yum install -q -y in virt-manager qemu-kvm qemu-kvm-tools qemu-img libvirt-daemon-kvm docker iptables ebtables dnsmasq + ;; + ubuntu) + wait_for_pkg_mgr apt-get + sudo apt-get install -y -q=3 virt-manager qemu-kvm libvirt-bin qemu-utils docker.io docker iptables ebtables dnsmasq + ;; esac echo "Ensuring libvirt and docker services are running..." @@ -172,15 +211,13 @@ 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} \ --graphics none --hvm --noautoconsole +trap destroy_vm_on_failures EXIT + _retries=30 while [[ $_retries -ne 0 ]]; do _ip=$(sudo virsh domifaddr ${VM_NAME} | grep -o --colour=never 192.168.140.[[:digit:]]* | cat ) @@ -201,7 +238,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 +259,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 +281,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 +306,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 +341,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!" |