Module: Deployinator::Helpers

Includes:
PluginHelpers, VersionHelpers
Included in:
DeleteLog, Deploy, Mustache
Defined in:
lib/deployinator/helpers.rb,
lib/deployinator/helpers/dsh.rb,
lib/deployinator/helpers/git.rb,
lib/deployinator/helpers/view.rb,
lib/deployinator/helpers/deploy.rb,
lib/deployinator/helpers/plugin.rb,
lib/deployinator/helpers/version.rb,
lib/deployinator/helpers/stack-tail.rb,
lib/deployinator/helpers/concurrency.rb

Defined Under Namespace

Modules: ConcurrencyHelpers, DeployHelpers, DshHelpers, GitHelpers, PluginHelpers, StackTailHelpers, VersionHelpers, ViewHelpers Classes: CommandContainsNewlinesError

Constant Summary collapse

RUN_LOG_PATH =
"run_logs/"

Instance Attribute Summary

Attributes included from PluginHelpers

#plugins

Instance Method Summary collapse

Methods included from VersionHelpers

curl_get_url, get_build, get_version, get_version_by_url

Methods included from PluginHelpers

#notify_plugins, #raise_event, #register_plugins

Instance Method Details

#all_eof(files) ⇒ Object


169
170
171
# File 'lib/deployinator/helpers.rb', line 169

def all_eof(files)
  files.find { |f| !f.eof }.nil?
end

#all_push_lock_infoObject


538
539
540
541
# File 'lib/deployinator/helpers.rb', line 538

def all_push_lock_info
  d = `test -f #{push_lock_all_path} && cat #{push_lock_all_path}`.chomp
  d.empty? ? nil : Hash[*[:who, :lock_time].zip(d.split("|")).flatten]
end

#all_pushes_locked?Boolean

Returns:

  • (Boolean)

529
530
531
# File 'lib/deployinator/helpers.rb', line 529

def all_pushes_locked?
    all_push_lock_info
end

#announce(announcement, options = {}) ⇒ Object


647
648
649
650
651
652
653
654
655
656
657
658
659
# File 'lib/deployinator/helpers.rb', line 647

def announce(announcement, options = {})
  raise_event(:announce, {:announcement => announcement, :options => options})

  if options[:send_email] && options[:send_email] == true
    stack = options[:stack]
    subject = options[:subject] || "#{stack} deployed #{options[:env]} by #{@username}"

    send_email({
      :subject => subject,
      :html_body => announcement
    })
  end
end

#average_duration(type, stack) ⇒ Object


707
708
709
710
711
712
713
# File 'lib/deployinator/helpers.rb', line 707

def average_duration(type, stack)
  log = `grep "#{type}|#{stack}" #{Deployinator.timing_log_path} | tac | head -5`
  timings = log.split("\n").collect { |line| line.split("|").last.to_f }
  avg_time = (timings.empty?) ? 30 : timings.inject(0) {|a,v| a+v} / timings.size
  puts "avg time for #{stack}/#{type}: #{avg_time}"
  avg_time
end

#can_remove_stack_lock?Boolean

Returns:

  • (Boolean)

624
625
626
627
628
629
630
631
632
633
634
635
636
637
# File 'lib/deployinator/helpers.rb', line 624

def can_remove_stack_lock?
  if is_admin?
    return true
  end

  # get the lock info to see if the user is the locker
  info = push_lock_info(@stack) || {}
  return true if info.empty?
  if info[:who] == @username
    return true
  end

  return false
end

#censor_cmd(cmd, secrets = []) ⇒ Object

Strips out the secrets from the command and replaces with *******


268
269
270
271
272
# File 'lib/deployinator/helpers.rb', line 268

def censor_cmd(cmd, secrets=[])
  censored = cmd.dup
  secrets.each { |secret| censored.gsub!(secret, "*******") }
  return censored
end

#check_command_safety(command, secrets = []) ⇒ Object


154
155
156
157
158
159
160
161
162
# File 'lib/deployinator/helpers.rb', line 154

