|
@@ -0,0 +1,139 @@
|
|
1
|
+#!/bin/bash
|
|
2
|
+#
|
|
3
|
+# .---. . .
|
|
4
|
+# | | |
|
|
5
|
+# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
|
|
6
|
+# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
|
|
7
|
+# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
|
|
8
|
+#
|
|
9
|
+# Freedom in the Cloud
|
|
10
|
+#
|
|
11
|
+# postgresql database functions
|
|
12
|
+#
|
|
13
|
+# License
|
|
14
|
+# =======
|
|
15
|
+#
|
|
16
|
+# Copyright (C) 2017 Bob Mottram <bob@freedombone.net>
|
|
17
|
+#
|
|
18
|
+# This program is free software: you can redistribute it and/or modify
|
|
19
|
+# it under the terms of the GNU Affero General Public License as published by
|
|
20
|
+# the Free Software Foundation, either version 3 of the License, or
|
|
21
|
+# (at your option) any later version.
|
|
22
|
+#
|
|
23
|
+# This program is distributed in the hope that it will be useful,
|
|
24
|
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
25
|
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
26
|
+# GNU Affero General Public License for more details.
|
|
27
|
+#
|
|
28
|
+# You should have received a copy of the GNU Affero General Public License
|
|
29
|
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
30
|
+
|
|
31
|
+# Set this when calling backup and restore commands
|
|
32
|
+USE_POSTGRESQL=
|
|
33
|
+
|
|
34
|
+function get_postgresql_password {
|
|
35
|
+ POSTGRESQL_PASSWORD=$(${PROJECT_NAME}-pass -u root -a postgresql)
|
|
36
|
+ if [[ "$POSTGRESQL_PASSWORD" == *'failed'* ]]; then
|
|
37
|
+ echo $'Could not obtain postgresql password'
|
|
38
|
+ exit 7835272
|
|
39
|
+ fi
|
|
40
|
+}
|
|
41
|
+
|
|
42
|
+function install_postgresql {
|
|
43
|
+ if [[ $(is_completed $FUNCNAME) == "1" ]]; then
|
|
44
|
+ return
|
|
45
|
+ fi
|
|
46
|
+
|
|
47
|
+ function_check get_postgresql_password
|
|
48
|
+ get_postgresql_password
|
|
49
|
+ if [ ! $POSTGRESQL_PASSWORD ]; then
|
|
50
|
+ if [ -f $IMAGE_PASSWORD_FILE ]; then
|
|
51
|
+ POSTGRESQL_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
|
|
52
|
+ else
|
|
53
|
+ POSTGRESQL_PASSWORD="$(openssl rand -base64 32 | cut -c1-${MINIMUM_PASSWORD_LENGTH})"
|
|
54
|
+ fi
|
|
55
|
+ fi
|
|
56
|
+ ${PROJECT_NAME}-pass -u root -a postgresql -p "$POSTGRESQL_PASSWORD"
|
|
57
|
+
|
|
58
|
+ apt-get -yq install postgresql postgresql-contrib postgresql-client
|
|
59
|
+ apt-get -yq remove --purge apache2-bin*
|
|
60
|
+ if [ -d /etc/apache2 ]; then
|
|
61
|
+ rm -rf /etc/apache2
|
|
62
|
+ echo $'Removed Apache installation after postgresql install'
|
|
63
|
+ fi
|
|
64
|
+
|
|
65
|
+ if [ ! -d /etc/postgresql ]; then
|
|
66
|
+ echo $"ERROR: postgresql does not appear to have installed. $CHECK_MESSAGE"
|
|
67
|
+ exit 78352
|
|
68
|
+ fi
|
|
69
|
+
|
|
70
|
+ if [ ! -f /usr/bin/psql ]; then
|
|
71
|
+ echo $"ERROR: psql command does not appear to have installed. $CHECK_MESSAGE"
|
|
72
|
+ exit 835290
|
|
73
|
+ fi
|
|
74
|
+
|
|
75
|
+ mark_completed $FUNCNAME
|
|
76
|
+}
|
|
77
|
+
|
|
78
|
+function add_postgresql_user {
|
|
79
|
+ postgresql_username=$1
|
|
80
|
+ postgresql_password=$2
|
|
81
|
+ sudo -u postgres psql -c "create user $postgresql_username password 'postgresql_password'"
|
|
82
|
+}
|
|
83
|
+
|
|
84
|
+function remove_postgresql_user {
|
|
85
|
+ postgresql_username=$1
|
|
86
|
+ sudo -u postgres psql -c "drop user $postgresql_username"
|
|
87
|
+}
|
|
88
|
+
|
|
89
|
+function remove_database_postgresql {
|
|
90
|
+ database_name="$1"
|
|
91
|
+ sudo -u postgres psql -c "drop database $database_name"
|
|
92
|
+}
|
|
93
|
+
|
|
94
|
+function run_query_postgresql {
|
|
95
|
+ database_name=$1
|
|
96
|
+ database_query=$2
|
|
97
|
+ sudo -u postgres psql -d $database_name -c "$database_query"
|
|
98
|
+}
|
|
99
|
+
|
|
100
|
+function run_query_postgresql_with_output {
|
|
101
|
+ database_name=$1
|
|
102
|
+ database_query=$2
|
|
103
|
+ output=$(sudo -u postgres psql -d $database_name -c << EOF
|
|
104
|
+use $database_name;
|
|
105
|
+$database_query
|
|
106
|
+EOF
|
|
107
|
+)
|
|
108
|
+ echo "$output"
|
|
109
|
+}
|
|
110
|
+
|
|
111
|
+function initialise_database_postgresql {
|
|
112
|
+ database_name=$1
|
|
113
|
+ database_file=$2
|
|
114
|
+ sudo -u postgres psql $database_name < $database_file
|
|
115
|
+ if [ ! "$?" = "0" ]; then
|
|
116
|
+ exit 7238525
|
|
117
|
+ fi
|
|
118
|
+}
|
|
119
|
+
|
|
120
|
+function create_database_postgresql {
|
|
121
|
+ app_name="$1"
|
|
122
|
+ app_admin_password="$2"
|
|
123
|
+ app_admin_username=$3
|
|
124
|
+ if [ ! -d $INSTALL_DIR ]; then
|
|
125
|
+ mkdir $INSTALL_DIR
|
|
126
|
+ fi
|
|
127
|
+ if [ ! $app_admin_username ]; then
|
|
128
|
+ app_admin_username=${app_name}admin
|
|
129
|
+ fi
|
|
130
|
+
|
|
131
|
+ echo "create database ${app_name};
|
|
132
|
+CREATE USER '$app_admin_username@localhost' IDENTIFIED BY '${app_admin_password}';
|
|
133
|
+GRANT ALL PRIVILEGES ON ${app_name}.* TO '$app_admin_username@localhost';
|
|
134
|
+flush privileges;
|
|
135
|
+quit" > $INSTALL_DIR/batch.sql
|
|
136
|
+ chmod 600 $INSTALL_DIR/batch.sql
|
|
137
|
+ sudo -u postgres psql -d $database_name --file=$INSTALL_DIR/batch.sql
|
|
138
|
+ shred -zu $INSTALL_DIR/batch.sql
|
|
139
|
+}
|