Fixing broken copy of BMRA host_vars
[kuberef.git] / functions.sh
index 55edfbf..4d75ee1 100755 (executable)
@@ -20,6 +20,58 @@ clean_up() {
     sleep 5
 }
 
+
+check_prerequisites() {
+    echo "Info  : Check prerequisites"
+
+    #-------------------------------------------------------------------------------
+    # We shouldn't be running as root
+    #-------------------------------------------------------------------------------
+    if [[ "$(whoami)" == "root" ]]; then
+        echo "ERROR : This script must not be run as root!"
+        echo "        Please switch to a regular user before running the script."
+        exit 1
+    fi
+
+    #-------------------------------------------------------------------------------
+    # Check for passwordless sudo
+    #-------------------------------------------------------------------------------
+    if ! sudo -n "true"; then
+        echo "ERROR : passwordless sudo is needed for '$(id -nu)' user."
+        exit 1
+    fi
+
+    #-------------------------------------------------------------------------------
+    # Check if SSH key exists
+    #-------------------------------------------------------------------------------
+    if [[ ! -f "$HOME/.ssh/id_rsa" ]]; then
+        echo "ERROR : You must have SSH keypair in order to run this script!"
+        exit 1
+    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 Ansible is installed
+    #-------------------------------------------------------------------------------
+    if ! command -v ansible &> /dev/null; then
+        echo "ERROR : Ansible not found. Please install."
+        exit 1
+    fi
+
+    #-------------------------------------------------------------------------------
+    # Check is libvirt is installed
+    #-------------------------------------------------------------------------------
+    if ! command -v virsh &> /dev/null; then
+        echo "ERROR : Libvirt not found. Please install."
+        exit 1
+    fi
+}
+
+
 # Create jumphost VM
 create_jump() {
 # Create VM image
@@ -46,7 +98,8 @@ 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-customize -a "/var/lib/libvirt/images/$VM_NAME/$VM_NAME.qcow2" \
+        --root-password password:"$ROOT_PASSWORD"
     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 \
@@ -104,7 +157,7 @@ copy_files_jump() {
 # Host Provisioning
 provision_hosts() {
     # shellcheck disable=SC2087
-    ssh -tT "$USERNAME"@"$(get_vm_ip)" << EOF
+    ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
 # Install and run cloud-infra
 if [ ! -d "${PROJECT_ROOT}/engine" ]; then
     ssh-keygen -t rsa -N "" -f ${PROJECT_ROOT}/.ssh/id_rsa
@@ -127,7 +180,7 @@ setup_network() {
     WORKER_IP=$(yq r "$CURRENTPATH"/hw_config/"$VENDOR"/pdf.yaml nodes.[1].interfaces.["$PXE_IF_INDEX"].address)
 # SSH to jumphost
     # shellcheck disable=SC2087
-    ssh -tT  "$USERNAME"@"$(get_vm_ip)" << EOF
+    ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
 ssh -o StrictHostKeyChecking=no root@$MASTER_IP \
     'bash -s' <  ${PROJECT_ROOT}/${VENDOR}/setup_network.sh
 ssh -o StrictHostKeyChecking=no root@$WORKER_IP \
@@ -138,28 +191,29 @@ EOF
 # k8s Provisioning (currently BMRA)
 provision_k8s() {
     # shellcheck disable=SC2087
-    ssh -tT  "$USERNAME"@"$(get_vm_ip)" << EOF
+    ssh -o StrictHostKeyChecking=no -tT "$USERNAME"@"$(get_vm_ip)" << EOF
 # Install BMRA
-if [ ! -d "${PROJECT_ROOT}/container-experience-kits" ]; then
+if ! command -v docker; then
     curl -fsSL https://get.docker.com/ | sh
     printf "Waiting for docker service..."
     until sudo docker info; do
         printf "."
         sleep 2
     done
-    git clone https://github.com/intel/container-experience-kits.git
-    cd ${PROJECT_ROOT}/container-experience-kits
-    git checkout v1.4.1
-    git submodule update --init
-    cp -r examples/group_vars examples/host_vars .
-    cp ${PROJECT_ROOT}/${INSTALLER}/inventory.ini \
+fi
+if [ ! -d "${PROJECT_ROOT}/container-experience-kits" ]; then
+    git clone --recurse-submodules --depth 1 https://github.com/intel/container-experience-kits.git -b v1.4.1 ${PROJECT_ROOT}/container-experience-kits/
+    cp -r ${PROJECT_ROOT}/container-experience-kits/examples/{group_vars,host_vars} ${PROJECT_ROOT}/container-experience-kits/
+#TODO Remove this once the reported issue is fixed in the next BMRA Release
+    sed -i '/\openshift/a \    extra_args: --ignore-installed PyYAML' \
+         ${PROJECT_ROOT}/container-experience-kits/roles/net-attach-defs-create/tasks/main.yml
+fi
+cp ${PROJECT_ROOT}/${INSTALLER}/inventory.ini \
     ${PROJECT_ROOT}/container-experience-kits/
-    cp ${PROJECT_ROOT}/${INSTALLER}/all.yml \
+cp ${PROJECT_ROOT}/${INSTALLER}/all.yml \
     ${PROJECT_ROOT}/container-experience-kits/group_vars/
-    cp ${PROJECT_ROOT}/${INSTALLER}/node1.yml \
+cp ${PROJECT_ROOT}/${INSTALLER}/node1.yml \
     ${PROJECT_ROOT}/container-experience-kits/host_vars/
-fi
-sudo service docker start
 sudo docker run --rm \
 -v ${PROJECT_ROOT}/container-experience-kits:/bmra \
 -v ~/.ssh/:/root/.ssh/ rihabbanday/bmra-install:centos \