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