From: pmikus Date: Fri, 17 Mar 2023 13:24:42 +0000 (+0000) Subject: feat(bootstrap): CDash CI X-Git-Url: https://gerrit.fd.io/r/gitweb?p=csit.git;a=commitdiff_plain;h=d66bbe4d85079621e13d6e2940c933da1297ba2b feat(bootstrap): CDash CI Signed-off-by: pmikus Change-Id: Ia9f5b1c9036b9f09c4d9bb041a41e9b49d2ead7f --- diff --git a/csit.infra.dash/do_release.sh b/csit.infra.dash/do_release.sh deleted file mode 100755 index 87e3b3acb4..0000000000 --- a/csit.infra.dash/do_release.sh +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env bash - -set -xuo pipefail - -command -v zip || exit 1 - -rm -f app.zip - -pushd app -find . -type d -name "__pycache__" -exec rm -rf "{}" \; -find . -type d -name ".webassets-cache" -exec rm -rf "{}" \; -zip -r ../app.zip . -popd - -pushd "../fdio.infra.terraform/" -pushd "terraform-aws-fdio-csit-dash-app-base" -export BUILD_ID=49 -export TF_VAR_application_version="${BUILD_ID-}" -export TF_LOG=INFO -rm -f terraform.tfstate -terraform init -terraform validate -terraform apply -no-color -auto-approve -application_version="$(terraform output application_version)" -popd -popd - -#aws --region eu-central-1 elasticbeanstalk update-environment \ -# --environment-name fdio-csit-dash-env \ -# --version-label "${application_version}" diff --git a/fdio.infra.terraform/terraform-aws-fdio-csit-dash-app-base/main.tf b/fdio.infra.terraform/terraform-aws-fdio-csit-dash-app-base/main.tf index aa76701f15..7eb5ea59b9 100644 --- a/fdio.infra.terraform/terraform-aws-fdio-csit-dash-app-base/main.tf +++ b/fdio.infra.terraform/terraform-aws-fdio-csit-dash-app-base/main.tf @@ -2,7 +2,7 @@ locals { bucket = var.application_bucket description = var.application_description name = var.application_name - name_version = "${var.application_name}-base-${var.application_version}" + name_version = "${var.application_name}-base-1.${var.application_version}" source = var.application_source } diff --git a/resources/libraries/bash/entry/bootstrap_docs.sh b/resources/libraries/bash/entry/bootstrap_aws_eb_version_deploy.sh similarity index 79% rename from resources/libraries/bash/entry/bootstrap_docs.sh rename to resources/libraries/bash/entry/bootstrap_aws_eb_version_deploy.sh index 9d2519ebf3..15d6dae405 100755 --- a/resources/libraries/bash/entry/bootstrap_docs.sh +++ b/resources/libraries/bash/entry/bootstrap_aws_eb_version_deploy.sh @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Cisco and/or its affiliates. +# Copyright (c) 2023 Cisco and/or its affiliates. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at: @@ -25,8 +25,8 @@ source "${BASH_FUNCTION_DIR}/common.sh" || { echo "Source failed." >&2 exit 1 } -source "${BASH_FUNCTION_DIR}/docs.sh" || die "Source failed." +source "${BASH_FUNCTION_DIR}/eb_version.sh" || die "Source failed." +source "${BASH_FUNCTION_DIR}/hugo.sh" || die "Source failed." +source "${BASH_FUNCTION_DIR}/terraform.sh" || die "Source failed." common_dirs || die -activate_virtualenv || die -generate_docs || die -die_on_docs_error || die +eb_version_deploy || die diff --git a/resources/libraries/bash/entry/bootstrap_trending.sh b/resources/libraries/bash/entry/bootstrap_aws_eb_version_verify.sh similarity index 79% rename from resources/libraries/bash/entry/bootstrap_trending.sh rename to resources/libraries/bash/entry/bootstrap_aws_eb_version_verify.sh index b172a81be5..c3b82574af 100755 --- a/resources/libraries/bash/entry/bootstrap_trending.sh +++ b/resources/libraries/bash/entry/bootstrap_aws_eb_version_verify.sh @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Cisco and/or its affiliates. +# Copyright (c) 2023 Cisco and/or its affiliates. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at: @@ -25,8 +25,8 @@ source "${BASH_FUNCTION_DIR}/common.sh" || { echo "Source failed." >&2 exit 1 } -source "${BASH_FUNCTION_DIR}/docs.sh" || die "Source failed." +source "${BASH_FUNCTION_DIR}/eb_version.sh" || die "Source failed." +source "${BASH_FUNCTION_DIR}/hugo.sh" || die "Source failed." +source "${BASH_FUNCTION_DIR}/terraform.sh" || die "Source failed." common_dirs || die -activate_virtualenv || die -generate_trending || die -die_on_docs_error || die +eb_version_verify || die diff --git a/resources/libraries/bash/entry/bootstrap_report.sh b/resources/libraries/bash/entry/bootstrap_report.sh index 191f910171..47a9d2e3d4 100755 --- a/resources/libraries/bash/entry/bootstrap_report.sh +++ b/resources/libraries/bash/entry/bootstrap_report.sh @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Cisco and/or its affiliates. +# Copyright (c) 2023 Cisco and/or its affiliates. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at: @@ -25,8 +25,8 @@ source "${BASH_FUNCTION_DIR}/common.sh" || { echo "Source failed." >&2 exit 1 } -source "${BASH_FUNCTION_DIR}/docs.sh" || die "Source failed." +source "${BASH_FUNCTION_DIR}/eb_version.sh" || die "Source failed." common_dirs || die activate_virtualenv || die generate_report || die -die_on_docs_error || die +die_on_error || die diff --git a/resources/libraries/bash/function/common.sh b/resources/libraries/bash/function/common.sh index b1979174e5..d98db18149 100644 --- a/resources/libraries/bash/function/common.sh +++ b/resources/libraries/bash/function/common.sh @@ -640,6 +640,8 @@ function prepare_topology () { # - TEST_CODE - String affecting test selection, usually jenkins job name. # - NODENESS - Node multiplicity of testbed, either "2n" or "3n". # - FLAVOR - Node flavor string, e.g. "clx" or "skx". + # Variables set: + # - TERRAFORM_MODULE_DIR - Terraform module directory. # Functions called: # - die - Print to stderr and exit. # - terraform_init - Terraform init topology. @@ -651,7 +653,11 @@ function prepare_topology () { case "${case_text}" in "1n_aws" | "2n_aws" | "3n_aws") export TF_VAR_testbed_name="${TEST_CODE}" + TERRAFORM_MODULE_DIR="terraform-aws-${NODENESS}-${FLAVOR}-c5n" terraform_init || die "Failed to call terraform init." + trap 'terraform_destroy' ERR || { + die "Trap attempt failed, please cleanup manually. Aborting!" + } terraform_apply || die "Failed to call terraform apply." ;; esac @@ -1197,7 +1203,8 @@ function untrap_and_unreserve_testbed () { # Variables read (by inner function): # - WORKING_TOPOLOGY - Path to topology yaml file of the reserved testbed. # - PYTHON_SCRIPTS_DIR - Path to directory holding Python scripts. - # Variables written: + # Variables set: + # - TERRAFORM_MODULE_DIR - Terraform module directory. # - WORKING_TOPOLOGY - Set to empty string on successful unreservation. # Trap unregistered: # - EXIT - Failure to untrap is reported, but ignored otherwise. @@ -1219,6 +1226,7 @@ function untrap_and_unreserve_testbed () { } case "${TEST_CODE}" in *"1n-aws"* | *"2n-aws"* | *"3n-aws"*) + TERRAFORM_MODULE_DIR="terraform-aws-${NODENESS}-${FLAVOR}-c5n" terraform_destroy || die "Failed to call terraform destroy." ;; *) diff --git a/resources/libraries/bash/function/docs.sh b/resources/libraries/bash/function/docs.sh deleted file mode 100644 index cb3f36d21a..0000000000 --- a/resources/libraries/bash/function/docs.sh +++ /dev/null @@ -1,267 +0,0 @@ -#!/usr/bin/env bash - -# Copyright (c) 2021 Cisco and/or its affiliates. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at: -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -set -exuo pipefail - - -function die_on_docs_error () { - - # Source this fragment if you want to abort on any failure. - # - # Variables read: - # - DOCS_EXIT_STATUS - Set by a generation function. - # Functions called: - # - die - Print to stderr and exit. - - set -exuo pipefail - - if [[ "${DOCS_EXIT_STATUS}" != "0" ]]; then - die "Failed to generate docs!" "${DOCS_EXIT_STATUS}" - fi -} - -function generate_docs () { - - # Generate docs content. - # - # Variable read: - # - ${TOOLS_DIR} - Path to existing resources subdirectory "tools". - # Variables set: - # - DOCS_EXIT_STATUS - Exit status of docs generation. - # Functions called: - # - die - Print to stderr and exit. - - set -exuo pipefail - - pushd "${TOOLS_DIR}"/doc_gen || die "Pushd failed!" - - BUILD_DIR="_build" - - # Remove the old build: - rm -rf ${BUILD_DIR} || true - rm -rf /tmp/tmp-csit* || true - - export WORKING_DIR=$(mktemp -d /tmp/tmp-csitXXX) || die "export failed" - - # Create working directories - mkdir -p "${BUILD_DIR}" || die "Mkdir failed!" - mkdir -p "${WORKING_DIR}"/resources/libraries/python/ || die "Mkdir failed!" - mkdir -p "${WORKING_DIR}"/resources/libraries/robot/ || die "Mkdir failed!" - mkdir -p "${WORKING_DIR}"/tests/ || die "Mkdir failed!" - - # Copy the Sphinx source files: - cp -r src/* ${WORKING_DIR}/ || die "Copy the Sphinx source files failed!" - - # Copy the source files to be processed: - from_dir="${RESOURCES_DIR}/libraries/python/" - to_dir="${WORKING_DIR}/resources/libraries/python/" - dirs="${from_dir} ${to_dir}" - rsync -ar --include='*/' --include='*.py' --exclude='*' ${dirs} || { - die "rSync failed!" - } - - from_dir="${RESOURCES_DIR}/libraries/robot/" - to_dir="${WORKING_DIR}/resources/libraries/robot/" - dirs="${from_dir} ${to_dir}" - rsync -ar --include='*/' --include '*.robot' --exclude '*' ${dirs} || { - die "rSync failed!" - } - touch ${to_dir}/index.robot || { - die "Touch index.robot file failed!" - } - - from_dir="${CSIT_DIR}/tests/" - to_dir="${WORKING_DIR}/tests/" - dirs="${from_dir} ${to_dir}" - rsync -ar --include='*/' --include '*.robot' --exclude '*' ${dirs} || { - die "rSync failed!" - } - - # to remove GPL licence section - find "${WORKING_DIR}/tests/" -type f -exec sed -i '/\*\*\*/,$!d' {} \; - - find ${WORKING_DIR}/ -type d -exec echo {} \; -exec touch {}/__init__.py \; - - python3 gen_rst.py || die "Generate .rst files failed!" - - # Generate the documentation: - DATE=$(date -u '+%d-%b-%Y') || die "Get date failed!" - - all_options=("-v") - all_options+=("-c" "${WORKING_DIR}") - all_options+=("-a") - all_options+=("-b" "html") - all_options+=("-E") - all_options+=("-D" "version="${GERRIT_BRANCH:-master}"") - all_options+=("${WORKING_DIR}" "${BUILD_DIR}/") - - set +e - sphinx-build "${all_options[@]}" - DOCS_EXIT_STATUS="$?" - set -e -} - -function generate_report () { - - # Generate report content. - # - # Variable read: - # - ${TOOLS_DIR} - Path to existing resources subdirectory "tools". - # - ${GERRIT_BRANCH} - Gerrit branch used for release tagging. - # Variables set: - # - DOCS_EXIT_STATUS - Exit status of report generation. - # Functions called: - # - die - Print to stderr and exit. - - set -exuo pipefail - - pushd "${TOOLS_DIR}"/presentation || die "Pushd failed!" - - # Set default values in config array. - typeset -A CFG - typeset -A DIR - - DIR[WORKING]="_tmp" - - # Create working directories. - mkdir "${DIR[WORKING]}" || die "Mkdir failed!" - - export PYTHONPATH=`pwd`:`pwd`/../../../ || die "Export failed!" - - all_options=("pal.py") - all_options+=("--specification" "specifications/report") - all_options+=("--release" "${GERRIT_BRANCH:-master}") - all_options+=("--week" $(date "+%V")) - all_options+=("--logging" "INFO") - all_options+=("--force") - - set +e - python "${all_options[@]}" - DOCS_EXIT_STATUS="$?" - set -e - -} - -function generate_report_local () { - - # Generate report from local content. - # - # Variable read: - # - ${TOOLS_DIR} - Path to existing resources subdirectory "tools". - # - ${CSIT_REPORT_FILENAME} - Source filename. - # - ${CSIT_REPORT_DIRECTORYNAME} - Source directory. - # - ${CSIT_REPORT_INSTALL_DEPENDENCIES} - Whether to install dependencies. - # - ${CSIT_REPORT_INSTALL_LATEX} - Whether to install latex. - # Variables set: - # - DOCS_EXIT_STATUS - Exit status of report generation. - # Functions called: - # - die - Print to stderr and exit. - - set -exuo pipefail - - pushd "${TOOLS_DIR}"/presentation || die "Pushd failed!" - - filename="${CSIT_REPORT_FILENAME-}" - directoryname="${CSIT_REPORT_DIRECTORYNAME-}" - install_dependencies="${CSIT_REPORT_INSTALL_DEPENDENCIES:-1}" - install_latex="${CSIT_REPORT_INSTALL_LATEX:-0}" - - # Set default values in config array. - typeset -A CFG - typeset -A DIR - - DIR[WORKING]="_tmp" - - # Install system dependencies. - if [[ ${install_dependencies} -eq 1 ]] ; - then - sudo apt -y update || die "APT update failed!" - sudo apt -y install libxml2 libxml2-dev libxslt-dev \ - build-essential zlib1g-dev unzip || die "APT install failed!" - fi - - if [[ ${install_latex} -eq 1 ]] ; - then - sudo apt -y update || die "APT update failed!" - sudo apt -y install xvfb texlive-latex-recommended \ - texlive-fonts-recommended texlive-fonts-extra texlive-latex-extra \ - latexmk wkhtmltopdf inkscape || die "APT install failed!" - target="/usr/share/texlive/texmf-dist/web2c/texmf.cnf" - sudo sed -i.bak 's/^\(main_memory\s=\s\).*/\110000000/' "${target}" || { - die "Patching latex failed!" - } - fi - - # Create working directories. - mkdir "${DIR[WORKING]}" || die "Mkdir failed!" - - export PYTHONPATH=`pwd`:`pwd`/../../../ || die "Export failed!" - - all_options=("pal.py") - all_options+=("--specification" "specifications/report_local") - all_options+=("--release" "${RELEASE:-master}") - all_options+=("--week" "${WEEK:-1}") - all_options+=("--logging" "INFO") - all_options+=("--force") - if [[ ${filename} != "" ]]; then - all_options+=("--input-file" "${filename}") - fi - if [[ ${directoryname} != "" ]]; then - all_options+=("--input-directory" "${directoryname}") - fi - - set +e - python "${all_options[@]}" - DOCS_EXIT_STATUS="$?" - set -e - -} - -function generate_trending () { - - # Generate trending content. - # - # Variable read: - # - ${TOOLS_DIR} - Path to existing resources subdirectory "tools". - # Variables set: - # - DOCS_EXIT_STATUS - Exit status of trending generation. - # Functions called: - # - die - Print to stderr and exit. - - set -exuo pipefail - - pushd "${TOOLS_DIR}"/presentation || die "Pushd failed!" - - # Set default values in config array. - typeset -A DIR - - DIR[WORKING]="_tmp" - - # Create working directories. - mkdir "${DIR[WORKING]}" || die "Mkdir failed!" - - export PYTHONPATH=`pwd`:`pwd`/../../../ || die "Export failed!" - - all_options=("pal.py") - all_options+=("--specification" "specifications/trending") - all_options+=("--logging" "INFO") - all_options+=("--force") - - set +e - python "${all_options[@]}" - DOCS_EXIT_STATUS="$?" - set -e - -} diff --git a/resources/libraries/bash/function/eb_version.sh b/resources/libraries/bash/function/eb_version.sh new file mode 100644 index 0000000000..7e1fdc5b6c --- /dev/null +++ b/resources/libraries/bash/function/eb_version.sh @@ -0,0 +1,158 @@ +#!/usr/bin/env bash + +# Copyright (c) 2023 Cisco and/or its affiliates. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -exuo pipefail + + +function die_on_error () { + + # Source this fragment if you want to abort on any failure. + # + # Variables read: + # - CODE_EXIT_STATUS - Exit status of report generation. + # Functions called: + # - die - Print to stderr and exit. + + set -exuo pipefail + + if [[ "${CODE_EXIT_STATUS}" != "0" ]]; then + die "Failed to generate docs!" "${CODE_EXIT_STATUS}" + fi +} + + +function eb_version_deploy () { + + # Deploy Elastic Beanstalk CDash content. + # + # Variable read: + # - ${CSIT_DIR} - CSIT main directory. + # - ${TERRAFORM_OUTPUT_VAL} - Terraform output value. + # Variables set: + # - CODE_EXIT_STATUS - Exit status of report generation. + # - TERRAFORM_OUTPUT_VAR - Register Terraform output variable name. + # Functions called: + # - eb_version_verify - Build and verify EB version. + # - terraform_apply - Apply EB version by Terraform. + # - terraform_output - Get the application name string from Terraform. + # - die - Print to stderr and exit. + + set -exuo pipefail + + eb_version_verify || die "Failed to call Elastic Beanstalk verify!" + terraform_apply || die "Failed to call Terraform apply!" + + TERRAFORM_OUTPUT_VAR="application_version" + terraform_output || die "Failed to call Terraform output!" + + #aws --region eu-central-1 elasticbeanstalk update-environment \ + # --environment-name fdio-csit-dash-env \ + # --version-label "${TERRAFORM_OUTPUT_VAL}" +} + + +function eb_version_verify () { + + # Build and verify Elastic Beanstalk CDash integrity. + # + # Variable read: + # - ${CSIT_DIR} - CSIT main directory. + # Variables set: + # - TERRAFORM_MODULE_DIR - Terraform module sub-directory. + # Functions called: + # - hugo_init_modules - Initialize Hugo modules. + # - hugo_build_site - Build static site with Hugo. + # - terraform_init - Initialize Terraform modules. + # - terraform_validate - Validate Terraform code. + # - die - Print to stderr and exit. + + set -exuo pipefail + + if ! installed zip; then + die "Please install zip!" + fi + + pushd "${CSIT_DIR}"/csit.infra.dash || die "Pushd failed!" + pushd app || die "Pushd failed!" + find . -type d -name "__pycache__" -exec rm -rf "{}" \; + find . -type d -name ".webassets-cache" -exec rm -rf "{}" \; + zip -r ../app.zip . || die "Compress failed!" + popd || die "Popd failed!" + popd || die "Popd failed!" + + TERRAFORM_MODULE_DIR="terraform-aws-fdio-csit-dash-app-base" + + export TF_VAR_application_version="${BUILD_ID-50}" + hugo_init_modules || die "Failed to call Hugo initialize!" + hugo_build_site || die "Failed to call Hugo build!" + terraform_init || die "Failed to call terraform init!" + terraform_validate || die "Failed to call terraform validate!" +} + + +function generate_report () { + + # Generate report content. + # + # Variable read: + # - ${TOOLS_DIR} - Path to existing resources subdirectory "tools". + # - ${GERRIT_BRANCH} - Gerrit branch used for release tagging. + # Variables set: + # - CODE_EXIT_STATUS - Exit status of report generation. + # Functions called: + # - die - Print to stderr and exit. + + set -exuo pipefail + + pushd "${TOOLS_DIR}"/presentation || die "Pushd failed!" + + # Set default values in config array. + typeset -A CFG + typeset -A DIR + + DIR[WORKING]="_tmp" + + # Create working directories. + mkdir "${DIR[WORKING]}" || die "Mkdir failed!" + + export PYTHONPATH=`pwd`:`pwd`/../../../ || die "Export failed!" + + all_options=("pal.py") + all_options+=("--specification" "specifications/report") + all_options+=("--release" "${GERRIT_BRANCH:-master}") + all_options+=("--week" $(date "+%V")) + all_options+=("--logging" "INFO") + all_options+=("--force") + + set +e + python "${all_options[@]}" + CODE_EXIT_STATUS="$?" + set -e +} + +function installed () { + + # Check if the given utility is installed. Fail if not installed. + # + # Arguments: + # - ${1} - Utility to check. + # Returns (implicitly): + # - 0 - If command is installed. + # - 1 - If command is not installed. + + set -exuo pipefail + + command -v "${1}" +} diff --git a/resources/libraries/bash/function/hugo.sh b/resources/libraries/bash/function/hugo.sh new file mode 100644 index 0000000000..b8d4c87df2 --- /dev/null +++ b/resources/libraries/bash/function/hugo.sh @@ -0,0 +1,100 @@ +#!/usr/bin/env bash + +# Copyright (c) 2023 Cisco and/or its affiliates. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -exuo pipefail + + +function go_install () { + + # Install Go. + + go_version="go1.20.2.linux-arm64.tar.gz" + go_url="https://go.dev/dl" + wget "${go_url}/${go_version}" + rm -rf "/usr/local/go" + tar -C "/usr/local" -xzf "go1.20.2.linux-arm64.tar.gz" + rm "go1.20.2.linux-arm64.tar.gz" + export PATH=$PATH:/usr/local/go/bin +} + + +function hugo_build_site () { + + # Build site via Hugo. + # + # Variable read: + # - ${CSIT_DIR} - CSIT main directory. + # Functions called: + # - die - Print to stderr and exit. + + if ! installed hugo; then + die "Please install Hugo!" + fi + + pushd "${CSIT_DIR}"/docs || die "Pushd failed!" + hugo || die "Failed to run Hugo build!" + popd || die "Popd failed!" +} + + +function hugo_init_modules () { + + # Initialize Hugo modules. + # + # Variable read: + # - ${CSIT_DIR} - CSIT main directory. + # Functions called: + # - die - Print to stderr and exit. + + if ! installed hugo; then + #die "Please install Hugo!" + go_install || die "Failed to install Go!" + hugo_install || die "Failed to install Hugo!" + terraform_install || die "Failed to install Terraform!" + fi + + pushd "${CSIT_DIR}"/docs || die "Pushd failed!" + hugo mod get -u || die "Failed to run Hugo mod!" + popd || die "Popd failed!" +} + + +function hugo_install () { + + # Install Hugo Extended. + + hugo_version="v0.111.3/hugo_extended_0.111.3_linux-arm64.deb" + hugo_url="https://github.com/gohugoio/hugo/releases/download" + hugo_link="${hugo_url}/${hugo_version}" + wget -O "hugo.deb" "${hugo_link}" || die "Failed to install Hugo!" + dpkg -i "hugo.deb" || die "Failed to install Hugo!" + rm "hugo.deb" || die "Failed to install Hugo!" +} + + +function installed () { + + # Check if the given utility is installed. Fail if not installed. + # + # Arguments: + # - ${1} - Utility to check. + # Returns (implicitly): + # - 0 - If command is installed. + # - 1 - If command is not installed. + + set -exuo pipefail + + command -v "${1}" +} diff --git a/resources/libraries/bash/function/terraform.sh b/resources/libraries/bash/function/terraform.sh index 1766381f75..e47f38937c 100644 --- a/resources/libraries/bash/function/terraform.sh +++ b/resources/libraries/bash/function/terraform.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -# Copyright (c) 2021 Cisco and/or its affiliates. +# Copyright (c) 2023 Cisco and/or its affiliates. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at: @@ -22,8 +22,7 @@ function terraform_apply () { # # Variable read: # - ${CSIT_DIR} - CSIT main directory, where terraform modules are located. - # - ${NODENESS} - Node multiplicity of desired testbed. - # - ${FLAVOR} - Node flavor string, see common.sh + # - ${TERRAFORM_MODULE_DIR} - Terraform module directory. set -exuo pipefail @@ -32,24 +31,21 @@ function terraform_apply () { fi pushd "${CSIT_DIR}"/fdio.infra.terraform || die "Pushd failed!" - pushd "terraform-aws-${NODENESS}-${FLAVOR}-c5n" || die "Pushd failed!" + pushd "${TERRAFORM_MODULE_DIR}" || die "Pushd failed!" export TF_LOG=INFO - trap 'terraform_destroy' ERR || { - die "Trap attempt failed, please cleanup manually. Aborting!" - } terraform apply -no-color -auto-approve || die "Terraform apply failed!" popd || die "Popd failed!" popd || die "Popd failed!" } + function terraform_destroy () { # Run terraform destroy command to prepare module. # # Variable read: # - ${CSIT_DIR} - CSIT main directory, where terraform modules are located. - # - ${NODENESS} - Node multiplicity of desired testbed. - # - ${FLAVOR} - Node flavor string, see common.sh + # - ${TERRAFORM_MODULE_DIR} - Terraform module directory. set -exuo pipefail @@ -58,7 +54,7 @@ function terraform_destroy () { fi pushd "${CSIT_DIR}"/fdio.infra.terraform || die "Pushd failed!" - pushd "terraform-aws-${NODENESS}-${FLAVOR}-c5n" || die "Pushd failed!" + pushd "${TERRAFORM_MODULE_DIR}" || die "Pushd failed!" export TF_LOG=INFO terraform destroy -auto-approve -no-color || die "Terraform destroy failed!" popd || die "Popd failed!" @@ -72,23 +68,16 @@ function terraform_init () { # # Variable read: # - ${CSIT_DIR} - CSIT main directory, where terraform modules are located. - # - ${NODENESS} - Node multiplicity of desired testbed. - # - ${FLAVOR} - Node flavor string, see common.sh + # - ${TERRAFORM_MODULE_DIR} - Terraform module directory. set -exuo pipefail if ! installed terraform; then - curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add - - os="$(lsb_release -cs)" || die "Failed to get OS release!" - repo="deb [arch=amd64] https://apt.releases.hashicorp.com ${os} main" - sudo apt-add-repository "${repo}" || die "Failed to add repo!" - apt update -y - apt install -y terraform - #die "Please install terraform!" + die "Please install terraform!" fi pushd "${CSIT_DIR}"/fdio.infra.terraform || die "Pushd failed!" - pushd "terraform-aws-${NODENESS}-${FLAVOR}-c5n" || die "Pushd failed!" + pushd "${TERRAFORM_MODULE_DIR}" || die "Pushd failed!" plugin_url="https://github.com/radekg/terraform-provisioner-ansible/" plugin_url+="releases/download/v2.5.0/" @@ -100,9 +89,69 @@ function terraform_init () { wget -O "${plugin_path}" "${plugin_url}" || die "Failed to download plugin!" chmod +x "${plugin_path}" || die "Failed to add execute rights!" + rm -f terraform.tfstate || die "Failed to clear terraform state!" export TF_LOG=INFO terraform init || die "Failed to run terraform init!" + popd || die "Popd failed!" + popd || die "Popd failed!" +} + + +function terraform_install () { + + # Install terraform. + + terraform_version="1.4.2/terraform_1.4.2_linux_arm64.zip" + terraform_url="https://releases.hashicorp.com/terraform" + terraform_link="${terraform_url}/${terraform_version}" + wget "${terraform_link}" || die "Failed to install Terraform!" + unzip "terraform_1.4.2_linux_arm64.zip" || die "Failed to install Terraform!" + mv "terraform" "/usr/local/bin" || die "Failed to install Terraform!" + rm "terraform_1.4.2_linux_arm64.zip" || die "Failed to install Terraform!" +} + +function terraform_output () { + + # Run terraform output command to prepare module. + # + # Variable read: + # - ${CSIT_DIR} - CSIT main directory, where terraform modules are located. + # - ${TERRAFORM_MODULE_DIR} - Terraform module directory. + # - ${TERRAFORM_OUTPUT_VAR} - Terraform variable to export. + + set -exuo pipefail + + if ! installed terraform; then + die "Please install terraform!" + fi + + pushd "${CSIT_DIR}"/fdio.infra.terraform || die "Pushd failed!" + pushd "${TERRAFORM_MODULE_DIR}" || die "Pushd failed!" + TERRAFORM_OUTPUT_VAL=$(terraform output --raw "${TERRAFORM_OUTPUT_VAR}") + popd || die "Popd failed!" + popd || die "Popd failed!" +} + + +function terraform_validate () { + + # Run terraform validate command to prepare module. + # + # Variable read: + # - ${CSIT_DIR} - CSIT main directory, where terraform modules are located. + # - ${TERRAFORM_MODULE_DIR} - Terraform module directory. + + set -exuo pipefail + + if ! installed terraform; then + die "Please install terraform!" + fi + + pushd "${CSIT_DIR}"/fdio.infra.terraform || die "Pushd failed!" + pushd "${TERRAFORM_MODULE_DIR}" || die "Pushd failed!" + export TF_LOG=INFO + terraform validate || die "Terraform validate failed!" popd || die "Popd failed!" popd || die "Popd failed!" }