1
- #! /bin/bash -i
1
+
2
2
3
3
clean_download () {
4
4
# The purpose of this function is to download a file with minimal impact on container layer size
5
- # this means if no valid downloader is found (curl or wget) then we install a downloader (currently wget) in a
6
- # temporary manner, and making sure to
5
+ # this means if no valid downloader is found (curl or wget) then we install a downloader (currently wget) in a
6
+ # temporary manner, and making sure to
7
7
# 1. uninstall the downloader at the return of the function
8
8
# 2. revert back any changes to the package installer database/cache (for example apt-get lists)
9
- # The above steps will minimize the leftovers being created while installing the downloader
9
+ # The above steps will minimize the leftovers being created while installing the downloader
10
10
# Supported distros:
11
11
# debian/ubuntu/alpine
12
12
@@ -15,16 +15,16 @@ clean_download() {
15
15
tempdir=$( mktemp -d)
16
16
downloader_installed=" "
17
17
18
- function _apt_get_install() {
18
+ _apt_get_install () {
19
19
tempdir=$1
20
20
21
- # copy current state of apt list - in order to revert back later (minimize contianer layer size)
22
- cp -p -R /var/lib/apt/lists $tempdir
21
+ # copy current state of apt list - in order to revert back later (minimize contianer layer size)
22
+ cp -p -R /var/lib/apt/lists $tempdir
23
23
apt-get update -y
24
24
apt-get -y install --no-install-recommends wget ca-certificates
25
25
}
26
26
27
- function _apt_get_cleanup() {
27
+ _apt_get_cleanup () {
28
28
tempdir=$1
29
29
30
30
echo " removing wget"
@@ -35,21 +35,20 @@ clean_download() {
35
35
rm -r /var/lib/apt/lists && mv $tempdir /lists /var/lib/apt/lists
36
36
}
37
37
38
- function _apk_install() {
38
+ _apk_install () {
39
39
tempdir=$1
40
- # copy current state of apk cache - in order to revert back later (minimize contianer layer size)
41
- cp -p -R /var/cache/apk $tempdir
40
+ # copy current state of apk cache - in order to revert back later (minimize contianer layer size)
41
+ cp -p -R /var/cache/apk $tempdir
42
42
43
43
apk add --no-cache wget
44
44
}
45
45
46
- function _apk_cleanup() {
46
+ _apk_cleanup () {
47
47
tempdir=$1
48
48
49
49
echo " removing wget"
50
- apk del wget
50
+ apk del wget
51
51
}
52
-
53
52
# try to use either wget or curl if one of them already installer
54
53
if type curl > /dev/null 2>&1 ; then
55
54
downloader=curl
@@ -76,7 +75,7 @@ clean_download() {
76
75
if [ $downloader = " wget" ] ; then
77
76
wget -q $url -O $output_location
78
77
else
79
- curl -sfL $url -o $output_location
78
+ curl -sfL $url -o $output_location
80
79
fi
81
80
82
81
# NOTE: the cleanup procedure was not implemented using `trap X RETURN` only because
@@ -90,54 +89,49 @@ clean_download() {
90
89
echo " distro not supported"
91
90
exit 1
92
91
fi
93
- fi
92
+ fi
94
93
95
94
}
96
95
96
+
97
97
ensure_nanolayer () {
98
98
# Ensure existance of the nanolayer cli program
99
99
local variable_name=$1
100
100
101
101
local required_version=$2
102
- # normalize version
103
- if ! [[ $required_version == v* ]]; then
104
- required_version=v$required_version
105
- fi
106
102
107
- local nanolayer_location =" "
103
+ local __nanolayer_location =" "
108
104
109
105
# If possible - try to use an already installed nanolayer
110
- if [[ -z " ${NANOLAYER_FORCE_CLI_INSTALLATION} " ] ]; then
111
- if [[ -z " ${NANOLAYER_CLI_LOCATION} " ] ]; then
106
+ if [ -z " ${NANOLAYER_FORCE_CLI_INSTALLATION} " ]; then
107
+ if [ -z " ${NANOLAYER_CLI_LOCATION} " ]; then
112
108
if type nanolayer > /dev/null 2>&1 ; then
113
109
echo " Found a pre-existing nanolayer in PATH"
114
- nanolayer_location =nanolayer
110
+ __nanolayer_location =nanolayer
115
111
fi
116
112
elif [ -f " ${NANOLAYER_CLI_LOCATION} " ] && [ -x " ${NANOLAYER_CLI_LOCATION} " ] ; then
117
- nanolayer_location =${NANOLAYER_CLI_LOCATION}
118
- echo " Found a pre-existing nanolayer which were given in env variable: $nanolayer_location "
113
+ __nanolayer_location =${NANOLAYER_CLI_LOCATION}
114
+ echo " Found a pre-existing nanolayer which were given in env variable: $__nanolayer_location "
119
115
fi
120
116
121
117
# make sure its of the required version
122
- if ! [[ -z " ${nanolayer_location } " ] ]; then
118
+ if ! [ -z " ${__nanolayer_location } " ]; then
123
119
local current_version
124
- current_version=$( $nanolayer_location --version)
125
- if ! [[ $current_version == v* ]]; then
126
- current_version=v$current_version
127
- fi
120
+ current_version=$( $__nanolayer_location --version)
121
+
128
122
129
123
if ! [ $current_version == $required_version ]; then
130
124
echo " skipping usage of pre-existing nanolayer. (required version $required_version does not match existing version $current_version )"
131
- nanolayer_location =" "
125
+ __nanolayer_location =" "
132
126
fi
133
127
fi
134
128
135
129
fi
136
130
137
- # If not previuse installation found, download it temporarly and delete at the end of the script
138
- if [[ -z " ${nanolayer_location } " ] ]; then
131
+ # If not previuse installation found, download it temporarly and delete at the end of the script
132
+ if [ -z " ${__nanolayer_location } " ]; then
139
133
140
- if [ " $( uname -sm) " == " Linux x86_64" ] || [ " $( uname -sm) " = = " Linux aarch64" ]; then
134
+ if [ " $( uname -sm) " = ' Linux x86_64' ] || [ " $( uname -sm) " = " Linux aarch64" ]; then
141
135
tmp_dir=$( mktemp -d -t nanolayer-XXXXXXXXXX)
142
136
143
137
clean_up () {
@@ -147,7 +141,7 @@ ensure_nanolayer() {
147
141
}
148
142
trap clean_up EXIT
149
143
150
-
144
+
151
145
if [ -x " /sbin/apk" ] ; then
152
146
clib_type=musl
153
147
else
@@ -158,11 +152,11 @@ ensure_nanolayer() {
158
152
159
153
# clean download will minimize leftover in case a downloaderlike wget or curl need to be installed
160
154
clean_download https://github.com/devcontainers-contrib/cli/releases/download/$required_version /$tar_filename $tmp_dir /$tar_filename
161
-
155
+
162
156
tar xfzv $tmp_dir /$tar_filename -C " $tmp_dir "
163
157
chmod a+x $tmp_dir /nanolayer
164
- nanolayer_location =$tmp_dir /nanolayer
165
-
158
+ __nanolayer_location =$tmp_dir /nanolayer
159
+
166
160
167
161
else
168
162
echo " No binaries compiled for non-x86-linux architectures yet: $( uname -m) "
@@ -171,5 +165,8 @@ ensure_nanolayer() {
171
165
fi
172
166
173
167
# Expose outside the resolved location
174
- declare -g ${variable_name} =$nanolayer_location
168
+ export ${variable_name} =$__nanolayer_location
169
+
175
170
}
171
+
172
+
0 commit comments