fix(bootstrap): EB deploy
[csit.git] / resources / libraries / bash / function / eb_version.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 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     # Variable read:
71     # - ${CSIT_DIR} - CSIT main directory.
72     # Variables set:
73     # - ${TERRAFORM_MODULE_DIR} - Terraform module sub-directory.
74     # Functions called:
75     # - hugo_init_modules - Initialize Hugo modules.
76     # - hugo_build_site - Build static site with Hugo.
77     # - terraform_init - Initialize Terraform modules.
78     # - terraform_validate - Validate Terraform code.
79     # - die - Print to stderr and exit.
80
81     set -exuo pipefail
82
83     if ! installed zip; then
84         die "Please install zip!"
85     fi
86
87     hugo_init_modules || die "Failed to call Hugo initialize!"
88     hugo_build_site || die "Failed to call Hugo build!"
89
90     pushd "${CSIT_DIR}"/csit.infra.dash || die "Pushd failed!"
91     pushd app || die "Pushd failed!"
92     find . -type d -name "__pycache__" -exec rm -rf "{}" \;
93     find . -type d -name ".webassets-cache" -exec rm -rf "{}" \;
94     zip -r ../app.zip . || die "Compress failed!"
95     popd || die "Popd failed!"
96     popd || die "Popd failed!"
97
98     TERRAFORM_MODULE_DIR="terraform-aws-fdio-csit-dash-app-base"
99
100     export TF_VAR_application_version="${BUILD_ID}"
101     terraform_init || die "Failed to call Terraform init!"
102     terraform_validate || die "Failed to call Terraform validate!"
103 }
104
105
106 function generate_report () {
107
108     # Generate report content.
109     #
110     # Variable read:
111     # - ${TOOLS_DIR} - Path to existing resources subdirectory "tools".
112     # - ${GERRIT_BRANCH} - Gerrit branch used for release tagging.
113     # Variables set:
114     # - ${CODE_EXIT_STATUS} - Exit status of report generation.
115     # Functions called:
116     # - die - Print to stderr and exit.
117
118     set -exuo pipefail
119
120     pushd "${TOOLS_DIR}"/presentation || die "Pushd failed!"
121
122     # Set default values in config array.
123     typeset -A CFG
124     typeset -A DIR
125
126     DIR[WORKING]="_tmp"
127
128     # Create working directories.
129     mkdir "${DIR[WORKING]}" || die "Mkdir failed!"
130
131     export PYTHONPATH=`pwd`:`pwd`/../../../ || die "Export failed!"
132
133     all_options=("pal.py")
134     all_options+=("--specification" "specifications/report")
135     all_options+=("--release" "${GERRIT_BRANCH:-master}")
136     all_options+=("--week" $(date "+%V"))
137     all_options+=("--logging" "INFO")
138     all_options+=("--force")
139
140     set +e
141     python "${all_options[@]}"
142     CODE_EXIT_STATUS="$?"
143     set -e
144 }
145
146 function installed () {
147
148     # Check if the given utility is installed. Fail if not installed.
149     #
150     # Arguments:
151     # - ${1} - Utility to check.
152     # Returns (implicitly):
153     # - 0 - If command is installed.
154     # - 1 - If command is not installed.
155
156     set -exuo pipefail
157
158     command -v "${1}"
159 }