Class: Cerberus::BuildCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/cerberus/manager.rb

Constant Summary collapse

DEFAULT_CONFIG =
{:scm => {:type => 'svn'}, 
  :log => {:enable => true},
  :at_time => '* *',
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(application_name, cli_options = {}) ⇒ BuildCommand

Returns a new instance of BuildCommand.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/cerberus/manager.rb', line 89

def initialize(application_name, cli_options = {})
  unless File.exists?("#{HOME}/config/#{application_name}.yml")
    say "Project '#{application_name}' does not exist in Cerberus. Type 'cerberus list' to see the list of all active projects."
  end
  
  app_root = "#{HOME}/work/#{application_name}"
  
  def_options = {:application_root => app_root + '/sources', :application_name => application_name} #pseudo options that stored in config. Could not be set in any config file not through CLI
  @config = Config.new(application_name, cli_options.merge(def_options))
  @config.merge!(DEFAULT_CONFIG, false)
  
  @status = Status.read("#{app_root}/status.log")
  
  scm_type = @config[:scm, :type]
  @scm = SCM.get(scm_type).new(@config[:application_root], @config)
  say "Client for SCM '#{scm_type}' does not installed" unless @scm.installed?
  
  builder_type = get_configuration_option(@config[:builder], :type, :rake)
  @builder = Builder.get(builder_type).new(@config)
end

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



82
83
84
# File 'lib/cerberus/manager.rb', line 82

def builder
  @builder
end

#scmObject (readonly)

Returns the value of attribute scm.



82
83
84
# File 'lib/cerberus/manager.rb', line 82

def scm
  @scm
end

#setup_script_outputObject (readonly)

Returns the value of attribute setup_script_output.



82
83
84
# File 'lib/cerberus/manager.rb', line 82

def setup_script_output
  @setup_script_output
end

#statusObject (readonly)

Returns the value of attribute status.



82
83
84
# File 'lib/cerberus/manager.rb', line 82

def status
  @status
end

#successObject (readonly)

Returns the value of attribute success.



82
83
84
# File 'lib/cerberus/manager.rb', line 82

def success
  @success
end

Instance Method Details

#runObject



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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/cerberus/manager.rb', line 110

def run
  begin
    Latch.lock("#{HOME}/work/#{@config[:application_name]}/.lock", :lock_ttl => 2 * LOCK_WAIT) do
      @scm.update!
      
      if @scm.has_changes? or @config[:force] or @status.previous_build_successful.nil?
        Dir.chdir File.join(@config[:application_root], @config[:build_dir] || '')
        @setup_script_output = `#{@config[:setup_script]}` if @config[:setup_script]

        build_successful = @builder.run
        @status.keep(build_successful, @scm.current_revision, @builder.brokeness)
        
        #Save logs to directory
        if @config[:log, :enable]
          log_dir = "#{HOME}/work/#{@config[:application_name]}/logs/"
          FileUtils.mkpath(log_dir)
          
          time = Time.now.strftime("%Y%m%d%H%M%S")
          file_name = "#{log_dir}/#{time}-#{@status.current_state.to_s}.log"
          body = [ @setup_script_output, scm.last_commit_message, builder.output ].join("\n\n")
          IO.write(file_name, body)
        end
        
        #send notifications
        active_publishers = get_configuration_option(@config[:publisher], :active, 'mail')
        active_publishers.split(/\W+/).each do |pub|
          
          publisher_config = @config[:publisher, pub]
          raise "Publisher have no configuration: #{pub}" unless publisher_config
          
          events = interpret_state(publisher_config[:on_event] || @config[:publisher, :on_event] || 'default')
          Publisher.get(pub, publisher_config).publish(@status, self, @config) if events.include?(@status.current_state)
        end
        
        #Process hooks
        hooks = @config[:hook]
        hooks.each_pair{|name, hook|
          events = interpret_state(hook[:on_event] || 'all', false)
          if events.include?(@status.current_state)
            `#{hook[:action]}`
          end
        } if hooks
      end
      
    end #lock
  rescue Exception => e
    if ENV['CERBERUS_ENV'] == 'TEST'
      raise e
    else
      File.open("#{HOME}/error.log", File::WRONLY|File::APPEND|File::CREAT) do |f| 
        f.puts Time.now.strftime("%a, %d %b %Y %H:%M:%S [#{@config[:application_name]}] --  #{e.class}")
        f.puts e.message unless e.message.empty?
        f.puts e.backtrace.collect{|line| ' '*5 + line}
        f.puts "\n"
      end
    end
  end
end

#run_time?(time) ⇒ Boolean

Returns:

  • (Boolean)


169
170
171
172
173
174
175
176
# File 'lib/cerberus/manager.rb', line 169

def run_time?(time)
  minute, hour = @config[:at_time].split
  say "Run time is configured wrong." if minute.nil? or hour.nil?
  if hour.cron_match?(time.hour)
    return minute.cron_match?(time.min)
  end
  return false
end