Add --dry-run option to nailgun-agent

It's a bit hard to debug nailgun-agent, because it
tries to send info over http somewhere and sleeps at
the beginning. So you can't run it on your desktop, for
example.

Let's add --dry-run option, to have ability to just get
and print all information.

This is a small improvement, so no bug or blueprint.

Change-Id: If7309635d40ff3263a671fddd7df20efd917097c
This commit is contained in:
Dmitry Guryanov 2016-04-22 19:27:01 +03:00
parent ea7350a649
commit c5f7deaa98
1 changed files with 36 additions and 9 deletions

45
agent
View File

@ -29,6 +29,7 @@ require 'rethtool'
require 'digest'
require 'timeout'
require 'uri'
require 'optparse'
# TODO(vsharshov): replace below lines by this string after excluding Ruby 1.8
require 'pathname'
require 'rexml/document'
@ -146,15 +147,19 @@ class NodeAgent
API_DEFAULT_PORT = "8443"
API_LEGACY_PORT = "8000"
def initialize(logger)
def initialize(logger, dry_run)
@logger = logger
@settings = get_settings()
@api_ip = URI(@settings['url']).host or API_DEFAULT_ADDRESS
scheme, api_port = get_scheme_and_port()
unless dry_run
@api_ip = URI(@settings['url']).host || API_DEFAULT_ADDRESS
scheme, api_port = get_scheme_and_port
@api_url = "#{scheme}://#{@api_ip}:#{api_port}/api"
@logger.info("API URL is #{@api_url}")
end
@api_url = "#{scheme}://#{@api_ip}:#{api_port}/api"
@logger.info("API URL is #{@api_url}")
@os = ohai_system_info
@numa_topology = get_numa_topology
@mpath_devices, @skip_devices = multipath_devices
@ -1033,6 +1038,12 @@ class NodeAgent
@node_state = "discover" if system_type == "bootstrap"
end
end
def print
s = _data.to_json
@logger.info("Data collected by nailgun-agent:")
@logger.info(s)
end
end
def write_data_to_file(logger, filename, data)
@ -1062,6 +1073,15 @@ def provisioned?
Socket.gethostname != 'bootstrap'
end
dry_run = false
OptionParser.new do |opts|
opts.banner = "Usage: nailgun-agent [options]"
opts.on("-d", "--dry-run", "Only print collected information, don't send it anywhere.") do |_d|
dry_run = true
end
end.parse!
logger = Logger.new(STDOUT)
if File.exist?('/etc/nailgun_uid')
@ -1072,18 +1092,25 @@ end
# random sleep is here to prevent target nodes
# from reporting to master node all at once
sleep_time = rand(30)
logger.debug("Sleep for #{sleep_time} seconds before sending request")
sleep(sleep_time)
unless dry_run
sleep_time = rand(30)
logger.debug("Sleep for #{sleep_time} seconds before sending request")
sleep(sleep_time)
end
if File.exist?('/etc/nailgun-agent/nodiscover')
logger.info("Discover prevented by /etc/nailgun-agent/nodiscover presence.")
exit 1
end
agent = NodeAgent.new(logger)
agent = NodeAgent.new(logger, dry_run)
agent.update_state
if dry_run
agent.print
exit 0
end
begin
unless File.exist?('/etc/nailgun_uid')
resp = agent.post