-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish-ip.py
executable file
·125 lines (106 loc) · 3.3 KB
/
publish-ip.py
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
#!/usr/bin/python3
"""IP Publisher.
Publishes the current wlan0 configuration to
https://nus-cg3002-9.herokuapp.com.
"""
import sys
import argparse
import os.path
import os
import subprocess
import time
import urllib.request
import urllib.parse
import json
import datetime
import traceback
last_content = None
def read_target(target_path):
header = []
requires = set()
with open(target_path, 'r') as f:
for line in f:
line = line.strip()
if not line:
continue
if line.startswith('Requires='):
line = line[len('Requires='):]
requires.add(line)
else:
header.append(line)
return header, requires
def write_target(target_path, header, requires):
with open(target_path, 'w') as f:
for line in header:
f.write(line + '\n')
f.write('\n')
for line in requires:
f.write('Requires=' + line + '\n')
def merge_target(target_path, requires_add):
try:
header, requires = read_target(target_path)
except FileNotFoundError:
header = [
'[Unit]',
'Description=Default target',
'AllowIsolate=true'
]
requires = set()
requires.update(requires_add)
write_target(target_path, header, requires)
def publish():
global last_content
content = subprocess.check_output(['ip', 'addr'], universal_newlines=True)
if content == last_content:
return
timezone = datetime.timezone(datetime.timedelta(hours=8), 'SGT')
timenow = datetime.datetime.now(timezone)
d = {'ipaddr': str(content), 'updated': str(timenow)}
data = urllib.parse.urlencode(d).encode()
print('Updating...')
urllib.request.urlopen('https://nus-cg3002-9.herokuapp.com/dancedance', data)
last_content = content
def monitor():
while True:
try:
publish()
except Exception:
traceback.print_exc()
time.sleep(10)
def install():
systemd_path = os.path.expanduser('~/.config/systemd/user')
os.makedirs(systemd_path, exist_ok=True)
target_path = os.path.join(systemd_path, 'default.target')
unit_path = os.path.join(systemd_path, 'publiship.service')
with open(unit_path, 'w') as f:
f.write('[Unit]\n')
f.write('Description=publish ip\n')
f.write('\n')
f.write('[Service]\n')
f.write('ExecStart={} -d\n'.format(os.path.abspath(__file__)))
f.write('KillMode=mixed\n')
f.write('Restart=always\n')
merge_target(target_path, [
'publiship.service',
])
print('systemd files installed.')
subprocess.check_call(['systemctl', '--user', 'daemon-reload'])
subprocess.check_call(['systemctl', '--user', 'restart', 'publiship'])
print('Make sure to run:')
print('')
print(' loginctl enable-linger <username>')
print('')
print('to allow the service to start on boot.')
def main(args, prog=None):
p = argparse.ArgumentParser(prog=prog)
p.add_argument('-i', '--install', action='store_true')
p.add_argument('-d', '--daemon', action='store_true')
args = p.parse_args(args)
if args.install:
install()
elif args.daemon:
monitor()
else:
publish()
if __name__ == '__main__':
main(sys.argv[1:], sys.argv[0])