import os
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["int_details"]
coll_route_info = db["ubuntu_route_info"]
coll_interface_info = db["ubuntu_interface_info"]

@app.route('/tunnel_data', methods = ['POST'])  
def dhcp_conf():
   data = request.json
   response = {"message":"successfull"}
  dhcp_netmask = data["dhcp_netmask"]
  lan_address = data["dhcp_subnet"]
  dhcp_start_address = data["dhcp_startaddr"]
  dhcp_end_address = data["dhcp_endaddr"]
  dhcp_gateway = data["dhcp_gateway"]
  domain_name = data["primary_dns"]
  optional_dns = data['secondary_dns"]
  bracket = "{"
  closebracket ="}"
  interface_name = data["intfc_name"]
  static_ip = data["intfc_ip"]
#Configure dhcpd.conf file
  with open("/etc/dhcp/dhcpd.conf", "w") as f:

    f.write(f"default-lease-time 600;\nmax-lease-time 7200;\nauthoritative;\nsubnet {lan_address} netmask {dhcp_netmask} {bracket} \n range {dhcp_start_address} {dhcp_end_address}; \n option routers {dhcp_gateway}; \n option subnet-mask {dhcp_netmask}; \n option domain-name-servers {domain_name}, {optional_dns}; \n{closebracket}")

# Configure network interface
#network_interface = # The primary network interface
  with open("/etc/network/interfaces", "w") as w:
    w.write(f"auto {interface_name} \n iface {interface_name} inet static \n \t address {static_ip} netmask {dhcp_netmask} \n \t gateway {dhcp_gateway}")
# Start the dhcpd service
  with open("/etc/default/isc-dhcp-server", "w") as f:
    f.write(f'INTERFACESv4="{interface_name}"')

  print(os.system(f"sudo systemctl enable isc-dhcp-server"))
  print(os.system(f"sudo systemctl start isc-dhcp-server"))

