Ver código fonte

Smilodon app

Bob Mottram 7 anos atrás
pai
commit
7cc56af805
3 arquivos alterados com 558 adições e 29 exclusões
  1. 357
    0
      src/freedombone-app-smilodon
  2. 85
    29
      src/freedombone-utils-backup
  3. 116
    0
      src/freedombone-utils-mongodb

+ 357
- 0
src/freedombone-app-smilodon Ver arquivo

@@ -0,0 +1,357 @@
1
+#!/bin/bash
2
+#
3
+# .---.                  .              .
4
+# |                      |              |
5
+# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
6
+# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
7
+# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
8
+#
9
+#                    Freedom in the Cloud
10
+#
11
+# Smilodon ActivityPub app
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
+VARIANTS='full full-vim social'
32
+
33
+IN_DEFAULT_INSTALL=0
34
+SHOW_ON_ABOUT=1
35
+SHOW_ICANN_ADDRESS_ON_ABOUT=0
36
+
37
+SMILODON_REPO="https://github.com/bashrc/smilodon"
38
+SMILODON_COMMIT='22072cfbd9bdbdd6f4bb5ab440d8f468e38a912a'
39
+SMILODON_ADMIN_PASSWORD=
40
+SMILODON_ONION_PORT=8054
41
+SMILODON_PATH=/etc/smilodon
42
+SMILODON_SECRET_KEY=
43
+
44
+smilodon_variables=(SMILODON_REPO
45
+                    SMILODON_ADMIN_PASSWORD
46
+                    USB_MOUNT
47
+                    SMILODON_SECRET_KEY
48
+                    MY_EMAIL_ADDRESS
49
+                    MY_USERNAME)
50
+
51
+function logging_on_smilodon {
52
+    echo -n ''
53
+}
54
+
55
+function logging_off_smilodon {
56
+    echo -n ''
57
+}
58
+
59
+function remove_user_smilodon {
60
+    remove_username="$1"
61
+}
62
+
63
+function add_user_smilodon {
64
+    new_username="$1"
65
+    new_user_password="$2"
66
+    echo '0'
67
+}
68
+
69
+function install_interactive_smilodon {
70
+    echo -n ''
71
+    APP_INSTALLED=1
72
+}
73
+
74
+function change_password_smilodon {
75
+    curr_username="$1"
76
+    new_user_password="$2"
77
+
78
+    #${PROJECT_NAME}-pass -u "$curr_username" -a smilodon -p "$new_user_password"
79
+}
80
+
81
+function smilodon_create_database {
82
+    if [ -f $IMAGE_PASSWORD_FILE ]; then
83
+        SMILODON_ADMIN_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
84
+    else
85
+        if [ ! $SMILODON_ADMIN_PASSWORD ]; then
86
+            SMILODON_ADMIN_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
87
+        fi
88
+    fi
89
+    if [ ! $SMILODON_ADMIN_PASSWORD ]; then
90
+        return
91
+    fi
92
+
93
+    function_check create_database_mongodb
94
+    create_database_mongodb smilodon "$SMILODON_ADMIN_PASSWORD" smilodon
95
+}
96
+
97
+function reconfigure_smilodon {
98
+    echo -n ''
99
+}
100
+
101
+function upgrade_smilodon {
102
+    CURR_SMILODON_COMMIT=$(get_completion_param "smilodon commit")
103
+    if [[ "$CURR_SMILODON_COMMIT" == "$SMILODON_COMMIT" ]]; then
104
+        return
105
+    fi
106
+
107
+    if [[ $(app_is_installed smilodon) == "1" ]]; then
108
+        systemctl stop smilodon
109
+        function_check set_repo_commit
110
+        set_repo_commit $SMILODON_PATH "smilodon commit" "$SMILODON_COMMIT" $SMILODON_REPO
111
+        chown -R smilodon:smilodon $SMILODON_PATH
112
+        systemctl start smilodon
113
+    fi
114
+
115
+}
116
+
117
+function backup_local_smilodon {
118
+    if [ -d $SMILODON_PATH ]; then
119
+        systemctl stop smilodon
120
+
121
+        USE_MONGODB=1
122
+        function_check backup_database_to_usb
123
+        backup_database_to_usb smilodon
124
+
125
+        backup_directory_to_usb $SMILODON_PATH smilodon
126
+
127
+        systemctl start smilodon
128
+    fi
129
+}
130
+
131
+function restore_local_smilodon {
132
+    temp_restore_dir=/root/tempsmilodon
133
+
134
+    systemctl stop smilodon
135
+
136
+    function_check smilodon_create_database
137
+    smilodon_create_database
138
+
139
+    USE_MONGODB=1
140
+    restore_database smilodon
141
+
142
+    if [ -d ${SMILODON_PATH} ]; then
143
+        if [ -d $temp_restore_dir${SMILODON_PATH} ]; then
144
+            if [ -d $temp_restore_dir${SMILODON_PATH} ]; then
145
+                rm -rf ${SMILODON_PATH}
146
+                mv $temp_restore_dir$SMILODON_PATH ${SMILODON_PATH}/
147
+            else
148
+                cp -r $temp_restore_dir/* ${SMILODON_PATH}/
149
+            fi
150
+            if [ ! "$?" = "0" ]; then
151
+                function_check backup_unmount_drive
152
+                backup_unmount_drive
153
+                systemctl start smilodon
154
+                exit 528823
155
+            fi
156
+            chown -R smilodon:smilodon ${SMILODON_PATH}
157
+        fi
158
+    fi
159
+
160
+    if [ -d $USB_MOUNT/backup/smilodon ]; then
161
+        chown -R smilodon:smilodon ${SMILODON_PATH}
162
+        if [ -d $temp_restore_dir ]; then
163
+            rm -rf $temp_restore_dir
164
+        fi
165
+    fi
166
+
167
+    systemctl start smilodon
168
+}
169
+
170
+function backup_remote_smilodon {
171
+    if [ -d $SMILODON_PATH ]; then
172
+        function_check suspend_site
173
+        suspend_site smilodon
174
+
175
+        systemctl stop smilodon
176
+
177
+        USE_MONGODB=1
178
+        function_check backup_database_to_friend
179
+        backup_database_to_friend smilodon
180
+
181
+        function_check backup_directory_to_friend
182
+        backup_directory_to_friend $SMILODON_PATH smilodon
183
+
184
+        systemctl start smilodon
185
+
186
+        function_check restart_site
187
+        restart_site
188
+    else
189
+        echo $"Smilodon domain specified but not found in $SMILODON_PATH"
190
+    fi
191
+}
192
+
193
+function restore_remote_smilodon {
194
+    temp_restore_dir=/root/tempsmilodon
195
+    if grep -q "smilodon domain" $COMPLETION_FILE; then
196
+        echo $"Restoring smilodon"
197
+        systemctl stop smilodon
198
+
199
+        function_check restore_database_from_friend
200
+
201
+        function_check smilodon_create_database
202
+        smilodon_create_database
203
+
204
+        USE_MONGODB=1
205
+        restore_database_from_friend smilodon
206
+
207
+        if [ -d $SMILODON_PATH ]; then
208
+            if [ -d $temp_restore_dir$SMILODON_PATH ]; then
209
+                rm -rf $SMILODON_PATH
210
+                mv $temp_restore_dir$SMILODON_PATH ${SMILODON_PATH}/
211
+            else
212
+                cp -r $temp_restore_dir/* ${SMILODON_PATH}/
213
+            fi
214
+            if [ ! "$?" = "0" ]; then
215
+                systemctl start smilodon
216
+                exit 6391643
217
+            fi
218
+        fi
219
+
220
+        if [ -d $SERVER_DIRECTORY/backup/smilodon ]; then
221
+            chown -R smilodon:smilodon ${SMILODON_PATH}
222
+        fi
223
+        if [ -d /root/tempsmilodon ]; then
224
+            rm -rf /root/tempsmilodon
225
+        fi
226
+
227
+        systemctl start smilodon
228
+
229
+        echo $"Restore of smilodon complete"
230
+    fi
231
+}
232
+
233
+function remove_smilodon {
234
+    nginx_dissite smilodon
235
+
236
+    systemctl stop smilodon
237
+    systemctl disable smilodon
238
+    rm /etc/systemd/system/smilodon.service
239
+
240
+    function_check remove_onion_service
241
+    remove_onion_service smilodon ${SMILODON_ONION_PORT}
242
+    if [ -f /etc/nginx/sites-available/smilodon ]; then
243
+        rm /etc/nginx/sites-available/smilodon
244
+    fi
245
+
246
+    groupdel -f smilodon
247
+    userdel -r smilodon
248
+
249
+    if [ -d $SMILODON_PATH ]; then
250
+        rm -rf $SMILODON_PATH
251
+    fi
252
+
253
+    function_check remove_mongodb_user
254
+    remove_mongodb_user smilodon
255
+
256
+    function_check drop_database_mongodb
257
+    drop_database_mongodb smilodon
258
+
259
+    remove_completion_param install_smilodon
260
+    sed -i '/smilodon_/d' $COMPLETION_FILE
261
+}
262
+
263
+function install_smilodon {
264
+    apt-get -yq install python3-pip
265
+
266
+    if [ -d $SMILODON_PATH ]; then
267
+        rm -rf $SMILODON_PATH
268
+    fi
269
+
270
+    if [ -d /repos/smilodon ]; then
271
+        mkdir $SMILODON_PATH
272
+        cp -r -p /repos/smilodon/. $SMILODON_PATH
273
+        cd $SMILODON_PATH
274
+        git pull
275
+    else
276
+        function_check git_clone
277
+        git_clone $SMILODON_REPO $SMILODON_PATH
278
+    fi
279
+
280
+    if [ ! -d $SMILODON_PATH ]; then
281
+        echo $'Could not clone smilodon repo'
282
+        exit 6784783
283
+    fi
284
+    cd $SMILODON_PATH
285
+    git checkout $SMILODON_COMMIT -b $SMILODON_COMMIT
286
+    set_completion_param "smilodon commit" "$SMILODON_COMMIT"
287
+
288
+    groupadd smilodon
289
+    useradd -c "Smilodon system account" -d $SMILODON_PATH -m -r -g smilodon smilodon
290
+
291
+    function_check install_mongodb
292
+    install_mongodb
293
+
294
+    smilodon_create_database
295
+
296
+    SMILODON_ONION_HOSTNAME=$(add_onion_service smilodon 80 ${SMILODON_ONION_PORT})
297
+
298
+    pip3 install -r requirements.txt
299
+    if [ ! "$?" = "0" ]; then
300
+        echo $'Unable to install smilodon dependencies'
301
+        exit 87352835
302
+    fi
303
+
304
+    echo 'server {' > /etc/nginx/sites-available/smilodon
305
+    echo "  listen 127.0.0.1:$SMILODON_ONION_PORT;" >> /etc/nginx/sites-available/smilodon
306
+    echo "  server_name $SMILODON_ONION_HOSTNAME;" >> /etc/nginx/sites-available/smilodon
307
+    echo '' >> /etc/nginx/sites-available/smilodon
308
+    echo '  access_log /dev/null;' >> /etc/nginx/sites-available/smilodon
309
+    echo '  error_log /dev/null;' >> /etc/nginx/sites-available/smilodon
310
+    echo '' >> /etc/nginx/sites-available/smilodon
311
+    echo '  location / {' >> /etc/nginx/sites-available/smilodon
312
+    echo '      proxy_pass http://localhost:5000;' >> /etc/nginx/sites-available/smilodon
313
+    echo '  }' >> /etc/nginx/sites-available/smilodon
314
+    echo '' >> /etc/nginx/sites-available/smilodon
315
+    echo '}' >> /etc/nginx/sites-available/smilodon
316
+
317
+    nginx_ensite smilodon
318
+    systemctl enable mongodb
319
+    systemctl restart mongodb
320
+    systemctl restart nginx
321
+
322
+    chown -R smilodon:smilodon ${SMILODON_PATH}
323
+
324
+    if [ ! $SMILODON_SECRET_KEY ]; then
325
+        SMILODON_SECRET_KEY="$(create_password 30)$(create_password 30)$(create_password 30)$(create_password 30)"
326
+    fi
327
+
328
+    echo '[Unit]' > /etc/systemd/system/smilodon.service
329
+    echo 'Description=Smilodon ActivityPub messenger' >> /etc/systemd/system/smilodon.service
330
+    echo 'After=network.target mongodb.service' >> /etc/systemd/system/smilodon.service
331
+    echo 'After=tor.service' >> /etc/systemd/system/smilodon.service
332
+    echo '' >> /etc/systemd/system/smilodon.service
333
+    echo '[Service]' >> /etc/systemd/system/smilodon.service
334
+    echo 'User=smilodon' >> /etc/systemd/system/smilodon.service
335
+    echo 'Group=smilodon' >> /etc/systemd/system/smilodon.service
336
+    echo "WorkingDirectory=${SMILODON_PATH}/" >> /etc/systemd/system/smilodon.service
337
+    echo "ExecStart=/usr/bin/python3 run.py" >> /etc/systemd/system/smilodon.service
338
+    echo "Environment=smilodon_domain_name=$SMILODON_ONION_HOSTNAME" >> /etc/systemd/system/smilodon.service
339
+    echo "Environment=secret_key='$SMILODON_SECRET_KEY'" >> /etc/systemd/system/smilodon.service
340
+    echo "Environment=mongodb_username='smilodon'" >> /etc/systemd/system/smilodon.service
341
+    echo "Environment=mongodb_password='$SMILODON_ADMIN_PASSWORD'" >> /etc/systemd/system/smilodon.service
342
+    echo "Environment=smilodon_admin_address=$MY_EMAIL_ADDRESS" >> /etc/systemd/system/smilodon.service
343
+    echo "Environment=MAIL_SERVER='localhost'" >> /etc/systemd/system/smilodon.service
344
+    echo "Environment=MAIL_PORT=25" >> /etc/systemd/system/smilodon.service
345
+    echo '' >> /etc/systemd/system/smilodon.service
346
+    echo '[Install]' >> /etc/systemd/system/smilodon.service
347
+    echo 'WantedBy=multi-user.target' >> /etc/systemd/system/smilodon.service
348
+    systemctl enable smilodon
349
+    systemctl daemon-reload
350
+    systemctl start smilodon
351
+
352
+    ${PROJECT_NAME}-pass -u $MY_USERNAME -a smilodon -p "$SMILODON_ADMIN_PASSWORD"
353
+
354
+    APP_INSTALLED=1
355
+}
356
+
357
+# NOTE: deliberately no exit 0

+ 85
- 29
src/freedombone-utils-backup Ver arquivo

@@ -236,15 +236,29 @@ function backup_database_local_usb {
236 236
         mkdir -p ${local_database_dir}
237 237
     fi
238 238
     echo $"Obtaining ${1} database backup"
239
+    database_file_extension='sql'
240
+    if [ $USE_MONGODB ]; then
241
+        database_file_extension='mdb'
242
+        USE_POSTGRESQL=
243
+    fi
239 244
     if [ ! $USE_POSTGRESQL ]; then
240
-        keep_database_running
241
-        mysqldump --lock-tables --password="$DATABASE_PASSWORD" ${1} > ${local_database_dir}/${1}.sql
245
+        if [ ! $USE_MONGODB ]; then
246
+            USE_MONGODB=
247
+            USE_POSTGRESQL=
248
+            keep_database_running
249
+            mysqldump --lock-tables --password="$DATABASE_PASSWORD" ${1} > ${local_database_dir}/${1}.${database_file_extension}
250
+        else
251
+            USE_MONGODB=
252
+            USE_POSTGRESQL=
253
+            mongodump --db ${1} --archive=${local_database_dir}/${1}.${database_file_extension} --gzip
254
+        fi
242 255
     else
256
+        USE_MONGODB=
243 257
         USE_POSTGRESQL=
244
-        sudo -u postgres pg_dump ${1} > ${local_database_dir}/${1}.sql
258
+        sudo -u postgres pg_dump ${1} > ${local_database_dir}/${1}.${database_file_extension}
245 259
     fi
246
-    if [ -f ${local_database_dir}/${1}.sql ]; then
247
-        if [ ! -s ${local_database_dir}/${1}.sql ]; then
260
+    if [ -f ${local_database_dir}/${1}.${database_file_extension} ]; then
261
+        if [ ! -s ${local_database_dir}/${1}.${database_file_extension} ]; then
248 262
             echo $"${1} database could not be saved"
249 263
             shred -zu ${local_database_dir}/*
250 264
             rm -rf ${local_database_dir}
@@ -552,16 +566,30 @@ function backup_database_remote {
552 566
     fi
553 567
 
554 568
     echo "Obtaining ${1} database backup"
569
+    database_file_extension='sql'
570
+    if [ $USE_MONGODB ]; then
571
+        database_file_extension='mdb'
572
+        USE_POSTGRESQL=
573
+    fi
555 574
     if [ ! $USE_POSTGRESQL ]; then
556
-        keep_database_running
557
-        mysqldump --lock-tables --password="$DATABASE_PASSWORD" ${1} > ${local_database_dir}/${1}.sql
575
+        if [ ! $USE_MONGODB ]; then
576
+            USE_MONGODB=
577
+            USE_POSTGRESQL=
578
+            keep_database_running
579
+            mysqldump --lock-tables --password="$DATABASE_PASSWORD" ${1} > ${local_database_dir}/${1}.${database_file_extension}
580
+        else
581
+            USE_MONGODB=
582
+            USE_POSTGRESQL=
583
+            mongodump --db ${1} --archive=${local_database_dir}/${1}.${database_file_extension} --gzip
584
+        fi
558 585
     else
586
+        USE_MONGODB=
559 587
         USE_POSTGRESQL=
560
-        sudo -u postgres pg_dump ${1} > ${local_database_dir}/${1}.sql
588
+        sudo -u postgres pg_dump ${1} > ${local_database_dir}/${1}.${database_file_extension}
561 589
     fi
562 590
 
563
-    if [ -f ${local_database_dir}/${1}.sql ]; then
564
-        if [ ! -s ${local_database_dir}/${1}.sql ]; then
591
+    if [ -f ${local_database_dir}/${1}.${database_file_extension} ]; then
592
+        if [ ! -s ${local_database_dir}/${1}.${database_file_extension} ]; then
565 593
             echo $"${1} database could not be saved"
566 594
             shred -zu ${local_database_dir}/*
567 595
             rm -rf ${local_database_dir}
@@ -569,7 +597,7 @@ function backup_database_remote {
569 597
             echo $"Unable to export ${1} database" | mail -s $"${PROJECT_NAME} backup to friends" $ADMIN_EMAIL_ADDRESS
570 598
             function_check restart_site
571 599
             restart_site
572
-            exit 5738
600
+            exit 57386728
573 601
         fi
574 602
     else
575 603
         echo $"${1} database could not be dumped"
@@ -578,7 +606,7 @@ function backup_database_remote {
578 606
         echo $"Unable to dump ${1} database" | mail -s $"${PROJECT_NAME} backup to friends" $ADMIN_EMAIL_ADDRESS
579 607
         function_check restart_site
580 608
         restart_site
581
-        exit 3687
609
+        exit 36874289
582 610
     fi
583 611
 }
584 612
 
@@ -642,33 +670,47 @@ function restore_database_from_friend {
642 670
     RESTORE_SUBDIR="root"
643 671
 
644 672
     if [ -d $SERVER_DIRECTORY/backup/${1} ]; then
673
+        database_file_extension='sql'
674
+        if [ $USE_MONGODB ]; then
675
+            database_file_extension='mdb'
676
+            USE_POSTGRESQL=
677
+        fi
645 678
         echo $"Restoring ${1} database"
646 679
         local_database_dir=/root/temp${1}data
647 680
         restore_directory_from_friend ${local_database_dir} ${1}data
648
-        database_file=${local_database_dir}/${RESTORE_SUBDIR}/temp${restore_app_name}data/${restore_app_name}.sql
681
+        database_file=${local_database_dir}/${RESTORE_SUBDIR}/temp${restore_app_name}data/${restore_app_name}.${database_file_extension}
649 682
         if [ ! -f $database_file ]; then
650
-            database_file=${local_database_dir}/${restore_app_name}.sql
683
+            database_file=${local_database_dir}/${restore_app_name}.${database_file_extension}
651 684
         fi
652 685
         if [ ! -f $database_file ]; then
653 686
             echo $"Unable to restore ${1} database"
654 687
             rm -rf ${local_database_dir}
655
-            exit 503
688
+            exit 5289252
656 689
         fi
657 690
         if [ ! $USE_POSTGRESQL ]; then
658
-            keep_database_running
659
-            mysqlsuccess=$(mysql -u root --password="$DATABASE_PASSWORD" ${1} -o < ${local_database_dir}/${RESTORE_SUBDIR}/temp${1}data/${1}.sql)
691
+            if [ ! $USE_MONGODB ]; then
692
+                USE_MONGODB=
693
+                USE_POSTGRESQL=
694
+                keep_database_running
695
+                mysqlsuccess=$(mysql -u root --password="$DATABASE_PASSWORD" ${restore_app_name} -o < ${database_file})
696
+            else
697
+                USE_MONGODB=
698
+                USE_POSTGRESQL=
699
+                mongorestore --gzip --archive=${database_file} --db ${restore_app_name}
700
+            fi
660 701
         else
702
+            USE_MONGODB=
661 703
             USE_POSTGRESQL=
662
-            mysqlsuccess=$(sudo -u postgres pg_restore ${local_database_dir}/${RESTORE_SUBDIR}/temp${1}data/${1}.sql)
704
+            mysqlsuccess=$(sudo -u postgres pg_restore ${database_file})
663 705
         fi
664 706
         if [ ! "$?" = "0" ]; then
665 707
             echo "$mysqlsuccess"
666
-            exit 964
708
+            exit 8735271
667 709
         fi
668 710
         if [ -d ${local_database_dir}/${RESTORE_SUBDIR}/temp${1}data ]; then
669 711
             shred -zu ${local_database_dir}/${RESTORE_SUBDIR}/temp${1}data/*
670 712
         else
671
-            shred -zu ${local_database_dir}/*.sql
713
+            shred -zu ${local_database_dir}/*.${database_file_extension}
672 714
         fi
673 715
         rm -rf ${local_database_dir}
674 716
         echo $"Restoring ${1} installation"
@@ -695,7 +737,7 @@ function restore_database_from_friend {
695 737
                         cp -r $restore_from_dir/* /var/www/${2}/htdocs/
696 738
                     fi
697 739
                     if [ ! "$?" = "0" ]; then
698
-                        exit 683
740
+                        exit 78352682
699 741
                     fi
700 742
                     if [ -d /etc/letsencrypt/live/${2} ]; then
701 743
                         ln -s /etc/letsencrypt/live/${2}/privkey.pem /etc/ssl/private/${2}.key
@@ -725,9 +767,14 @@ function restore_database {
725 767
         fi
726 768
         function_check restore_directory_from_usb
727 769
         restore_directory_from_usb "${local_database_dir}" "${restore_app_name}data"
728
-        database_file=${local_database_dir}/${RESTORE_SUBDIR}/temp${restore_app_name}data/${restore_app_name}.sql
770
+        database_file_extension='sql'
771
+        if [ $USE_MONGODB ]; then
772
+            database_file_extension='mdb'
773
+            USE_POSTGRESQL=
774
+        fi
775
+        database_file=${local_database_dir}/${RESTORE_SUBDIR}/temp${restore_app_name}data/${restore_app_name}.${database_file_extension}
729 776
         if [ ! -f $database_file ]; then
730
-            database_file=${local_database_dir}/${restore_app_name}.sql
777
+            database_file=${local_database_dir}/${restore_app_name}.${database_file_extension}
731 778
         fi
732 779
         if [ ! -f $database_file ]; then
733 780
             echo $"Unable to restore ${restore_app_name} database"
@@ -736,12 +783,21 @@ function restore_database {
736 783
             set_user_permissions
737 784
             function_check backup_unmount_drive
738 785
             backup_unmount_drive
739
-            exit 503
786
+            exit 7825235
740 787
         fi
741 788
         if [ ! $USE_POSTGRESQL ]; then
742
-            keep_database_running
743
-            mysqlsuccess=$(mysql -u root --password="$DATABASE_PASSWORD" ${restore_app_name} -o < $database_file)
789
+            if [ ! $USE_MONGODB ]; then
790
+                USE_MONGODB=
791
+                USE_POSTGRESQL=
792
+                keep_database_running
793
+                mysqlsuccess=$(mysql -u root --password="$DATABASE_PASSWORD" ${restore_app_name} -o < $database_file)
794
+            else
795
+                USE_MONGODB=
796
+                USE_POSTGRESQL=
797
+                mongorestore --gzip --archive=$database_file --db ${restore_app_name}
798
+            fi
744 799
         else
800
+            USE_MONGODB=
745 801
             USE_POSTGRESQL=
746 802
             mysqlsuccess=$(sudo -u postgres pg_restore $database_file)
747 803
         fi
@@ -751,12 +807,12 @@ function restore_database {
751 807
             set_user_permissions
752 808
             function_check set_user_permissions
753 809
             backup_unmount_drive
754
-            exit 964
810
+            exit 482638995
755 811
         fi
756 812
         if [ -d ${local_database_dir}/${RESTORE_SUBDIR}/temp${restore_app_name}data ]; then
757 813
             shred -zu ${local_database_dir}/${RESTORE_SUBDIR}/temp${restore_app_name}data/*
758 814
         else
759
-            shred -zu ${local_database_dir}/*.sql
815
+            shred -zu ${local_database_dir}/*.${database_file_extension}
760 816
         fi
761 817
 
762 818
         rm -rf ${local_database_dir}
@@ -789,7 +845,7 @@ function restore_database {
789 845
                     if [ ! "$?" = "0" ]; then
790 846
                         set_user_permissions
791 847
                         backup_unmount_drive
792
-                        exit 683
848
+                        exit 78252429
793 849
                     fi
794 850
                     if [ -d /etc/letsencrypt/live/${restore_app_domain} ]; then
795 851
                         ln -s /etc/letsencrypt/live/${restore_app_domain}/privkey.pem /etc/ssl/private/${restore_app_domain}.key

+ 116
- 0
src/freedombone-utils-mongodb Ver arquivo

@@ -0,0 +1,116 @@
1
+#!/bin/bash
2
+#
3
+# .---.                  .              .
4
+# |                      |              |
5
+# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
6
+# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
7
+# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
8
+#
9
+#                    Freedom in the Cloud
10
+#
11
+# mongodb 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_MONGODB=
33
+
34
+function store_original_mongodb_password {
35
+    if [ ! -f /root/.mongodboriginal ]; then
36
+        echo $'Storing original mongodb password'
37
+        ORIGINAL_MONGODB_PASSWORD=$(${PROJECT_NAME}-pass -u root -a mongodb)
38
+        # We can store this in plaintext because it will soon be of historical interest only
39
+        echo -n "$ORIGINAL_MONGODB_PASSWORD" > /root/.mongodboriginal
40
+    fi
41
+}
42
+
43
+function get_mongodb_password {
44
+    MONGODB_PASSWORD=$(${PROJECT_NAME}-pass -u root -a mongodb)
45
+    if [[ "$MONGODB_PASSWORD" == *'failed'* ]]; then
46
+        echo $'Could not obtain mongodb password'
47
+        exit 7835272
48
+    fi
49
+}
50
+
51
+function install_mongodb {
52
+    if [[ $(is_completed $FUNCNAME) == "1" ]]; then
53
+        return
54
+    fi
55
+
56
+    function_check get_mongodb_password
57
+    get_mongodb_password
58
+    if [ ! $MONGODB_PASSWORD ]; then
59
+        if [ -f $IMAGE_PASSWORD_FILE ]; then
60
+            MONGODB_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
61
+        else
62
+            MONGODB_PASSWORD="$(openssl rand -base64 32 | cut -c1-${MINIMUM_PASSWORD_LENGTH})"
63
+        fi
64
+    fi
65
+    ${PROJECT_NAME}-pass -u root -a mongodb -p "$MONGODB_PASSWORD"
66
+
67
+    apt-get -yq install mongodb mongodb-tools
68
+    apt-get -yq remove --purge apache2-bin*
69
+    if [ -d /etc/apache2 ]; then
70
+        rm -rf /etc/apache2
71
+        echo $'Removed Apache installation after mongodb install'
72
+    fi
73
+
74
+    if [ ! -d /var/lib/mongodb ]; then
75
+        echo $"ERROR: mongodb does not appear to have installed. $CHECK_MESSAGE"
76
+        exit 78352
77
+    fi
78
+
79
+    mark_completed $FUNCNAME
80
+}
81
+
82
+function add_mongodb_user {
83
+    mongodb_username=$1
84
+    mongodb_password=$2
85
+
86
+    mongo admin --eval "db.createUser({user: '$mongodb_username', pwd: '$mongodb_password', roles: [ { role: 'userAdminAnyDatabase', db: 'admin' } ] })"
87
+}
88
+
89
+function remove_mongodb_user {
90
+    mongodb_username=$1
91
+    mongo admin --eval "db.removeUser($mongodb_username)"
92
+}
93
+
94
+function drop_database_mongodb {
95
+    database_name="$1"
96
+    if [[ "$database_name" == 'admin' ]]; then
97
+        return
98
+    fi
99
+    mongo $database_name --eval "db.runCommand( { dropDatabase: 1 } )"
100
+}
101
+
102
+function initialise_database_mongodb {
103
+    database_name=$1
104
+    database_file=$2
105
+    mongorestore $database_file
106
+    if [ ! "$?" = "0" ]; then
107
+        exit 8358365
108
+    fi
109
+}
110
+
111
+function create_database_mongodb {
112
+    app_name="$1"
113
+    app_admin_password="$2"
114
+    app_admin_username=$3
115
+    mongo admin --eval "db.createUser({user: '$app_admin_username', pwd: '$app_admin_password', roles: [ { role: 'dbOwner', db: '$app_name' } ] })"
116
+}