log: stdout if on tty, in logfile otherwise

This commit is contained in:
Gilles Grandou 2020-10-04 22:49:12 +02:00
parent 2f970cd71c
commit a63bd3a5a5

View File

@ -201,6 +201,10 @@ def write_zone_list(zone_filename, zone, update):
def log(msg):
stamp = time.strftime("%Y-%m-%d %H:%M:%S")
if sys.stdout.isatty():
for line in msg.split('\n'):
print("%s - %s" % (stamp, msg))
else:
with open(log_filename, 'a') as logfile:
for line in msg.split('\n'):
logfile.write("%s - %s\n" % (stamp, msg))
@ -226,7 +230,7 @@ def ovh_update_zone(domain, zone, update, sync_zone):
result = client.get('/domain/zone/{}/record'.format(domain),
fieldType=prot, subDomain=name)
for id in result:
print('OVH: delete entry #{} {} {}'.format(id, name, prot))
log('OVH: delete entry #{} {} {}'.format(id, name, prot))
client.delete('/domain/zone/{}/record/{}'.format(domain, id))
for prot, name, addr in update['delete']:
@ -238,43 +242,45 @@ def ovh_update_zone(domain, zone, update, sync_zone):
for id in result:
r = client.get('/domain/zone/%s/record/%d' % (domain, id))
if r['fieldType'] == prot and r['target'] == addr:
print("OVH: delete entry for %s %s %s (#%s)" % (name, prot, addr, id))
log("OVH: delete entry for %s %s %s (#%s)" % (name, prot, addr, id))
client.delete('/domain/zone/%s/record/%d' % (domain, id))
for prot, name, addr in update['add']:
if not prot in ['A', 'AAAA']:
continue
print("OVH: create entry for %s %s %s" % (name, prot, addr))
log("OVH: create entry for %s %s %s" % (name, prot, addr))
client.post('/domain/zone/%s/record' % domain,
fieldType=prot,
subDomain=name,
target=addr,
ttl=60)
print("OVH: Refresh zone %s" % domain)
log("OVH: Refresh zone %s" % domain)
client.post('/domain/zone/%s/refresh' % domain)
return True
except:
print('OVH update error\n')
log('OVH update error\n')
return False
def livebox_rule_id(words):
id='dyndomain_{}'.format('_'.join(words))
id = id.replace('.', '_')
return id
def livebox_delete_port_nat(port, addr):
print('livebox: delete PortNat {} to {}'.format(port, addr))
log('livebox: delete PortNat {} to {}'.format(port, addr))
id = livebox_rule_id([port])
r = sysbus.requete('Firewall:deletePortForwarding', { 'id': id, 'origin': 'webui' })
def livebox_delete_pinhole(port, addr):
print('livebox: delete pinhole {} to {}'.format(port, addr))
log('livebox: delete pinhole {} to {}'.format(port, addr))
id = livebox_rule_id([port, addr])
r = sysbus.requete('Firewall:deletePinhole', { 'id': id, 'origin': 'webui' })
def livebox_add_port_nat(port, addr):
print('livebox: add PortNat {} to {}'.format(port, addr))
log('livebox: add PortNat {} to {}'.format(port, addr))
id = livebox_rule_id([port])
a = {
'id': id,
@ -288,7 +294,7 @@ def livebox_add_port_nat(port, addr):
r = sysbus.requete('Firewall:setPortForwarding', a)
def livebox_add_pinhole(port, addr):
print('livebox: add pinhole {} to {}'.format(port, addr))
log('livebox: add pinhole {} to {}'.format(port, addr))
id = livebox_rule_id([port, addr])
a = {
'id': id,
@ -326,7 +332,7 @@ def send_update_mail(mail_to, mail_from, zone_domain, update, mail_ignore_list,
if not okmail:
return
print('Send email to %s' % mail_to)
log('Send email to %s' % mail_to)
msg = EmailMessage()
msg['Subject'] = "Livebox update in %s" % zone_domain
@ -354,7 +360,6 @@ def send_update_mail(mail_to, mail_from, zone_domain, update, mail_ignore_list,
s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()
#print(txt)
load_conf()
@ -366,7 +371,7 @@ if not ping(wan_hostname):
sysbus.load_conf()
r = sysbus.auth(False)
if not r:
print('Error: cannot authenticate on livebox')
log('Error: cannot authenticate on livebox')
sys.exit(1)
hosts = get_hosts()