diff --git a/README.md b/README.md index 30086bb..6459712 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Bocker -Docker implemented in 100 lines of bash. +Docker implemented in around 100 lines of bash. * [Prerequisites](#prerequisites) * [Example Usage](#example-usage) diff --git a/bocker b/bocker index c3de2c4..37e41c8 100755 --- a/bocker +++ b/bocker @@ -1,9 +1,13 @@ #!/usr/bin/env bash set -o errexit -o nounset -o pipefail; shopt -s nullglob -btrfs_path='/var/bocker'; cgroups='cpu,cpuacct,cpuset,memory'; $(cgget -h &> /dev/null) && CGX="cgexec -g $cgroups" || CGX='_=' +btrfs_path='/var/bocker' +cgroups='cpu,cpuacct,cpuset,memory'; $(cgget -h &> /dev/null) && CGX="cgexec -g $cgroups" || CGX='_=' + function bocker_check() { btrfs subvolume list "$btrfs_path" | grep -qw "$1" && echo 0 || echo 1 -}; function bocker_init() { #HELP Create an image from a directory:\nBOCKER init +} + +function bocker_init() { #HELP Create an image from a directory:\nBOCKER init uuid="img_$(shuf -i 42002-42254 -n 1)" if [[ -d "$1" ]]; then [[ "$(bocker_check "$uuid")" == 0 ]] && bocker_run "$@" @@ -14,7 +18,9 @@ function bocker_check() { else echo "No directory named '$1' exists" fi -}; function bocker_pull() { #HELP Pull an image from Docker Hub:\nBOCKER pull +} + +function bocker_pull() { #HELP Pull an image from Docker Hub:\nBOCKER pull token="$(curl -sL -o /dev/null -D- -H 'X-Docker-Token: true' "https://index.docker.io/v1/repositories/$1/images" | tr -d '\r' | awk -F ': *' '$1 == "X-Docker-Token" { print $2 }')" registry='https://registry-1.docker.io/v1' id="$(curl -sL -H "Authorization: Token $token" "$registry/repositories/$1/tags/$2" | sed 's/"//g')" @@ -27,24 +33,32 @@ function bocker_check() { done echo "$1:$2" > /tmp/"$tmp_uuid"/img.source bocker_init /tmp/"$tmp_uuid" && rm -rf /tmp/"$tmp_uuid" -}; function bocker_rm() { #HELP Delete an image or container:\nBOCKER rm +} + +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 cgdelete -g $cgroups:/$1 &> /dev/null || true echo "Removed: $1" -}; function bocker_images() { #HELP List images:\nBOCKER images +} + +function bocker_images() { #HELP List images:\nBOCKER images echo -e "IMAGE_ID\t\tSOURCE" for img in "$btrfs_path"/img_*; do img=$(basename "$img") echo -e "$img\t\t$(cat "$btrfs_path/$img/img.source")" done -}; function bocker_ps() { #HELP List containers:\nBOCKER 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 bocker_run() { #HELP Create a container:\nBOCKER run +} + +function bocker_run() { #HELP Create a container:\nBOCKER run uuid="ps_$(shuf -i 42002-42254 -n 1)" [[ "$(bocker_check "$1")" == 1 ]] && echo "No image named '$1' exists" && exit 1 [[ "$(bocker_check "$uuid")" == 0 ]] && echo "UUID conflict, retrying..." && bocker_run "$@" && return @@ -68,21 +82,31 @@ function bocker_check() { 2>&1 | tee "$btrfs_path/$uuid/$uuid.log" || true ip link del dev veth0_"$uuid" ip netns del netns_"$uuid" -}; function bocker_exec() { #HELP Execute a command in an existing container:\nBOCKER exec +} + +function bocker_exec() { #HELP Execute a command in an existing container:\nBOCKER exec [[ "$(bocker_check "$1")" == 1 ]] && echo "No container named '$1' exists" && exit 1 cmd=${@:2} && cid=$(ps xao ppid,pid,cmd | grep "$(ps -u | grep unshare | grep "$1" | awk '{ print $2 }')" | grep -v unshare | awk '{ print $2 }') nsenter -t "$cid" -m -u -i -n -p chroot "$btrfs_path/$1" "$cmd" -}; function bocker_logs() { #HELP View logs from a container:\nBOCKER logs +} + +function bocker_logs() { #HELP View logs from a container:\nBOCKER logs [[ "$(bocker_check "$1")" == 1 ]] && echo "No container named '$1' exists" && exit 1 cat "$btrfs_path/$1/$1.log" -}; function bocker_commit() { #HELP Commit a container to an image:\nBOCKER commit +} + +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 bocker_help() { #HELP Display this message:\nBOCKER help +} + +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-}" ]] && bocker_help "$0" +} + +[[ -z "${1-}" ]] && bocker_help "$0" case $1 in pull|init|rm|images|ps|run|exec|logs|commit) bocker_"$1" "${@:2}" ;; *) bocker_help "$0" ;; diff --git a/tests/test_length b/tests/test_length deleted file mode 100644 index 8f7a441..0000000 --- a/tests/test_length +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env bash -set -o errexit -o nounset -o pipefail - -[[ "$(wc -l < bocker)" -le 100 ]]; echo $?