glazed 0.1.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 +7 -0
- data/.rspec +3 -0
- data/.yardopts +5 -0
- data/CHANGELOG.md +18 -0
- data/LICENSE.txt +202 -0
- data/NOTICE +16 -0
- data/README.md +3 -0
- data/Rakefile +12 -0
- data/exe/glazed +8 -0
- data/lib/glazed/cli_session.rb +178 -0
- data/lib/glazed/collector.rb +113 -0
- data/lib/glazed/command/base.rb +146 -0
- data/lib/glazed/command/orchestrate.rb +78 -0
- data/lib/glazed/command/run.rb +51 -0
- data/lib/glazed/command/send_signal.rb +69 -0
- data/lib/glazed/command/status.rb +179 -0
- data/lib/glazed/command/version.rb +23 -0
- data/lib/glazed/command.rb +39 -0
- data/lib/glazed/command_result.rb +26 -0
- data/lib/glazed/dialect/actions.rb +95 -0
- data/lib/glazed/dialect/communication_config.rb +63 -0
- data/lib/glazed/dialect/interaction.rb +32 -0
- data/lib/glazed/dialect/run.rb +84 -0
- data/lib/glazed/dialect/runbook.rb +97 -0
- data/lib/glazed/dialect/sensitive_pattern.rb +31 -0
- data/lib/glazed/dialect.rb +12 -0
- data/lib/glazed/dialects/awplus.rb +84 -0
- data/lib/glazed/dialects/base.rb +246 -0
- data/lib/glazed/dialects/ios.rb +173 -0
- data/lib/glazed/dialects/junos.rb +78 -0
- data/lib/glazed/dialects/nec_ix.rb +71 -0
- data/lib/glazed/dialects/wlc.rb +56 -0
- data/lib/glazed/dialects.rb +65 -0
- data/lib/glazed/finder.rb +62 -0
- data/lib/glazed/must.rb +54 -0
- data/lib/glazed/orchestrator/policy.rb +184 -0
- data/lib/glazed/orchestrator.rb +288 -0
- data/lib/glazed/plugin_registry.rb +70 -0
- data/lib/glazed/proto.rb +20 -0
- data/lib/glazed/proto_json.rb +34 -0
- data/lib/glazed/redactor.rb +158 -0
- data/lib/glazed/runner.rb +176 -0
- data/lib/glazed/secret.rb +74 -0
- data/lib/glazed/secrets.rb +59 -0
- data/lib/glazed/shell_command.rb +90 -0
- data/lib/glazed/sources/base.rb +79 -0
- data/lib/glazed/sources/command.rb +67 -0
- data/lib/glazed/sources/inline.rb +56 -0
- data/lib/glazed/sources/json_files.rb +87 -0
- data/lib/glazed/sources.rb +105 -0
- data/lib/glazed/state_backends/base.rb +72 -0
- data/lib/glazed/state_backends/sqlite.rb +299 -0
- data/lib/glazed/state_backends.rb +75 -0
- data/lib/glazed/step_context.rb +65 -0
- data/lib/glazed/steps/base.rb +89 -0
- data/lib/glazed/steps/batch_shell.rb +57 -0
- data/lib/glazed/steps/fetch.rb +67 -0
- data/lib/glazed/steps/publish.rb +94 -0
- data/lib/glazed/steps/redact.rb +77 -0
- data/lib/glazed/steps/shell.rb +107 -0
- data/lib/glazed/steps.rb +83 -0
- data/lib/glazed/storages/base.rb +167 -0
- data/lib/glazed/storages/filesystem.rb +144 -0
- data/lib/glazed/storages/git.rb +181 -0
- data/lib/glazed/storages/github.rb +269 -0
- data/lib/glazed/storages/memory.rb +125 -0
- data/lib/glazed/storages.rb +88 -0
- data/lib/glazed/transports/base.rb +103 -0
- data/lib/glazed/transports/ssh.rb +177 -0
- data/lib/glazed/transports/telnet.rb +100 -0
- data/lib/glazed/transports.rb +28 -0
- data/lib/glazed/v1/config_pb.rb +25 -0
- data/lib/glazed/v1/inventory_pb.rb +27 -0
- data/lib/glazed/v1/orchestrator_pb.rb +27 -0
- data/lib/glazed/v1/redaction_pb.rb +23 -0
- data/lib/glazed/v1/secret_pb.rb +22 -0
- data/lib/glazed/v1/state_pb.rb +25 -0
- data/lib/glazed/v1/steps_pb.rb +25 -0
- data/lib/glazed/v1/storage_pb.rb +34 -0
- data/lib/glazed/v1/workflow_pb.rb +29 -0
- data/lib/glazed/validation.rb +31 -0
- data/lib/glazed/version.rb +9 -0
- data/lib/glazed/workflow.rb +70 -0
- data/lib/glazed.rb +18 -0
- metadata +245 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: fc8ac8baaf98756ff8b05fd712251274f27ec1c93bd02b980c528a5e8e059970
|
|
4
|
+
data.tar.gz: db6f2becb3b6d74ad1e44f8f8f385641e9732e92d54cf536130150988bac395b
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 64eea926af805078a1e5e0a4653839e2c5ef5207f5515d1a8bfeb464f8ff2dbfda58ca6d25dc596c179ae8e9594f793f1548e10606b0bd2005ca087a735fc6c9
|
|
7
|
+
data.tar.gz: fe504897bf58461f509b0ca08e619661c52e04a9c75d61f4349d97d98679cb0f95ccdce73ad5216adfb7b1492f40f886bc14fc137c6ec11bf86be2982aacd41d
|
data/.rspec
ADDED
data/.yardopts
ADDED
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
## [0.1.0] - 2026-10-14
|
|
2
|
+
|
|
3
|
+
Initial release.
|
|
4
|
+
|
|
5
|
+
### Enhancements
|
|
6
|
+
|
|
7
|
+
- Collect configuration over SSH or Telnet, with dialects for Cisco IOS/IOS XE, Cisco WLC (AireOS), Juniper Junos, NEC UNIVERGE IX and Allied Telesis AlliedWare Plus, ported from Oxidized
|
|
8
|
+
- Class-based dialect DSL with inheritance ([docs/dialect-api.md](../docs/dialect-api.md))
|
|
9
|
+
- Redaction of the secrets a dialect locates: replaced with `[~REDACTED~]`, or with `[~REDACTED:dgst=…~]` carrying a keyed HMAC so changed secrets still show in diffs
|
|
10
|
+
- Storages: a local git repository, and a GitHub repository branch through the Git Data API. Unchanged content is not written again, and every write reports whether it changed
|
|
11
|
+
- Inventory from JSON files, the config itself, or a command printing it, through pluggable inventory providers
|
|
12
|
+
- Workflows of `fetch`, `redact` and `publish` steps passing artifacts between named storages, defined as protobuf messages (`glazed.v1`) so external orchestrators can run them
|
|
13
|
+
- `glazed run` command, running a workflow in-process
|
|
14
|
+
- `GLAZED_CONFIG_FILE` names the config file when `-c` is omitted
|
|
15
|
+
- `glazed orchestrate` command, running workflows for each node on their own intervals and after signals with trailing debounce, with state in a pluggable state backend ([docs/orchestrator.md](../docs/orchestrator.md))
|
|
16
|
+
- `glazed signal` command, making nodes due soon from scripts or notification receivers
|
|
17
|
+
- `glazed status` command, showing when each node runs next and how its last run went, exiting 1 when one is failing or overdue
|
|
18
|
+
- `sqlite` state backend, kept in a local file or in any storage supporting conditional writes (`filesystem`, `memory`, glazed-aws's `s3`)
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
data/NOTICE
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
glazed
|
|
2
|
+
Copyright 2025-2026 Sorah Fukumori
|
|
3
|
+
|
|
4
|
+
This product includes software derived from Oxidized
|
|
5
|
+
(https://github.com/ytti/oxidized), licensed under the Apache License,
|
|
6
|
+
Version 2.0:
|
|
7
|
+
|
|
8
|
+
Copyright 2013-2015 Saku Ytti <saku@ytti.fi>
|
|
9
|
+
Copyright 2013-2015 Samer Abdel-Hafez <sam@arahant.net>
|
|
10
|
+
and Oxidized contributors
|
|
11
|
+
|
|
12
|
+
Derived from Oxidized as of commit d7f51c2, and modified for glazed:
|
|
13
|
+
|
|
14
|
+
- lib/glazed/dialects/{awplus,ios,junos,nec_ix,wlc}.rb
|
|
15
|
+
Prompts, command sequences, output cleanup and secret patterns ported
|
|
16
|
+
from lib/oxidized/model/{awplus,ios,junos,necix,aireos}.rb.
|
data/README.md
ADDED
data/Rakefile
ADDED
data/exe/glazed
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# typed: strict
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
|
|
6
|
+
require 'glazed'
|
|
7
|
+
require 'glazed/command_result'
|
|
8
|
+
require 'glazed/dialect/actions'
|
|
9
|
+
require 'glazed/dialect/interaction'
|
|
10
|
+
require 'glazed/transports/base'
|
|
11
|
+
|
|
12
|
+
module Glazed
|
|
13
|
+
# Interactive command-line session over a transport, driven by a prompt
|
|
14
|
+
# pattern, data filters and interactions.
|
|
15
|
+
#
|
|
16
|
+
# Received data is kept as bytes (ASCII-8BIT), so a character split across
|
|
17
|
+
# reads cannot break matching. It is filtered fragment by fragment, appended to {#buffer}, and
|
|
18
|
+
# then offered to each interaction. Text up to the end of a match is consumed
|
|
19
|
+
# by {#expect} and {#command}.
|
|
20
|
+
#
|
|
21
|
+
# @example
|
|
22
|
+
# session = Glazed::CliSession.new(transport:, prompt: /^\S+#\s?$/)
|
|
23
|
+
# session.command('show version').output
|
|
24
|
+
class CliSession
|
|
25
|
+
# Default seconds to wait for expected output.
|
|
26
|
+
DEFAULT_TIMEOUT = 60 #: Integer
|
|
27
|
+
# Longest single wait on the transport, so a deadline is checked regularly.
|
|
28
|
+
READ_SLICE = 1.0 #: Float
|
|
29
|
+
|
|
30
|
+
# @param transport [Transports::Base] open transport
|
|
31
|
+
# @param prompt [Regexp] pattern of the command prompt
|
|
32
|
+
# @param data_filters [Array<Proc>] filters applied to each received fragment, in order
|
|
33
|
+
# @param interactions [Array<Dialect::Interaction>] responders to patterns in the buffer
|
|
34
|
+
# @param exec [Boolean] run {#command} through {Transports::Base#exec} when supported
|
|
35
|
+
# @param timeout [Numeric] default seconds to wait for expected output
|
|
36
|
+
# @param progress [Proc] receives a message whenever the session starts waiting for the device
|
|
37
|
+
#: (transport: Transports::Base, prompt: Regexp, ?data_filters: Array[Dialect::data_filter], ?interactions: Array[Dialect::Interaction], ?exec: bool, ?timeout: Numeric, ?progress: ^(String) -> void) -> void
|
|
38
|
+
def initialize(transport:, prompt:, data_filters: [], interactions: [], exec: false, timeout: DEFAULT_TIMEOUT, progress: ->(_message) {})
|
|
39
|
+
@transport = transport #: Transports::Base
|
|
40
|
+
@prompt = prompt #: Regexp
|
|
41
|
+
@data_filters = data_filters #: Array[Dialect::data_filter]
|
|
42
|
+
@interactions = interactions #: Array[Dialect::Interaction]
|
|
43
|
+
@exec = exec #: bool
|
|
44
|
+
@timeout = timeout #: Numeric
|
|
45
|
+
@progress = progress #: ^(String) -> void
|
|
46
|
+
@buffer = String.new(encoding: Encoding::BINARY) #: String
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# @return [Transports::Base] underlying transport
|
|
50
|
+
#: Transports::Base
|
|
51
|
+
attr_reader :transport
|
|
52
|
+
# @return [Regexp] pattern of the command prompt
|
|
53
|
+
#: Regexp
|
|
54
|
+
attr_reader :prompt
|
|
55
|
+
# @return [Numeric] default seconds to wait for expected output
|
|
56
|
+
#: Numeric
|
|
57
|
+
attr_reader :timeout
|
|
58
|
+
# @return [String] received text not yet consumed
|
|
59
|
+
#: String
|
|
60
|
+
attr_reader :buffer
|
|
61
|
+
|
|
62
|
+
# Sends raw data.
|
|
63
|
+
#
|
|
64
|
+
# @param data [String] bytes to send, including any newline
|
|
65
|
+
# @return [void]
|
|
66
|
+
#: (String data) -> void
|
|
67
|
+
def write(data)
|
|
68
|
+
transport.write(data)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Waits until a pattern matches the buffer, then consumes the buffer up to
|
|
72
|
+
# the end of the match.
|
|
73
|
+
#
|
|
74
|
+
# @param patterns [Array<Regexp>] patterns tried in order
|
|
75
|
+
# @param timeout [Numeric] seconds to wait
|
|
76
|
+
# @return [MatchData] the first match
|
|
77
|
+
# @raise [TimeoutError] when nothing matches in time
|
|
78
|
+
# @raise [ConnectionClosed] when the device closes the connection first
|
|
79
|
+
#: (*Regexp patterns, ?timeout: Numeric) -> MatchData
|
|
80
|
+
def expect(*patterns, timeout: self.timeout)
|
|
81
|
+
description = patterns.map { it == prompt ? 'prompt' : it.inspect }.join(' or ')
|
|
82
|
+
match = wait_until(description, timeout:) { patterns.lazy.filter_map { it.match(buffer) }.first }
|
|
83
|
+
consume(match)
|
|
84
|
+
match
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# Runs a command and waits for the prompt.
|
|
88
|
+
#
|
|
89
|
+
# @param line [String] command line, without newline
|
|
90
|
+
# @param timeout [Numeric] seconds to wait for the prompt
|
|
91
|
+
# @return [CommandResult] output without the echoed command and the prompt
|
|
92
|
+
# @raise [TimeoutError] when the prompt does not appear in time
|
|
93
|
+
# @raise [ConnectionClosed] when the device closes the connection first
|
|
94
|
+
#: (String line, ?timeout: Numeric) -> CommandResult
|
|
95
|
+
def command(line, timeout: self.timeout)
|
|
96
|
+
if @exec && transport.exec?
|
|
97
|
+
@progress.call("waiting for output of #{line.inspect}")
|
|
98
|
+
return CommandResult.new(command: line, output: filter(transport.exec(line, timeout:).b))
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
discard
|
|
102
|
+
write("#{line}\n")
|
|
103
|
+
# The prompt is searched only after the echoed line, so a prompt left over
|
|
104
|
+
# from before the command cannot end it early.
|
|
105
|
+
match = wait_until("prompt after #{line.inspect}", timeout:) do
|
|
106
|
+
echo_end = buffer.index("\n")
|
|
107
|
+
prompt.match(buffer, echo_end + 1) if echo_end
|
|
108
|
+
end
|
|
109
|
+
output_start = (buffer.index("\n") || -1) + 1
|
|
110
|
+
output = buffer[output_start...match.begin(0)] || String.new(encoding: Encoding::BINARY)
|
|
111
|
+
consume(match)
|
|
112
|
+
CommandResult.new(command: line, output:)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Clears the buffer.
|
|
116
|
+
#
|
|
117
|
+
# @return [String] the discarded text
|
|
118
|
+
#: () -> String
|
|
119
|
+
def discard
|
|
120
|
+
discarded = @buffer
|
|
121
|
+
@buffer = String.new(encoding: Encoding::BINARY)
|
|
122
|
+
discarded
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# @param description [String] what is awaited, for error messages
|
|
126
|
+
# @param timeout [Numeric] seconds to wait
|
|
127
|
+
# @yieldreturn [MatchData, nil] match against the current buffer, or nil to keep waiting
|
|
128
|
+
# @return [MatchData] the first non-nil match
|
|
129
|
+
# @raise [TimeoutError] when nothing matches in time
|
|
130
|
+
# @raise [ConnectionClosed] when the device closes the connection first
|
|
131
|
+
#: (String description, timeout: Numeric) { () -> MatchData? } -> MatchData
|
|
132
|
+
private def wait_until(description, timeout:, &matcher)
|
|
133
|
+
@progress.call("waiting for #{description}")
|
|
134
|
+
deadline = now + timeout.to_f
|
|
135
|
+
loop do
|
|
136
|
+
match = matcher.call
|
|
137
|
+
return match if match
|
|
138
|
+
|
|
139
|
+
remaining = deadline - now
|
|
140
|
+
raise TimeoutError, "timed out after #{timeout}s waiting for #{description}; buffer tail: #{(buffer[-200..] || buffer).dump}" unless remaining.positive?
|
|
141
|
+
|
|
142
|
+
data = transport.read(timeout: [remaining, READ_SLICE].min)
|
|
143
|
+
raise ConnectionClosed, "connection closed while waiting for #{description}" if data.nil?
|
|
144
|
+
|
|
145
|
+
feed(data) unless data.empty?
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# @param data [String] received fragment
|
|
150
|
+
# @return [void]
|
|
151
|
+
#: (String data) -> void
|
|
152
|
+
private def feed(data)
|
|
153
|
+
@buffer << filter(data.b).b
|
|
154
|
+
@interactions.each do |interaction|
|
|
155
|
+
match = interaction.pattern.match(@buffer)
|
|
156
|
+
@buffer = Dialect::Actions.apply(@buffer, interaction.responder.call(self, @buffer.dup, match)) if match
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# @param data [String] received text
|
|
161
|
+
# @return [String] text after every data filter
|
|
162
|
+
#: (String data) -> String
|
|
163
|
+
private def filter(data)
|
|
164
|
+
@data_filters.inject(data.dup) { |result, data_filter| data_filter.call(result) }
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# @param match [MatchData] match against the buffer
|
|
168
|
+
# @return [void]
|
|
169
|
+
#: (MatchData match) -> void
|
|
170
|
+
private def consume(match)
|
|
171
|
+
@buffer = buffer[match.end(0)..] || String.new(encoding: Encoding::BINARY)
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
# @return [Float] monotonic clock reading in seconds
|
|
175
|
+
#: () -> Float
|
|
176
|
+
private def now = Process.clock_gettime(Process::CLOCK_MONOTONIC).to_f
|
|
177
|
+
end
|
|
178
|
+
end
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# typed: strict
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
|
|
6
|
+
require 'glazed'
|
|
7
|
+
require 'glazed/cli_session'
|
|
8
|
+
require 'glazed/dialect/run'
|
|
9
|
+
require 'glazed/dialects'
|
|
10
|
+
require 'glazed/proto'
|
|
11
|
+
require 'glazed/secret'
|
|
12
|
+
require 'glazed/transports'
|
|
13
|
+
|
|
14
|
+
module Glazed
|
|
15
|
+
# Collects one node's configuration: connects, logs in, runs the dialect's
|
|
16
|
+
# :get runbook, and logs out.
|
|
17
|
+
#
|
|
18
|
+
# @example
|
|
19
|
+
# config = Glazed::Collector.new(node:).call
|
|
20
|
+
class Collector
|
|
21
|
+
# Transport used when a node names none.
|
|
22
|
+
DEFAULT_TRANSPORT = 'ssh' #: String
|
|
23
|
+
|
|
24
|
+
# @param node [Proto::V1::Node] node to collect from
|
|
25
|
+
# @param transport [Transports::Base, nil] transport to use instead of building one from the node
|
|
26
|
+
# @param timeout [Numeric] seconds to wait for each expected output
|
|
27
|
+
# @param progress [Proc] receives a message when connecting and whenever waiting for the device
|
|
28
|
+
#: (node: Proto::V1::Node, ?transport: Transports::Base?, ?timeout: Numeric, ?progress: ^(String) -> void) -> void
|
|
29
|
+
def initialize(node:, transport: nil, timeout: CliSession::DEFAULT_TIMEOUT, progress: ->(_message) {})
|
|
30
|
+
@node = node #: Proto::V1::Node
|
|
31
|
+
@dialect = Dialects.find(node.dialect) #: singleton(Dialects::Base)
|
|
32
|
+
@kind = (node.transport.empty? ? DEFAULT_TRANSPORT : node.transport) #: String
|
|
33
|
+
@transport = transport || build_transport #: Transports::Base
|
|
34
|
+
@config = @dialect.communication_config(@kind.to_sym) #: Dialect::CommunicationConfig
|
|
35
|
+
@timeout = timeout #: Numeric
|
|
36
|
+
@progress = progress #: ^(String) -> void
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# @return [Proto::V1::Node] node to collect from
|
|
40
|
+
#: Proto::V1::Node
|
|
41
|
+
attr_reader :node
|
|
42
|
+
# @return [Class<Dialects::Base>] dialect of the node
|
|
43
|
+
#: singleton(Dialects::Base)
|
|
44
|
+
attr_reader :dialect
|
|
45
|
+
|
|
46
|
+
# Collects the configuration. The connection is closed even on failure.
|
|
47
|
+
#
|
|
48
|
+
# @return [String] the configuration, in UTF-8
|
|
49
|
+
# @raise [TimeoutError] when the device does not answer as the dialect expects
|
|
50
|
+
# @raise [ConnectionClosed] when the device drops the connection
|
|
51
|
+
# @raise [InvalidEncoding] when the configuration is not valid UTF-8
|
|
52
|
+
#: () -> String
|
|
53
|
+
def call
|
|
54
|
+
session = CliSession.new(
|
|
55
|
+
transport: @transport,
|
|
56
|
+
prompt: dialect.prompt_pattern,
|
|
57
|
+
data_filters: dialect.data_filters,
|
|
58
|
+
interactions: dialect.interactions,
|
|
59
|
+
exec: @config.exec,
|
|
60
|
+
timeout: @timeout,
|
|
61
|
+
progress: @progress,
|
|
62
|
+
)
|
|
63
|
+
run = Dialect::Run.new(cli: session, node:, dialect:)
|
|
64
|
+
@transport.pty_options = @config.pty_options
|
|
65
|
+
@progress.call("connecting to #{@transport.host} over #{@kind}")
|
|
66
|
+
@transport.open
|
|
67
|
+
begin
|
|
68
|
+
login(session) unless exec_only?
|
|
69
|
+
@config.post_login_hooks.each { it.call(run) }
|
|
70
|
+
output = dialect.runbook_for(:get).execute(run).dup.force_encoding(Encoding::UTF_8)
|
|
71
|
+
raise InvalidEncoding, "#{node.name}: collected configuration is not valid UTF-8" unless output.valid_encoding?
|
|
72
|
+
|
|
73
|
+
@config.pre_logout_hooks.each { it.call(run) }
|
|
74
|
+
output
|
|
75
|
+
ensure
|
|
76
|
+
@transport.close
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# @return [Boolean] whether every command runs on exec channels, so the shell is never used
|
|
81
|
+
#: () -> bool
|
|
82
|
+
private def exec_only? = @config.exec && @transport.exec?
|
|
83
|
+
|
|
84
|
+
# Answers login prompts the dialect declares, then waits for the first prompt.
|
|
85
|
+
#
|
|
86
|
+
# @param session [CliSession] session to log in through
|
|
87
|
+
# @return [void]
|
|
88
|
+
#: (CliSession session) -> void
|
|
89
|
+
private def login(session)
|
|
90
|
+
if (username_prompt = @config.username_prompt)
|
|
91
|
+
session.expect(username_prompt)
|
|
92
|
+
session.write("#{node.username}\n")
|
|
93
|
+
end
|
|
94
|
+
if (password_prompt = @config.password_prompt)
|
|
95
|
+
session.expect(password_prompt)
|
|
96
|
+
session.write("#{Secret.maybe(node.password)&.value}\n")
|
|
97
|
+
end
|
|
98
|
+
session.expect(session.prompt)
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# @return [Transports::Base] transport built from the node
|
|
102
|
+
# @raise [Finder::NotFound] when the transport is unknown
|
|
103
|
+
#: () -> Transports::Base
|
|
104
|
+
private def build_transport
|
|
105
|
+
Transports.find(@kind).new(
|
|
106
|
+
host: node.host.empty? ? node.name : node.host,
|
|
107
|
+
port: node.port.zero? ? nil : node.port,
|
|
108
|
+
username: node.username,
|
|
109
|
+
password: Secret.maybe(node.password),
|
|
110
|
+
)
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|