-# Create jumphost VM
-create_jump() {
-# Create VM image
- sudo mkdir -p "/var/lib/libvirt/images/$VM_NAME"
- sudo qemu-img create -f qcow2 \
- -o backing_file=/var/lib/libvirt/images/ubuntu-18.04.qcow2 \
- "/var/lib/libvirt/images/$VM_NAME/$VM_NAME.qcow2" 10G
-
-# Create VM cloud-init configuration files
- cat <<EOL > user-data
- #cloud-config
- users:
- - name: $USERNAME
- ssh-authorized-keys:
- - $(cat "$HOME/.ssh/id_rsa.pub")
- sudo: ['ALL=(ALL) NOPASSWD:ALL']
- groups: sudo
- shell: /bin/bash
-EOL
- cat <<EOL > meta-data
- local-hostname: $VM_NAME
-EOL
-
-# Create VM
- sudo genisoimage -output "/var/lib/libvirt/images/$VM_NAME/$VM_NAME-cidata.iso" \
- -volid cidata -joliet -rock user-data meta-data
-
- sudo virt-install --connect qemu:///system --name "$VM_NAME" \
- --ram 4096 --vcpus=4 --os-type linux --os-variant ubuntu16.04 \
- --disk path="/var/lib/libvirt/images/$VM_NAME/$VM_NAME.qcow2",format=qcow2 \
- --disk "/var/lib/libvirt/images/$VM_NAME/$VM_NAME-cidata.iso",device=cdrom \
- --import --network network=default --network bridge="$BRIDGE",model=rtl8139 --noautoconsole
- jumpbox_ip=$(get_vm_ip)
- i=0
- while [ -z "$jumpbox_ip" ]; do
- sleep $((++i))
- jumpbox_ip=$(get_vm_ip)
- done
- i=0
- until nc -w5 -z "$jumpbox_ip" 22; do
- sleep $((++i))
+error() {
+ _print_msg "ERROR" "$1"
+ exit 1
+}
+
+_print_msg() {
+ echo "$(date +%H:%M:%S) - $1: $2"
+}
+
+assert_non_empty() {
+ if [ -z "$1" ]; then
+ error "$2"
+ fi
+}
+if [ "${DEBUG:-false}" == "true" ]; then
+ set -o xtrace
+fi
+
+check_prerequisites() {
+ info "Check prerequisites"
+
+ #-------------------------------------------------------------------------------
+ # Check for DEPLOYMENT type
+ #-------------------------------------------------------------------------------
+ if ! [[ "$DEPLOYMENT" =~ ^(full|k8s)$ ]]; then
+ error "Unsupported value for DEPLOYMENT ($DEPLOYMENT)"
+ fi
+
+ #-------------------------------------------------------------------------------
+ # We shouldn't be running as root
+ #-------------------------------------------------------------------------------
+ if [[ "$(whoami)" == "root" ]] && [[ "$DEPLOYMENT" != "k8s" ]]; then
+ error "This script must not be run as root! Please switch to a regular user before running the script."
+ fi
+
+ #-------------------------------------------------------------------------------
+ # Check for passwordless sudo
+ #-------------------------------------------------------------------------------
+ if ! sudo -n "true"; then
+ error "passwordless sudo is needed for '$(id -nu)' user."
+ fi
+
+ #-------------------------------------------------------------------------------
+ # Check if SSH key exists
+ #-------------------------------------------------------------------------------
+ if [[ ! -f "$HOME/.ssh/id_rsa" ]]; then
+ error "You must have SSH keypair in order to run this script!"
+ fi
+
+ #-------------------------------------------------------------------------------
+ # We are using sudo so we need to make sure that env_reset is not present
+ #-------------------------------------------------------------------------------
+ sudo sed -i "s/^Defaults.*env_reset/#&/" /etc/sudoers
+
+ #-------------------------------------------------------------------------------
+ # Check if Python Virtual Environment is installed
+ #-------------------------------------------------------------------------------
+ if ! command -v virtualenv &> /dev/null; then
+ error "VirtualEnv not found. Please install."
+ fi
+
+ #-------------------------------------------------------------------------------
+ # Check if PIP Installs Packages is installed
+ #-------------------------------------------------------------------------------
+ if ! command -v pip &> /dev/null; then
+ error "PIP not found. Please install."
+ fi
+
+ #-------------------------------------------------------------------------------
+ # Check is libvirt is installed
+ #-------------------------------------------------------------------------------
+ for tool in ansible yq virsh jq; do
+ if ! command -v "$tool" &> /dev/null; then
+ error "$tool not found. Please install."
+ fi