Skip to content

Commit

Permalink
integrate providers mirror inside tf toolchain
Browse files Browse the repository at this point in the history
  • Loading branch information
yanndegat committed Feb 27, 2024
1 parent 39e6099 commit 96de1dc
Show file tree
Hide file tree
Showing 15 changed files with 150 additions and 296 deletions.
5 changes: 0 additions & 5 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,3 @@ toolchain_type(
name = "tfdoc_toolchain_type",
visibility = ["//visibility:public"],
)

toolchain_type(
name = "tf_plugins_mirror_toolchain_type",
visibility = ["//visibility:public"],
)
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,29 @@ bazel_dep(name = "rules_tf", version = "0.0.5")
# )

tf = use_extension("@rules_tf//tf:extensions.bzl", "tf_repositories", dev_dependency = True)
tf.download( version = "1.5.7", use_tofu = False )

# Switch to tofu
# tf = use_extension("@rules_tf//tf:extensions.bzl", "tf_repositories")
# tf.download( version = "1.6.0", use_tofu = True )

use_repo(tf, "tf_toolchains")

plugins_mirror = use_extension("@rules_tf//tf:extensions.bzl", "plugins_mirror", dev_dependency = True)
plugins_mirror.versions(
name = "default",
tf.download(
version = "1.5.7",
use_tofu = False,
versions = {
"random" : "hashicorp/random:3.3.2",
"null" : "hashicorp/null:3.1.1",
}
}
)

use_repo(plugins_mirror, "tf_plugins_mirrors")
# Switch to tofu
# tf = use_extension("@rules_tf//tf:extensions.bzl", "tf_repositories")
# tf.download(
# version = "1.6.0",
# use_tofu = True,
# mirror = {
# "random" : "hashicorp/random:3.3.2",
# "null" : "hashicorp/null:3.1.1",
# }
# )

