mirror of
https://github.com/valitydev/salt.git
synced 2024-11-08 01:18:58 +00:00
store in grains and execute once if overwrite is false
This commit is contained in:
parent
88dac06bb2
commit
523683ca54
@ -42,6 +42,8 @@ def run(name,
|
|||||||
database,
|
database,
|
||||||
query,
|
query,
|
||||||
output=None,
|
output=None,
|
||||||
|
grain=None,
|
||||||
|
key=None,
|
||||||
overwrite=True,
|
overwrite=True,
|
||||||
**connection_args):
|
**connection_args):
|
||||||
'''
|
'''
|
||||||
@ -57,7 +59,15 @@ def run(name,
|
|||||||
The query to execute
|
The query to execute
|
||||||
|
|
||||||
output
|
output
|
||||||
The file to store results (if defined)
|
grain: output in a grain
|
||||||
|
other: the file to store results
|
||||||
|
None: output to the result comment (default)
|
||||||
|
|
||||||
|
grain:
|
||||||
|
grain to store the output (need output=grain)
|
||||||
|
|
||||||
|
key:
|
||||||
|
the output is stored as a dictionnary in the specified grain
|
||||||
|
|
||||||
overwrite
|
overwrite
|
||||||
The file will be overwritten if it already exists (default)
|
The file will be overwritten if it already exists (default)
|
||||||
@ -79,17 +89,64 @@ def run(name,
|
|||||||
).format(name)
|
).format(name)
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
# The database is present, execute the query
|
# Check if execution needed
|
||||||
results = __salt__['mysql.query'](database, query, **connection_args)
|
if output == 'grain':
|
||||||
ret['comment'] = results
|
if grain != None and key == None:
|
||||||
|
if not overwrite and grain in __salt__['grains.ls']():
|
||||||
|
ret['comment'] = 'No execution needed. Grain ' + grain\
|
||||||
|
+ ' already set'
|
||||||
|
return ret
|
||||||
|
elif grain != None and key != None:
|
||||||
|
if grain in __salt__['grains.ls']():
|
||||||
|
grain_value = __salt__['grains.get'](grain)
|
||||||
|
else:
|
||||||
|
grain_value = {}
|
||||||
|
if not overwrite and key in grain_value:
|
||||||
|
ret['comment'] = 'No execution needed. Grain ' + grain\
|
||||||
|
+ ':' + key + ' already set'
|
||||||
|
return ret
|
||||||
|
else:
|
||||||
|
ret['result'] = False
|
||||||
|
ret['comment'] = "Error: output type 'grain' needs the grain "\
|
||||||
|
+ "parameter\n"
|
||||||
|
return ret
|
||||||
|
elif output is not None:
|
||||||
|
if not overwrite and os.path.isfile(output):
|
||||||
|
ret['comment'] = 'No execution needed. File ' + output\
|
||||||
|
+ ' already set'
|
||||||
|
return ret
|
||||||
|
|
||||||
if output is not None:
|
# The database is present, execute the query
|
||||||
if overwrite or not os.path.isfile(output):
|
query_result = __salt__['mysql.query'](database, query, **connection_args)
|
||||||
ret['changes']['query'] = "Executed. Output into " + output
|
mapped_results = []
|
||||||
with open(output, 'w') as output_file:
|
for res in query_result['results']:
|
||||||
for res in results['results']:
|
mapped_line = {}
|
||||||
for idx, col in enumerate(results['columns']):
|
for idx, col in enumerate(query_result['columns']):
|
||||||
output_file.write(col + ':' + res[idx] + '\n')
|
mapped_line[col] = res[idx]
|
||||||
|
mapped_results.append(mapped_line)
|
||||||
|
query_result['results'] = mapped_results
|
||||||
|
ret['comment'] = query_result
|
||||||
|
|
||||||
|
if output == 'grain':
|
||||||
|
if grain != None and key == None:
|
||||||
|
__salt__['grains.setval'](grain, query_result)
|
||||||
|
ret['changes']['query'] = "Executed. Output into grain: "\
|
||||||
|
+ grain
|
||||||
|
elif grain != None and key != None:
|
||||||
|
if grain in __salt__['grains.ls']():
|
||||||
|
grain_value = __salt__['grains.get'](grain)
|
||||||
|
else:
|
||||||
|
grain_value = {}
|
||||||
|
grain_value[key] = query_result
|
||||||
|
__salt__['grains.setval'](grain, grain_value)
|
||||||
|
ret['changes']['query'] = "Executed. Output into grain: "\
|
||||||
|
+ grain + ":" + key
|
||||||
|
elif output is not None:
|
||||||
|
ret['changes']['query'] = "Executed. Output into " + output
|
||||||
|
with open(output, 'w') as output_file:
|
||||||
|
for res in query_result['results']:
|
||||||
|
for col, val in res:
|
||||||
|
output_file.write(col + ':' + val + '\n')
|
||||||
else:
|
else:
|
||||||
ret['changes']['query'] = "Executed"
|
ret['changes']['query'] = "Executed"
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user