This repository has been archived by the owner on May 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathinstall_asset.sh
executable file
·197 lines (162 loc) · 4.98 KB
/
install_asset.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
189
190
191
192
193
194
195
196
197
#!/bin/bash
#
# Copyright (c) 2018 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -e
repo_owner="clearcontainers"
latest=false
readonly runtime_versions_url="https://raw.githubusercontent.com/${repo_owner}/runtime/master/versions.txt"
readonly versions_txt="$GOPATH/src/github.com/${repo_owner}/runtime/versions.txt"
readonly tmp_dir=$(mktemp -t -d install-assets.XXXX)
#fake repository dir to query kernel and image version from remote
fake_repo_dir="${tmp_dir}/fake_repo"
cleanup() {
rm -rf "${tmp_dir}"
}
trap cleanup EXIT
function usage() {
cat << EOT
Usage: $0 <kernel|image> <version>
Install clear containers asset (kernel or image) <version>.
image: When 'latest' version is used, if image is does not exist on github, it will build it
using version.txt file from ${repo_owner}/runtime.
It will check for versions file in the next order:
- ${versions_txt}
- ${runtime_versions_url}
kernel: If 'latest' version is used, this script will download the latest kernel published in
${https://github.com/${repo_owner}/linux/releases}
version: Use 'latest' to pull latest asset version
EOT
exit 1
}
asset="$1"
version="$2"
[ -z "${asset}" ] && usage
[ -z "${version}" ] && usage
die(){
msg="$*"
echo "ERROR: ${msg}" >&2
exit 1
}
resolve_version() {
local repo="$1"
local version="$2"
[ -n "${version}" ] || die "version not provided"
[ -n "${repo}" ] || die "repo not provided"
[ "${version}" == "latest" ] && version=$(get_latest_version "${repo}")
echo "$version"
}
#Get latest version by checking remote tags
#We dont ask to github api directly because force a user to provide a GITHUB token
function get_latest_version {
repo="${1}"
[ -n "${repo}" ] || die "repo not provided"
mkdir -p ${fake_repo_dir}
pushd ${fake_repo_dir} >> /dev/null
git init -q
git remote add origin "https://github.com/${repo_owner}/${repo}.git"
case "$asset" in
kernel)
cc_release=$(git ls-remote --tags 2>/dev/null \
| grep -oP '\-\d+\.container' \
| grep -oP '\d+' \
| sort -n | \
tail -1 )
# Kernel version is incremental we take the latest revision.
tag=$(git ls-remote --tags 2>/dev/null \
| grep -oP "v\d+\.\d+\.\d+\-${cc_release}.container" \
| tail -1)
;;
image)
# Image version is formed bt agent version and clear linux version.
# This variables are taked from versions.txt file
tag="cc-${clear_vm_image_version}-agent-${cc_agent_version:0:6}"
;;
*)
die "unknown asset $asset"
;;
esac
popd >> /dev/null
echo "${tag}"
}
function build_asset {
local asset="$1"
case "$asset" in
image)
packaging="github.com/${repo_owner}/packaging"
go get -d -u ${packaging} || true
"$GOPATH/src/${packaging}/release-tools/release_image_github.sh" -o "${PWD}" -a ${cc_agent_version} -c ${clear_vm_image_version} release
;;
*)
die "Dont know how to build $asset"
;;
esac
}
function download_asset {
local asset="$1"
local version="$2"
[ -n "${asset}" ] || die "asset is needed"
[ -n "${version}" ] || die "version is needed"
[ "${version}" == "latest" ] && latest="true"
if [ -f "${versions_txt}" ];then
echo "Using ${versions_txt}"
source ${versions_txt}
else
echo "Using ${runtime_versions_url}"
source <(curl -sL "${runtime_versions_url}")
fi
case "$asset" in
kernel)
repo_name=linux
#tarball version-binaries
binaries_dir_fmt='%s-binaries'
;;
image)
repo_name=osbuilder
#tarball name is image-version-binaries
binaries_dir_fmt='image-%s-binaries'
;;
*)
echo "unknown asset $asset"
usage
;;
esac
version=$(resolve_version "$repo_name" "$version")
echo "version to install ${version}"
printf -v binaries_dir "${binaries_dir_fmt}" "${version}"
local binaries_tarball="${binaries_dir}.tar.gz"
local shasum_file="SHA512SUMS"
local releases_url="https://github.com/${repo_owner}/${repo_name}/releases"
tar_url="${releases_url}/download/${version}/${binaries_tarball}"
sha_url="${releases_url}/download/${version}/${shasum_file}"
echo "download $tar_url"
STATUSCODE=$(curl -OL --write-out "%{http_code}" "${tar_url}")
if [ "$STATUSCODE" == "200" ]; then
echo "download $sha_url"
curl -OL "${sha_url}"
sha512sum -c ${shasum_file}
elif [ ${latest} == "true" ]; then
# Latest is release is not created yet, build it
build_asset "${asset}"
else
die "Failed to find asset version ${version}"
fi
tar xf "${binaries_tarball}"
pushd "${binaries_dir}"
sudo make install
popd
}
pushd ${tmp_dir}
download_asset "$asset" "${version}"
popd