-
Notifications
You must be signed in to change notification settings - Fork 2
/
branch-autorunonce.sh
118 lines (102 loc) · 4.02 KB
/
branch-autorunonce.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash
#variables
name='branch'
ip='192.168.0.99'
communityname='new.local'
communityport='5984'
#for win
#community='192.168.0.99:5984'
community="`getent hosts $communityname | awk '{ print $1 }'`:$communityport"
# load local settings from branch.config file
if [[ -r ./branch.config ]] ; then
source ./branch.config
fi
# rename hostname of image to name
pirateship rename $name
# template for continuous replication for databases
function replicate {
curl -H 'Content-Type: application/json' -X POST http://127.0.0.1:5984/_replicator -d ' {"source": "http://'$community'/'$1'", "target": "http://127.0.0.1:5984/'$1'", "create_target": true, "continuous": true} '
curl -H 'Content-Type: application/json' -X POST http://127.0.0.1:5984/_replicator -d ' {"source": "http://127.0.0.1:5984/'$1'", "target": "http://'$community'/'$1'", "continuous": true} '
#curl -X GET http://$community/$1/_security | xargs curl -H 'Content-Type: application/json' -X PUT http://127.0.0.1:5984/$1/_security -d {}
}
# create couchdb docker container
docker run -d -p 5984:5984 --name $name -v /srv/data/$name:/usr/local/var/lib/couchdb -v /srv/log/$name:/usr/local/var/log/couchdb dogi/rpi-couchdb
wget http://ftp.debian.org/debian/pool/main/j/jq/jq_1.4-1~bpo70+1_armhf.deb
dpkg -i jq_1.4-1~bpo70+1_armhf.deb
while ! curl -X GET http://127.0.0.1:5984/_all_dbs ; do
sleep 1
done
# configurations database
curl -H 'Content-Type: application/json' -X POST http://127.0.0.1:5984/_replicate -d ' {"source": "http://'$community'/configurations", "target": "http://127.0.0.1:5984/configurations", "create_target": true} '
conf=`curl -X GET http://127.0.0.1:5984/configurations/_all_docs | sed '1d;$ d' | jq .id | tr -d '\"'`
doc=`curl -X GET 'http://127.0.0.1:5984/configurations/'$conf | jq '.nationName=""' | jq '.nationUrl=""' | jq '.subType = "branch"' | jq 'with_entries(select(.key != "_id"))'`
curl -X PUT 'http://127.0.0.1:5984/configurations/'$conf -d "$doc"
# branch
replicate activitylog
replicate apps
replicate assignmentpaper
replicate assignments
replicate calendar
replicate collectionlist
replicate communities
replicate community
replicate courseschedule
replicate coursestep
replicate feedback
replicate groups
replicate invitations
replicate languages
replicate mail
replicate meetups
replicate membercourseprogress
replicate nationreports
replicate publicationdistribution
replicate publications
replicate report
replicate requests
replicate resourcefrequency
replicate shelf
replicate usermeetups
replicate members
replicate communityreports
replicate resources
# write '/boot/autrun.sh'
echo '#!/bin/sh' > /boot/autorun.sh
echo '' >> /boot/autorun.sh
echo 'sleep 1' >> /boot/autorun.sh
echo 'docker start '$name >> /boot/autorun.sh
# expand filesystem
cd /usr/local/lib/
npm update
pirateship expandfs
# redirect to bell
mkdir -p /root/ole
echo '#!/usr/bin/env node' > /root/ole/server.js
echo '' >> /root/ole/server.js
echo "var express = require('express')" >> /root/ole/server.js
echo 'var PortJack = express()' >> /root/ole/server.js
echo 'PortJack.get(/^(.+)$/, function(req, res) {' >> /root/ole/server.js
echo 'var options = {' >> /root/ole/server.js
echo '"'$name'.local": "http://'$name'.local:5984/apps/_design/bell/MyApp/index.html",' >> /root/ole/server.js
echo '"'$ip'": "http://'$ip':5984/apps/_design/bell/MyApp/index.html"' >> /root/ole/server.js
echo '}' >> /root/ole/server.js
echo 'if (options.hasOwnProperty(req.hostname)) {' >> /root/ole/server.js
echo "res.setHeader('Location', options[req.hostname])" >> /root/ole/server.js
echo '}' >> /root/ole/server.js
echo 'else {' >> /root/ole/server.js
echo "res.setHeader('Location', 'http://ole.org')" >> /root/ole/server.js
echo '}' >> /root/ole/server.js
echo 'res.statusCode = 302' >> /root/ole/server.js
echo 'res.end()' >> /root/ole/server.js
echo '})' >> /root/ole/server.js
echo 'PortJack.listen(80)' >> /root/ole/server.js
chmod +x /root/ole/server.js
cd /root/ole
npm install express
# add to '/boot/autorun.sh'
echo '' >> /boot/autorun.sh
echo 'node /root/ole/server.js' >> /boot/autorun.sh
sync
sync
sync
reboot