Class: Scout::Command

Inherits:
Object
  • Object
show all
Includes:
HTTP
Defined in:
lib/scout/command.rb,
lib/scout/command/run.rb,
lib/scout/command/sign.rb,
lib/scout/command/test.rb,
lib/scout/command/install.rb,
lib/scout/command/realtime.rb,
lib/scout/command/troubleshoot.rb

Direct Known Subclasses

Install, Realtime, Run, Sign, Test, Troubleshoot

Defined Under Namespace

Classes: APITimeoutError, Install, Realtime, Run, Sign, Test, Troubleshoot

Constant Summary collapse

HTTP_HEADERS =
{ "Client-Version"  => Scout::VERSION }

Constants included from HTTP

HTTP::CA_FILE, HTTP::VERIFY_MODE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HTTP

#build_http

Constructor Details

#initialize(options, args) ⇒ Command

Returns a new instance of Command.



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/scout/command.rb', line 172

def initialize(options, args)
  @roles   = options[:roles]
  @server  = options[:server]  || "https://server.pingdom.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
  @server_name    = options[:server_name]
  @http_proxy     = options[:http_proxy] || ""
  @https_proxy    = options[:https_proxy] || ""
  @hostname       = options[:hostname] || Socket.gethostname
  @environment    = options[:environment] || ""
  @options = options
  @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.



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

def config_dir
  @config_dir
end

#historyObject (readonly)

Returns the value of attribute history.



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

def history
  @history
end

#hostnameObject (readonly)

Returns the value of attribute hostname.



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

def hostname
  @hostname
end

#log_pathObject (readonly)

Returns the value of attribute log_path.



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

def log_path
  @log_path
end

#serverObject (readonly)

Returns the value of attribute server.



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

def server
  @server
end

#server_nameObject (readonly)

Returns the value of attribute server_name.



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

def server_name
  @server_name
end

Class Method Details

.dispatch(argv) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/scout/command.rb', line 156

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



14
15
16
# File 'lib/scout/command.rb', line 14

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

.program_pathObject



18
19
20
# File 'lib/scout/command.rb', line 18

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

.usageObject



22
23
24
# File 'lib/scout/command.rb', line 22

def self.usage
  @usage
end

.userObject



10
11
12
# File 'lib/scout/command.rb', line 10

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

Instance Method Details

#create_pid_file_or_exitObject



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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/scout/command.rb', line 240

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
    running = true
    pid = File.read(pid_file).strip.to_i rescue "unknown"
    if pid.is_a?(Fixnum)
      if pid.zero? 
        running = false
      else
        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
      end # pid.zero?
    end # pid.is_a?(Fixnum)
    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://server.pingdom.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



220
221
222
# File 'lib/scout/command.rb', line 220

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

#logObject



205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/scout/command.rb', line 205

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



228
229
230
# File 'lib/scout/command.rb', line 228

def program_name
  @program_name ||= Command.program_name
end

#program_pathObject



232
233
234
# File 'lib/scout/command.rb', line 232

def program_path
  @program_path ||= Command.program_path
end

#usageObject



236
237
238
# File 'lib/scout/command.rb', line 236

def usage
  @usage ||= Command.usage
end

#userObject



224
225
226
# File 'lib/scout/command.rb', line 224

def user
  @user ||= Command.user
end

#verbose?Boolean

Returns:

  • (Boolean)


201
202
203
# File 'lib/scout/command.rb', line 201

def verbose?
  @verbose
end