mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Update to reflect refactor to LazyLoader
This commit is contained in:
parent
5d390d3a5f
commit
3b746ac2f6
7 changed files with 148 additions and 130 deletions
|
@ -416,7 +416,8 @@ class ProxyMinion(parsers.MinionOptionParser):
|
|||
If sub-classed, run any shutdown operations on this method.
|
||||
'''
|
||||
if 'proxymodule' in self.minion.opts:
|
||||
self.minion.opts['proxymodule']['shutdown'](self.minion.opts)
|
||||
proxy_fn = self.minion.opts['proxymodule'].loaded_base_name + '.shutdown'
|
||||
self.minion.opts['proxymodule'][proxy_fn](self.minion.opts)
|
||||
logger.info('The proxy minion is shut down')
|
||||
|
||||
|
||||
|
|
|
@ -13,6 +13,11 @@ def __virtual__():
|
|||
else:
|
||||
return __virtualname__
|
||||
|
||||
def kernel():
|
||||
return {'kernel':'proxy'}
|
||||
|
||||
def os():
|
||||
return {'os':'proxy'}
|
||||
|
||||
def location():
|
||||
return {'location': 'In this darn virtual machine. Let me out!'}
|
||||
|
@ -23,4 +28,4 @@ def os_family():
|
|||
|
||||
|
||||
def os_data():
|
||||
return __opts__['proxyobject'].grains()
|
||||
return {'os_data': 'funkyHttp release 1.0.a.4.g'}
|
||||
|
|
|
@ -207,7 +207,7 @@ def raw_mod(opts, name, functions, mod='modules'):
|
|||
return dict(loader._dict) # return a copy of *just* the funcs for `name`
|
||||
|
||||
|
||||
def proxy(opts, functions, whitelist=None):
|
||||
def proxy(opts, functions, whitelist=None, loaded_base_name=None):
|
||||
'''
|
||||
Returns the proxy module for this salt-proxy-minion
|
||||
'''
|
||||
|
@ -216,6 +216,7 @@ def proxy(opts, functions, whitelist=None):
|
|||
tag='proxy',
|
||||
whitelist=whitelist,
|
||||
pack={'__proxy__': functions},
|
||||
loaded_base_name=loaded_base_name
|
||||
)
|
||||
|
||||
|
||||
|
@ -1032,14 +1033,20 @@ class LazyLoader(salt.utils.lazy.LazyDict):
|
|||
# If this is a proxy minion then MOST modules cannot work. Therefore, require that
|
||||
# any module that does work with salt-proxy-minion define __proxyenabled__ as a list
|
||||
# containing the names of the proxy types that the module supports.
|
||||
if not hasattr(mod, 'render') and 'proxy' in self.opts:
|
||||
if not hasattr(mod, '__proxyenabled__') or \
|
||||
(self.opts['proxy']['proxytype'] not in mod.__proxyenabled__ and
|
||||
'*' not in mod.__proxyenabled__):
|
||||
err_string = 'not a proxy_minion enabled module'
|
||||
self.missing_modules[module_name] = err_string
|
||||
self.missing_modules[name] = err_string
|
||||
return False
|
||||
#
|
||||
# Render modules and state modules are OK though
|
||||
if 'proxy' in self.opts:
|
||||
log.debug('self.tag is {}'.format(self.tag))
|
||||
# import pydevd
|
||||
# pydevd.settrace('172.16.207.1', port=65500, stdoutToServer=True, stderrToServer=True)
|
||||
if self.tag not in ['render', 'states']:
|
||||
if not hasattr(mod, '__proxyenabled__') or \
|
||||
(self.opts['proxy']['proxytype'] not in mod.__proxyenabled__ and
|
||||
'*' not in mod.__proxyenabled__):
|
||||
err_string = 'not a proxy_minion enabled module'
|
||||
self.missing_modules[module_name] = err_string
|
||||
self.missing_modules[name] = err_string
|
||||
return False
|
||||
|
||||
if getattr(mod, '__load__', False) is not False:
|
||||
log.info(
|
||||
|
|
|
@ -749,7 +749,6 @@ class Minion(MinionBase):
|
|||
if pid > 0:
|
||||
continue
|
||||
else:
|
||||
reinit_crypto()
|
||||
log.debug('---------- making object')
|
||||
proxyminion = salt.cli.daemons.ProxyMinion()
|
||||
log.debug('---------- object made')
|
||||
|
@ -2933,18 +2932,22 @@ class ProxyMinion(Minion):
|
|||
opts['master'] = self.eval_master(opts,
|
||||
timeout,
|
||||
safe)
|
||||
|
||||
fq_proxyname = 'proxy.'+opts['proxy']['proxytype']
|
||||
fq_proxyname = opts['proxy']['proxytype']
|
||||
log.debug('------------------ proxy module --------')
|
||||
log.debug('{}'.format(opts['proxy']))
|
||||
self.proxymodule = salt.loader.proxy(opts, fq_proxyname)
|
||||
# Need to match the function signature of the other loader fns
|
||||
# which is def proxy(opts, functions, whitelist=None, loaded_base_name=None)
|
||||
# 'functions' for other loaders is a LazyLoader object
|
||||
# but since we are not needing to merge functions into another fn dictionary
|
||||
# we will pass 'None' in
|
||||
self.proxymodule = salt.loader.proxy(opts, None, loaded_base_name=fq_proxyname)
|
||||
log.debug('------------------ proxy module init --------')
|
||||
log.debug('{}'.format(self.proxymodule.keys()))
|
||||
log.debug('{0}'.format(self.proxymodule))
|
||||
# log.debug('{0}'.format(self.proxymodule['init']))
|
||||
opts['proxymodule'] = self.proxymodule
|
||||
opts['grains'] = salt.loader.grains(opts)
|
||||
opts['id'] = opts['proxymodule']['junos.id'](opts)
|
||||
opts['id'] = opts['proxymodule'][fq_proxyname+'.id']()
|
||||
opts.update(resolve_dns(opts))
|
||||
self.opts = opts
|
||||
self.authenticate(timeout, safe)
|
||||
|
@ -2955,6 +2958,7 @@ class ProxyMinion(Minion):
|
|||
opts['environment'],
|
||||
pillarenv=opts.get('pillarenv'),
|
||||
).compile_pillar()
|
||||
opts['proxymodule'][fq_proxyname+'.init']()
|
||||
self.functions, self.returners, self.function_errors = self._load_modules()
|
||||
self.serial = salt.payload.Serial(self.opts)
|
||||
self.mod_opts = self._prep_mod_opts()
|
||||
|
|
|
@ -106,7 +106,8 @@ def ping():
|
|||
'''
|
||||
|
||||
if 'proxymodule' in __opts__:
|
||||
return __opts__['proxymodule']['junos.ping']()
|
||||
ping_cmd = __opts__['proxymodule'].loaded_base_name + '.ping'
|
||||
return __opts__['proxymodule'][ping_cmd]()
|
||||
else:
|
||||
return True
|
||||
|
||||
|
|
|
@ -22,17 +22,17 @@ thisproxy = {}
|
|||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
def __init__(opts):
|
||||
'''
|
||||
Open the connection to the Junos device, login, and bind to the
|
||||
Resource class
|
||||
'''
|
||||
log.debug('Opening connection to junos')
|
||||
thisproxy['conn'] = jnpr.junos.Device(user=opts['proxy']['username'],
|
||||
host=opts['proxy']['host'],
|
||||
password=opts['proxy']['passwd'])
|
||||
thisproxy['conn'].open()
|
||||
thisproxy['conn'].bind(cu=jnpr.junos.utils.config.Config)
|
||||
# def __init__(opts):
|
||||
# '''
|
||||
# Open the connection to the Junos device, login, and bind to the
|
||||
# Resource class
|
||||
# '''
|
||||
# log.debug('Opening connection to junos')
|
||||
# thisproxy['conn'] = jnpr.junos.Device(user=opts['proxy']['username'],
|
||||
# host=opts['proxy']['host'],
|
||||
# password=opts['proxy']['passwd'])
|
||||
# thisproxy['conn'].open()
|
||||
# thisproxy['conn'].bind(cu=jnpr.junos.utils.config.Config)
|
||||
|
||||
def conn():
|
||||
return thisproxy['conn']
|
||||
|
|
|
@ -19,6 +19,9 @@ HAS_REST_EXAMPLE = True
|
|||
|
||||
__proxyenabled__ = ['rest_sample']
|
||||
|
||||
grains_cache = {}
|
||||
url = 'http://172.16.207.1:8000/'
|
||||
|
||||
|
||||
def __virtual__():
|
||||
'''
|
||||
|
@ -26,122 +29,119 @@ def __virtual__():
|
|||
'''
|
||||
if not HAS_REQUESTS:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
class Proxyconn(object):
|
||||
# Interface with the REST sample web service (rest.py at
|
||||
# https://github.com/cro/salt-proxy-rest)
|
||||
def init():
|
||||
pass
|
||||
|
||||
def id():
|
||||
'''
|
||||
Interface with the REST sample web service (rest.py at
|
||||
https://github.com/cro/salt-proxy-rest)
|
||||
Return a unique ID for this proxy minion
|
||||
'''
|
||||
def __init__(self, details):
|
||||
self.url = details['url']
|
||||
self.grains_cache = {}
|
||||
r = requests.get(url+'id')
|
||||
return r.text.encode('ascii', 'ignore')
|
||||
|
||||
def id(self, opts):
|
||||
'''
|
||||
Return a unique ID for this proxy minion
|
||||
'''
|
||||
r = requests.get(self.url+'id')
|
||||
return r.text.encode('ascii', 'ignore')
|
||||
def grains():
|
||||
'''
|
||||
Get the grains from the proxied device
|
||||
'''
|
||||
if not grains_cache:
|
||||
r = requests.get(url+'info')
|
||||
self.grains_cache = r.json()
|
||||
return grains_cache
|
||||
|
||||
def grains(self):
|
||||
'''
|
||||
Get the grains from the proxied device
|
||||
'''
|
||||
if not self.grains_cache:
|
||||
r = requests.get(self.url+'info')
|
||||
self.grains_cache = r.json()
|
||||
return self.grains_cache
|
||||
def grains_refresh():
|
||||
'''
|
||||
Refresh the grains from the proxied device
|
||||
'''
|
||||
grains_cache = {}
|
||||
return grains()
|
||||
|
||||
def grains_refresh(self):
|
||||
'''
|
||||
Refresh the grains from the proxied device
|
||||
'''
|
||||
self.grains_cache = {}
|
||||
return self.grains()
|
||||
def service_start(name):
|
||||
'''
|
||||
Start a "service" on the REST server
|
||||
'''
|
||||
r = requests.get(url+'service/start/'+name)
|
||||
return r.json()
|
||||
|
||||
def service_start(self, name):
|
||||
'''
|
||||
Start a "service" on the REST server
|
||||
'''
|
||||
r = requests.get(self.url+'service/start/'+name)
|
||||
return r.json()
|
||||
def service_stop(name):
|
||||
'''
|
||||
Stop a "service" on the REST server
|
||||
'''
|
||||
r = requests.get(url+'service/stop/'+name)
|
||||
return r.json()
|
||||
|
||||
def service_stop(self, name):
|
||||
'''
|
||||
Stop a "service" on the REST server
|
||||
'''
|
||||
r = requests.get(self.url+'service/stop/'+name)
|
||||
return r.json()
|
||||
def service_restart(name):
|
||||
'''
|
||||
Restart a "service" on the REST server
|
||||
'''
|
||||
r = requests.get(url+'service/restart/'+name)
|
||||
return r.json()
|
||||
|
||||
def service_restart(self, name):
|
||||
'''
|
||||
Restart a "service" on the REST server
|
||||
'''
|
||||
r = requests.get(self.url+'service/restart/'+name)
|
||||
return r.json()
|
||||
def service_list():
|
||||
'''
|
||||
List "services" on the REST server
|
||||
'''
|
||||
r = requests.get(url+'service/list')
|
||||
return r.json()
|
||||
|
||||
def service_list(self):
|
||||
'''
|
||||
List "services" on the REST server
|
||||
'''
|
||||
r = requests.get(self.url+'service/list')
|
||||
return r.json()
|
||||
def service_status(name):
|
||||
'''
|
||||
Check if a service is running on the REST server
|
||||
'''
|
||||
r = requests.get(url+'service/status/'+name)
|
||||
return r.json()
|
||||
|
||||
def service_status(self, name):
|
||||
'''
|
||||
Check if a service is running on the REST server
|
||||
'''
|
||||
r = requests.get(self.url+'service/status/'+name)
|
||||
return r.json()
|
||||
def package_list():
|
||||
'''
|
||||
List "packages" installed on the REST server
|
||||
'''
|
||||
r = requests.get(url+'package/list')
|
||||
return r.json()
|
||||
|
||||
def package_list(self):
|
||||
'''
|
||||
List "packages" installed on the REST server
|
||||
'''
|
||||
r = requests.get(self.url+'package/list')
|
||||
return r.json()
|
||||
def package_install(name, **kwargs):
|
||||
'''
|
||||
Install a "package" on the REST server
|
||||
'''
|
||||
cmd = self.url+'package/install/'+name
|
||||
if 'version' in kwargs:
|
||||
cmd += '/'+kwargs['version']
|
||||
else:
|
||||
cmd += '/1.0'
|
||||
r = requests.get(cmd)
|
||||
|
||||
def package_install(self, name, **kwargs):
|
||||
'''
|
||||
Install a "package" on the REST server
|
||||
'''
|
||||
cmd = self.url+'package/install/'+name
|
||||
if 'version' in kwargs:
|
||||
cmd += '/'+kwargs['version']
|
||||
def package_remove(name):
|
||||
'''
|
||||
Remove a "package" on the REST server
|
||||
'''
|
||||
r = requests.get(url+'package/remove/'+name)
|
||||
return r.json()
|
||||
|
||||
def package_status(name):
|
||||
'''
|
||||
Check the installation status of a package on the REST server
|
||||
'''
|
||||
r = requests.get(self.url+'package/status/'+name)
|
||||
return r.json()
|
||||
|
||||
def ping():
|
||||
'''
|
||||
Is the REST server up?
|
||||
'''
|
||||
r = requests.get(url+'ping')
|
||||
try:
|
||||
if r.status_code == 200:
|
||||
return True
|
||||
else:
|
||||
cmd += '/1.0'
|
||||
r = requests.get(cmd)
|
||||
|
||||
def package_remove(self, name):
|
||||
'''
|
||||
Remove a "package" on the REST server
|
||||
'''
|
||||
r = requests.get(self.url+'package/remove/'+name)
|
||||
return r.json()
|
||||
|
||||
def package_status(self, name):
|
||||
'''
|
||||
Check the installation status of a package on the REST server
|
||||
'''
|
||||
r = requests.get(self.url+'package/status/'+name)
|
||||
return r.json()
|
||||
|
||||
def ping(self):
|
||||
'''
|
||||
Is the REST server up?
|
||||
'''
|
||||
r = requests.get(self.url+'ping')
|
||||
try:
|
||||
if r.status_code == 200:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
except Exception:
|
||||
return False
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def shutdown(self, opts):
|
||||
'''
|
||||
For this proxy shutdown is a no-op
|
||||
'''
|
||||
pass
|
||||
def shutdown(opts):
|
||||
'''
|
||||
For this proxy shutdown is a no-op
|
||||
'''
|
||||
pass
|
||||
|
|
Loading…
Add table
Reference in a new issue