Compare commits

...

55 Commits
v0.1 ... master

Author SHA1 Message Date
Peter Wilmott
000633061c Remove base-image requirement 2015-08-12 19:01:32 +01:00
Peter Wilmott
22dc3c5b19 Update README.md 2015-08-06 18:24:11 +01:00
Peter Wilmott
b6653f6962 Remove unneeded whitespace 2015-08-05 20:23:14 +01:00
Peter Wilmott
3740778537 Merge pull request #21 from Fusion/master
Renamed cmdline args to BOCKER_ prefix
2015-08-05 20:10:30 +01:00
Peter Wilmott
7911226036 Add input checks to exec; Make exec's grep more robust; Added tests for exec 2015-08-05 20:09:25 +01:00
Peter Wilmott
25029b4f07 Merge pull request #22 from NonerKao/master
Fix a typo in function bocker_commit
2015-08-05 19:11:27 +01:00
Quey-Liang Kao
abb635a24c Fix a typo in function bocker_commit
In the first two line of bocker_commit(),
the program checks if the arguments are valid,
but the check of image does not return a correct message.
2015-08-06 01:44:19 +08:00
Chris F Ravenscroft
747c8b251f Renamed cmdline args to BOCKER_ prefix 2015-08-03 14:18:53 -07:00
Peter Wilmott
4956a537b7 Correctly reset IFS after changing 2015-08-03 18:27:29 +00:00
Peter Wilmott
23fdb390a6 Update README.md 2015-08-03 19:23:21 +01:00
Peter Wilmott
c4c748ded7 Update README.md 2015-08-03 19:23:02 +01:00
Peter Wilmott
74c2b197c7 Allow the setting of cgroup limits using env variables; Allocate CPU resources via shares not cores 2015-08-03 18:22:07 +00:00
Peter Wilmott
df8c4065c7 Merge pull request #19 from ivuk/fix-typo-readme
Small fixes for README.md
2015-08-03 19:16:10 +01:00
Igor Vuk
e7dd210844 Small fixes for README.md 2015-08-03 20:04:28 +02:00
Peter Wilmott
f33b31b3d6 Make each isolation level in bocker_run more clear 2015-07-31 21:59:07 +00:00
Peter Wilmott
1f48b32c42 Cleaned up cgroup implementation
Set default limits to 1 core and 512MB, CGX has also been removed to try
and make it more clear what is calling what.
2015-07-31 19:35:42 +00:00
Peter Wilmott
bc1b4719b5 Bocker is now written in 'around 100 lines of bash'
The 100 lines limit was a fun idea but has started to hurt
development. There will still be a focus on keeping it short and simple,
but no hard limit imposed
2015-07-31 18:12:59 +00:00
Peter Wilmott
11129aecc0 Separate nsenter arguments
For some reason it doesn't like -muinp, so you need to use -m -u -i -n
-p instead.
2015-07-31 16:55:07 +00:00
Peter Wilmott
25efd9497d Added libcgroup-tools to the list of packages to install 2015-07-31 16:17:06 +00:00
Peter Wilmott
8505276c60 Update README.md 2015-07-27 18:51:59 +00:00
Peter Wilmott
6f8e5e9cb9 Merge branch 'Fusion-master' 2015-07-27 18:50:29 +00:00
Peter Wilmott
baf937abdd Merge branch 'master' of https://github.com/Fusion/bocker into Fusion-master 2015-07-27 18:44:16 +00:00
Peter Wilmott
afcbd1bd06 Open functions on the same line that the previous function closes 2015-07-27 19:18:43 +01:00
Peter Wilmott
1b5f10b8ab Merge branch 'wking-indent-function-bodies' 2015-07-27 19:09:33 +01:00
Peter Wilmott
7bd579c349 Indent function bodies 2015-07-27 19:09:20 +01:00
Peter Wilmott
b240a47ac8 Implement 'bocker pull' for Docker Hub integration 2015-07-27 19:04:14 +01:00
Peter Wilmott
d5b38393c2 Small style changes 2015-07-27 17:51:08 +01:00
Peter Wilmott
7c85abd1b4 Merge branch 'Fusion-master' 2015-07-27 17:31:08 +01:00
Chris F Ravenscroft
c64c575abb Now with cgroup support 2015-07-26 17:05:06 -07:00
Peter Wilmott
5d48897ffa Merge branch 'master' of https://github.com/Fusion/bocker into Fusion-master 2015-07-26 22:15:18 +01:00
Chris F Ravenscroft
d4b9247f14 Execute commands in a container 2015-07-25 23:37:19 -07:00
W. Trevor King
871a851ffc Indent function bodies
This makes it a bit easier to read.
2015-07-25 21:16:04 -07:00
Peter Wilmott
caf4a38b9a Save 4 lines, thanks @SkUrRiEr 2015-07-24 16:46:25 +00:00
Peter Wilmott
036d2b6ffc Merge pull request #11 from pixelb/patch-1
mention coreutils as a prerequisite
2015-07-24 09:28:22 +01:00
Pádraig Brady
61f8ed7a49 mention coreutils as a prerequisite
cp --reflink=auto was added in coreutils 7.5
2015-07-24 00:13:25 +01:00
Peter Wilmott
0b5dc8b338 #3 : Deterministically generate IP and MAC addresses from the container IDs 2015-07-24 00:01:42 +01:00
Peter Wilmott
aa13b05e63 Merge branch 'master' of github.com:p8952/bocker 2015-07-23 18:16:36 +01:00
Peter Wilmott
5d9c745eef Merge branch 'SkUrRiEr-master' 2015-07-23 18:14:31 +01:00
Peter Wilmott
5246b814a3 Resolve merge conflicts; remove 80 char width limit for now 2015-07-23 18:14:08 +01:00
Peter Wilmott
b6678d7a2b Merge pull request #8 from alexandregz/typo-README
typo
2015-07-23 18:06:36 +01:00
Peter Wilmott
aad843d8d2 Merge branch 'TomiBelan-master' 2015-07-23 18:00:54 +01:00
Peter Wilmott
e84b1f768c Merge branch 'master' of https://github.com/TomiBelan/bocker into TomiBelan-master 2015-07-23 17:58:15 +01:00
Peter Wilmott
6e6ee2d3b2 Improve test coverage 2015-07-23 17:56:51 +01:00
Alexandre Espinosa Menor
a9f3039c59 typo 2015-07-23 17:02:19 +02:00
Tomi Belan
c3efade5c4 Shorter case statement 2015-07-23 16:56:27 +02:00
Julian Calaby
3eaea7e44c Trick sed into always ignoring slashes in $0 2015-07-23 09:48:52 +10:00
Julian Calaby
103182ad40 Split sed script to only format help comments 2015-07-23 09:48:30 +10:00
Julian Calaby
d229161326 Move exit call out of HELP()
This saves another line as it's only needed in one place.
2015-07-23 09:32:48 +10:00
Julian Calaby
3bf68db6a4 Use the actual executable path in the help messages
And it doesn't cost any lines
2015-07-23 09:32:48 +10:00
Julian Calaby
dbbeaf8732 Document "commit" and "help" commands 2015-07-23 09:32:48 +10:00
Julian Calaby
7edff781c1 Reduce lines required for command documentation 2015-07-23 09:32:48 +10:00
Peter Wilmott
39ebf616c4 Put all test in their own directory; Add a script to run all tests 2015-07-22 19:06:58 +01:00
Peter Wilmott
222b630588 Merge branch 'jbayer-add-test' 2015-07-22 18:38:57 +01:00
Jonathan Dowland
5624b48038 remove a useless use of cat and or logic 2015-07-22 10:37:27 +01:00
James Bayer
2232bb67ae add important test 2015-07-21 20:37:45 -07:00
14 changed files with 298 additions and 120 deletions

