net-connector 0.7.3 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -1
- data/README.md +13 -11
- data/docs/architecture.md +18 -1
- data/lib/net/connector/device/base.rb +7 -1
- data/lib/net/connector/device/tftp.rb +14 -8
- data/lib/net/connector/engine/authentication.rb +1 -0
- data/lib/net/connector/engine/dialogue.rb +0 -132
- data/lib/net/connector/engine/errors.rb +0 -92
- data/lib/net/connector/engine/log.rb +17 -1
- data/lib/net/connector/engine/redactor.rb +98 -0
- data/lib/net/connector/engine/response_reader.rb +139 -0
- data/lib/net/connector/engine/session.rb +6 -2
- data/lib/net/connector/netdisco/backup_run.rb +22 -3
- data/lib/net/connector/netdisco/cli/options.rb +3 -0
- data/lib/net/connector/netdisco/cli.rb +11 -8
- data/lib/net/connector/netdisco/connection.rb +0 -1
- data/lib/net/connector/netdisco/database_client.rb +4 -2
- data/lib/net/connector/netdisco/device.rb +2 -0
- data/lib/net/connector/netdisco/diagnostic.rb +4 -0
- data/lib/net/connector/netdisco/fleet.rb +10 -1
- data/lib/net/connector/netdisco/plan.rb +4 -0
- data/lib/net/connector/netdisco/planner.rb +2 -1
- data/lib/net/connector/netdisco/progress.rb +22 -7
- data/lib/net/connector/netdisco/report/files.rb +3 -2
- data/lib/net/connector/netdisco/result_store.rb +5 -7
- data/lib/net/connector/netdisco/rules.rb +1 -0
- data/lib/net/connector/netdisco/settings.rb +6 -0
- data/lib/net/connector/netdisco/tftp_archive.rb +2 -3
- data/lib/net/connector/netdisco/tftp_verification.rb +1 -0
- data/lib/net/connector/netdisco/worker.rb +2 -0
- data/lib/net/connector/netdisco.rb +1 -21
- data/lib/net/connector/vendor/palo_alto/running_config.rb +26 -6
- data/lib/net/connector/version.rb +1 -1
- metadata +7 -6
- data/lib/net/connector/netdisco/tftp_history.rb +0 -16
- /data/{exe/net-connector-backup → bin/net-backup} +0 -0
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "expect/pty"
|
|
4
|
+
require_relative "dialogue"
|
|
5
|
+
|
|
6
|
+
module Net
|
|
7
|
+
module Connector
|
|
8
|
+
# 在传输层上执行有界对话;收到字节或分页标记不会重置截止时间。
|
|
9
|
+
class ResponseReader
|
|
10
|
+
# 保存会话引用,以便读取器使用传输、配置和脱敏上下文。
|
|
11
|
+
def initialize(session)
|
|
12
|
+
@session = session
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# 读取到结束提示、失败模式或超时,并处理交互响应和输出上限。
|
|
16
|
+
def read(prompt:, interactions:, phase:, timeout: nil, deadline: nil, command: nil, failures: {})
|
|
17
|
+
deadline ||= Expect.monotonic + timeout
|
|
18
|
+
patterns = [*failures.keys, *interactions.map(&:pattern), prompt]
|
|
19
|
+
raw = []
|
|
20
|
+
output = []
|
|
21
|
+
counts = Hash.new(0)
|
|
22
|
+
bytes = 0
|
|
23
|
+
loop do
|
|
24
|
+
event = read_event(patterns, deadline)
|
|
25
|
+
raw << event.before unless event.before.empty?
|
|
26
|
+
output << event.before unless event.before.empty?
|
|
27
|
+
bytes += event.before.bytesize + event.match.bytesize
|
|
28
|
+
validate_event!(event, bytes, raw, phase, command)
|
|
29
|
+
|
|
30
|
+
if event.index == patterns.size
|
|
31
|
+
raw << event.match
|
|
32
|
+
output << event.match
|
|
33
|
+
elsif event.index == patterns.size - 1
|
|
34
|
+
return finish_response(event.match, raw, output, phase, command)
|
|
35
|
+
elsif event.index < failures.size
|
|
36
|
+
raise build_connection_error(failures.values.fetch(event.index), phase, command), cause: nil
|
|
37
|
+
else
|
|
38
|
+
reply = interactions.fetch(event.index - failures.size)
|
|
39
|
+
raw << event.match
|
|
40
|
+
output << event.match if reply.capture?
|
|
41
|
+
respond(reply, event.match, counts, deadline, phase, command)
|
|
42
|
+
end
|
|
43
|
+
next if Expect.monotonic < deadline
|
|
44
|
+
|
|
45
|
+
raise build_read_error(nil, phase, command, raw.join), cause: nil
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
private
|
|
50
|
+
|
|
51
|
+
# 分页和流式输出只消耗剩余时间,不为下一次读取重新计时。
|
|
52
|
+
def read_event(patterns, deadline)
|
|
53
|
+
@session.transport.read(patterns, timeout: [deadline - Expect.monotonic, 0].max)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# 连接失败模式携带稳定错误码,供连接级恢复规则选择处理方式。
|
|
57
|
+
def build_connection_error(failure, phase, command)
|
|
58
|
+
klass, code = failure
|
|
59
|
+
@session.build_error(klass, "device connection failed (#{code})", phase: phase, code: code, command: command)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# 先限制原始字节数,再处理传输失败,避免错误输出绕过总量限制。
|
|
63
|
+
def validate_event!(event, bytes, raw, phase, command)
|
|
64
|
+
if bytes > @session.configuration.max_output_bytes
|
|
65
|
+
raise @session.build_error(OutputLimitExceeded, "device output exceeded max_output_bytes",
|
|
66
|
+
phase: phase, command: command, output: (raw + [event.match]).join), cause: nil
|
|
67
|
+
end
|
|
68
|
+
raise build_read_error(event, phase, command, raw.join), cause: nil unless event.matched?
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# 完整提示符必须消费字节;交互标记可从业务输出排除,但原始响应始终保留。
|
|
72
|
+
def finish_response(prompt, raw, output, phase, command)
|
|
73
|
+
if prompt.empty?
|
|
74
|
+
raise @session.build_error(PromptError, "prompt pattern did not consume any output",
|
|
75
|
+
phase: phase, command: command, output: raw.join), cause: nil
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
raw << prompt
|
|
79
|
+
output << prompt
|
|
80
|
+
raw_text = raw.join
|
|
81
|
+
Response.new(raw: raw_text, output: (output == raw) ? raw_text : output.join, prompt: prompt)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# 重复提示和缺失应答必须在写入前失败;所有应答共用读取操作的截止时间。
|
|
85
|
+
def respond(reply, prompt, counts, deadline, phase, command)
|
|
86
|
+
counts[reply] += 1
|
|
87
|
+
if prompt.empty? || (reply.limit && counts[reply] > reply.limit)
|
|
88
|
+
raise build_interaction_error("response rejected or repeated", phase, command), cause: nil
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
value = interaction_response(reply, prompt, phase, command)
|
|
92
|
+
raise build_interaction_error("response unavailable", phase, command), cause: nil unless value.is_a?(String)
|
|
93
|
+
|
|
94
|
+
@session.redactor.remember(value.chomp) if reply.sensitive?
|
|
95
|
+
@session.write(value, deadline: deadline, phase: phase, command: command)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# 动态口令尚未返回时无法加入词表,须在敏感范围内先归一化回调异常。
|
|
99
|
+
# 成功后由调用方登记响应,临时敏感标记不会影响后续普通命令的诊断。
|
|
100
|
+
def interaction_response(reply, prompt, phase, command)
|
|
101
|
+
return reply.response(prompt) unless reply.sensitive?
|
|
102
|
+
|
|
103
|
+
@session.redactor.with_scope(reuse: true) do
|
|
104
|
+
@session.redactor.sensitive!
|
|
105
|
+
reply.response(prompt)
|
|
106
|
+
rescue => error
|
|
107
|
+
raise @session.normalize_error(error, phase: phase, command: command), cause: nil
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# 将交互拒绝或响应缺失转换为认证或脚本错误。
|
|
112
|
+
def build_interaction_error(message, phase, command)
|
|
113
|
+
authenticating = %i[login enable].include?(phase)
|
|
114
|
+
klass = authenticating ? AuthenticationError : ScriptError
|
|
115
|
+
label = authenticating ? "authentication" : "command interaction"
|
|
116
|
+
@session.build_error(klass, "#{label} #{message}", phase: phase, command: command)
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# 根据 EOF、提示符、阶段和底层异常选择具体读取错误类型。
|
|
120
|
+
def build_read_error(event, phase, command, output)
|
|
121
|
+
if event&.error.is_a?(Exception)
|
|
122
|
+
return @session.build_error(TransportError, "transport read failed", phase: phase, command: command,
|
|
123
|
+
underlying: event.error, output: output)
|
|
124
|
+
end
|
|
125
|
+
klass = if event&.error == :eof
|
|
126
|
+
ConnectionClosed
|
|
127
|
+
elsif command&.prompt
|
|
128
|
+
PromptError
|
|
129
|
+
elsif %i[login enable].include?(phase)
|
|
130
|
+
LoginTimeout
|
|
131
|
+
else
|
|
132
|
+
CommandTimeout
|
|
133
|
+
end
|
|
134
|
+
@session.build_error(klass, "device response #{event&.error || :timeout}", phase: phase,
|
|
135
|
+
command: command, output: output)
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
require "English"
|
|
4
4
|
require_relative "authentication"
|
|
5
5
|
require_relative "log"
|
|
6
|
+
require_relative "redactor"
|
|
7
|
+
require_relative "response_reader"
|
|
6
8
|
require_relative "recovery"
|
|
7
9
|
require_relative "transport"
|
|
8
10
|
|
|
@@ -45,12 +47,14 @@ module Net
|
|
|
45
47
|
started = Expect.monotonic
|
|
46
48
|
@log.log_event("operation_start", level: :debug, phase: :script)
|
|
47
49
|
result = yield
|
|
48
|
-
log_operation_completion(started, result.is_a?(Result) ? result.error : nil, steps: steps.size)
|
|
49
|
-
result
|
|
50
50
|
rescue => error
|
|
51
51
|
failure = normalize_error(error, phase: :script)
|
|
52
52
|
log_operation_completion(started, failure, steps: steps.size)
|
|
53
53
|
raise failure, cause: nil
|
|
54
|
+
else
|
|
55
|
+
# 完成事件只发送一次;观察者失败由外层保留业务结果,不再次调用同一观察者。
|
|
56
|
+
log_operation_completion(started, result.is_a?(Result) ? result.error : nil, steps: steps.size)
|
|
57
|
+
result
|
|
54
58
|
end
|
|
55
59
|
end
|
|
56
60
|
|
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
require "json"
|
|
4
4
|
require "socket"
|
|
5
|
+
require_relative "cli/options"
|
|
6
|
+
require_relative "connection"
|
|
7
|
+
require_relative "fleet"
|
|
8
|
+
require_relative "progress"
|
|
9
|
+
require_relative "report/files"
|
|
10
|
+
require_relative "../storage/batch_directory"
|
|
5
11
|
|
|
6
12
|
module Net
|
|
7
13
|
module Connector
|
|
@@ -38,8 +44,9 @@ module Net
|
|
|
38
44
|
|
|
39
45
|
Storage::PrivateFile.write(File.join(batch_directory, "plan.json"), JSON.pretty_generate(preview))
|
|
40
46
|
report = execute(fleet, settings, plan, batch_directory, progress, success_policy, server_root)
|
|
47
|
+
report = record_progress_error(report, progress)
|
|
41
48
|
report = Report::Files.write(report, directory: batch_directory, plan: plan, concurrency: settings.concurrency)
|
|
42
|
-
finish(report, progress, options, batch_directory)
|
|
49
|
+
finish(report, progress, options, batch_directory, plan, settings.concurrency)
|
|
43
50
|
rescue ArgumentError, Client::Error => exception
|
|
44
51
|
@error.puts "备份参数或清单错误:#{exception.message}"
|
|
45
52
|
2
|
|
@@ -95,10 +102,16 @@ module Net
|
|
|
95
102
|
end
|
|
96
103
|
end
|
|
97
104
|
|
|
98
|
-
def finish(report, progress, options, batch_directory)
|
|
105
|
+
def finish(report, progress, options, batch_directory, plan, concurrency)
|
|
99
106
|
progress.finish(report)
|
|
100
107
|
progress.location(report.report_location) if report.report_location
|
|
101
|
-
|
|
108
|
+
summary_path = File.join(batch_directory, "summary.txt")
|
|
109
|
+
progress.location(summary_path, label: "文本报告") if File.file?(summary_path)
|
|
110
|
+
updated = record_progress_error(report, progress)
|
|
111
|
+
unless updated.equal?(report)
|
|
112
|
+
# 末尾显示也可能首次失败,只补写诊断,不重复设备任务或事件。
|
|
113
|
+
report = Report::Files.write(updated, directory: batch_directory, plan: plan, concurrency: concurrency)
|
|
114
|
+
end
|
|
102
115
|
if options[:json]
|
|
103
116
|
@output.puts JSON.generate(directory: batch_directory, counts: report.counts,
|
|
104
117
|
tasks_succeeded: report.policy_success?, success: report.policy_success?,
|
|
@@ -108,6 +121,12 @@ module Net
|
|
|
108
121
|
report.policy_success? ? 0 : 1
|
|
109
122
|
end
|
|
110
123
|
|
|
124
|
+
def record_progress_error(report, progress)
|
|
125
|
+
return report unless progress.output_error && !report.callback_errors.include?(progress.output_error)
|
|
126
|
+
|
|
127
|
+
report.with(callback_errors: [*report.callback_errors, progress.output_error].freeze)
|
|
128
|
+
end
|
|
129
|
+
|
|
111
130
|
def tftp_root(settings, options)
|
|
112
131
|
server = settings.tftp_server || (raise ArgumentError, "TFTP_HOST or YAML tftp.server is required")
|
|
113
132
|
return options[:tftp_root] || @env["TFTP_ROOT"] if options[:tftp_root] || @env["TFTP_ROOT"]
|
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
require "json"
|
|
4
4
|
require "ipaddr"
|
|
5
5
|
require "optparse"
|
|
6
|
+
require_relative "../version"
|
|
7
|
+
require_relative "fleet"
|
|
8
|
+
require_relative "cli/options"
|
|
6
9
|
|
|
7
10
|
module Net
|
|
8
11
|
module Connector
|
|
@@ -46,23 +49,23 @@ module Net
|
|
|
46
49
|
return 0
|
|
47
50
|
end
|
|
48
51
|
|
|
49
|
-
|
|
50
|
-
summary =
|
|
52
|
+
report = backup(fleet, plan, settings, options)
|
|
53
|
+
summary = report.summary
|
|
51
54
|
@output.puts JSON.pretty_generate(summary)
|
|
52
|
-
|
|
55
|
+
report.policy_success? ? 0 : 1
|
|
53
56
|
rescue Storage::PrivateFile::WriteError => exception
|
|
54
57
|
message = Storage::PrivateFile.receipt_error?(exception) ? exception.message : exception.class.name
|
|
55
|
-
@error.puts "net-
|
|
58
|
+
@error.puts "net-backup: #{message}"
|
|
56
59
|
2
|
|
57
60
|
rescue OptionParser::ParseError
|
|
58
|
-
@error.puts "net-
|
|
61
|
+
@error.puts "net-backup: 参数无效;使用 --help 查看参数"
|
|
59
62
|
2
|
|
60
63
|
rescue ArgumentError, KeyError, Errno::ENOENT,
|
|
61
64
|
Psych::Exception, JSON::ParserError, Client::Error => exception
|
|
62
|
-
@error.puts "net-
|
|
65
|
+
@error.puts "net-backup: #{exception.message}"
|
|
63
66
|
2
|
|
64
67
|
rescue StandardError => exception
|
|
65
|
-
@error.puts "net-
|
|
68
|
+
@error.puts "net-backup 失败(#{exception.class})"
|
|
66
69
|
2
|
|
67
70
|
end
|
|
68
71
|
|
|
@@ -113,7 +116,7 @@ module Net
|
|
|
113
116
|
def parse_options
|
|
114
117
|
options = { environment: {} }
|
|
115
118
|
parser = OptionParser.new do |args|
|
|
116
|
-
args.banner = "用法:net-
|
|
119
|
+
args.banner = "用法:net-backup [--config FILE] [--plan | --show-config | --export IP] [--tftp]"
|
|
117
120
|
Options.add_settings(args, options)
|
|
118
121
|
Options.add_execution(args, options)
|
|
119
122
|
args.on("--source SOURCE", %w[http postgres], "清单来源:http(默认)或 postgres") { |value| options[:source] = value }
|
|
@@ -48,8 +48,10 @@ module Net
|
|
|
48
48
|
connected = false
|
|
49
49
|
begin
|
|
50
50
|
Timeout.timeout(budget.remaining, Client::InventoryTimeout) do
|
|
51
|
-
#
|
|
52
|
-
|
|
51
|
+
# 先取得连接句柄再交付超时,确保异常到达时 ensure 能显式关闭连接。
|
|
52
|
+
Thread.handle_interrupt(Client::InventoryTimeout => :never) do
|
|
53
|
+
connection = PG::Connection.connect_start(@connection_options)
|
|
54
|
+
end
|
|
53
55
|
# PostgreSQL NOTICE 可能包含 SQL 或数据,不交给 libpq 默认的 stderr 输出器。
|
|
54
56
|
connection.set_notice_processor { |_notice| nil }
|
|
55
57
|
wait_for_connection(connection, budget)
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative "../engine/error_metadata"
|
|
4
|
+
require_relative "../engine/errors"
|
|
5
|
+
require_relative "../device/local_backup"
|
|
6
|
+
require_relative "../device/tftp/receipt"
|
|
7
|
+
require_relative "../storage/private_file"
|
|
4
8
|
|
|
5
9
|
module Net
|
|
6
10
|
module Connector
|
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "fileutils"
|
|
4
|
-
|
|
4
|
+
require_relative "../storage"
|
|
5
|
+
require_relative "client"
|
|
6
|
+
require_relative "settings"
|
|
7
|
+
require_relative "device"
|
|
8
|
+
require_relative "worker"
|
|
9
|
+
require_relative "result_store"
|
|
10
|
+
require_relative "planner"
|
|
11
|
+
require_relative "report"
|
|
12
|
+
require_relative "tftp_verification"
|
|
13
|
+
require_relative "tftp_archive"
|
|
5
14
|
|
|
6
15
|
module Net
|
|
7
16
|
module Connector
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative "plan"
|
|
4
|
+
require_relative "batch"
|
|
4
5
|
|
|
5
6
|
module Net
|
|
6
7
|
module Connector
|
|
@@ -20,7 +21,7 @@ module Net
|
|
|
20
21
|
end
|
|
21
22
|
selected_tasks = if limit_per_vendor
|
|
22
23
|
ready_tasks.group_by { |_index, device| device.vendor }.values.flat_map do |tasks|
|
|
23
|
-
tasks.
|
|
24
|
+
tasks.min_by(limit_per_vendor) { |_index, device| device.host }
|
|
24
25
|
end
|
|
25
26
|
else
|
|
26
27
|
ready_tasks
|
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "io/console"
|
|
4
|
+
require_relative "../engine/error_metadata"
|
|
5
|
+
require_relative "../engine/log/event"
|
|
4
6
|
|
|
5
7
|
module Net
|
|
6
8
|
module Connector
|
|
7
9
|
module Netdisco
|
|
8
10
|
# 共享一个实例接收并发会话事件和任务回调;人类进度不混入 JSON 输出。
|
|
9
11
|
class Progress
|
|
12
|
+
attr_reader :output_error
|
|
13
|
+
|
|
10
14
|
def initialize(io: $stderr, enabled: true, verbose: false)
|
|
11
15
|
@io, @enabled, @verbose = io, enabled, verbose
|
|
12
16
|
@tty = io.respond_to?(:tty?) && io.tty?
|
|
@@ -148,8 +152,8 @@ module Net
|
|
|
148
152
|
@mutex.synchronize { refresh(force: true) }
|
|
149
153
|
end
|
|
150
154
|
|
|
151
|
-
def location(path)
|
|
152
|
-
write(nil, "
|
|
155
|
+
def location(path, label: "报告")
|
|
156
|
+
write(nil, "#{label}:#{path}")
|
|
153
157
|
end
|
|
154
158
|
|
|
155
159
|
private
|
|
@@ -209,9 +213,7 @@ module Net
|
|
|
209
213
|
|
|
210
214
|
clear_refresh
|
|
211
215
|
@last_status = lines
|
|
212
|
-
@
|
|
213
|
-
@io.flush
|
|
214
|
-
@refreshing = true
|
|
216
|
+
@refreshing = write_output(lines.join("\n"))
|
|
215
217
|
end
|
|
216
218
|
|
|
217
219
|
# 留一列避免终端自动换行,窄窗口也不会留下旧进度行。
|
|
@@ -231,7 +233,7 @@ module Net
|
|
|
231
233
|
def clear_refresh
|
|
232
234
|
return unless @refreshing
|
|
233
235
|
|
|
234
|
-
|
|
236
|
+
write_output("\r\e[2K" + ("\e[1A\r\e[2K" * (@last_status.length - 1)))
|
|
235
237
|
@refreshing = false
|
|
236
238
|
end
|
|
237
239
|
|
|
@@ -246,8 +248,21 @@ module Net
|
|
|
246
248
|
clear_refresh
|
|
247
249
|
line = "[#{Time.now.strftime("%H:%M:%S")}] [#{@completed}/#{@total}#{percent} 执行中 #{@active}] [#{host || "批次"}] #{message}"
|
|
248
250
|
line = "[#{Time.now.strftime("%H:%M:%S")}] #{host ? "#{host} " : ""}#{message}" unless @verbose
|
|
249
|
-
|
|
251
|
+
write_output(line.gsub(/[[:cntrl:]]/, " ") + "\n")
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
# 显示失败只停用进度输出;批次仍保存回执和事件,并记录一次受控回调诊断。
|
|
255
|
+
# 所有调用均持有 @mutex,心跳与设备线程不会重复访问已失败的输出。
|
|
256
|
+
def write_output(text)
|
|
257
|
+
return false unless @enabled
|
|
258
|
+
|
|
259
|
+
@io.write(text)
|
|
250
260
|
@io.flush
|
|
261
|
+
true
|
|
262
|
+
rescue IOError, SystemCallError => error
|
|
263
|
+
@output_error = { host: nil, error_type: ErrorMetadata.type(error.class.name) }.freeze
|
|
264
|
+
@enabled = false
|
|
265
|
+
false
|
|
251
266
|
end
|
|
252
267
|
end
|
|
253
268
|
end
|
|
@@ -16,8 +16,9 @@ module Net
|
|
|
16
16
|
Storage::PrivateFile.write(destination, JSON.pretty_generate(finalized.summary))
|
|
17
17
|
finalized
|
|
18
18
|
rescue StandardError => error
|
|
19
|
-
location = committed_location(error, destination)
|
|
20
|
-
|
|
19
|
+
location = committed_location(error, destination) || report.report_location
|
|
20
|
+
# 更新已有诊断时仍保留首次报告故障,以及之前已提交的报告位置。
|
|
21
|
+
failed = report.report_error ? report.with(report_location: location) : report.with_report_error(error, location: location)
|
|
21
22
|
begin
|
|
22
23
|
saved = failed.with(report_location: destination)
|
|
23
24
|
Storage::PrivateFile.write(destination, JSON.pretty_generate(saved.summary))
|
|
@@ -12,17 +12,15 @@ module Net
|
|
|
12
12
|
# 每批保存一份私有 JSON 报告,通过重命名防止半写文件。
|
|
13
13
|
class Json
|
|
14
14
|
# 以临时文件和重命名原子写入私有 JSON 报告。
|
|
15
|
-
def write(
|
|
15
|
+
def write(report, directory:)
|
|
16
16
|
destination = File.expand_path(directory)
|
|
17
17
|
FileUtils.mkdir_p(destination, mode: 0o700)
|
|
18
|
-
filename = "netdisco-#{
|
|
18
|
+
filename = "netdisco-#{report.mode}-#{report.started_at.strftime("%Y%m%dT%H%M%SZ")}-#{SecureRandom.hex(4)}.json"
|
|
19
19
|
path = File.join(destination, filename)
|
|
20
|
-
Storage::PrivateFile.write(path, JSON.pretty_generate(
|
|
20
|
+
Storage::PrivateFile.write(path, JSON.pretty_generate(report.summary)).path
|
|
21
21
|
end
|
|
22
22
|
end
|
|
23
23
|
|
|
24
|
-
Text = Json # Compatibility with the former name of the JSON writer.
|
|
25
|
-
|
|
26
24
|
# 数据库仓储由调用方提供,可使用 Active Record 或其他对象。
|
|
27
25
|
# 仓储须实现 create!,本项目不负责数据库表结构。
|
|
28
26
|
class Database
|
|
@@ -34,8 +32,8 @@ module Net
|
|
|
34
32
|
end
|
|
35
33
|
|
|
36
34
|
# 把批量摘要写入调用方仓储并返回记录位置。
|
|
37
|
-
def write(
|
|
38
|
-
record = @repository.create!(
|
|
35
|
+
def write(report, **_options)
|
|
36
|
+
record = @repository.create!(report.summary)
|
|
39
37
|
record.respond_to?(:id) ? "database:#{record.id}" : "database"
|
|
40
38
|
end
|
|
41
39
|
end
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "json"
|
|
4
|
+
require_relative "../../connector"
|
|
5
|
+
require_relative "client"
|
|
6
|
+
require_relative "database_client"
|
|
7
|
+
require_relative "config_file"
|
|
8
|
+
require_relative "rules"
|
|
9
|
+
require_relative "planner"
|
|
4
10
|
|
|
5
11
|
module Net
|
|
6
12
|
module Connector
|
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
require "fileutils"
|
|
4
4
|
require "tempfile"
|
|
5
|
+
require "digest"
|
|
5
6
|
require_relative "../storage/backup_lock"
|
|
6
7
|
require_relative "../storage/private_file"
|
|
7
8
|
require_relative "../storage/safe_file"
|
|
8
9
|
require_relative "tftp_verification"
|
|
10
|
+
require_relative "diagnostic"
|
|
9
11
|
|
|
10
12
|
module Net
|
|
11
13
|
module Connector
|
|
@@ -69,9 +71,6 @@ module Net
|
|
|
69
71
|
end
|
|
70
72
|
end
|
|
71
73
|
|
|
72
|
-
alias path_for upload_filename
|
|
73
|
-
alias capture upload_and_archive
|
|
74
|
-
|
|
75
74
|
private
|
|
76
75
|
|
|
77
76
|
# 文件系统的 mtime 可能落后于 Time.now;用同一服务器目录的时间建立上传边界。
|
|
@@ -1,25 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
require_relative "netdisco/client"
|
|
5
|
-
require_relative "netdisco/database_client"
|
|
6
|
-
require_relative "netdisco/rules"
|
|
7
|
-
require_relative "netdisco/device"
|
|
8
|
-
require_relative "netdisco/config_file"
|
|
9
|
-
require_relative "netdisco/settings"
|
|
10
|
-
require_relative "netdisco/worker"
|
|
11
|
-
require_relative "netdisco/result_store"
|
|
12
|
-
require_relative "netdisco/batch"
|
|
13
|
-
require_relative "netdisco/planner"
|
|
14
|
-
require_relative "netdisco/fleet"
|
|
15
|
-
require_relative "netdisco/progress"
|
|
3
|
+
# 公共入口只组合应用流程;每个模块自行声明依赖,不要求调用方维护加载顺序。
|
|
16
4
|
require_relative "netdisco/cli"
|
|
17
|
-
require_relative "storage/batch_directory"
|
|
18
|
-
require_relative "netdisco/report/text"
|
|
19
|
-
require_relative "netdisco/connection"
|
|
20
|
-
require_relative "netdisco/cli/options"
|
|
21
|
-
require_relative "netdisco/report/files"
|
|
22
|
-
require_relative "netdisco/tftp_verification"
|
|
23
|
-
require_relative "netdisco/tftp_archive"
|
|
24
|
-
require_relative "netdisco/tftp_history"
|
|
25
5
|
require_relative "netdisco/backup_run"
|
|
@@ -48,19 +48,39 @@ module Net
|
|
|
48
48
|
return false if text.empty?
|
|
49
49
|
|
|
50
50
|
pending = +""
|
|
51
|
+
quote = nil
|
|
51
52
|
text.each_line do |line|
|
|
52
53
|
next if pending.empty? && line.strip.empty?
|
|
53
54
|
return false if pending.empty? && !line.start_with?("set ")
|
|
54
55
|
|
|
55
56
|
pending << line
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
end
|
|
57
|
+
quote = quote_after(line, quote)
|
|
58
|
+
next if quote
|
|
59
|
+
|
|
60
|
+
Shellwords.split(pending)
|
|
61
|
+
pending.clear
|
|
62
62
|
end
|
|
63
63
|
pending.empty?
|
|
64
|
+
rescue ArgumentError
|
|
65
|
+
false
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# 每行只扫描新增字节,整条逻辑命令闭合后才交给 Shellwords 校验。
|
|
69
|
+
# 单引号中的反斜杠是普通字符;其他位置的转义只影响紧随的一个字节。
|
|
70
|
+
def quote_after(line, quote)
|
|
71
|
+
escaped = false
|
|
72
|
+
line.each_byte do |byte|
|
|
73
|
+
if escaped
|
|
74
|
+
escaped = false
|
|
75
|
+
elsif byte == 92 && quote != 39
|
|
76
|
+
escaped = true
|
|
77
|
+
elsif byte == quote
|
|
78
|
+
quote = nil
|
|
79
|
+
elsif quote.nil? && (byte == 34 || byte == 39)
|
|
80
|
+
quote = byte
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
quote
|
|
64
84
|
end
|
|
65
85
|
|
|
66
86
|
# 去掉命令回显和提示符,保留真正需要校验的设备响应。
|