Skip to content

Commit

Permalink
add docker proxy minion
Browse files Browse the repository at this point in the history
  • Loading branch information
gtmanfred committed Jun 4, 2018
1 parent d49c2dc commit 8a341cd
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 3 deletions.
1 change: 1 addition & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ additional-builtins=__opts__,
__proxy__,
__serializers__,
__reg__,
__executors__,
__events__

# List of strings which can identify a callback function by name. A callback
Expand Down
31 changes: 31 additions & 0 deletions salt/executors/docker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
'''
Docker executor module
.. versionadded: Fluorine
Used with the docker proxy minion.
'''
from __future__ import absolute_import, unicode_literals


DOCKER_MOD_MAP = {
'state.sls': 'docker.sls',
'state.apply': 'docker.apply',
'state.highstate': 'docker.highstate',
}


def execute(opts, data, func, args, kwargs):
'''
Directly calls the given function with arguments
'''
if data['fun'] == 'saltutil.find_job':
return __executors__['direct_call.execute'](opts, data, func, args, kwargs)
if data['fun'] in DOCKER_MOD_MAP:
return __executors__['direct_call.execute'](opts, data, __salt__[DOCKER_MOD_MAP[data['fun']]], [opts['proxy']['name']] + args, kwargs)
return __salt__['docker.call'](opts['proxy']['name'], data['fun'], *args, **kwargs)


def allow_missing_funcs():
return True
4 changes: 3 additions & 1 deletion salt/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -971,12 +971,14 @@ def executors(opts, functions=None, context=None, proxy=None):
'''
Returns the executor modules
'''
return LazyLoader(
executors = LazyLoader(
_module_dirs(opts, 'executors', 'executor'),
opts,
tag='executor',
pack={'__salt__': functions, '__context__': context or {}, '__proxy__': proxy or {}},
)
executors.pack['__executors__'] = executors
return executors


def cache(opts, serial):
Expand Down
9 changes: 7 additions & 2 deletions salt/minion.py
Original file line number Diff line number Diff line change
Expand Up @@ -1589,8 +1589,9 @@ def _thread_return(cls, minion_instance, opts, data):
data['arg'],
data)
minion_instance.functions.pack['__context__']['retcode'] = 0

executors = data.get('module_executors') or opts.get('module_executors', ['direct_call'])
executors = data.get('module_executors') or \
getattr(minion_instance, 'module_executors', []) or \
opts.get('module_executors', ['direct_call'])
if isinstance(executors, six.string_types):
executors = [executors]
elif not isinstance(executors, list) or not executors:
Expand Down Expand Up @@ -3597,6 +3598,7 @@ def _post_master_init(self, master):
self._running = False
raise SaltSystemExit(code=-1, msg=errmsg)

self.module_executors = self.proxy.get('{0}.module_executors'.format(fq_proxyname), lambda: [])()
proxy_init_fn = self.proxy[fq_proxyname + '.init']
proxy_init_fn(self.opts)

Expand Down Expand Up @@ -3747,6 +3749,9 @@ def _target(cls, minion_instance, opts, data, connected):
minion_instance.proxy.reload_modules()

fq_proxyname = opts['proxy']['proxytype']

minion_instance.module_executors = minion_instance.proxy.get('{0}.module_executors'.format(fq_proxyname), lambda: [])()

proxy_init_fn = minion_instance.proxy[fq_proxyname + '.init']
proxy_init_fn(opts)
if not hasattr(minion_instance, 'serial'):
Expand Down
57 changes: 57 additions & 0 deletions salt/modules/dockermod.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@
'signal_': 'signal',
'start_': 'start',
'tag_': 'tag',
'apply_': 'apply'
}

# Minimum supported versions
Expand All @@ -271,6 +272,13 @@
__virtualname__ = 'docker'
__virtual_aliases__ = ('dockerng', 'moby')

__proxyenabled__ = ['docker']
__outputter__ = {
'sls': 'highstate',
'apply': 'highstate',
'highstate': 'highstate',
}


def __virtual__():
'''
Expand Down Expand Up @@ -6586,6 +6594,9 @@ def _compile_state(sls_opts, mods=None):
'''
st_ = HighState(sls_opts)

if not mods:
return st_.compile_low_chunks()

high_data, errors = st_.render_highstate({sls_opts['saltenv']: mods})
high_data, ext_errors = st_.state.reconcile_extend(high_data)
errors += ext_errors
Expand Down Expand Up @@ -6692,6 +6703,27 @@ def call(name, function, *args, **kwargs):
run_all(name, subprocess.list2cmdline(rm_thin_argv))


def apply_(name, mods=None, **kwargs):
'''
.. versionadded:: Flourine
Apply states! This function will call highstate or state.sls based on the
arguments passed in, ``apply`` is intended to be the main gateway for
all state executions.
CLI Example:
.. code-block:: bash
salt 'docker' docker.apply web01
salt 'docker' docker.apply web01 test
salt 'docker' docker.apply web01 test,pkgs
'''
if mods:
return sls(name, mods, **kwargs)
return highstate(name, **kwargs)


def sls(name, mods=None, **kwargs):
'''
Apply the states defined by the specified SLS modules to the running
Expand Down Expand Up @@ -6809,6 +6841,31 @@ def sls(name, mods=None, **kwargs):
return ret


def highstate(name, saltenv='base', **kwargs):
'''
Apply a highstate to the running container
.. versionadded:: Flourine
The container does not need to have Salt installed, but Python is required.
name
Container name or ID
saltenv : base
Specify the environment from which to retrieve the SLS indicated by the
`mods` parameter.
CLI Example:
.. code-block:: bash
salt myminion docker.highstate compassionate_mirzakhani
'''
return sls(name, saltenv='base', **kwargs)


def sls_build(repository,
tag='latest',
base='opensuse/python',
Expand Down
72 changes: 72 additions & 0 deletions salt/proxy/docker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# -*- coding: utf-8 -*-
'''
Docker Proxy Minion
.. versionadded: Fluorine
:depends: docker
This proxy minion is just a shim to the docker executor, which will use the
:py:func:`docker.call <salt.modules.dockermod.call>` for everything except
state runs.
To configure the proxy minion:
.. code-block:: yaml
proxy:
proxytype: docker
name: festive_leakey
It is also possible to just name the proxy minion the same name as the
container, and use grains to configure the proxy minion:
.. code-block:: yaml
proxy:
proxytype: docker
name: {{grains['id']}}
name
Name of the docker container
'''
from __future__ import absolute_import, unicode_literals

__proxyenabled__ = ['docker']
__virtualname__ = 'docker'


def __virtual__():
if __opts__.get('proxy', {}).get('proxytype') != __virtualname__:
return False, 'Proxytype does not match: {0}'.format(__virtualname__)
return True


def module_executors():
'''
List of module executors to use for this Proxy Minion
'''
return ['docker', ]


def init(opts):
'''
Always initialize
'''
__context__['initialized'] = True


def initialized():
'''
This should always be initialized
'''
return __context__.get('initialized', False)


def shutdown(opts):
'''
Nothing needs to be done to shutdown
'''
__context__['initialized'] = False

0 comments on commit 8a341cd

Please sign in to comment.