2
.gitignore vendored
View File

@ -1,3 +1 @@
.vagrant/ .vagrant/
base-image/
util-linux/

View File

@ -1,5 +1,5 @@
# Bocker # Bocker
Docker implemented in 100 lines of bash. Docker implemented in around 100 lines of bash.
* [Prerequisites](#prerequisites) * [Prerequisites](#prerequisites)
* [Example Usage](#example-usage) * [Example Usage](#example-usage)
@ -12,51 +12,56 @@ Docker implemented in 100 lines of bash.
The following packages are needed to run bocker. The following packages are needed to run bocker.
* btrfs-progs * btrfs-progs
* curl
* iproute2 * iproute2
* iptables * iptables
* libcgroup-tools
* util-linux >= 2.25.2 * util-linux >= 2.25.2
* coreutils >= 7.5
Because most distributions do not ship a new enough version of util-linux you will probably need grab the sources from [here](https://www.kernel.org/pub/linux/utils/util-linux/v2.25/) and compile it yourself. Because most distributions do not ship a new enough version of util-linux you will probably need to grab the sources from [here](https://www.kernel.org/pub/linux/utils/util-linux/v2.25/) and compile it yourself.
Additionally your system will need to be configured with the following. Additionally your system will need to be configured with the following:
* A btrfs filesystem mounted under `/var/bocker` * A btrfs filesystem mounted under `/var/bocker`
* A network bridge called `bridge0` and an IP of 10.0.0.1/24 * A network bridge called `bridge0` and an IP of 10.0.0.1/24
* IP forwarding enabled in `/proc/sys/net/ipv4/ip_forward` * IP forwarding enabled in `/proc/sys/net/ipv4/ip_forward`
* A firewall routing traffic from `bridge0` to a physical interface. * A firewall routing traffic from `bridge0` to a physical interface.
* A `base-image` which contains the filesystem to seed your container with.
For ease of use a Vagrantfile is included which will build the needed environment. For ease of use a Vagrantfile is included which will build the needed environment.
Even if you meet the above prerequisites you probably still want to **run bocker in a virtual machine**. Bocker runs as root and among other things needs to make changes to your network interfaces, routing table, and firewall rules. **I can make no guarantees that it wont trash your system**. Even if you meet the above prerequisites you probably still want to **run bocker in a virtual machine**. Bocker runs as root and among other things needs to make changes to your network interfaces, routing table, and firewall rules. **I can make no guarantees that it won't trash your system**.
## Example Usage ## Example Usage
``` ```
$ bocker init base-image/ $ bocker pull centos 7
Created: img_84632 ######################################################################## 100.0%
######################################################################## 100.0%
######################################################################## 100.0%
Created: img_42150
$ bocker images $ bocker images
IMAGE_ID IMAGE_ID SOURCE
img_84632 img_42150 centos:7
$ bocker run img_84632 uname -sro $ bocker run img_42150 cat /etc/centos-release
Linux 3.10.0-123.20.1.el7.x86_64 GNU/Linux CentOS Linux release 7.1.1503 (Core)
$ bocker ps $ bocker ps
CONTAINER_ID COMMAND CONTAINER_ID COMMAND
ps_12277 uname -sro ps_42045 cat /etc/centos-release
$ bocker logs ps_12277 $ bocker logs ps_42045
Linux 3.10.0-123.20.1.el7.x86_64 GNU/Linux CentOS Linux release 7.1.1503 (Core)
$ bocker rm ps_12277 $ bocker rm ps_42045
Removed: ps_12277 Removed: ps_42045
$ bocker run img_84632 which wget $ bocker run img_42150 which wget
which: no wget in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin) which: no wget in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
$ bocker run img_84632 yum install -y wget $ bocker run img_42150 yum install -y wget
Installing : wget-1.14-10.el7_0.1.x86_64 1/1 Installing : wget-1.14-10.el7_0.1.x86_64 1/1
Verifying : wget-1.14-10.el7_0.1.x86_64 1/1 Verifying : wget-1.14-10.el7_0.1.x86_64 1/1
Installed : wget.x86_64 0:1.14-10.el7_0.1 Installed : wget.x86_64 0:1.14-10.el7_0.1
@ -64,47 +69,62 @@ Complete!
$ bocker ps $ bocker ps
CONTAINER_ID COMMAND CONTAINER_ID COMMAND
ps_14099 which wget ps_42018 yum install -y wget
ps_43377 yum install -y wget ps_42182 which wget
$bocker commit ps_43377 img_84632 $ bocker commit ps_42018 img_42150
Removed: img_84632 Removed: img_42150
Created: img_84632 Created: img_42150
$ bocker run img_84632 which wget $ bocker run img_42150 which wget
/usr/bin/wget /usr/bin/wget
$ bocker rm ps_14099 $ bocker run img_42150 cat /proc/1/cgroup
Removed: ps_14099 ...
4:memory:/ps_42152
3:cpuacct,cpu:/ps_42152
$ bocker rm ps_43377 $ cat /sys/fs/cgroup/cpu/ps_42152/cpu.shares
Removed: ps_43377 512
$ bocker rm ps_95942 $ cat /sys/fs/cgroup/memory/ps_42152/memory.limit_in_bytes
Removed: ps_95942 512000000
$ bocker rm img_84632 $ BOCKER_CPU_SHARE=1024 \
Removed: img_84632 BOCKER_MEM_LIMIT=1024 \
bocker run img_42150 cat /proc/1/cgroup
...
4:memory:/ps_42188
3:cpuacct,cpu:/ps_42188
$ cat /sys/fs/cgroup/cpu/ps_42188/cpu.shares
1024
$ cat /sys/fs/cgroup/memory/ps_42188/memory.limit_in_bytes
1024000000
``` ```
## Functionality: Currently Implemented ## Functionality: Currently Implemented
* `docker build` * `docker build`
* `docker pull`
* `docker images` * `docker images`
* `docker ps` * `docker ps`
* `docker run` * `docker run`
* `docker exec`
* `docker logs` * `docker logs`
* `docker commit` * `docker commit`
* `docker rm` / `docker rmi` * `docker rm` / `docker rmi`
* Networking * Networking
* Quota Support / CGroups
`bocker init` provides a very limited implemetation of `docker build` `bocker init` provides a very limited implementation of `docker build`
## Functionality: Not Yet Implemented ## Functionality: Not Yet Implemented
* Port Forwarding
* Data Volumes
* Data Volume Containers * Data Volume Containers
* Data Volumes
* Port Forwarding
## License ## License

2
Vagrantfile vendored
View File

@ -1,7 +1,7 @@
$script = <<SCRIPT $script = <<SCRIPT
( (
rpm -i https://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm rpm -i https://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
yum install -y -q autoconf automake btrfs-progs docker gettext-devel git libtool python-pip yum install -y -q autoconf automake btrfs-progs docker gettext-devel git libcgroup-tools libtool python-pip
fallocate -l 10G ~/btrfs.img fallocate -l 10G ~/btrfs.img
mkdir /var/bocker mkdir /var/bocker

187
bocker
View File

@ -1,96 +1,119 @@
#!/usr/bin/env bash #!/usr/bin/env bash
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' && cgroups='cpu,cpuacct,memory';
function CHECK() { [[ $# -gt 0 ]] && while [ "${1:0:2}" == '--' ]; do OPTION=${1:2}; [[ $OPTION =~ = ]] && declare "BOCKER_${OPTION/=*/}=${OPTION/*=/}" || declare "BOCKER_${OPTION}=x"; shift; done
[[ "$1" == 'img' ]] && TYPE='image'
[[ "$1" == 'ps' ]] && TYPE='container' function bocker_check() {
[[ "$1" == '' ]] && TYPE='container or image' btrfs subvolume list "$btrfs_path" | grep -qw "$1" && echo 0 || echo 1
if [[ "$2" == "$1"* ]]; then }
if btrfs subvolume list "$btrfs_path" | grep -qw "$2"; then
return 0 function bocker_init() { #HELP Create an image from a directory:\nBOCKER init <directory>
uuid="img_$(shuf -i 42002-42254 -n 1)"
if [[ -d "$1" ]]; then
[[ "$(bocker_check "$uuid")" == 0 ]] && bocker_run "$@"
btrfs subvolume create "$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
echo "Created: $uuid"
else
echo "No directory named '$1' exists"
fi fi
fi
echo "No $TYPE named '$2' exists" && exit 1
} }
function INIT() {
if [[ -d "$1" ]]; then function bocker_pull() { #HELP Pull an image from Docker Hub:\nBOCKER pull <name> <tag>
uuid="img_$(shuf -i 10000-99999 -n 1)" 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 }')"
btrfs subvolume create "$btrfs_path/$uuid" > /dev/null registry='https://registry-1.docker.io/v1'
cp -rf --reflink=auto "$1"/* "$btrfs_path/$uuid" > /dev/null id="$(curl -sL -H "Authorization: Token $token" "$registry/repositories/$1/tags/$2" | sed 's/"//g')"
echo "Created: $uuid" [[ "${#id}" -ne 64 ]] && echo "No image named '$1:$2' exists" && exit 1
else ancestry="$(curl -sL -H "Authorization: Token $token" "$registry/images/$id/ancestry")"
echo "No directory named '$1' exists" IFS=',' && ancestry=(${ancestry//[\[\] \"]/}) && IFS=' \n\t'; tmp_uuid="$(uuidgen)" && mkdir /tmp/"$tmp_uuid"
fi for id in "${ancestry[@]}"; do
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
done
echo "$1:$2" > /tmp/"$tmp_uuid"/img.source
bocker_init /tmp/"$tmp_uuid" && rm -rf /tmp/"$tmp_uuid"
} }
function RM() {
CHECK '' "$1" function bocker_rm() { #HELP Delete an image or container:\nBOCKER rm <image_id or container_id>
btrfs subvolume delete "$btrfs_path/$1" > /dev/null [[ "$(bocker_check "$1")" == 1 ]] && echo "No container named '$1' exists" && exit 1
echo "Removed: $1" btrfs subvolume delete "$btrfs_path/$1" > /dev/null
cgdelete -g "$cgroups:/$1" &> /dev/null || true
echo "Removed: $1"
} }
function IMAGES() {
echo -e "IMAGE_ID" function bocker_images() { #HELP List images:\nBOCKER images
for img in "$btrfs_path"/img_*; do echo -e "IMAGE_ID\t\tSOURCE"
basename "$img" for img in "$btrfs_path"/img_*; do
done img=$(basename "$img")
echo -e "$img\t\t$(cat "$btrfs_path/$img/img.source")"
done
} }
function PS() {
echo -e "CONTAINER_ID\t\tCOMMAND" function bocker_ps() { #HELP List containers:\nBOCKER ps
for ps in "$btrfs_path"/ps_*; do echo -e "CONTAINER_ID\t\tCOMMAND"
ps=$(basename "$ps") for ps in "$btrfs_path"/ps_*; do
echo -e "$ps\t\t$(cat "$btrfs_path/$ps/$ps.cmd")" ps=$(basename "$ps")
done echo -e "$ps\t\t$(cat "$btrfs_path/$ps/$ps.cmd")"
done
} }
function RUN() {
CHECK 'img' "$1" function bocker_run() { #HELP Create a container:\nBOCKER run <image_id> <command>
uuid="ps_$(shuf -i 10000-99999 -n 1)" uuid="ps_$(shuf -i 42002-42254 -n 1)"
ip link add dev veth0_"$uuid" type veth peer name veth1_"$uuid" [[ "$(bocker_check "$1")" == 1 ]] && echo "No image named '$1' exists" && exit 1
ip link set dev veth0_"$uuid" up [[ "$(bocker_check "$uuid")" == 0 ]] && echo "UUID conflict, retrying..." && bocker_run "$@" && return
ip link set veth0_"$uuid" master bridge0 cmd="${@:2}" && ip="$(echo "${uuid: -3}" | sed 's/0//g')" && mac="${uuid: -3:1}:${uuid: -2}"
ip netns add netns_"$uuid" ip link add dev veth0_"$uuid" type veth peer name veth1_"$uuid"
ip link set veth1_"$uuid" netns netns_"$uuid" ip link set dev veth0_"$uuid" up
ip netns exec netns_"$uuid" ip link set dev lo up ip link set veth0_"$uuid" master bridge0
ip netns exec netns_"$uuid" ip addr add 10.0.0.2/24 dev veth1_"$uuid" ip netns add netns_"$uuid"
ip netns exec netns_"$uuid" ip link set dev veth1_"$uuid" up ip link set veth1_"$uuid" netns netns_"$uuid"
ip netns exec netns_"$uuid" ip route add default via 10.0.0.1 ip netns exec netns_"$uuid" ip link set dev lo up
btrfs subvolume snapshot "$btrfs_path/$1" "$btrfs_path/$uuid" > /dev/null ip netns exec netns_"$uuid" ip link set veth1_"$uuid" address 02:42:ac:11:00"$mac"
echo 'nameserver 8.8.8.8' > "$btrfs_path/$uuid"/etc/resolv.conf ip netns exec netns_"$uuid" ip addr add 10.0.0."$ip"/24 dev veth1_"$uuid"
echo "$2" > "$btrfs_path/$uuid/$uuid.cmd" ip netns exec netns_"$uuid" ip link set dev veth1_"$uuid" up
ip netns exec netns_"$uuid" "unshare" -fp --mount-proc "chroot" \ ip netns exec netns_"$uuid" ip route add default via 10.0.0.1
"$btrfs_path/$uuid" /bin/sh -c "/bin/mount -t proc proc /proc && $2" \ btrfs subvolume snapshot "$btrfs_path/$1" "$btrfs_path/$uuid" > /dev/null
2>&1 | tee "$btrfs_path/$uuid/$uuid.log" || true echo 'nameserver 8.8.8.8' > "$btrfs_path/$uuid"/etc/resolv.conf
ip link del dev veth0_"$uuid" echo "$cmd" > "$btrfs_path/$uuid/$uuid.cmd"
ip netns del netns_"$uuid" cgcreate -g "$cgroups:/$uuid"
: "${BOCKER_CPU_SHARE:=512}" && cgset -r cpu.shares="$BOCKER_CPU_SHARE" "$uuid"
: "${BOCKER_MEM_LIMIT:=512}" && cgset -r memory.limit_in_bytes="$((BOCKER_MEM_LIMIT * 1000000))" "$uuid"
cgexec -g "$cgroups:$uuid" \
ip netns exec netns_"$uuid" \
unshare -fmuip --mount-proc \
chroot "$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 Execute a command in a running container:\nBOCKER exec <container_id> <command>
cat "$btrfs_path/$1/$1.log" [[ "$(bocker_check "$1")" == 1 ]] && echo "No container named '$1' exists" && exit 1
cid="$(ps o ppid,pid | grep "^$(ps o pid,cmd | grep -E "^\ *[0-9]+ unshare.*$1" | awk '{print $1}')" | awk '{print $2}')"
[[ ! "$cid" =~ ^\ *[0-9]+$ ]] && echo "Container '$1' exists but is not running" && exit 1
nsenter -t "$cid" -m -u -i -n -p chroot "$btrfs_path/$1" "${@:2}"
} }
function COMMIT() {
CHECK 'ps' "$1" && CHECK 'img' "$2" && RM "$2" function bocker_logs() { #HELP View logs from a container:\nBOCKER logs <container_id>
btrfs subvolume snapshot "$btrfs_path/$1" "$btrfs_path/$2" > /dev/null [[ "$(bocker_check "$1")" == 1 ]] && echo "No container named '$1' exists" && exit 1
echo "Created: $2" cat "$btrfs_path/$1/$1.log"
} }
function HELP() {
echo -e "Create an image: \n\t./bocker init <image_directory>\n" function bocker_commit() { #HELP Commit a container to an image:\nBOCKER commit <container_id> <image_id>
echo -e "List images: \n\t./bocker images\n" [[ "$(bocker_check "$1")" == 1 ]] && echo "No container named '$1' exists" && exit 1
echo -e "Create a container: \n\t./bocker run <image_id> <command>\n" [[ "$(bocker_check "$2")" == 1 ]] && echo "No image named '$2' exists" && exit 1
echo -e "List containers: \n\t./bocker ps\n" bocker_rm "$2" && btrfs subvolume snapshot "$btrfs_path/$1" "$btrfs_path/$2" > /dev/null
echo -e "View logs from a container: \n\t./bocker logs <container_id>\n" echo "Created: $2"
echo -e "Delete an image or container: \n\t./bocker rm <image_or_container_id>"
exit 0
} }
[[ -z "${1-}" ]] && 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"
case $1 in case $1 in
init) INIT "$2" ;; pull|init|rm|images|ps|run|exec|logs|commit) bocker_"$1" "${@:2}" ;;
rm) RM "$2" ;; *) bocker_help "$0" ;;
images) IMAGES ;;
ps) PS ;;
run)
IMAGE="$2"
shift && shift
RUN "$IMAGE" "$*"
;;
logs) LOGS "$2" ;;
commit) COMMIT "$2" "$3" ;;
*) HELP ;;
esac esac

15
test Executable file
View File

@ -0,0 +1,15 @@
#!/usr/bin/env bash
exit_code=0
for t in tests/test_*; do
bash tests/teardown > /dev/null 2>&1
bash "$t" > /dev/null 2>&1
if [[ $? == 0 ]]; then
echo -e "\e[1;32mPASSED\e[0m : $t"
else
echo -e "\e[1;31mFAILED\e[0m : $t"
exit_code=1
fi
bash tests/teardown > /dev/null 2>&1
done
exit "$exit_code"

10
tests/teardown Normal file
View File

@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
for img in $(./bocker images | grep 'img' | awk '{print $1}'); do
./bocker rm "$img"
done
for ps in $(./bocker ps | grep 'ps' | awk '{print $1}'); do
./bocker rm "$ps"
done

21
tests/test_commit Normal file
View File

@ -0,0 +1,21 @@
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
img="$(./bocker init ~/base-image | awk '{print $2}')"
./bocker images | grep -qw "$img"
[[ "$?" == 0 ]]
./bocker run "$img" which wget
ps="$(./bocker ps | grep 'which wget' | awk '{print $1}')"
logs="$(./bocker logs "$ps")"
./bocker rm "$ps"
[[ "$logs" == "which: no wget in"* ]]
./bocker run "$img" yum install -y wget
ps="$(./bocker ps | grep 'yum install -y wget' | awk '{print $1}')"
./bocker commit "$ps" "$img"
./bocker run "$img" which wget
ps="$(./bocker ps | grep 'which wget' | awk '{print $1}')"
logs="$(./bocker logs "$ps")"
[[ "$logs" == '/usr/bin/wget' ]]

23
tests/test_exec Normal file
View File

@ -0,0 +1,23 @@
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
img="$(./bocker init ~/base-image | awk '{print $2}')"
./bocker images | grep -qw "$img"
[[ "$?" == 0 ]]
# ▼ ▼ ▼ Race condition waiting to happen ▼ ▼ ▼
./bocker run "$img" "sleep 5 && ps aux" &
sleep 2
ps="$(./bocker ps | grep 'sleep 5' | awk '{print $1}')"
exec="$(./bocker exec "$ps" ps aux | wc -l)"
[[ "$exec" == "4" ]]
sleep 3
# ▲ ▲ ▲ Race condition waiting to happen ▲ ▲ ▲
./bocker run "$img" ps aux
ps="$(./bocker ps | grep 'ps aux' | awk '{print $1}')"
exec="$(./bocker exec "$ps" ps aux)" || true
[[ "$exec" == "Container '$ps' exists but is not running" ]]
exec="$(./bocker exec foo ps aux)" || true
[[ "$exec" == "No container named 'foo' exists" ]]

4
tests/test_images Normal file
View File

@ -0,0 +1,4 @@
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
[[ "$(./bocker images | head -n 1)" == 'IMAGE_ID SOURCE' ]]

4
tests/test_init Normal file
View File

@ -0,0 +1,4 @@
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
[[ "$(./bocker init ~/base-image)" == 'Created: img_'* ]]

4
tests/test_ps Normal file
View File

@ -0,0 +1,4 @@
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
[[ "$(./bocker ps | head -n 1)" == 'CONTAINER_ID COMMAND' ]]

16
tests/test_pull Normal file
View File

@ -0,0 +1,16 @@
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
centos_img="$(./bocker pull centos 7 2> /dev/null | awk '{print $2}')"
./bocker run "$centos_img" cat /etc/redhat-release
ps="$(./bocker ps | grep 'cat /etc/redhat-release' | awk '{print $1}')"
logs="$(./bocker logs "$ps")"
./bocker rm "$ps"
[[ "$logs" == "CentOS Linux release 7"* ]]
ubuntu_img="$(./bocker pull ubuntu 14.04 2> /dev/null | awk '{print $2}')"
./bocker run "$ubuntu_img" tail -n1 /etc/lsb-release
ps="$(./bocker ps | grep 'tail -n1 /etc/lsb-release' | awk '{print $1}')"
logs="$(./bocker logs "$ps")"
./bocker rm "$ps"
[[ "$logs" == *"Ubuntu 14.04"* ]]

16
tests/test_rm Normal file
View File

@ -0,0 +1,16 @@
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
img="$(./bocker init ~/base-image | awk '{print $2}')"
cmd="echo $RANDOM"
./bocker run "$img" "$cmd"
ps="$(./bocker ps | grep "$cmd" | awk '{print $1}')"
[[ "$(./bocker images | grep -c "$img")" == 1 ]]
[[ "$(./bocker ps | grep -c "$cmd")" == 1 ]]
./bocker rm "$img"
./bocker rm "$ps"
[[ "$(./bocker images | grep -c "$img")" == 0 ]]
[[ "$(./bocker ps | grep -c "$cmd")" == 0 ]]

24
tests/test_run Normal file
View File

@ -0,0 +1,24 @@
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
function bocker_run_test() {
./bocker run "$1" "$2" > /dev/null
ps="$(./bocker ps | grep "$2" | awk '{print $1}')"
logs="$(./bocker logs "$ps")"
if [[ "$logs" == *"$3"* ]]; then
echo 0
else
echo 1
fi
}
img="$(./bocker init ~/base-image | awk '{print $2}')"
./bocker images | grep -qw "$img"
[[ "$?" == 0 ]]
[[ "$(bocker_run_test "$img" 'echo foo' 'foo')" == 0 ]]
[[ "$(bocker_run_test "$img" 'uname' 'Linux')" == 0 ]]
[[ "$(bocker_run_test "$img" 'cat /proc/self/stat' '3 (cat)')" == 0 ]]
[[ "$(bocker_run_test "$img" 'ip addr' 'veth1_ps_')" == 0 ]]
[[ "$(bocker_run_test "$img" 'ping -c 1 8.8.8.8' '0% packet loss')" == 0 ]]
[[ "$(bocker_run_test "$img" 'ping -c 1 google.com' '0% packet loss')" == 0 ]]