Applying Piccolo theme
[kuberef.git] / functions.sh
index c4690ef..3ca3e09 100755 (executable)
@@ -4,6 +4,8 @@
 #
 # SPDX-License-Identifier: Apache-2.0
 
+OS_ID=$(grep '^ID=' /etc/os-release | cut -f2- -d= | sed -e 's/\"//g')
+
 info() {
     _print_msg "INFO" "$1"
 }
@@ -62,6 +64,90 @@ check_prerequisites() {
     #-------------------------------------------------------------------------------
     sudo sed -i "s/^Defaults.*env_reset/#&/" /etc/sudoers
 
+    #-------------------------------------------------------------------------------
+    # Installing prerequisites
+    #-------------------------------------------------------------------------------
+    if [ "$OS_ID" == "ubuntu" ]; then
+
+        sudo apt update -y
+        ansible --version
+        RESULT=$?
+        if [ $RESULT -ne 0 ]; then
+            sudo apt-add-repository --yes --update ppa:ansible/ansible
+            sudo apt-get install -y ansible
+        fi
+
+        yq --version
+        RESULT=$?
+        if [ $RESULT -ne 0 ]; then
+            sudo wget https://github.com/mikefarah/yq/releases/download/3.4.1/yq_linux_amd64 -O /usr/bin/yq
+            sudo chmod +x /usr/bin/yq
+        fi
+
+        virsh --version
+        RESULT=$?
+        if [ $RESULT -ne 0 ]; then
+            sudo apt-get install -y virsh
+        fi
+
+        jq --version
+        RESULT=$?
+        if [ $RESULT -ne 0 ]; then
+            sudo apt-get install -y jq
+        fi
+
+        virtualenv --version
+        RESULT=$?
+        if [ $RESULT -ne 0 ]; then
+            sudo apt-get install -y virtualenv
+        fi
+
+        pip --version
+        if [ $RESULT -ne 0 ]; then
+            sudo apt-get install -y pip
+        fi
+
+    elif [ "$OS_ID" == "centos" ]; then
+
+        sudo yum update -y
+        ansible --version
+        RESULT=$?
+        if [ $RESULT -ne 0 ]; then
+            sudo dnf install epel-release
+            sudo dnf install ansible
+        fi
+
+        yq --version
+        RESULT=$?
+        if [ $RESULT -ne 0 ]; then
+            sudo wget https://github.com/mikefarah/yq/releases/download/3.4.1/yq_linux_amd64 -O /usr/bin/yq
+            sudo chmod +x /usr/bin/yq
+        fi
+
+        virsh --version
+        RESULT=$?
+        if [ $RESULT -ne 0 ]; then
+            sudo yum install -y virsh
+        fi
+
+        jq --version
+        RESULT=$?
+        if [ $RESULT -ne 0 ]; then
+            sudo yum install -y jq
+        fi
+
+        virtualenv --version
+        RESULT=$?
+        if [ $RESULT -ne 0 ]; then
+            sudo yum install -y virtualenv
+        fi
+
+        pip --version
+        if [ $RESULT -ne 0 ]; then
+            sudo yum install -y pip
+        fi
+    fi
+
     #-------------------------------------------------------------------------------
     # Check if necessary tools are installed
     #-------------------------------------------------------------------------------
@@ -160,10 +246,9 @@ copy_files_jump() {
 
 # Host Provisioning
 provision_hosts_baremetal() {
+    CMD="./deploy.sh -s ironic -d ${DISTRO} -p file:///${PROJECT_ROOT}/engine/engine/pdf.yaml -i file:///${PROJECT_ROOT}/engine/engine/idf.yaml"
     if [ "${DEBUG:-false}" == "true" ]; then
-        DEBUG_FLAG="-v"
-    else
-        DEBUG_FLAG=""
+        CMD+=" -v"
     fi
 
     # shellcheck disable=SC2087
@@ -176,19 +261,11 @@ fi
 cp "${PROJECT_ROOT}"/"${VENDOR}"/{pdf.yaml,idf.yaml} \
 "${PROJECT_ROOT}"/engine/engine
 cd "${PROJECT_ROOT}"/engine/engine || return
-./deploy.sh -s ironic "${DEBUG_FLAG}" -d "${DISTRO}" \
--p file:///"${PROJECT_ROOT}"/engine/engine/pdf.yaml \
--i file:///"${PROJECT_ROOT}"/engine/engine/idf.yaml
+${CMD}
 EOF
 }
 
 provision_hosts_vms() {
-    if [ "${DEBUG:-false}" == "true" ]; then
-        DEBUG_FLAG="-v"
-    else
-        DEBUG_FLAG=""
-    fi
-
     # shellcheck disable=SC2087
     # Install and run cloud-infra
     if [ ! -d "${CURRENTPATH}/engine" ]; then
@@ -196,9 +273,12 @@ provision_hosts_vms() {
     fi
     cp "${CURRENTPATH}"/hw_config/"${VENDOR}"/{pdf.yaml,idf.yaml} "${CURRENTPATH}"/engine/engine
     cd "${CURRENTPATH}"/engine/engine || return
-    ./deploy.sh -s ironic "${DEBUG_FLAG}" \
-    -p file:///"${CURRENTPATH}"/engine/engine/pdf.yaml \
-    -i file:///"${CURRENTPATH}"/engine/engine/idf.yaml
+    CMD="./deploy.sh -s ironic -p file:///${CURRENTPATH}/engine/engine/pdf.yaml -i file:///${CURRENTPATH}/engine/engine/idf.yaml"
+    if [ "${DEBUG:-false}" == "true" ]; then
+        CMD+=" -v"
+    fi
+
+    ${CMD}
 }
 
 # Setup networking on provisioned hosts (Adapt setup_network.sh according to your network setup)
@@ -341,7 +421,7 @@ creates_virtualenv() {
     if [  ! -d "$CURRENTPATH/.venv" ]; then
         virtualenv "$CURRENTPATH/.venv"
     fi
-    # shellcheck disable=SC1090
+    # shellcheck disable=SC1090,SC1091
     source "$CURRENTPATH/.venv/bin/activate"
     pip install -r "$CURRENTPATH/requirements.txt"
 }