forked from theojulienne/ninjasphere-docker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sphere.sh
executable file
·154 lines (130 loc) · 3.65 KB
/
sphere.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
#!/bin/bash
VERSION=1.2
IMAGE=${IMAGE:-ninjasphere/ninjasphere}
SCRIPT_PATH=$(cd $(dirname $0); pwd)
die() {
echo "$*" 1>&2
exit 1
}
with() {
alternate-cloud() {
local domain=$1
local host=$2
test -n "$host" || die "usage: with alternate-cloud {domain} {host} start ..."
shift 2
WITH_ARGS="-v ${SCRIPT_PATH}/sphere-config/config:/opt/ninjablocks/config -e NINJA_SPHERE_CLIENT_OPTS=--docker --add-host api.${domain}:${host} --add-host mqtt.${domain}:${host}"
test -n "$1" || die "usage: with alternate-cloud 192.168.99.100 start..."
"$@"
}
"$@"
}
start() {
if [[ $NINJA_SERIAL == "" ]]; then
if [ -f volume-data/sphere-serial.conf ]; then
NINJA_SERIAL=$(cat volume-data/sphere-serial.conf)
fi
fi
if [[ $NINJA_SERIAL == "" ]]; then
echo "Serial number must be specified the first time: $0 init <serial>"
exit 1
fi
mkdir -p volume-data
init "$NINJA_SERIAL"
docker run -d -it \
-v $(pwd)/volume-data:/data \
--env NINJA_SERIAL=$NINJA_SERIAL \
--name=ninjasphere \
-p 1883:1883 -p 8000:8000 -p 80:9080 -p 9001:9001 \
${WITH_ARGS} \
"$@" \
${IMAGE}
echo 'Sphere service launched.'
}
stop() {
docker kill ninjasphere
docker rm ninjasphere
}
ps() {
docker exec ninjasphere ps aux
}
interact() {
docker exec -it ninjasphere bash
}
logs() {
docker "$@" ninjasphere
}
run-driver() {
export sphere_installDirectory=$SCRIPT_PATH
export PATH=$PATH:$SCRIPT_PATH/driver-bin
if [[ "$1" == "" ]]; then
echo "You must specify the path to the driver"
exit 1
fi
if test -z "$MQTT_HOST" && which boot2docker >/dev/null; then
MQTT_HOST=$(boot2docker ip 2>/dev/null)
else
MQTT_HOST=${MQTT_HOST:-127.0.0.1}
fi
"$@" --mqtt.host=$MQTT_HOST --mqtt.port=1883
}
init() {
serial=${1:-$(sphere-go-serial)}
test -n "$serial" || die "init: must specify a serial number"
mkdir -p volume-data
echo -n "$serial" > volume-data/sphere-serial.conf
echo "NINJA_SERIAL=$serial;" > volume-data/sphere-serial.env
}
serial() {
test -f volume-data/sphere-serial.conf && echo $(cat volume-data/sphere-serial.conf) || die "not initialized"
}
case "$1" in
start)
shift 1
start "$@"
;;
stop)
stop
;;
init)
shift 1
init "$@"
;;
ps)
ps
;;
shell)
interact
;;
logs)
logs "$@"
;;
run-driver)
shift 1
run-driver "$@"
;;
serial)
shift 1
serial "$@"
;;
with)
shift 1
with "$@"
;;
version)
echo "$VERSION"
;;
*)
echo "Usage: $0 start [arg...] -- start the sphere stack, init must have been run first"
echo " $0 init [serial] -- initialize the serial number for the docker-ised sphere"
echo " $0 stop -- stop the sphere stack"
echo " $0 ps -- 'ps aux' inside the container"
echo " $0 shell -- 'bash' inside the container"
echo " $0 serial -- the serial number of the docker-used sphere"
echo " $0 logs [-f] -- show (or follow) the logs"
echo " $0 version -- the version number of the sphere.sh script"
echo ""
echo "To connect to an alternate cloud, run: $0 [ with alternate-cloud {domain} {host} ] start, for example:"
echo ""
echo " $0 with alternate-cloud example.com 192.168.99.100 start"
exit 1
esac