net-connector 0.4.0 → 0.4.2
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 +110 -68
- data/CONTRIBUTING.md +40 -0
- data/README.md +140 -95
- data/SECURITY.md +11 -0
- data/docs/VERIFICATION.md +129 -5
- data/docs/architecture.md +321 -248
- data/lib/net/connector/device/base.rb +12 -4
- data/lib/net/connector/device/interface_description.rb +3 -0
- data/lib/net/connector/device/profile/builder.rb +236 -0
- data/lib/net/connector/device/profile.rb +2 -227
- data/lib/net/connector/device/running_config/strategy.rb +5 -0
- data/lib/net/connector/device/running_config.rb +7 -0
- data/lib/net/connector/engine/command.rb +14 -2
- data/lib/net/connector/engine/configuration.rb +10 -7
- data/lib/net/connector/engine/dialogue.rb +53 -28
- data/lib/net/connector/engine/errors.rb +41 -54
- data/lib/net/connector/engine/execution.rb +32 -5
- data/lib/net/connector/engine/log.rb +23 -17
- data/lib/net/connector/engine/session.rb +39 -7
- data/lib/net/connector/engine/terminal_renderer.rb +7 -4
- data/lib/net/connector/netdisco/batch.rb +31 -3
- data/lib/net/connector/netdisco/cli.rb +92 -50
- data/lib/net/connector/netdisco/client.rb +206 -73
- data/lib/net/connector/netdisco/config_file.rb +26 -4
- data/lib/net/connector/netdisco/device.rb +4 -8
- data/lib/net/connector/netdisco/diagnostic.rb +91 -0
- data/lib/net/connector/netdisco/fleet.rb +105 -108
- data/lib/net/connector/netdisco/inventory_budget.rb +49 -0
- data/lib/net/connector/netdisco/plan.rb +77 -0
- data/lib/net/connector/netdisco/planner.rb +12 -23
- data/lib/net/connector/netdisco/report.rb +94 -0
- data/lib/net/connector/netdisco/rules.rb +28 -5
- data/lib/net/connector/netdisco/settings.rb +194 -85
- data/lib/net/connector/netdisco/worker.rb +43 -27
- data/lib/net/connector/operations/backup_lock.rb +116 -0
- data/lib/net/connector/operations/local_backup.rb +49 -9
- data/lib/net/connector/operations/parse_output.rb +20 -3
- data/lib/net/connector/operations/private_file.rb +94 -5
- data/lib/net/connector/operations/safe_file.rb +62 -0
- data/lib/net/connector/operations/saved_config/legacy_index.rb +109 -0
- data/lib/net/connector/operations/saved_config.rb +40 -10
- data/lib/net/connector/operations/tftp/file_upload.rb +55 -0
- data/lib/net/connector/operations/tftp/strategy.rb +9 -11
- data/lib/net/connector/operations/tftp_backup.rb +78 -15
- data/lib/net/connector/operations/tftp_receipt.rb +73 -0
- data/lib/net/connector/operations/topology/immediate_strategy.rb +45 -0
- data/lib/net/connector/operations/topology/strategy.rb +34 -0
- data/lib/net/connector/operations/topology.rb +98 -29
- data/lib/net/connector/operations.rb +6 -0
- data/lib/net/connector/vendor/cisco_ios/running_config.rb +1 -0
- data/lib/net/connector/vendor/cisco_ios/tftp_backup.rb +9 -2
- data/lib/net/connector/vendor/cisco_ios/topology.rb +18 -3
- data/lib/net/connector/vendor/cisco_nxos/running_config.rb +1 -0
- data/lib/net/connector/vendor/cisco_nxos/tftp_backup.rb +8 -1
- data/lib/net/connector/vendor/h3c/tftp_backup.rb +7 -18
- data/lib/net/connector/vendor/h3c/topology.rb +17 -3
- data/lib/net/connector/vendor/hillstone/running_config.rb +1 -0
- data/lib/net/connector/vendor/hillstone/tftp_backup.rb +16 -3
- data/lib/net/connector/vendor/hillstone/topology.rb +17 -3
- data/lib/net/connector/vendor/huawei/tftp_backup.rb +8 -18
- data/lib/net/connector/vendor/palo_alto/running_config.rb +4 -0
- data/lib/net/connector/vendor/palo_alto/tftp_backup.rb +19 -8
- data/lib/net/connector/vendor/palo_alto/topology.rb +16 -1
- data/lib/net/connector/vendor/radware/tftp_backup.rb +11 -4
- data/lib/net/connector/vendor/radware/topology.rb +3 -0
- data/lib/net/connector/version.rb +1 -1
- metadata +60 -28
|
@@ -1,24 +1,10 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative "plan"
|
|
4
|
+
|
|
3
5
|
module Net
|
|
4
6
|
module Connector
|
|
5
7
|
module Netdisco
|
|
6
|
-
# 固定一份清单上的选择结果与跳过原因,供预览和执行复用。
|
|
7
|
-
Plan = Data.define(:mode, :inventory, :ready, :outcomes) do
|
|
8
|
-
# Data 只冻结对象自身;这里复制并冻结可能由调用方修改的计划容器。
|
|
9
|
-
def initialize(mode:, inventory:, ready:, outcomes:)
|
|
10
|
-
unless inventory.is_a?(Array) && ready.is_a?(Array) && outcomes.is_a?(Array)
|
|
11
|
-
raise ArgumentError, "plan inventory, ready tasks, and outcomes must be Arrays"
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
tasks = ready.map { |task| task.is_a?(Array) ? task.dup.freeze : task }.freeze
|
|
15
|
-
super(mode: mode, inventory: inventory.dup.freeze, ready: tasks, outcomes: outcomes.dup.freeze)
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
# 按清单顺序返回选中的设备。
|
|
19
|
-
def selected = ready.map(&:last)
|
|
20
|
-
end
|
|
21
|
-
|
|
22
8
|
# 根据设备就绪状态、厂商采样及远端文件冲突建立备份计划。
|
|
23
9
|
class Planner
|
|
24
10
|
# 保存本次规划使用的清单快照。
|
|
@@ -37,22 +23,25 @@ module Net
|
|
|
37
23
|
else
|
|
38
24
|
candidates
|
|
39
25
|
end
|
|
40
|
-
# PAN-OS 导出使用固定远端文件名,同批只允许一台设备写入。
|
|
41
|
-
palo_alto_index = selected.find { |device, _index| device.vendor == :palo_alto }&.last if mode == :tftp
|
|
42
26
|
selected_indices = selected.to_h { |_device, index| [index, true] }
|
|
27
|
+
filenames = {}
|
|
28
|
+
if mode == :tftp
|
|
29
|
+
# 先按采样顺序保留每个目标的首台设备,再按清单顺序输出结果。
|
|
30
|
+
selected.each { |device, index| filenames[device.tftp_filename] ||= index }
|
|
31
|
+
end
|
|
43
32
|
outcomes = Array.new(@inventory.size)
|
|
44
33
|
ready = @inventory.each_with_index.filter_map do |device, index|
|
|
45
34
|
if !device.ready?
|
|
46
35
|
outcomes[index] = skipped(device, device.issue)
|
|
47
36
|
nil
|
|
48
|
-
elsif
|
|
37
|
+
elsif !selected_indices.key?(index)
|
|
38
|
+
outcomes[index] = skipped(device, :sample_limit)
|
|
39
|
+
nil
|
|
40
|
+
elsif mode == :tftp && filenames.fetch(device.tftp_filename) != index
|
|
49
41
|
outcomes[index] = skipped(device, :remote_filename_collision)
|
|
50
42
|
nil
|
|
51
|
-
elsif selected_indices.key?(index)
|
|
52
|
-
[index, device].freeze
|
|
53
43
|
else
|
|
54
|
-
|
|
55
|
-
nil
|
|
44
|
+
[index, device].freeze
|
|
56
45
|
end
|
|
57
46
|
end
|
|
58
47
|
Plan.new(mode: mode, inventory: @inventory, ready: ready.freeze, outcomes: outcomes.freeze)
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "forwardable"
|
|
4
|
+
require_relative "batch"
|
|
5
|
+
|
|
6
|
+
module Net
|
|
7
|
+
module Connector
|
|
8
|
+
module Netdisco
|
|
9
|
+
# v2 显式包装旧 Batch;成功策略与清单覆盖分开,默认 Batch JSON 不增加字段。
|
|
10
|
+
class Report
|
|
11
|
+
extend Forwardable
|
|
12
|
+
SUCCESS = %i[backed_up reported_uploaded].freeze
|
|
13
|
+
ATTEMPTED = [*SUCCESS, :failed, :saved_with_error, :reported_with_error].freeze
|
|
14
|
+
IGNORABLE = %i[filtered sample_limit].freeze
|
|
15
|
+
private_constant :SUCCESS, :ATTEMPTED, :IGNORABLE
|
|
16
|
+
|
|
17
|
+
attr_reader :batch, :policy, :duration_ms, :report_diagnostic
|
|
18
|
+
def_delegators :@batch, *Batch.members, :success?, :status, :counts
|
|
19
|
+
|
|
20
|
+
# selected 必须使用带策略和覆盖信息的 v2;默认 strict 继续使用旧 schema。
|
|
21
|
+
def self.options(policy: :strict, schema: nil)
|
|
22
|
+
raise ArgumentError, "success_policy must be strict or selected" unless %i[strict selected].include?(policy)
|
|
23
|
+
|
|
24
|
+
schema = policy == :selected ? 2 : 1 if schema.nil?
|
|
25
|
+
raise ArgumentError, "report_schema must be 1 or 2" unless schema.is_a?(Integer) && [1, 2].include?(schema)
|
|
26
|
+
raise ArgumentError, "selected success policy requires report schema 2" if policy == :selected && schema != 2
|
|
27
|
+
|
|
28
|
+
{ policy: policy, schema: schema }.freeze
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def initialize(batch, policy: :strict, duration_ms: nil, report_diagnostic: nil)
|
|
32
|
+
self.class.options(policy: policy, schema: 2)
|
|
33
|
+
raise ArgumentError, "report requires a Batch" unless batch.instance_of?(Batch)
|
|
34
|
+
unless duration_ms.nil? || (duration_ms.is_a?(Numeric) && duration_ms.real? && duration_ms.finite? && duration_ms >= 0)
|
|
35
|
+
raise ArgumentError, "duration_ms must be nonnegative and finite"
|
|
36
|
+
end
|
|
37
|
+
unless report_diagnostic.nil? || report_diagnostic.instance_of?(Diagnostic)
|
|
38
|
+
raise ArgumentError, "report_diagnostic must be a Diagnostic"
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
@batch, @policy, @duration_ms, @report_diagnostic = batch, policy, duration_ms, report_diagnostic
|
|
42
|
+
freeze
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# 旧 success?/status 委托给 Batch;新策略绝不把未知状态当作可忽略跳过。
|
|
46
|
+
def policy_success?
|
|
47
|
+
return batch.success? if policy == :strict
|
|
48
|
+
return false unless callback_errors.empty? && report_error.nil?
|
|
49
|
+
|
|
50
|
+
outcomes.any? { |item| SUCCESS.include?(item.status) } &&
|
|
51
|
+
outcomes.all? { |item| SUCCESS.include?(item.status) || IGNORABLE.include?(item.status) }
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def with(**attributes)
|
|
55
|
+
self.class.new(batch.with(**attributes), policy: policy, duration_ms: duration_ms,
|
|
56
|
+
report_diagnostic: report_diagnostic)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def with_report_error(error, location: nil)
|
|
60
|
+
diagnostic = Diagnostic.from(error, phase: :report)
|
|
61
|
+
self.class.new(batch.with(report_location: location, report_error: diagnostic.error_type),
|
|
62
|
+
policy: policy, duration_ms: duration_ms, report_diagnostic: diagnostic)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# 复用旧业务数据,但重新白名单化所有诊断字段,包括手工构造的旧 Batch。
|
|
66
|
+
def summary
|
|
67
|
+
legacy = batch.summary
|
|
68
|
+
legacy.merge(
|
|
69
|
+
schema_version: 2, policy: policy, policy_success: policy_success?, duration_ms: duration_ms,
|
|
70
|
+
coverage: { complete: !outcomes.empty? && outcomes.all? { |item| ATTEMPTED.include?(item.status) },
|
|
71
|
+
attempted: outcomes.count { |item| ATTEMPTED.include?(item.status) },
|
|
72
|
+
skipped: outcomes.count { |item| !ATTEMPTED.include?(item.status) } },
|
|
73
|
+
devices: device_summaries(legacy.fetch(:devices)),
|
|
74
|
+
callback_errors: callback_errors.map { |entry| { host: entry[:host], error_type: Diagnostic.type(entry[:error_type]) } },
|
|
75
|
+
report_location: report_location, report_error: Diagnostic.type(report_error),
|
|
76
|
+
report_diagnostic: report_diagnostic&.to_h
|
|
77
|
+
)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def inspect = "#<#{self.class} policy=#{policy} status=#{status} policy_success=#{policy_success?}>"
|
|
81
|
+
|
|
82
|
+
private
|
|
83
|
+
|
|
84
|
+
def device_summaries(entries)
|
|
85
|
+
entries.zip(outcomes).map do |entry, item|
|
|
86
|
+
diagnostic = item.diagnostic || Diagnostic.new(error_code: item.error_code, error_type: item.error_type)
|
|
87
|
+
entry.merge(error_code: diagnostic.error_code, error_type: diagnostic.error_type,
|
|
88
|
+
diagnostic: diagnostic.to_h.except(:error_code, :error_type))
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
@@ -12,20 +12,33 @@ module Net
|
|
|
12
12
|
host_overrides: {}, mappings: [])
|
|
13
13
|
@include_hosts = string_list(include_hosts)
|
|
14
14
|
@exclude_hosts = string_list(exclude_hosts)
|
|
15
|
+
@include_vendors = vendor_list(include_vendors)
|
|
16
|
+
@vendor_overrides = vendor_mapping(vendor_overrides)
|
|
17
|
+
@host_overrides = host_mapping(host_overrides)
|
|
18
|
+
@mappings = device_mappings(mappings)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# 校验允许使用的厂商列表。
|
|
22
|
+
def vendor_list(include_vendors)
|
|
15
23
|
raise ArgumentError, "include_vendors must be an Array" unless include_vendors.is_a?(Array)
|
|
16
24
|
|
|
17
|
-
|
|
25
|
+
vendors = include_vendors.map do |vendor|
|
|
18
26
|
raise ArgumentError, "include_vendors contains an invalid value" unless vendor.is_a?(String) || vendor.is_a?(Symbol)
|
|
19
27
|
|
|
20
28
|
vendor.to_sym
|
|
21
29
|
end.freeze
|
|
22
|
-
unless
|
|
30
|
+
unless vendors.all? { |vendor| Net::Connector.vendors.include?(vendor) }
|
|
23
31
|
raise ArgumentError, "include_vendors contains an unsupported connector"
|
|
24
32
|
end
|
|
33
|
+
vendors
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# 将清单厂商标签映射为受支持的连接器。
|
|
37
|
+
def vendor_mapping(vendor_overrides)
|
|
25
38
|
unless vendor_overrides.is_a?(Hash)
|
|
26
39
|
raise ArgumentError, "vendor_overrides must be a Hash"
|
|
27
40
|
end
|
|
28
|
-
|
|
41
|
+
vendor_overrides.to_h do |label, vendor|
|
|
29
42
|
unless label.is_a?(String) && vendor.is_a?(String) && !label.empty?
|
|
30
43
|
raise ArgumentError, "vendor_overrides must map labels to connector names"
|
|
31
44
|
end
|
|
@@ -35,9 +48,13 @@ module Net
|
|
|
35
48
|
|
|
36
49
|
[key, value]
|
|
37
50
|
end.freeze
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# 按规范化管理地址覆盖厂商,拒绝网段和无效地址。
|
|
54
|
+
def host_mapping(host_overrides)
|
|
38
55
|
raise ArgumentError, "host_overrides must be a Hash" unless host_overrides.is_a?(Hash)
|
|
39
56
|
|
|
40
|
-
|
|
57
|
+
host_overrides.to_h do |host, vendor|
|
|
41
58
|
unless host.is_a?(String) && !host.include?("/") && vendor.is_a?(String)
|
|
42
59
|
raise ArgumentError, "host_overrides must map IP addresses to connector names"
|
|
43
60
|
end
|
|
@@ -51,9 +68,13 @@ module Net
|
|
|
51
68
|
|
|
52
69
|
[address, connector]
|
|
53
70
|
end.freeze
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# 型号映射仅接受已声明字段,并冻结每条规则。
|
|
74
|
+
def device_mappings(mappings)
|
|
54
75
|
raise ArgumentError, "mappings must be an Array" unless mappings.is_a?(Array)
|
|
55
76
|
|
|
56
|
-
|
|
77
|
+
mappings.map do |rule|
|
|
57
78
|
unless rule.is_a?(Hash) && rule["vendor"].is_a?(String) && !rule["vendor"].empty? &&
|
|
58
79
|
rule["connector"].is_a?(String) &&
|
|
59
80
|
(rule.keys - %w[vendor os model_prefix connector]).empty? &&
|
|
@@ -68,6 +89,8 @@ module Net
|
|
|
68
89
|
end.freeze
|
|
69
90
|
end
|
|
70
91
|
|
|
92
|
+
private :vendor_list, :vendor_mapping, :host_mapping, :device_mappings
|
|
93
|
+
|
|
71
94
|
# 根据地址、显式规则和厂商信息识别连接器。
|
|
72
95
|
def resolve(row)
|
|
73
96
|
begin
|
|
@@ -1,41 +1,95 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "json"
|
|
4
|
-
require "uri"
|
|
5
4
|
|
|
6
5
|
module Net
|
|
7
6
|
module Connector
|
|
8
7
|
module Netdisco
|
|
9
|
-
#
|
|
8
|
+
# 活的凭据来源与不可变的批次策略分开保存;只有明确列出的非敏感字段进入快照。
|
|
10
9
|
class Settings
|
|
11
10
|
DEFAULT_BACKUP_DIRECTORY = "backups"
|
|
12
11
|
DEFAULT_CONCURRENCY = 4
|
|
13
12
|
MAX_CONCURRENCY = 50
|
|
14
|
-
DEFAULT_PAGE_SIZE =
|
|
13
|
+
DEFAULT_PAGE_SIZE = Client::DEFAULTS.fetch(:page_size)
|
|
14
|
+
UNSET = Object.new.freeze
|
|
15
|
+
private_constant :UNSET
|
|
15
16
|
|
|
16
|
-
# 加载 YAML 配置,并使环境变量覆盖文件配置。
|
|
17
17
|
def self.from_file(path, env: ENV)
|
|
18
|
-
new(env: ConfigFile.load(path)
|
|
18
|
+
new(env: env, defaults: ConfigFile.load(path))
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
@
|
|
21
|
+
def initialize(env: ENV, defaults: {}, overrides: {}, policy: nil, fixed: false)
|
|
22
|
+
@env, @defaults, @overrides = env, defaults, overrides
|
|
23
|
+
@policy, @fixed = policy, fixed
|
|
24
24
|
end
|
|
25
25
|
|
|
26
|
-
#
|
|
27
|
-
def
|
|
28
|
-
|
|
29
|
-
if
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
26
|
+
# 快照自身不保留 ENV、凭据 resolver 或原 Settings 的引用,不能携带秘密进入摘要。
|
|
27
|
+
def snapshot(mode: :backup)
|
|
28
|
+
return @policy.validate!(mode: mode) if @policy
|
|
29
|
+
return validate!(mode: mode) if @fixed
|
|
30
|
+
|
|
31
|
+
keys = mode == :export ? ["NET_CONNECTOR_BACKUP_DIRECTORY"] : non_secret_keys
|
|
32
|
+
values = keys.each_with_object({}) do |key, result|
|
|
33
|
+
item = raw(key, UNSET)
|
|
34
|
+
result[key.freeze] = item.is_a?(String) ? item.dup.freeze : item unless item.equal?(UNSET)
|
|
35
|
+
end
|
|
36
|
+
self.class.new(env: values.freeze, fixed: true).validate!(mode: mode).freeze
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# CLI 的一次预览/执行使用同一策略;凭据仍指向原环境映射,允许逐设备轮换。
|
|
40
|
+
def for_run(mode: :backup)
|
|
41
|
+
self.class.new(env: @env, defaults: @defaults, overrides: @overrides, policy: snapshot(mode: mode))
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def validate!(mode:)
|
|
45
|
+
unless %i[backup tftp inventory show_config export].include?(mode)
|
|
46
|
+
raise ArgumentError, "unknown settings validation mode"
|
|
47
|
+
end
|
|
48
|
+
backup_directory
|
|
49
|
+
return self if mode == :export
|
|
50
|
+
|
|
51
|
+
client_options
|
|
52
|
+
rules
|
|
53
|
+
concurrency
|
|
54
|
+
limit_per_vendor
|
|
55
|
+
log_directory
|
|
56
|
+
connection_options
|
|
57
|
+
Net::Connector.vendors.each { |vendor| connection_options(vendor) }
|
|
58
|
+
tftp_source_files.each_value { |source| TftpTarget.validate_source_file!(source) }
|
|
59
|
+
tftp_vrfs
|
|
60
|
+
TftpTarget.new(host: tftp_server, path: "preflight.cfg") if tftp_server
|
|
61
|
+
self
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def client(policy: snapshot(mode: :inventory))
|
|
65
|
+
options = policy.client_options
|
|
66
|
+
raise ArgumentError, "NETDISCO_URL is required" unless options.fetch(:url)
|
|
67
|
+
|
|
68
|
+
authentication = if (token = secret_value("NETDISCO_API_KEY"))
|
|
69
|
+
{ api_key: token }
|
|
70
|
+
else
|
|
71
|
+
{ username: required_secret("NETDISCO_USERNAME"), password: required_secret("NETDISCO_PASSWORD") }
|
|
72
|
+
end
|
|
73
|
+
Client.new(**options, **authentication)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# 原生客户端和 --show-config 共享同一预算、地址与 HTTP 策略校验。
|
|
77
|
+
def client_options
|
|
78
|
+
options = Client::DEFAULTS.to_h do |name, default|
|
|
79
|
+
key = "NETDISCO_#{name.to_s.upcase}"
|
|
80
|
+
parsed = case name
|
|
81
|
+
when :inventory_timeout then number(key, default)
|
|
82
|
+
when :allow_insecure_http then boolean(key, default)
|
|
83
|
+
else integer(key, default)
|
|
84
|
+
end
|
|
85
|
+
[name, parsed]
|
|
34
86
|
end
|
|
35
|
-
Client.
|
|
87
|
+
options = Client.options(**options)
|
|
88
|
+
url = value("NETDISCO_URL")
|
|
89
|
+
Client.validate_url!(url, allow_insecure_http: options.fetch(:allow_insecure_http)) if url
|
|
90
|
+
options.merge(url: url).freeze
|
|
36
91
|
end
|
|
37
92
|
|
|
38
|
-
# 读取清单筛选和厂商映射规则。
|
|
39
93
|
def rules
|
|
40
94
|
Rules.new(include_hosts: list("NET_CONNECTOR_INCLUDE_HOSTS"),
|
|
41
95
|
exclude_hosts: list("NET_CONNECTOR_EXCLUDE_HOSTS"),
|
|
@@ -45,15 +99,13 @@ module Net
|
|
|
45
99
|
mappings: json("NET_CONNECTOR_DEVICE_RULES", []))
|
|
46
100
|
end
|
|
47
101
|
|
|
48
|
-
# 读取并展开本地备份目录。
|
|
49
102
|
def backup_directory
|
|
50
|
-
path =
|
|
103
|
+
path = raw("NET_CONNECTOR_BACKUP_DIRECTORY", DEFAULT_BACKUP_DIRECTORY)
|
|
51
104
|
raise ArgumentError, "NET_CONNECTOR_BACKUP_DIRECTORY must be nonempty" unless present?(path)
|
|
52
105
|
|
|
53
106
|
File.expand_path(path)
|
|
54
107
|
end
|
|
55
108
|
|
|
56
|
-
# 读取并限制设备任务并发数。
|
|
57
109
|
def concurrency
|
|
58
110
|
count = integer("NET_CONNECTOR_CONCURRENCY", DEFAULT_CONCURRENCY)
|
|
59
111
|
raise ArgumentError, "NET_CONNECTOR_CONCURRENCY must be at most #{MAX_CONCURRENCY}" if count > MAX_CONCURRENCY
|
|
@@ -61,121 +113,178 @@ module Net
|
|
|
61
113
|
count
|
|
62
114
|
end
|
|
63
115
|
|
|
64
|
-
# 确定厂商采样数量:本地默认全量,TFTP 默认每厂商五台。
|
|
65
116
|
def limit_per_vendor(tftp: false)
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
117
|
+
count = if configured?("NET_CONNECTOR_SAMPLE_PER_VENDOR")
|
|
118
|
+
integer("NET_CONNECTOR_SAMPLE_PER_VENDOR", 5) unless raw("NET_CONNECTOR_SAMPLE_PER_VENDOR").nil?
|
|
119
|
+
elsif tftp
|
|
120
|
+
5
|
|
121
|
+
end
|
|
122
|
+
Planner.validate_limit!(count)
|
|
123
|
+
count
|
|
69
124
|
end
|
|
70
125
|
|
|
71
|
-
# 读取厂商的 TFTP 源文件配置,并提供华为启动配置默认路径。
|
|
72
126
|
def tftp_source_files
|
|
73
|
-
files = { huawei:
|
|
127
|
+
files = { huawei: raw("NET_CONNECTOR_HUAWEI_TFTP_SOURCE_FILE", "flash:/startup.cfg") }
|
|
74
128
|
%i[h3c h3c_wireless].each do |vendor|
|
|
75
129
|
key = "NET_CONNECTOR_#{vendor.to_s.upcase}_TFTP_SOURCE_FILE"
|
|
76
|
-
files[vendor] =
|
|
130
|
+
files[vendor] = raw(key) if configured?(key)
|
|
77
131
|
end
|
|
78
132
|
files
|
|
79
133
|
end
|
|
80
134
|
|
|
81
|
-
# 读取各厂商 TFTP 出口 VRF 的名称映射。
|
|
82
135
|
def tftp_vrfs
|
|
83
|
-
|
|
84
|
-
raise ArgumentError, "NET_CONNECTOR_TFTP_VRFS must be a JSON object" unless
|
|
136
|
+
mapping = json("NET_CONNECTOR_TFTP_VRFS", {})
|
|
137
|
+
raise ArgumentError, "NET_CONNECTOR_TFTP_VRFS must be a JSON object" unless mapping.is_a?(Hash)
|
|
138
|
+
|
|
139
|
+
self.class.validate_vrfs!(mapping.transform_keys(&:to_sym))
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def self.validate_vrfs!(vrfs)
|
|
143
|
+
unless vrfs.is_a?(Hash) && vrfs.all? { |vendor, name|
|
|
144
|
+
%i[cisco_nxos hillstone].include?(vendor) && name.is_a?(String) &&
|
|
145
|
+
name.match?(/\A[A-Za-z0-9_][A-Za-z0-9_.-]*\z/)
|
|
146
|
+
}
|
|
147
|
+
raise ArgumentError, "vrfs must map supported vendor names to safe VRF names"
|
|
148
|
+
end
|
|
149
|
+
vrfs
|
|
150
|
+
end
|
|
85
151
|
|
|
86
|
-
|
|
152
|
+
def tftp_server = value("TFTP_HOST")
|
|
153
|
+
|
|
154
|
+
def connection_options(vendor = nil)
|
|
155
|
+
prefix = "NET_CONNECTOR_#{vendor.to_s.upcase}_"
|
|
156
|
+
configuration = Configuration.new(
|
|
157
|
+
protocol: (vendor && value(prefix + "PROTOCOL")) || value("NET_CONNECTOR_PROTOCOL") || "ssh",
|
|
158
|
+
log_level: value("NET_CONNECTOR_LOG_LEVEL") || "info", known_hosts: value("NET_CONNECTOR_KNOWN_HOSTS"),
|
|
159
|
+
host_key_policy: value("NET_CONNECTOR_HOST_KEY_POLICY") || "strict",
|
|
160
|
+
max_script_output_bytes: optional_integer("NET_CONNECTOR_MAX_SCRIPT_OUTPUT_BYTES")
|
|
161
|
+
)
|
|
162
|
+
options = %i[protocol log_level known_hosts host_key_policy].to_h { |key| [key, configuration.public_send(key)] }
|
|
163
|
+
options[:max_script_output_bytes] = configuration.max_script_output_bytes if configuration.max_script_output_bytes
|
|
164
|
+
options.freeze
|
|
87
165
|
end
|
|
88
166
|
|
|
89
|
-
#
|
|
90
|
-
|
|
91
|
-
|
|
167
|
+
# 兼容原入口;Fleet 传入本批策略,独立调用则读取当前策略和当前秘密。
|
|
168
|
+
def credentials_for(device, policy: snapshot(mode: :backup))
|
|
169
|
+
credentials = device_credentials_for(device)
|
|
170
|
+
policy.connection_options(device.vendor).merge(credentials) if credentials
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def device_credentials_for(device)
|
|
92
174
|
prefix = "NET_CONNECTOR_#{device.vendor.to_s.upcase}_"
|
|
93
|
-
username =
|
|
175
|
+
username = secret_value(prefix + "USERNAME") || secret_value("NET_CONNECTOR_DEVICE_USERNAME")
|
|
94
176
|
return nil unless username
|
|
95
177
|
|
|
96
|
-
{
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
enable_password: value(prefix + "ENABLE_PASSWORD") || value("NET_CONNECTOR_ENABLE_PASSWORD"),
|
|
100
|
-
protocol: (value(prefix + "PROTOCOL") || value("NET_CONNECTOR_PROTOCOL") || "ssh").to_sym,
|
|
101
|
-
log_level: (value("NET_CONNECTOR_LOG_LEVEL") || "info").to_sym,
|
|
102
|
-
known_hosts: value("NET_CONNECTOR_KNOWN_HOSTS"),
|
|
103
|
-
host_key_policy: (value("NET_CONNECTOR_HOST_KEY_POLICY") || "strict").to_sym
|
|
104
|
-
}
|
|
178
|
+
{ username: username,
|
|
179
|
+
password: secret_value(prefix + "PASSWORD") || secret_value("NET_CONNECTOR_DEVICE_PASSWORD"),
|
|
180
|
+
enable_password: secret_value(prefix + "ENABLE_PASSWORD") || secret_value("NET_CONNECTOR_ENABLE_PASSWORD") }
|
|
105
181
|
end
|
|
106
182
|
|
|
107
|
-
# 读取并展开设备日志目录。
|
|
108
183
|
def log_directory
|
|
109
184
|
path = value("NET_CONNECTOR_LOG_DIRECTORY")
|
|
110
185
|
File.expand_path(path) if path
|
|
111
186
|
end
|
|
112
187
|
|
|
113
|
-
# 仅展示非敏感配置,不输出设备凭据或 API 密钥。
|
|
114
|
-
# 生成不包含凭据的可展示配置。
|
|
115
188
|
def public_config
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
189
|
+
snapshot(mode: :show_config).send(:config_hash)
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def inspect = "#<#{self.class}>"
|
|
193
|
+
|
|
194
|
+
private
|
|
195
|
+
|
|
196
|
+
def config_hash
|
|
124
197
|
{
|
|
125
|
-
netdisco:
|
|
126
|
-
backup: { directory: backup_directory, concurrency: concurrency,
|
|
127
|
-
limit_per_vendor: limit_per_vendor },
|
|
198
|
+
netdisco: client_options,
|
|
199
|
+
backup: { directory: backup_directory, concurrency: concurrency, limit_per_vendor: limit_per_vendor },
|
|
128
200
|
inventory: { include_hosts: list("NET_CONNECTOR_INCLUDE_HOSTS"),
|
|
129
201
|
exclude_hosts: list("NET_CONNECTOR_EXCLUDE_HOSTS"),
|
|
130
202
|
include_vendors: list("NET_CONNECTOR_INCLUDE_VENDORS"),
|
|
131
203
|
vendor_overrides: json("NET_CONNECTOR_VENDOR_OVERRIDES", {}),
|
|
132
204
|
host_overrides: json("NET_CONNECTOR_HOST_OVERRIDES", {}),
|
|
133
205
|
device_rules: json("NET_CONNECTOR_DEVICE_RULES", []) },
|
|
134
|
-
ssh: {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
206
|
+
ssh: connection_options.transform_values { |item| item.is_a?(Symbol) ? item.to_s : item }.merge(
|
|
207
|
+
log_directory: log_directory,
|
|
208
|
+
vendor_protocols: Net::Connector.vendors.filter_map do |vendor|
|
|
209
|
+
protocol = value("NET_CONNECTOR_#{vendor.to_s.upcase}_PROTOCOL")
|
|
210
|
+
[vendor, protocol] if protocol
|
|
211
|
+
end.to_h
|
|
212
|
+
),
|
|
213
|
+
tftp: { server: tftp_server, vrfs: tftp_vrfs.transform_keys(&:to_s), source_files: tftp_source_files }
|
|
140
214
|
}
|
|
141
|
-
rescue URI::InvalidURIError
|
|
142
|
-
raise ArgumentError, "NETDISCO_URL is invalid"
|
|
143
215
|
end
|
|
144
216
|
|
|
145
|
-
|
|
217
|
+
def non_secret_keys
|
|
218
|
+
keys = ConfigFile::FIELDS.values.flat_map { |section| section.values.map(&:first) }
|
|
219
|
+
keys + Net::Connector.vendors.map { |vendor| "NET_CONNECTOR_#{vendor.to_s.upcase}_PROTOCOL" }
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
def raw(key, default = nil)
|
|
223
|
+
return @policy.send(:raw, key, default) if @policy
|
|
224
|
+
return @overrides.fetch(key) if @overrides.key?(key)
|
|
225
|
+
return @env.fetch(key) if @env.key?(key)
|
|
226
|
+
|
|
227
|
+
@defaults.fetch(key, default)
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
def configured?(key) = !raw(key, UNSET).equal?(UNSET)
|
|
146
231
|
|
|
147
|
-
# 解析指定环境变量中的 JSON 配置。
|
|
148
232
|
def json(key, default)
|
|
149
|
-
|
|
150
|
-
|
|
233
|
+
text = value(key)
|
|
234
|
+
text ? JSON.parse(text) : default
|
|
151
235
|
rescue JSON::ParserError
|
|
152
|
-
raise ArgumentError, "#{key} must be valid JSON"
|
|
236
|
+
raise ArgumentError, "#{key} must be valid JSON", cause: nil
|
|
153
237
|
end
|
|
154
238
|
|
|
155
|
-
|
|
156
|
-
def list(key) = @env.fetch(key, "").split(",").map(&:strip).reject(&:empty?)
|
|
239
|
+
def list(key) = (value(key) || "").split(",").map(&:strip).reject(&:empty?)
|
|
157
240
|
|
|
158
|
-
# 读取正整数配置并统一错误信息。
|
|
159
241
|
def integer(key, default)
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
raise ArgumentError, "#{key} must be a positive integer" unless value.positive?
|
|
242
|
+
result = Integer(raw(key, default).to_s, 10)
|
|
243
|
+
raise ArgumentError unless result.positive?
|
|
163
244
|
|
|
164
|
-
|
|
245
|
+
result
|
|
165
246
|
rescue ArgumentError, TypeError
|
|
166
|
-
raise ArgumentError, "#{key} must be a positive integer"
|
|
247
|
+
raise ArgumentError, "#{key} must be a positive integer", cause: nil
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
def optional_integer(key)
|
|
251
|
+
integer(key, nil) unless raw(key).nil?
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
def number(key, default)
|
|
255
|
+
input = raw(key, default)
|
|
256
|
+
result = input.is_a?(String) ? Float(input) : input
|
|
257
|
+
unless result.is_a?(Numeric) && result.real? && result.finite? && result.positive? && result.to_f.finite?
|
|
258
|
+
raise ArgumentError
|
|
259
|
+
end
|
|
260
|
+
result
|
|
261
|
+
rescue ArgumentError, TypeError
|
|
262
|
+
raise ArgumentError, "#{key} must be a positive finite number", cause: nil
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
def boolean(key, default)
|
|
266
|
+
result = raw(key, default)
|
|
267
|
+
return true if result == true || result == "true"
|
|
268
|
+
return false if result == false || result == "false"
|
|
269
|
+
|
|
270
|
+
raise ArgumentError, "#{key} must be true or false"
|
|
167
271
|
end
|
|
168
272
|
|
|
169
|
-
# 读取非空字符串配置。
|
|
170
273
|
def value(key)
|
|
171
|
-
result =
|
|
274
|
+
result = raw(key)
|
|
275
|
+
raise ArgumentError, "#{key} must be a String" unless result.nil? || result.is_a?(String)
|
|
276
|
+
|
|
172
277
|
result if present?(result)
|
|
173
278
|
end
|
|
174
279
|
|
|
175
|
-
|
|
176
|
-
|
|
280
|
+
def secret_value(key)
|
|
281
|
+
result = @env[key]
|
|
282
|
+
raise ArgumentError, "#{key} must be a String" unless result.nil? || result.is_a?(String)
|
|
283
|
+
|
|
284
|
+
result.dup.freeze if present?(result)
|
|
285
|
+
end
|
|
177
286
|
|
|
178
|
-
#
|
|
287
|
+
def required_secret(key) = secret_value(key) || (raise ArgumentError, "#{key} is required")
|
|
179
288
|
def present?(value) = value.is_a?(String) && !value.strip.empty?
|
|
180
289
|
end
|
|
181
290
|
end
|