Browse Source

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
Dmitry Guryanov 3 years ago
parent
commit
c5f7deaa98
1 changed files with 36 additions and 9 deletions
  1. 36
    9
      agent

+ 36
- 9
agent View File

@@ -29,6 +29,7 @@ require 'rethtool'
29 29
 require 'digest'
30 30
 require 'timeout'
31 31
 require 'uri'
32
+require 'optparse'
32 33
 # TODO(vsharshov): replace below lines by this string after excluding Ruby 1.8
33 34
 require 'pathname'
34 35
 require 'rexml/document'
@@ -146,15 +147,19 @@ class NodeAgent
146 147
   API_DEFAULT_PORT = "8443"
147 148
   API_LEGACY_PORT = "8000"
148 149
 
149
-  def initialize(logger)
150
+  def initialize(logger, dry_run)
150 151
     @logger = logger
151 152
     @settings = get_settings()
152
-    @api_ip = URI(@settings['url']).host or API_DEFAULT_ADDRESS
153 153
 
154
-    scheme, api_port = get_scheme_and_port()
154
+    unless dry_run
155
+      @api_ip = URI(@settings['url']).host || API_DEFAULT_ADDRESS
156
+
157
+      scheme, api_port = get_scheme_and_port
158
+
159
+      @api_url = "#{scheme}://#{@api_ip}:#{api_port}/api"
160
+      @logger.info("API URL is #{@api_url}")
161
+    end
155 162
 
156
-    @api_url = "#{scheme}://#{@api_ip}:#{api_port}/api"
157
-    @logger.info("API URL is #{@api_url}")
158 163
     @os = ohai_system_info
159 164
     @numa_topology = get_numa_topology
160 165
     @mpath_devices, @skip_devices = multipath_devices
@@ -1033,6 +1038,12 @@ class NodeAgent
1033 1038
       @node_state = "discover" if system_type == "bootstrap"
1034 1039
     end
1035 1040
   end
1041
+
1042
+  def print
1043
+    s = _data.to_json
1044
+    @logger.info("Data collected by nailgun-agent:")
1045
+    @logger.info(s)
1046
+  end
1036 1047
 end
1037 1048
 
1038 1049
 def write_data_to_file(logger, filename, data)
@@ -1062,6 +1073,15 @@ def provisioned?
1062 1073
   Socket.gethostname != 'bootstrap'
1063 1074
 end
1064 1075
 
1076
+dry_run = false
1077
+OptionParser.new do |opts|
1078
+  opts.banner = "Usage: nailgun-agent [options]"
1079
+
1080
+  opts.on("-d", "--dry-run", "Only print collected information, don't send it anywhere.") do |_d|
1081
+    dry_run = true
1082
+  end
1083
+end.parse!
1084
+
1065 1085
 logger = Logger.new(STDOUT)
1066 1086
 
1067 1087
 if File.exist?('/etc/nailgun_uid')
@@ -1072,18 +1092,25 @@ end
1072 1092
 
1073 1093
 # random sleep is here to prevent target nodes
1074 1094
 # from reporting to master node all at once
1075
-sleep_time = rand(30)
1076
-logger.debug("Sleep for #{sleep_time} seconds before sending request")
1077
-sleep(sleep_time)
1095
+unless dry_run
1096
+  sleep_time = rand(30)
1097
+  logger.debug("Sleep for #{sleep_time} seconds before sending request")
1098
+  sleep(sleep_time)
1099
+end
1078 1100
 
1079 1101
 if File.exist?('/etc/nailgun-agent/nodiscover')
1080 1102
   logger.info("Discover prevented by /etc/nailgun-agent/nodiscover presence.")
1081 1103
   exit 1
1082 1104
 end
1083 1105
 
1084
-agent = NodeAgent.new(logger)
1106
+agent = NodeAgent.new(logger, dry_run)
1085 1107
 agent.update_state
1086 1108
 
1109
+if dry_run
1110
+  agent.print
1111
+  exit 0
1112
+end
1113
+
1087 1114
 begin
1088 1115
   unless File.exist?('/etc/nailgun_uid')
1089 1116
     resp = agent.post

Loading…
Cancel
Save