rollbot-docker.sh 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/env sh
  2. if [ $# -lt 1 ] || [ "$1" == "help" ] || [ "$1" == "h" ]
  3. then
  4. echo "usage: $0 (help|status|clean|run|logs) [container-name]"
  5. echo " help - print this message"
  6. echo " status - report whether a container named container-name can be found, and whether or not it is running"
  7. echo " clean - kill and remove any existing containers matching container-name"
  8. echo " run - kill and remove any existing containers matching container-name, build the current directory into a new image, and deploy a new container container-name"
  9. echo " logs - tail the logs of the container named container-name, equivalent to docker logs -f container-name"
  10. echo " container-name defaults to rollbot3-instance"
  11. echo " Run as root to ensure docker can be used, or set FORCE_NOROOT=* to force the script to run anyway."
  12. exit 1
  13. fi
  14. if [ "$EUID" -ne 0 ] && [ -z "$FORCE_NOROOT" ]
  15. then
  16. echo "$0 must be run as root to use docker. Set FORCE_NOROOT=* to force the script to run anyway."
  17. exit 1
  18. fi
  19. if [ $# -gt 1 ]
  20. then
  21. CONTAINER_NAME=$2
  22. else
  23. CONTAINER_NAME="rollbot3-instance"
  24. fi
  25. status_check() {
  26. docker inspect -f '{{.State.Running}}' $CONTAINER_NAME 2> /dev/null
  27. }
  28. clean_container() {
  29. STATUS=$(status_check)
  30. if [ "$STATUS" = "true" ]
  31. then
  32. echo "Container $CONTAINER_NAME is running and will be stopped and removed."
  33. docker kill $CONTAINER_NAME
  34. docker rm $CONTAINER_NAME
  35. elif [ "$STATUS" = "false" ]
  36. then
  37. echo "Container $CONTAINER_NAME is stopped and will be removed."
  38. docker rm $CONTAINER_NAME
  39. else
  40. echo "No existing container $CONTAINER_NAME could be found, no cleanup will be performed."
  41. fi
  42. }
  43. case $1 in
  44. "l"|"logs")
  45. STATUS=$(status_check)
  46. if [ "$STATUS" = "true" ] || [ "$STATUS" = "false" ]
  47. then
  48. docker logs -f $CONTAINER_NAME
  49. else
  50. echo "No existing container $CONTAINER_NAME could be found."
  51. fi
  52. ;;
  53. "r"|"run")
  54. clean_container
  55. echo "Building rollbot3 image as rollbot3:latest"
  56. docker build -t rollbot3:latest .
  57. echo "Executing new container $CONTAINER_NAME using rollbot3:latest"
  58. docker run -p6070:6070 -v ./config:/rollbot-config --name $CONTAINER_NAME -d rollbot3
  59. echo "Rollbot endpoint accessible at http://localhost:6070/rollbot"
  60. ;;
  61. "c"|"clean")
  62. clean_container
  63. ;;
  64. "s"|"status")
  65. STATUS=$(status_check)
  66. if [ "$STATUS" = "true" ]
  67. then
  68. echo "Existing container $CONTAINER_NAME is running."
  69. elif [ "$STATUS" = "false" ]
  70. then
  71. echo "Existing container $CONTAINER_NAME is stopped."
  72. else
  73. echo "No existing container $CONTAINER_NAME could be found."
  74. fi
  75. ;;
  76. *)
  77. echo "Unknown option $1, must be one of help, status, clean, run, logs."
  78. exit 1
  79. ;;
  80. esac