Browse Source

tahoe-lafs

Bob Mottram 8 years ago
parent
commit
71abe7cb5b

+ 243
- 0
src/freedombone-app-tahoelafs View File

@@ -0,0 +1,243 @@
1
+#!/bin/bash
2
+#
3
+# .---.                  .              .
4
+# |                      |              |
5
+# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
6
+# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
7
+# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
8
+#
9
+#                    Freedom in the Cloud
10
+#
11
+# Distributed storage
12
+#
13
+# License
14
+# =======
15
+#
16
+# Copyright (C) 2014-2016 Bob Mottram <bob@robotics.uk.to>
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='mesh'
32
+
33
+TAHOELAFS_PORT=50213
34
+TAHOELAFS_WEB_PORT=3456
35
+TAHOELAFS_STORAGE_SPACE=1G
36
+TAHOELAFS_SHARED_DIR='Shared'
37
+
38
+function reconfigure_tahoelafs {
39
+    for d in /home/*/ ; do
40
+        USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
41
+        if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then
42
+            if [ -d /home/$USERNAME/.tahoe ]; then
43
+                su -c 'tahoe stop' - $USERNAME
44
+                rm -rf /home/$USERNAME/.tahoe
45
+            fi
46
+            if [ -d /home/$USERNAME/.tahoe-introducer ]; then
47
+                rm -rf /home/$USERNAME/.tahoe-introducer
48
+            fi
49
+
50
+            if [ -d /home/$USERNAME/${TAHOELAFS_SHARED_DIR} ]; then
51
+                rm -rf /home/$USERNAME/${TAHOELAFS_SHARED_DIR}
52
+            fi
53
+
54
+            su -c 'tahoe create-node' - $USERNAME
55
+
56
+            if [ ! -d /home/$USERNAME/.tahoe ]; then
57
+                exit 63722
58
+            fi
59
+
60
+            su -c 'tahoe create-introducer ~/.tahoe-introducer' - $USERNAME
61
+
62
+            if [ ! -d /home/$USERNAME/.tahoe-introducer ]; then
63
+                exit 365272
64
+            fi
65
+
66
+            # create a shared directory
67
+            if [ ! -d /home/$USERNAME/${TAHOELAFS_SHARED_DIR} ]; then
68
+                mkdir /home/$USERNAME/${TAHOELAFS_SHARED_DIR}
69
+                chown -R $USERNAME:$USERNAME /home/$USERNAME/${TAHOELAFS_SHARED_DIR}
70
+            fi
71
+
72
+            TAHOELAFS_CONFIG=/home/$USERNAME/.tahoe/tahoe.cfg
73
+            echo '[node]' > $TAHOELAFS_CONFIG
74
+            echo "nickname = $USERNAME" >> $TAHOELAFS_CONFIG
75
+            echo "web.port = tcp:$TAHOELAFS_WEB_PORT:interface=127.0.0.1" >> $TAHOELAFS_CONFIG
76
+            echo 'web.static = public_html' >> $TAHOELAFS_CONFIG
77
+            echo "tub.port = tcp:$TAHOELAFS_PORT" >> $TAHOELAFS_CONFIG
78
+            echo "tub.location = tcp:$HOSTNAME:$TAHOELAFS_PORT" >> $TAHOELAFS_CONFIG
79
+            echo 'timeout.keepalive = 240' >> $TAHOELAFS_CONFIG
80
+            echo 'timeout.disconnect = 1800' >> $TAHOELAFS_CONFIG
81
+            echo '' >> $TAHOELAFS_CONFIG
82
+            echo '[client]' >> $TAHOELAFS_CONFIG
83
+            echo 'introducer.furl = None' >> $TAHOELAFS_CONFIG
84
+            echo '#helper.furl =' >> $TAHOELAFS_CONFIG
85
+            echo '' >> $TAHOELAFS_CONFIG
86
+            echo 'shares.needed = 1' >> $TAHOELAFS_CONFIG
87
+            echo 'shares.happy = 1' >> $TAHOELAFS_CONFIG
88
+            echo 'shares.total = 1' >> $TAHOELAFS_CONFIG
89
+            echo '' >> $TAHOELAFS_CONFIG
90
+            echo '[storage]' >> $TAHOELAFS_CONFIG
91
+            echo 'enabled = true' >> $TAHOELAFS_CONFIG
92
+            echo "reserved_space = $TAHOELAFS_STORAGE_SPACE" >> $TAHOELAFS_CONFIG
93
+            echo '#expire.enabled = true' >> $TAHOELAFS_CONFIG
94
+            echo '#expire.mode =' >> $TAHOELAFS_CONFIG
95
+            echo '' >> $TAHOELAFS_CONFIG
96
+            echo '[helper]' >> $TAHOELAFS_CONFIG
97
+            echo 'enabled = false' >> $TAHOELAFS_CONFIG
98
+            echo '' >> $TAHOELAFS_CONFIG
99
+            echo '[drop_upload]' >> $TAHOELAFS_CONFIG
100
+            echo 'enabled = true' >> $TAHOELAFS_CONFIG
101
+            echo "local.directory = ~/${TAHOELAFS_SHARED_DIR}" >> $TAHOELAFS_CONFIG
102
+            chown $USERNAME:$USERNAME $TAHOELAFS_CONFIG
103
+
104
+            su -c 'tahoe start ~/.tahoe-introducer' - $USERNAME
105
+            su -c 'tahoe start' - $USERNAME
106
+        fi
107
+    done
108
+}
109
+
110
+function upgrade_tahoelafs {
111
+    echo -n ''
112
+    # TODO
113
+}
114
+
115
+function backup_local_tahoelafs {
116
+    echo -n ''
117
+    # TODO
118
+}
119
+
120
+function restore_local_tahoelafs {
121
+    echo -n ''
122
+    # TODO
123
+}
124
+
125
+function backup_remote_tahoelafs {
126
+    echo -n ''
127
+    # TODO
128
+}
129
+
130
+function restore_remote_tahoelafs {
131
+    echo -n ''
132
+    # TODO
133
+}
134
+
135
+function remove_tahoelafs {
136
+    if ! grep -Fxq "install_tahoelafs" $COMPLETION_FILE; then
137
+        return
138
+    fi
139
+    iptables -D INPUT -p udp --dport $TAHOELAFS_PORT -j ACCEPT
140
+    iptables -D INPUT -p tcp --dport $TAHOELAFS_PORT -j ACCEPT
141
+    function_check save_firewall_settings
142
+    save_firewall_settings
143
+
144
+    apt-get -y remove --purge tahoe-lafs
145
+
146
+    sed -i '/install_tahoelafs/d' $COMPLETION_FILE
147
+    sed -i '/configure_firewall_for_tahoelafs/d' $COMPLETION_FILE
148
+}
149
+
150
+function configure_firewall_for_tahoelafs {
151
+    if grep -Fxq "configure_firewall_for_tahoelafs" $COMPLETION_FILE; then
152
+        return
153
+    fi
154
+    iptables -A INPUT -p udp --dport $TAHOELAFS_PORT -j ACCEPT
155
+    iptables -A INPUT -p tcp --dport $TAHOELAFS_PORT -j ACCEPT
156
+    function_check save_firewall_settings
157
+    save_firewall_settings
158
+    OPEN_PORTS+=("Tahoe-LAFS $TAHOELAFS_PORT")
159
+    echo 'configure_firewall_for_tahoelafs' >> $COMPLETION_FILE
160
+}
161
+
162
+function tahoelafs_update_script {
163
+    update_script_filename=$1
164
+
165
+    echo '#!/bin/bash' > $update_script_filename
166
+    echo 'PEERS_FILE=$(mktemp /tmp/tahoelafs-peers.XXXXXX)' >> $update_script_filename
167
+    echo -n 'avahi-browse -atl | grep "Workstation" | ' >> $update_script_filename
168
+    echo -n "awk -F ' ' '{print \$4}' " >> $update_script_filename
169
+    echo '| sort -u > $PEERS_FILE' >> $update_script_filename
170
+    echo 'if [ ! "$?" = "0" ]; then' >> $update_script_filename
171
+    echo '    exit 1' >> $update_script_filename
172
+    echo 'fi' >> $update_script_filename
173
+    echo '' >> $update_script_filename
174
+    echo 'if [ ! -f $PEERS_FILE ]; then' >> $update_script_filename
175
+    echo '    exit 0' >> $update_script_filename
176
+    echo 'fi' >> $update_script_filename
177
+    echo '' >> $update_script_filename
178
+    echo 'furl=""' >> $update_script_filename
179
+    echo 'while IFS="" read -r line || [[ -n "$line" ]]; do' >> $update_script_filename
180
+    echo '    if [[ $furl != "" ]]; then' >> $update_script_filename
181
+    echo -n '        furl="$furl,$line:' >> $update_script_filename
182
+    echo -n "$TAHOELAFS_PORT" >> $update_script_filename
183
+    echo '"' >> $update_script_filename
184
+    echo '    else' >> $update_script_filename
185
+    echo -n '        furl="$line:' >> $update_script_filename
186
+    echo -n "$TAHOELAFS_PORT" >> $update_script_filename
187
+    echo '"' >> $update_script_filename
188
+    echo '    fi' >> $update_script_filename
189
+    echo 'done < "$PEERS_FILE"' >> $update_script_filename
190
+    echo '' >> $update_script_filename
191
+    echo 'rm $PEERS_FILE' >> $update_script_filename
192
+    echo '' >> $update_script_filename
193
+    echo 'for d in /home/*/ ; do' >> $update_script_filename
194
+    echo -n '    USERNAME=$(echo "$d" | ' >> $update_script_filename
195
+    echo "awk -F '/' '{print \$3}')'" >> $update_script_filename
196
+    echo '    if [ -f /home/$USERNAME/.tahoe/tahoe.cfg ]; then' >> $update_script_filename
197
+    echo '        if ! grep -q "introducer.furl = ${furl}" /home/$USERNAME/.tahoe/tahoe.cfg; then' >> $update_script_filename
198
+    echo '            sed -i "s|introducer.furl =.*|introducer.furl = ${furl}|g" /home/$USERNAME/.tahoe/tahoe.cfg' >> $update_script_filename
199
+    echo '            chown $USERNAME:$USERNAME /home/$USERNAME/.tahoe/tahoe.cfg' >> $update_script_filename
200
+    echo '            su -c "tahoe restart" - $USERNAME' >> $update_script_filename
201
+    echo '        fi' >> $update_script_filename
202
+    echo '    fi' >> $update_script_filename
203
+    echo 'done' >> $update_script_filename
204
+    echo '' >> $update_script_filename
205
+    echo 'exit 0' >> $update_script_filename
206
+    chmod +x $update_script_filename
207
+}
208
+
209
+function mesh_install_tahoelafs {
210
+    chroot "$rootdir" apt-get -y install tahoe-lafs
211
+    chroot "$rootdir" su -c 'tahoe create-client' - $MY_USERNAME
212
+    if [ ! -d $rootdir/home/$MY_USERNAME/.tahoe ]; then
213
+        exit 63722
214
+    fi
215
+    tahoelafs_update_script $rootdir/usr/bin/update-tahoelafs
216
+
217
+    if ! grep -q "update-tahoelafs" $rootdir/etc/crontab; then
218
+        echo "*/1            * *   *   *   root /usr/bin/update-tahoelafs 2> /dev/null" >> $rootdir/etc/crontab
219
+    fi
220
+}
221
+
222
+function install_tahoelafs {
223
+    if [ $INSTALLING_MESH ]; then
224
+        mesh_install_tahoelafs
225
+        return
226
+    fi
227
+
228
+    if grep -Fxq "install_tahoelafs" $COMPLETION_FILE; then
229
+        return
230
+    fi
231
+
232
+    apt-get -y install tahoe-lafs
233
+    configure_firewall_for_tahoelafs
234
+    reconfigure_tahoelafs
235
+    tahoelafs_update_script /usr/bin/update-tahoelafs
236
+
237
+    function_check cron_add_mins
238
+    cron_add_mins 1 '/usr/bin/update-tahoelafs 2> /dev/null'
239
+
240
+    echo 'install_tahoelafs' >> $COMPLETION_FILE
241
+}
242
+
243
+# NOTE: deliberately no exit 0

