Add gitlab issue/merge request templates
[AGL/meta-agl.git] / scripts / distro-manifest-generator.sh
1 #!/usr/bin/env bash
2
3 ################################################################################
4 #
5 # The MIT License (MIT)
6 #
7 # Copyright (c) 2018 Stéphane Desneux <sdx@iot.bzh>
8 #
9 # Permission is hereby granted, free of charge, to any person obtaining a copy
10 # of this software and associated documentation files (the "Software"), to deal
11 # in the Software without restriction, including without limitation the rights
12 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 # copies of the Software, and to permit persons to whom the Software is
14 # furnished to do so, subject to the following conditions:
15 #
16 # The above copyright notice and this permission notice shall be included in
17 # all copies or substantial portions of the Software.
18 #
19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 # SOFTWARE.
26 #
27 ################################################################################
28
29 mode=deploy
30 manifest=
31 verbose=0
32 format=bash
33 sourcefile=
34 timestamp="$(date -u +%Y%m%d_%H%M%S_%Z)"
35
36 function info() { echo "$@" >&2; }
37 function error() { echo "$BASH_SOURCE: $@" >&2; }
38 function out() { echo -n "$@"; }
39 function out_object() {
40         # expected stdin stream is:
41         # --------------
42         # key
43         # value
44         # key
45         # value
46         # ...
47         # --------------
48         local sep=""
49         local k
50         case $format in
51                 bash)
52                         while read x; do
53                                 [[ -z "$k" ]] && { k="$x"; continue; }
54                                 out "$sep${k}="
55                                 out_value "$x"
56                                 sep=$'\n'
57                                 k=
58                         done
59                         out "$sep"
60                         ;;
61                 json)
62                         out "{"
63                         while read x; do
64                                 [[ -z "$k" ]] && { k="$x"; continue; }
65                                 out "$sep\"${k}\":"
66                                 out_value "$x"
67                                 sep=","
68                                 k=
69                         done
70                         out "}"
71                         ;;
72         esac
73 }
74
75 function out_array() {
76         # expected stdin stream is:
77         # --------------
78         # value
79         # value
80         # ...
81         # --------------
82         local sep=""
83         case $format in
84                 bash)
85                         while read x; do
86                                 out "$sep"
87                                 out_value "$x"
88                                 sep=" "
89                         done
90                         ;;
91                 json)
92                         out "["
93                         while read x; do
94                                 out $sep
95                                 out_value "$x"
96                                 sep=","
97                         done
98                         out "]"
99                         ;;
100         esac
101 }
102
103 function out_value() {
104         # string
105         # number
106         # object
107         # array
108         # 'true'
109         # 'false'
110         # 'null'
111
112         x=$1
113
114         # litterals
115         if [[ "$x" =~ ^(true|false|null)$ ]]; then
116                 out "$x"
117         # number
118         elif [[ "$x" =~ ^[+-]?[0-9]+(\.[0-9]+)?$ ]]; then
119                 out "$x"
120         # object
121         elif [[ "$x" =~ ^\{.*\}$ ]]; then
122                 out "$x"
123         # array
124         elif [[ "$x" =~ ^\[.*\]$ ]]; then
125                 out "$x"
126         # string
127         else
128                 out "\"$(sed 's/\("\)/\\\1/g' <<<$x)\""
129         fi
130 }
131
132 function out_comment() {
133         case $format in
134                 bash)
135                         [[ "$verbose" == 1 ]] && echo "# $@" || true
136                         ;;
137                 json)
138                         ;;
139         esac
140 }
141
142 function _getgitmanifest() {
143         # this function takes the setup.manifest generated by setup script and uses DIST_METADIR
144         # to analyze git repos
145
146         local manifest=$1 mode=$2
147         [[ -f $manifest ]] && source $manifest || { error "Invalid setup manifest '$manifest'"; return 1; }
148         [[ ! -d "$DIST_METADIR" ]] && {
149                 error "Invalid meta directory. Check variable DIST_METADIR in manifest file '$manifest'."
150                 error "$BASH_SOURCE: Also, check directory '$DIST_METADIR'."
151                 return 2
152         }
153         local GIT=$(which git) REALPATH=$(which realpath)
154         [[ ! -x $GIT ]] && { error "$BASH_SOURCE: Unable to find git command in $PATH."; return 3; }
155         [[ ! -x $REALPATH ]] && { error "$BASH_SOURCE: Unable to find realpath command in $PATH."; return 4; }
156
157         local gitrepo gitrev metagitdir sep=""
158         DIST_LAYERS=""
159         for metagitdir in $(find $DIST_METADIR -maxdepth 2 -type d \( -not -path '*/.*' \) -exec test -d "{}/.git" \; -print -prune); do
160                 gitrepo=$($REALPATH -Ls $metagitdir --relative-to=$DIST_METADIR)
161                 pushd $DIST_METADIR/$gitrepo &>/dev/null && {
162                         gitrev=$( { $GIT describe --long --dirty --always 2>/dev/null || echo "unknown_revision"; } | tr ' \t' '__' )
163                         popd &>/dev/null
164                 } || {
165                         gitrev=unknown
166                 }
167                 DIST_LAYERS="${DIST_LAYERS}${sep}${gitrepo}:${gitrev}"
168                 sep=" "
169         done
170
171         # layers checksum
172         DIST_LAYERS_MD5=$(echo $DIST_LAYERS|md5sum -|awk '{print $1;}')
173
174         # in json, transform layers in an object, features in array
175         [[ "$format" == "json" ]] && {
176                 DIST_FEATURES=$(for x in $DIST_FEATURES; do
177                         echo $x
178                 done | out_array)
179                 DIST_LAYERS=$(for x in $DIST_LAYERS; do
180                         echo ${x%%:*}
181                         echo ${x#*:}
182                 done | out_object)
183         }
184
185
186         # compute build hash
187         DIST_BUILD_HASH="F${DIST_FEATURES_MD5:0:8}-L${DIST_LAYERS_MD5:0:8}"
188         DIST_BUILD_ID="${DIST_DISTRO_NAME}-${DIST_MACHINE}-F${DIST_FEATURES_MD5:0:8}-L${DIST_LAYERS_MD5:0:8}"
189
190
191         # compute setup manifest path and build TS
192         DIST_SETUP_MANIFEST="$($REALPATH $manifest)"
193
194         # Manifest build timestamp
195         DIST_BUILD_TS="$timestamp"
196
197         # build topic from setup topic
198         DIST_BUILD_TOPIC="${DIST_SETUP_TOPIC}"
199
200         # what to retain from setup manifest?
201         # to generate the full list: cat setup.manifest  | grep = | cut -f1 -d"=" | awk '{printf("%s ",$1);}'
202         declare -A SETUP_VARS
203         SETUP_VARS[deploy]="DIST_MACHINE DIST_FEATURES DIST_FEATURES_MD5 DIST_BUILD_HOST DIST_BUILD_OS DIST_SETUP_TS"
204         SETUP_VARS[target]="DIST_MACHINE DIST_FEATURES"
205         SETUP_VARS[sdk]="DIST_MACHINE DIST_FEATURES"
206
207         # extra vars not coming from setup.manifest but generated here
208         declare -A EXTRA_VARS
209         EXTRA_VARS[deploy]="DIST_SETUP_MANIFEST DIST_BUILD_TS DIST_LAYERS DIST_LAYERS_MD5 DIST_BUILD_HASH DIST_BUILD_ID DIST_BUILD_TOPIC"
210         EXTRA_VARS[target]="DIST_LAYERS DIST_BUILD_HASH DIST_BUILD_ID DIST_BUILD_TS DIST_BUILD_TOPIC"
211         EXTRA_VARS[sdk]="DIST_LAYERS DIST_BUILD_HASH DIST_BUILD_ID DIST_BUILD_TS DIST_BUILD_TOPIC"
212
213         # BITBAKE_VARS may be defined from external file to source (--source arg)
214         # this is used to dump extra vars from inside bitbake recipe
215
216         { for x in ${SETUP_VARS[$mode]} ${EXTRA_VARS[$mode]} ${BITBAKE_VARS[$mode]}; do
217                 k=$x
218                 [[ "$format" == "json" ]] && {
219                         k=${k#DIST_} # remove prefix
220                         k=${k,,*} # to lower case
221                 }
222                 echo $k
223                 echo ${!x}
224         done } | out_object
225 }
226
227 function getmanifest() {
228         local rc=0
229         out_comment "DISTRO BUILD MANIFEST"
230         out_comment
231
232         # add layers manifest
233         out_comment "----- this fragment has been generated by $BASH_SOURCE"
234         _getgitmanifest $1 $2 || rc=$?
235         out_comment "------------ end of $BASH_SOURCE fragment --------"
236
237         return $rc
238 }
239
240 function __usage() {
241         cat <<EOF >&2
242 Usage: $BASH_SOURCE [-v|--verbose] [-f|--format <fmt>] [-t|--timestamp <value>] [-m|--mode <mode>] [-s|--source <file>] <setup_manifest_file>
243    Options:
244       -v|--verbose: generate comments in the output file
245       -s|--source: extra file to source (get extra variables generated from bitbake recipe)
246       -t|--timestamp: set build timestamp (default: current date - may not be the same ts as bitbake)
247       -f|--format: specify output format: 'bash' or 'json'
248       -m|--mode: specify the destination for the generated manifest
249          'deploy' : for the tmp/deploy/images/* directories
250          'target' : for the manifest to be installed inside a target image
251          'sdk'    : for the manifest to be installed inside the SDK
252
253    <setup_manifest_file> is the input manifest generated from setup script
254 EOF
255 }
256
257 set -e
258
259 tmp=$(getopt -o h,v,m:,f:,t:,s: --long help,verbose,mode:,format:,timestamp:,source: -n "$BASH_SOURCE" -- "$@") || {
260         error "Invalid arguments."
261         __usage
262         exit 1
263 }
264 eval set -- $tmp
265
266 while true; do
267         case "$1" in
268                 -h|--help) __usage; exit 0;;
269                 -v|--verbose) verbose=1; shift ;;
270                 -f|--format) format=$2; shift 2;;
271                 -m|--mode) mode=$2; shift 2;;
272                 -t|--timestamp) timestamp=$2; shift 2;;
273                 -s|--source) sourcefile=$2; shift 2;;
274                 --) shift; break;;
275                 *) fatal "Internal error";;
276         esac
277 done
278
279 manifest=$1
280 shift
281 [[ ! -f "$manifest" ]] && { __usage; exit 1; }
282
283 case $mode in
284         deploy|target|sdk) ;;
285         *) error "Invalid mode specified. Allowed modes are: 'deploy', 'target', 'sdk'"; __usage; exit 42;;
286 esac
287
288 case $format in
289         bash|json) ;;
290         *) error "Invalid format specified. Allowed formats are 'json' or 'bash'"; __usage; exit 43;;
291 esac
292
293 info "Generating manifest: mode=$mode format=$format manifest=$manifest"
294 [[ -f "$sourcefile" ]] && {
295         info "Sourcing file $sourcefile"
296         . $sourcefile
297         # this may define extra vars: to be taken into account BITBAKE_VARS must be defined
298 }
299
300 [[ "$format" == "json" ]] && {
301         # if jq is present, use it to format json output
302         jq=$(which jq || true)
303         [[ -n "$jq" ]] && {
304                 getmanifest $manifest $mode | $jq ""
305                 exit ${PIPESTATUS[0]}
306         }
307 }
308
309 getmanifest $manifest $mode
310