/
kano-network-hook
executable file
·70 lines (58 loc) · 2.25 KB
/
kano-network-hook
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
#!/usr/bin/env python
#
# kano-network-hook
#
# Copyright (C) 2015-2017 Kano Computing Ltd.
# License: http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
#
# This script is called from the dhcpcd-hooks scripts,
# to send the RaspberryPI CPU Serial Number to Kano.
# It runs as the superuser.
#
import os
import sys
import time
from kano.logging import logger
from kano.utils import get_cpu_id, get_rpi_model, run_cmd
def send_cpu_id():
sent=False
# This is a loose dependency as we can't make kano-toolset
# depend on profile.
try:
from kano_profile.tracking_events import generate_event
generate_event('ping')
except Exception as e:
logger.error("Error sending the ping event ({})".format(e))
# Get the unit CPU Serial number and send it, save a local copy
cpuid_filename = '/etc/cpuid-{}'.format(get_cpu_id())
if os.path.isfile(cpuid_filename):
# We have already sent the CPU ID
pass
else:
try:
# The code below is the legacy way of tracking cpuids
# TODO: To be removed when the new way is known to be reliable
# Save a trace of the CPUID so we dont send it over again
# The UTC timestamp is embedded inside the cpuid file as well
utc_time=time.asctime(time.gmtime(time.time()))
with open(cpuid_filename, 'a') as f:
os.utime(cpuid_filename, None)
f.write('RaspberryPI model: {}\n{}\n'.format(get_rpi_model(), utc_time))
# Send this file over the network
cmdline='curl --retry 5 --retry-delay 3 -T "{}" ' \
'--user "cpuname:mko821fgx" http://cpuids.kano.me//cpuserialnos/'.format(cpuid_filename)
_, _,rc = run_cmd(cmdline)
assert (rc==0)
sent=True
except:
if os.path.isfile(cpuid_filename): os.unlink(cpuid_filename)
logger.error('Error sending Kit serial number')
return sent
if __name__ == '__main__':
rc = 1
# A DHCP REBOOT event is an indication that the Internet should be reachable
if len(sys.argv) > 1 and sys.argv[1] == 'REBOOT':
if send_cpu_id() == True:
logger.info('Kit serial number has been sent')
rc=0
exit (rc)