Guest User

Untitled

a guest
May 19th, 2018
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # The MIT License (MIT)
  4. #
  5. # Copyright (c) 2014-2018 Greg Messner, greg@messners.com
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining a copy
  8. # of this software and associated documentation files (the "Software"), to deal
  9. # in the Software without restriction, including without limitation the rights
  10. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. # copies of the Software, and to permit persons to whom the Software is
  12. # furnished to do so, subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be included in all
  15. # copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. # SOFTWARE.
  24.  
  25. #
  26. # This is the service utility script for running MySQL in a docker container. It is intended
  27. # to be called from upstart or systemd service scripts, but can also be used from the command line
  28. # to start, stop, and check the status of a running MySQL container.
  29. #
  30.  
  31. # Load the docker utility functions
  32. SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  33. source $SCRIPT_DIR/docker-functions.sh
  34.  
  35. #
  36. # Print the help information for the MySQL service container.
  37. #
  38. function help {
  39.  
  40. echo
  41. echo " This script performs one or more actions on the MySQL service container."
  42. echo
  43. echo " Usage : $(basename $0) action ..."
  44. echo
  45. echo " Available actions"
  46. echo " ------------------------------------------------------------------------"
  47. echo " attach Attaches the current process to the running MySQL service,"
  48. echo " usually only used by actual OS services"
  49. echo " cleanup Cleans up (removes) the MySQL service container"
  50. echo " destroy Stops and removes the MySQL service container, and deletes"
  51. echo " the associated database data (docker volume)"
  52. echo " help Prints this help information and exits"
  53. echo " start Starts the MySQL service container. When starting the service"
  54. echo " the following environment variables must be set:"
  55. echo " MYSQL_DATABASE, MYSQL_USER, and MYSQL_PASSWORD"
  56. echo " status Returns a one word status for the MySQL service"
  57. echo " stop Stops the MySQL service container"
  58. echo
  59. echo " NOTE: Multiple actions may be combined on a single command line, for example:"
  60. echo " $(basename $0) start attach"
  61. echo
  62. }
  63.  
  64.  
  65. #
  66. # Stops the service container if it is running and deletes the associated docker volume.
  67. #
  68. destroy() {
  69.  
  70. # Make sure the container is stopped and removed
  71. df_kill_container ${MYSQL_CONTAINER}
  72.  
  73. # Remove the docker volume if it is present
  74. if docker volume inspect ${MYSQL_DATA_VOLUME} >/dev/null 2>&1 ; then
  75. echo -n "Removing ${MYSQL_DATA_VOLUME} data volume..."
  76. docker volume rm --force ${MYSQL_DATA_VOLUME} >/dev/null 2>&1
  77. echo "done"
  78. fi
  79. }
  80.  
  81.  
  82. #
  83. # Launches the container and waits for it to be fully started. After calling 'start', you can
  84. # call the 'attach' method to attach to the log output of the container.
  85. #
  86. start() {
  87.  
  88. # Make sure the database name, username, and password is set, if not, abort
  89. ENV_ERRORS="false"
  90. if [ -z "$MYSQL_DATABASE" ]; then
  91. echo "MYSQL_DATABASE env variable is not set!" 1>&2
  92. ENV_ERRORS="true"
  93. fi
  94.  
  95. if [ -z "$MYSQL_USER" ]; then
  96. echo "MYSQL_USER is env variable not set!" 1>&2
  97. ENV_ERRORS="true"
  98. fi
  99.  
  100. if [ -z "$MYSQL_PASSWORD" ]; then
  101. echo "MYSQL_PASSWORD env variable is not set!" 1>&2
  102. ENV_ERRORS="true"
  103. fi
  104.  
  105. if [ "$ENV_ERRORS" == "true" ]; then
  106. exit 1
  107. fi
  108.  
  109. # If the data volume does not exist, create it
  110. if ! docker volume inspect ${MYSQL_DATA_VOLUME} >/dev/null 2>&1 ; then
  111. echo "Creating ${MYSQL_DATA_VOLUME} data volume"
  112. docker volume create ${MYSQL_DATA_VOLUME} >/dev/null 2>&1
  113. fi
  114.  
  115. # Make sure that docker does not have the service container present
  116. df_kill_container ${MYSQL_CONTAINER}
  117.  
  118. # Run the docker container
  119. CONTAINER_ID=$(docker run --name "${MYSQL_CONTAINER}" \
  120. --detach --restart=no \
  121. --health-cmd="mysqladmin ping --silent" \
  122. -p ${MYSQL_PORT}:3306 \
  123. -e MYSQL_RANDOM_ROOT_PASSWORD=yes \
  124. -e MYSQL_USER=${MYSQL_USER} \
  125. -e MYSQL_PASSWORD="${MYSQL_PASSWORD}" \
  126. -e MYSQL_DATABASE=${MYSQL_DATABASE} \
  127. -v ${MYSQL_DATA_VOLUME}:/var/lib/mysql \
  128. ${MYSQL_IMAGE_NAME}:${MYSQL_IMAGE_VERSION})
  129.  
  130. if [ $? -ne 0 ]; then
  131. echo "Error starting ${MYSQL_CONTAINER}"
  132. exit 1
  133. fi
  134.  
  135. # Wait a maximum of 60 seconds for the container to be fully started and healthy
  136. echo "Started ${MYSQL_CONTAINER}, container ID=${CONTAINER_ID}"
  137. echo "Waiting for ${MYSQL_CONTAINER} to become healthy"
  138. df_wait_for_container 60 ${MYSQL_CONTAINER}
  139. }
  140.  
  141.  
  142. #############################################################################
  143.  
  144. # Need at least a single argumet (action), if none are provided putput usage info and exit with error.
  145. if [ $# -lt 1 ]; then
  146. echo "Usage : $(basename $0) attach|cleanup|destroy|help|status|start|stop"
  147. exit 1
  148. fi
  149.  
  150. if [ "$1" == "help" ]; then
  151. help
  152. exit 0
  153. fi
  154.  
  155. # Default the env variables if they are not set.
  156. MYSQL_IMAGE_NAME=${MYSQL_IMAGE_NAME:-mysql}
  157. MYSQL_IMAGE_VERSION=${MYSQL_IMAGE_VERSION:-"5.7.22"}
  158. MYSQL_CONTAINER=${MYSQL_CONTAINER:-mysql-db}
  159. MYSQL_DATA_VOLUME=${MYSQL_DATA_VOLUME:-mysql-db-data}
  160. MYSQL_PORT=${MYSQL_PORT:-3306}
  161.  
  162. #
  163. # Loop thru the arguments (actions). Some actions may be paired together, for example:
  164. #
  165. # start attach - Will start the container and attach the current process to it
  166. # stop cleanup - Will stop the container and then clean it up (remove it)
  167. #
  168. for ARG in "$@"
  169. do
  170. case "$ARG" in
  171.  
  172. "attach")
  173. df_attach_container $MYSQL_CONTAINER
  174. ;;
  175.  
  176. "cleanup")
  177. df_cleanup_container $MYSQL_CONTAINER
  178. ;;
  179.  
  180. "destroy")
  181. destroy
  182. ;;
  183.  
  184. "start")
  185. start
  186. ;;
  187.  
  188. "status")
  189. df_container_status $MYSQL_CONTAINER
  190. ;;
  191.  
  192. "stop")
  193. df_stop_container $MYSQL_CONTAINER
  194. ;;
  195.  
  196. *)
  197. echo "'$1' is not a valid $(basename $0 '.sh') command, aborting!"
  198. exit 1
  199. ;;
  200. esac
  201.  
  202. RESULTS=$?
  203. if [ $RESULTS -ne 0 ]; then
  204. exit $RESULTS
  205. fi
  206. done
  207.  
  208. # vim: syntax=sh ts=4 sw=4 sts=4 sr noet
Add Comment
Please, Sign In to add comment