Merge pull request #1822 from bretep/feature/Issue-1817

Feature/issue 1817
This commit is contained in:
Thomas S Hatch 2012-08-13 22:38:06 -07:00
commit af7b649495
2 changed files with 56 additions and 22 deletions

View File

@ -5,6 +5,7 @@ The networking module for RHEL/Fedora based distros
import logging
import re
from os.path import exists, join
import StringIO
# import third party libs
import jinja2
@ -703,6 +704,14 @@ def _write_file_network(data, filename):
fout.write(data)
fout.close()
def _read_temp(data):
tout = StringIO.StringIO()
tout.write(data)
tout.seek(0)
output = tout.readlines()
tout.close()
return output
def build_bond(iface, settings):
'''
@ -727,6 +736,9 @@ def build_bond(iface, settings):
__salt__['cmd.run']('cat {0} >> /etc/modprobe.conf'.format(path))
__salt__['kmod.load']('bonding')
if settings['test']:
return _read_temp(data)
return _read_file(path)
@ -765,8 +777,12 @@ def build_interface(iface, iface_type, enabled, settings):
template = env.get_template('rh{0}_eth.jinja'.format(rh_major))
ifcfg = template.render(opts)
if settings['test']:
return _read_temp(ifcfg)
_write_file_iface(iface, ifcfg, _RH_NETWORK_SCRIPT_DIR, 'ifcfg-{0}')
path = join(_RH_NETWORK_SCRIPT_DIR, 'ifcfg-{0}'.format(iface))
return _read_file(path)
@ -865,6 +881,9 @@ def build_network_settings(settings):
opts = _parse_network_settings(settings,current_network_settings)
template = env.get_template('network.jinja')
network = template.render(opts)
if settings['test']:
return _read_temp(network)
# Wirte settings
_write_file_network(network, _RH_NETWORK_FILE)

View File

@ -165,6 +165,7 @@ def managed(name, type, enabled=True, **kwargs):
'result': True,
'comment': 'Interface {0} is up to date.'.format(name)
}
kwargs['test'] = __opts__['test']
# Build interface
try:
@ -172,23 +173,21 @@ def managed(name, type, enabled=True, **kwargs):
new = __salt__['ip.build_interface'](name, type, enabled, kwargs)
if __opts__['test']:
if old == new:
return ret
pass
if not old and new:
ret['result'] = None
ret['comment'] = 'Interface {0} is set to be added.'
ret['comment'] = ret['comment'].format(name)
return ret
ret['comment'] = 'Interface {0} is set to be added.'.format(name)
elif old != new:
diff = difflib.unified_diff(old, new)
ret['result'] = None
ret['comment'] = 'Interface {0} is set to be updated.'
ret['comment'] = ret['comment'].format(
name)
return ret
if not old and new:
ret['changes']['interface'] = 'Added network interface.'
elif old != new:
diff = difflib.unified_diff(old, new)
ret['changes']['interface'] = ''.join(diff)
ret['comment'] = 'Interface {0} is set to be updated.'.format(name)
ret['changes']['interface'] = ''.join(diff)
else:
if not old and new:
ret['changes']['interface'] = 'Added network interface.'
elif old != new:
diff = difflib.unified_diff(old, new)
ret['changes']['interface'] = ''.join(diff)
except AttributeError as error:
ret['result'] = False
ret['comment'] = error.message
@ -199,17 +198,32 @@ def managed(name, type, enabled=True, **kwargs):
try:
old = __salt__['ip.get_bond'](name)
new = __salt__['ip.build_bond'](name, kwargs)
if not old and new:
ret['changes']['bond'] = 'Added bond.'
elif old != new:
diff = difflib.unified_diff(old, new)
ret['changes']['bond'] = ''.join(diff)
if __opts__['test']:
if old == new:
pass
if not old and new:
ret['result'] = None
ret['comment'] = 'Bond interface {0} is set to be added.'.format(name)
elif old != new:
diff = difflib.unified_diff(old, new)
ret['result'] = None
ret['comment'] = 'Bond interface {0} is set to be updated.'.format(name)
ret['changes']['bond'] = ''.join(diff)
else:
if not old and new:
ret['changes']['bond'] = 'Added bond {0}.'.format(name)
elif old != new:
diff = difflib.unified_diff(old, new)
ret['changes']['bond'] = ''.join(diff)
except AttributeError as error:
#TODO Add a way of reversing the interface changes.
ret['result'] = False
ret['comment'] = error.message
return ret
if __opts__['test']:
return ret
#Bring up/shutdown interface
try:
if enabled:
@ -243,6 +257,7 @@ def system(name, **kwargs):
'comment': 'Global network settings are up to date.'
}
apply_net_settings = False
kwargs['test'] = __opts__['test']
# Build global network settings
try:
old = __salt__['ip.get_network_settings']()
@ -255,14 +270,14 @@ def system(name, **kwargs):
ret['comment'] = 'Global network settings are set to be added.'
return ret
elif old != new:
diff = difflib.unified_diff(old, new)
ret['result'] = None
ret['comment'] = \
'Global network settings are set to be updated.'
ret['comment'] = 'Global network settings are set to be updated.'
ret['changes']['network_settings'] = ''.join(diff)
return ret
if not old and new:
apply_net_settings = True
ret['changes']['network_settings'] = \
'Added global network settings.'
ret['changes']['network_settings'] = 'Added global network settings.'
elif old != new:
diff = difflib.unified_diff(old, new)
apply_net_settings = True