libreschool-client/libreschool-client

114 lines
3.3 KiB
Python
Executable File

#!/usr/bin/env python
import configparser
import uuid
import re
import os
import socket, struct
import requests
import sys
import socket
import fileinput
import json
def get_default_gateway_linux():
"""Read the default gateway directly from /proc."""
with open("/proc/net/route") as fh:
for line in fh:
fields = line.strip().split()
if fields[1] != '00000000' or not int(fields[3], 16) & 2:
continue
return socket.inet_ntoa(struct.pack("<L", int(fields[2], 16)))
def update_config_file(config):
# update config
with open('/etc/libreschool-client/libreschool-client.conf', 'w') as configfile:
config.write(configfile)
def set_new_hostname(hostname, domainname, oldhostname):
os.system("hostname -b "+hostname)
with open('/etc/hostname', 'w') as hostnamefile:
hostnamefile.write(hostname)
if not os.path.isfile('/etc/hosts'):
f1=open('/etc/hosts','r').read()
m=f1.replace(oldhostname,hostname)
m=m.replace(oldhostname+'.'+domainname, hostname+'.'+domainname)
f2=open('/etc/hosts','w')
f2.write(m)
return
config = configparser.ConfigParser()
config.read('/etc/libreschool-client/libreschool-client.conf')
# check for client section
# and create it if non-existamt
if not 'client' in config:
config['client'] = {}
# generate client uuid if not set
if not 'uuid' in config['client']:
config['client']['uuid'] = str(uuid.uuid4())
# if uuid is not in the correct format, generate another
match = re.search(r'^[a-f0-9]{8}(-[a-f0-9]{4}){3}-[a-f0-9]{12}$', config['client']['uuid'], re.M | re.I)
if not match:
config['client']['uuid'] = str(uuid.uuid4())
# check for server section
# and create it if non-existamt
if not 'server' in config:
config['server'] = {}
# generate server ping url if not set
if not 'ping_url' in config['server']:
config['server']['ping_url'] = 'http://' + get_default_gateway_linux() + ':3000/ping'
# update config
update_config_file(config)
data = {
'client_uuid': config['client']['uuid'],
'hostname': socket.gethostname(),
'last_config': config['server'].get('last_config','0')
}
# if server uuid is set
if 'uuid' in config['server']:
# and is in the correct format
match = re.search(r'^[a-f0-9]{8}(-[a-f0-9]{4}){3}-[a-f0-9]{12}$', config['server']['uuid'], re.M | re.I)
if match:
# add it to the request
data['server_uuid']=config['server']['uuid']
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json; charset=UTF-8'
}
try:
r = requests.post(config['server']['ping_url'], data=json.dumps(data), headers=headers, timeout=5)
except requests.exceptions.RequestException as e:
print e
sys.exit(1)
resp = r.json()
if 'error' in resp:
print resp['error']
sys.exit(1)
if not 'uuid' in config['server']:
if 'server_uuid' in resp:
config['server']['uuid'] = resp['server_uuid']
update_config_file(config)
if not 'ping_url' in config['server']:
if 'ping_url' in resp:
config['server']['ping_url'] = resp['ping_url']
update_config_file(config)
if 'hostname' in resp and 'domainname' in resp:
if not data['hostname'] == resp['hostname']:
set_new_hostname(resp['hostname'],resp['domainname'],data['hostname'])