zerotier: fix 'unknown operand' error
[feed/packages.git] / net / zerotier / files / zerotier.init
1 #!/bin/sh /etc/rc.common
2
3 START=90
4
5 USE_PROCD=1
6
7 PROG=/usr/bin/zerotier-one
8 CONFIG_PATH=/var/lib/zerotier-one
9
10 section_enabled() {
11 config_get_bool enabled "$1" 'enabled' 0
12 [ $enabled -gt 0 ]
13 }
14
15 start_instance() {
16 local cfg="$1"
17 local port secret interface config_path
18 local ARGS=""
19
20 section_enabled "$cfg" || return 1
21
22 config_get config_path $cfg 'config_path'
23 config_get_bool port $cfg 'port'
24 config_get secret $cfg 'secret'
25 config_get interface $cfg 'interface'
26
27 # Remove existing link or folder
28 rm -rf $CONFIG_PATH
29
30 # Create link from CONFIG_PATH to config_path
31 if [ -n "$config_path" -a "$config_path" != $CONFIG_PATH ]; then
32 if [ ! -d "$config_path" ]; then
33 echo "ZeroTier config_path does not exist: $config_path"
34 return
35 fi
36
37 ln -s $config_path $CONFIG_PATH
38 fi
39
40 mkdir -p $CONFIG_PATH/networks.d
41
42 if [ -n "$port" ]; then
43 ARGS="$ARGS -p$port"
44 fi
45
46 if [ "$secret" = "generate" ]; then
47 echo "Generate secret - please wait..."
48 local tmp="/tmp/zt.$cfg.secret"
49 zerotier-idtool generate "$tmp" > /dev/null
50 secret="$(cat $tmp)"
51 rm "$tmp"
52
53 uci set zerotier.$cfg.secret="$secret"
54 uci commit zerotier
55 fi
56
57 if [ -n "$secret" ]; then
58 echo "$secret" > $CONFIG_PATH/identity.secret
59 # make sure there is not previous identity.public
60 rm -f $CONFIG_PATH/identity.public
61 fi
62
63 add_join() {
64 # an (empty) config file will cause ZT to join a network
65 touch $CONFIG_PATH/networks.d/$1.conf
66 }
67
68 config_list_foreach $cfg 'join' add_join
69
70 procd_open_instance
71 procd_add_reload_interface_trigger "$interface"
72 procd_set_param command $PROG $ARGS $CONFIG_PATH
73 procd_close_instance
74 }
75
76 service_triggers() {
77 procd_add_reload_trigger zerotier
78 }
79
80 start_service() {
81 config_load 'zerotier'
82 config_foreach start_instance 'zerotier'
83 }
84
85 stop_instance() {
86 local cfg="$1"
87
88 # Remove existing link or folder
89 rm -rf $CONFIG_PATH
90 }
91
92 stop_service() {
93 config_load 'zerotier'
94 config_foreach stop_instance 'zerotier'
95 }