Class: RightScale::AgentController

Inherits:
Object
  • Object
show all
Includes:
CommonParser
Defined in:
lib/right_agent/scripts/agent_controller.rb

Constant Summary collapse

FORCED_OPTIONS =
{
  :threadpool_size => 1
}
DEFAULT_OPTIONS =
{
  :single_threaded => true,
  :log_dir => Platform.filesystem.log_dir,
  :daemonize => true
}
@@agent =
nil

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CommonParser

#agent_type, #parse_common, #resolve_identity

Class Method Details

.runObject

Create and run controller

Return

true

Always return true



104
105
106
107
# File 'lib/right_agent/scripts/agent_controller.rb', line 104

def self.run
  c = AgentController.new
  c.control(c.parse_args)
end

Instance Method Details

#control(options) ⇒ Object

Parse arguments and execute request

Parameters

options(Hash)

Command line options

Return

true

Always return true



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
# File 'lib/right_agent/scripts/agent_controller.rb', line 116

def control(options)

  # Initialize directory settings
  AgentConfig.cfg_dir = options[:cfg_dir]
  AgentConfig.pid_dir = options[:pid_dir]

  # List agents if requested
  list_configured_agents if options[:list]

  # Validate arguments
  action = options.delete(:action)
  fail("No action specified on the command line.", print_usage = true) unless action
  if action == 'kill' && (options[:pid_file].nil? || !File.file?(options[:pid_file]))
    fail("Missing or invalid pid file #{options[:pid_file]}", print_usage = true)
  end
  if options[:agent_name]
    if action == 'start'
      cfg = configure_agent(action, options)
    else
      cfg = AgentConfig.load_cfg(options[:agent_name])
      fail("Deployment is missing configuration file #{AgentConfig.cfg_file(options[:agent_name]).inspect}.") unless cfg
    end
    options.delete(:identity)
    options = cfg.merge(options)
    AgentConfig.root_dir = options[:root_dir]
    AgentConfig.pid_dir = options[:pid_dir]
    Log.program_name = syslog_program_name(options)
    Log.log_to_file_only(options[:log_to_file_only])
    configure_proxy(options[:http_proxy], options[:http_no_proxy]) if options[:http_proxy]
  elsif options[:identity]
    options[:agent_name] = AgentConfig.agent_name(options[:identity])
  end
  @options = DEFAULT_OPTIONS.clone.merge(options.merge(FORCED_OPTIONS))
  FileUtils.mkdir_p(@options[:pid_dir]) unless @options[:pid_dir].nil? || File.directory?(@options[:pid_dir])

  # Execute request
  success = case action
  when /show|killall/
    action = 'stop' if action == 'killall'
    s = true
    AgentConfig.cfg_agents.each { |agent_name| s &&= dispatch(action, agent_name) }
    s
  when 'kill'
    kill_process
  else
    dispatch(action, @options[:agent_name])
  end

  exit(1) unless success
end

#parse_argsObject

Create options hash from command line arguments

Return

options(Hash)

Parsed options



171
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
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
# File 'lib/right_agent/scripts/agent_controller.rb', line 171

def parse_args
  options = {:thin_command_client => false}

  opts = OptionParser.new do |opts|
    parse_common(opts, options)
    parse_other_args(opts, options)

    opts.on("-s", "--start AGENT") do |a|
      options[:action] = 'start'
      options[:agent_name] = a
    end

    opts.on("-p", "--stop AGENT") do |a|
      options[:action] = 'stop'
      options[:agent_name] = a
    end

    opts.on("--stop-agent ID") do |id|
      options[:action] = 'stop'
      options[:identity] = id
    end

    opts.on("-k", "--kill PIDFILE") do |file|
      options[:pid_file] = file
      options[:action] = 'kill'
    end

    opts.on("-K", "--killall") do
      options[:action] = 'killall'
    end

    opts.on("-U", "--status") do
      options[:action] = 'show'
    end

    opts.on("-l", "--list") do
      options[:list] = true
    end

    opts.on("--log-level LVL") do |lvl|
      options[:log_level] = lvl
    end

    opts.on("-c", "--cfg-dir DIR") do |d|
      options[:cfg_dir] = d
    end

    opts.on("-z", "--pid-dir DIR") do |dir|
      options[:pid_dir] = dir
    end

    opts.on("--log-dir DIR") do |dir|
      options[:log_dir] = dir

      # Ensure log directory exists (for windows, etc.)
      FileUtils.mkdir_p(dir) unless File.directory?(dir)
    end

    opts.on("-f", "--foreground") do
      options[:daemonize] = false
      #Squelch Ruby VM warnings about various things
      $VERBOSE = nil
    end

    opts.on("-I", "--interactive") do
      options[:console] = true
    end

    opts.on_tail("--help") do
      puts Usage.scan(__FILE__)
      exit
    end

  end

  begin
    opts.parse(ARGV)
  rescue Exception => e
    exit 0 if e.is_a?(SystemExit)
    fail(e.message, print_usage = true)
  end

  # allow specific arguments to use a thin command client for faster
  # execution (on Windows, etc.)
  unless options[:thin_command_client]
    # require full right_agent for any commands which do not specify thin
    # command client.
    require File.normalize_path(File.join(File.dirname(__FILE__), '..', '..', 'right_agent'))
  end
  resolve_identity(options)
  options
end