Created
September 18, 2013 22:50
-
-
Save mech422/6616850 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Support for Debconf | |
''' | |
# Import python libs | |
import logging | |
import os | |
import re | |
# Import salt libs | |
import salt.utils | |
log = logging.getLogger(__name__) | |
__func_alias__ = { | |
'set_': 'set' | |
} | |
def __virtual__(): | |
''' | |
Confirm this module is on a Debian based system and that debconf-utils | |
is installed. | |
''' | |
if __grains__['os_family'] != 'Debian': | |
return False | |
if salt.utils.which('debconf-get-selections') is None: | |
log.info('Package debconf-utils is not installed.') | |
return False | |
return 'debconf' | |
def _unpack_lines(out): | |
''' | |
Unpack the debconf lines | |
''' | |
rexp = ('(?ms)' | |
'^(?P<package>[^#]\\S+)[\t ]+' | |
'(?P<question>\\S+)[\t ]+' | |
'(?P<type>\\S+)[\t ]+' | |
'(?P<value>[^\n]*)$') | |
lines = re.findall(rexp, out) | |
return lines | |
def get_selections(fetchempty=True): | |
''' | |
Answers to debconf questions for all packages in the following format:: | |
{'package': [['question', 'type', 'value'], ...]} | |
CLI Example: | |
.. code-block:: bash | |
salt '*' debconf.get_selections | |
''' | |
selections = {} | |
cmd = 'debconf-get-selections' | |
out = __salt__['cmd.run_stdout'](cmd) | |
lines = _unpack_lines(out) | |
for line in lines: | |
package, question, type_, value = line | |
if fetchempty or value: | |
(selections | |
.setdefault(package, []) | |
.append([question, type_, value])) | |
return selections | |
def show(name): | |
''' | |
Answers to debconf questions for a package in the following format:: | |
[['question', 'type', 'value'], ...] | |
If debconf doesn't know about a package, we return None. | |
CLI Example: | |
.. code-block:: bash | |
salt '*' debconf.show <package name> | |
''' | |
selections = get_selections() | |
result = selections.get(name) | |
return result | |
def _set_file(path): | |
''' | |
Execute the set selections command for debconf | |
''' | |
path = _get_template_texts(source_list = [ path ], | |
template='jinja', | |
defaults = None, | |
context = None, | |
env = 'base', **kwargs) | |
cmd = 'debconf-set-selections {0}'.format(path) | |
__salt__['cmd.run_stdout'](cmd) | |
def set_(package, question, type, value, *extra): | |
''' | |
Set answers to debconf questions for a package. | |
CLI Example: | |
.. code-block:: bash | |
salt '*' debconf.set <package> <question> <type> <value> [<value> ...] | |
''' | |
if extra: | |
value = ' '.join((value,) + tuple(extra)) | |
fd_, fname = salt.utils.mkstemp(prefix="salt-", close_fd=False) | |
line = "{0} {1} {2} {3}".format(package, question, type, value) | |
os.write(fd_, line) | |
os.close(fd_) | |
_set_file(fname) | |
os.unlink(fname) | |
return True | |
def set_file(path, **kwargs): | |
''' | |
Set answers to debconf questions from a file. | |
CLI Example: | |
.. code-block:: bash | |
salt '*' debconf.set_file salt://pathto/pkg.selections | |
''' | |
path = __salt__['cp.cache_file'](path, kwargs.get('__env__', 'base')) | |
if path: | |
_set_file(path) | |
return True | |
return False | |
def _get_template_texts(source_list = [], template='jinja', defaults = None, | |
context = None, env = 'base', **kwargs): | |
''' | |
Iterate a list of sources and process them as templates. | |
Returns a list of 'chunks' containing the rendered templates. | |
''' | |
ret = {'name': '_get_template_texts', 'changes': {}, | |
'result': True, 'comment': '', 'data': []} | |
if not source_list: | |
return _error(ret, | |
'_get_template_texts called with empty source_list') | |
txtl = [] | |
for (source, source_hash) in source_list: | |
# FIX TODO: Remove this when | |
# http://github.com/saltstack/salt/issues/7290 | |
# is closed. Until then, that bug is why its broke :-P | |
tmpctx = defaults if defaults else {} | |
if context: | |
tmpctx.update(context) | |
rndrd_templ_fn = __salt__['cp.get_template'](source,'', | |
template=template, env=env, | |
context = tmpctx, **kwargs ) | |
msg = 'cp.get_template returned {0} (Called with: {1})' | |
log.debug(msg.format(rndrd_templ_fn,source)) | |
if rndrd_templ_fn: | |
tmplines = None | |
with salt.utils.fopen(rndrd_templ_fn, 'rb') as fp_: | |
tmplines = fp_.readlines() | |
if not tmplines: | |
msg = 'Failed to read rendered template file {0} ({1})' | |
log.debug( msg.format(rndrd_templ_fn,source)) | |
ret['name'] = source | |
return _error(ret, msg.format( rndrd_templ_fn,source) ) | |
txtl.append( ''.join(tmplines)) | |
else: | |
msg = 'Failed to load template file {0}'.format(source) | |
log.debug(msg) | |
ret['name'] = source | |
return _error(ret, msg ) | |
ret['data'] = txtl | |
return ret | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment