Skip to content

Commit

Permalink
change terraform plugin sharing to do so by copying the plugins to th…
Browse files Browse the repository at this point in the history
…e home directory location that is searched by terraform .
  • Loading branch information
wr0ngway committed Oct 18, 2018
1 parent 58e51a1 commit 9779685
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 48 deletions.
15 changes: 15 additions & 0 deletions lib/simplygenius/atmos/commands/init.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
require_relative 'terraform'
require 'fileutils'
require 'os'

module SimplyGenius
module Atmos
module Commands

class Init < Terraform
include FileUtils

def self.description
"Runs terraform init"
Expand All @@ -13,6 +16,18 @@ def self.description
def execute
@terraform_arguments.insert(0, "init")
super

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
end

end

end
Expand Down
12 changes: 0 additions & 12 deletions lib/simplygenius/atmos/terraform_executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ def execute(*terraform_args, skip_secrets: false, output_io: nil)

def setup_working_dir(skip_backend: false)
clean_links
link_shared_plugin_dir
link_support_dirs
link_recipes
write_atmos_vars
Expand Down Expand Up @@ -250,17 +249,6 @@ def link_support_dirs
end
end

def link_shared_plugin_dir
if ! Atmos.config["atmos.terraform.disable_shared_plugins"]
shared_plugins_dir = File.join(Atmos.config.tmp_root, "terraform_plugins")
mkdir_p(shared_plugins_dir)
terraform_state_dir = File.join(tf_recipes_dir, '.terraform')
mkdir_p(terraform_state_dir)
terraform_plugins_dir = File.join(terraform_state_dir, 'plugins')
ln_sf(shared_plugins_dir, terraform_plugins_dir)
end
end

def link_recipes
@recipes.each do |recipe|
ln_sf(File.join(Atmos.config.root_dir, 'recipes', "#{recipe}.tf"), tf_recipes_dir)
Expand Down
38 changes: 38 additions & 0 deletions spec/commands/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,44 @@ module Commands

end

describe "shared_plugin_dir" do

it "copies plugins to user shared plugin dir if enabled" do
within_construct do |c|
c.file('config/atmos.yml')

Atmos.config = Config.new("ops")

env = Hash.new
te = TerraformExecutor.new(env)
expect(Atmos.config.provider.auth_manager).to receive(:authenticate).and_yield(env)
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(:mkdir_p).with(/.terraform.d\/plugins/)
cli.run([])
end
end

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

Atmos.config = Config.new("ops")

env = Hash.new
te = TerraformExecutor.new(env)
expect(Atmos.config.provider.auth_manager).to receive(:authenticate).and_yield(env)
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(:mkdir_p).with(/.terraform.d\/plugins/).never
cli.run([])
end
end

end

end

end
Expand Down
33 changes: 0 additions & 33 deletions spec/terraform_executor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,39 +150,6 @@ module Atmos

end

describe "link_shared_plugin_dir" do

it "links a shared plugin dir into terraform cwd" do
within_construct do |c|
c.file('config/atmos.yml')

Atmos.config = Config.new("ops")
te.send(:link_shared_plugin_dir)

dest = File.join(Atmos.config.tmp_root, "terraform_plugins")
expect(File.exist?(dest)).to be true
link = File.join(te.send(:tf_recipes_dir), '.terraform', 'plugins')
expect(File.symlink?(link)).to be true
expect(File.readlink(link)).to eq(dest)
end
end

it "doesn't link if config disabled" do
within_construct do |c|
c.file('config/atmos.yml', YAML.dump("atmos" => {"terraform" => {"disable_shared_plugins" => true}}))

Atmos.config = Config.new("ops")
te.send(:link_shared_plugin_dir)

dest = File.join(Atmos.config.tmp_root, "terraform_plugins")
expect(File.exist?(dest)).to be false
link = File.join(te.send(:tf_recipes_dir), '.terraform', 'plugins')
expect(File.symlink?(link)).to be false
end
end

end

describe "clean_links" do

it "removes atmos working dir links" do
Expand Down
9 changes: 6 additions & 3 deletions templates/new/config/atmos/runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ atmos:
terraform:
# Disable module fetch from convenience plan/apply commands
disable_auto_modules: false
# By default, all terraform plugins are placed in a directory so they can be
# reused across all envs/groups. This can be disabled here (you can also
# override using the terraform cli options for plugins)
# 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
disable_shared_plugins: false

0 comments on commit 9779685

Please sign in to comment.