This repository has been archived by the owner on Jan 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start refactor to remove "generated-at-runtime" tf workspace files
- Loading branch information
Showing
17 changed files
with
875 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import argparse | ||
import collections | ||
import os | ||
import tarfile | ||
|
||
parser = argparse.ArgumentParser( | ||
fromfile_prefix_chars='@', | ||
description='Bundle terraform files into an archive') | ||
|
||
parser.add_argument( | ||
'--file', action='append', metavar=('tgt_path', 'src'), nargs=2, default=[], | ||
help="'src' file will be added to 'tgt_path'") | ||
|
||
parser.add_argument( | ||
'--embed', action='append', metavar=('embed_path', 'src_tar'), nargs=2, default=[], | ||
help="'src' archive will be embedded in 'embed_path'. If 'embed_path=.' then archive content will be merged into " | ||
"the output root") | ||
|
||
parser.add_argument( | ||
'--output', action='store', required=True, | ||
help="Output path of bundled archive") | ||
|
||
BundleItem = collections.namedtuple('BundleItem', 'tarinfo file') | ||
|
||
|
||
class Bundle: | ||
|
||
def __init__(self, output): | ||
# map of paths to BundleItems | ||
self._file_map = {} | ||
self._output = tarfile.open(output, "w") | ||
|
||
def add(self, src, arcname): | ||
f = open(os.path.realpath(src), 'r') | ||
tarinfo = self._output.gettarinfo(arcname=arcname, fileobj=f) | ||
if self._file_map.has_key(tarinfo.name): | ||
raise ValueError("File '%s' is already in archive" % tarinfo.name) | ||
tarinfo.mtime = 0 # zero out modification time | ||
self._file_map[tarinfo.name] = BundleItem(tarinfo, f) | ||
|
||
def embed(self, archive, embed_path): | ||
tar = tarfile.open(archive) | ||
for tarinfo in tar.getmembers(): | ||
f = tar.extractfile(tarinfo) | ||
if embed_path != ".": | ||
tarinfo.name = embed_path + "/" + tarinfo.name | ||
if self._file_map.has_key(tarinfo.name): | ||
raise ValueError("File '%s' is already in archive" % tarinfo.name) | ||
self._file_map[tarinfo.name] = BundleItem(tarinfo, f) | ||
|
||
def finish(self): | ||
for path in sorted(self._file_map.keys()): | ||
tarinfo, f = self._file_map[path] | ||
self._output.addfile(tarinfo, fileobj=f) | ||
|
||
|
||
def main(args): | ||
""" | ||
:return: | ||
""" | ||
|
||
# output = tarfile.open(args.output, "w") | ||
bundle = Bundle(args.output) | ||
|
||
# add each args.file | ||
for tgt_path, src in args.file: | ||
bundle.add(src, tgt_path) | ||
# embed each args.embed | ||
for embed_path, src_tar in args.embed: | ||
bundle.embed(src_tar, embed_path) | ||
|
||
bundle.finish() | ||
|
||
if __name__ == '__main__': | ||
main(parser.parse_args()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
load("//terraform:providers.bzl", "WorkspaceInfo", "tf_workspace_files_prefix") | ||
load("//terraform/internal:image_embedder_lib.bzl", "create_image_publisher", "image_publisher_aspect", "image_publisher_attrs") | ||
|
||
def _integration_test_impl(ctx): | ||
""" | ||
""" | ||
|
||
runfiles = [] | ||
transitive_runfiles = [] | ||
|
||
transitive_runfiles.append(ctx.attr._runner_template.data_runfiles.files) | ||
transitive_runfiles.append(ctx.attr._stern.data_runfiles.files) | ||
transitive_runfiles.append(ctx.attr.srctest.data_runfiles.files) | ||
transitive_runfiles.append(ctx.attr.terraform_workspace.data_runfiles.files) | ||
render_workspace = ctx.attr.terraform_workspace[WorkspaceInfo].render_workspace | ||
|
||
ctx.actions.expand_template( | ||
template = ctx.file._runner_template, | ||
substitutions = { | ||
"%{render_workspace}": render_workspace.short_path, | ||
"%{srctest}": ctx.executable.srctest.short_path, | ||
"%{stern}": ctx.executable._stern.short_path, | ||
}, | ||
output = ctx.outputs.executable, | ||
is_executable = True, | ||
) | ||
|
||
return [DefaultInfo( | ||
runfiles = ctx.runfiles( | ||
files = runfiles, | ||
transitive_files = depset(transitive = transitive_runfiles), | ||
), | ||
)] | ||
|
||
# Wraps the source test with infrastructure spinup and teardown | ||
terraform_integration_test = rule( | ||
test = True, | ||
implementation = _integration_test_impl, | ||
attrs = image_publisher_attrs + { | ||
"terraform_workspace": attr.label( | ||
doc = "TF Workspace to spin up before testing & tear down after testing.", | ||
mandatory = True, | ||
executable = True, | ||
cfg = "host", | ||
providers = [WorkspaceInfo], | ||
aspects = [image_publisher_aspect], | ||
), | ||
"srctest": attr.label( | ||
doc = "Label of source test to wrap", | ||
mandatory = True, | ||
executable = True, | ||
cfg = "target", # 'host' does not work for jvm source tests, because it launches with @embedded_jdk//:jar instead of @local_jdk//:jar | ||
aspects = [image_publisher_aspect], | ||
), | ||
"_runner_template": attr.label( | ||
default = "//terraform/internal:integration_test_runner.sh.tpl", | ||
allow_single_file = True, | ||
), | ||
"_stern": attr.label( | ||
executable = True, | ||
cfg = "host", | ||
default = "@tool_stern", | ||
), | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.