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.

Yields:



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

def initialize
  @config = ControllerConfig.new
  yield @config if block_given?
end

Instance Attribute Details

#configObject

rubocop:disable ClassLength



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

def config
  @config
end

Instance Method Details

#build_version(version_name) ⇒ Object



140
141
142
143
144
145
146
147
# File 'lib/moonshot/controller.rb', line 140

def build_version(version_name)
  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 AbcSize



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

def create # rubocop:disable AbcSize
  # 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.values.each 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(', ')}" # rubocop:disable LineLength
  end

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

#deleteObject



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

def 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.delete
  run_hook(:deploy, :post_delete)
  run_plugins(:post_delete)
end

#deploy_version(version_name) ⇒ Object



149
150
151
152
153
# File 'lib/moonshot/controller.rb', line 149

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

#doctorObject



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/moonshot/controller.rb', line 169

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



18
19
20
# File 'lib/moonshot/controller.rb', line 18

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

#pushObject



134
135
136
137
138
# File 'lib/moonshot/controller.rb', line 134

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

#sshObject



181
182
183
184
185
186
187
188
189
190
# File 'lib/moonshot/controller.rb', line 181

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



192
193
194
# File 'lib/moonshot/controller.rb', line 192

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

#statusObject



127
128
129
130
131
132
# File 'lib/moonshot/controller.rb', line 127

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

#updateObject

rubocop:disable AbcSize



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
121
122
123
124
125
# File 'lib/moonshot/controller.rb', line 74

def update # rubocop:disable AbcSize
  # 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.
  ParentStackParameterLoader.new(@config).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.each 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_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(', ')}" # rubocop:disable LineLength
  end

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