Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 26 additions & 27 deletions scripts/network/exdhcp/dhcpd_edithosts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/python
#!/usr/bin/env python3

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
Expand All @@ -18,10 +19,11 @@


# Usage: dhcpd_edithosts.py mac ip hostname dns gateway nextserver
import sys, os
from os.path import exists

import sys
import os
from pathlib import Path
from time import sleep
from os import remove

usage = '''dhcpd_edithosts.py mac ip hostname dns gateway nextserver'''
conf_path = "/etc/dhcpd.conf"
Expand All @@ -36,62 +38,61 @@ def lock():
sleep(1)
count = count + 1
if count > sleep_max:
print "Can not get file lock at %s, time expired" % file_lock
print("Can not get file lock at {}, time expired".format(file_lock))
return False

try:
f = open(file_lock, "w")
f.close()
f = Path(file_lock).open(mode="w")
return True
except IOError,e:
print "Cannot create file lock at /etc/dhcpd.conf_locked,", e
except IOError as e:
print("Cannot create file lock at /etc/dhcpd.conf_locked: {}".format(e))
return False


def unlock():
if exists(file_lock) == False:
print "Cannot find %s when unlocking, race condition happens" % file_lock
print("Cannot find {} when unlocking, race condition happens".format(file_lock))
else:
try:
remove(file_lock)
os.remove(file_lock)
return True
except IOError, e:
print "Cannot remove file lock at %s" % file_lock
except IOError as e:
print("Cannot remove file lock at {0}: {1}".format(file_lock,e))
return False

def insert_host_entry(mac, ip, hostname, dns, gateway, next_server):
if lock() == False:
return 1

cmd = 'sed -i /"fixed-address %s"/d %s' % (ip, conf_path)
cmd = 'sed -i /"fixed-address {0}"/d {1}'.format(ip,conf_path)
ret = os.system(cmd)
if ret != 0:
print "Command %s failed" % cmd
print("Command {} failed".format(cmd))
unlock()
return 1

cmd = 'sed -i /"hardware ethernet %s"/d %s' % (mac, conf_path)
cmd = 'sed -i /"hardware ethernet {0}"/d {1}'.format(mac,conf_path)
ret = os.system(cmd)
if ret != 0:
print "Command %s failed" % cmd
print("Command {} failed".format(cmd))
unlock()
return 1

if next_server != "null":
entry = host_entry1 % (hostname, mac, ip, dns, "cloudnine.internal", gateway, next_server)
else:
entry = host_entry % (hostname, mac, ip, dns, "cloudnine.internal", gateway)
cmd = '''echo '%s' >> %s''' % (entry, conf_path)
cmd = '''echo '{0}' >> {1}'''.format(entry,conf_path)
ret = os.system(cmd)
if ret != 0:
print "Command %s failed" % cmd
print("Command {} failed".format(cmd))
unlock()
return 1

cmd = 'service dhcpd restart'
ret = os.system(cmd)
if ret != 0:
print "Command %s failed" % cmd
print("Command {} failed".format(cmd))
unlock()
return 1

Expand All @@ -102,22 +103,20 @@ def insert_host_entry(mac, ip, hostname, dns, gateway, next_server):

if __name__ == "__main__":
if len(sys.argv) < 7:
print usage
print(usage)
sys.exit(1)

mac = sys.argv[1]
ip = sys.argv[2]
hostname = sys.argv[3]
dns = sys.argv[4]
gateway = sys.argv[5]
next_server = sys.argv[6]

if exists(conf_path) == False:
if Path(conf_path).exists() == False:
conf_path = "/etc/dhcp/dhcpd.conf"
if exists(conf_path) == False:
print "Cannot find dhcpd.conf"
print("Cannot find dhcpd.conf")
sys.exit(1)

ret = insert_host_entry(mac, ip, hostname, dns, gateway, next_server)
sys.exit(ret)