use_repo(tf, "tf_toolchains")
register_toolchains(
"@tf_toolchains//:all",
"@tf_plugins_mirrors//:all",
dev_dependency = True,
)
```
Expand Down
1 change: 0 additions & 1 deletion distro/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ pkg_tar(
"//tf/toolchains/tfdoc:standard_package",
"//tf/toolchains/tflint:standard_package",
"//tf/toolchains/tofu:standard_package",
"//tf/toolchains/plugins_mirror:standard_package",
],
extension = "tar.gz",
# It is all source code, so make it read-only.
Expand Down
18 changes: 6 additions & 12 deletions tests/bcr/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,16 @@ local_path_override(
)

tf = use_extension("@rules_tf//tf:extensions.bzl", "tf_repositories")

tf.download( version = "1.5.7", use_tofu = False )

use_repo(tf, "tf_toolchains")

plugins_mirror = use_extension("@rules_tf//tf:extensions.bzl", "plugins_mirror")
plugins_mirror.versions(
name = "default",
versions = {
tf.download(
version = "1.5.7",
use_tofu = False,
mirror = {
"random" : "hashicorp/random:3.3.2",
"null" : "hashicorp/null:3.1.1",
}
},
)

use_repo(plugins_mirror, "tf_plugins_mirrors")
use_repo(tf, "tf_toolchains")
register_toolchains(
"@tf_toolchains//:all",
"@tf_plugins_mirrors//:all",
)
45 changes: 4 additions & 41 deletions tf/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ load("@rules_tf//tf/toolchains/terraform:toolchain.bzl", "terraform_download")
load("@rules_tf//tf/toolchains/tflint:toolchain.bzl", "tflint_download")
load("@rules_tf//tf/toolchains/tfdoc:toolchain.bzl", "tfdoc_download")
load("@rules_tf//tf/toolchains/tofu:toolchain.bzl", "tofu_download")
load("@rules_tf//tf/toolchains/plugins_mirror:toolchain.bzl", _plugins_mirror = "plugins_mirror")
load("@rules_tf//tf:toolchains.bzl", "tf_toolchains", "tf_plugins_mirrors_toolchains")
load("@rules_tf//tf:toolchains.bzl", "tf_toolchains")
load("@rules_tf//tf:versions.bzl", "TERRAFORM_VERSION")
load("@rules_tf//tf:versions.bzl", "TOFU_VERSION")
load("@rules_tf//tf:versions.bzl", "TFDOC_VERSION")
Expand Down Expand Up @@ -88,6 +87,7 @@ def _tf_repositories(ctx):
version = version_tag.version,
os = host_detected_os,
arch = host_detected_arch,
mirror = version_tag.mirror,
)
tofu_toolchains += [tf_repo_name]
else:
Expand All @@ -96,6 +96,7 @@ def _tf_repositories(ctx):
version = version_tag.version,
os = host_detected_os,
arch = host_detected_arch,
mirror = version_tag.mirror,
)
terraform_toolchains += [tf_repo_name]

Expand All @@ -113,6 +114,7 @@ _version_tag = tag_class(
attrs = {
"use_tofu": attr.bool(mandatory = True, default = False),
"version": attr.string(mandatory = True),
"mirror": attr.string_dict(mandatory = True),
},
)

Expand All @@ -124,42 +126,3 @@ tf_repositories = module_extension(
os_dependent = True,
arch_dependent = True,
)


def _plugins_mirror_ext_impl(ctx):
mirror_toolchains = {}

for module in ctx.modules:
for index, version_tag in enumerate(module.tags.versions):
repo_name = _repo_name(
module = module,
tool = "tf_plugins_mirror",
index = index,
suffix = version_tag.name,
)

_plugins_mirror(
name = repo_name,
)

mirror_toolchains[repo_name] = ",".join(["'{}': '{}'".format(k, version_tag.versions[k]) for k in version_tag.versions])

tf_plugins_mirrors_toolchains(
name = "tf_plugins_mirrors",
mirror_repos = mirror_toolchains,
)

_plugins_mirror_tag = tag_class(attrs = {
"name": attr.string(mandatory = True),
"versions": attr.string_dict(mandatory = True),
})

plugins_mirror = module_extension(
implementation = _plugins_mirror_ext_impl,
tag_classes = {
"versions": _plugins_mirror_tag,
},
os_dependent = True,
arch_dependent = True,
)

6 changes: 2 additions & 4 deletions tf/rules/tf-lint.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ load("@rules_tf//tf/rules:providers.bzl", "TfModuleInfo", "TfProvidersVersionsIn
def _impl(ctx):
tflint_runtime = ctx.toolchains["@rules_tf//:tflint_toolchain_type"].runtime
tf_runtime = ctx.toolchains["@rules_tf//:tf_toolchain_type"].runtime
tf_plugins_mirror = ctx.toolchains["@rules_tf//:tf_plugins_mirror_toolchain_type"].runtime.dir

config_file = ""

Expand All @@ -15,10 +14,10 @@ def _impl(ctx):
runner = tflint_runtime.runner.path,
mod_dir = ctx.label.package,
config_file = config_file,
plugins_mirror = tf_plugins_mirror.short_path,
plugins_mirror = tf_runtime.mirror.path,
)

deps = ctx.attr.module[TfModuleInfo].transitive_srcs.to_list() + tflint_runtime.deps + [tf_runtime.tf, tf_plugins_mirror]
deps = ctx.attr.module[TfModuleInfo].transitive_srcs.to_list() + tflint_runtime.deps + tf_runtime.deps

ctx.actions.write(
output = ctx.outputs.executable,
Expand All @@ -42,7 +41,6 @@ tf_lint_test = rule(
},
test = True,
toolchains = [
"@rules_tf//:tf_plugins_mirror_toolchain_type",
"@rules_tf//:tf_toolchain_type",
"@rules_tf//:tflint_toolchain_type",
],
Expand Down
9 changes: 2 additions & 7 deletions tf/rules/tf-module.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -142,23 +142,19 @@ tf_module_deps = rule(

def _tf_validate_impl(ctx):
tf_runtime = ctx.toolchains["@rules_tf//:tf_toolchain_type"].runtime
tf_plugins_mirror = ctx.toolchains["@rules_tf//:tf_plugins_mirror_toolchain_type"].runtime.dir

cmd = "{tf} -chdir={dir} init -backend=false -input=false -plugin-dir=$PWD/{plugins_mirror} > /dev/null; {tf} -chdir={dir} validate".format(
dir = ctx.attr.module.label.package,
tf = tf_runtime.tf.path,
plugins_mirror = tf_plugins_mirror.short_path,
plugins_mirror = tf_runtime.mirror.path,
)

ctx.actions.write(
output = ctx.outputs.executable,
content = cmd,
)

deps = ctx.attr.module[TfModuleInfo].transitive_srcs.to_list() + [
tf_plugins_mirror,
tf_runtime.tf,
]
deps = ctx.attr.module[TfModuleInfo].transitive_srcs.to_list() + tf_runtime.deps

return [DefaultInfo(
runfiles = ctx.runfiles(files = deps),
Expand All @@ -171,7 +167,6 @@ tf_validate_test = rule(
},
test = True,
toolchains = [
"@rules_tf//:tf_plugins_mirror_toolchain_type",
"@rules_tf//:tf_toolchain_type",
],
)
Expand Down
35 changes: 3 additions & 32 deletions tf/toolchains.bzl
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
load("@rules_tf//tf/toolchains/terraform:toolchain.bzl", _terraform_toolchain = "terraform_toolchain")
load("@rules_tf//tf/toolchains:tf_toolchain.bzl", _tf_toolchain = "tf_toolchain")
load("@rules_tf//tf/toolchains/terraform:toolchain.bzl", _terraform_declare_toolchain_chunk = "DECLARE_TOOLCHAIN_CHUNK")
load("@rules_tf//tf/toolchains/tofu:toolchain.bzl", _tofu_toolchain = "tofu_toolchain")
load("@rules_tf//tf/toolchains/tofu:toolchain.bzl", _tofu_declare_toolchain_chunk = "DECLARE_TOOLCHAIN_CHUNK")
load("@rules_tf//tf/toolchains/tfdoc:toolchain.bzl", _tfdoc_toolchain = "tfdoc_toolchain")
load("@rules_tf//tf/toolchains/tfdoc:toolchain.bzl", _tfdoc_declare_toolchain_chunk = "DECLARE_TOOLCHAIN_CHUNK")
load("@rules_tf//tf/toolchains/tflint:toolchain.bzl", _tflint_toolchain = "tflint_toolchain")
load("@rules_tf//tf/toolchains/tflint:toolchain.bzl", _tflint_declare_toolchain_chunk = "DECLARE_TOOLCHAIN_CHUNK")
load("@rules_tf//tf/toolchains/plugins_mirror:toolchain.bzl", _plugins_mirror_toolchain = "plugins_mirror_toolchain")
load("@rules_tf//tf/toolchains/plugins_mirror:toolchain.bzl", _plugins_mirror_declare_toolchain_chunk = "DECLARE_TOOLCHAIN_CHUNK")

platforms = {
"linux_amd64": {
Expand Down Expand Up @@ -69,19 +66,16 @@ def detect_host_platform(ctx):
return os, arch


terraform_toolchain = _terraform_toolchain
tofu_toolchain = _tofu_toolchain
tf_toolchain = _tf_toolchain
tfdoc_toolchain = _tfdoc_toolchain
tflint_toolchain = _tflint_toolchain
plugins_mirror_toolchain = _plugins_mirror_toolchain

def _tf_toolchains_impl(ctx):
content = """
load("@rules_tf//tf:toolchains.bzl", "platforms")
load("@rules_tf//tf:toolchains.bzl", "tfdoc_toolchain")
load("@rules_tf//tf:toolchains.bzl", "tflint_toolchain")
load("@rules_tf//tf:toolchains.bzl", "terraform_toolchain")
load("@rules_tf//tf:toolchains.bzl", "tofu_toolchain")
load("@rules_tf//tf:toolchains.bzl", "tf_toolchain")
package(default_visibility = ["//visibility:public"])
"""
Expand Down Expand Up @@ -131,26 +125,3 @@ tf_toolchains = repository_rule(
"arch": attr.string(mandatory = True),
},
)


def _tf_plugins_mirrors_toolchains_impl(ctx):
content = """
load("@rules_tf//tf:toolchains.bzl", "plugins_mirror_toolchain")
package(default_visibility = ["//visibility:public"])
"""

for repo in ctx.attr.mirror_repos:
chunk = _plugins_mirror_declare_toolchain_chunk.format(
toolchain_repo = repo,
versions = ctx.attr.mirror_repos[repo],
)
content += chunk

ctx.file( "BUILD.bazel", content, executable = False )

tf_plugins_mirrors_toolchains = repository_rule(
implementation = _tf_plugins_mirrors_toolchains_impl,
attrs = {
"mirror_repos": attr.string_dict(mandatory = True),
},
)
26 changes: 0 additions & 26 deletions tf/toolchains/plugins_mirror/BUILD.bazel

This file was deleted.

Loading

0 comments on commit 96de1dc

Please sign in to comment.