-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfby_init
More file actions
executable file
·93 lines (72 loc) · 2.76 KB
/
fby_init
File metadata and controls
executable file
·93 lines (72 loc) · 2.76 KB
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
#!/usr/bin/env python
import sys
from friskby import DeviceConfig
import friskby_controlpanel.friskby_settings as settings
from friskby_controlpanel import FriskbyInterface
def error_exit():
msg = """
The fby_init shell script should be invoked with the device id of
your device as commandline argument:
fby_init myFriskPI_id
Make sure that the device has been 'unlocked' in the control panel of
the Friskby server before attempting to initialize. When the
initialization has completed you will get a url which can be used to
further monitor and control your sensor.
"""
sys.exit( msg )
def locked_device(device_id):
msg = """
Exception 'KeyError' was raised while configuring the device: %s,
that probably means that your device is locked on the FriskBY
server. Please go to the server admin panel and uncheck the 'Locked'
checkbox for device:%s, observe that the device is automatically
locked after each download.
""" % (device_id , device_id)
sys.exit( msg )
def wrong_id(device_id):
msg = """
Exception 'ValueError' was raised while configuring the device:
%s, this probably means that your device ID was wrong - the server did
not recognize device id:%s. Please make sure the device is already
preconfigured on the server, and that you have typed the id correctly.
""" % (device_id, device_id)
sys.exit( msg )
def salt_config_error():
sys.exit( """Can't open /etc/salt/minion for writing. Make sure
salt-minion is installed and this tool is running with sufficient
privileges to change this file.""")
def stop_device( ):
fbi = FriskbyInterface( )
fbi.manage("stop")
def start_device( ):
fbi = FriskbyInterface( )
fbi.manage("start")
def download_config(device_id):
config_url = settings.config_url( device_id )
print("Fetching config for device %s from: %s" % (device_id, config_url))
try:
config = DeviceConfig.download( config_url )
except KeyError:
locked_device( device_id )
except ValueError:
wrong_id( device_id )
config_path = settings.DEVICE_CONFIG_PATH
print("Saving device configuration to:%s" % config_path )
config.save( filename = config_path )
def write_salt_config(device_id):
finger = "79:74:ee:b0:e1:98:2e:28:a3:8c:85:f7:05:04:6e:64:75:bc:c0:d3:80:3b:54:51:66:f7:78:61:3d:d0:1c:c9"
try:
with open('/etc/salt/minon', 'w') as config:
config.write("master: salt.friskby.no\n")
config.write("master_finger: %s\n" % finger)
config.write("id: %s\n" % device_id)
except IOError e:
salt_config_error()
if __name__ == "__main__":
if len(sys.argv) < 2:
error_exit( )
dev_id = sys.argv[1]
stop_device( )
download_config( dev_id )
write_salt_config( dev_id )
start_device( )