3 # Copyright (c) 2023 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
19 function terraform_apply () {
21 # Run terraform apply command to prepare topology.
24 # - ${CSIT_DIR} - CSIT main directory, where terraform modules are located.
25 # - ${TERRAFORM_MODULE_DIR} - Terraform module directory.
29 if ! installed terraform; then
30 die "Please install terraform!"
33 pushd "${CSIT_DIR}"/fdio.infra.terraform || die "Pushd failed!"
34 pushd "${TERRAFORM_MODULE_DIR}" || die "Pushd failed!"
36 terraform apply -no-color -auto-approve || die "Terraform apply failed!"
37 popd || die "Popd failed!"
38 popd || die "Popd failed!"
42 function terraform_destroy () {
44 # Run terraform destroy command to prepare module.
47 # - ${CSIT_DIR} - CSIT main directory, where terraform modules are located.
48 # - ${TERRAFORM_MODULE_DIR} - Terraform module directory.
52 if ! installed terraform; then
53 die "Please install terraform!"
56 pushd "${CSIT_DIR}"/fdio.infra.terraform || die "Pushd failed!"
57 pushd "${TERRAFORM_MODULE_DIR}" || die "Pushd failed!"
59 terraform destroy -auto-approve -no-color || die "Terraform destroy failed!"
60 popd || die "Popd failed!"
61 popd || die "Popd failed!"
65 function terraform_init () {
67 # Run terraform init command to prepare module.
70 # - ${CSIT_DIR} - CSIT main directory, where terraform modules are located.
71 # - ${TERRAFORM_MODULE_DIR} - Terraform module directory.
75 if ! installed terraform; then
76 die "Please install terraform!"
79 pushd "${CSIT_DIR}"/fdio.infra.terraform || die "Pushd failed!"
80 pushd "${TERRAFORM_MODULE_DIR}" || die "Pushd failed!"
82 plugin_url="https://github.com/radekg/terraform-provisioner-ansible/"
83 plugin_url+="releases/download/v2.5.0/"
84 plugin_url+="terraform-provisioner-ansible-linux-amd64_v2.5.0"
85 plugin_dir="${HOME}/.terraform.d/plugins/"
86 plugin_path+="${plugin_dir}terraform-provisioner-ansible_v2.5.0"
88 mkdir -p "${plugin_dir}" || die "Failed to create dir!"
89 wget -O "${plugin_path}" "${plugin_url}" || die "Failed to download plugin!"
90 chmod +x "${plugin_path}" || die "Failed to add execute rights!"
92 rm -f terraform.tfstate || die "Failed to clear terraform state!"
94 terraform init || die "Failed to run terraform init!"
95 popd || die "Popd failed!"
96 popd || die "Popd failed!"
100 function terraform_install () {
104 OS_ARCH=$(uname -m) || die "Failed to get arch."
106 x86_64) architecture="amd64" ;;
107 aarch64) architecture="arm64" ;;
110 terraform_version="1.4.2/terraform_1.4.2_linux_${architecture}.zip"
111 terraform_url="https://releases.hashicorp.com/terraform"
112 terraform_link="${terraform_url}/${terraform_version}"
113 wget "${terraform_link}" || die "Failed to install Terraform!"
114 unzip "terraform_1.4.2_linux_${architecture}.zip" || {
115 die "Failed to install Terraform!"
117 mv "terraform" "/usr/local/bin" || die "Failed to install Terraform!"
118 rm "terraform_1.4.2_linux_${architecture}.zip" || {
119 die "Failed to install Terraform!"
124 function terraform_output () {
126 # Run terraform output command to prepare module.
129 # - ${CSIT_DIR} - CSIT main directory, where terraform modules are located.
130 # - ${TERRAFORM_MODULE_DIR} - Terraform module directory.
131 # - ${TERRAFORM_OUTPUT_VAR} - Terraform variable to export.
135 if ! installed terraform; then
136 die "Please install terraform!"
139 pushd "${CSIT_DIR}"/fdio.infra.terraform || die "Pushd failed!"
140 pushd "${TERRAFORM_MODULE_DIR}" || die "Pushd failed!"
141 TERRAFORM_OUTPUT_VAL=$(terraform output --raw "${TERRAFORM_OUTPUT_VAR}")
142 popd || die "Popd failed!"
143 popd || die "Popd failed!"
147 function terraform_validate () {
149 # Run terraform validate command to prepare module.
152 # - ${CSIT_DIR} - CSIT main directory, where terraform modules are located.
153 # - ${TERRAFORM_MODULE_DIR} - Terraform module directory.
157 if ! installed terraform; then
158 die "Please install terraform!"
161 pushd "${CSIT_DIR}"/fdio.infra.terraform || die "Pushd failed!"
162 pushd "${TERRAFORM_MODULE_DIR}" || die "Pushd failed!"
164 terraform validate || die "Terraform validate failed!"
165 popd || die "Popd failed!"
166 popd || die "Popd failed!"
170 function installed () {
172 # Check if the given utility is installed. Fail if not installed.
175 # - ${1} - Utility to check.
176 # Returns (implicitly):
177 # - 0 - If command is installed.
178 # - 1 - If command is not installed.