Class: RakeCloudspin::Tasks::StackTask

Inherits:
BaseTask show all
Defined in:
lib/rake_cloudspin/tasks/stack_task.rb

Instance Method Summary collapse

Methods inherited from BaseTask

#role_user_variable_name, #spin_user_variables, #stack_config

Methods inherited from RakeCloudspin::TaskLib

#check_required, check_required_for, #initialize, parameter, parameter_definitions, #process_arguments, #process_block, #setup_defaults, setup_defaults_for

Constructor Details

This class inherits a constructor from RakeCloudspin::TaskLib

Instance Method Details

#backend_config_builderObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rake_cloudspin/tasks/stack_task.rb', line 82

def backend_config_builder
  lambda do |args|
    {
      'region' => stack_config.region,
      'bucket' => Statebucket.build_bucket_name(
        estate: stack_config(args).estate, 
        deployment_identifier: stack_config(args).deployment_identifier,
        component: stack_config(args).component
      ),
      'key' => state_key(args),
      'encrypt' => true,
      'profile' => stack_config(args).aws_profile,
      'role_arn' => stack_config(args).spin_stack_manager_role_arn
    }
  end
end

#defineObject



6
7
8
9
# File 'lib/rake_cloudspin/tasks/stack_task.rb', line 6

def define
  define_terraform_tasks
  define_vars_task
end

#define_terraform_tasksObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rake_cloudspin/tasks/stack_task.rb', line 11

def define_terraform_tasks
  RakeTerraform.define_command_tasks do |t, args|
    t.configuration_name = "#{stack_type}-#{stack_name}"
    t.source_directory = "#{stack_type}/#{stack_name}/infra"
    t.work_directory = 'work'
    t.vars = terraform_vars_builder
    if uses_local_state?
      t.state_file = local_state_path_builder
    elsif uses_remote_state?
      t.backend_config = backend_config_builder
    else
      raise "ERROR: state_type '#{state_type}' not supported"
    end
  end
end

#define_vars_taskObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rake_cloudspin/tasks/stack_task.rb', line 27

def define_vars_task
  desc "Show terraform variables for stack '#{stack_name}'"
  task :vars do |t, args|
    puts "Terraform variables for stack '#{stack_name}'"
    puts "---------------------------------------"
    puts "#{terraform_vars_builder.call(args).to_yaml}"
    puts "---------------------------------------"
    if uses_local_state?
      puts "Local statefile path:"
      puts "---------------------------------------"
      puts "#{local_state_path_builder.call(args)}"
    elsif uses_remote_state?
      puts "Backend configuration for stack '#{stack_name}':"
      puts "---------------------------------------"
      puts "#{backend_config_builder.call(args).to_yaml}"
    else
      puts "Unknown state configuration type ('#{state_type}')"
    end
    puts "---------------------------------------"
  end
end

#local_state_path_builderObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/rake_cloudspin/tasks/stack_task.rb', line 71

def local_state_path_builder
  lambda do |args|
    Paths.from_project_root_directory(
        'state',
        stack_config(args).deployment_identifier || 'delivery',
        stack_config(args).component,
        stack_type,
        "#{stack_name}.tfstate")
    end
end

#state_configurationObject



61
62
63
# File 'lib/rake_cloudspin/tasks/stack_task.rb', line 61

def state_configuration
  stack_config.state
end

#state_key(args) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/rake_cloudspin/tasks/stack_task.rb', line 99

def state_key(args)
  [
    stack_config(args).deployment_identifier || 'delivery',
    stack_config(args).component,
    stack_type,
    "#{stack_name}.tfstate"
  ].join('/')
end

#terraform_vars_builderObject



65
66
67
68
69
# File 'lib/rake_cloudspin/tasks/stack_task.rb', line 65

def terraform_vars_builder
  lambda do |args|
    stack_config(args).vars.merge(spin_user_variables(args))
  end
end

#uses_local_state?Boolean

Returns:

  • (Boolean)


49
50
51
52
53
# File 'lib/rake_cloudspin/tasks/stack_task.rb', line 49

def uses_local_state?
  state_configuration.nil? ||
    state_configuration['type'].to_s.empty? || 
    state_configuration['type'] == 'local'
end

#uses_remote_state?Boolean

Returns:

  • (Boolean)


55
56
57
58
59
# File 'lib/rake_cloudspin/tasks/stack_task.rb', line 55

def uses_remote_state?
  ! state_configuration.nil? &&
    ! state_configuration['type'].to_s.empty? &&
    state_configuration['type'] == 's3'
end