Skip to content

Commit

Permalink
replace plugin sharing with built in terraform variant
Browse files Browse the repository at this point in the history
  • Loading branch information
wr0ngway committed Jan 11, 2021
1 parent 9878336 commit eb75266
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 50 deletions.
39 changes: 19 additions & 20 deletions Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
simplygenius-atmos (0.12.2)
simplygenius-atmos (0.13.0)
activesupport (>= 5.2.4.3)
aws-sdk-cloudwatchlogs
aws-sdk-core
Expand Down Expand Up @@ -32,44 +32,44 @@ PATH
GEM
remote: https://rubygems.org/
specs:
activesupport (6.0.3.4)
activesupport (6.1.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
zeitwerk (~> 2.2, >= 2.2.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
zeitwerk (~> 2.3)
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
aws-eventstream (1.1.0)
aws-partitions (1.383.0)
aws-partitions (1.415.0)
aws-sdk-cloudwatchlogs (1.38.0)
aws-sdk-core (~> 3, >= 3.109.0)
aws-sigv4 (~> 1.1)
aws-sdk-core (3.109.1)
aws-sdk-core (3.110.0)
aws-eventstream (~> 1, >= 1.0.2)
aws-partitions (~> 1, >= 1.239.0)
aws-sigv4 (~> 1.1)
jmespath (~> 1.0)
aws-sdk-ecr (1.39.0)
aws-sdk-ecr (1.40.0)
aws-sdk-core (~> 3, >= 3.109.0)
aws-sigv4 (~> 1.1)
aws-sdk-ecs (1.70.0)
aws-sdk-ecs (1.72.0)
aws-sdk-core (~> 3, >= 3.109.0)
aws-sigv4 (~> 1.1)
aws-sdk-iam (1.46.0)
aws-sdk-core (~> 3, >= 3.109.0)
aws-sigv4 (~> 1.1)
aws-sdk-kms (1.39.0)
aws-sdk-kms (1.40.0)
aws-sdk-core (~> 3, >= 3.109.0)
aws-sigv4 (~> 1.1)
aws-sdk-organizations (1.53.0)
aws-sdk-organizations (1.55.0)
aws-sdk-core (~> 3, >= 3.109.0)
aws-sigv4 (~> 1.1)
aws-sdk-s3 (1.83.1)
aws-sdk-s3 (1.87.0)
aws-sdk-core (~> 3, >= 3.109.0)
aws-sdk-kms (~> 1)
aws-sigv4 (~> 1.1)
aws-sdk-ssm (1.95.0)
aws-sdk-ssm (1.101.0)
aws-sdk-core (~> 3, >= 3.109.0)
aws-sigv4 (~> 1.1)
aws-sigv4 (1.2.2)
Expand Down Expand Up @@ -97,7 +97,7 @@ GEM
hashdiff (1.0.1)
hashie (3.6.0)
highline (2.0.3)
i18n (1.8.5)
i18n (1.8.7)
concurrent-ruby (~> 1.0)
inifile (3.0.0)
jmespath (1.4.0)
Expand All @@ -107,7 +107,7 @@ GEM
little-plugger (~> 1.1)
multi_json (~> 1.10)
method_source (1.0.0)
minitest (5.14.2)
minitest (5.14.3)
multi_json (1.15.0)
os (1.0.1)
pry (0.13.1)
Expand Down Expand Up @@ -145,17 +145,16 @@ GEM
tins (~> 1.0)
test_construct (2.0.2)
thor (0.19.4)
thread_safe (0.3.6)
tins (1.26.0)
sync
tzinfo (1.2.7)
thread_safe (~> 0.1)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
vcr (6.0.0)
webmock (3.9.2)
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
zeitwerk (2.4.0)
zeitwerk (2.4.2)

PLATFORMS
ruby
Expand Down
2 changes: 0 additions & 2 deletions lib/simplygenius/atmos/commands/init.rb
Expand Up @@ -15,8 +15,6 @@ def self.description
def execute
@terraform_arguments.insert(0, "init")
super

init_shared_plugins
end

end
Expand Down
16 changes: 7 additions & 9 deletions lib/simplygenius/atmos/commands/terraform.rb
Expand Up @@ -28,20 +28,16 @@ def init_automatically(auth_env, get_modules)
if auto_init && auto_init_enabled && ! backend_initialized
exe = TerraformExecutor.new(process_env: auth_env)
exe.run("init", get_modules: get_modules.present?)
init_shared_plugins
end
end

def init_shared_plugins
def enable_shared_plugins(env)
if ! Atmos.config["atmos.terraform.disable_shared_plugins"]
home_dir = OS.windows? ? File.join("~", "Application Data") : "~"
shared_plugins_dir = File.expand_path(File.join(home_dir,".terraform.d", "plugins"))
logger.debug("Updating shared terraform plugins dir: #{shared_plugins_dir}")
mkdir_p(shared_plugins_dir)
terraform_plugins_dir = File.join(Atmos.config.tf_working_dir,'recipes', '.terraform', 'plugins')
if File.exist?(terraform_plugins_dir)
cp_r("#{terraform_plugins_dir}/.", shared_plugins_dir)
end
plugin_cache_dir = File.expand_path(File.join(home_dir,".terraform.d", "plugin-cache"))
logger.debug("Plugin cache dir: #{plugin_cache_dir}")
mkdir_p(plugin_cache_dir)
env["TF_PLUGIN_CACHE_DIR"] = plugin_cache_dir
end
end

Expand All @@ -58,6 +54,8 @@ def execute
begin
get_modules = @terraform_arguments.delete("--get-modules")

enable_shared_plugins(auth_env)

init_automatically(auth_env, get_modules)

exe = TerraformExecutor.new(process_env: auth_env)
Expand Down
2 changes: 1 addition & 1 deletion lib/simplygenius/atmos/version.rb
@@ -1,5 +1,5 @@
module SimplyGenius
module Atmos
VERSION = "0.12.2"
VERSION = "0.13.0"
end
end
2 changes: 1 addition & 1 deletion spec/commands/init_spec.rb
Expand Up @@ -26,7 +26,7 @@ module Commands
expect(TerraformExecutor).to receive(:new).
with(process_env: env).and_return(te)
expect(te).to receive(:run).with("init", get_modules: false)
expect(cli).to receive(:init_shared_plugins)
expect(cli).to receive(:enable_shared_plugins)
cli.run([])
expect(cli.auto_init).to be_falsey
end
Expand Down
23 changes: 12 additions & 11 deletions spec/commands/terraform_spec.rb
Expand Up @@ -39,21 +39,22 @@ module Commands

describe "shared_plugin_dir" do

it "copies plugins to user shared plugin dir if enabled" do
it "sets up plugin cache if enabled" do
within_construct do |c|
Atmos.config = Config.new("ops")
expect(cli).to receive(:mkdir_p).with(/.terraform.d\/plugins/)
cli.init_shared_plugins
env = {}
cli.enable_shared_plugins(env)
expect(env).to include("TF_PLUGIN_CACHE_DIR")
end
end

it "doesn't copy plugins to user shared plugin dir if disabled" do
it "doesn't setup plugin cacheif disabled" do
within_construct do |c|
c.file('config/atmos.yml', YAML.dump("atmos" => {"terraform" => {"disable_shared_plugins" => true}}))
Atmos.config = Config.new("ops")

expect(cli).to receive(:mkdir_p).with(/.terraform.d\/plugins/).never
cli.init_shared_plugins
env = {}
cli.enable_shared_plugins(env)
expect(env).to_not include("TF_PLUGIN_CACHE_DIR")
end
end

Expand All @@ -71,7 +72,7 @@ module Commands
expect(TerraformExecutor).to receive(:new).twice.and_return(te)
expect(te).to receive(:run).with('init', get_modules: false)
expect(te).to receive(:run).with('--help', 'foo', '--bar', get_modules: false)
expect(cli).to receive(:init_shared_plugins)
expect(cli).to receive(:enable_shared_plugins)
cli.run(['--help', 'foo', '--bar'])
end

Expand All @@ -87,7 +88,7 @@ module Commands
expect(TerraformExecutor).to receive(:new).and_return(te)
expect(te).to receive(:run).with('init', get_modules: false).never
expect(te).to receive(:run).with('--help', 'foo', '--bar', get_modules: false)
expect(cli).to_not receive(:init_shared_plugins)
expect(cli).to receive(:enable_shared_plugins)
cli.run(['--help', 'foo', '--bar'])
end

Expand All @@ -101,7 +102,7 @@ module Commands
expect(TerraformExecutor).to receive(:new).and_return(te)
expect(te).to receive(:run).with('init', get_modules: false).never
expect(te).to receive(:run).with('--help', 'foo', '--bar', get_modules: false)
expect(cli).to_not receive(:init_shared_plugins)
expect(cli).to receive(:enable_shared_plugins)
cli.run(['--help', 'foo', '--bar'])
end

Expand All @@ -115,7 +116,7 @@ module Commands
expect(TerraformExecutor).to receive(:new).and_return(te)
expect(te).to receive(:run).with('init', get_modules: false).never
expect(te).to receive(:run).with('--help', 'foo', '--bar', get_modules: false)
expect(cli).to_not receive(:init_shared_plugins)
expect(cli).to receive(:enable_shared_plugins)
cli.run(['--help', 'foo', '--bar'])
end

Expand Down
12 changes: 6 additions & 6 deletions templates/new/config/atmos/runtime.yml
Expand Up @@ -119,12 +119,12 @@ atmos:
terraform:
# Disable module fetch from convenience plan/apply commands
disable_auto_modules: false
# By default (value=false), `atmos init` will update the terraform user
# plugin directory (~/.terraform.d/plugins) with the plugins for the current
# env/group so that they can be reused across all env/group combinations.
# Otherwise, disabling this functionality (value=true) means that each
# env/group combination will be independent and download all plugins for
# itself only
# By default (value=false), `atmos init` (and auto init) will set
# TF_PLUGIN_CACHE_DIR to ~/.terraform.d/plugin-cache so that when terraform
# installs providers/plugins they can be reused across all env/group
# combinations. Otherwise, disabling this functionality (value=true) means
# that each env/group combination will be independent and download all
# plugins for itself only
disable_shared_plugins: false
# Customize what gets linked into the working directory that terraform gets
# executed in
Expand Down

0 comments on commit eb75266

Please sign in to comment.