Skip to content

Commit

Permalink
Distill context to disk
Browse files Browse the repository at this point in the history
write it to resources/linchpin.distilled
  • Loading branch information
Clint Savage committed Apr 2, 2018
1 parent 791706a commit 7b0ba2a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 23 deletions.
19 changes: 4 additions & 15 deletions linchpin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,19 +742,6 @@ def get_run_data(self, tx_id, fields, targets=()):
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:
Expand All @@ -766,8 +753,10 @@ def get_run_data(self, tx_id, fields, targets=()):
data_array = {}
for k, v in f[0].iteritems():
if field == 'outputs':
value = v[0]
data_array[k] = v[0]
values = []
for value in v:
values.append(value)
data_array[k] = values
else:
data_array[k] = v

Expand Down
84 changes: 76 additions & 8 deletions linchpin/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re
import ast
import sys
import json
import click

from distutils import dir_util
Expand Down Expand Up @@ -173,6 +174,72 @@ def _get_data_path(self):
return data_w_path


def _write_distilled_context(self, run_data):
"""
This method takes all of the provided run_data, loops through the
distiller section of the linchpin.constants and writes out the
linchpin.context (name TBD) to the 'resources' directory.
"""

dist_roles = self.get_cfg('distiller')

resources_path = self.get_evar('resources_folder')
context_file = '{0}/{1}/{2}'.format(self.workspace,
resources_path,
'linchpin.distilled')

roles = []
dist_data = {}


# get roles used
for target, data in run_data.iteritems():
inputs = data.get('inputs')
topo = inputs.get('topology_data')
res_grps = topo.get('resource_groups')

for group in res_grps:
res_defs = group.get('resource_definitions')
for rd in res_defs:
roles.append(rd.get('role'))

fields = {}
outputs = data.get('outputs')
resources = outputs.get('resources')

for dist_role, flds in dist_roles.iteritems():
if dist_role in roles:
for f in flds.split(','):
if '.' not in f:
fld = fields.get('single', [])
fields[f] = None
else:
k, v = f.split('.')
fld = fields.get(k, [])
fld.append(v)
fields[k] = fld



for res in resources:
for k, v in fields.iteritems():
res_data = {}
if not v:
res_data[k] = res.get(k)
else:
r = res.get(k)[0]
for value in v:
res_data[value] = r.get(value)

if target not in dist_data.keys():
dist_data[target] = []
dist_data[target].append(res_data)

with open(context_file, 'w+') as f:
f.write(json.dumps(dist_data))


def lp_down(self, pinfile, targets=(), run_id=None):
"""
This function takes a list of targets, and performs a shutdown on
Expand Down Expand Up @@ -219,20 +286,21 @@ def lp_up(self, targets=(), run_id=None, tx_id=None):
# 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'))
# Thsi is what the API allows.
# 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',))
run_data = self.get_run_data(new_tx_id, ('inputs', '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')
gen_resources = self.get_evar('generate_resources')

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

# Show success and errors, with data
return (return_code, return_data)
Expand Down
4 changes: 4 additions & 0 deletions linchpin/linchpin.constants
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ default_resources_path = {{ workspace }}/%(resources_folder)s
source = templates/
pinfile = PinFile

[distiller]
bkr_server = test,happiness
aws_ec2 = instances.id,instances.public_ip,instances.private_ip,instances.public_dns_name,instances.private_dns_name

[logger]
enable = True
file = linchpin.log
Expand Down

0 comments on commit 7b0ba2a

Please sign in to comment.