-
Notifications
You must be signed in to change notification settings - Fork 28
/
build
executable file
·163 lines (133 loc) · 5.62 KB
/
build
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
#!/usr/bin/env bash
# fail fast
set -eo pipefail
set -eu
start_time=$(date +%s%N)
# debug
#set -x
function error() {
echo " ! $*" >&2
exit 1
}
function topic() {
echo "-----> $*"
}
function indent() {
c='s/^/ /'
case $(uname) in
Darwin) sed -l "$c";;
*) sed -u "$c";;
esac
}
layers_dir="$1"
env_dir="$2/env"
plan_path="$3"
if compgen -G "${env_dir}/*" > /dev/null; then
for var in ${env_dir}/*; do
declare "$(basename ${var})=$(<${var})"
done
fi
apt_layer=$layers_dir/apt
mkdir -p $apt_layer
# Store which STACK we are running on in the cache to bust the cache if it changes
if [ -f $apt_layer/STACK ]; then
CACHED_STACK=$(cat "$apt_layer/STACK")
else
CACHED_STACK=$CNB_STACK_ID
fi
# Ensure we store the STACK in the cache for next time.
echo "$CNB_STACK_ID" > "$apt_layer/STACK"
APT_CACHE_DIR="$apt_layer/cache"
APT_STATE_DIR="$apt_layer/state"
APT_SOURCELIST_DIR="$apt_layer/sources" # place custom sources.list here
APT_SOURCES="$APT_SOURCELIST_DIR/sources.list"
APT_VERSION=$(apt-get -v | awk 'NR == 1{ print $2 }')
case "$APT_VERSION" in
0* | 1.0*) APT_FORCE_YES="--force-yes";;
*) APT_FORCE_YES="--allow-downgrades --allow-remove-essential --allow-change-held-packages";;
esac
if [ -f $apt_layer/Aptfile ] && cmp -s Aptfile $apt_layer/Aptfile && [[ $CACHED_STACK == $CNB_STACK_ID ]] ; then
topic "Aptfile and stack look the same, reusing cache"
else
# Aptfile changed or does not exist or STACK changed
topic "Detected Aptfile changes, flushing cache"
rm -rf $APT_CACHE_DIR
mkdir -p "$APT_CACHE_DIR/archives/partial"
mkdir -p "$APT_STATE_DIR/lists/partial"
mkdir -p "$APT_SOURCELIST_DIR" # make dir for sources
cp -f Aptfile "$apt_layer"/Aptfile
cat "/etc/apt/sources.list" > "$APT_SOURCES" # no cp here
# add custom repositories from Aptfile to sources.list
# like>> :repo:deb http://cz.archive.ubuntu.com/ubuntu artful main universe
if grep -q -e "^:repo:" Aptfile; then
topic "Adding custom repositories"
cat Aptfile | grep -s -e "^:repo:" | sed 's/^:repo:\(.*\)\s*$/\1/g' >> $APT_SOURCES
fi
APT_OPTIONS="-o debug::nolocking=true -o dir::cache=$APT_CACHE_DIR -o dir::state=$APT_STATE_DIR"
APT_OPTIONS="$APT_OPTIONS -o dir::etc::sourcelist=$APT_SOURCES -o dir::etc::sourceparts=/dev/null"
topic "Updating apt caches"
# Ignore stderr from apt-get update containing: rm: cannot remove '/var/cache/apt/archives/partial/*.deb': Permission denied
apt-get $APT_OPTIONS update 2> >(grep -v "rm: cannot remove") | indent
for PACKAGE in $(cat Aptfile | grep -v -s -e '^#' | grep -v -s -e "^:repo:"); do
if [[ $PACKAGE == *deb ]]; then
PACKAGE_NAME=$(basename $PACKAGE .deb)
PACKAGE_FILE=$APT_CACHE_DIR/archives/$PACKAGE_NAME.deb
topic "Fetching $PACKAGE"
curl --silent --show-error --fail -L -z $PACKAGE_FILE -o $PACKAGE_FILE $PACKAGE 2>&1 | indent
else
topic "Fetching .debs for $PACKAGE"
apt-get $APT_OPTIONS -y $APT_FORCE_YES -d install --reinstall $PACKAGE | indent
fi
done
topic "Installing apt packages with dpkg"
for DEB in $(ls -1 $APT_CACHE_DIR/archives/*.deb); do
echo "$(basename $DEB)" | indent
dpkg -x $DEB $apt_layer/
done
topic "Writing profile script and environment variables"
exports_path=$apt_layer/.profile.d/000_apt.sh
mkdir -p $apt_layer/.profile.d
touch $exports_path
apt_env_dir=$apt_layer/env
mkdir -p $apt_env_dir
path="$apt_layer/usr/bin"
echo "PATH=$path" | indent
echo "export PATH=\"$path:\$PATH\"" >> $exports_path
echo -n "$path:$PATH" > $apt_env_dir/PATH.prepend
echo -n ":" > $apt_env_dir/PATH.delim
ld_library_path=$apt_layer/lib/x86_64-linux-gnu:$apt_layer/lib/i386-linux-gnu:$apt_layer/lib:$apt_layer/usr/lib/x86_64-linux-gnu:$apt_layer/usr/lib/i386-linux-gnu:$apt_layer/usr/lib
echo "LD_LIBRARY_PATH=$ld_library_path" | indent
echo "export LD_LIBRARY_PATH=\"$ld_library_path:\$LD_LIBRARY_PATH\"" >> $exports_path
echo -n "$ld_library_path" > $apt_env_dir/LD_LIBRARY_PATH.prepend
echo -n ":" > $apt_env_dir/LD_LIBRARY_PATH.delim
library_path=$ld_library_path
echo "LIBRARY_PATH=$library_path" | indent
echo "export LIBRARY_PATH=\"$library_path:\$LIBRARY_PATH\"" >> $exports_path
echo -n "$library_path" > $apt_env_dir/LIBRARY_PATH.prepend
echo -n ":" > $apt_env_dir/LIBRARY_PATH.delim
include_path=$apt_layer/usr/include:$apt_layer/usr/include/x86_64-linux-gnu
echo "INCLUDE_PATH=$include_path" | indent
echo "export INCLUDE_PATH=\"$include_path:\$INCLUDE_PATH\"" >> $exports_path
echo -n "$include_path" > $apt_env_dir/INCLUDE_PATH.prepend
echo -n ":" > $apt_env_dir/INCLUDE_PATH.delim
cpath="\$INCLUDE_PATH"
echo "CPATH=$cpath" | indent
echo "export CPATH=\"$cpath\"" >> $exports_path
cp "$apt_env_dir"/INCLUDE_PATH.prepend "$apt_env_dir"/CPATH.prepend
echo -n ":" > $apt_env_dir/CPATH.delim
cpppath="\$INCLUDE_PATH"
echo "CPPPATH=$cpppath" | indent
echo "export CPPPATH=\"$cpppath\"" >> $exports_path
cp "$apt_env_dir"/INCLUDE_PATH.prepend "$apt_env_dir"/CPPPATH.prepend
echo -n ":" > $apt_env_dir/CPPPATH.delim
pkg_config_path=$apt_layer/usr/lib/x86_64-linux-gnu/pkgconfig:$apt_layer/usr/lib/i386-linux-gnu/pkgconfig:$apt_layer/usr/lib/pkgconfig
echo "PKG_CONFIG_PATH=$pkg_config_path" | indent
echo "export PKG_CONFIG_PATH=\"$pkg_config_path:\$PKG_CONFIG_PATH\"" >> $exports_path
echo -n "$pkg_config_path" > $apt_env_dir/PKG_CONFIG_PATH.prepend
echo -n ":" > $apt_env_dir/PKG_CONFIG_PATH.delim
fi
echo "launch = true" > ${apt_layer}.toml
echo "build = true" >> ${apt_layer}.toml
echo "cache = true" >> ${apt_layer}.toml
end_time=$(date +%s%N)
echo "Elapsed time: $(( $(( $end_time - $start_time )) / 1000000 ))ms" | indent