forked from kata-containers/kata-containers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_versions_txt.sh
executable file
·188 lines (152 loc) · 5.02 KB
/
gen_versions_txt.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env bash
# Copyright (c) 2018 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0
#
[ -z "${DEBUG}" ] || set -x
set -e
set -o errexit
set -o nounset
set -o pipefail
readonly script_name="$(basename "${BASH_SOURCE[0]}")"
readonly script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly versions_txt="versions.txt"
project="kata-containers"
source "${script_dir}/../scripts/lib.sh"
ARCH=${ARCH:-$(arch_to_golang "$(uname -m)")}
get_kata_version() {
cat "${script_dir}/../../../VERSION"
}
gen_version_file() {
local branch="$1"
local kata_version="$2"
local ref="refs/heads/${branch}"
if [ "${kata_version}" == "HEAD" ]; then
kata_version="${branch}"
ref="refs/heads/${branch}"
else
ref="refs/tags/${kata_version}^{}"
fi
qemu_vanilla_branch=$(get_from_kata_deps "assets.hypervisor.qemu.version")
# Check if qemu.version can be used to get the version and hash, otherwise use qemu.tag
qemu_vanilla_ref="refs/heads/${qemu_vanilla_branch}"
if ! (git ls-remote --heads "https://github.com/qemu/qemu.git" | grep -q "refs/heads/${qemu_vanilla_branch}"); then
qemu_vanilla_branch=$(get_from_kata_deps "assets.hypervisor.qemu.tag")
qemu_vanilla_ref="refs/tags/${qemu_vanilla_branch}^{}"
fi
qemu_vanilla_version=$(curl -s -L "https://raw.githubusercontent.com/qemu/qemu/${qemu_vanilla_branch}/VERSION")
qemu_vanilla_hash=$(git ls-remote https://github.com/qemu/qemu.git | grep "${qemu_vanilla_ref}" | awk '{print $1}')
kernel_version=$(get_from_kata_deps "assets.kernel.version")
#Remove extra 'v'
kernel_version=${kernel_version#v}
golang_version=$(get_from_kata_deps "languages.golang.meta.newest-version")
# - is not a valid char for rpmbuild
# see https://github.com/semver/semver/issues/145
kata_version=$(get_kata_version)
kata_version=${kata_version/-/\~}
cat > "$versions_txt" <<EOF
# This is a generated file from ${script_name}
kata_version=${kata_version}
# Dependencies
kata_osbuilder_version=${kata_version}
qemu_vanilla_version=${qemu_vanilla_version}
qemu_vanilla_hash=${qemu_vanilla_hash}
kernel_version=${kernel_version}
# Golang
go_version=${golang_version}
EOF
}
die() {
local msg="${1:-}"
local print_usage=$"${2:-}"
if [ -n "${msg}" ]; then
echo -e "ERROR: ${msg}\n"
fi
[ -n "${print_usage}" ] && usage 1
}
usage() {
exit_code=$"${1:-0}"
cat <<EOF
Usage:
${script_name} [--compare | -h | --help] <kata-branch>
Generate a ${versions_txt} file, containing version numbers and commit hashes
of all the kata components under the git branch <kata-branch>.
Options:
-h, --help Print this help.
--compare Only compare the kata version at branch <kata-branch> with the
one in ${versions_txt} and leave the file untouched.
--head Use <kata-branch>'s head to generate the versions file.
EOF
exit "${exit_code}"
}
main() {
local compareOnly=
local use_head=
local use_tag=
case "${1:-}" in
"-h"|"--help")
usage
;;
--compare)
compareOnly=1
shift
;;
--head)
use_head=1
shift
;;
--tag)
use_tag=1
shift
;;
-*)
die "Invalid option: ${1:-}" "1"
shift
;;
esac
local kata_version=
if [ -n "$use_tag" ]; then
if [ -n "${use_head}" ]; then
die "tag and head options are mutually exclusive"
fi
# We are generating versions based on the provided tag
local tag="${1:-}"
[ -n "${tag}" ] || die "No tag specified" "1"
# use the runtime's repository to determine branch information
local repo="github.com/kata-containers/kata-containers"
local repo_dir="kata-containers"
git clone --quiet "https://${repo}.git" "${repo_dir}"
pushd "${repo_dir}" >> /dev/null
local branch=$(git branch -r -q --contains "${tag}" | grep -E "master|stable|main" | grep -v HEAD)
popd >> /dev/null
rm -rf ${repo_dir}
[ -n "${branch}" ] || die "branch for tag ${tag} not found"
# in the event this is on master as well as stable, or multiple stables, just pick the first branch
# (ie, 1.8.0-alpha0 may live on stable-1.8 as well as master: we'd just use master in this case)
branch=$(echo ${branch} | awk -F" " '{print $1}')
# format will be origin/<branch-name> - let's drop origin:
branch=$(echo ${branch} | awk -F"/" '{print $2}')
echo "generating versions for tag ${tag} which is on branch ${branch}"
kata_version=${tag}
else
local branch="${1:-}"
[ -n "${branch}" ] || die "No branch specified" "1"
if [ -n "${use_head}" ]; then
kata_version="HEAD"
else
kata_version=$(get_kata_version)
fi
fi
if [ -n "$compareOnly" ]; then
source "./${versions_txt}" || exit 1
kata_version=${kata_version/\~/-}
[ -n "${kata_version}" ] || die "${version_file} does not contain a valid kata_version variable"
# Replacing ~ with -, as - is not a valid char for rpmbuild
# see https://github.com/semver/semver/issues/145
[ "$(get_kata_version)" = "${kata_version/\~/-}" ] && compare_result="matches" || compare_result="is different from"
echo "${kata_version} in ${versions_txt} ${compare_result} the version at branch ${branch}"
return
fi
gen_version_file "${branch}" "${kata_version}"
}
main $@