Class: Scout::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/es-scout/command.rb,
lib/es-scout/command/run.rb,
lib/es-scout/command/test.rb,
lib/es-scout/command/install.rb,
lib/es-scout/command/troubleshoot.rb

Direct Known Subclasses

Install, Run, Test, Troubleshoot

Defined Under Namespace

Classes: APITimeoutError, Install, Run, Test, Troubleshoot

Constant Summary collapse

HTTP_HEADERS =
{ "Client-Version"  => Scout::VERSION,
"Client-Hostname" => Socket.gethostname}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, args) ⇒ Command

Returns a new instance of Command.



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/es-scout/command.rb', line 140

def initialize(options, args)
  @server  = options[:server]  || "https://scoutapp.com/"
  @history = options[:history] ||
             File.join( File.join( (File.expand_path("~") rescue "/"),
                                   ".scout" ),
                        "client_history.yaml" )
  @verbose = options[:verbose] || false
  @level   = options[:level]   || "info"
  @force   = options[:force]   || false

  @args    = args

  # create config dir if necessary
  @config_dir = File.dirname(history)
  FileUtils.mkdir_p(@config_dir) # ensure dir exists

  @log_path = File.join(@config_dir, "latest_run.log")

end

Instance Attribute Details

#config_dirObject (readonly)

Returns the value of attribute config_dir.



160
161
162
# File 'lib/es-scout/command.rb', line 160

def config_dir
  @config_dir
end

#historyObject (readonly)

Returns the value of attribute history.



160
161
162
# File 'lib/es-scout/command.rb', line 160

def history
  @history
end

#log_pathObject (readonly)

Returns the value of attribute log_path.



160
161
162
# File 'lib/es-scout/command.rb', line 160

def log_path
  @log_path
end

#serverObject (readonly)

Returns the value of attribute server.



160
161
162
# File 'lib/es-scout/command.rb', line 160

def server
  @server
end

Class Method Details

.dispatch(argv) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/es-scout/command.rb', line 124

def self.dispatch(argv)
  # capture help command
  argv.push("--help") if argv.first == 'help'
  options = parse_options(argv)
  command = if name_or_key = argv.shift
              if cls = (Scout::Command.const_get(name_or_key.capitalize) rescue nil)
                cls.new(options, argv)
              else
                Run.new(options, [name_or_key] + argv)
              end
            else
              Install.new(options, argv)
            end
  command.run
end

.program_nameObject



12
13
14
# File 'lib/es-scout/command.rb', line 12

def self.program_name
  @program_name ||= File.basename($PROGRAM_NAME)
end

.program_pathObject



16
17
18
# File 'lib/es-scout/command.rb', line 16

def self.program_path
  @program_path ||= File.expand_path($PROGRAM_NAME)
end

.usageObject



20
21
22
# File 'lib/es-scout/command.rb', line 20

def self.usage
  @usage
end

.userObject



8
9
10
# File 'lib/es-scout/command.rb', line 8

def self.user
  @user ||= ENV["USER"] || ENV["USERNAME"] || "root"
end

Instance Method Details

#create_pid_file_or_exitObject



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
# File 'lib/es-scout/command.rb', line 202

def create_pid_file_or_exit
  pid_file = File.join(config_dir, "scout_client_pid.txt")
  begin
    File.open(pid_file, File::CREAT|File::EXCL|File::WRONLY) do |pid|
      pid.puts $$
    end
    at_exit do
      begin
        File.unlink(pid_file)
      rescue
        log.error "Unable to unlink pid file:  #{$!.message}" if log
      end
    end
  rescue
    pid     = File.read(pid_file).strip.to_i rescue "unknown"
    running = true
    begin
      Process.kill(0, pid)
      if stat = File.stat(pid_file)
        if mtime = stat.mtime
          if Time.now - mtime > 25 * 60  # assume process is hung after 25m
            log.info "Trying to KILL an old process..." if log
            Process.kill("KILL", pid)
            running = false
          end
        end
      end
    rescue Errno::ESRCH
      running = false
    rescue
      # do nothing, we didn't have permission to check the running process
    end
    if running
      if pid == "unknown"
        log.warn "Could not create or read PID file.  "                +
                 "You may need to specify the path to the config directory.  " +
                 "See:  http://scoutapp.com/help#data_file" if log
      else
        log.warn "Process #{pid} was already running" if log
      end
      exit
    else
      log.info "Stale PID file found.  Clearing it and reloading..." if log
      File.unlink(pid_file) rescue nil
      retry
    end
  end

  self
end

#levelObject



182
183
184
# File 'lib/es-scout/command.rb', line 182

def level
  Logger.const_get(@level.upcase) rescue Logger::INFO
end

#logObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/es-scout/command.rb', line 167

def log
  return @log if defined? @log
  @log = if verbose?
           log                 = ScoutLogger.new($stdout)
           log.datetime_format = "%Y-%m-%d %H:%M:%S "
           log.level           = level
           log
         else
           log                 = ScoutLogger.new(nil)
           log.datetime_format = "%Y-%m-%d %H:%M:%S "
           log.level           = Logger::DEBUG
           log
         end
end

#program_nameObject



190
191
192
# File 'lib/es-scout/command.rb', line 190

def program_name
  @program_name ||= Command.program_name
end

#program_pathObject



194
195
196
# File 'lib/es-scout/command.rb', line 194

def program_path
  @program_path ||= Command.program_path
end

#usageObject



198
199
200
# File 'lib/es-scout/command.rb', line 198

def usage
  @usage ||= Command.usage
end

#userObject



186
187
188
# File 'lib/es-scout/command.rb', line 186

def user
  @user ||= Command.user
end

#verbose?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/es-scout/command.rb', line 163

def verbose?
  @verbose
end