Class: Scout::Command::Run

Inherits:
Scout::Command show all
Defined in:
lib/scout/command/run.rb

Constant Summary

Constants inherited from Scout::Command

HTTP_HEADERS

Constants included from HTTP

HTTP::CA_FILE, HTTP::VERIFY_MODE

Instance Attribute Summary

Attributes inherited from Scout::Command

#config_dir, #history, #hostname, #log_path, #server, #server_name

Instance Method Summary collapse

Methods inherited from Scout::Command

#create_pid_file_or_exit, dispatch, #initialize, #level, #log, program_name, #program_name, program_path, #program_path, usage, #usage, user, #user, #verbose?

Methods included from HTTP

#build_http

Constructor Details

This class inherits a constructor from Scout::Command

Instance Method Details

#runObject



6
7
8
9
10
11
12
13
14
15
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
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/scout/command/run.rb', line 6

def run
  key = @args.first
  # TODO: this is an awkward way to force creation of the config directory. Could use a little refactoring.
  configuration_directory = config_dir
  log.debug("Running Scout [#{Scout::VERSION}] with server_metrics [#{ServerMetrics::VERSION}] on #{@hostname}") if log
  log.debug("Configuration directory is #{configuration_directory} ") if log
  # TODO: too much external logic of command doing things TO server. This should be moved into the server class.
  @scout = Scout::Server.new(server, key, history, log, server_name, @http_proxy, @https_proxy, @roles, @hostname, @environment)
  @scout.load_history
  
  unless $stdin.tty?
    log.info "Sleeping #{@scout.sleep_interval} sec" if log
    sleep @scout.sleep_interval
  end

  begin
    @scout.fetch_plan
  rescue SystemExit => e
    puts "Failure. Run with '-v -ldebug' for more information" if $stdin.tty?
    raise e
  end

  # Spawn or stop streamer as needed
  if @scout.streamer_command.is_a?(String)
    if @scout.streamer_command.start_with?("start")
      log.info "streamer command: start"
      Scout::StreamerDaemon.start_daemon(history, @scout.streamer_command, @hostname, @http_proxy)
    elsif @scout.streamer_command == "stop"
      log.info "streamer command: stop"
      Scout::StreamerDaemon.stop_daemon(history)
    end
  end

  # Check in if appropriate
  if @scout.new_plan || @scout.time_to_checkin?  || @force
    if @scout.new_plan
      log.info("Now checking in with new plugin plan") if log
    elsif @scout.time_to_checkin?
      log.info("It is time to checkin") if log
    elsif @force
      log.info("overriding checkin schedule with --force and checking in now.") if log
    end

    begin
      create_pid_file_or_exit
      @scout.run_plugins_by_plan
      @scout.save_history
      puts "Successfully reported to #{server}" if $stdin.tty?
    rescue SystemExit => e
      puts "Failure. Run with '-v -ldebug' for more information" if $stdin.tty?
      raise e
    end

    begin
      # Since this is a new checkin, overwrite the existing log
      File.open(log_path, "w") do|log_file|
        log_file.puts log.messages # log.messages is an array of every message logged during this run
      end
    rescue
      log.info "Could not write to #{log_path}."
    end
  else
    log.info "Not time to checkin yet. Next checkin in #{@scout.next_checkin}. Override by passing --force to the scout command" if log
    begin
      # Since this a ping, append to the existing log
      File.open(log_path, "a") do|log_file|
        log_file.puts log.messages
      end
    rescue
      log.info "Could not write to #{log_path}."
    end
  end
end