mirror of
https://github.com/p8952/bocker.git
synced 2025-11-09 00:13:59 +01:00
Merge branch 'wking-indent-function-bodies'
This commit is contained in:
commit
1b5f10b8ab
136
bocker
136
bocker
@ -2,94 +2,94 @@
|
|||||||
set -o errexit -o nounset -o pipefail; shopt -s nullglob
|
set -o errexit -o nounset -o pipefail; shopt -s nullglob
|
||||||
btrfs_path='/var/bocker';
|
btrfs_path='/var/bocker';
|
||||||
function bocker_check() {
|
function bocker_check() {
|
||||||
btrfs subvolume list "$btrfs_path" | grep -qw "$1" && echo 0 || echo 1
|
btrfs subvolume list "$btrfs_path" | grep -qw "$1" && echo 0 || echo 1
|
||||||
}
|
}
|
||||||
function bocker_init() { #HELP Create an image from a directory:\nBOCKER init <directory>
|
function bocker_init() { #HELP Create an image from a directory:\nBOCKER init <directory>
|
||||||
uuid="img_$(shuf -i 42002-42254 -n 1)"
|
uuid="img_$(shuf -i 42002-42254 -n 1)"
|
||||||
if [[ -d "$1" ]]; then
|
if [[ -d "$1" ]]; then
|
||||||
[[ "$(bocker_check "$uuid")" == 0 ]] && bocker_run "$@"
|
[[ "$(bocker_check "$uuid")" == 0 ]] && bocker_run "$@"
|
||||||
btrfs subvolume create "$btrfs_path/$uuid" > /dev/null
|
btrfs subvolume create "$btrfs_path/$uuid" > /dev/null
|
||||||
cp -rf --reflink=auto "$1"/* "$btrfs_path/$uuid" > /dev/null
|
cp -rf --reflink=auto "$1"/* "$btrfs_path/$uuid" > /dev/null
|
||||||
[[ ! -f "$btrfs_path/$uuid"/img.source ]] && echo "$1" > "$btrfs_path/$uuid"/img.source
|
[[ ! -f "$btrfs_path/$uuid"/img.source ]] && echo "$1" > "$btrfs_path/$uuid"/img.source
|
||||||
echo "Created: $uuid"
|
echo "Created: $uuid"
|
||||||
else
|
else
|
||||||
echo "No directory named '$1' exists"
|
echo "No directory named '$1' exists"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
function bocker_pull() { #HELP Pull an image from Docker Hub:\nBOCKER pull <name> <tag>
|
function bocker_pull() { #HELP Pull an image from Docker Hub:\nBOCKER pull <name> <tag>
|
||||||
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 }')"
|
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'
|
registry='https://registry-1.docker.io/v1'
|
||||||
id="$(curl -sL -H "Authorization: Token $token" "$registry/repositories/$1/tags/$2" | sed 's/"//g')"
|
id="$(curl -sL -H "Authorization: Token $token" "$registry/repositories/$1/tags/$2" | sed 's/"//g')"
|
||||||
[[ "${#id}" -ne 64 ]] && echo "No image named '$1:$2' exists" && exit 1
|
[[ "${#id}" -ne 64 ]] && echo "No image named '$1:$2' exists" && exit 1
|
||||||
ancestry="$(curl -sL -H "Authorization: Token $token" "$registry/images/$id/ancestry")"
|
ancestry="$(curl -sL -H "Authorization: Token $token" "$registry/images/$id/ancestry")"
|
||||||
IFS=',' && ancestry=(${ancestry//[\[\] \"]/}) && unset IFS; tmp_uuid="$(uuidgen)" && mkdir /tmp/"$tmp_uuid"
|
IFS=',' && ancestry=(${ancestry//[\[\] \"]/}) && unset IFS; tmp_uuid="$(uuidgen)" && mkdir /tmp/"$tmp_uuid"
|
||||||
for id in "${ancestry[@]}"; do
|
for id in "${ancestry[@]}"; do
|
||||||
curl -#L -H "Authorization: Token $token" "$registry/images/$id/layer" -o /tmp/"$tmp_uuid"/layer.tar
|
curl -#L -H "Authorization: Token $token" "$registry/images/$id/layer" -o /tmp/"$tmp_uuid"/layer.tar
|
||||||
tar xf /tmp/"$tmp_uuid"/layer.tar -C /tmp/"$tmp_uuid" && rm /tmp/"$tmp_uuid"/layer.tar
|
tar xf /tmp/"$tmp_uuid"/layer.tar -C /tmp/"$tmp_uuid" && rm /tmp/"$tmp_uuid"/layer.tar
|
||||||
done
|
done
|
||||||
echo "$1:$2" > /tmp/"$tmp_uuid"/img.source
|
echo "$1:$2" > /tmp/"$tmp_uuid"/img.source
|
||||||
bocker_init /tmp/"$tmp_uuid" && rm -rf /tmp/"$tmp_uuid"
|
bocker_init /tmp/"$tmp_uuid" && rm -rf /tmp/"$tmp_uuid"
|
||||||
}
|
}
|
||||||
function bocker_rm() { #HELP Delete an image or container:\nBOCKER rm <image_id or container_id>
|
function bocker_rm() { #HELP Delete an image or container:\nBOCKER rm <image_id or container_id>
|
||||||
[[ "$(bocker_check "$1")" == 1 ]] && echo "No container named '$1' exists" && exit 1
|
[[ "$(bocker_check "$1")" == 1 ]] && echo "No container named '$1' exists" && exit 1
|
||||||
btrfs subvolume delete "$btrfs_path/$1" > /dev/null
|
btrfs subvolume delete "$btrfs_path/$1" > /dev/null
|
||||||
echo "Removed: $1"
|
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"
|
echo -e "IMAGE_ID\t\tSOURCE"
|
||||||
for img in "$btrfs_path"/img_*; do
|
for img in "$btrfs_path"/img_*; do
|
||||||
img=$(basename "$img")
|
img=$(basename "$img")
|
||||||
echo -e "$img\t\t$(cat "$btrfs_path/$img/img.source")"
|
echo -e "$img\t\t$(cat "$btrfs_path/$img/img.source")"
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
function bocker_ps() { #HELP List containers:\nBOCKER ps
|
function bocker_ps() { #HELP List containers:\nBOCKER ps
|
||||||
echo -e "CONTAINER_ID\t\tCOMMAND"
|
echo -e "CONTAINER_ID\t\tCOMMAND"
|
||||||
for ps in "$btrfs_path"/ps_*; do
|
for ps in "$btrfs_path"/ps_*; do
|
||||||
ps=$(basename "$ps")
|
ps=$(basename "$ps")
|
||||||
echo -e "$ps\t\t$(cat "$btrfs_path/$ps/$ps.cmd")"
|
echo -e "$ps\t\t$(cat "$btrfs_path/$ps/$ps.cmd")"
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
function bocker_run() { #HELP Create a container:\nBOCKER run <image_id> <command>
|
function bocker_run() { #HELP Create a container:\nBOCKER run <image_id> <command>
|
||||||
uuid="ps_$(shuf -i 42002-42254 -n 1)"
|
uuid="ps_$(shuf -i 42002-42254 -n 1)"
|
||||||
[[ "$(bocker_check "$1")" == 1 ]] && echo "No image named '$1' exists" && exit 1
|
[[ "$(bocker_check "$1")" == 1 ]] && echo "No image named '$1' exists" && exit 1
|
||||||
[[ "$(bocker_check "$uuid")" == 0 ]] && echo "UUID conflict, retrying..." && bocker_run "$@" && return
|
[[ "$(bocker_check "$uuid")" == 0 ]] && echo "UUID conflict, retrying..." && bocker_run "$@" && return
|
||||||
cmd=${@:2} && ip="$(echo "${uuid: -3}" | sed 's/0//g')" && mac="${uuid: -3:1}:${uuid: -2}"
|
cmd=${@:2} && ip="$(echo "${uuid: -3}" | sed 's/0//g')" && mac="${uuid: -3:1}:${uuid: -2}"
|
||||||
ip link add dev veth0_"$uuid" type veth peer name veth1_"$uuid"
|
ip link add dev veth0_"$uuid" type veth peer name veth1_"$uuid"
|
||||||
ip link set dev veth0_"$uuid" up
|
ip link set dev veth0_"$uuid" up
|
||||||
ip link set veth0_"$uuid" master bridge0
|
ip link set veth0_"$uuid" master bridge0
|
||||||
ip netns add netns_"$uuid"
|
ip netns add netns_"$uuid"
|
||||||
ip link set veth1_"$uuid" netns 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 link set dev lo up
|
||||||
ip netns exec netns_"$uuid" ip link set veth1_"$uuid" address 02:42:ac:11:00"$mac"
|
ip netns exec netns_"$uuid" ip link set veth1_"$uuid" address 02:42:ac:11:00"$mac"
|
||||||
ip netns exec netns_"$uuid" ip addr add 10.0.0."$ip"/24 dev veth1_"$uuid"
|
ip netns exec netns_"$uuid" ip addr add 10.0.0."$ip"/24 dev veth1_"$uuid"
|
||||||
ip netns exec netns_"$uuid" ip link set dev veth1_"$uuid" up
|
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
|
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
|
btrfs subvolume snapshot "$btrfs_path/$1" "$btrfs_path/$uuid" > /dev/null
|
||||||
echo 'nameserver 8.8.8.8' > "$btrfs_path/$uuid"/etc/resolv.conf
|
echo 'nameserver 8.8.8.8' > "$btrfs_path/$uuid"/etc/resolv.conf
|
||||||
echo "$cmd" > "$btrfs_path/$uuid/$uuid.cmd"
|
echo "$cmd" > "$btrfs_path/$uuid/$uuid.cmd"
|
||||||
ip netns exec netns_"$uuid" "unshare" -fmuip --mount-proc "chroot" \
|
ip netns exec netns_"$uuid" "unshare" -fmuip --mount-proc "chroot" \
|
||||||
"$btrfs_path/$uuid" /bin/sh -c "/bin/mount -t proc proc /proc && $cmd" \
|
"$btrfs_path/$uuid" /bin/sh -c "/bin/mount -t proc proc /proc && $cmd" \
|
||||||
2>&1 | tee "$btrfs_path/$uuid/$uuid.log" || true
|
2>&1 | tee "$btrfs_path/$uuid/$uuid.log" || true
|
||||||
ip link del dev veth0_"$uuid"
|
ip link del dev veth0_"$uuid"
|
||||||
ip netns del netns_"$uuid"
|
ip netns del netns_"$uuid"
|
||||||
}
|
}
|
||||||
function bocker_exec() { #HELP Execute a command in an existing container:\nBOCKER exec <container_id> <command>
|
function bocker_exec() { #HELP Execute a command in an existing container:\nBOCKER exec <container_id> <command>
|
||||||
[[ "$(bocker_check "$1")" == 1 ]] && echo "No container named '$1' exists" && exit 1
|
[[ "$(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 }')
|
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" -muinp chroot "$btrfs_path/$1" "$cmd"
|
nsenter -t "$cid" -muinp chroot "$btrfs_path/$1" "$cmd"
|
||||||
}
|
}
|
||||||
function bocker_logs() { #HELP View logs from a container:\nBOCKER logs <container_id>
|
function bocker_logs() { #HELP View logs from a container:\nBOCKER logs <container_id>
|
||||||
[[ "$(bocker_check "$1")" == 1 ]] && echo "No container named '$1' exists" && exit 1
|
[[ "$(bocker_check "$1")" == 1 ]] && echo "No container named '$1' exists" && exit 1
|
||||||
cat "$btrfs_path/$1/$1.log"
|
cat "$btrfs_path/$1/$1.log"
|
||||||
}
|
}
|
||||||
function bocker_commit() { #HELP Commit a container to an image:\nBOCKER commit <container_id> <image_id>
|
function bocker_commit() { #HELP Commit a container to an image:\nBOCKER commit <container_id> <image_id>
|
||||||
[[ "$(bocker_check "$1")" == 1 ]] && echo "No container named '$1' exists" && exit 1
|
[[ "$(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_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
|
bocker_rm "$2" && btrfs subvolume snapshot "$btrfs_path/$1" "$btrfs_path/$2" > /dev/null
|
||||||
echo "Created: $2"
|
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"
|
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
|
case $1 in
|
||||||
|
|||||||
@ -2,14 +2,14 @@
|
|||||||
set -o errexit -o nounset -o pipefail
|
set -o errexit -o nounset -o pipefail
|
||||||
|
|
||||||
function bocker_run_test() {
|
function bocker_run_test() {
|
||||||
./bocker run "$1" "$2" > /dev/null
|
./bocker run "$1" "$2" > /dev/null
|
||||||
ps="$(./bocker ps | grep "$2" | awk '{print $1}')"
|
ps="$(./bocker ps | grep "$2" | awk '{print $1}')"
|
||||||
logs="$(./bocker logs "$ps")"
|
logs="$(./bocker logs "$ps")"
|
||||||
if [[ "$logs" == *"$3"* ]]; then
|
if [[ "$logs" == *"$3"* ]]; then
|
||||||
echo 0
|
echo 0
|
||||||
else
|
else
|
||||||
echo 1
|
echo 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
img="$(./bocker init ~/base-image | awk '{print $2}')"
|
img="$(./bocker init ~/base-image | awk '{print $2}')"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user