Skip to content

Commit

Permalink
Merge pull request #3 from RomanValov/master
Browse files Browse the repository at this point in the history
new: rework network wizard
  • Loading branch information
RomanValov committed Aug 12, 2019
2 parents 82eda1a + 8792f73 commit c7b2930
Show file tree
Hide file tree
Showing 22 changed files with 833 additions and 437 deletions.
13 changes: 6 additions & 7 deletions lib/ram/net/hostname/input
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@ with ram.context(__name__):
from net.utils import ValidateHostname


# Maximum length of hostname to set is defined via sysconf HOST_NAME_MAX
# but this identifier is not available via python's os.sysconf interface.
def __ValidateHostname(value):
return ValidateHostname(value, length=64)


if __name__ == '__main__':
params = ram.param()
config = ram.query()

# Maximum length of hostname to set is defined via sysconf HOST_NAME_MAX
# but this identifier is not available via python's os.sysconf interface.
def __ValidateHostname(value):
return ValidateHostname(value, length=64, only_isfqdn=params.fqdn)

config['hostname'], = ram.widgets.RunEntry(
"Hostname",
"Specify hostname for the machine.",
"Specify %s for the machine" % ("fully qualified domain name (FQDN)" if params.fqdn else "hostname"),
[("Hostname", config['hostname'], __ValidateHostname)],
allowCancel=not params.forced,
supplySaved=not params.erased,
Expand Down
1 change: 1 addition & 0 deletions lib/ram/net/hostname/param
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
forced
erased
fqdn
16 changes: 8 additions & 8 deletions lib/ram/net/ifcfg_redhat.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,12 @@ def DelIface(self, ifname):
self.routes[ifname].clear()

@_modify_settings
def AddIface(self, ifname, defconf=False):
def AddIface(self, ifname):
self.ifcfgs[ifname] = env.cfgopen(_IFCFG_PATH + ifname, readonly=False, delempty=True)
self.ifcfgs[ifname].update(self.DEFLOOP_NETCONF if self.IsLoopback(ifname) else self.DEFAULT_NETCONF)
self.ifcfgs[ifname]['DEVICE'] = ifname
self.ifcfgs[ifname]['DEFCONF'] = 'yes' if defconf else 'no'
self.routes[ifname] = env.cfgopen(_ROUTE_PATH + ifname, readonly=False, delempty=True)

def GetIfaceDefConf(self, ifname):
if self.IsLoopback(ifname):
return False
defconf = self.ifcfgs[ifname]['DEFCONF'] or 'no'
return defconf.lower() == 'yes'

def GetIfaceDevName(self, ifname):
return self.ifcfgs[ifname]['DEVICE'] or ifname

Expand All @@ -105,6 +98,13 @@ def SetIfaceBootProto(self, ifname, bootproto):
self.ifcfgs[ifname]['PERSISTENT_DHCLIENT'] = 'yes'
self.ifcfgs[ifname]['BOOTPROTO'] = bootproto

def GetIfaceUseDhcp(self, ifname):
return bool(self.GetIfaceBootProto(ifname))

@_modify_settings
def SetIfaceUseDhcp(self, ifname, usedhcp):
self.SetIfaceBootProto(ifname, "dhcp" if usedhcp else "")

def GetIfaceEnabled(self, ifname):
if self.IsLoopback(ifname):
return True
Expand Down
189 changes: 138 additions & 51 deletions lib/ram/net/ifconfig/input
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,23 @@ with ram.context(__name__):
from wiz.entry import RunDictIndex
from net.utils import ValidateEmptyOrIpV4

from ..resolver.entry import ModifyPeerDnsDevice
from ..resolver.entry import RemovePeerDnsDevice

from ..resolver.utils import CheckPeerDnsDevice

from ..routing.entry import ModifyGatewayDevice
from ..routing.entry import RemoveGatewayDevice
from ..routing.entry import EditGatewayAddress

from ..routing.utils import CheckGatewayDevice
from ..routing.utils import ShownGatewayIpAddr


def EditIfaceIpAddressAndNetwork(config, ifname):
if config[ifname]['usedhcp'] and not ram.widgets.AskViaButtons(
ifconf = config['ifconfig'][ifname]

if ifconf['usedhcp'] and not ram.widgets.AskViaButtons(
"Use static configuration?",
"Interface `%s` is configured to obtain network parameters via DHCP protocol.\n\n"
"Would you like to use static configuration?\n" % ifname
Expand All @@ -21,26 +35,29 @@ def EditIfaceIpAddressAndNetwork(config, ifname):
"Interface IP configuration",
"Specify IP address and netmask for the interface `%s`." % ifname,
[
("Address", config[ifname]['ip_addr'], ValidateEmptyOrIpV4),
("Netmask", config[ifname]['netmask'], ValidateEmptyOrIpV4),
("Address", ifconf['ip_addr'], ValidateEmptyOrIpV4),
("Netmask", ifconf['netmask'], ValidateEmptyOrIpV4),
],
)

config[ifname]['usedhcp'] = ""
config[ifname]['ip_addr'] = ip_addr
config[ifname]['netmask'] = netmask
ifconf['usedhcp'] = ""
ifconf['ip_addr'] = ip_addr
ifconf['netmask'] = netmask


def EditNetworkIface(config, ifname):
ifconf = config['ifconfig'][ifname]
backup_peerdns = config['resolver']['peerdns']
backup_default = config['routing']['default']

def __SwitchIfaceEnabled():
config[ifname]['enabled'] = "" if config[ifname]['enabled'] else ifname
ifconf['enabled'] = "" if ifconf['enabled'] else "_"

def __SwitchIfaceUseDhcp():
if not config[ifname]['usedhcp']:
config[ifname]['usedhcp'] = "dhcp"
elif config[ifname]['ip_addr'] and config[ifname]['netmask']:
config[ifname]['usedhcp'] = ""
if not ifconf['usedhcp']:
ifconf['usedhcp'] = "_"
elif ifconf['ip_addr'] and ifconf['netmask']:
ifconf['usedhcp'] = ""
else:
EditIfaceIpAddressAndNetwork(config, ifname)

Expand All @@ -51,7 +68,7 @@ def EditNetworkIface(config, ifname):
EditIfaceIpAddressAndNetwork(config, ifname)

def __SwitchIfaceDefconf():
if config[ifname]['hw_addr']:
if ifconf['hw_addr']:
if not ram.widgets.AskViaButtons(
"Reset interface `%s`?" % ifname,
"Would you like to reset interface `%s`?" % ifname,
Expand All @@ -64,16 +81,48 @@ def EditNetworkIface(config, ifname):
):
return

config[ifname]['enabled'] = ""
config[ifname]['defconf'] = ifname
DelNetworkIfaceNow(config, ifname)
return ifname

def __SwitchIfacePeerDns():
if not config['resolver']['peerdns'] == ifname:
ModifyPeerDnsDevice(config, ifname)
elif backup_peerdns and backup_peerdns != ifname:
ModifyPeerDnsDevice(config, backup_peerdns, show_confirm=False)
else:
RemovePeerDnsDevice(config)

def __SwitchIfaceDefault():
if not config['routing']['default'] == ifname:
ModifyGatewayDevice(config, ifname)
elif backup_default and backup_default != ifname:
ModifyGatewayDevice(config, backup_default, show_confirm=False)
else:
RemoveGatewayDevice(config)

def __EditIfaceIpGateway():
EditGatewayAddress(config, ifname)

def __MkEditNetworkIface():
devname = config[ifname]['hw_addr'] or "*** NOT FOUND ***"
enabled = "yes" if config[ifname]['enabled'] else "no"
usedhcp = "yes" if config[ifname]['usedhcp'] else "no"
ip_addr = "dhcp" if config[ifname]['usedhcp'] else config[ifname]['ip_addr']
netmask = "dhcp" if config[ifname]['usedhcp'] else config[ifname]['netmask']
devname = ifconf['hw_addr'] or "*** NOT FOUND ***"
enabled = "yes" if ifconf['enabled'] else "no"
usedhcp = "yes" if ifconf['usedhcp'] else "no"
ip_addr = "dhcp" if ifconf['usedhcp'] else ifconf['ip_addr']
netmask = "dhcp" if ifconf['usedhcp'] else ifconf['netmask']

if config['resolver']['peerdns'] == ifname:
iserror, warning = CheckPeerDnsDevice(ifconf)
peerdns = "yes" + (" *" if warning else "")
else:
peerdns = "no"

if config['routing']['default'] == ifname:
iserror, warning = CheckGatewayDevice(ifconf)
default = "yes" + (" *" if warning else "")
else:
default = "no"

gateway = ShownGatewayIpAddr(ifconf)

return [
("%-16s %-16s" % ("HW addr:", devname), 0),
Expand All @@ -83,79 +132,119 @@ def EditNetworkIface(config, ifname):
("", 2),
("%-16s %-15s" % ("IP addr:", ip_addr), __EditIfaceIpAddress),
("%-16s %-15s" % ("Netmask:", netmask), __EditIfaceIpNetwork),
("%-16s %-15s" % ("Gateway:", gateway), __EditIfaceIpGateway),
("", 3),
("Reset configuration ..." if config[ifname]['hw_addr'] else "Delete configuration ...", __SwitchIfaceDefconf),
("%-16s %s" % ("Default route:", default), __SwitchIfaceDefault),
("", 4),
("%-16s %s" % ("Use DHCP DNS:", peerdns), __SwitchIfacePeerDns),
("", 5),
("Reset configuration ..." if ifconf['hw_addr'] else "Delete configuration ...", __SwitchIfaceDefconf),
]

return ram.widgets.RunMenu("Select Action - %s" % ifname, __MkEditNetworkIface)


def AddNetworkIface(config, ifname, device=None):
def AddNetworkIfaceNow(config, ifname):
ifconf = config['ifconfig'][ifname]

ifconf['defconf'] = ""
ifconf['enabled'] = "_"
ifconf['usedhcp'] = "_"

if not config['routing']['default']:
config['routing']['default'] = ifname

if not config['resolver']['peerdns'] and (
not config['resolver']['pri_dns'] and
not config['resolver']['sec_dns']
):
config['resolver']['peerdns'] = ifname


def AddNetworkIface(config, ifname):
if ram.widgets.AskViaButtons(
"Unconfigured interface `%s`" % ifname,
"Interface `%s` is found but not configured.\n\n"
"Would you like to initialize it now?" % ifname
):
config[ifname]['defconf'] = ""
config[ifname]['enabled'] = ifname
config[ifname]['usedhcp'] = "dhcp"
AddNetworkIfaceNow(config, ifname)
EditNetworkIface(config, ifname)


def DelNetworkIfaceNow(config, ifname):
ifconf = config['ifconfig'][ifname]

ifconf['defconf'] = "_"
ifconf['enabled'] = ""
ifconf['usedhcp'] = ""

if device:
EditNetworkIface(config, ifname)
if config['routing']['default'] == ifname:
config['routing']['default'] = ""

if config['resolver']['peerdns'] == ifname:
config['resolver']['peerdns'] = ""

def DelNetworkIface(config, ifname, device=None):
if not device and ram.widgets.AskViaButtons(

def DelNetworkIface(config, ifname):
if ram.widgets.AskViaButtons(
"Missing interface `%s`" % ifname,
"Interface `%s` is configured but not found.\n\n"
"Would you like to delete it permanently?" % ifname
):
config[ifname]['defconf'] = ifname
DelNetworkIfaceNow(config, ifname)
else:
EditNetworkIface(config, ifname)


def ActNetworkIface(config, ifname, device=None):
if not config[ifname]['hw_addr']:
DelNetworkIface(config, ifname, device)
elif config[ifname]['defconf']:
AddNetworkIface(config, ifname, device)
def ActNetworkIface(config, ifname):
ifconf = config['ifconfig'][ifname]

if not ifconf:
ram.widgets.ShowError(ifname, "ERROR: Device is not configured!")
elif not ifconf['hw_addr']:
DelNetworkIface(config, ifname)
elif ifconf['defconf']:
AddNetworkIface(config, ifname)
else:
EditNetworkIface(config, ifname)


if __name__ == '__main__':
params = ram.param()
config = ram.query()

ifaces = config.keys()
config = ram.query('net.network')

if not params.device:
def __FormatIfEntry(ifname):
if config[ifname]['hw_addr'] and config[ifname]['defconf']:
ifconf = config['ifconfig'][ifname]

if ifconf['hw_addr'] and ifconf['defconf']:
status = " new ... "
else:
status = "< %03s >" % ("on" if config[ifname]['enabled'] else "off")
status = "< %03s >" % ("on" if ifconf['enabled'] else "off")

return "%-6s %-18s: %-9s" % (
ifname, config[ifname]['hw_addr'] or "*** NOT FOUND ***", status
ifname, ifconf['hw_addr'] or "*** NOT FOUND ***", status
)

def __FilterIfEntry(ifname):
return config[ifname]['hw_addr'] or not config[ifname]['defconf']
ifconf = config['ifconfig'][ifname]

return ifconf['hw_addr'] or not ifconf['defconf']

def __ModifyIfEntry(ifname):
ActNetworkIface(config, ifname)

def __SwitchIfEntry(ifname):
if not config['defconf']:
config[ifname]['enabled'] = "" if config[ifname]['enabled'] else ifname
ifconf = config['ifconfig'][ifname]

if not ifconf['defconf']:
ifconf['enabled'] = "" if ifconf['enabled'] else "_"

while ifaces:
while config:
RunDictIndex(
"Select Action - Interfaces",
"", "",
values_fn=lambda: sorted(ifaces),
values_fn=lambda: sorted(config['ifconfig']),
format_fn=__FormatIfEntry,
filter_fn=__FilterIfEntry,
modify_fn=__ModifyIfEntry,
Expand All @@ -164,7 +253,7 @@ if __name__ == '__main__':
)

if not params.wizard or any(
config[ifname]['enabled'] for ifname in ifaces
config['ifconfig'][_]['enabled'] for _ in config['ifconfig']
) or ram.widgets.AskViaButtons(
"Network unconfigured",
"No enabled network interfaces found!\n\n"
Expand All @@ -185,9 +274,7 @@ if __name__ == '__main__':
"No present network interfaces found!\n"
)

elif params.device in ifaces:
ActNetworkIface(config, params.device, params.device)
elif params.device is not None:
raise SystemExit("No suitable device found on the machine.")
elif params.device != "no":
ActNetworkIface(config, params.device)

ram.store(input=config)
ram.store('net.network', input=config)
33 changes: 23 additions & 10 deletions lib/ram/net/input
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,26 @@ with ram.context(__name__):
from wiz.entry import UnitEntry

if __name__ == '__main__':
ram.widgets.RunMenu(
"Select action",
[
("Interface configuration ...", UnitEntry('net.ifconfig', apply=False)),
("", 0),
("Routing configuration ...", UnitEntry('net.routing', apply=False)),
("Change the system hostname ...", UnitEntry('net.hostname', apply=False)),
("DNS resolver configuration ...", UnitEntry('net.resolver', apply=False)),
]
)
params = ram.param()
isfqdn = 'fqdn' if params.fqdn else 'nofqdn'

if params.wizard:
ram.widgets.RunList(
[
UnitEntry('net.hostname', 'forced', 'erased', isfqdn, apply=False),
UnitEntry('net.ifconfig', 'wizard', apply=False),
UnitEntry('net.routing', 'wizard', apply=False),
UnitEntry('net.resolver', 'wizard', apply=False),
]
)
else:
ram.widgets.RunMenu(
"Select action",
[
("Interface configuration ...", UnitEntry('net.ifconfig', apply=False)),
("", 0),
("Routing configuration ...", UnitEntry('net.routing', apply=False)),
("Change the system hostname ...", UnitEntry('net.hostname', isfqdn, apply=False)),
("DNS resolver configuration ...", UnitEntry('net.resolver', apply=False)),
]
)

0 comments on commit c7b2930

Please sign in to comment.