From e7bd486ec6d8c65ce5611dad69059e0b7c22f394 Mon Sep 17 00:00:00 2001 From: Dmitry Guryanov Date: Fri, 22 Apr 2016 19:27:01 +0300 Subject: [PATCH] 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 (cherry picked from commit c5f7deaa98c8b137d8d08d8d8c0c2f8406277576) --- agent | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/agent b/agent index 89ab60f..ad75aa3 100755 --- a/agent +++ b/agent @@ -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 @@ -1080,6 +1085,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) @@ -1109,6 +1120,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') @@ -1119,18 +1139,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