+ 1
- 0
src/freedombone-image-customise View File

@@ -555,6 +555,7 @@ initialise_mesh() {
555 555
     install_avahi
556 556
     install_batman
557 557
     install_tomb
558
+    install_tahoelafs
558 559
     #install_librevault
559 560
     #install_ipfs
560 561
     install_tox

+ 10
- 0
src/freedombone-image-mesh View File

@@ -745,6 +745,15 @@ function setup_ipfs {
745 745
     echo 'IPFS installed with ID $IPFS_PEER_ID' >> $INSTALL_LOG
746 746
 }
747 747
 
748
+function setup_tahoelafs {
749
+    reconfigure_tahoelafs
750
+
751
+    TAHOELAFS_CONFIG=/home/${MY_USERNAME}/.tahoe/tahoe.cfg
752
+    if [ ! -f ${TAHOELAFS_CONFIG} ]; then
753
+        exit 673923
754
+    fi
755
+}
756
+
748 757
 # whether to reset the identity
749 758
 set_new_identity=
750 759
 if [ $2 ]; then
@@ -800,6 +809,7 @@ if [ -f $MESH_INSTALL_SETUP ]; then
800 809
     fi
801 810
     configure_toxcore
802 811
     create_tox_user
812
+    setup_tahoelafs
803 813
     setup_ipfs
804 814
     mesh_amnesic
805 815
 

+ 3
- 0
src/freedombone-mesh-batman View File

@@ -64,6 +64,7 @@ IPFS_PORT=4001
64 64
 TOX_PORT=33445
65 65
 TRACKER_PORT=6969
66 66
 LIBREVAULT_PORT=42345
67
+TAHOELAFS_PORT=50213
67 68
 
68 69
 # Ethernet bridge definition (bridged to bat0)
69 70
 BRIDGE=br-mesh
@@ -144,6 +145,7 @@ function stop {
144 145
     iptables -D INPUT -p udp --dport $TOX_PORT -j ACCEPT
145 146
     iptables -D INPUT -p tcp --dport $LIBREVAULT_PORT -j ACCEPT
146 147
     iptables -D INPUT -p udp --dport $LIBREVAULT_PORT -j ACCEPT
148
+    iptables -D INPUT -p tcp --dport $TAHOELAFS_PORT -j ACCEPT
147 149
 
148 150
     systemctl restart network-manager
149 151
 }
@@ -251,6 +253,7 @@ function start {
251 253
     iptables -A INPUT -p udp --dport $TOX_PORT -j ACCEPT
252 254
     iptables -A INPUT -p tcp --dport $LIBREVAULT_PORT -j ACCEPT
253 255
     iptables -A INPUT -p udp --dport $LIBREVAULT_PORT -j ACCEPT
256
+    iptables -A INPUT -p tcp --dport $TAHOELAFS_PORT -j ACCEPT
254 257
 
255 258
     systemctl restart avahi-daemon
256 259