From d4b9247f1470d3cd9a12b3b43a7a917830cae1c9 Mon Sep 17 00:00:00 2001 From: Chris F Ravenscroft Date: Wed, 22 Jul 2015 20:57:59 -0700 Subject: [PATCH] Execute commands in a container --- bocker | 81 ++++++++++++++++++++++++---------------------------------- 1 file changed, 33 insertions(+), 48 deletions(-) diff --git a/bocker b/bocker index 76676f4..ab2c89a 100755 --- a/bocker +++ b/bocker @@ -1,20 +1,13 @@ #!/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 bocker_check() { +btrfs subvolume list "$btrfs_path" | grep -qw "$1" && echo 0 || echo 1 } -function INIT() { +function bocker_init() { #HELP Create an image:\nBOCKER init +uuid="img_$(shuf -i 42002-42254 -n 1)" if [[ -d "$1" ]]; then - uuid="img_$(shuf -i 10000-99999 -n 1)" + [[ "$(bocker_check "$uuid")" == 0 ]] && bocker_run "$@" btrfs subvolume create "$btrfs_path/$uuid" > /dev/null cp -rf --reflink=auto "$1"/* "$btrfs_path/$uuid" > /dev/null echo "Created: $uuid" @@ -22,75 +15,67 @@ else echo "No directory named '$1' exists" fi } -function RM() { -CHECK '' "$1" +function bocker_rm() { #HELP Delete an image or container:\nBOCKER rm +[[ "$(bocker_check "$1")" == 1 ]] && echo "No container named '$1' exists" && exit 1 btrfs subvolume delete "$btrfs_path/$1" > /dev/null echo "Removed: $1" } -function IMAGES() { +function bocker_images() { #HELP List images:\nBOCKER images echo -e "IMAGE_ID" for img in "$btrfs_path"/img_*; do basename "$img" done } -function PS() { +function bocker_ps() { #HELP List containers:\nBOCKER 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)" +function bocker_run() { #HELP Create a container:\nBOCKER run /dev/null echo 'nameserver 8.8.8.8' > "$btrfs_path/$uuid"/etc/resolv.conf -echo "$2" > "$btrfs_path/$uuid/$uuid.cmd" +echo "$cmd" > "$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" \ + "$btrfs_path/$uuid" /bin/sh -c "/bin/mount -t proc proc /proc && $cmd" \ 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" +function bocker_exec() { #HELP Run bash (default) or comnmands in a container:\nBOCKER exec +[[ "$(bocker_check "$1")" == 1 ]] && echo "No container named '$1' exists" && exit 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 +function bocker_commit() { #HELP Commit a container to an image:\nBOCKER commit +[[ "$(bocker_check "$1")" == 1 ]] && echo "No container named '$1' exists" && exit 1 +[[ "$(bocker_check "$2")" == 1 ]] && echo "No image named '$1' exists" && exit 1 +bocker_rm "$2" && btrfs subvolume snapshot "$btrfs_path/$1" "$btrfs_path/$2" > /dev/null echo "Created: $2" } -function HELP() { -echo -e "Create an image: \n\t./bocker init \n" -echo -e "List images: \n\t./bocker images\n" -echo -e "Create a container: \n\t./bocker run \n" -echo -e "List containers: \n\t./bocker ps\n" -echo -e "View logs from a container: \n\t./bocker logs \n" -echo -e "Delete an image or container: \n\t./bocker rm " -exit 0 +function bocker_help() { #HELP Display this message:\nBOCKER help +sed -n "s/^.*#HELP\\s//p;" < "$1" | sed "s/\\\\n/\n\t/g;s/$/\n/;s!BOCKER!${1/!/\\!}!g" } -[[ -z "${1-}" ]] && HELP +[[ -z "${1-}" ]] && bocker_help "$0" case $1 in - init) INIT "$2" ;; - rm) RM "$2" ;; - images) IMAGES ;; - ps) PS ;; - run) - IMAGE="$2" - shift && shift - RUN "$IMAGE" "$*" - ;; - logs) LOGS "$2" ;; - commit) COMMIT "$2" "$3" ;; - *) HELP ;; + init|rm|images|ps|run|exec|logs|commit) bocker_"$1" "${@:2}" ;; + *) bocker_help "$0" ;; esac