Guest User

Untitled

a guest
May 19th, 2018
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.74 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 Postgres 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 Postgres 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 Postgres service container.
  37. #
  38. function help {
  39.  
  40. echo
  41. echo " This script performs one or more actions on the Postgres 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 Postgres service,"
  48. echo " usually only used by actual OS services"
  49. echo " cleanup Cleans up (removes) the Postgres service container"
  50. echo " destroy Stops and removes the Postgres 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 Postgres service container. When starting the service"
  54. echo " the following environment variables must be set:"
  55. echo " POSTGRES_DB, POSTGRES_USER, and POSTGRES_PASSWORD"
  56. echo " status Returns a one word status for the Postgres service"
  57. echo " stop Stops the Postgres 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 ${POSTGRES_CONTAINER}
  72.  
  73. # Remove the docker volume if it is present
  74. if docker volume inspect ${POSTGRES_DATA_VOLUME} >/dev/null 2>&1 ; then
  75. echo -n "Removing ${POSTGRES_DATA_VOLUME} data volume..."
  76. docker volume rm --force ${POSTGRES_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 a service script 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 "$POSTGRES_DB" ]; then
  91. echo "POSTGRES_DB env variable is not set!" 1>&2
  92. ENV_ERRORS="true"
  93. fi
  94.  
  95. if [ -z "$POSTGRES_USER" ]; then
  96. echo "POSTGRES_USER env variable is not set!" 1>&2
  97. ENV_ERRORS="true"
  98. fi
  99.  
  100. if [ -z "$POSTGRES_PASSWORD" ]; then
  101. echo "POSTGRES_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 ${POSTGRES_DATA_VOLUME} >/dev/null 2>&1 ; then
  111. echo "Creating ${POSTGRES_DATA_VOLUME} data volume"
  112. docker volume create ${POSTGRES_DATA_VOLUME} >/dev/null 2>&1
  113. fi
  114.  
  115. # If the local postgres config file is set and exists, set the container up to use it
  116. LOCAL_POSTGRES_CONFIG=()
  117. if [[ ! -z $LOCAL_POSTGRES_CONFIG_FILE ]] && [[ -f $LOCAL_POSTGRES_CONFIG_FILE ]]; then
  118. echo "Setting up container to use local postgres configuration"
  119. LOCAL_POSTGRES_CONFIG=("-c" "config_file=$LOCAL_POSTGRES_CONFIG_FILE")
  120. fi
  121.  
  122. # Make sure that docker does not have the service container present
  123. df_kill_container ${POSTGRES_CONTAINER}
  124.  
  125. # Run the docker container
  126. CONTAINER_ID=$(docker run --name "${POSTGRES_CONTAINER}" \
  127. --detach --restart=no \
  128. --health-cmd="pg_isready -h localhost" \
  129. -p ${POSTGRES_PORT}:5432 \
  130. -e POSTGRES_DB=${POSTGRES_DB} \
  131. -e POSTGRES_USER=${POSTGRES_USER} \
  132. -e POSTGRES_PASSWORD="${POSTGRES_PASSWORD}" \
  133. -v ${POSTGRES_DATA_VOLUME}:/var/lib/postgresql/data \
  134. ${POSTGRES_IMAGE_NAME}:${POSTGRES_IMAGE_VERSION} ${LOCAL_POSTGRES_CONFIG[*]})
  135.  
  136. if [ $? -ne 0 ]; then
  137. echo "Error starting ${POSTGRES_CONTAINER}"
  138. exit 1
  139. fi
  140.  
  141. # Wait a maximum of 60 seconds for the container to be fully started and healthy
  142. echo "Started ${POSTGRES_CONTAINER}, container ID=${CONTAINER_ID}"
  143. echo "Waiting for ${POSTGRES_CONTAINER} to become healthy"
  144. df_wait_for_container 60 ${POSTGRES_CONTAINER}
  145. }
  146.  
  147. #############################################################################
  148.  
  149. # Need at least a single argumet (action), if none are provided putput usage info and exit with error.
  150. if [ $# -lt 1 ]; then
  151. echo "Usage : $(basename $0) attach|cleanup|destroy|help|status|start|stop"
  152. exit 1
  153. fi
  154.  
  155. if [ "$1" == "help" ]; then
  156. help
  157. exit 0
  158. fi
  159.  
  160. # Default the env variables if they are not set.
  161. POSTGRES_IMAGE_NAME=${POSTGRES_IMAGE_NAME:-postgres}
  162. POSTGRES_IMAGE_VERSION=${POSTGRES_IMAGE_VERSION:-"9.6.8"}
  163. POSTGRES_CONTAINER=${POSTGRES_CONTAINER:-postgres-db}
  164. POSTGRES_DATA_VOLUME=${POSTGRES_DATA_VOLUME:-postgres-db-data}
  165. POSTGRES_PORT=${POSTGRES_PORT:-5432}
  166.  
  167. #
  168. # Loop thru the arguments (actions). Some actions may be paired together, for example:
  169. #
  170. # start attach - Will start the container and attach the current process to it
  171. # stop cleanup - Will stop the container and then clean it up (remove it)
  172. #
  173. for ARG in "$@"
  174. do
  175. case "$ARG" in
  176.  
  177. "attach")
  178. df_attach_container $POSTGRES_CONTAINER
  179. ;;
  180.  
  181. "cleanup")
  182. df_cleanup_container $POSTGRES_CONTAINER
  183. ;;
  184.  
  185. "destroy")
  186. destroy
  187. ;;
  188.  
  189. "start")
  190. start
  191. ;;
  192.  
  193. "status")
  194. df_container_status $POSTGRES_CONTAINER
  195. ;;
  196.  
  197. "stop")
  198. df_stop_container $POSTGRES_CONTAINER
  199. ;;
  200.  
  201. *)
  202. echo "'$1' is not a valid $(basename $0 '.sh') command, aborting!"
  203. exit 1
  204. ;;
  205. esac
  206.  
  207. RESULTS=$?
  208. if [ $RESULTS -ne 0 ]; then
  209. exit $RESULTS
  210. fi
  211. done
  212.  
  213. # vim: syntax=sh ts=4 sw=4 sts=4 sr noet
Add Comment
Please, Sign In to add comment