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 terraform_version="1.4.2/terraform_1.4.2_linux_arm64.zip"
105 terraform_url="https://releases.hashicorp.com/terraform"
106 terraform_link="${terraform_url}/${terraform_version}"
107 wget "${terraform_link}" || die "Failed to install Terraform!"
108 unzip "terraform_1.4.2_linux_arm64.zip" || die "Failed to install Terraform!"
109 mv "terraform" "/usr/local/bin" || die "Failed to install Terraform!"
110 rm "terraform_1.4.2_linux_arm64.zip" || die "Failed to install Terraform!"
114 function terraform_output () {
116 # Run terraform output command to prepare module.
119 # - ${CSIT_DIR} - CSIT main directory, where terraform modules are located.
120 # - ${TERRAFORM_MODULE_DIR} - Terraform module directory.
121 # - ${TERRAFORM_OUTPUT_VAR} - Terraform variable to export.
125 if ! installed terraform; then
126 die "Please install terraform!"
129 pushd "${CSIT_DIR}"/fdio.infra.terraform || die "Pushd failed!"
130 pushd "${TERRAFORM_MODULE_DIR}" || die "Pushd failed!"
131 TERRAFORM_OUTPUT_VAL=$(terraform output --raw "${TERRAFORM_OUTPUT_VAR}")
132 popd || die "Popd failed!"
133 popd || die "Popd failed!"
137 function terraform_validate () {
139 # Run terraform validate command to prepare module.
142 # - ${CSIT_DIR} - CSIT main directory, where terraform modules are located.
143 # - ${TERRAFORM_MODULE_DIR} - Terraform module directory.
147 if ! installed terraform; then
148 die "Please install terraform!"
151 pushd "${CSIT_DIR}"/fdio.infra.terraform || die "Pushd failed!"
152 pushd "${TERRAFORM_MODULE_DIR}" || die "Pushd failed!"
154 terraform validate || die "Terraform validate failed!"
155 popd || die "Popd failed!"
156 popd || die "Popd failed!"
160 function installed () {
162 # Check if the given utility is installed. Fail if not installed.
165 # - ${1} - Utility to check.
166 # Returns (implicitly):
167 # - 0 - If command is installed.
168 # - 1 - If command is not installed.