Guest User

Untitled

a guest
Jan 15th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. #!/bin/bash
  2. set -x
  3. set -o errexit
  4. PASSWORD=$(date +%s | sha256sum | base64 | head -c 32)
  5. INITSQL=/opt/postgres/initscripts/init_quac_db.sql
  6. CONTAINERS="guacamole guacd postgres"
  7.  
  8. pause() {
  9. read -p "Enter to continue..."
  10. }
  11.  
  12. clean() {
  13. stop
  14. docker system prune -a --force --volumes
  15. }
  16.  
  17. clean_full() {
  18. echo "Removing all unused and dangling Docker Containers/Images/Volumes"
  19. clean
  20. # TODO put in check and warn if data is found before removing
  21. rm -rf /opt/postgres/data
  22. rm -rf /opt/postgres/initscripts
  23. mkdir -p /opt/postgres/data
  24. mkdir -p /opt/postgres/initscripts
  25. }
  26.  
  27.  
  28. #--attach STDOUT \
  29. s_postgres() {
  30. docker run \
  31. --name postgres \
  32. --detach \
  33. -e POSTGRES_PASSWORD=$PASSWORD \
  34. --volume /opt/postgres/data:/var/lib/postgresql/data \
  35. --volume /opt/postgres/initscripts:/docker-entrypoint-initdb.d \
  36. postgres
  37. }
  38.  
  39. s_guacd() {
  40. docker run --name guacd -d guacamole/guacd
  41. }
  42.  
  43. s_guacamole() {
  44. docker run --name guacamole \
  45. --link guacd:guacd \
  46. --link postgres:postgres \
  47. -e POSTGRES_DATABASE=guacamole_db \
  48. -e POSTGRES_USER=guacamole_user \
  49. -e POSTGRES_PASSWORD=$PASSWORD \
  50. -d -p 127.0.0.1:8080:8080 guacamole/guacamole
  51. }
  52.  
  53. start() {
  54. s_postgres
  55. s_guacd
  56. s_guacamole
  57. }
  58.  
  59. stop() {
  60. for C in $CONTAINERS; do
  61. if [ -n "$(docker ps -q --filter "Name=$C")" ]; then
  62. docker stop $C
  63. fi
  64. done
  65. }
  66.  
  67. restart() {
  68. stop
  69. start
  70. }
  71.  
  72. create_init_sql() {
  73. # Stop any running guac envs
  74. # Clean previous installed guac envs
  75. clean_full
  76. ID=$(docker create guacamole/guacamole)
  77. echo "CREATE DATABASE guacamole_db;" > $INITSQL
  78. echo "\connect guacamole_db" >> $INITSQL
  79. docker cp $ID:/opt/guacamole/postgresql/schema/001-create-schema.sql - | tar -xO >> $INITSQL
  80. docker cp $ID:/opt/guacamole/postgresql/schema/002-create-admin-user.sql - | tar -xO >> $INITSQL
  81. docker rm -v $ID
  82. echo "CREATE USER guacamole_user WITH PASSWORD '$PASSWORD';" >> $INITSQL
  83. echo "GRANT SELECT,INSERT,UPDATE,DELETE ON ALL TABLES IN SCHEMA public TO guacamole_user;" >> $INITSQL
  84. echo "GRANT SELECT,USAGE ON ALL SEQUENCES IN SCHEMA public TO guacamole_user;" >> $INITSQL
  85. echo "GRANT ALL PRIVILEGES ON DATABASE guacamole_db TO guacamole_user;" >> $INITSQL
  86. chmod 777 /opt/postgres/initscripts
  87. chmod 777 $INITSQL
  88. stop
  89. }
  90.  
  91. # Create guacamole db init scripts
  92. create_init_sql
  93. # Start Guac env with mapping created sql init scripts.
  94. start
Add Comment
Please, Sign In to add comment