bocker/bocker
2015-07-23 09:48:52 +10:00

91 lines
2.9 KiB
Bash
Executable File

#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail; shopt -s nullglob
btrfs_path='/var/bocker';
function CHECK() {
[[ "$1" == 'img' ]] && TYPE='image'
[[ "$1" == 'ps' ]] && TYPE='container'
[[ "$1" == '' ]] && TYPE='container or image'
if [[ "$2" == "$1"* ]]; then
if btrfs subvolume list "$btrfs_path" | grep -qw "$2"; then
return 0
fi
fi
echo "No $TYPE named '$2' exists" && exit 1
}
function INIT() {
if [[ -d "$1" ]]; then
uuid="img_$(shuf -i 10000-99999 -n 1)"
btrfs subvolume create "$btrfs_path/$uuid" > /dev/null
cp -rf --reflink=auto "$1"/* "$btrfs_path/$uuid" > /dev/null
echo "Created: $uuid"
else
echo "No directory named '$1' exists"
fi
}
function RM() {
CHECK '' "$1"
btrfs subvolume delete "$btrfs_path/$1" > /dev/null
echo "Removed: $1"
}
function IMAGES() {
echo -e "IMAGE_ID"
for img in "$btrfs_path"/img_*; do
basename "$img"
done
}
function PS() {
echo -e "CONTAINER_ID\t\tCOMMAND"
for ps in "$btrfs_path"/ps_*; do
ps=$(basename "$ps")
echo -e "$ps\t\t$(cat "$btrfs_path/$ps/$ps.cmd")"
done
}
function RUN() {
CHECK 'img' "$1"
uuid="ps_$(shuf -i 10000-99999 -n 1)"
ip link add dev veth0_"$uuid" type veth peer name veth1_"$uuid"
ip link set dev veth0_"$uuid" up
ip link set veth0_"$uuid" master bridge0
ip netns add netns_"$uuid"
ip link set veth1_"$uuid" netns netns_"$uuid"
ip netns exec netns_"$uuid" ip link set dev lo up
ip netns exec netns_"$uuid" ip addr add 10.0.0.2/24 dev veth1_"$uuid"
ip netns exec netns_"$uuid" ip link set dev veth1_"$uuid" up
ip netns exec netns_"$uuid" ip route add default via 10.0.0.1
btrfs subvolume snapshot "$btrfs_path/$1" "$btrfs_path/$uuid" > /dev/null
echo 'nameserver 8.8.8.8' > "$btrfs_path/$uuid"/etc/resolv.conf
echo "$2" > "$btrfs_path/$uuid/$uuid.cmd"
ip netns exec netns_"$uuid" "unshare" -fp --mount-proc "chroot" \
"$btrfs_path/$uuid" /bin/sh -c "/bin/mount -t proc proc /proc && $2" \
2>&1 | tee "$btrfs_path/$uuid/$uuid.log" || true
ip link del dev veth0_"$uuid"
ip netns del netns_"$uuid"
}
function LOGS() {
CHECK 'ps' "$1"
cat "$btrfs_path/$1/$1.log"
}
function COMMIT() {
CHECK 'ps' "$1" && CHECK 'img' "$2" && RM "$2"
btrfs subvolume snapshot "$btrfs_path/$1" "$btrfs_path/$2" > /dev/null
echo "Created: $2"
}
function HELP() {
sed -n "s/^.*#HELP\\s//p;" < $1 | sed "s/\\\\n/\n\t/g;s/$/\n/;s!BOCKER!${1/!/\\!}!g"
}
[[ -z "${1-}" ]] && HELP $0 && exit 0
case $1 in
init) INIT "$2" ;; #HELP Create an image:\nBOCKER init <image_directory>
images) IMAGES ;; #HELP List images:\nBOCKER images
run) #HELP Create a container:\nBOCKER run <image_id> <command>
IMAGE="$2"
shift && shift
RUN "$IMAGE" "$*"
;;
ps) PS ;; #HELP List containers:\nBOCKER ps
logs) LOGS "$2" ;; #HELP View logs from a container:\nBOCKER logs <container_id>
commit) COMMIT "$2" "$3" ;; #HELP Commit a container to the image:\nBOCKER commit <container_id> <image_id>
rm) RM "$2" ;; #HELP Delete an image or container:\nBOCKER rm <image_id or container_id>
*) HELP $0 ;; #HELP Display this message:\nBOCKER help
esac