rollbot-docker.sh 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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" ]
  47. then
  48. docker logs -f $CONTAINER_NAME
  49. elif [ "$STATUS" = "false" ]
  50. then
  51. echo "Existing container $CONTAINER_NAME is stopped."
  52. else
  53. echo "No existing container $CONTAINER_NAME could be found."
  54. fi
  55. ;;
  56. "r"|"run")
  57. clean_container
  58. echo "Building rollbot3 image as rollbot3:latest"
  59. docker build -t rollbot3:latest .
  60. echo "Executing new container $CONTAINER_NAME using rollbot3:latest"
  61. docker run -p6070:6070 --name $CONTAINER_NAME -d rollbot3
  62. echo "Rollbot endpoint accessible at http://localhost:6070/rollbot"
  63. ;;
  64. "c"|"clean")
  65. clean_container
  66. ;;
  67. "s"|"status")
  68. STATUS=$(status_check)
  69. if [ "$STATUS" = "true" ]
  70. then
  71. echo "Existing container $CONTAINER_NAME is running."
  72. elif [ "$STATUS" = "false" ]
  73. then
  74. echo "Existing container $CONTAINER_NAME is stopped."
  75. else
  76. echo "No existing container $CONTAINER_NAME could be found."
  77. fi
  78. ;;
  79. *)
  80. echo "Unknown option $1, must be one of help, status, clean, run, logs."
  81. exit 1
  82. ;;
  83. esac