run.sh 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env bash
  2. #
  3. # A script to build and run the Swarm development environment using Docker.
  4. set -e
  5. ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
  6. # DEFAULT_NAME is the default name for the Docker image and container
  7. DEFAULT_NAME="swarm-dev"
  8. usage() {
  9. cat >&2 <<USAGE
  10. usage: $0 [options]
  11. Build and run the Swarm development environment.
  12. Depends on Docker being installed locally.
  13. OPTIONS:
  14. -n, --name NAME Docker image and container name [default: ${DEFAULT_NAME}]
  15. -d, --docker-args ARGS Custom args to pass to 'docker run' (e.g. '-p 8000:8000' to expose a port)
  16. -h, --help Show this message
  17. USAGE
  18. }
  19. main() {
  20. local name="${DEFAULT_NAME}"
  21. local docker_args=""
  22. parse_args "$@"
  23. build_image
  24. run_image
  25. }
  26. parse_args() {
  27. while true; do
  28. case "$1" in
  29. -h | --help)
  30. usage
  31. exit 0
  32. ;;
  33. -n | --name)
  34. if [[ -z "$2" ]]; then
  35. echo "ERROR: --name flag requires an argument" >&2
  36. exit 1
  37. fi
  38. name="$2"
  39. shift 2
  40. ;;
  41. -d | --docker-args)
  42. if [[ -z "$2" ]]; then
  43. echo "ERROR: --docker-args flag requires an argument" >&2
  44. exit 1
  45. fi
  46. docker_args="$2"
  47. shift 2
  48. ;;
  49. *)
  50. break
  51. ;;
  52. esac
  53. done
  54. if [[ $# -ne 0 ]]; then
  55. usage
  56. echo "ERROR: invalid arguments" >&2
  57. exit 1
  58. fi
  59. }
  60. build_image() {
  61. docker build --tag "${name}" "${ROOT}/swarm/dev"
  62. }
  63. run_image() {
  64. exec docker run \
  65. --privileged \
  66. --interactive \
  67. --tty \
  68. --rm \
  69. --hostname "${name}" \
  70. --name "${name}" \
  71. --volume "${ROOT}:/go/src/github.com/ethereum/go-ethereum" \
  72. --volume "/var/run/docker.sock:/var/run/docker.sock" \
  73. ${docker_args} \
  74. "${name}" \
  75. /bin/bash
  76. }
  77. main "$@"