Skip to content

Commit

Permalink
Merge pull request #258 from terrateamio/main
Browse files Browse the repository at this point in the history
Release v1
  • Loading branch information
orbitz authored Apr 23, 2024
2 parents d2e2828 + 8bc72db commit 35b2814
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
FROM ghcr.io/terrateamio/action-base:latest

ENV RESOURCELY_VERSION=1.0.12

COPY proxy/bin /usr/local/proxy/bin
COPY conftest-wrapper /usr/local/bin/conftest-wrapper
COPY checkov-wrapper /usr/local/bin/checkov-wrapper
COPY cdktf-setup.sh /cdktf-setup.sh
Expand Down
2 changes: 2 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#! /bin/sh

export PATH="$PATH":/usr/local/proxy/bin

WORK_TOKEN="$1"
API_BASE_URL="$2"

Expand Down
File renamed without changes.
19 changes: 19 additions & 0 deletions proxy/bin/resourcely-cli
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#! /usr/bin/env bash

set -e
set -u

if [[ ! -f /usr/local/bin/resourcely-cli ]]; then
flock /tmp/resourcely-install \
curl \
-s \
-L \
-o \
/tmp/resourcely-cli-v"$RESOURCELY_VERSION"-linux-amd64.tar.gz \
https://github.com/Resourcely-Inc/resourcely-container-registry/releases/download/v"$RESOURCELY_VERSION"/resourcely-cli-v"$RESOURCELY_VERSION"-linux-amd64.tar.gz

flock /tmp/resourcely-install tar -xzf /tmp/resourcely-cli-v"$RESOURCELY_VERSION"-linux-amd64.tar.gz
flock /tmp/resourcely-install mv resourcely-cli /usr/local/bin/ || true
fi

exec /usr/local/bin/resourcely-cli "$@"
9 changes: 9 additions & 0 deletions terrat_runner/github_actions/run_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

from . import core

from . import workflow_step_drift_create_issue
from . import workflow_step_resourcely


class Run_time(object):
def initialize(self, state):
Expand Down Expand Up @@ -82,3 +85,9 @@ def work_index(self, state):

requests_retry.put(state.api_base_url + '/v1/work-manifests/' + state.work_token,
json=output)

def steps(self):
return {
'drift_create_issue': workflow_step_drift_create_issue.run,
'resourcely': workflow_step_resourcely.run
}
41 changes: 41 additions & 0 deletions terrat_runner/github_actions/workflow_step_resourcely.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import os
import tempfile

import workflow_step_run
import workflow_step_terraform


def run(state, config):
result = workflow_step_terraform.run(state,
{
'args': ['show', '-json', '$TERRATEAM_PLAN_FILE'],
'output_key': 'plan_json'
})

if result.failed:
return result._replace(workflow_step={'type': 'resourcely'})

plan_json = result.outputs['text']

with tempfile.TemporaryDirectory() as tmpdir:
json_file = os.path.join(tmpdir, 'json')
with open(json_file, 'w') as f:
f.write(plan_json)

run_config = {
'cmd': ['resourcely-cli',
'--no_color',
'evaluate',
'--change_request_url',
'https://github.com/{}/pull/{}'.format(state.env['GITHUB_REPOSITORY'],
state.work_manifest['run_kind_data']['id']),
'--change_request_sha',
'${GITHUB_SHA}',
'--plan',
json_file],
'capture_output': True
}

result = workflow_step_run.run(state, run_config)

return result
16 changes: 15 additions & 1 deletion terrat_runner/repo_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ def _get_hooks(hooks):
}


def _get_integrations(repo_config, workflow_integrations):
global_integrations = _get(repo_config, 'integrations', {})
resourcely = _get(workflow_integrations,
'resourcely',
_get(global_integrations, 'resourcely', {}))
return {
'resourcely': {
'enabled': _get(resourcely, 'enabled', False)
}
}


def get_all_hooks(repo_config):
return _get_hooks(_get(_get(repo_config, 'hooks', {}), 'all', {}))

Expand Down Expand Up @@ -87,6 +99,7 @@ def get_workflow(repo_config, idx):
cfg = {
'apply': workflow.get('apply', _default_apply_workflow()),
'plan': workflow.get('plan', _default_plan_workflow()),
'integrations': _get_integrations(repo_config, _get(workflow, 'integrations', {}))
}

default_engine = get_engine(repo_config)
Expand Down Expand Up @@ -144,7 +157,8 @@ def get_default_workflow(repo_config):
return {
'apply': _default_apply_workflow(),
'plan': _default_plan_workflow(),
'engine': get_engine(repo_config)
'engine': get_engine(repo_config),
'integrations': _get_integrations(repo_config, {})
}


Expand Down
11 changes: 10 additions & 1 deletion terrat_runner/work_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
import workflow_step_terrateam_ssh_key_setup


def _merge_integrations(state, steps, integrations):
if state.work_manifest.get('run_kind') == 'pr' and integrations['resourcely']['enabled']:
steps = steps + [{'type': 'resourcely'}]

return steps


class Exec(work_exec.ExecInterface):
def pre_hooks(self, state):
pre_hooks = rc.get_all_hooks(state.repo_config)['pre']
Expand Down Expand Up @@ -80,12 +87,14 @@ def exec(self, state, d):

state = state._replace(env=env)

plan_steps = _merge_integrations(state, workflow['plan'], workflow['integrations'])

state = workflow_step.run_steps(
state._replace(working_dir=os.path.join(state.working_dir, path),
path=path,
workspace=workspace,
workflow=workflow),
workflow['plan'])
plan_steps)

result = {
'path': path,
Expand Down
10 changes: 6 additions & 4 deletions terrat_runner/workflow_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
# which each take their own configuration parameters.
import logging

import github_actions.workflow_step_drift_create_issue
import workflow

import workflow_step_apply
import workflow_step_env
import workflow_step_infracost_setup
Expand All @@ -18,7 +18,6 @@

STEPS = {
'apply': workflow_step_apply.run,
'drift_create_issue': github_actions.workflow_step_drift_create_issue.run,
'env': workflow_step_env.run,
'infracost_setup': workflow_step_infracost_setup.run,
'init': workflow_step_init.run,
Expand All @@ -32,19 +31,22 @@


def run_steps(state, steps, restrict_types=None):
valid_steps = STEPS.copy()
valid_steps.update(state.run_time.steps())

results = []

for step in steps:
if 'type' not in step:
raise Exception('Step must contain a type')
elif step['type'] not in STEPS:
elif step['type'] not in valid_steps:
raise Exception('Step type {} is unknown'.format(step['type']))
elif restrict_types and step['type'] not in restrict_types:
raise Exception('Step type {} not allowed in this mode'.format(step['type']))
else:
try:
logging.info('STEP : RUN : %s : %r', state.working_dir, step)
result = STEPS[step['type']](state, step)
result = valid_steps[step['type']](state, step)
state = result.state
except Exception as exn:
logging.exception(exn)
Expand Down
2 changes: 1 addition & 1 deletion terrat_runner/workflow_step_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def run(state, config):
state=state,
workflow_step={
'type': 'run',
'cmd': config['cmd'],
'cmd': proc.args,
'exit_code': proc.returncode
},
outputs=outputs)
Expand Down

0 comments on commit 35b2814

Please sign in to comment.