Network: support for setting DNS list and hostname

parent 4b300714
# -*- coding: utf-8 -*-
import socket
import os
from pyroute2 import IPDB
from pyroute2.netlink.rtnl.ifinfmsg import IFF_UP
from ipaddress import IPv4Address
from yaml import dump
from yaml import dump, safe_load
from settingsd import const
from settingsd import config
......@@ -13,7 +14,7 @@ from settingsd import shared
from settingsd import logger
import settingsd.tools as tools
import settingsd.tools.process
from settingsd.tools.process import execProcess
import settingsd.tools.editors
##### Private constants #####
......@@ -39,6 +40,16 @@ class Network(service.FunctionObject) :
with open(filename, 'w+') as network_config:
network_config.write(dump(settings))
@service.functionMethod(NETWORK_METHODS_NAMESPACE, in_signature="s")
def reloadNetworkConfig(self, filename):
with open(filename, 'r') as network_config:
settings = safe_load(network_config.read())
if settings.get('hostname') is not None:
self._set_hostname(settings['hostname'])
if settings.get('dns') is not None:
self._set_dns_servers(settings['dns'])
def _dump_interfaces(self):
interfaces = {}
......@@ -57,9 +68,9 @@ class Network(service.FunctionObject) :
def _get_dns_servers(self):
with open('/etc/resolv.conf', 'r') as resolv_conf:
return [
line.split(' ')[1]
for line in resolv_conf.readlines()
if line.startswith('nameserver')
line.strip().split(' ')[1]
for line in resolv_conf.read().splitlines()
if line.strip().startswith('nameserver')
]
def _prefix_to_netmask(self, prefix):
......@@ -67,6 +78,22 @@ class Network(service.FunctionObject) :
(0xffffffff << (32 - prefix)) & 0xffffffff
))
def _set_dns_servers(self, dns_list):
with open('/etc/resolv.conf', 'r+') as resolv_conf:
lines = [
line for line in resolv_conf.read().splitlines()
if not line.strip().startswith('nameserver')
]
lines += ['nameserver ' + dns for dns in dns_list]
resolv_conf.seek(0, os.SEEK_SET)
resolv_conf.write('\n'.join(lines))
resolv_conf.truncate()
def _set_hostname(self, hostname):
with open('/etc/hostname', 'w') as etc_hostname:
etc_hostname.write(hostname)
execProcess(['hostname', hostname], shell=True)
##### Public classes #####
class Service(service.Service) :
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment