forked from ninjasphere/sphere-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sphere-stack.sh
executable file
·273 lines (244 loc) · 6.46 KB
/
sphere-stack.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/env bash
VERSION=1.3
die() {
echo "$*" 1>&2
exit 1
}
version() {
echo "$VERSION"
}
ip() {
if test -n "$DOCKER_HOST"; then
local ip=${DOCKER_HOST#tcp://};
echo ${ip%:*};
else
echo "127.0.0.1"
fi
}
domain() {
echo ${NINJA_CLOUD_DOMAIN}
}
machine() {
if test -n "$DOCKER_HOST"; then
docker-machine ls | cut -c1-20 | grep "\*\$" | cut -f1 -d' '
else
echo ""
return 1
fi
}
create() {
config() {
mkdir -p config
chmod 0700 config
cd templates
for f in *.sh; do
(. ./$f) > ../config/$(basename $f .sh) || die "died while running template $f"
done
cd ..
}
services() {
docker-compose -p spherestack -f services-docker-compose.yml up -d
}
resources() {
if test -z "$DOCKER_HOST"; then
sudo mkdir -p /var/lib/sphere-stack || die "failed to create sphere-stack"
else
ssh -i ~/.docker/machine/machines/$(machine)/id_rsa docker@$(ip) <<EOF
sudo mkdir -p /mnt/sda1/var/lib/sphere-stack &&
sudo ln -sf /mnt/sda1/var/lib/sphere-stack /var/lib/sphere-stack
EOF
fi
docker-compose -p spherestack -f resources-docker-compose.yml up -d
}
couch() {
docker exec -i spherestack_spherecouch_1 curl -X PUT http://127.0.0.1:5984/sphere_modelstore;
curl -X PUT http://$(ip):5984/sphere_modelstore/_design/manifest -d @manifest.json
}
mysql() {
cat test_data.sql | docker exec -i spherestack_spheremysql_1 mysql -uroot
}
keys() {
mkdir -p haproxy/ssl &&
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout haproxy/ssl/sphere.key -out haproxy/ssl/sphere.crt &&
cat haproxy/ssl/sphere.key haproxy/ssl/sphere.crt >> haproxy/ssl/wildcard.pem
}
"$@"
}
hosts-append() {
local d=$(domain) || exit $?
echo $(ip) \
${NINJA_API_ENDPOINT} \
${NINJA_ID_ENDPOINT} \
mqtt.$d \
""
}
start() {
resource=$1
shift 1
case "$resource" in
resources)
docker-compose -p spherestack -f resources-docker-compose.yml up -d
;;
services)
docker-compose -p spherestack -f services-docker-compose.yml up -d
;;
*)
die "unknown resource: $resource"
;;
esac
}
recreate() {
resource=$1
shift 1
case "$resource" in
resources)
docker-compose -p spherestack -f resources-docker-compose.yml stop
docker-compose -p spherestack -f resources-docker-compose.yml rm
docker-compose -p spherestack -f resources-docker-compose.yml up -d
;;
services)
docker-compose -p spherestack -f services-docker-compose.yml stop
docker-compose -p spherestack -f services-docker-compose.yml rm -f
docker-compose -p spherestack -f services-docker-compose.yml up -d
;;
*)
;;
esac
}
stop() {
resource=$1
shift 1
case "$resource" in
resources)
docker-compose -p spherestack -f resources-docker-compose.yml stop
;;
services)
docker-compose -p spherestack -f services-docker-compose.yml stop
;;
*)
die "unknown resource: $resource"
;;
esac
}
restart() {
resource=$1
shift 1
(stop "$resource" "$@")
start "$resource" "$@"
}
logs() {
resource=$1
shift 1
case "$resource" in
resources)
docker-compose -p spherestack -f resources-docker-compose.yml logs
;;
services)
docker-compose -p spherestack -f services-docker-compose.yml logs
;;
*)
die "unknown resource: $resource"
;;
esac
}
pwgen() {
cat /dev/urandom | dd count=1 bs=256 2>/dev/null | openssl base64 | cut -c1-20 | head -1
}
edit() {
${EDITOR:-vi} .sphere-stack/master
create config
}
update() {
application-table() {
docker exec -i spherestack_spheremysql_1 mysql douitsu -uroot <<EOF
update application set is_ninja_official=1 where appid = '${NINJA_APP_TOKEN}';
EOF
}
"$@"
}
generate() {
master() {
cat <<EOF
NINJA_SIGNING_SECRET=$(pwgen);
NINJA_SESSION_SECRET=$(pwgen);
NINJA_RABBIT_SECRET=$(pwgen);
NINJA_CLOUD_DOMAIN=example.com;
NINJA_API_ENDPOINT=api.\${NINJA_CLOUD_DOMAIN};
NINJA_ID_ENDPOINT=id.\${NINJA_CLOUD_DOMAIN};
NINJA_APP_TOKEN=app_XX;
NINJA_APP_KEY=sk_XX;
EOF
}
"$@"
}
init() {
mkdir -p .sphere-stack
chmod 0700 .sphere-stack
generate master > .sphere-stack/defaults
if ! test -f .sphere-stack/master; then
generate master > .sphere-stack/master
echo "./sphere-stack/master has been initialized"
else
echo "skipping initialization of .sphere-stack/master - existing tokens will be used"
fi
. .sphere-stack/defaults
. .sphere-stack/master
create config
}
usage() {
cat <<EOF
$0 init - initialize the current directory with a default configuration
$0 edit - edit the configuration
$0 create - create the couch database
$0 ip - the ip of the docker machine
$0 domain - the domain of the stack
$0 machine - the machine
$0 generate master - generate the master
$0 create resources - create the resources composition
$0 create services - create the services composition
$0 create couch - create the couch data store
$0 create mysql - create the mysql data store
$0 create keys - create the keys
$0 start [resources|services] - start the specified composition
$0 stop [resources|services] - stop the specified composition
$0 logs [resources|services] - logs from the specified composition
$0 restart [resources|services] - restart the specified composition
$0 version - report the script version
EOF
}
cmd=$1
shift 1
case $cmd in
init|version)
$cmd "$@"
;;
ip|domain|hosts-append|machine|init|edit|update)
test -f .sphere-stack/master || die "run ./sphere-stack.sh init first!"
test -f .sphere-stack/defaults && . .sphere-stack/defaults
. .sphere-stack/master
$cmd "$@"
;;
create|start|stop|logs|recreate)
test -f .sphere-stack/master || die "run ./sphere-stack.sh init first!"
test -f .sphere-stack/defaults && . .sphere-stack/defaults
. .sphere-stack/master
if test "$1" == "all"; then
shift 1
case "$cmd" in
stop)
$cmd services "$@"
$cmd resources "$@"
;;
*)
$cmd resources "$@"
$cmd services "$@"
;;
esac
else
$cmd "$@"
fi
;;
*)
usage
;;
esac