mirror of the now-defunct rocklinux.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
5.4 KiB

  1. # --- ROCK-COPYRIGHT-NOTE-BEGIN ---
  2. #
  3. # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  4. # Please add additional copyright information _after_ the line containing
  5. # the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
  6. # the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
  7. #
  8. # ROCK Linux: rock-src/package/michiel/postgresql/stone_mod_postgresql.sh
  9. # ROCK Linux is Copyright (C) 1998 - 2005 Clifford Wolf
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation; either version 2 of the License, or
  14. # (at your option) any later version. A copy of the GNU General Public
  15. # License can be found at Documentation/COPYING.
  16. #
  17. # Many people helped and are helping developing ROCK Linux. Please
  18. # have a look at http://www.rocklinux.org/ and the Documentation/TEAM
  19. # file for details.
  20. #
  21. # --- ROCK-COPYRIGHT-NOTE-END ---
  22. #
  23. # [MAIN] 75 pgsql PostgreSQL Database Setup
  24. pgsql_start() {
  25. gui_cmd 'starting postgresql server' "rc postgresql start"
  26. }
  27. pgsql_stop() {
  28. gui_cmd 'stopping postgresql server' "rc postgresql stop"
  29. }
  30. pgsql_initdb() {
  31. export `locale`
  32. locale=""
  33. encoding=""
  34. if gui_yesno "Initialize the database with the current system locale? ($LANG)" ; then
  35. locale=$LANG
  36. else
  37. gui_input "Locale to initialize the DB with: " "$locale" "locale"
  38. fi
  39. if gui_yesno "Initialize the database with the default encoding for the locale $locale ?" ; then
  40. encoding=""
  41. else
  42. gui_input "Encoding to initialize the DB with (i.e. UNICODE): " "$encoding" "encoding"
  43. fi
  44. gui_cmd "Initializing PostgreSQL Database (locale: $locale)" \
  45. "echo \"D_prefix/bin/initdb --locale=$locale --encoding=$encoding\" | su - postgres"
  46. }
  47. pgsql_create_user() {
  48. createdb="--no-createdb"
  49. adduser="--no-adduser"
  50. gui_input "User to create: " "$username" "username"
  51. if getent passwd $username > /dev/null 2>&1 ; then
  52. gui_yesno "should the user be able to create databases?" && createdb="--createdb"
  53. gui_yesno "should the user be able to add users?" && adduser="--adduser"
  54. gui_cmd "Creating ProstgreSQL User $username" \
  55. "echo \"D_prefix/bin/createuser $createdb $adduser $username\" | su - postgres"
  56. else
  57. gui_message "$username is not a valid system user!" ;
  58. fi
  59. }
  60. pgsql_user_menu() {
  61. function=$1
  62. umenu="gui_menu pgsql_users 'Please select user:'"
  63. for uname in `echo "D_prefix/bin/psql -c '\du' template1" | su - postgres | tail -n+4 | head -n-2 \
  64. | awk -F'|' '{ print $1 ; }' | tr -d ' '| grep -v postgres`; do
  65. umenu="$umenu '$uname' '$function $uname'"
  66. done;
  67. unset uname function
  68. eval $umenu
  69. }
  70. pgsql_db_menu() {
  71. user=$1
  72. function=$2
  73. dmenu="gui_menu pgsql_dbs 'Please select database:'"
  74. for db in `echo "D_prefix/bin/psql -l" | su - postgres | tail -n+4 | head -n-2 | grep " | $user.[ ]*|" \
  75. |awk -F'|' '{ print $1 ; }' | tr -d ' '| grep -v template` ; do
  76. dmenu="$dmenu '$db' '$function $db'"
  77. done;
  78. unset user function
  79. eval $dmenu
  80. }
  81. pgsql_drop_user() {
  82. if [ -z "$1" ] ; then
  83. pgsql_user_menu pgsql_drop_user
  84. else
  85. uname=$1
  86. if gui_yesno "are you sure you want to drop the user '$uname' ?" ; then
  87. gui_cmd "Dropping User $uname" "echo \"D_prefix/bin/dropuser $uname\" | su - postgres"
  88. else
  89. gui_message "user deletion aborted"
  90. fi
  91. unset uname
  92. fi
  93. }
  94. pgsql_create_db() {
  95. if [ -z "$1" -a -z "$uname" ] ; then
  96. pgsql_user_menu pgsql_create_db ;
  97. return
  98. fi
  99. [ -z "$uname" -a -n "$1" ] && uname=$1
  100. gui_input "Name of the database to create for user $uname:" "$dbname" "dbname"
  101. [ -z "$dbname" ] && return;
  102. [ -z "$encoding" ] && encoding="UNICODE"
  103. gui_input "Encoding for the database to create:" "$encoding" "encoding"
  104. [ -z "$encoding" ] && return;
  105. gui_cmd "Creating database $dbname for user $uname - $encoding" \
  106. "echo \"D_prefix/bin/createdb -E $encoding $dbname\" | su - $uname"
  107. unset dbname encoding uname
  108. }
  109. pgsql_drop_db() {
  110. if [ -z "$1" -a -z "$uname" ] ; then
  111. pgsql_user_menu pgsql_drop_db ;
  112. return
  113. fi
  114. [ -z "$uname" -a -n "$1" ] && uname=$1
  115. if [ -z "$2" -a -z "$dbname" ] ; then
  116. pgsql_db_menu $uname "pgsql_drop_db $uname ";
  117. return
  118. fi
  119. [ -z "$dbname" -a -n "$2" ] && dbname=$2
  120. [ -z "$dbname" -o -z "$uname" ] && return;
  121. if gui_yesno "Are you sure you want to drop $uname's database $dbname?" ; then
  122. gui_cmd "Dropping database $dbname for user $uname" \
  123. "echo \"D_prefix/bin/dropdb $dbname\" | su - $uname"
  124. fi
  125. unset dbname uname
  126. }
  127. pgsql_gen_menu () {
  128. eval `echo "D_prefix/bin/initdb --show 2>&1" | su - postgres | grep PGDATA`
  129. PGPID="`echo "D_prefix/bin/pg_ctl status" | su - postgres | grep -o 'PID: .[^)]*' | tr -d 'PID: '`"
  130. echo "gui_menu pgsql 'PostgresQL Database Setup'"
  131. # initialisation is needed once, so we note if it seems done
  132. echo " 'Initialize database system'"
  133. echo " 'pgsql_initdb'"
  134. if [ -f $PGDATA/postgresql.conf ] ; then
  135. echo " 'note: this seems to be done already' ''"
  136. echo " '' ''"
  137. if [ -z "$PGPID" ] ; then
  138. echo " 'note: postgres needs to be running for more options' ''"
  139. echo " '' ''"
  140. echo " 'Start PostgreSQL Server' 'pgsql_start'"
  141. else
  142. echo " 'Create a user' 'pgsql_create_user'"
  143. echo " 'Drop a user' 'pgsql_drop_user'"
  144. echo " '' ''"
  145. echo " 'Create a database' 'pgsql_create_db'"
  146. echo " 'Drop a database' 'pgsql_drop_db'"
  147. echo " '' ''"
  148. echo " 'Stop PostgreSQL Server' 'pgsql_stop'"
  149. fi
  150. fi
  151. }
  152. main() {
  153. while
  154. eval `pgsql_gen_menu`
  155. do : ; done
  156. }