ed3bda32aaded768287909f6c5f70d1779467f98
[csit.git] / resources / libraries / bash / function / terraform.sh
1 #!/usr/bin/env bash
2
3 # Copyright (c) 2021 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     # - ${NODENESS} - Node multiplicity of desired testbed.
26     # - ${FLAVOR} - Node flavor string, see common.sh
27
28     set -exuo pipefail
29
30     if ! installed terraform; then
31         die "Please install terraform!"
32     fi
33
34     pushd "${CSIT_DIR}"/fdio.infra.terraform || die "Pushd failed!"
35     pushd "${NODENESS}_${FLAVOR}_c5n" || die "Pushd failed!"
36     export TF_LOG=INFO
37     trap 'terraform_destroy' ERR || {
38          die "Trap attempt failed, please cleanup manually. Aborting!"
39     }
40     terraform apply -no-color -auto-approve  || die "Terraform apply failed!"
41     popd || die "Popd failed!"
42     popd || die "Popd failed!"
43 }
44
45 function terraform_destroy () {
46
47     # Run terraform destroy command to prepare module.
48     #
49     # Variable read:
50     # - ${CSIT_DIR} - CSIT main directory, where terraform modules are located.
51     # - ${NODENESS} - Node multiplicity of desired testbed.
52     # - ${FLAVOR} - Node flavor string, see common.sh
53
54     set -exuo pipefail
55
56     if ! installed terraform; then
57         die "Please install terraform!"
58     fi
59
60     pushd "${CSIT_DIR}"/fdio.infra.terraform || die "Pushd failed!"
61     pushd "${NODENESS}_${FLAVOR}_c5n" || die "Pushd failed!"
62     export TF_LOG=INFO
63     terraform destroy -auto-approve -no-color || die "Terraform destroy failed!"
64     popd || die "Popd failed!"
65     popd || die "Popd failed!"
66 }
67
68
69 function terraform_init () {
70
71     # Run terraform init command to prepare module.
72     #
73     # Variable read:
74     # - ${CSIT_DIR} - CSIT main directory, where terraform modules are located.
75     # - ${NODENESS} - Node multiplicity of desired testbed.
76     # - ${FLAVOR} - Node flavor string, see common.sh
77
78     set -exuo pipefail
79
80     if ! installed terraform; then
81         curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
82         os="$(lsb_release -cs)" || die "Failed to get OS release!"
83         repo="deb [arch=amd64] https://apt.releases.hashicorp.com ${os} main"
84         sudo apt-add-repository "${repo}" || die "Failed to add repo!"
85         apt update -y
86         apt install -y terraform
87         #die "Please install terraform!"
88     fi
89
90     pushd "${CSIT_DIR}"/fdio.infra.terraform || die "Pushd failed!"
91     pushd "${NODENESS}_${FLAVOR}_c5n" || die "Pushd failed!"
92
93     plugin_url="https://github.com/radekg/terraform-provisioner-ansible/"
94     plugin_url+="releases/download/v2.5.0/"
95     plugin_url+="terraform-provisioner-ansible-linux-amd64_v2.5.0"
96     plugin_dir="${HOME}/.terraform.d/plugins/"
97     plugin_path+="${plugin_dir}terraform-provisioner-ansible_v2.5.0"
98
99     mkdir -p "${plugin_dir}" || die "Failed to create dir!"
100     wget -O "${plugin_path}" "${plugin_url}" || die "Failed to download plugin!"
101     chmod +x "${plugin_path}" || die "Failed to add execute rights!"
102
103     export TF_LOG=INFO
104     terraform init || die "Failed to run terraform init!"
105
106     popd || die "Popd failed!"
107     popd || die "Popd failed!"
108 }
109
110
111 function installed () {
112
113     # Check if the given utility is installed. Fail if not installed.
114     #
115     # Arguments:
116     # - ${1} - Utility to check.
117     # Returns (implicitly):
118     # - 0 - If command is installed.
119     # - 1 - If command is not installed.
120
121     set -exuo pipefail
122
123     command -v "${1}"
124 }