Merge remote-tracking branch 'origin/feature/jenkins-compatible-build-script' into...
[tails-test.git] / vagrant / provision / assets / build-tails
blobcf6b4000b4858dfd1e96bcf2c084e1366eed70e8
1 #!/bin/sh
3 # This script is used by both our Vagrant and Jenkins -based build environments.
5 set -e
7 TMPFS_SIZE_MB=6144 # 6 GB
8 TMPFS_SIZE_BYTES=$(($TMPFS_SIZE_MB * 1024 * 1024))
10 as_root_do() {
11 sudo \
12 ${RSYNC_PROXY:+RSYNC_PROXY="$RSYNC_PROXY"} \
13 ${http_proxy:+http_proxy="$http_proxy"} \
14 ${https_proxy:+https_proxy="$https_proxy"} \
15 ${ftp_proxy:+ftp_proxy="$ftp_proxy"} \
16 ${no_proxy:+no_proxy="$no_proxy"} \
17 ${MKSQUASHFS_OPTIONS:+MKSQUASHFS_OPTIONS="$MKSQUASHFS_OPTIONS"} \
18 "$@"
21 usable_memory() {
22 free -b | awk '/cache:/ { print $4 }'
25 bootstrap_stage_is_ok() {
26 [ -d "$1" ] || return 1
27 [ "$(sudo du -sm $1 | cut -f1)" -ge 100 ] || return 1
28 for dir in bin dev etc lib proc root sbin sys usr var; do
29 [ -d "$1/$dir" ] || return 1
30 done
31 return 0
34 cleanup() {
35 [ -n "$BUILD_DIR" ] || return 0
36 cd /
37 mounts=$(mount | grep -E "^[^[:space:]]+ on $BUILD_DIR" | \
38 awk '{print $3}' | sort -r)
39 [ -n "$mounts" ] && sudo umount $mounts
40 [ -d "$TMPFS_DIR" ] && ( sudo umount $TMPFS_DIR ; rmdir $TMPFS_DIR )
41 [ -d "$BUILD_DIR" ] && sudo rm -rf $BUILD_DIR
44 trap cleanup EXIT
46 if [ "$TAILS_RAM_BUILD" ]; then
47 if [ "$(usable_memory)" -lt "$TMPFS_SIZE_BYTES" ]; then
48 echo "Not enough memory available for an in-memory build. Aborting." >&2
49 exit 1
53 if [ -n "$JENKINS_URL" ]; then
54 # Build triggered by Jenkins
55 if [ -z "$WORKSPACE" ]; then
56 echo "WORKSPACE environment variable is not set. Aborting." >&2
58 if mountpoint -q /srv/www; then
59 ARTIFACTS_DIR=/srv/www/"$JOB_NAME"
60 else
61 echo "Shared directory is not available, saving artifacts in workspace." >&2
62 ARTIFACTS_DIR="$WORKSPACE"
64 if [ -z "$GIT_BRANCH" ]; then
65 echo "GIT_BRANCH environment variable is not set. Aborting." >&2
67 if [ -z "$GIT_COMMIT" ]; then
68 echo "GIT_COMMIT environment variable is not set. Aborting." >&2
70 REV="$GIT_BRANCH"
71 COMMIT="$GIT_COMMIT"
72 cd "$WORKSPACE"
73 else
74 # Build triggered by Vagrant
75 WORKSPACE=/home/vagrant/amnesia
76 ARTIFACTS_DIR=/vagrant
77 REV="${1:-$(git --git-dir=/amnesia.git name-rev --name-only HEAD)}"
78 COMMIT="$(git --git-dir=/amnesia.git rev-parse --verify "$REV")"
79 test -d "$WORKSPACE" || git clone /amnesia.git "$WORKSPACE"
80 cd "$WORKSPACE"
81 git fetch origin
82 git checkout --force "$REV"
83 git reset --hard "$COMMIT"
86 install -m 0700 -d "$ARTIFACTS_DIR"
88 if [ "$TAILS_CLEAN_BUILD" ]; then
89 as_root_do lb clean --all
90 sudo rm -rf /var/cache/stages_bootstrap
92 ./build-wiki
94 BUILD_DIR=$(mktemp -d /tmp/tails-build.XXXXXXXX)
95 if [ "$TAILS_RAM_BUILD" ]; then
96 TMPFS_DIR=$(mktemp -d /tmp/tmpfs.XXXXXXXX)
97 as_root_do mount -t tmpfs -o "noatime,size=${TMPFS_SIZE_BYTES},mode=0770,uid=root,gid=${USER}" tmpfs "$TMPFS_DIR"
98 as_root_do mount -t aufs -o "noatime,noxino,dirs=$TMPFS_DIR=rw:${WORKSPACE}/=rr+wh" aufs "$BUILD_DIR"
99 else
100 as_root_do rsync -a "$WORKSPACE"/ "$BUILD_DIR"/
103 cd "$BUILD_DIR"
104 as_root_do lb config --cache-packages false
106 if [ "$TAILS_BOOTSTRAP_CACHE" ] && \
107 bootstrap_stage_is_ok /var/cache/stages_bootstrap; then
108 # restore bootstrap stage and make live-build use it
109 sudo mkdir -p "$BUILD_DIR"/cache/stages_bootstrap
110 sudo mount --bind /var/cache/stages_bootstrap \
111 "$BUILD_DIR"/cache/stages_bootstrap
112 sudo touch "$BUILD_DIR"/.stage/bootstrap
113 sudo touch "$BUILD_DIR"/.stage/bootstrap_cache.save
116 as_root_do lb build
118 mv -f tails-* "$ARTIFACTS_DIR"
120 if [ "$TAILS_BOOTSTRAP_CACHE" ] && \
121 ! sudo umount "$BUILD_DIR"/cache/stages_bootstrap 2>/dev/null; then
122 # the cached bootstrap stage wasn't used (maybe it hadn't been
123 # cached yet?) so we save the good one from the current build.
124 sudo rsync -a --delete "$BUILD_DIR"/cache/stages_bootstrap/ \
125 /var/cache/stages_bootstrap