forked from moul/image-builder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.sh
118 lines (104 loc) · 2.68 KB
/
lib.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
# Declares helpers for image building
set -e
[ "$DEBUG" = "1" ] && set -x
prepare_nbd_volume() {
device=$1
if ! `mountpoint -q "$TARGET"`; then
sudo mkfs.ext4 "$device"
sudo mkdir -p "$TARGET.device"
sudo mount "$device" "$TARGET.device"
rsync -aHAX "$TARGET/" "$TARGET.device"
fi
}
require_debootstrap() {
type -P debootstrap >/dev/null && return
sudo apt-get update
sudo apt-get -y install debootstrap
}
clean_workspace() {
sudo rm -rf $TARGET/* $TARGET/.??*
}
debootstrap() {
if [ ! -d "$TARGET.debootstrap" ]; then
sudo debootstrap \
--arch="$ARCH" \
--variant="$VARIANT" \
--components="$COMPONENTS" \
--include="$PKGS_INCLUDE" \
"$VERSION" \
"$TARGET.debootstrap" \
"$MIRROR" \
"$SCRIPT"
fi
rsync -aHAX "$TARGET.debootstrap/" "$TARGET/"
}
upgrade_debs() {
do_in_target "apt-get update"
do_in_target "apt-get -y upgrade"
}
secondstage() {
# This step could be done directly by removing
# do_in_target /debootstrap/debootstrap --second-stage
echo "Not needed anymore (removed the --foreign option)"
}
patch_target() {
patches_dir=../$1
for file in $(find "$patches_dir" -type f | sed -n "s|^$patches_dir/||p"); do
sudo mkdir -p "$TARGET/$(dirname $file)"
sudo cp "$patches_dir/$file" "$TARGET/$file"
done
}
clean_target() {
clean_paths="$@"
for path in $clean_paths; do
if [ -e "$TARGET/$path" ]; then
sudo rm -rf "$TARGET/$path"
fi
done
for file in $(find "$TARGET/var/log" -type f); do
echo | sudo tee $file
done
find "$TARGET" \( -name "*~" -or -name ".??*~" -or -name "#*#" -or -name ".#*" \) -delete
}
archive_target() {
sudo tar -C "$TARGET" -czf "$NAME.tar.gz" .
}
do_in_target() {
sudo chroot "$TARGET" su - root -c "$@"
}
push_to_s3() {
edit_date=$(stat -c %Y "$TARGET")
s3cmd put --acl-public "$NAME.tar.gz" "$S3_URL/$NAME-${edit_date}.tar.gz"
s3cmd put --acl-public "$NAME.tar.gz" "$S3_URL/$NAME-latest.tar.gz"
s3cmd ls "s3://rescue-images/rescue/"
# s3cmd cp --acl-public "s3://rescue-images/rescue/$NAME-${edit_date}.tar.gz" "s3://rescue-images/rescue/$NAME-latest.tar.gz"
}
cli() {
case $1 in
"tarball")
build_image
patch_image
upgrade_image
clean_image
archive_target
push_to_s3
exit 0
;;
"image")
NBD_DEVICE=${2:-"/dev/nbd1"}
build_image
patch_image
upgrade_image
clean_image
prepare_nbd_volume $NBD_DEVICE
sync
exit 0
;;
"build_image"|"patch_image"|"archive_target"|"prepare_nbd_volume"|"upgrade_image"|"clean_image"|"push_to_s3")
eval $@
exit 0
;;
esac
echo >&2 "usage: [DEBUG=1] $0 (tarball|image)"
exit 1
}