feat(tests): IPv6 fixes
[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     terraform apply -auto-approve  || die "Failed to run terraform apply!"
38     popd || die "Popd failed!"
39     popd || die "Popd failed!"
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     # - ${NODENESS} - Node multiplicity of desired testbed.
49     # - ${FLAVOR} - Node flavor string, see common.sh
50
51     set -exuo pipefail
52
53     if ! installed terraform; then
54         die "Please install terraform!"
55     fi
56
57     pushd "${CSIT_DIR}"/fdio.infra.terraform || die "Pushd failed!"
58     pushd "${NODENESS}_${FLAVOR}_c5n" || die "Pushd failed!"
59     export TF_LOG=INFO
60     terraform destroy -auto-approve || die "Failed to run terraform destroy!"
61     popd || die "Popd failed!"
62     popd || die "Popd failed!"
63 }
64
65
66 function terraform_init () {
67
68     # Run terraform init command to prepare module.
69     #
70     # Variable read:
71     # - ${CSIT_DIR} - CSIT main directory, where terraform modules are located.
72     # - ${NODENESS} - Node multiplicity of desired testbed.
73     # - ${FLAVOR} - Node flavor string, see common.sh
74
75     set -exuo pipefail
76
77     if ! installed terraform; then
78         die "Please install terraform!"
79     fi
80
81     pushd "${CSIT_DIR}"/fdio.infra.terraform || die "Pushd failed!"
82     pushd "${NODENESS}_${FLAVOR}_c5n" || die "Pushd failed!"
83
84     plugin_url="https://github.com/radekg/terraform-provisioner-ansible/"
85     plugin_url+="releases/download/v2.5.0/"
86     plugin_url+="terraform-provisioner-ansible-linux-amd64_v2.5.0"
87     plugin_dir="${HOME}/.terraform.d/plugins/"
88     plugin_path+="${plugin_dir}terraform-provisioner-ansible_v2.5.0"
89
90     mkdir -p "${plugin_dir}" || die "Failed to create dir!"
91     wget -O "${plugin_path}" "${plugin_url}" || die "Failed to download plugin!"
92     chmod +x "${plugin_path}" || die "Failed to add execute rights!"
93
94     export TF_LOG=INFO
95     terraform init || die "Failed to run terraform init!"
96
97     popd || die "Popd failed!"
98     popd || die "Popd failed!"
99 }
100
101
102 function installed () {
103
104     # Check if the given utility is installed. Fail if not installed.
105     #
106     # Arguments:
107     # - ${1} - Utility to check.
108     # Returns (implicitly):
109     # - 0 - If command is installed.
110     # - 1 - If command is not installed.
111
112     set -exuo pipefail
113
114     command -v "${1}"
115 }