Skip to content

Commit

Permalink
Merge pull request #38 from k-kinzal/add-docker-binary-donloader
Browse files Browse the repository at this point in the history
Add downloader for docker binary
  • Loading branch information
k-kinzal authored Jan 1, 2019
2 parents 943b7df + 5293f3f commit 0bddd00
Show file tree
Hide file tree
Showing 18 changed files with 134 additions and 20 deletions.
10 changes: 0 additions & 10 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,9 @@ jobs:
steps:
- attach_workspace:
at: ./
- run:
# FIXME: host: ubuntu, guest: alpine not working
# https://twitter.com/amaya382/status/1077874567119396864
# https://amaya382.hatenablog.jp/entry/2016/10/05/030254
name: Download docker binary
command: |
mkdir -p bin
docker run -v $PWD/bin:/tmp/bin docker /bin/sh -c 'cp $(which docker) /tmp/bin'
- run:
name: Integration Test
command: |
echo 'export PATH='$PWD'/bin:$PATH' >> $BASH_ENV
source $BASH_ENV
find test/integration/*/test.sh | xargs -I{} /bin/sh -c "{} || exit 255 && echo pass {}"
workflows:
Expand Down
3 changes: 2 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ run:

issues:
exclude:
- "`aliases.Schema` composite literal uses unkeyed fields"
- "`aliases.Schema` composite literal uses unkeyed fields"
- "`aliases.BinarySchema` composite literal uses unkeyed fields"
1 change: 1 addition & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (ctx *runContext) GetCommandShema() *aliases.Schema {
schema := aliases.Schema{
index,
path.Base(index),
aliases.BinarySchema{"", ""},
nil,
flags.bool("detach", "d"),
flags.bool("sig-proxy"),
Expand Down
68 changes: 68 additions & 0 deletions pkg/aliases/binary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package aliases

import (
"fmt"
"os"
"os/exec"
"path"
"strings"
)

type BinaryManager struct {
binaryDir string
}

func (manager *BinaryManager) getFilename(image string, tag string) string {
filename := fmt.Sprintf("%s:%s", image, tag)
filename = strings.Replace(filename, "/", "-", -1)
filename = strings.Replace(filename, ":", "-", -1)
filename = strings.Replace(filename, ".", "-", -1)
filename = strings.Replace(filename, "_", "-", -1)

return filename
}

func (manager *BinaryManager) exists(image string, tag string) bool {
filename := manager.getFilename(image, tag)
filepath := path.Join(manager.binaryDir, filename)

if _, err := os.Stat(filepath); os.IsNotExist(err) {
return false
}

return true
}

func (manager *BinaryManager) download(image string, tag string) error {
filename := manager.getFilename(image, tag)

cmd := exec.Command(
"docker",
"run",
"-v",
fmt.Sprintf("%s:%s", manager.binaryDir, "/data"),
fmt.Sprintf("%s:%s", image, tag),
"sh",
"-c",
fmt.Sprintf("cp $(which docker) /data/%s", filename),
)
cmd.Stderr = os.Stderr

if err := cmd.Run(); err != nil {
return err
}

return nil
}

func (manager *BinaryManager) Get(image string, tag string) (*string, error) {
if !manager.exists(image, tag) {
if err := manager.download(image, tag); err != nil {
return nil, fmt.Errorf("runtime error: %s", err)
}
}

filepath := path.Join(manager.binaryDir, manager.getFilename(image, tag))

return &filepath, nil
}
7 changes: 6 additions & 1 deletion pkg/aliases/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,12 @@ func NewCommand(ctx Context, schema Schema) (*posix.Cmd, error) {
cmd.Args = append(cmd.Args, "--volume", strconv.Quote(v))
}
if (len(schema.Dependencies) > 0) && ctx.HasDockerSocket() {
cmd.Args = append(cmd.Args, "--volume", strconv.Quote(fmt.Sprintf("%s:/usr/local/bin/docker", ctx.DockerBinaryPath())))
binary := BinaryManager{path.Join(ctx.HomePath(), "docker")}
binarypath, err := binary.Get(schema.Docker.Image, schema.Docker.Tag)
if err != nil {
return nil, err
}
cmd.Args = append(cmd.Args, "--volume", strconv.Quote(fmt.Sprintf("%s:/usr/local/bin/docker", *binarypath)))
if sock := ctx.DockerSocketPath(); sock != nil {
cmd.Args = append(cmd.Args, "--volume", strconv.Quote(fmt.Sprintf("%s:/var/run/docker.sock", *sock)))
}
Expand Down
8 changes: 7 additions & 1 deletion pkg/aliases/schema.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package aliases

type BinarySchema struct {
Image string `yaml:"image" validate:"required" default:"docker"`
Tag string `yaml:"tag" validate:"required" default:"18.09.0"`
}

type Schema struct {
// aliases configuration
Path string
FileName string
Dependencies []string `yaml:"dependencies"`
Docker BinarySchema `yaml:"docker"`
Dependencies []string `yaml:"dependencies"`
// docker run options
Detach *string `yaml:"detach" validate:"omitempty,bool|script"`
SigProxy *string `yaml:"sigProxy" validate:"omitempty,bool|script"`
Expand Down
2 changes: 1 addition & 1 deletion test/integration/dependencies/alias
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
alias alpine='docker run --env ALIASES_PWD="${ALIASES_PWD:-$PWD}" --interactive --network "host" --privileged --rm $(test "$(if tty >/dev/null; then echo true; else echo false; fi)" = "true" && echo "--tty") --volume "[DOCKER]:/usr/local/bin/docker" --volume "/var/run/docker.sock:/var/run/docker.sock" --volume "[TEMP_DIR]/kubectl:/usr/local/bin/kubectl" alpine:${ALPINE_VERSION:-"3.8"}'
alias alpine='docker run --env ALIASES_PWD="${ALIASES_PWD:-$PWD}" --interactive --network "host" --privileged --rm $(test "$(if tty >/dev/null; then echo true; else echo false; fi)" = "true" && echo "--tty") --volume "[HOME]/.aliases/docker/docker-18-09-0:/usr/local/bin/docker" --volume "/var/run/docker.sock:/var/run/docker.sock" --volume "[TEMP_DIR]/kubectl:/usr/local/bin/kubectl" alpine:${ALPINE_VERSION:-"3.8"}'
alias kubectl='docker run --interactive --network "host" --rm $(test "$(if tty >/dev/null; then echo true; else echo false; fi)" = "true" && echo "--tty") --volume "[HOME]/.kube:/root/.kube" --volume "${ALIASES_PWD:-$PWD}:/kube" --workdir "/kube" chatwork/kubectl:${KUBECTL_VERSION:-"1.11.2"}'
4 changes: 2 additions & 2 deletions test/integration/dependencies/alpine
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/bin/sh

if [ -p /dev/stdin ]; then
cat - | docker run --env ALIASES_PWD="${ALIASES_PWD:-$PWD}" --interactive --network "host" --privileged --rm $(test "$(if tty >/dev/null; then echo true; else echo false; fi)" = "true" && echo "--tty") --volume "[DOCKER]:/usr/local/bin/docker" --volume "/var/run/docker.sock:/var/run/docker.sock" --volume "[TEMP_DIR]/kubectl:/usr/local/bin/kubectl" alpine:${ALPINE_VERSION:-"3.8"} "$@"
cat - | docker run --env ALIASES_PWD="${ALIASES_PWD:-$PWD}" --interactive --network "host" --privileged --rm $(test "$(if tty >/dev/null; then echo true; else echo false; fi)" = "true" && echo "--tty") --volume "[HOME]/.aliases/docker/docker-18-09-0:/usr/local/bin/docker" --volume "/var/run/docker.sock:/var/run/docker.sock" --volume "[TEMP_DIR]/kubectl:/usr/local/bin/kubectl" alpine:${ALPINE_VERSION:-"3.8"} "$@"
exit $?
else
docker run --env ALIASES_PWD="${ALIASES_PWD:-$PWD}" --interactive --network "host" --privileged --rm $(test "$(if tty >/dev/null; then echo true; else echo false; fi)" = "true" && echo "--tty") --volume "[DOCKER]:/usr/local/bin/docker" --volume "/var/run/docker.sock:/var/run/docker.sock" --volume "[TEMP_DIR]/kubectl:/usr/local/bin/kubectl" alpine:${ALPINE_VERSION:-"3.8"} "$@"
docker run --env ALIASES_PWD="${ALIASES_PWD:-$PWD}" --interactive --network "host" --privileged --rm $(test "$(if tty >/dev/null; then echo true; else echo false; fi)" = "true" && echo "--tty") --volume "[HOME]/.aliases/docker/docker-18-09-0:/usr/local/bin/docker" --volume "/var/run/docker.sock:/var/run/docker.sock" --volume "[TEMP_DIR]/kubectl:/usr/local/bin/kubectl" alpine:${ALPINE_VERSION:-"3.8"} "$@"
exit $?
fi
2 changes: 1 addition & 1 deletion test/integration/dependencies/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ TEST_DIR="$(cd "$(dirname "${0}")"; echo "$(pwd)")"

ALIASES=$(cd "${TEST_DIR}/../../..//dist"; echo "$(pwd)/aliases -c ${TEST_DIR}/aliases.yaml")
DIFF=$(if which colordiff >/dev/null; then echo "colordiff -Buw --strip-trailing-cr"; else echo "diff -Bw"; fi)
MASK="sed -e s|$(which docker):|[DOCKER]:|g -e s|${HOME}|[HOME]|g -e s|${TEMP_DIR}|[TEMP_DIR]|g -e s|[0-9]*-[0-9]*-[0-9]*T[0-9]*:[0-9]*:[0-9]*Z|yyyy-mm-ddThh:MM:ssZ|g"
MASK="sed -e s|${HOME}|[HOME]|g -e s|${TEMP_DIR}|[TEMP_DIR]|g -e s|[0-9]*-[0-9]*-[0-9]*T[0-9]*:[0-9]*:[0-9]*Z|yyyy-mm-ddThh:MM:ssZ|g"

${ALIASES} gen --export-path "${TEMP_DIR}" | ${MASK} | sort | ${DIFF} ${TEST_DIR}/alias -
${ALIASES} gen --export --export-path "${TEMP_DIR}" | ${MASK} | ${DIFF} ${TEST_DIR}/export -
Expand Down
2 changes: 2 additions & 0 deletions test/integration/docker-binary/alias
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alias kubectl='docker run --interactive --network "host" --rm $(test "$(if tty >/dev/null; then echo true; else echo false; fi)" = "true" && echo "--tty") --volume "[HOME]/.kube:/root/.kube" --volume "${ALIASES_PWD:-$PWD}:/kube" --workdir "/kube" chatwork/kubectl:${KUBECTL_VERSION:-"1.11.2"}'
alias ubuntu='docker run --env ALIASES_PWD="${ALIASES_PWD:-$PWD}" --interactive --network "host" --privileged --rm $(test "$(if tty >/dev/null; then echo true; else echo false; fi)" = "true" && echo "--tty") --volume "[HOME]/.aliases/docker/docker-18-09-0:/usr/local/bin/docker" --volume "/var/run/docker.sock:/var/run/docker.sock" --volume "[TEMP_DIR]/kubectl:/usr/local/bin/kubectl" amd64/ubuntu:${UBUNTU_VERSION:-"19.04"}'
13 changes: 13 additions & 0 deletions test/integration/docker-binary/aliases.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/usr/local/bin/kubectl:
image: chatwork/kubectl
tag: 1.11.2
volume:
- $HOME/.kube:/root/.kube
- $PWD:/kube
workdir: /kube

/usr/local/bin/ubuntu:
image: amd64/ubuntu
tag: 19.04
dependencies:
- /usr/local/bin/kubectl
1 change: 1 addition & 0 deletions test/integration/docker-binary/export
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export PATH="[TEMP_DIR]:$PATH"
1 change: 1 addition & 0 deletions test/integration/docker-binary/stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Docker version 18.09.0, build 4d60db4
17 changes: 17 additions & 0 deletions test/integration/docker-binary/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh

set -eu

TEMP_DIR=$(mktemp -d)
TEST_DIR="$(cd "$(dirname "${0}")"; echo "$(pwd)")"

ALIASES=$(cd "${TEST_DIR}/../../..//dist"; echo "$(pwd)/aliases -c ${TEST_DIR}/aliases.yaml")
DIFF=$(if which colordiff >/dev/null; then echo "colordiff -Buw --strip-trailing-cr"; else echo "diff -Bw"; fi)
MASK="sed -e s|${HOME}|[HOME]|g -e s|${TEMP_DIR}|[TEMP_DIR]|g"

${ALIASES} gen --export-path "${TEMP_DIR}" | ${MASK} | sort | ${DIFF} ${TEST_DIR}/alias -
${ALIASES} gen --export --export-path "${TEMP_DIR}" | ${MASK} | ${DIFF} ${TEST_DIR}/export -
cat ${TEMP_DIR}/ubuntu | ${MASK} | ${DIFF} ${TEST_DIR}/ubuntu -

${TEMP_DIR}/ubuntu /bin/sh -c "docker -v" | ${DIFF} ${TEST_DIR}/stdout -
${ALIASES} run /usr/local/bin/ubuntu /bin/sh -c "docker -v" | ${DIFF} ${TEST_DIR}/stdout -
9 changes: 9 additions & 0 deletions test/integration/docker-binary/ubuntu
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh

if [ -p /dev/stdin ]; then
cat - | docker run --env ALIASES_PWD="${ALIASES_PWD:-$PWD}" --interactive --network "host" --privileged --rm $(test "$(if tty >/dev/null; then echo true; else echo false; fi)" = "true" && echo "--tty") --volume "[HOME]/.aliases/docker/docker-18-09-0:/usr/local/bin/docker" --volume "/var/run/docker.sock:/var/run/docker.sock" --volume "[TEMP_DIR]/kubectl:/usr/local/bin/kubectl" amd64/ubuntu:${UBUNTU_VERSION:-"19.04"} "$@"
exit $?
else
docker run --env ALIASES_PWD="${ALIASES_PWD:-$PWD}" --interactive --network "host" --privileged --rm $(test "$(if tty >/dev/null; then echo true; else echo false; fi)" = "true" && echo "--tty") --volume "[HOME]/.aliases/docker/docker-18-09-0:/usr/local/bin/docker" --volume "/var/run/docker.sock:/var/run/docker.sock" --volume "[TEMP_DIR]/kubectl:/usr/local/bin/kubectl" amd64/ubuntu:${UBUNTU_VERSION:-"19.04"} "$@"
exit $?
fi
2 changes: 1 addition & 1 deletion test/integration/expand-env/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ TEST_DIR="$(cd "$(dirname "${0}")"; echo "$(pwd)")"

ALIASES=$(cd "${TEST_DIR}/../../..//dist"; echo "$(pwd)/aliases -c ${TEST_DIR}/aliases.yaml")
DIFF=$(if which colordiff >/dev/null; then echo "colordiff -Buw --strip-trailing-cr"; else echo "diff -Bw"; fi)
MASK="sed -e s|$(which docker):|[DOCKER]:|g -e s|${HOME}|[HOME]|g -e s|${TEMP_DIR}|[TEMP_DIR]|g"
MASK="sed -e s|${HOME}|[HOME]|g -e s|${TEMP_DIR}|[TEMP_DIR]|g"

USER=root
DOCKER_MOUNT_TYPE=volume
Expand Down
2 changes: 1 addition & 1 deletion test/integration/pipe/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ TEST_DIR="$(cd "$(dirname "${0}")"; echo "$(pwd)")"

ALIASES=$(cd "${TEST_DIR}/../../..//dist"; echo "$(pwd)/aliases -c ${TEST_DIR}/aliases.yaml")
DIFF=$(if which colordiff >/dev/null; then echo "colordiff -Buw --strip-trailing-cr"; else echo "diff -Bw"; fi)
MASK="sed -e s|$(which docker):|[DOCKER]:|g -e s|${HOME}|[HOME]|g -e s|${TEMP_DIR}|[TEMP_DIR]|g"
MASK="sed -e s|${HOME}|[HOME]|g -e s|${TEMP_DIR}|[TEMP_DIR]|g"

${ALIASES} gen --export-path "${TEMP_DIR}" | ${MASK} | sort | ${DIFF} ${TEST_DIR}/alias -
${ALIASES} gen --export --export-path "${TEMP_DIR}" | ${MASK} | ${DIFF} ${TEST_DIR}/export -
Expand Down
2 changes: 1 addition & 1 deletion test/integration/simple/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ TEST_DIR="$(cd "$(dirname "${0}")"; echo "$(pwd)")"

ALIASES=$(cd "${TEST_DIR}/../../..//dist"; echo "$(pwd)/aliases -c ${TEST_DIR}/aliases.yaml")
DIFF=$(if which colordiff >/dev/null; then echo "colordiff -Buw --strip-trailing-cr"; else echo "diff -Bw"; fi)
MASK="sed -e s|$(which docker):|[DOCKER]:|g -e s|${HOME}|[HOME]|g -e s|${TEMP_DIR}|[TEMP_DIR]|g"
MASK="sed -e s|${HOME}|[HOME]|g -e s|${TEMP_DIR}|[TEMP_DIR]|g"

${ALIASES} gen --export-path "${TEMP_DIR}" | ${MASK} | sort | ${DIFF} ${TEST_DIR}/alias -
${ALIASES} gen --export --export-path "${TEMP_DIR}" | ${MASK} | ${DIFF} ${TEST_DIR}/export -
Expand Down

0 comments on commit 0bddd00

Please sign in to comment.