You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
calculate-utils-3-install/install/variables/net.py

308 lines
9.3 KiB

#-*- coding: utf-8 -*-
# Copyright 2008-2012 Calculate Ltd. http://www.calculate-linux.org
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import sys
import re
from os import path
from calculate.lib.datavars import Variables,VariableError
from calculate.lib.variables import Net
from calculate.lib.cl_lang import setLocalTranslate
setLocalTranslate('cl_install',sys.modules[__name__])
from calculate.lib.utils.ip import (getInterfaces,getIp,getMask,getMac,
cidrToMask,maskToCidr,getIpNet,isDhcpIp,checkIp,checkMask)
from calculate.lib.utils.device import lspci
class InstallNet(Net):
vars = ["os_net_interfaces_info","os_install_net_data",
"os_install_net_hostname","os_install_net_allow",
"os_install_net_interfaces","os_install_net_name",
"os_install_net_mac","os_install_net_ip","os_install_net_network",
"os_install_net_cidr","os_install_net_route_hash",
"os_install_net_mask",
"os_install_net_route_network","os_install_net_route_gw",
"os_install_net_route_dev","os_install_net_route_src",
"os_install_net_route","os_install_net_nmroute",
"os_install_net_dns","os_install_net_conf",
"os_install_net_settings","os_install_net_dhcp_set",
"os_install_net_dns_search","os_install_net_domain",
"os_install_net_fqdn"]
# inforamation about net interfaces
os_net_interfaces_info = {}
# hash for information about net
os_install_net_data = {'type':'table',
'mode':'w',
'opt':["--ip"],
'metavalue':"IFACE:IP",
'source':["os_install_net_interfaces",
"os_install_net_dhcp_set",
"os_install_net_ip",
"os_install_net_mask",
"os_install_net_name",
"os_install_net_mac"],
'help':_("ip address with net (example:%s)")%"192.168.1.1/24",
'label':_("Addresses") }
# computer hostname
os_install_net_hostname = {'mode':"w"}
# allowed network
os_install_net_allow ={}
# net interfaces
os_install_net_interfaces={
'type':'list',
'mode':'r',
'label':_("Interface")}
# net device name
os_install_net_name={
'type':'list',
'mode':'r',
'label':_("Name")}
# net devices mac
os_install_net_mac={
'type':'list',
'mode':'r',
'label':_("MAC")}
# ip for all network interfaces
os_install_net_ip = {
'type':'list',
'mode':'w',
'label':_("IP address")}
# network for ip
os_install_net_network = {
'type':'list',
'mode':'r',
'label':_("Network")}
# ip cidr
os_install_net_cidr = {
'type':'list',
'mode':'r',
'label':_("CIDR")}
os_install_net_mask = {
'type':'choiceedit-list',
'mode':'w',
'label':_("Mask")}
# dhcp or not
os_install_net_dhcp_set = {
'type':'bool-list',
'mode':'w',
'label':_("DHCP")}
# routing
os_install_net_route_hash = {}
# net for route
os_install_net_route_network = {'value':[]}
# gw for route
os_install_net_route_gw = {'value':[]}
# dev for route
os_install_net_route_dev = {'value':[]}
# src for route
os_install_net_route_src = {'value':[]}
# data by route for conf.d/net
os_install_net_route = {'hide':True}
# data by route for NetworkManager
os_install_net_nmroute = {'hide':True}
# dns servers
os_install_net_dns = {}
# net setup (networkmanager or openrc)
os_install_net_conf = {}
# net service configured
os_install_net_settings = {'mode':'w',
'value':''}
# dns search
os_install_net_dns_search = {'mode':"w"}
# domain
os_install_net_domain = {'mode':"w"}
os_install_net_fqdn = {'mode':"w",
'opt':['--hostname'],
'label':_("Hostname"),
'help':_("set the short hostname or full hostname")}
def set_os_install_net_fqdn(self,value):
if "." in value:
return value
else:
return "%s.%s"%(value,self.Get('os_install_net_domain'))
def check_os_install_net_fqdn(self,value):
maxfqdn = 254
if len(value) > maxfqdn:
raise VariableError(
_("Hostname length should be less that %d")%maxfqdn)
def get_os_install_net_fqdn(self):
return self.Get('os_net_fqdn')
def get_os_install_net_hostname(self):
return self.Get('os_install_net_fqdn').partition('.')[0]
def get_os_install_net_domain(self):
return self.Get('os_install_net_fqdn').partition('.')[2]
def get_os_install_net_interfaces(self):
return getInterfaces()
def get_os_install_net_ip(self):
"""
Get ip for interface (Example:192.168.1.1)
"""
return map(lambda x:getIp(x),self.Get('os_install_net_interfaces'))
def get_os_install_net_cidr(self):
"""
Get CIDR of ip,net (Example: 24)
"""
return map(lambda x:maskToCidr(x),
self.Get('os_install_net_mask'))
def get_os_install_net_mask(self):
"""
Get mask for interfaces (Example: 255.255.0.0)
"""
return map(lambda x:cidrToMask(getMask(x)),
self.Get('os_install_net_interfaces'))
def get_os_install_net_mac(self):
"""
Get MAC for interfaces (Example: 01:02:03:04:05:06)
"""
return map(lambda x:getMac(x),
self.Get('os_install_net_interfaces'))
def get_os_install_net_network(self):
"""
Get networks (Example:192.168.0.0/16)
"""
return map(lambda x:getIpNet(x[0],x[1]) if x[0] else "",
zip(self.Get('os_install_net_ip'),
self.Get('os_install_net_mask')))
def get_os_install_net_dhcp_set(self):
"""
Get method of receiving ip (dhcp on/off)
"""
return map(lambda x:"on" if isDhcpIp(x) else "off",
self.Get('os_install_net_interfaces'))
def get_os_install_net_name(self):
"""
Get name for interfaces
"""
rePci = re.compile(r"(\d\d:\d\d\.\d)(?:/[^/]+){2}$")
def getPci(interface):
pathname = path.realpath(path.join('/sys/class/net',
interface))
pci = rePci.search(pathname)
if pci:
return pci.group(1)
else:
return ""
pciEthernet = lspci(shortInfo=True)
return map(lambda x:"{vendor} {name}".format(**x),
map(lambda x:pciEthernet.get(getPci(x),
{'vendor':_("Unknown"),
'name':_("vendor")}),
self.Get('os_install_net_interfaces')))
def check_os_install_net_ip(self,value):
"""
Check ip address
"""
wrongIp = filter(lambda x:not checkIp(x),value)
if wrongIp:
raise VariableError(_("Wrong IP address %s")%wrongIp[0])
def set_os_install_net_mask(self,value):
"""
Convert to mask CIDR value
"""
def convertCidrToMask(x):
if x and x.isdigit() and int(x) in range(0,33):
return cidrToMask(int(x))
else:
return x
res = map(convertCidrToMask,value)
return res
def check_os_install_net_mask(self,value):
"""
Check mask address
"""
wrongIp = filter(lambda x:not checkMask(x),value)
if wrongIp:
raise VariableError(_("Wrong mask %s")%wrongIp[0])
def choice_os_install_net_mask(self):
"""
List value of mask
"""
return ["255.255.255.255",
"255.255.255.0",
"255.255.0.0",
"255.0.0.0",
"0.0.0.0"]
def set_os_install_net_dhcp_set(self,value):
"""
Get method of receiving ip (dhcp on/off)
"""
return map(lambda x:("on"
if isDhcpIp(x[0]) else "off") if x[1] == "auto" else x[1],
zip(self.Get('os_install_net_interfaces'),
value))
def get_os_install_net_allow(self):
"""Allowed network"""
return self.Get("os_net_allow")
def uncompatible_net_vars(self):
"""
Network setting up unavailable for flash installation
"""
if self.Get('os_install_root_type') == 'flash':
return \
_("Network configuration unavailable for flash installation")
return ""
uncompatible_os_install_net_data = \
uncompatible_os_install_net_ip = \
uncompatible_os_install_net_mask = \
uncompatible_os_install_net_hostname = \
uncompatible_net_vars