127 lines
3.7 KiB
Python
Executable File
127 lines
3.7 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import configparser
|
|
import uuid
|
|
import re
|
|
import os
|
|
import socket, struct
|
|
import requests
|
|
import sys
|
|
import socket
|
|
import fileinput
|
|
import json
|
|
import lsb_release
|
|
import psutil
|
|
|
|
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):
|
|
with open('/etc/libreschool-client/libreschool-client.conf', 'w') as configfile:
|
|
config.write(configfile)
|
|
|
|
def set_new_hostname(hostname, domainname, oldhostname):
|
|
if os.path.isfile('/etc/hosts'):
|
|
f1=open('/etc/hosts','r').read()
|
|
m1=f1.replace(oldhostname,hostname)
|
|
m2=m1.replace(oldhostname+'.'+domainname, hostname+'.'+domainname)
|
|
f2=open('/etc/hosts','w')
|
|
f2.write(m2)
|
|
os.system("hostname -b "+hostname)
|
|
with open('/etc/hostname', 'w') as hostnamefile:
|
|
hostnamefile.write(hostname)
|
|
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 '--with-inventory' in sys.argv:
|
|
cpu = "Unknown"
|
|
with open("/proc/cpuinfo") as fh:
|
|
for line in fh:
|
|
fields = line.strip().split()
|
|
if "model name" in line:
|
|
cpu = re.sub( ".*model name.*:\s*", "", line, 1)
|
|
data['cpu'] = cpu
|
|
data['ram'] = int(psutil.virtual_memory().total / 1024)
|
|
distinfo = lsb_release.get_distro_information()
|
|
data['distro'] = distinfo.get('DESCRIPTION', 'n/a')
|
|
|
|
# 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'])
|