vpp-hst: add VPP HostStack Test framework jobs
[ci-management.git] / docker / scripts / build_executor_docker_image.sh
1 #! /bin/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 -euxo pipefail
17
18 # Log all output to stdout & stderr to a log file
19 export DOCKER_DATE=${DOCKER_DATE:-"$(date -u +%Y_%m_%d_%H%M%S_UTC)"}
20 logname="/tmp/$(basename $0).${DOCKER_DATE}.log"
21 echo -e "\n*** Logging output to $logname ***\n\n"
22 exec > >(tee -a $logname) 2>&1
23
24 export CIMAN_DOCKER_SCRIPTS=${CIMAN_DOCKER_SCRIPTS:-"$(dirname $BASH_SOURCE)"}
25 . "$CIMAN_DOCKER_SCRIPTS/lib_vpp.sh"
26 . "$CIMAN_DOCKER_SCRIPTS/lib_csit.sh"
27 . "$CIMAN_DOCKER_SCRIPTS/lib_apt.sh"
28
29 all_os_names=""
30 ci_tag=""
31 ci_image=""
32 os_names=""
33 push_to_docker_hub=""
34 dump_dockerfile=""
35
36 usage() {
37     set +x
38     echo
39     echo "Usage: $0 [-c <class>] [-p] [-r <role>] -a | <os name> [... <os name>]"
40     echo "  -a            Run all OS's supported on class $EXECUTOR_CLASS & arch $OS_ARCH"
41     echo "  -c <class>    Default is '$EXECUTOR_DEFAULT_CLASS'"
42     executor_list_classes
43     echo "  -d            Generate Dockerfile, dump it to stdout, and exit"
44     echo "  -p            Push docker images to Docker Hub"
45     echo "  -r <role>     Add a role based tag (e.g. sandbox-x86_64):"
46     executor_list_roles
47     executor_list_os_names
48     exit 1
49 }
50
51 must_be_run_as_root_or_docker_group
52 while getopts ":ac:dhpr:" opt; do
53     case "$opt" in
54         a)  all_os_names="1" ;;
55         c) if executor_verify_class "$OPTARG" ; then
56                EXECUTOR_CLASS="$OPTARG"
57                EXECUTOR_CLASS_ARCH="$EXECUTOR_CLASS-$OS_ARCH"
58            else
59                echo "ERROR: Invalid executor class '$OPTARG'!"
60                usage
61            fi ;;
62         d) dump_dockerfile="1"; set +x ;;
63         h) usage ;;
64         p) push_to_docker_hub="1" ;;
65         r) if executor_verify_role "$OPTARG" ; then
66                ci_tag="${OPTARG}-$OS_ARCH"
67            else
68                echo "ERROR: Invalid executor role: '$OPTARG'!"
69                usage
70            fi ;;
71         \?)
72             echo "ERROR: Invalid option: -$OPTARG" >&2
73             usage ;;
74         :)
75             echo "ERROR: Option -$OPTARG requires an argument." >&2
76             usage ;;
77     esac
78 done
79 shift $(( $OPTIND-1 ))
80
81 if [ -n "$all_os_names" ] ; then
82     os_names="${EXECUTOR_CLASS_ARCH_OS_NAMES[$EXECUTOR_CLASS_ARCH]}"
83 else
84     os_names="$@"
85 fi
86
87 # Validate arguments
88 if [ -z "$os_names" ] ; then
89     echo "ERROR: Missing executor OS name(s) for class '$EXECUTOR_CLASS'!"
90     usage
91 fi
92 for executor_os_name in $os_names ; do
93     if ! executor_verify_os_name "$executor_os_name" ; then
94         set_opts="$-"
95         set +x # disable trace output
96         echo "ERROR: Invalid executor OS name for class '$EXECUTOR_CLASS': $executor_os_name!"
97         executor_list_os_names
98         echo
99         exit 1
100     fi
101 done
102
103 # Build the specified docker images
104 docker_build_setup_ciman
105 docker_build_setup_vpp
106 docker_build_setup_csit
107 for executor_os_name in $os_names ; do
108     docker_from_image="${executor_os_name/-/:}"
109     # Remove '-' and '.' from executor_os_name in Docker Hub repo name
110     os_name="${executor_os_name//-}"
111     repository="fdiotools/${EXECUTOR_CLASS}-${os_name//.}"
112     executor_docker_image="$repository:$DOCKER_TAG"
113
114     case "$executor_os_name" in
115         ubuntu*)
116             generate_apt_dockerfile "$EXECUTOR_CLASS" "$executor_os_name" \
117                                     "$docker_from_image" "$executor_docker_image" ;;
118         debian*)
119             generate_apt_dockerfile "$EXECUTOR_CLASS" "$executor_os_name" \
120                                     "$docker_from_image" "$executor_docker_image" ;;
121         *)
122             echo "ERROR: Don't know how to generate dockerfile for OS $executor_os_name!"
123             usage ;;
124     esac
125
126     if [ -n "$dump_dockerfile" ] ; then
127         line="==========================================================================="
128         echo -e "\nDockerfile for '$EXECUTOR_CLASS' executor docker image on OS '$executor_os_name':\n$line"
129         cat "$DOCKERFILE"
130         echo -e "$line\n"
131     else
132         docker build -t "$executor_docker_image" "$DOCKER_BUILD_DIR"
133         rm -f "$DOCKERFILE"
134         if [ -n "$ci_tag" ] ; then
135             ci_image="$repository:$ci_tag"
136             echo -e "\nAdding docker tag $ci_image to $executor_docker_image"
137             docker tag "$executor_docker_image" "$ci_image"
138         fi
139         if [ -n "$push_to_docker_hub" ] ; then
140             echo -e "\nPushing $executor_docker_image to Docker Hub..."
141             docker login
142             docker push "$executor_docker_image"
143             if [ -n "$ci_image" ] ; then
144                 echo -e "\nPushing $ci_image to Docker Hub..."
145                 docker push "$ci_image"
146             fi
147         fi
148     fi
149 done
150
151 echo -e "\n$(basename $BASH_SOURCE) COMPLETE\nHave a great day! :D"