mirror of
https://github.com/valitydev/salt.git
synced 2024-11-08 09:23:56 +00:00
commit
c69ee780e9
@ -31,7 +31,7 @@ def __virtual__():
|
||||
'''
|
||||
Provides zpool.
|
||||
'''
|
||||
if _check_zpool( ):
|
||||
if _check_zpool():
|
||||
return 'zpool'
|
||||
return False
|
||||
|
||||
|
@ -3,89 +3,93 @@ Simple returner for CouchDB. Optional configuration
|
||||
settings are listed below, along with sane defaults.
|
||||
|
||||
couchdb.hooks
|
||||
* is a list of dict objects.
|
||||
* in each dict there is a key and value.
|
||||
* the value is eval()'d
|
||||
* optional "eval", which is executed beforehand.
|
||||
* is a list of dict objects.
|
||||
* in each dict there is a key and value.
|
||||
* the value is eval()'d
|
||||
* optional "eval", which is executed beforehand.
|
||||
|
||||
couchdb.db: 'salt'
|
||||
couchdb.url: 'http://salt:5984/'
|
||||
couchdb.hooks: [ { "key": "timestamp", "value": "time.time()", "eval": "import time" } ]
|
||||
couchdb.db: 'salt'
|
||||
couchdb.url: 'http://salt:5984/'
|
||||
couchdb.hooks: [ { "key": "timestamp", "value": "time.time()", "eval": "import time" } ]
|
||||
|
||||
'''
|
||||
import logging
|
||||
|
||||
log = logging.getLogger( __name__ )
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
# Import the required modules.
|
||||
try:
|
||||
import couchdb
|
||||
HAS_COUCHDB = True
|
||||
import couchdb
|
||||
HAS_COUCHDB = True
|
||||
except ImportError:
|
||||
HAS_COUCHDB = False
|
||||
HAS_COUCHDB = False
|
||||
|
||||
def __virtual__( ):
|
||||
if not HAS_COUCHDB:
|
||||
return False
|
||||
return 'couchdb'
|
||||
|
||||
def _get_options( ):
|
||||
'''
|
||||
Get the couchdb options from salt. Apply defaults
|
||||
if required.
|
||||
'''
|
||||
server_url = __salt__['config.option']('couchdb.url')
|
||||
if not server_url:
|
||||
log.debug( "Using default url." )
|
||||
server_url = "http://salt:5984/"
|
||||
def __virtual__():
|
||||
if not HAS_COUCHDB:
|
||||
return False
|
||||
return 'couchdb'
|
||||
|
||||
db_name = __salt__['config.option']('couchdb.db')
|
||||
if not db_name:
|
||||
log.debug( "Using default database." )
|
||||
db_name = "salt"
|
||||
|
||||
hooks = __salt__['config.option']('couchdb.hooks')
|
||||
if not hooks:
|
||||
log.debug( "Using default hooks" )
|
||||
hooks = [ { "key": "timestamp", "value": "time.time()", "eval": "import time" } ]
|
||||
def _get_options():
|
||||
'''
|
||||
Get the couchdb options from salt. Apply defaults
|
||||
if required.
|
||||
'''
|
||||
server_url = __salt__['config.option']('couchdb.url')
|
||||
if not server_url:
|
||||
log.debug("Using default url.")
|
||||
server_url = "http://salt:5984/"
|
||||
|
||||
return { "url": server_url, "db": db_name, "hooks": hooks }
|
||||
db_name = __salt__['config.option']('couchdb.db')
|
||||
if not db_name:
|
||||
log.debug("Using default database.")
|
||||
db_name = "salt"
|
||||
|
||||
def _generate_doc( ret, options ):
|
||||
'''
|
||||
Create a object that will be saved into the database based on
|
||||
options.
|
||||
'''
|
||||
|
||||
# Just set the document ID to the jid.
|
||||
r = ret
|
||||
r["_id"] = ret["jid"]
|
||||
hooks = __salt__['config.option']('couchdb.hooks')
|
||||
if not hooks:
|
||||
log.debug("Using default hooks")
|
||||
hooks = [{"key": "timestamp", "value": "time.time()", "eval": "import time"}]
|
||||
|
||||
for hook in options["hooks"]:
|
||||
return {"url": server_url, "db": db_name, "hooks": hooks}
|
||||
|
||||
# Eval if specified.
|
||||
if hasattr( hook, "eval" ):
|
||||
eval( hook["eval"] )
|
||||
|
||||
r[hook["key"]] = eval( hook["value"] )
|
||||
|
||||
return r
|
||||
def _generate_doc(ret, options):
|
||||
'''
|
||||
Create a object that will be saved into the database based on
|
||||
options.
|
||||
'''
|
||||
|
||||
def returner( ret ):
|
||||
'''
|
||||
Take in the return and shove it into the couchdb database.
|
||||
'''
|
||||
# Just set the document ID to the jid.
|
||||
r = ret
|
||||
r["_id"] = ret["jid"]
|
||||
|
||||
# Get the options from configuration.
|
||||
options = _get_options( )
|
||||
|
||||
# Create a connection to the server.
|
||||
server = couchdb.client.Server( options["url"] )
|
||||
for hook in options["hooks"]:
|
||||
|
||||
# Create the database if the configuration calls for it.
|
||||
if not options["db"] in server:
|
||||
log.debug( "Creating database %s" % options["db"] )
|
||||
server.create( options["db"] )
|
||||
# Eval if specified.
|
||||
if hasattr(hook, "eval"):
|
||||
eval(hook["eval"])
|
||||
|
||||
# Save the document that comes out of _generate_doc.
|
||||
server[options["db"]].save( _generate_doc( ret, options ) )
|
||||
r[hook["key"]] = eval(hook["value"])
|
||||
|
||||
return r
|
||||
|
||||
|
||||
def returner(ret):
|
||||
'''
|
||||
Take in the return and shove it into the couchdb database.
|
||||
'''
|
||||
|
||||
# Get the options from configuration.
|
||||
options = _get_options()
|
||||
|
||||
# Create a connection to the server.
|
||||
server = couchdb.client.Server(options["url"])
|
||||
|
||||
# Create the database if the configuration calls for it.
|
||||
if options["db"] not in server:
|
||||
log.debug("Creating database %s" % options["db"])
|
||||
server.create(options["db"])
|
||||
|
||||
# Save the document that comes out of _generate_doc.
|
||||
server[options["db"]].save(_generate_doc(ret, options))
|
||||
|
Loading…
Reference in New Issue
Block a user