scaleway-image-archlinux/lib.sh

119 lines
2.7 KiB
Bash
Raw Normal View History

2014-10-15 13:30:39 +02:00
# 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"
2014-10-21 16:01:40 +02:00
sudo mkdir -p "$TARGET.device"
sudo mount "$device" "$TARGET.device"
2014-10-21 16:22:41 +02:00
rsync -aHAX "$TARGET/" "$TARGET.device"
2014-10-15 13:30:39 +02:00
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() {
2014-10-28 16:18:30 +01:00
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/"
2014-10-15 13:30:39 +02:00
}
2014-10-15 19:30:46 +02:00
upgrade_debs() {
2014-10-29 15:49:36 +01:00
do_in_target "apt-get update"
do_in_target "apt-get -y upgrade"
2014-10-15 19:30:46 +02:00
}
2014-10-15 13:30:39 +02:00
secondstage() {
# This step could be done directly by removing
2014-10-28 16:18:30 +01:00
# do_in_target /debootstrap/debootstrap --second-stage
echo "Not needed anymore (removed the --foreign option)"
2014-10-15 13:30:39 +02:00
}
patch_target() {
2014-10-21 12:42:59 +02:00
patches_dir=../$1
2014-10-15 13:30:39 +02:00
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() {
2014-11-09 22:52:00 +01:00
clean_paths="$@"
2014-10-15 13:30:39 +02:00
for path in $clean_paths; do
if [ -e "$TARGET/$path" ]; then
sudo rm -rf "$TARGET/$path"
fi
done
2014-10-15 19:30:46 +02:00
for file in $(find "$TARGET/var/log" -type f); do
2014-10-15 23:12:21 +02:00
echo | sudo tee $file
2014-10-15 19:30:46 +02:00
done
2014-10-27 18:13:50 +01:00
find "$TARGET" \( -name "*~" -or -name ".??*~" -or -name "#*#" -or -name ".#*" \) -delete
2014-10-15 13:30:39 +02:00
}
archive_target() {
sudo tar -C "$TARGET" -czf "$NAME.tar.gz" .
}
do_in_target() {
sudo chroot "$TARGET" su - root -c "$@"
}
2014-10-27 17:34:22 +01:00
push_to_s3() {
edit_date=$(stat -c %Y "$TARGET")
s3cmd put --acl-public "$NAME.tar.gz" "$S3_URL/$NAME-${edit_date}.tar.gz"
2014-11-05 16:12:14 +01:00
s3cmd put --acl-public "$NAME.tar.gz" "$S3_URL/$NAME-latest.tar.gz"
2014-10-27 17:34:22 +01:00
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"
}
2014-10-15 13:30:39 +02:00
cli() {
case $1 in
"tarball")
build_image
2014-10-15 19:34:58 +02:00
patch_image
2014-10-16 18:58:43 +02:00
upgrade_image
clean_image
2014-10-15 13:30:39 +02:00
archive_target
2014-10-27 17:34:22 +01:00
push_to_s3
2014-10-15 13:30:39 +02:00
exit 0
;;
"image")
2014-10-17 19:53:30 +02:00
NBD_DEVICE=${2:-"/dev/nbd1"}
2014-10-15 13:30:39 +02:00
build_image
2014-10-15 19:34:58 +02:00
patch_image
2014-10-16 18:58:43 +02:00
upgrade_image
clean_image
2014-10-21 16:01:40 +02:00
prepare_nbd_volume $NBD_DEVICE
2014-10-21 16:34:23 +02:00
sync
2014-10-15 19:34:58 +02:00
exit 0
;;
2014-10-27 17:34:22 +01:00
"build_image"|"patch_image"|"archive_target"|"prepare_nbd_volume"|"upgrade_image"|"clean_image"|"push_to_s3")
eval $@
2014-10-17 00:07:38 +02:00
exit 0
;;
2014-10-15 13:30:39 +02:00
esac
2014-10-16 18:58:43 +02:00
echo >&2 "usage: [DEBUG=1] $0 (tarball|image)"
2014-10-15 13:30:39 +02:00
exit 1
}