def check_command_safety(command, secrets = [])
  if command.include? "\n"
    censored_cmd = censor_cmd(command, secrets)
    raise CommandContainsNewlinesError.new(command),
      "Command contains newlines and may not execute as intended: #{censored_cmd}"
  end

  command
end

#clean(msg) ⇒ Object


353
354
355
# File 'lib/deployinator/helpers.rb', line 353

def clean(msg)
  (msg || "").gsub("|", "/").gsub('"', """).gsub("'", "'")
end

#deploy_host?Boolean

Returns:

  • (Boolean)

756
757
758
# File 'lib/deployinator/helpers.rb', line 756

def deploy_host?
  ! Deployinator.deploy_host.nil?
end

#dev_context?Boolean

Returns:

  • (Boolean)

22
23
24
# File 'lib/deployinator/helpers.rb', line 22

def dev_context?
  Deployinator.app_context['context'] == 'dev'
end

#diff(r1, r2, stack = "web", time = null) ⇒ Object


303
304
305
306
307
308
309
# File 'lib/deployinator/helpers.rb', line 303

def diff(r1, r2, stack="web", time=null)
  if (!time)
    time = Time.now.to_i
  end
  redirect "/diff/#{stack}/#{r1}/#{r2}/github?time=#{time}"
  return
end

#diff_url(stack, old_build, new_build) ⇒ Object


661
662
663
# File 'lib/deployinator/helpers.rb', line 661

def diff_url(stack, old_build, new_build)
  raise_event(:diff, {:stack => stack, :old_build => old_build, :new_build => new_build})
end

#environmentsObject


372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/deployinator/helpers.rb', line 372

def environments
  custom_env = "#{stack}_environments"
  envs = send(custom_env) if respond_to?(custom_env.to_sym)
  envs ||=
  [{
    :name            => "production",
    :title           => "Deploy #{stack} production",
    :method          => "production",
    :current_version => proc{send(:"#{stack}_production_version")},
    :current_build   => proc{get_build(send(:"#{stack}_production_version"))},
    :next_build      => proc{send(:head_build)}
  }]

  # Support simplified symbol for methods
  envs.map! do |env|
    new_env = env
    new_env[:current_version] = proc{send(env[:current_version])} if env[:current_version].is_a? Symbol
    new_env[:current_build] = proc{send(env[:current_build])} if env[:current_build].is_a? Symbol
    new_env[:next_build] = proc{send(env[:next_build])} if env[:next_build].is_a? Symbol
    new_env
  end

  envs.each_with_index { |env, i| env[:number] = "%02d." % (i + 1); env[:not_last] = (i < envs.size - 1) }
end

#get_deploy_target_statusObject

Public: check if the deploy host is up or not show a little slug in the header with the deploy host name and status


748
749
750
751
752
753
754
# File 'lib/deployinator/helpers.rb', line 748

def get_deploy_target_status
  status = %x{ssh -o ConnectTimeout=5 #{Deployinator.deploy_host} uptime | awk '{print $2}' }.rstrip
  if status != 'up'
    status = "<b>DOWN!</b>"
  end
  "#{Deployinator.deploy_host} is #{status}"
end

#get_from_cache(cache_file, cache_ttl = 5) ⇒ Object

Public: gets the contents from a cache file if it hasn't expired

Paramaters:

cache_file: path to a cache file
cache_ttl : how long in seconds the cached content is good for
A negative number will indicate you don't care how old the cache
file is.

Returns: cached content or false if expired or cache file doesn't exist.


465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/deployinator/helpers.rb', line 465

def get_from_cache(cache_file, cache_ttl=5)
  if File.exists?(cache_file)
    now = Time.now
    file_mtime = File.mtime(cache_file)
    file_age = now - file_mtime
    if ((cache_ttl < 0) || (file_age <= cache_ttl))
      file = File.open(cache_file, "r:UTF-8")
      return file.read
    else
      # Return False if the cache is old
      return false
    end
  else
    # Return False if the cache file doesn't exist
    return false
  end
end

#get_logObject


315
316
317
318
319
# File 'lib/deployinator/helpers.rb', line 315

def get_log
  log_entries.collect do |line|
    "[" + line.split("|").join("] [") + "]"
  end.join("<br>")
end

#get_run_logs(opts = {}) ⇒ Object

Public: fetch the run_logs in 'run_logs/' based on sevaral parameters

Parameters:

opts
        :offset
        :limit
        :filename - <stack>-<method>

Returns an array of hashes with name and time keys


407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/deployinator/helpers.rb', line 407

def get_run_logs(opts={})
  offset = opts[:offset] || 0
  limit = opts[:limit] || -1
  filename = opts[:filename] || ""
  glob = Deployinator::Helpers::RUN_LOG_PATH + "*.html"
  files = Dir.glob(glob)

  # filter for config files
  files.select! {|file| file.match(/^((?!web[-_]config_diff.html).)*$/) && file.match(/html/)}

  # filter for princess or production run_logs
  files.select! {|file| file.match(/#{filename}/)}

  # map files to hash with name and time keys
  files.map! do |file|
    { :name => File.basename(file), :time => Time.at(file[/(\d{8,})/].to_i) }
  end

  # sort files chronologically,
  files.sort_by! {|file| file[:time]}.reverse!

  # select files within an index range
  files[offset...offset +limit]

end

#get_stack_selectObject

Public: Outputs stack data for use in templating the stack selection box in the header.

Returns an array of hashes with the fields “stack” and “current” where “current” is true for the currently selected stack.


560
561
562
563
564
565
566
567
568
# File 'lib/deployinator/helpers.rb', line 560

def get_stack_select
  stacks = Deployinator.get_visible_stacks
  output = Array.new
  stacks.each do |s|
    current = stack == s
    output << { "stack" => s, "current" => current }
  end
  output
end

#head_buildObject


760
761
762
763
764
765
766
767
768
769
770
771
772
773
# File 'lib/deployinator/helpers.rb', line 760

def head_build
  meth = "#{stack}_head_build"
  if self.respond_to?(meth)
    self.send(meth)
  else
    if git_info_for_stack.key?(stack.to_sym)
      rev = get_git_head_rev(stack)
      puts rev
      return rev
    else
      puts "ERROR: add your stack in git_info_for_stack"
    end
  end
end

#help_urlObject


38
39
40
# File 'lib/deployinator/helpers.rb', line 38

def help_url
  "#{Deployinator.help_url}"
end

#help_url?Boolean

Returns:

  • (Boolean)

34
35
36
# File 'lib/deployinator/helpers.rb', line 34

def help_url?
  ! Deployinator.help_url.nil?
end

#http_hostObject


279
280
281
# File 'lib/deployinator/helpers.rb', line 279

def http_host
  @host
end

365
366
367
368
369
370
# File 'lib/deployinator/helpers.rb', line 365

def hyperlink(msg)
  (msg || "").gsub(/([A-Z]{2,10}-[0-9]{2,})/) do |issue|
    issue_url = Deployinator.issue_tracker.call(issue)
    "<a href='#{issue_url}' target='_blank'>#{issue}</a>"
  end
end

#init(env) ⇒ Object


42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/deployinator/helpers.rb', line 42

def init(env)
  @username = 'nobody'
  @groups = ['nogroup']
  @local = false
  @host = env['HTTP_HOST']
  auth_info = raise_event(:auth, {:env => env, :request => request})
  if !auth_info.nil?
    raise "You must login." unless auth_info[:authorized]
    @username = auth_info[:username]
    @groups = auth_info[:groups]
    @host = auth_info[:host]
    @local = auth_info[:local]
  end
end

#is_admin?(user_groups = nil, admin_groups = nil) ⇒ Boolean

Public: helper method to check if the current user is part of an group. This method let's you pass in groups for explicitness and testing but will fall back on @group and Deployinator.admin_groups.

Param:

user_groups  - groups a user is part of
admin_groups - admin groups

Returns true if the current user is part of and admin group and false otherwise

Returns:

  • (Boolean)

812
813
814
815
816
817
818
# File 'lib/deployinator/helpers.rb', line 812

def is_admin?
  unless @groups.nil? then
    Deployinator.admin_groups.each { |cg| return true if @groups.include?(cg) }
  end

  return false
end

Creates a current-stackname symlink for each deploy for easier tailing

filename - String of the current run log filename stack - String containing the stack for this deploy


67
68
69
# File 'lib/deployinator/helpers.rb', line 67

def link_stack_logfile(filename, stack)
  run_cmd %Q{ln -nfs #{Deployinator.root_dir}/#{run_log_path}#{filename} #{Deployinator.root_dir}/#{run_log_path}current-#{stack}}
end

#lock_all_pushes(who) ⇒ Object


513
514
515
516
517
518
519
520
521
522
523
# File 'lib/deployinator/helpers.rb', line 513

def lock_all_pushes(who)
  log_and_stream("LOCKING ALL STACKS<br>")
  if lock_info = all_pushes_locked?
    log_and_stream("All pushes locked by #{lock_info[:who]}")
    return false
  end

  dt = Time.now.strftime("%m/%d/%Y %H:%M")
  log_string_to_file("#{who}|#{dt}", push_lock_all_path);
  return true
end

#lock_pushes(stack, who, method) ⇒ Object


497
498
499
500
501
502
503
504
505
506
507
# File 'lib/deployinator/helpers.rb', line 497

def lock_pushes(stack, who, method)
  log_and_stream("LOCKING #{stack}<br>")
  if lock_info = push_lock_info(stack)
    log_and_stream("Pushes locked by #{lock_info[:who]} - #{lock_info[:method]}<br>")
    return false
  end

  dt = Time.now.strftime("%m/%d/%Y %H:%M")
  log_string_to_file("#{who}|#{method}|#{dt}", push_lock_path(stack))
  return true
end

#log(env, who, msg, stack) ⇒ Object

Public: log a given message to the log_path file. The method calls log_string_to_file for lower level logging functionality.

env - String which represents the environment the log was produced in who - String which represents the active user msg - String representing the actual log message stack - String representing the current deploy stack

Returns the return code of log_string_to_file


330
331
332
333
334
335
336
337
338
339
# File 'lib/deployinator/helpers.rb', line 330

def log(env, who, msg, stack)
  s = stack
  string_to_log = "#{now}|#{env}|#{clean(who)}|#{clean(msg)}|#{s}|#{@filename}"

  # remove any newline characters, in case someone passed us something with
  # unintentional \n's
  string_to_log = string_to_log.lines.map(&:chomp).join('')

  log_string_to_file(string_to_log, Deployinator.log_path)
end

#log_and_shout(options = {}) ⇒ Object


665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
# File 'lib/deployinator/helpers.rb', line 665

def log_and_shout(options={})
  options[:stack] ||= @stack

  raise "Must have stack" unless options[:stack]

  options[:env] ||= "PROD"
  options[:nice_env] ||= nicify_env(options[:env])
  options[:user] ||= @username
  options[:start] = @start_time unless options[:start] || ! @start_time

  if (options[:start])
    options[:end] = Time.now.to_i unless options.key?(:end)
    options[:duration] = options[:end] - options[:start]

    log_and_stream "Ended at #{options[:end]}<br>Took: #{options[:duration]} seconds<br>"
    timing_log options[:duration], options[:nice_env], options[:stack]
  end

  if (options[:old_build] && options[:build])
    log_str = "#{options[:stack]} #{options[:nice_env]} deploy: old #{options[:old_build]}, new: #{options[:build]}"
    log options[:env], options[:user], log_str, options[:stack]
    d_url = diff_url(options[:stack], options[:old_build], options[:build])
  end

  if (options[:old_build] && options[:build] && (options[:irc_channels] || options[:send_email]))
    announcement = "#{options[:stack]} #{options[:env]} deployed by #{options[:user]}"
    announcement << " build: #{options[:build]} took: #{options[:duration]} seconds "
    announcement << "diff: #{d_url}" if d_url
    announce announcement, options
  end
end

#log_and_stream(output) ⇒ Object

output to a file, and the streaming output handler Public: helper function to write a message to the logfile and have it streamed in the webfrontend also. The frontend is HTML markuped so you can use HTML in the log message and it will be rendered with the given CSS of the site. Some classes can be used per default in Deployinator to show the output also in an error or info notification box. These are then displayed in a box above the logging output.

output - String to be logged and shown in the output

Examples:

log_and_stream(<div class="stderror"> ERROR! </div>)
log_and_stream(<div class="info_msg"> INFO! </div>)

Returns nothing


94
95
96
97
98
# File 'lib/deployinator/helpers.rb', line 94

def log_and_stream(output)
  write_file output, runlog_filename if runlog_filename
  return @block.call(output) unless @block.nil?
  ""
end

#log_and_stream_error(erroutput, log_errors = true) ⇒ Object


173
174
175
176
177
178
179
# File 'lib/deployinator/helpers.rb', line 173

def log_and_stream_error(erroutput, log_errors = true)
  if (log_errors) then
    log_and_stream("<span class='stderr'>STDERR: #{CGI::escapeHTML(erroutput)}</span><br>")
  else
    log_and_stream("STDERR:#{CGI::escapeHTML(erroutput)}<br>")
  end
end

#log_entries(options = {}) ⇒ Object


715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
# File 'lib/deployinator/helpers.rb', line 715

def log_entries(options = {})
  stacks = []
  stacks << "LOG MESSAGE" unless (options[:no_limit] || options[:no_global])

  stacks << options[:stack] if options[:stack]

  env = options[:env] ? "\\|#{options[:env].upcase}\\|" : ""
  limit = (options[:no_limit] && options[:no_limit] == true) ? nil : (options[:limit] || 40)

  # stack should be the last part of the log line from the last pipe to the end
  # modified this to take into account the run_log entry at the end
  unless stacks.empty? && env.empty?
    grep = "| egrep '#{env}.*\\|\(#{stacks.join("|")}\)\\|'"
  end

  # extra grep does another filter to the line, needed to get CONFIG PRODUCTION
  if defined? options[:extragrep]
    extragrep = "| egrep -i '#{options[:extragrep]}' "
  end

  if options[:page]
    num_per_page = 40
    limit = "| head -#{num_per_page * options[:page].to_i} | tail -#{num_per_page}"
  else
    limit = "| head -#{limit}" if limit
  end

  log = `tac #{Deployinator.log_path} #{grep} #{extragrep} #{limit}`
  log.split("\n")
end

#log_error(msg, e = nil) ⇒ Object


775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
# File 'lib/deployinator/helpers.rb', line 775

def log_error(msg, e = nil)
  log_msg = e.nil? ? msg : "#{msg} (#{e.message})"
  log_and_stream "<div class=\"stderr\">#{log_msg}</div>"
  if !e.nil?
    begin
      template = open("#{File.dirname(__FILE__)}/templates/exception.mustache").read

      regex = /(?<file>.*?):(?<line>\d+):.*?`(?<method>.*)'/
      context = e.backtrace.map do |line|
        match = regex.match(line)
        {
          :file => match['file'],
          :line => match['line'],
          :method => match['method']
        }
      end

      output = Mustache.render(template, {:exceptions => context})
      log_and_stream output
    rescue
      log_and_stream e.backtrace.inspect
    end
  end
  # This is so we have something in the log if/when this fails
  puts log_msg
end

#log_string_to_file(string, path) ⇒ Object

Public: wrapper method around appending stdout to a logfile.

string - String representing the log message path - String representing the path to the logfile

Returns true if echo exited with 0, false for non-zero exit and nil if the call fails


348
349
350
351
# File 'lib/deployinator/helpers.rb', line 348

def log_string_to_file(string, path)
  cmd = %Q{echo  "#{string}" >> #{path}}
  system(cmd)
end

#move_stack_logfile(stack) ⇒ Object

Moves current-stackname symlink so tailer won't accidentally pick up on last push race condition

stack - String containing the stack for this deploy


75
76
77
# File 'lib/deployinator/helpers.rb', line 75

def move_stack_logfile(stack)
  run_cmd %Q{mv #{Deployinator.root_dir}/#{run_log_path}current-#{stack} #{Deployinator.root_dir}/#{run_log_path}last-#{stack}}
end

#nice_timeObject


357
358
359
# File 'lib/deployinator/helpers.rb', line 357

def nice_time
  "%Y-%m-%d %H:%M:%S"
end

#nicify_env(env) ⇒ Object


274
275
276
277
# File 'lib/deployinator/helpers.rb', line 274

def nicify_env(env)
  env = "production" if env == "PROD"
  env.downcase
end

#not_dev?Boolean

Returns:

  • (Boolean)

26
27
28
# File 'lib/deployinator/helpers.rb', line 26

def not_dev?
  Deployinator.app_context['context'] != 'dev'
end

#nowObject


361
362
363
# File 'lib/deployinator/helpers.rb', line 361

def now
  Time.now.gmtime.strftime(nice_time)
end

#push_lock_all_pathObject


551
552
553
# File 'lib/deployinator/helpers.rb', line 551

def push_lock_all_path
  "#{Deployinator.root(["log"])}/all-push-lock"
end

#push_lock_info(stack) ⇒ Object


533
534
535
536
# File 'lib/deployinator/helpers.rb', line 533

def push_lock_info(stack)
  d = `test -f #{push_lock_path(stack)} && cat #{push_lock_path(stack)}`.chomp
  d.empty? ? nil : Hash[*[:who, :method, :lock_time].zip(d.split("|")).flatten]
end

#push_lock_path(stack) ⇒ Object


547
548
549
# File 'lib/deployinator/helpers.rb', line 547

def push_lock_path(stack)
  "#{Deployinator.root(["log"])}/#{stack}-push-lock"
end

#pushes_locked?(stack) ⇒ Boolean

Returns:

  • (Boolean)

543
544
545
# File 'lib/deployinator/helpers.rb', line 543

def pushes_locked?(stack)
  all_pushes_locked? || push_lock_info(stack)
end

#run_cmd(cmd, timing_metric = nil, log_errors = true, secrets = []) ⇒ Object

Run external command with timing information streams and logs the output of the command as well does not (currently) check exit status codes


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/deployinator/helpers.rb', line 184

def run_cmd(cmd, timing_metric=nil, log_errors=true, secrets=[])
  ret = ""
  error_message = ""
  exit_code = 0
  start = Time.now.to_i
  timestamp = Time.now.to_s
  censored_cmd = censor_cmd(cmd, secrets)
  plugin_state = {
    :cmd            => censored_cmd,
    :timing_metric  => timing_metric,
    :start_time     => start,
    :log_errors     => log_errors,
    :filename       => @filename,
  }
  raise_event(:run_command_start, plugin_state)
  log_and_stream "<div class='command'><h4>#{timestamp}: Running #{censored_cmd}</h4>\n<p class='output'>"
  time = Benchmark.measure do
    Open3.popen3(cmd) do |inn, out, err, wait_thr|
      output = ""
      erroutput = ""
      waserror = false
      inn.close_write
      begin
        files = [out, err]
        until files.empty? do
          ready = IO.select(files)
          if ready
            readable = ready[0]
            readable.each do |f|
              fileno = f.fileno
              begin
                chr = f.read_nonblock(1)
                if fileno == out.fileno
                  output << chr
                  ret << chr
                  if chr == "\n" || chr == "\r"
                    log_and_stream CGI::escapeHTML(output) + "<br>"
                    output = ""
                  end
                elsif fileno == err.fileno
                  waserror = true
                  erroutput <<  chr
                  error_message << chr
                  if chr == "\n" || chr == "\r"
                    log_and_stream_error(erroutput, log_errors)
                    erroutput = ""
                  end
                else
                  raise "unknown fileno, was expecting either standardout(#{out.fileno}, or standarderror(#{err.fileno} but got #{fileno}"
                end
              rescue EOFError => e
                # not an error - we just reached the end of file
                files.delete f # closes it it looks like
              end
            end # readable.each
          end #if ready
        end #until files
      rescue IOError => e
        puts "IOError: #{e}"
      end # begin

      log_and_stream(CGI::escapeHTML(output)) unless output.empty?
      log_and_stream_error(erroutput, log_errors) unless erroutput.empty?
      if waserror
          plugin_state[:error_message] = error_message
          raise_event(:run_command_error, plugin_state)
      end
      # Log non-zero exits
      if wait_thr.value.exitstatus != 0 then
        log_and_stream("<span class='stderr'>DANGER! #{censored_cmd} had an exit value of: #{wait_thr.value.exitstatus}</span><br>")
        exit_code = wait_thr.value.exitstatus
      end
    end
  end
  log_and_stream "</p>"
  log_and_stream "<h5>Time: #{time}</h5></div>"
  plugin_state[:exit_code] = exit_code
  plugin_state[:stdout] = ret
  plugin_state[:time] = time.real
  raise_event(:run_command_end, plugin_state)
  return { :stdout => ret, :exit_code => exit_code }
end

#run_cmd_safely(command, timing_metric = nil, log_errors = true, secrets = []) ⇒ Object


164
165
166
167
# File 'lib/deployinator/helpers.rb', line 164

def run_cmd_safely(command, timing_metric = nil, log_errors = true, secrets=[])
  command = check_command_safety(command, secrets = [])
  run_cmd(command, timing_metric, log_errors, secrets)
end

#run_cmd_with_retries(cmd, num_retries = 5, sleep_seconds = 2, timing_metric = nil, secrets = []) ⇒ Object

Run external command with timing information streams and logs the output of the command as well If the command fails, it is retried some number of times This defaults to 5, but can be specified with the num_retries parameter If all the retries fail, an exception is thrown Between retries it will sleep for a given period, defaulting to 2 seconds


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/deployinator/helpers.rb', line 132

def run_cmd_with_retries(cmd, num_retries=5, sleep_seconds=2, timing_metric=nil, secrets=[])
  censored_cmd = censor_cmd(cmd, secrets)
  for i in 1..num_retries
    if i == num_retries then
      result = run_cmd(cmd, timing_metric, true, secrets)
    else
      result = run_cmd(cmd, timing_metric, false, secrets)
    end
    if result[:exit_code] == 0
      return result
    else
      retries_remaining = num_retries - i
      unless i == num_retries
        log_and_stream("`#{censored_cmd}` failed, will retry #{retries_remaining} more times<br>")
        sleep sleep_seconds
      end
    end
  end

  raise "Unable to execute `#{censored_cmd}` after retrying #{num_retries} times"
end

#run_log_pathObject


30
31
32
# File 'lib/deployinator/helpers.rb', line 30

def run_log_path
  RUN_LOG_PATH
end

#run_log_url(filename) ⇒ Object

Public: given a run logs filename, return a full URL to the runlg

Params:

filename - string of the filename

Returns a string URL where that runlog can be viewed


576
577
578
# File 'lib/deployinator/helpers.rb', line 576

def run_log_url(filename)
  "http://#{Deployinator.hostname}/run_logs/view/#{filename}"
end

#runlog_filename(name = nil) ⇒ Object

gives the filename to send runlog to based on whether we are in the main thread or not We do this because we want to be able to use log_and_stream seamlessly in a parallel thread. So, all log_and_stream calls in all but the main thread will log to a seaparate file output - String filename to log to


105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/deployinator/helpers.rb', line 105

def runlog_filename(name=nil)
  if @filename
    if Thread.main == Thread.current
      @filename
    elsif Thread.current[:logfile_name]
      Thread.current[:logfile_name]
    elsif name
      Thread.current[:logfile_name] = runlog_thread_filename(name)
      Thread.current[:logfile_name]
    else
      raise 'Logfile name not defined in thread. Expecting name parameter to be passed in.'
    end
  end
end

#runlog_thread_filename(name) ⇒ Object

gives us the filename to log to in thread output - String filename for thread


122
123
124
# File 'lib/deployinator/helpers.rb', line 122

def runlog_thread_filename(name)
  @filename + '-' + name.to_s
end

#send_email(options) ⇒ Object


311
312
313
# File 'lib/deployinator/helpers.rb', line 311

def send_email(options)
  Pony.mail(options)
end

#stackObject


283
284
285
# File 'lib/deployinator/helpers.rb', line 283

def stack
  @stack
end

#strip_ws_to_nil(s) ⇒ Object

Public: strips all of the whitespace from a string. If the string only whitespace, return nil.

s - the string to strip whitespace from

Example

if strip_ws_to_nil(hostname).nil?
  puts "blank hostname is not valid!"
end

Returns A whitespace-free string or nil.


443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/deployinator/helpers.rb', line 443

def strip_ws_to_nil(s)
  if s.nil?
    nil
  else
    s = s.gsub(/\s+/, "")
    if s == ''
      nil
    else
      s
    end
  end
end

#timing_log(duration, type, stack, timestamp = nil) ⇒ Object


697
698
699
700
701
702
703
704
705
# File 'lib/deployinator/helpers.rb', line 697

def timing_log(duration, type, stack, timestamp=nil)
  if (timestamp == nil) then
    timestamp = Time.now.to_i
  end

  current = now()
  log_string_to_file("#{current}|#{type}|#{stack}|#{duration}", Deployinator.timing_log_path)
  raise_event(:timing_log, {:duration => duration, :current => current, :type => type, :timestamp => timestamp})
end

#unlock_all_pushesObject


525
526
527
# File 'lib/deployinator/helpers.rb', line 525

def unlock_all_pushes
    system(%Q{rm #{push_lock_all_path}});
end

#unlock_pushes(stack) ⇒ Object


509
510
511
# File 'lib/deployinator/helpers.rb', line 509

def unlock_pushes(stack)
  system(%Q{rm #{push_lock_path(stack)}})
end

#with_timeout(seconds, description = nil, throw_exception = false, quiet = false, extra_opts = {}, &block) ⇒ Object

Public: wrap a block into a timeout

seconds - timeout in seconds description - optional description for logging (default:“”) throw_exception - options param to throw exception back up stack quiet - optional boolean for logging as a big red warning using the stderr div class extra_opts - optional hash to pass along to plugins &block - block to call

Example

with_timeout(20){system("curl -s http://google.com")}
with_timeout 30 do; system("curl -s http://google.com"); end

Returns nothing


594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
# File 'lib/deployinator/helpers.rb', line 594

def with_timeout(seconds, description=nil, throw_exception=false, quiet=false, extra_opts={}, &block)
  begin
    Timeout.timeout(seconds) do
      yield
    end
  rescue Timeout::Error => e
    info = "#{Time.now}: Timeout: #{e}"
    info += " for #{description}" unless description.nil?
    # log and stream if log filename is not undefined
    if (/undefined/ =~ @filename).nil?
      if quiet
        log_and_stream "#{info}<br>"
      else
        log_and_stream "<div class=\"stderr\">#{info}</div>"
      end
    end
    state = {
      :seconds => seconds,
      :info => info,
      :stack => stack,
      :extra_opts => extra_opts
    }
    raise_event(:timeout, state)
    if throw_exception
      raise e
    end
    ""
  end
end

#write_file(str, file) ⇒ Object


57
58
59
60
61
# File 'lib/deployinator/helpers.rb', line 57

def write_file(str, file)
  File.open("#{RUN_LOG_PATH}#{file}", "a:UTF-8") do |f|
    f.print str.force_encoding("UTF-8")
  end
end

#write_to_cache(cache_file, contents) ⇒ Object

Public: writes the supplied contents to the cache file, ensuring that encoding is correct

Parameters:

cache_file: path to the cache file
content: the data to write to the file

Returns nothing


491
492
493
494
495
# File 'lib/deployinator/helpers.rb', line 491

def write_to_cache(cache_file, contents)
  File.open(cache_file, 'w:UTF-8') do |f|
    f.write(contents.force_encoding('UTF-8'))
  end
end