Class: Moonshot::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/moonshot/controller.rb

Overview

The Controller coordinates and performs all Moonshot actions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Controller

Returns a new instance of Controller.



8
9
10
# File 'lib/moonshot/controller.rb', line 8

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject

rubocop:disable Metrics/ClassLength



6
7
8
# File 'lib/moonshot/controller.rb', line 6

def config
  @config
end

Instance Method Details

#build_version(version_name) ⇒ Object



136
137
138
139
140
141
142
143
144
# File 'lib/moonshot/controller.rb', line 136

def build_version(version_name)
  run_plugins(:setup_build)
  run_plugins(:pre_build)
  run_hook(:build, :pre_build, version_name)
  run_hook(:build, :build, version_name)
  run_hook(:build, :post_build, version_name)
  run_plugins(:post_build)
  run_hook(:repo, :store, @config.build_mechanism, version_name)
end

#createObject

rubocop:disable Metrics/AbcSize



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
# File 'lib/moonshot/controller.rb', line 16

def create # rubocop:disable Metrics/AbcSize
  run_plugins(:setup_create)
  # Scan the template for all required parameters and configure
  # the ParameterCollection.
  @config.parameters = ParameterCollection.from_template(stack.template)

  # Import all Outputs from parent stacks as Parameters on this
  # stack.
  ParentStackParameterLoader.new(@config).load!

  # If there is an answer file, use it to populate parameters.
  if @config.answer_file
    YAML.load_file(@config.answer_file).each do |key, value|
      @config.parameters[key] = value
    end
  end

  # Apply any overrides configured, such as from the CLI -p option.
  @config.parameter_overrides.each do |key, value|
    @config.parameters[key] = value
  end

  # Interview the user for missing parameters, using the
  # appropriate prompts.
  @config.parameters.hash.each_value do |sp|
    next if sp.set?

    parameter_source = @config.parameter_sources.fetch(sp.name,
                                                       @config.default_parameter_source)
    parameter_source.get(sp)
  end

  # Plugins get the final say on parameters before create,
  # allowing them to manipulate user supplied input and answers
  # file content.
  run_plugins(:pre_create)

  # Fail if any parameters are still missing without defaults.
  missing_parameters = @config.parameters.missing_for_create
  unless missing_parameters.empty?
    raise "The following parameters were not provided: #{missing_parameters.map(&:name).join(', ')}"
  end

  run_hook(:deploy, :pre_create)
  stack_ok = stack.create
  if stack_ok # rubocop:disable Style/GuardClause
    run_hook(:deploy, :post_create)
    run_plugins(:post_create)
  else
    raise 'Stack creation failed!'
  end
end

#deleteObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/moonshot/controller.rb', line 153

def delete
  run_plugins(:setup_delete)
  # Populate the current values of parameters, for use by plugins.
  @config.parameters = ParameterCollection.from_template(stack.template)
  stack.parameters.each do |key, value|
    @config.parameters[key].use_previous!(value) if @config.parameters.key?(key)
  end

  run_plugins(:pre_delete)
  run_hook(:deploy, :pre_delete)
  stack_ok = stack.delete
  if stack_ok # rubocop:disable Style/GuardClause
    run_hook(:deploy, :post_delete)
    run_plugins(:post_delete)
  else
    raise 'Stack deletion failed!'
  end
end

#deploy_version(version_name) ⇒ Object



146
147
148
149
150
151
# File 'lib/moonshot/controller.rb', line 146

def deploy_version(version_name)
  run_plugins(:setup_deploy)
  run_plugins(:pre_deploy)
  run_hook(:deploy, :deploy, @config.artifact_repository, version_name)
  run_plugins(:post_deploy)
end

#doctorObject



172
173
174
175
176
177
178
179
180
181
182
# File 'lib/moonshot/controller.rb', line 172

def doctor
  success = true
  success &&= stack.doctor_hook
  success &&= run_hook(:build, :doctor)
  success &&= run_hook(:repo, :doctor)
  success &&= run_hook(:deploy, :doctor)
  results = run_plugins(:doctor)

  success = false if results.value?(false)
  success
end

#listObject



12
13
14
# File 'lib/moonshot/controller.rb', line 12

def list
  Moonshot::StackLister.new(@config.app_name).list
end

#pushObject



130
131
132
133
134
# File 'lib/moonshot/controller.rb', line 130

def push
  version = @config.dev_build_name_proc.call(@config)
  build_version(version)
  deploy_version(version)
end

#sshObject



184
185
186
187
188
189
190
191
192
193
194
# File 'lib/moonshot/controller.rb', line 184

def ssh
  run_plugins(:pre_ssh)
  @config.ssh_instance ||= SSHTargetSelector.new(
    stack, asg_name: @config.ssh_auto_scaling_group_name
  ).choose!
  cb = SSHCommandBuilder.new(@config.ssh_config, @config.ssh_instance)
  result = cb.build(@config.ssh_command)

  warn "Opening SSH connection to #{@config.ssh_instance} (#{result.ip})..."
  exec(result.cmd)
end

#stackObject



196
197
198
# File 'lib/moonshot/controller.rb', line 196

def stack
  @stack ||= Stack.new(@config)
end

#statusObject



122
123
124
125
126
127
128
# File 'lib/moonshot/controller.rb', line 122

def status
  run_plugins(:setup_status)
  run_plugins(:pre_status)
  run_hook(:deploy, :status)
  stack.status
  run_plugins(:post_status)
end

#update(dry_run:, force:, refresh_parameters:) ⇒ Object

rubocop:disable Metrics/AbcSize



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/moonshot/controller.rb', line 69

def update(dry_run:, force:, refresh_parameters:) # rubocop:disable Metrics/AbcSize
  run_plugins(:setup_update)
  # Scan the template for all required parameters and configure
  # the ParameterCollection.
  @config.parameters = ParameterCollection.from_template(stack.template)

  # Set all values already provided by the stack to UsePreviousValue.
  stack.parameters.each do |key, value|
    @config.parameters[key].use_previous!(value) if @config.parameters.key?(key)
  end

  # Import all Outputs from parent stacks as Parameters on this
  # stack.
  parent_stack_params = ParentStackParameterLoader.new(@config)
  refresh_parameters ? parent_stack_params.load! : parent_stack_params.load_missing_only!

  # If there is an answer file, use it to populate parameters.
  if @config.answer_file
    YAML.load_file(@config.answer_file).each do |key, value|
      @config.parameters[key] = value
    end
  end

  # Apply any overrides configured, such as from the CLI -p option.
  @config.parameter_overrides.each do |key, value|
    @config.parameters[key] = value
  end

  # Interview the user for missing parameters, using the
  # appropriate prompts.
  @config.parameters.values.reject(&:set?).each do |sp|
    parameter_source = @config.parameter_sources.fetch(sp.name,
                                                       @config.default_parameter_source)
    parameter_source.get(sp)
  end

  # Plugins get the final say on parameters before create,
  # allowing them to manipulate user supplied input and answers
  # file content.
  run_plugins(:pre_update)

  # Fail if any parameters are still missing without defaults.
  missing_parameters = @config.parameters.missing_for_update
  unless missing_parameters.empty?
    raise "The following parameters were not provided: #{missing_parameters.map(&:name).join(', ')}"
  end

  run_hook(:deploy, :pre_update)
  stack.update(dry_run:, force:)
  run_hook(:deploy, :post_update)
  run_plugins(:post_update)
end