mirror of
https://github.com/valitydev/salt.git
synced 2024-11-07 08:58:59 +00:00
Add conf files to /etc/modprobe.d
This commit is contained in:
parent
6c78e2246e
commit
ca8ecedf90
@ -43,6 +43,7 @@ _RH_CONFIG_BONDING_OPTS = [
|
||||
'use_carrier', 'lacp_rate', 'hashing-algorithm'
|
||||
]
|
||||
_RH_NETWORK_SCRIPT_DIR = '/etc/sysconfig/network-scripts'
|
||||
_RH_NETWORK_CONF_FILES = '/etc/modprobe.d'
|
||||
_MAC_REGEX = re.compile('([0-9A-F]{1,2}:){5}[0-9A-F]{1,2}')
|
||||
_CONFIG_TRUE = [ 'yes', 'on', 'true', '1', True]
|
||||
_CONFIG_FALSE = [ 'no', 'off', 'false', '0', False]
|
||||
@ -101,11 +102,7 @@ def _parse_ethtool_opts(opts, iface):
|
||||
else:
|
||||
_raise_error(iface, option, valid)
|
||||
|
||||
result = []
|
||||
for key in config:
|
||||
result.append(key)
|
||||
result.append(str(config[key]))
|
||||
return ' '.join(result)
|
||||
return config
|
||||
|
||||
def _parse_settings_bond(opts, iface):
|
||||
'''
|
||||
@ -444,29 +441,36 @@ def _raise_error(iface, option, expected):
|
||||
log.error(msg)
|
||||
raise AttributeError(msg)
|
||||
|
||||
def _read_file(iface):
|
||||
def _read_file(path):
|
||||
'''
|
||||
Reads and returns the contents of a file
|
||||
'''
|
||||
path = join(_RH_NETWORK_SCRIPT_DIR, 'ifcfg-%s' % iface)
|
||||
try:
|
||||
with open(path, 'rb') as contents:
|
||||
return contents.readlines()
|
||||
except:
|
||||
return ''
|
||||
|
||||
def _write_file(iface, data):
|
||||
filename = join(_RH_NETWORK_SCRIPT_DIR, 'ifcfg-%s' % iface)
|
||||
if not exists(_RH_NETWORK_SCRIPT_DIR):
|
||||
def _write_file(iface, data, folder, pattern):
|
||||
filename = join(folder, pattern % iface)
|
||||
if not exists(folder):
|
||||
msg = '%s cannot be written. %s does not exists'
|
||||
msg = msg % (filename, _RH_NETWORK_SCRIPT_DIR)
|
||||
msg = msg % (filename, folder)
|
||||
log.error(msg)
|
||||
raise AttributeError(msg)
|
||||
fout = open(filename, 'w')
|
||||
fout.write(data)
|
||||
fout.close()
|
||||
|
||||
def build(iface, type, settings):
|
||||
def build_bond(iface, settings):
|
||||
opts = _parse_settings_eth(settings, iface)
|
||||
template = env.get_template('conf.jinja')
|
||||
data = template.render(opts)
|
||||
_write_file(iface, data, _RH_NETWORK_CONF_FILES, '%s.conf')
|
||||
path = join(_RH_NETWORK_CONF_FILES, '%s.conf' % iface)
|
||||
return _read_file(path)
|
||||
|
||||
def build_interface(iface, type, settings):
|
||||
if type not in _IFACE_TYPES:
|
||||
_raise_error(iface, type, _IFACE_TYPES)
|
||||
|
||||
@ -482,8 +486,14 @@ def build(iface, type, settings):
|
||||
template = env.get_template('eth.jinja')
|
||||
ifcfg = template.render(opts)
|
||||
|
||||
_write_file(iface, ifcfg)
|
||||
return _read_file(iface)
|
||||
_write_file(iface, ifcfg, _RH_NETWORK_SCRIPT_DIR, 'ifcfg-%s')
|
||||
path = join(_RH_NETWORK_SCRIPT_DIR, 'ifcfg-%s' % iface)
|
||||
return _read_file(path)
|
||||
|
||||
def get(iface):
|
||||
return _read_file(iface)
|
||||
def get_bond(iface):
|
||||
path = join(_RH_NETWORK_CONF_FILES, '%s.conf' % iface)
|
||||
return _read_file(path)
|
||||
|
||||
def get_interface(iface):
|
||||
path = join(_RH_NETWORK_SCRIPT_DIR, 'ifcfg-%s' % iface)
|
||||
return _read_file(path)
|
||||
|
2
salt/modules/rh_network/conf.jinja
Normal file
2
salt/modules/rh_network/conf.jinja
Normal file
@ -0,0 +1,2 @@
|
||||
alias {{name}} bonding
|
||||
options {{name}}{% for item in ethtool %}{{item}}={{ethtool[item]}} {%endfor%}
|
@ -3,7 +3,7 @@
|
||||
{% for server in dns -%}
|
||||
DNS{{loop.index}}={{server}}
|
||||
{% endfor -%}
|
||||
{% if ethtool %}ETHTOOL_OPTS={{ethtool}}
|
||||
{% if ethtool %}ETHTOOL_OPTS={%for item in ethtool %}{{item}} {{ethtool[item]}} {%endfor%}
|
||||
{%endif%}{% if gateway %}GATEWAY={{gateway}}
|
||||
{%endif%}{% if addr %}HWADDR={{addr}}
|
||||
{%endif%}{% if ipaddr %}IPADDR={{ipaddr}}
|
||||
|
@ -108,16 +108,32 @@ def managed(
|
||||
# get proposed iface submit to builder
|
||||
# diff iface
|
||||
try:
|
||||
old = __salt__['network.get'](name)
|
||||
new = __salt__['network.build'](name, type, kwargs)
|
||||
old = __salt__['network.get_interface'](name)
|
||||
new = __salt__['network.build_interface'](name, type, kwargs)
|
||||
if not old and new:
|
||||
ret['changes']['diff'] = 'Added ifcfg script'
|
||||
ret['changes']['interface'] = 'Added network interface'
|
||||
elif old != new:
|
||||
diff = difflib.unified_diff(old, new)
|
||||
ret['changes']['diff'] = ''.join(diff)
|
||||
ret['changes']['interface'] = ''.join(diff)
|
||||
except AttributeError, error:
|
||||
ret['result'] = False
|
||||
ret['comment'] = error.message
|
||||
return ret
|
||||
|
||||
if type == 'bond':
|
||||
try:
|
||||
old = __salt__['network.get_bond'](name)
|
||||
new = __salt__['network.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)
|
||||
except AttributeError, error:
|
||||
#TODO Add a way of reversing the interface changes.
|
||||
ret['result'] = False
|
||||
ret['comment'] = error.message
|
||||
return ret
|
||||
|
||||
return ret
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user