Class: Deployinator::Controller

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

Overview

Public: the main Controller class knows how to extract options from the options argument it gets passed from its run method and then prepares the Deploy class to be ready to deploy.

Instance Method Summary collapse

Instance Method Details

#run(options) ⇒ Object

Public: run the actual deploy from the given parameters

Params:

options - hash that includes at least the following fields:
          { :username => "name of the user that is deploying",
            :stack => "name of the stack to deploy",
            :stage => "name of the stage of the stack to deploy"
          }

Returns nothing



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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/deployinator/controller.rb', line 84

def run(options)
  options[:method] = stage_to_method(options[:stack], options[:stage])
  if options[:method].nil?
    raise "No method defined for me to call: #{options[:stack]}, #{options[:stage]}"
  end

  # config pus needs :env populated here
  options[:env] = {
    :username => options[:username]
  }

  if Deployinator.get_stacks.include?(options[:stack])
    require "stacks/#{options[:stack]}"
    klass = "#{Mustache.classify(options[:stack])}Deploy"
    deploy_class = Deployinator::Stacks.const_get("#{klass}")
  else
    raise "No such stack #{options[:stack]}"
  end

  deploy_instance = deploy_class.new(options)
  deploy_instance.register_plugins(options[:stack])

  locked = deploy_instance.lock_pushes(options[:stack], options[:username], options[:method])

  unless locked
    return deploy_instance
  end

  @start_time = Time.now
  deploy_instance.log_and_stream "Push started at #{@start_time.to_i}\n"
  deploy_instance.log_and_stream "Calling #{options[:method]}\n";
  deploy_instance.link_stack_logfile(deploy_instance.get_filename, options[:stack])

  deploy_instance.raise_event(:deploy_start)

  begin
    state = deploy_instance.send(options[:method], options)
  rescue Exception => e
    deploy_instance.log_error("There was an exception during this deploy. Aborted!", e)
    deploy_instance.raise_event(:deploy_error, {:exception => e})
  end

  if state.nil? || !state.is_a?(Hash)
    state = {}
  end
  deploy_instance.raise_event(:deploy_end, state)

  if options[:method].match(/config_push/)
    env = options[:method].match(/prod/) ? "production" : "princess"
  elsif options[:method].match(/force_builda/)
    env = "force asset rebuild"
  else
    env = options[:method][/(dev|qa|production|princess|prod|webs|stage|config)/i, 1] || "other"
    env = "production" if env.match(/prod|webs/)
  end

  # display a message that the deploy is done and call the JavaScript
  # deploy done function
  msg = "<h4>#{env.to_s.upcase} deploy in #{options[:stack]} stack complete</h4>"
  deploy_instance.log_and_stream(msg+"<p class='output'>")
  deploy_instance.log_and_stream("<script id='deploy-done'>window.deploy_done('#{msg}', '#{options[:stage]}');</script>")

  deploy_instance.unlock_pushes(options[:stack])
  deploy_instance.move_stack_logfile(options[:stack])

  return deploy_instance
end

#stage_to_method(stack, stage) ⇒ Object

Public: get the correct method name for the stage to deploy. This allows us to call things princess and prod even though the methods in the stacks have crazy names.

Params:

stack - the name of the stack to deploy
stage - the stage of the stack to deploy

Returns the name of the deploy method as a String which then can be sent to the Deploy class



70
71
72
# File 'lib/deployinator/controller.rb', line 70

def stage_to_method(stack, stage)
  "#{stack}_#{stage}"
end