Class: SimplyGenius::Atmos::Commands::Bootstrap

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/simplygenius/atmos/commands/bootstrap.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from UI

#agree, #ask, #choose, color_enabled, color_enabled=, #display, #error, #notify, #say, #warn

Class Method Details

.descriptionObject



9
10
11
# File 'lib/simplygenius/atmos/commands/bootstrap.rb', line 9

def self.description
  "Sets up the initial aws account for use by atmos"
end

Instance Method Details

#executeObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/simplygenius/atmos/commands/bootstrap.rb', line 16

def execute
  orig_config = Atmos.config
  Atmos.config = Config.new(Atmos.config.atmos_env, 'bootstrap')

  tf_init_dir = File.join(Atmos.config.tf_working_dir, '.terraform')
  tf_initialized = File.exist?(tf_init_dir)
  backend_initialized = File.exist?(File.join(tf_init_dir, 'terraform.tfstate'))

  rebootstrap_msg = <<~EOF
    Bootstrap should only be performed when provisioning an account for the first
    time.  Try 'atmos terraform init'
  EOF

  if !force? && tf_initialized
    signal_usage_error(rebootstrap_msg)
  end

  Atmos.config.provider.auth_manager.authenticate(ENV, bootstrap: true) do |auth_env|
    begin
      exe = TerraformExecutor.new(process_env: auth_env)

      skip_backend = true
      skip_secrets = true
      if backend_initialized
        skip_backend = false
        skip_secrets = false
      end

      # Cases
      # 1) bootstrap of new account - success
      # 2) repeating bootstrap of new account due to failure partway - success
      # 3) try to rebootstrap existing account on fresh checkout - should fail trying to create resources of same name, check output for this?
      # 4) bootstrap new account with no-default secrets

      # Need to init before we can create the resources to store state in bootstrap
      exe.run("init", "-input=false", "-lock=false",
              skip_backend: true, skip_secrets: true)

      # Bootstrap to create the resources needed to store state
      exe.run("apply", "-input=false",
              skip_backend: true, skip_secrets: true)

      # Need to init to setup the backend state after we create the resources
      # to store state in bootstrap
      exe.run("init", "-input=false", "-force-copy", skip_secrets: true)

      # Might as well init the non-bootstrap case as well once the state
      # storage has been setup in bootstrap
      Atmos.config = orig_config
      exe = TerraformExecutor.new(process_env: auth_env)
      exe.run("init", "-input=false", skip_secrets: true)

    rescue TerraformExecutor::ProcessFailed => e
      logger.error(e.message)
      logger.error(rebootstrap_msg)
    end
  end
end