Skip to content

Commit

Permalink
terrateamio/terrateam#232 ADD Pulumi support
Browse files Browse the repository at this point in the history
  • Loading branch information
orbitz committed Jan 17, 2025
1 parent 2a47aea commit df9d601
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM ghcr.io/terrateamio/action-base:latest

COPY proxy/bin /usr/local/proxy/bin

COPY entrypoint.sh /entrypoint.sh
COPY terrat_runner /terrat_runner

Expand Down
14 changes: 14 additions & 0 deletions proxy/bin/pulumi
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#! /usr/bin/env bash

set -e
set -u

export PATH=$HOME/.pulumi/bin:$PATH

if [ -f $HOME/.pulumi/bin/pulumi ]; then
exec pulumi "$@"
else
flock /tmp/pulumi-install bash -c 'curl -fsSL https://get.pulumi.com | sh' 1>&2
exec $HOME/.pulumi/bin/pulumi "$@"
fi

2 changes: 2 additions & 0 deletions terrat_runner/repo_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ def get_workflow(repo_config, idx):
engine['version'] = _get(engine, 'version', get_default_tf_version(repo_config))
elif engine['name'] == 'tofu':
engine['version'] = _get(engine, 'version', default_tf_version)
elif engine['name'] == 'pulumi':
pass
else:
raise Exception('Unknown engine')

Expand Down
2 changes: 1 addition & 1 deletion terrat_runner/work_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def exec(self, state, d):
path,
create_and_select_workspace)

work_exec.set_tf_version_env(
work_exec.set_engine_env(
env,
state.repo_config,
workflow['engine'],
Expand Down
6 changes: 3 additions & 3 deletions terrat_runner/work_exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def _read(fname):
return workflow_version


def set_tf_version_env(env, repo_config, engine, repo_root, working_dir):
def set_engine_env(env, repo_config, engine, repo_root, working_dir):
ENGINE_NAME = 'TERRATEAM_ENGINE_NAME'
TF_CMD_ENV_NAME = 'TERRATEAM_TF_CMD'
TOFU_ENV_NAME = 'TOFUENV_TOFU_DEFAULT_VERSION'
Expand Down Expand Up @@ -76,7 +76,7 @@ def set_tf_version_env(env, repo_config, engine, repo_root, working_dir):
env[TERRAGRUNT_ENV_NAME] = engine['version']
env[TERRAGRUNT_TF_PATH_ENV_NAME] = env[TF_CMD_ENV_NAME]

else:
elif engine['name'] in ['terraform', 'tofu']:
env[TF_CMD_ENV_NAME] = 'terraform'
version = engine.get('version')

Expand Down Expand Up @@ -140,7 +140,7 @@ def _run(state, exec_cb):
# Using state.working_dir twice as a bit of a hack because
# determine_tf_version expects the directory that we are running the command
# in as an option as well, but at this point there is none.
set_tf_version_env(
set_engine_env(
env,
state.repo_config,
rc.get_engine(state.repo_config),
Expand Down
2 changes: 1 addition & 1 deletion terrat_runner/work_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def exec(self, state, d):
path,
create_and_select_workspace)

work_exec.set_tf_version_env(
work_exec.set_engine_env(
env,
state.repo_config,
workflow['engine'],
Expand Down
2 changes: 1 addition & 1 deletion terrat_runner/work_unsafe_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def exec(self, state, d):
else:
workflow = rc.get_workflow(state.repo_config, workflow_idx)

work_exec.set_tf_version_env(
work_exec.set_engine_env(
env,
state.repo_config,
workflow['engine'],
Expand Down
33 changes: 32 additions & 1 deletion terrat_runner/workflow_step_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
import cmd
import repo_config
import retry

import workflow_step_run
import workflow_step_terraform

TRIES = 3
INITIAL_SLEEP = 1
BACKOFF = 1.5


def run(state, config):
def run_tf(state, config):
original_config = config
config = original_config.copy()
config['args'] = ['init']
Expand Down Expand Up @@ -54,3 +56,32 @@ def run(state, config):
return result._replace(success=(proc.returncode == 0))

return result._replace(step='tf/init')


def run_pulumi(state, config):
logging.info('WORKFLOW_STEP_INIT : engine=%s',
state.workflow['engine']['name'])

result = workflow_step_run.run(
state,
{
'cmd': (['pulumi', 'login'] + config.get('extra_args', []))
})._replace(step='pulumi/init')

if not result.success:
return result

result = workflow_step_run.run(
state,
{
'cmd': ['pulumi', 'stack', 'select', state.workspace],
})._replace(step='pulumi/init')

return result


def run(state, config):
if state.env['TERRATEAM_ENGINE_NAME'] == 'pulumi':
return run_pulumi(state, config)
else:
return run_tf(state, config)
61 changes: 60 additions & 1 deletion terrat_runner/workflow_step_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import cmd
import repo_config as rc
import requests_retry

import workflow_step_run
import workflow_step_terraform


Expand Down Expand Up @@ -198,7 +200,7 @@ def plan_fast_and_loose(state, config):
return run_plan(state, config, targets)


def run(state, config):
def run_tf(state, config):
if config.get('mode') == 'fast-and-loose':
result = plan_fast_and_loose(state, config)
else:
Expand Down Expand Up @@ -251,3 +253,60 @@ def run(state, config):
payload = {'text': 'Could not store plan file, with the following error:\n\n' + output}

return result._replace(payload=payload, step='tf/plan')


def run_pulumi(state, config):
logging.info('WORKFLOW_STEP_PLAN : engine=%s',
state.workflow['engine']['name'])

result = workflow_step_run.run(
state,
{
'cmd': ['pulumi', 'preview']
})

if (not result.success and (
'exit_code' not in result.payload
or result.payload['exit_code'] != 0)):
return result._replace(step='pulumi/plan')

has_changes = True

payload = {
'plan': result.payload['text'],
'text': result.payload['text'],
'has_changes': has_changes
}

with open(state.env['TERRATEAM_PLAN_FILE'], 'w') as f:
f.write('{}')

plan_storage = rc.get_plan_storage(state.repo_config)

(success, output) = _store_plan(state,
plan_storage,
state.work_token,
state.api_base_url,
state.env['TERRATEAM_DIR'],
state.env['TERRATEAM_WORKSPACE'],
state.env['TERRATEAM_PLAN_FILE'],
has_changes)

if success:
result = result._replace(success=success)
else:
logging.error('PLAN_STORE_FAILED : %s : %s : %s',
state.env['TERRATEAM_DIR'],
state.env['TERRATEAM_WORKSPACE'],
output)
result = result._replace(success=False)
payload = {'text': 'Could not store plan file, with the following error:\n\n' + output}

return result._replace(payload=payload, step='pulumi/plan')


def run(state, config):
if state.env['TERRATEAM_ENGINE_NAME'] == 'pulumi':
return run_pulumi(state, config)
else:
return run_tf(state, config)

0 comments on commit df9d601

Please sign in to comment.