fix(etl): Typo"
[csit.git] / resources / libraries / bash / function / eb_version.sh
1 #!/usr/bin/env bash
2
3 # Copyright (c) 2024 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 die_on_error () {
20
21     # Source this fragment if you want to abort on any failure.
22     #
23     # Variables read:
24     # - ${CODE_EXIT_STATUS} - Exit status of report generation.
25     # Functions called:
26     # - die - Print to stderr and exit.
27
28     set -exuo pipefail
29
30     if [[ "${CODE_EXIT_STATUS}" != "0" ]]; then
31         die "Failed to generate docs!" "${CODE_EXIT_STATUS}"
32     fi
33 }
34
35
36 function eb_version_deploy () {
37
38     # Deploy Elastic Beanstalk CDash content.
39     #
40     # Variable read:
41     # - ${CSIT_DIR} - CSIT main directory.
42     # - ${TERRAFORM_OUTPUT_VAL} - Terraform output value.
43     # Variables set:
44     # - ${CODE_EXIT_STATUS} - Exit status of report generation.
45     # - ${TERRAFORM_OUTPUT_VAR} - Register Terraform output variable name.
46     # Functions called:
47     # - eb_version_verify - Build and verify EB version.
48     # - terraform_apply - Apply EB version by Terraform.
49     # - terraform_output - Get the application name string from Terraform.
50     # - die - Print to stderr and exit.
51
52     set -exuo pipefail
53
54     eb_version_build_verify || die "Failed to call Elastic Beanstalk verify!"
55     terraform_apply || die "Failed to call Terraform apply!"
56
57     TERRAFORM_OUTPUT_VAR="application_version"
58     terraform_output || die "Failed to call Terraform output!"
59
60     #aws --region eu-central-1 elasticbeanstalk update-environment \
61     #    --environment-name fdio-csit-dash-env \
62     #    --version-label "${TERRAFORM_OUTPUT_VAL}"
63 }
64
65
66 function eb_version_build_verify () {
67
68     # Build and verify Elastic Beanstalk CDash integrity.
69     #
70     # Variables set:
71     # - ${TERRAFORM_MODULE_DIR} - Terraform module sub-directory.
72     # Functions called:
73     # - terraform_init - Initialize Terraform modules.
74     # - terraform_validate - Validate Terraform code.
75     # - die - Print to stderr and exit.
76
77     set -exuo pipefail
78
79     eb_version_create_release || die "Failed to create release!"
80
81     TERRAFORM_MODULE_DIR="terraform-aws-fdio-csit-dash-app-base"
82
83     export TF_VAR_application_version="${BUILD_ID}"
84     terraform_init || die "Failed to call Terraform init!"
85     terraform_validate || die "Failed to call Terraform validate!"
86 }
87
88
89 function eb_version_create_release () {
90
91     # Create release of Elastic Beanstalk CDash.
92     #
93     # Variable read:
94     # - ${CSIT_DIR} - CSIT main directory.
95     # Functions called:
96     # - hugo_init_modules - Initialize Hugo modules.
97     # - hugo_build_site - Build static site with Hugo.
98     # - die - Print to stderr and exit.
99
100     set -exuo pipefail
101
102     if ! installed zip; then
103         die "Please install zip!"
104     fi
105
106     hugo_init_modules || die "Failed to call Hugo initialize!"
107     hugo_build_site || die "Failed to call Hugo build!"
108
109     pushd "${CSIT_DIR}"/csit.infra.dash || die "Pushd failed!"
110     pushd app || die "Pushd failed!"
111     find . -type d -name "__pycache__" -exec rm -rf "{}" \;
112     find . -type d -name ".webassets-cache" -exec rm -rf "{}" \;
113     zip -r ../app.zip . || die "Compress failed!"
114     popd || die "Popd failed!"
115     popd || die "Popd failed!"
116 }
117
118
119 function installed () {
120
121     # Check if the given utility is installed. Fail if not installed.
122     #
123     # Arguments:
124     # - ${1} - Utility to check.
125     # Returns (implicitly):
126     # - 0 - If command is installed.
127     # - 1 - If command is not installed.
128
129     set -exuo pipefail
130
131     command -v "${1}"
132 }