feat(jobspec): Unify soak jobspecs
[csit.git] / resources / libraries / bash / function / terraform.sh
1 #!/usr/bin/env bash
2
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:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15
16 set -exuo pipefail
17
18
19 function terraform_apply () {
20
21     # Run terraform apply command to prepare topology.
22     #
23     # Variable read:
24     # - ${CSIT_DIR} - CSIT main directory, where terraform modules are located.
25     # - ${TERRAFORM_MODULE_DIR} - Terraform module directory.
26
27     set -exuo pipefail
28
29     if ! installed terraform; then
30         die "Please install terraform!"
31     fi
32
33     pushd "${CSIT_DIR}"/fdio.infra.terraform || die "Pushd failed!"
34     pushd "${TERRAFORM_MODULE_DIR}" || die "Pushd failed!"
35     export TF_LOG=INFO
36     terraform apply -no-color -auto-approve  || die "Terraform apply failed!"
37     popd || die "Popd failed!"
38     popd || die "Popd failed!"
39 }
40
41
42 function terraform_destroy () {
43
44     # Run terraform destroy command to prepare module.
45     #
46     # Variable read:
47     # - ${CSIT_DIR} - CSIT main directory, where terraform modules are located.
48     # - ${TERRAFORM_MODULE_DIR} - Terraform module directory.
49
50     set -exuo pipefail
51
52     if ! installed terraform; then
53         die "Please install terraform!"
54     fi
55
56     pushd "${CSIT_DIR}"/fdio.infra.terraform || die "Pushd failed!"
57     pushd "${TERRAFORM_MODULE_DIR}" || die "Pushd failed!"
58     export TF_LOG=INFO
59     terraform destroy -auto-approve -no-color || die "Terraform destroy failed!"
60     popd || die "Popd failed!"
61     popd || die "Popd failed!"
62 }
63
64
65 function terraform_init () {
66
67     # Run terraform init command to prepare module.
68     #
69     # Variable read:
70     # - ${CSIT_DIR} - CSIT main directory, where terraform modules are located.
71     # - ${TERRAFORM_MODULE_DIR} - Terraform module directory.
72
73     set -exuo pipefail
74
75     if ! installed terraform; then
76         die "Please install terraform!"
77     fi
78
79     pushd "${CSIT_DIR}"/fdio.infra.terraform || die "Pushd failed!"
80     pushd "${TERRAFORM_MODULE_DIR}" || die "Pushd failed!"
81
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"
87
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!"
91
92     rm -f terraform.tfstate || die "Failed to clear terraform state!"
93     export TF_LOG=INFO
94     terraform init || die "Failed to run terraform init!"
95     popd || die "Popd failed!"
96     popd || die "Popd failed!"
97 }
98
99
100 function terraform_install () {
101
102     # Install terraform.
103
104     OS_ARCH=$(uname -m) || die "Failed to get arch."
105     case "${OS_ARCH}" in
106         x86_64) architecture="amd64" ;;
107         aarch64) architecture="arm64" ;;
108     esac
109
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!"
116     }
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!"
120     }
121 }
122
123
124 function terraform_output () {
125
126     # Run terraform output command to prepare module.
127     #
128     # Variable read:
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.
132
133     set -exuo pipefail
134
135     if ! installed terraform; then
136         die "Please install terraform!"
137     fi
138
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!"
144 }
145
146
147 function terraform_validate () {
148
149     # Run terraform validate command to prepare module.
150     #
151     # Variable read:
152     # - ${CSIT_DIR} - CSIT main directory, where terraform modules are located.
153     # - ${TERRAFORM_MODULE_DIR} - Terraform module directory.
154
155     set -exuo pipefail
156
157     if ! installed terraform; then
158         die "Please install terraform!"
159     fi
160
161     pushd "${CSIT_DIR}"/fdio.infra.terraform || die "Pushd failed!"
162     pushd "${TERRAFORM_MODULE_DIR}" || die "Pushd failed!"
163     export TF_LOG=INFO
164     terraform validate || die "Terraform validate failed!"
165     popd || die "Popd failed!"
166     popd || die "Popd failed!"
167 }
168
169
170 function installed () {
171
172     # Check if the given utility is installed. Fail if not installed.
173     #
174     # Arguments:
175     # - ${1} - Utility to check.
176     # Returns (implicitly):
177     # - 0 - If command is installed.
178     # - 1 - If command is not installed.
179
180     set -exuo pipefail
181
182     command -v "${1}"
183 }