aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/dev/run.sh
blob: 71ed13262b441ad763ad0fc67bffd43b5b85eb0d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env bash
#
# A script to build and run the Swarm development environment using Docker.

set -e

ROOT="$(cd "$(dirname "$0")/../.." && pwd)"

# DEFAULT_NAME is the default name for the Docker image and container
DEFAULT_NAME="swarm-dev"

usage() {
  cat >&2 <<USAGE
usage: $0 [options]

Build and run the Swarm development environment.

Depends on Docker being installed locally.

OPTIONS:
  -n, --name NAME          Docker image and container name [default: ${DEFAULT_NAME}]
  -d, --docker-args ARGS   Custom args to pass to 'docker run' (e.g. '-p 8000:8000' to expose a port)
  -h, --help               Show this message
USAGE
}

main() {
  local name="${DEFAULT_NAME}"
  local docker_args=""
  parse_args "$@"
  build_image
  run_image
}

parse_args() {
  while true; do
    case "$1" in
      -h | --help)
        usage
        exit 0
        ;;
      -n | --name)
        if [[ -z "$2" ]]; then
          echo "ERROR: --name flag requires an argument" >&2
          exit 1
        fi
        name="$2"
        shift 2
        ;;
      -d | --docker-args)
        if [[ -z "$2" ]]; then
          echo "ERROR: --docker-args flag requires an argument" >&2
          exit 1
        fi
        docker_args="$2"
        shift 2
        ;;
      *)
        break
        ;;
    esac
  done

  if [[ $# -ne 0 ]]; then
    usage
    echo "ERROR: invalid arguments" >&2
    exit 1
  fi
}

build_image() {
  docker build --tag "${name}" "${ROOT}/swarm/dev"
}

run_image() {
  exec docker run \
    --privileged \
    --interactive \
    --tty \
    --rm \
    --hostname "${name}" \
    --name     "${name}" \
    --volume   "${ROOT}:/go/src/github.com/dexon-foundation/dexon" \
    --volume   "/var/run/docker.sock:/var/run/docker.sock" \
    ${docker_args} \
    "${name}" \
    /bin/bash
}

main "$@"