From 282a3fb2d377edb9474938f7ce8274ab71842a83 Mon Sep 17 00:00:00 2001 From: Alex Somesan Date: Mon, 26 Mar 2018 13:54:47 +0200 Subject: [PATCH 1/2] Bazel build simplifications * only build one platform at a time * copy templates + resources as runtime files on the cli target * remove unused rules and dependencies * source version for tarball from versions.bzl * version the tarball --- .bazelrc | 1 + BUILD.bazel | 126 +++++------------- Jenkinsfile | 12 +- README.md | 7 +- WORKSPACE | 35 ++--- examples/BUILD.bazel | 8 +- images/tectonic-installer/Dockerfile | 2 +- installer/BUILD.bazel | 26 ++++ installer/cmd/tectonic/BUILD.bazel | 51 ------- installer/pkg/workflow/utils.go | 11 +- tests/rspec/lib/aws_cluster.rb | 7 +- tests/rspec/lib/cluster.rb | 2 +- .../lib/shared_examples/build_folder_setup.rb | 4 +- tests/rspec/lib/tfstate_file.rb | 2 +- tests/rspec/spec/spec_helper.rb | 2 +- version.bzl | 8 ++ 16 files changed, 113 insertions(+), 191 deletions(-) delete mode 100644 installer/cmd/tectonic/BUILD.bazel create mode 100644 version.bzl diff --git a/.bazelrc b/.bazelrc index faa484733a..f4cab9c4cb 100644 --- a/.bazelrc +++ b/.bazelrc @@ -1 +1,2 @@ +startup --output_base ./build build --color=yes --workspace_status_command=./buildvars.sh diff --git a/BUILD.bazel b/BUILD.bazel index 09e808b673..f334f09e71 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,4 +1,5 @@ load("//bazel-rules:gen_test.bzl", "gen_test") +load("//:version.bzl", "TECTONIC_VERSION") package( default_visibility = ["//visibility:public"], @@ -17,7 +18,7 @@ config_setting( ) genrule( - name = "terraform_runtime", + name = "terraform_runtime_fmt", output_to_bindir = 1, srcs = select({ "//:linux": ["@terraform_runtime_linux//:terraform"], @@ -28,19 +29,6 @@ genrule( executable = True, ) -genrule( - name = "provider_matchbox", - output_to_bindir = 1, - srcs = select({ - "//:linux": ["@terraform_provider_matchbox_linux//:terraform-provider-matchbox"], - "//:darwin": ["@terraform_provider_matchbox_darwin//:terraform-provider-matchbox"], - }), - outs = ["bin/terraform-provider-matchbox"], - cmd = "cp $(<) $(@)", -) - -exports_files(["config.tf"]) - load("@io_bazel_rules_go//go:def.bzl", "go_prefix") go_prefix("github.com/coreos/tectonic-installer") @@ -61,96 +49,54 @@ alias( alias( name = "cli", - actual = "//installer/cmd/tectonic:tectonic", -) - -load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar") - -# We use a genrule here to combine the tarballs rather than a pkg_tar -# because we want to be able to optionally add a version string to the -# directory name and pkg_tar does not allow for formatted names. -genrule( - name = "tarball", - srcs = [ - ":tarball_tectonic_binary_darwin", - ":tarball_tectonic_binary_linux", - ":tarball_terraform_configuration", - ":tarball_terraform_binary_darwin", - ":tarball_terraform_binary_linux", - ":tarball_terraform_provider_matchbox_darwin", - ":tarball_terraform_provider_matchbox_linux", - ":tarball_examples", - ], - outs = ["tectonic.tar.gz"], - cmd = '\n'.join([ - "VERSION=$${TECTONIC_VERSION:-}", - "DIR=tectonic", - "[ ! -z $$VERSION ] && DIR=\"$$DIR\"_\"$$VERSION\"", - "TMPDIR=$$(mktemp -d)", - "mkdir $$TMPDIR/$$DIR", - "for s in $(SRCS); do", - "tar -C $$TMPDIR/$$DIR --extract --file $$s --strip-components=1", - "done", - "tar -C $$TMPDIR --create --gzip --file $@ $$DIR" - ]), - output_to_bindir = 1, + actual = "//installer:tectonic", ) -pkg_tar( - name = "tarball_terraform_configuration", - mode = "0644", - srcs = glob(["modules/**"]) + glob(["steps/**"]) + ["config.tf"], - strip_prefix = ".", -) +template_files = glob([ + "modules/**/*", + "steps/**/*", + "config.tf" +]) -pkg_tar( - name = "tarball_tectonic_binary_darwin", - mode = "0755", - package_dir = "tectonic-installer/darwin", - srcs = ["//installer/cmd/tectonic:darwin"], -) +exports_files(template_files) -pkg_tar( - name = "tarball_tectonic_binary_linux", - mode = "0755", - package_dir = "tectonic-installer/linux", - srcs = ["//installer/cmd/tectonic:linux"], +filegroup( + name = "template_resources", + srcs = template_files ) -pkg_tar( - name = "tarball_terraform_binary_darwin", - mode = "0755", - package_dir = "tectonic-installer/darwin", - srcs = ["@terraform_runtime_darwin//:terraform"], -) +load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar") -pkg_tar( - name = "tarball_terraform_binary_linux", - mode = "0755", - package_dir = "tectonic-installer/linux", - srcs = ["@terraform_runtime_linux//:terraform"], +alias( + name = "tarball", + actual = ":tectonic-%s" % TECTONIC_VERSION ) pkg_tar( - name = "tarball_terraform_provider_matchbox_darwin", - mode = "0755", - package_dir = "tectonic-installer/darwin", - srcs = ["@terraform_provider_matchbox_darwin//:terraform-provider-matchbox"], + name = "tectonic-%s" % TECTONIC_VERSION, + strip_prefix = '.', + package_dir = "tectonic-%s" % TECTONIC_VERSION, + extension = "tar.gz", + srcs = [ + "//installer:tectonic", + "//:template_resources", + "//examples:tectonic_cli_examples", + ], + deps = [":tf_bin"] ) -pkg_tar( - name = "tarball_terraform_provider_matchbox_linux", - mode = "0755", - package_dir = "tectonic-installer/linux", - srcs = ["@terraform_provider_matchbox_linux//:terraform-provider-matchbox"], +filegroup ( + name = "terraform_runtime", + srcs = select({ + "//:linux": ["@terraform_runtime_linux//:terraform"], + "//:darwin": ["@terraform_runtime_darwin//:terraform"], + }) ) pkg_tar( - name = "tarball_examples", - package_dir = "examples", - # As long as we are not auto-generating the tectonic cli configuration - # examples, make sure to add the manually created once to the tarball. - srcs = ["//examples:tectonic_cli_configs"], + name = "tf_bin", + package_dir = "installer", + srcs = [":terraform_runtime"] ) filegroup( @@ -167,6 +113,6 @@ filegroup( gen_test( name = "terraform_fmt", command = "./bin/terraform fmt -list -check -write=false", - deps = [":terraform_runtime", ":terraform_files"], + deps = [":terraform_runtime_fmt", ":terraform_files"], size = "small", ) diff --git a/Jenkinsfile b/Jenkinsfile index ff1dbe682f..6eab16a768 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -166,12 +166,12 @@ pipeline { bazel build tarball tests/smoke # Jenkins `stash` does not follow symlinks - thereby temporarily copy the files to the root dir - cp bazel-bin/tectonic.tar.gz . + cp bazel-bin/tectonic-dev.tar.gz . cp bazel-bin/tests/smoke/linux_amd64_stripped/smoke . """ - stash name: 'tectonic.tar.gz', includes: 'tectonic.tar.gz' + stash name: 'tectonic-tarball', includes: 'tectonic-dev.tar.gz' stash name: 'smoke-tests', includes: 'smoke' - archiveArtifacts allowEmptyArchive: true, artifacts: 'tectonic.tar.gz' + archiveArtifacts allowEmptyArchive: true, artifacts: 'tectonic-dev.tar.gz' } withDockerContainer(tectonicSmokeTestEnvImage) { @@ -239,7 +239,7 @@ pipeline { withCredentials(quayCreds) { ansiColor('xterm') { unstash 'clean-repo' - unstash 'tectonic.tar.gz' + unstash 'tectonic-tarball' sh """ docker build -t quay.io/coreos/tectonic-installer:master -f images/tectonic-installer/Dockerfile . docker login -u="$QUAY_ROBOT_USERNAME" -p="$QUAY_ROBOT_SECRET" quay.io @@ -305,12 +305,12 @@ def forcefullyCleanWorkspace() { def unstashCleanRepoTectonicTarGZSmokeTests() { unstash 'clean-repo' - unstash 'tectonic.tar.gz' + unstash 'tectonic-tarball' unstash 'smoke-tests' sh """#!/bin/bash -ex # Jenkins `stash` does not follow symlinks - thereby temporarily copy the files to the root dir mkdir -p bazel-bin/tests/smoke/linux_amd64_stripped/ - cp tectonic.tar.gz bazel-bin/. + cp tectonic-dev.tar.gz bazel-bin/. cp smoke bazel-bin/tests/smoke/linux_amd64_stripped/. """ } diff --git a/README.md b/README.md index b0e0f6bf4d..b231c09875 100644 --- a/README.md +++ b/README.md @@ -46,14 +46,13 @@ These instructions can be used for AWS: 2. Extract the tarball ```shell - cd bazel-bin - tar -zxf tectonic.tar.gz - cd tectonic + tar -zxf bazel-bin/tectonic-dev.tar.gz + cd tectonic-dev ``` 3. Add binaries to $PATH ```shell - export PATH=$(pwd)/tectonic-installer/linux/:$PATH + export PATH=$(pwd)/installer:$PATH ``` 4. Edit Tectonic configuration file including the $CLUSTER_NAME diff --git a/WORKSPACE b/WORKSPACE index 12b54518fd..a7354023b9 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -1,28 +1,31 @@ -workspace(name = "tectonic_installer") +workspace(name = "installer") -terrafom_version = "0.11.2" - -provider_matchbox_version = "0.2.2" +terrafom_version = "0.11.7" supported_platforms = [ "linux", "darwin", ] -# Latest working commit for cross compilation [0] until fix [1] lands. -# [0] https://github.com/bazelbuild/rules_go/issues/1240#issuecomment-357789209 -# [1] https://github.com/bazelbuild/rules_go/pull/1248 +# We need a feature from recent master to get rid of the platform specific part of the go_binary path. +# https://github.com/bazelbuild/rules_go/pull/1393 + git_repository( name = "io_bazel_rules_go", remote = "https://github.com/bazelbuild/rules_go.git", - commit = "3f38260eda98d23e9142bb905caede5912508770" + commit = "361556b0d27318d1b8fa42c91a4baa4ab5ea1c58" ) +# http_archive( +# name = "io_bazel_rules_go", +# url = "https://github.com/bazelbuild/rules_go/releases/download/0.10.1/rules_go-0.10.1.tar.gz", +# sha256 = "4b14d8dd31c6dbaf3ff871adcd03f28c3274e42abc855cb8fb4d01233c0154dc", +# ) + http_archive( name = "bazel_gazelle", - sha256 = "4952295aa35241082eefbb53decd7d4dd4e67a1f52655d708a1da942e3f38975", - url = "https://github.com/bazelbuild/bazel-gazelle/archive/eaa1e87d2a3ca716780ca6650ef5b9b9663b8773.zip", - strip_prefix = "bazel-gazelle-eaa1e87d2a3ca716780ca6650ef5b9b9663b8773", + url = "https://github.com/bazelbuild/bazel-gazelle/releases/download/0.10.1/bazel-gazelle-0.10.1.tar.gz", + sha256 = "d03625db67e9fb0905bbd206fa97e32ae9da894fe234a493e7517fd25faec914", ) load("@io_bazel_rules_go//go:def.bzl", "go_rules_dependencies", "go_register_toolchains", "go_repository") @@ -44,13 +47,3 @@ gazelle_dependencies() type = "zip", url = "https://releases.hashicorp.com/terraform/%s/terraform_%s_%s_amd64.zip" % (terrafom_version, terrafom_version, platform), ) for platform in supported_platforms] - -[new_http_archive( - name = "terraform_provider_matchbox_%s" % platform, - build_file_content = """exports_files( -["terraform-provider-matchbox"], -visibility = ["//visibility:public"] -)""", - strip_prefix = "terraform-provider-matchbox-v%s-%s-amd64/" % (provider_matchbox_version, platform), - url = "https://github.com/coreos/terraform-provider-matchbox/releases/download/v%s/terraform-provider-matchbox-v%s-%s-amd64.tar.gz" % (provider_matchbox_version, provider_matchbox_version, platform), -) for platform in supported_platforms] diff --git a/examples/BUILD.bazel b/examples/BUILD.bazel index dbdea724d2..e5987d4671 100644 --- a/examples/BUILD.bazel +++ b/examples/BUILD.bazel @@ -1,5 +1,9 @@ +example_cli_configs = glob(["tectonic.*.yaml"]) + filegroup( - name= "tectonic_cli_configs", - srcs = glob(["tectonic.*.yaml"]), + name= "tectonic_cli_examples", + srcs = example_cli_configs, visibility = ["//visibility:public"], ) + +exports_files(example_cli_configs) diff --git a/images/tectonic-installer/Dockerfile b/images/tectonic-installer/Dockerfile index 4d780cdce0..c5754f5344 100644 --- a/images/tectonic-installer/Dockerfile +++ b/images/tectonic-installer/Dockerfile @@ -4,4 +4,4 @@ FROM alpine:3.7 # https://github.com/moby/moby/issues/1676) Make sure to first copy the tarball # from bazel-bin/tectonic.tar.gz to the docker build context folder of your # choice (e.g. the root of the repository). -ADD tectonic.tar.gz / +ADD tectonic-dev.tar.gz / diff --git a/installer/BUILD.bazel b/installer/BUILD.bazel index fa2cfd519b..181f712310 100644 --- a/installer/BUILD.bazel +++ b/installer/BUILD.bazel @@ -1,3 +1,5 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") + test_suite( name = "cli_units", tests = [ @@ -7,3 +9,27 @@ test_suite( "//installer/pkg/workflow:go_default_test", ], ) + +go_library( + name = "go_default_library", + srcs = [ + "cmd/tectonic/main.go", + ], + visibility = ["//visibility:private"], + deps = [ + "//installer/pkg/workflow:go_default_library", + "//installer/vendor/github.com/Sirupsen/logrus:go_default_library", + "//installer/vendor/gopkg.in/alecthomas/kingpin.v2:go_default_library", + ], +) + +go_binary( + name = "tectonic", + out = "tectonic", + embed = [":go_default_library"], + # Use pure to build a pure-go binary. + # This has the nice side effect of making the binary statically linked. + pure = "on", + visibility = ["//visibility:public"], + data = ["//:template_resources"] +) diff --git a/installer/cmd/tectonic/BUILD.bazel b/installer/cmd/tectonic/BUILD.bazel deleted file mode 100644 index de0b70758b..0000000000 --- a/installer/cmd/tectonic/BUILD.bazel +++ /dev/null @@ -1,51 +0,0 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") - -go_library( - name = "go_default_library", - srcs = [ - "main.go", - ], - importpath = "github.com/coreos/tectonic-installer/installer/cmd/tectonic", - visibility = ["//visibility:private"], - deps = [ - "//installer/pkg/workflow:go_default_library", - "//installer/vendor/github.com/Sirupsen/logrus:go_default_library", - "//installer/vendor/gopkg.in/alecthomas/kingpin.v2:go_default_library", - ], -) - -go_binary( - name = "tectonic", - embed = [":go_default_library"], - importpath = "github.com/coreos/tectonic-installer/installer/cmd/tectonic", - # Use pure to build a pure-go binary. - # This has the nice side effect of making the binary statically linked. - pure = "on", - visibility = ["//visibility:public"], -) - -go_binary( - name = "darwin", - basename = "tectonic", - embed = [":go_default_library"], - goarch = "amd64", - goos = "darwin", - importpath = "github.com/coreos/tectonic-installer/installer/cmd/tectonic", - # Use pure to build a pure-go binary. - # This has the nice side effect of making the binary statically linked. - pure = "on", - visibility = ["//visibility:public"], -) - -go_binary( - name = "linux", - basename = "tectonic", - embed = [":go_default_library"], - goarch = "amd64", - goos = "linux", - importpath = "github.com/coreos/tectonic-installer/installer/cmd/tectonic", - # Use pure to build a pure-go binary. - # This has the nice side effect of making the binary statically linked. - pure = "on", - visibility = ["//visibility:public"], -) diff --git a/installer/pkg/workflow/utils.go b/installer/pkg/workflow/utils.go index 0574d4b276..18018ba73b 100644 --- a/installer/pkg/workflow/utils.go +++ b/installer/pkg/workflow/utils.go @@ -7,10 +7,9 @@ import ( "os" "path" "path/filepath" - "runtime" "github.com/coreos/tectonic-installer/installer/pkg/config" - "github.com/coreos/tectonic-installer/installer/pkg/config-generator" + configgenerator "github.com/coreos/tectonic-installer/installer/pkg/config-generator" log "github.com/Sirupsen/logrus" ) @@ -28,7 +27,9 @@ const ( joinWorkersStep = "joining_workers" configFileName = "config.yaml" internalFileName = "internal.yaml" - binaryPrefix = "tectonic-installer" + kubeConfigPath = "generated/auth/kubeconfig" + binaryPrefix = "installer" + tncDaemonSet = "tectonic-node-controller" ) func copyFile(fromFilePath, toFilePath string) error { @@ -214,10 +215,6 @@ func baseLocation() (string, error) { return "", fmt.Errorf("undetermined location of own executable: %s", err) } ex = path.Dir(ex) - if path.Base(ex) != runtime.GOOS { - return "", fmt.Errorf("%s executable in unknown location: %s", path.Base(ex), err) - } - ex = path.Dir(ex) if path.Base(ex) != binaryPrefix { return "", fmt.Errorf("%s executable in unknown location: %s", path.Base(ex), err) } diff --git a/tests/rspec/lib/aws_cluster.rb b/tests/rspec/lib/aws_cluster.rb index 7c156f8e08..ff04b2da4e 100644 --- a/tests/rspec/lib/aws_cluster.rb +++ b/tests/rspec/lib/aws_cluster.rb @@ -25,7 +25,7 @@ def initialize(config_file) @aws_region = ENV['TF_VAR_tectonic_aws_region'] @name = @config_file.cluster_name - @build_path = File.join(File.dirname(ENV['RELEASE_TARBALL_PATH']), "tectonic/#{@name}") + @build_path = File.join(File.dirname(ENV['RELEASE_TARBALL_PATH']), "tectonic-dev/#{@name}") @manifest_path = File.join(@build_path, 'generated') @kubeconfig = File.join(manifest_path, 'auth/kubeconfig') @@ -207,15 +207,14 @@ def destroy end def run_tectonic_cli(env, cmd, flags = '') - os = Gem::Platform.local.os tectonic_binary = File.join( File.dirname(ENV['RELEASE_TARBALL_PATH']), - "tectonic/tectonic-installer/#{os}/tectonic" + 'tectonic-dev/installer/tectonic' ) tectonic_logs = File.join( File.dirname(ENV['RELEASE_TARBALL_PATH']), - "tectonic/#{@name}/logs/tectonic-#{cmd}.log" + "tectonic-dev/#{@name}/logs/tectonic-#{cmd}.log" ) command = "#{tectonic_binary} #{cmd} #{flags}" diff --git a/tests/rspec/lib/cluster.rb b/tests/rspec/lib/cluster.rb index 40a39ded74..2c5c0934a0 100644 --- a/tests/rspec/lib/cluster.rb +++ b/tests/rspec/lib/cluster.rb @@ -30,7 +30,7 @@ def initialize(tfvars_file) @tectonic_admin_password = ENV['TF_VAR_tectonic_admin_password'] || PasswordGenerator.generate_password save_console_creds(@name, @tectonic_admin_email, @tectonic_admin_password) - @build_path = File.join(File.dirname(ENV['RELEASE_TARBALL_PATH']), "tectonic/build/#{@name}") + @build_path = File.join(File.dirname(ENV['RELEASE_TARBALL_PATH']), "tectonic-dev/#{@name}") @manifest_path = File.join(@build_path, 'generated') @kubeconfig = File.join(manifest_path, 'auth/kubeconfig') @tfstate_file = TFStateFile.new(@build_path) diff --git a/tests/rspec/lib/shared_examples/build_folder_setup.rb b/tests/rspec/lib/shared_examples/build_folder_setup.rb index 4cda610b81..5e00b66fdc 100644 --- a/tests/rspec/lib/shared_examples/build_folder_setup.rb +++ b/tests/rspec/lib/shared_examples/build_folder_setup.rb @@ -5,7 +5,7 @@ RSpec.shared_examples 'withBuildFolderSetupWithConfig' do |config_path| before(:all) do - Dir.chdir(File.join(File.dirname(ENV['RELEASE_TARBALL_PATH']), 'tectonic')) + Dir.chdir(File.join(File.dirname(ENV['RELEASE_TARBALL_PATH']), 'tectonic-dev')) # TODO: Only ignore on AWS temp_config_file = ConfigFile.new(config_path) @name = ENV['CLUSTER'] || NameGenerator.generate(temp_config_file.prefix) @@ -21,7 +21,7 @@ RSpec.shared_examples 'withBuildFolderSetup' do |tf_vars_path| before(:all) do - Dir.chdir(File.join(File.dirname(ENV['RELEASE_TARBALL_PATH']), 'tectonic')) + Dir.chdir(File.join(File.dirname(ENV['RELEASE_TARBALL_PATH']), 'tectonic-dev')) temp_tfvars_file = TFVarsFile.new(tf_vars_path) @name = ENV['CLUSTER'] || NameGenerator.generate(temp_tfvars_file.prefix) ENV['CLUSTER'] = @name diff --git a/tests/rspec/lib/tfstate_file.rb b/tests/rspec/lib/tfstate_file.rb index 097a285616..f1756725e4 100644 --- a/tests/rspec/lib/tfstate_file.rb +++ b/tests/rspec/lib/tfstate_file.rb @@ -20,7 +20,7 @@ def value(address, wanted_key) end end - msg = "could not find value for key \"#{wanted_key}\" in tfstate file #{@build_path}" + msg = "could not find value for key \"#{wanted_key}\" in '#{@tfstate_file_name}' tfstate file #{@build_path}" raise TFStateFileValueForKeyDoesNotExist, msg end diff --git a/tests/rspec/spec/spec_helper.rb b/tests/rspec/spec/spec_helper.rb index dfcd72e109..4917c70dc4 100644 --- a/tests/rspec/spec/spec_helper.rb +++ b/tests/rspec/spec/spec_helper.rb @@ -29,7 +29,7 @@ ENV['RSPEC_PATH'] = Dir.pwd unless ENV.key?('RSPEC_PATH') unless ENV.key?('RELEASE_TARBALL_PATH') - ENV['RELEASE_TARBALL_PATH'] = File.expand_path(File.join(Dir.pwd, '../../bazel-bin/tectonic.tar.gz')) + ENV['RELEASE_TARBALL_PATH'] = File.expand_path(File.join(Dir.pwd, '../../bazel-bin/tectonic-dev.tar.gz')) end # Supressing the `Run options` announcements to have a standard TAP output. diff --git a/version.bzl b/version.bzl new file mode 100644 index 0000000000..57182b95e6 --- /dev/null +++ b/version.bzl @@ -0,0 +1,8 @@ +# This variable set the release version. +# +# BEFORE building an official release, please update it +# with the actual release version string. +# +# THIS FILE SHOULD BE AUTOGENERATED BY RELEASE AUTOMATION. + +TECTONIC_VERSION="dev" From 15d15e6a2403fb1c8407ba9cc06a2f25f5593b11 Mon Sep 17 00:00:00 2001 From: Alex Somesan Date: Thu, 19 Apr 2018 09:25:06 -0400 Subject: [PATCH 2/2] Fix the Grafitti helper to support new step separation --- tests/rspec/lib/grafiti.rb | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/tests/rspec/lib/grafiti.rb b/tests/rspec/lib/grafiti.rb index e0fa1ca91a..37d8196339 100644 --- a/tests/rspec/lib/grafiti.rb +++ b/tests/rspec/lib/grafiti.rb @@ -1,5 +1,8 @@ # frozen_string_literal: true +require 'yaml' +require 'json' + # Grafiti contains helper functions to use the http://github.com/coreos/grafiti tool # class Grafiti @@ -7,7 +10,7 @@ class Grafiti attr_reader :tmp_dir attr_reader :config_file_path attr_reader :tag_file_path - attr_reader :terraform_log_path + attr_reader :terraform_internal_file attr_reader :aws_region def initialize(build_path, region) @@ -16,7 +19,7 @@ def initialize(build_path, region) @tmp_dir = `mktemp -d -p #{@build_path}`.chomp @config_file_path = File.join(@tmp_dir, 'config.toml') @tag_file_path = File.join(@tmp_dir, 'tag.json') - @terraform_log_path = File.join(build_path, 'terraform.tfstate') + @terraform_internal_file = File.join(build_path, 'internal.yaml') write_config_file write_tag_file end @@ -34,10 +37,8 @@ def clean end def cluster_id - cmd = 'grep -m 1 -oP'\ - ' \'^ *\"tags\.tectonicClusterID\": \"\K[0-9a-z-]*(?=\",$)\''\ - " #{terraform_log_path}" - `#{cmd}`.chomp + config = YAML.load_file(terraform_internal_file) + config['clusterId'] || '' end def write_config_file @@ -45,9 +46,7 @@ def write_config_file end def write_tag_file - content = '{"TagFilters":['\ - " {\"Key\":\"tectonicClusterID\",\"Values\":[\"#{cluster_id}\""\ - ']}]}' - IO.write(@tag_file_path, content) + tags = { 'TagFilters' => [{ 'Key' => 'tectonicClusterID', 'Values' => [cluster_id] }] } + IO.write(@tag_file_path, tags.to_json) end end