e47f38937c97cf24aafa4e61637d9bb4dcbaa12d
[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     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!"
111 }
112
113
114 function terraform_output () {
115
116     # Run terraform output command to prepare module.
117     #
118     # Variable read:
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.
122
123     set -exuo pipefail
124
125     if ! installed terraform; then
126         die "Please install terraform!"
127     fi
128
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!"
134 }
135
136
137 function terraform_validate () {
138
139     # Run terraform validate command to prepare module.
140     #
141     # Variable read:
142     # - ${CSIT_DIR} - CSIT main directory, where terraform modules are located.
143     # - ${TERRAFORM_MODULE_DIR} - Terraform module directory.
144
145     set -exuo pipefail
146
147     if ! installed terraform; then
148         die "Please install terraform!"
149     fi
150
151     pushd "${CSIT_DIR}"/fdio.infra.terraform || die "Pushd failed!"
152     pushd "${TERRAFORM_MODULE_DIR}" || die "Pushd failed!"
153     export TF_LOG=INFO
154     terraform validate || die "Terraform validate failed!"
155     popd || die "Popd failed!"
156     popd || die "Popd failed!"
157 }
158
159
160 function installed () {
161
162     # Check if the given utility is installed. Fail if not installed.
163     #
164     # Arguments:
165     # - ${1} - Utility to check.
166     # Returns (implicitly):
167     # - 0 - If command is installed.
168     # - 1 - If command is not installed.
169
170     set -exuo pipefail
171
172     command -v "${1}"
173 }