Skip to content

Commit

Permalink
WIP for writing out distilled data
Browse files Browse the repository at this point in the history
  • Loading branch information
Clint Savage committed Mar 29, 2018
1 parent 976ff41 commit be44b96
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 9 deletions.
81 changes: 76 additions & 5 deletions linchpin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,9 +624,9 @@ def do_action(self, provision_data, action='up', run_id=None, tx_id=None):
provision_data[target]['hooks']}
])

if provision_data[target].get('vars', None):
vars_data = provision_data[target].get('vars')
self.set_evar('vars_data', vars_data)
if provision_data[target].get('cfgs', None):
vars_data = provision_data[target].get('cfgs')
self.set_evar('cfgs_data', vars_data)
rundb.update_record(target,
rundb_id,
'cfgs',
Expand Down Expand Up @@ -693,10 +693,8 @@ def do_action(self, provision_data, action='up', run_id=None, tx_id=None):

for target, data in results.iteritems():
for k, v in data['rundb_data'].iteritems():

summary[target] = {k: {'rc': v['rc'], 'uhash': v['uhash']}}


rundb.update_record('linchpin', lp_id, 'action', action)
rundb.update_record('linchpin', lp_id, 'targets', [summary])

Expand All @@ -707,6 +705,79 @@ def do_action(self, provision_data, action='up', run_id=None, tx_id=None):
return (return_code, lp_data)


def get_run_data(self, tx_id, fields, targets=()):
"""
Returns the RunDB for data from a specified field given either a run_id
or tx_id. The fields consist of the major sections in the RunDB (target
view only). Those fields are action, start, end, inputs, outputs,
uhash, and rc.
:param tx_id: tx_id to search
:param fields: Tuple of fields to retrieve for each record requested.
:param targets: Tuple of targets to search from within the tx_ids
"""

rundb = self.setup_rundb()

tgt_run_ids = {}
target_data = {}

record = rundb.get_tx_record(tx_id)

if not record or not len(record):
return None


# get run_ids to query
if len(targets):
for tgts in record['targets']:
for tgt, data in tgts.iteritems():
if tgt in targets:
tgt_run_ids[tgt] = int(data.keys()[0])
else:
for tgts in record['targets']:
for tgt, data in tgts.iteritems():
tgt_run_ids[tgt] = int(data.keys()[0])

for target, run_id in tgt_run_ids.iteritems():
record = rundb.get_record(target, run_id=run_id, action='up')
field_data = {}

# {"action": "",
# "inputs": [],
# "outputs": [],
# "cfgs": [],
# "start": "",
# "end": "",
# "rc": 0,
# "uhash": ""}


import pdb
pdb.set_trace()
single_value_fields = ('action', 'start', 'end', 'rc', 'uhash')

for field in fields:
f = record[0].get(field)
if f:
if field in single_value_fields:
field_data[field] = f
else:
data_array = {}
for k, v in f[0].iteritems():
if field == 'outputs':
value = v[0]
data_array[k] = v[0]
else:
data_array[k] = v

field_data[field] = data_array

target_data[target] = field_data

return target_data


def _invoke_playbooks(self, resources, action='up', console=True):
"""
Uses the Ansible API code to invoke the specified linchpin playbook
Expand Down
36 changes: 32 additions & 4 deletions linchpin/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,35 @@ def lp_up(self, targets=(), run_id=None, tx_id=None):
An optional tx_id if the task is idempotent
"""

return self._execute_action('up',
targets,
run_id=run_id,
tx_id=tx_id)
# Prep input data

# Execute prepped data
return_code, return_data = self._execute_action('up',
targets,
run_id=run_id,
tx_id=tx_id)

# Distill data
new_tx_id = return_data.keys()[0]

# run_data = self.get_run_data(new_tx_id, ('outputs', 'inputs',
# 'action', 'cfgs', 'start',
# 'end', 'rc', 'uhash'))

run_data = self.get_run_data(new_tx_id, ('outputs',))

# Export distilled data in useful ways
### Write out run_data to a file for now
gen_resources = self.get_evar('generate_resources')
distill_data = self.get_cfg('lp', 'distill_data')

# if (ast.literal_eval(distill_data) and
# not ast.literal_eval(gen_resources.title())):
# self.write_out_context(run_data)

# Show success and errors, with data
return (return_code, return_data)



def lp_destroy(self, targets=(), run_id=None, tx_id=None):
Expand All @@ -232,6 +257,9 @@ def lp_destroy(self, targets=(), run_id=None, tx_id=None):
An optional tx_id to use
"""

# prep inputs
#

return self._execute_action('destroy',
targets,
run_id=run_id,
Expand Down

0 comments on commit be44b96

Please sign in to comment.