More cleanup, found another spot where proxy needed to be passed to a load_modules.

This commit is contained in:
C. R. Oldham 2015-10-07 16:41:02 -06:00
parent 64f967d731
commit e9b281041c
8 changed files with 195 additions and 60 deletions

View file

@ -128,6 +128,7 @@ def _module_dirs(
return cli_module_dirs + ext_type_types + [ext_types, sys_types]
CALLS = {}
def minion_mods(
opts,
@ -181,6 +182,7 @@ def minion_mods(
utils = {}
if proxy is None:
proxy = {}
if not whitelist:
whitelist = opts.get('whitelist_modules', None)
ret = LazyLoader(_module_dirs(opts, 'modules', 'module'),

View file

@ -704,7 +704,7 @@ class Minion(MinionBase):
)
# Late setup the of the opts grains, so we can log from the grains
# module
if 'proxyid' not in self.opts:
if salt.utils.is_proxy():
self.opts['grains'] = salt.loader.grains(opts)
# TODO: remove?
@ -1330,7 +1330,11 @@ class Minion(MinionBase):
Refresh the functions and returners.
'''
log.debug('Refreshing modules. Notify={0}'.format(notify))
self.functions, self.returners, _ = self._load_modules(force_refresh, notify=notify)
try:
self.functions, self.returners, _ = self._load_modules(force_refresh, notify=notify, proxy=self.proxy)
except NameError:
self.functions, self.returners, _ = self._load_modules(force_refresh, notify=notify)
self.schedule.functions = self.functions
self.schedule.returners = self.returners
@ -2511,7 +2515,7 @@ class ProxyMinion(Minion):
fq_proxyname = self.opts['pillar']['proxy']['proxytype']
self.opts['proxy'] = self.opts['pillar']['proxy']
# Need to load the modules so they get all the dunder variables
# # Need to load the modules so they get all the dunder variables
self.functions, self.returners, self.function_errors = self._load_modules()
# we can then sync any proxymodules down from the master

View file

@ -6,6 +6,7 @@ from __future__ import absolute_import
# Import python libs
import logging
import salt.utils
log = logging.getLogger(__name__)
@ -19,7 +20,7 @@ def __virtual__():
'''
Only work on proxy
'''
if 'proxy' in __opts__:
if salt.utils.is_proxy():
return __virtualname__
return False

View file

@ -1,47 +0,0 @@
# -*- coding: utf-8 -*-
'''
Module for interfacing to the REST example
pre-pre-ALPHA QUALITY code.
'''
from __future__ import absolute_import
# Import python libraries
import logging
# Set up logging
log = logging.getLogger(__name__)
# Define the module's virtual name
__virtualname__ = 'rest_example'
__proxyenabled__ = ['rest_example']
def __virtual__():
'''
'''
if 'proxyobject' in __opts__:
return __virtualname__
else:
return False
def grains_refresh():
'''
Refresh the cache.
'''
return __opts__['proxyobject'].grains_refresh()
def ping():
ret = dict()
conn = __opts__['proxyobject']
if conn.ping():
ret['message'] = 'pong'
ret['out'] = True
else:
ret['out'] = False

View file

@ -529,7 +529,7 @@ def highstate(test=None,
if 'pillarenv' in kwargs:
opts['pillarenv'] = kwargs['pillarenv']
st_ = salt.state.HighState(opts, pillar, kwargs.get('__pub_jid'))
st_ = salt.state.HighState(opts, pillar, kwargs.get('__pub_jid'), proxy=__proxy__)
st_.push_active()
try:
ret = st_.call_highstate(
@ -681,10 +681,10 @@ def sls(mods,
'{0}.cache.p'.format(kwargs.get('cache_name', 'highstate'))
)
try:
st_ = salt.state.HighState(opts, pillar, kwargs.get('__pub_jid'), proxy=__proxy__)
except NameError:
st_ = salt.state.HighState(opts, pillar, kwargs.get('__pub_jid'))
# try:
st_ = salt.state.HighState(opts, pillar, kwargs.get('__pub_jid'), proxy=__proxy__)
# except NameError:
# st_ = salt.state.HighState(opts, pillar, kwargs.get('__pub_jid'))
if kwargs.get('cache'):
if os.path.isfile(cfn):

View file

@ -110,9 +110,12 @@ def ping():
salt '*' test.ping
'''
if 'proxymodule' in __opts__:
ping_cmd = __opts__['proxymodule'].loaded_base_name + '.ping'
return __opts__['proxymodule'][ping_cmd]()
ping_cmd = __opts__['proxy']['proxytype'] + '.ping'
if salt.utils.is_proxy():
if __opts__.get('add_proxymodule_to_opts', False):
return __opts__['proxymodule'][ping_cmd]()
else:
return __proxy__[ping_cmd]()
else:
return True

171
salt/proxy/rest_sample.py Normal file
View file

@ -0,0 +1,171 @@
# -*- coding: utf-8 -*-
'''
This is a simple proxy-minion designed to connect to and communicate with
the bottle-based web service contained in https://github.com/salt-contrib/proxyminion_rest_example
'''
from __future__ import absolute_import
# Import python libs
import logging
import salt.utils.http
HAS_REST_EXAMPLE = True
# This must be present or the Salt loader won't load this module
__proxyenabled__ = ['rest_sample']
# Variables are scoped to this module so we can have persistent data
# across calls to fns in here.
GRAINS_CACHE = {}
DETAILS = {}
# Want logging!
log = logging.getLogger(__file__)
# This does nothing, it's here just as an example and to provide a log
# entry when the module is loaded.
def __virtual__():
'''
Only return if all the modules are available
'''
log.debug('rest_sample proxy __virtual__() called...')
return True
# Every proxy module needs an 'init', though you can
# just put a 'pass' here if it doesn't need to do anything.
def init(opts):
log.debug('rest_sample proxy init() called...')
# Save the REST URL
DETAILS['url'] = opts['proxy']['url']
# Make sure the REST URL ends with a '/'
if not DETAILS['url'].endswith('/'):
DETAILS['url'] += '/'
def id(opts):
'''
Return a unique ID for this proxy minion. This ID MUST NOT CHANGE.
If it changes while the proxy is running the salt-master will get
really confused and may stop talking to this minion
'''
r = salt.utils.http.query(opts['proxy']['url']+'id', decode_type='json', decode=True)
return r['dict']['id'].encode('ascii', 'ignore')
def grains():
'''
Get the grains from the proxied device
'''
if not GRAINS_CACHE:
r = salt.utils.http.query(DETAILS['url']+'info', decode_type='json', decode=True)
GRAINS_CACHE = r['dict']
return GRAINS_CACHE
def grains_refresh():
'''
Refresh the grains from the proxied device
'''
GRAINS_CACHE = {}
return grains()
def service_start(name):
'''
Start a "service" on the REST server
'''
r = salt.utils.http.query(DETAILS['url']+'service/start/'+name, decode_type='json', decode=True)
return r['dict']
def service_stop(name):
'''
Stop a "service" on the REST server
'''
r = salt.utils.http.query(DETAILS['url']+'service/stop/'+name, decode_type='json', decode=True)
return r['dict']
def service_restart(name):
'''
Restart a "service" on the REST server
'''
r = salt.utils.http.query(DETAILS['url']+'service/restart/'+name, decode_type='json', decode=True)
return r['dict']
def service_list():
'''
List "services" on the REST server
'''
r = salt.utils.http.query(DETAILS['url']+'service/list', decode_type='json', decode=True)
return r['dict']
def service_status(name):
'''
Check if a service is running on the REST server
'''
r = salt.utils.http.query(DETAILS['url']+'service/status/'+name, decode_type='json', decode=True)
return r['dict']
def package_list():
'''
List "packages" installed on the REST server
'''
r = salt.utils.http.query(DETAILS['url']+'package/list', decode_type='json', decode=True)
return r['dict']
def package_install(name, **kwargs):
'''
Install a "package" on the REST server
'''
cmd = DETAILS['url']+'package/install/'+name
if 'version' in kwargs:
cmd += '/'+kwargs['version']
else:
cmd += '/1.0'
r = salt.utils.http.query(cmd, decode_type='json', decode=True)
return r['dict']
def package_remove(name):
'''
Remove a "package" on the REST server
'''
r = salt.utils.http.query(DETAILS['url']+'package/remove/'+name, decode_type='json', decode=True)
return r['dict']
def package_status(name):
'''
Check the installation status of a package on the REST server
'''
r = salt.utils.http.query(DETAILS['url']+'package/status/'+name, decode_type='json', decode=True)
return r['dict']
def ping():
'''
Is the REST server up?
'''
r = salt.utils.http.query(DETAILS['url']+'ping', decode_type='json', decode=True)
try:
return r['dict'].get('ret', False)
except Exception:
return False
def shutdown(opts):
'''
For this proxy shutdown is a no-op
'''
log.debug('rest_sample proxy shutdown() called...')
pass

View file

@ -597,6 +597,7 @@ class State(object):
if 'grains' not in opts:
opts['grains'] = salt.loader.grains(opts)
self.opts = opts
self.proxy = proxy
self._pillar_override = pillar
self.opts['pillar'] = self._gather_pillar()
self.state_con = {}
@ -774,7 +775,7 @@ class State(object):
reload(site)
except RuntimeError:
log.error('Error encountered during module reload. Modules were not reloaded.')
self.load_modules()
self.load_modules(proxy=self.proxy)
if not self.opts.get('local', False) and self.opts.get('multiprocessing', True):
self.functions['saltutil.refresh_modules']()