Skip to content

Commit

Permalink
Merge pull request #307 from terrateamio/main
Browse files Browse the repository at this point in the history
Release v1
  • Loading branch information
orbitz authored Sep 22, 2024
2 parents 0adfdf0 + 5314591 commit 03e96e8
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
6 changes: 6 additions & 0 deletions terrat_runner/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,15 @@ def run_with_output(state, config):
proc = subprocess.Popen(cmd,
cwd=state.working_dir,
env=env,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)

if 'input' in config:
proc.stdin.write(config['input'].encode('utf-8'))

proc.stdin.close()

line = proc.stdout.readline()
output = io.StringIO()
while line:
Expand Down
5 changes: 4 additions & 1 deletion terrat_runner/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

import repo_config
import run_state

import work_apply
import work_build_config
import work_exec
import work_manifest
import work_plan
Expand Down Expand Up @@ -67,7 +69,8 @@ def tf_operation(state, op):
'plan': lambda state: tf_operation(state, work_plan.Exec()),
'apply': lambda state: tf_operation(state, work_apply.Exec()),
'unsafe-apply': lambda state: tf_operation(state, work_unsafe_apply.Exec()),
'index': lambda state: state.run_time.work_index(state)
'index': lambda state: state.run_time.work_index(state),
'build-config': lambda state: work_build_config.run(state)
}


Expand Down
4 changes: 4 additions & 0 deletions terrat_runner/repo_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,7 @@ def get_plan_storage(config):

def get_indexer(config):
return _get(config, 'indexer', {'enabled': False})


def get_config_builder(config):
return _get(config, 'config_builder', {'enabled': False})
49 changes: 49 additions & 0 deletions terrat_runner/work_build_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import json
import os
import tempfile

import cmd
import repo_config as rc
import requests_retry


def run(state):
config_builder = rc.get_config_builder(state.repo_config)

if not config_builder['enabled']:
raise Exception('Impossible')

with tempfile.TemporaryDirectory() as tmpdir:
script = config_builder['script']

script_path = os.path.join(tmpdir, 'config-builder')

with open(script_path, 'w') as f:
f.write(script)

os.chmod(script_path, 0o005)

try:
(proc, output) = cmd.run_with_output(state,
{
'cmd': [script_path],
'input': json.dumps(state.repo_config),
'cwd': tmpdir
})
if proc.returncode == 0:
try:
config = json.loads(output)
requests_retry.put(state.api_base_url + '/v1/work-manifests/' + state.work_token,
json={'config': config})
except json.JSONDecodeError as exn:
requests_retry.put(state.api_base_url + '/v1/work-manifests/' + state.work_token,
json={'msg': exn.msg})
except Exception as exn:
requests_retry.put(state.api_base_url + '/v1/work-manifests/' + state.work_token,
json={'msg': str(exn)})
else:
requests_retry.put(state.api_base_url + '/v1/work-manifests/' + state.work_token,
json={'msg': output})
except Exception as exn:
requests_retry.put(state.api_base_url + '/v1/work-manifests/' + state.work_token,
json={'msg': str(exn)})

0 comments on commit 03e96e8

Please sign in to comment.