Commit 2ead74a6 by Oliver Pinter

freebsd context update and networking update

* load virtio_console if not loaded
* implmeneted time update
* and some networking related function
parent 26290f66
......@@ -32,7 +32,7 @@ from hashlib import md5
from ssh import PubKey
from .network import change_ip_ubuntu, change_ip_rhel
from .network import change_ip_freebsd
from context import BaseContext
from twisted.internet import reactor
......@@ -65,7 +65,7 @@ class Context(BaseContext):
# http://stackoverflow.com/questions/12081310/
# python-module-to-change-system-date-and-time
def _linux_set_time(time):
def _freebsd_set_time(time):
import ctypes
import ctypes.util
......@@ -91,45 +91,30 @@ class Context(BaseContext):
@staticmethod
def restart_networking():
if distro == 'debian':
subprocess.call(['/etc/init.d/networking', 'restart'])
elif distro == 'rhel':
subprocess.call(['/bin/systemctl', 'restart', 'network'])
pass
subprocess.call(['/sbin/service', 'netif', 'restart'])
@staticmethod
def change_ip(interfaces, dns):
if distro == 'debian':
change_ip_ubuntu(interfaces, dns)
elif distro == 'rhel':
change_ip_rhel(interfaces, dns)
change_ip_freebsd(interfaces, dns)
@staticmethod
def set_time(time):
Context._linux_set_time(float(time))
Context._freebsd_set_time(float(time))
try:
subprocess.call(['/etc/init.d/ntp', 'restart'])
subprocess.call(['/usr/sbin/service' 'ntpd', 'onerestart'])
except:
pass
@staticmethod
def set_hostname(hostname):
if distro == 'debian':
with open('/etc/hostname', 'w') as f:
f.write(hostname)
elif distro == 'rhel':
for line in fileinput.input('/etc/sysconfig/network',
inplace=1):
if line.startswith('HOSTNAME='):
print 'HOSTNAME=%s' % hostname
else:
print line.rstrip()
with open('/etc/hostname', 'w') as f:
f.write(hostname)
with open('/etc/hosts', 'w') as f:
f.write("127.0.0.1 localhost\n"
"127.0.1.1 %s\n" % hostname)
subprocess.call(['/bin/hostname', hostname])
subprocess.call(['/usr/sbin/service', 'hostname', 'restart'])
@staticmethod
def mount_store(host, username, password):
......
......@@ -6,13 +6,11 @@ import subprocess
logger = logging.getLogger()
interfaces_file = '/etc/network/interfaces'
ifcfg_template = '/etc/sysconfig/network-scripts/ifcfg-%s'
interfaces_file = '/etc/rc.conf.d/ifconfig_'
def get_interfaces_linux(interfaces):
def get_interfaces_freebsd(interfaces):
for ifname in netifaces.interfaces():
mac = netifaces.ifaddresses(ifname)[17][0]['addr']
mac = netifaces.ifaddresses(ifname)[18][0]['addr']
conf = interfaces.get(mac.upper())
if conf:
yield ifname, conf
......@@ -52,15 +50,12 @@ def remove_interfaces_ubuntu(devices):
print line
def change_ip_ubuntu(interfaces, dns):
data = list(get_interfaces_linux(interfaces))
def change_ip_freebsd(interfaces, dns):
data = list(get_interfaces_freebsd(interfaces))
for ifname, conf in data:
subprocess.call(('/sbin/ifdown', ifname))
subprocess.call(('/sbin/ip', 'addr', 'flush', 'dev', ifname))
subprocess.call(('/sbin/ip', 'link', 'set', 'dev', ifname,
'down'))
remove_interfaces_ubuntu(dict(data).keys())
subprocess.call(('/usr/sbin/service','netif', 'stop', ifname))
#remove_interfaces_ubuntu(dict(data).keys())
with open(interfaces_file, 'a') as f:
for ifname, conf in data:
......@@ -105,35 +100,3 @@ def change_ip_ubuntu(interfaces, dns):
# u'gw4': u'10.255.255.1', u'addresses': [u'10.255.255.9']}},
# '8.8.8.8')
def change_ip_rhel(interfaces, dns):
for ifname, conf in get_interfaces_linux(interfaces):
subprocess.call(('/sbin/ifdown', ifname))
subprocess.call(('/sbin/ip', 'addr', 'flush', 'dev', ifname))
subprocess.call(('/sbin/ip', 'link', 'set', 'dev', ifname, 'down'))
with open(ifcfg_template % ifname,
'w') as f:
f.write('DEVICE=%s\n'
'DNS1=%s\n'
'BOOTPROTO=none\n'
'NM_CONTROLLED=no\n'
'USERCTL=no\n'
'ONBOOT=yes\n' % (ifname, dns))
for i in conf['addresses']:
ip_with_prefix = IPNetwork(i)
ip = ip_with_prefix.ip
if ip.version == 6:
f.write('IPV6INIT=yes\n'
'IPV6ADDR=%(ip)s/%(prefixlen)d\n'
'IPV6_DEFAULTGW=%(gw)s\n' % {
'ip': ip,
'prefixlen': ip_with_prefix.prefixlen,
'gw': conf['gw6']})
else:
f.write('NETMASK=%(netmask)s\n'
'IPADDR=%(ip)s\n'
'GATEWAY=%(gw)s\n' % {
'ip': ip,
'netmask': str(ip_with_prefix.netmask),
'gw': conf['gw4']})
subprocess.call(('/sbin/ifup', ifname))
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 sign in to comment