fqdn_facts 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +340 -0
- data/README.md +164 -0
- data/Rakefile +7 -0
- data/fqdn_facts.gemspec +24 -0
- data/lib/fqdn_facts/base_handler.rb +279 -0
- data/lib/fqdn_facts/core_ext.rb +55 -0
- data/lib/fqdn_facts/errors.rb +28 -0
- data/lib/fqdn_facts/version.rb +22 -0
- data/lib/fqdn_facts.rb +81 -0
- data/spec/fqdn_facts_spec.rb +61 -0
- data/spec/spec_helper.rb +2 -0
- metadata +105 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: df9ca38b346ae900a649ac39a6815d26643ff556
|
4
|
+
data.tar.gz: 1f629c750963efc5f7020baaa237f359e51ad7ae
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2b39dc3811576c37969b974bf723578d99bce42897e9f79f4ae2d9f3f4fb5d1ac6426f66c65bdb28c57295a64048eaccf62cb0abc383324ddde1212bafaf9433
|
7
|
+
data.tar.gz: 2218beac65bf4292abde592da849aece27ee2926715317af70cf5351919315ea7cd1c17d6508335ada84d6f1304ba99e1c3363c9b07522e1586d51bb775d54f9
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,340 @@
|
|
1
|
+
GNU GENERAL PUBLIC LICENSE
|
2
|
+
Version 2, June 1991
|
3
|
+
|
4
|
+
Copyright (C) 1989, 1991 Free Software Foundation, Inc., <http://fsf.org/>
|
5
|
+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
6
|
+
Everyone is permitted to copy and distribute verbatim copies
|
7
|
+
of this license document, but changing it is not allowed.
|
8
|
+
|
9
|
+
Preamble
|
10
|
+
|
11
|
+
The licenses for most software are designed to take away your
|
12
|
+
freedom to share and change it. By contrast, the GNU General Public
|
13
|
+
License is intended to guarantee your freedom to share and change free
|
14
|
+
software--to make sure the software is free for all its users. This
|
15
|
+
General Public License applies to most of the Free Software
|
16
|
+
Foundation's software and to any other program whose authors commit to
|
17
|
+
using it. (Some other Free Software Foundation software is covered by
|
18
|
+
the GNU Lesser General Public License instead.) You can apply it to
|
19
|
+
your programs, too.
|
20
|
+
|
21
|
+
When we speak of free software, we are referring to freedom, not
|
22
|
+
price. Our General Public Licenses are designed to make sure that you
|
23
|
+
have the freedom to distribute copies of free software (and charge for
|
24
|
+
this service if you wish), that you receive source code or can get it
|
25
|
+
if you want it, that you can change the software or use pieces of it
|
26
|
+
in new free programs; and that you know you can do these things.
|
27
|
+
|
28
|
+
To protect your rights, we need to make restrictions that forbid
|
29
|
+
anyone to deny you these rights or to ask you to surrender the rights.
|
30
|
+
These restrictions translate to certain responsibilities for you if you
|
31
|
+
distribute copies of the software, or if you modify it.
|
32
|
+
|
33
|
+
For example, if you distribute copies of such a program, whether
|
34
|
+
gratis or for a fee, you must give the recipients all the rights that
|
35
|
+
you have. You must make sure that they, too, receive or can get the
|
36
|
+
source code. And you must show them these terms so they know their
|
37
|
+
rights.
|
38
|
+
|
39
|
+
We protect your rights with two steps: (1) copyright the software, and
|
40
|
+
(2) offer you this license which gives you legal permission to copy,
|
41
|
+
distribute and/or modify the software.
|
42
|
+
|
43
|
+
Also, for each author's protection and ours, we want to make certain
|
44
|
+
that everyone understands that there is no warranty for this free
|
45
|
+
software. If the software is modified by someone else and passed on, we
|
46
|
+
want its recipients to know that what they have is not the original, so
|
47
|
+
that any problems introduced by others will not reflect on the original
|
48
|
+
authors' reputations.
|
49
|
+
|
50
|
+
Finally, any free program is threatened constantly by software
|
51
|
+
patents. We wish to avoid the danger that redistributors of a free
|
52
|
+
program will individually obtain patent licenses, in effect making the
|
53
|
+
program proprietary. To prevent this, we have made it clear that any
|
54
|
+
patent must be licensed for everyone's free use or not licensed at all.
|
55
|
+
|
56
|
+
The precise terms and conditions for copying, distribution and
|
57
|
+
modification follow.
|
58
|
+
|
59
|
+
GNU GENERAL PUBLIC LICENSE
|
60
|
+
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
61
|
+
|
62
|
+
0. This License applies to any program or other work which contains
|
63
|
+
a notice placed by the copyright holder saying it may be distributed
|
64
|
+
under the terms of this General Public License. The "Program", below,
|
65
|
+
refers to any such program or work, and a "work based on the Program"
|
66
|
+
means either the Program or any derivative work under copyright law:
|
67
|
+
that is to say, a work containing the Program or a portion of it,
|
68
|
+
either verbatim or with modifications and/or translated into another
|
69
|
+
language. (Hereinafter, translation is included without limitation in
|
70
|
+
the term "modification".) Each licensee is addressed as "you".
|
71
|
+
|
72
|
+
Activities other than copying, distribution and modification are not
|
73
|
+
covered by this License; they are outside its scope. The act of
|
74
|
+
running the Program is not restricted, and the output from the Program
|
75
|
+
is covered only if its contents constitute a work based on the
|
76
|
+
Program (independent of having been made by running the Program).
|
77
|
+
Whether that is true depends on what the Program does.
|
78
|
+
|
79
|
+
1. You may copy and distribute verbatim copies of the Program's
|
80
|
+
source code as you receive it, in any medium, provided that you
|
81
|
+
conspicuously and appropriately publish on each copy an appropriate
|
82
|
+
copyright notice and disclaimer of warranty; keep intact all the
|
83
|
+
notices that refer to this License and to the absence of any warranty;
|
84
|
+
and give any other recipients of the Program a copy of this License
|
85
|
+
along with the Program.
|
86
|
+
|
87
|
+
You may charge a fee for the physical act of transferring a copy, and
|
88
|
+
you may at your option offer warranty protection in exchange for a fee.
|
89
|
+
|
90
|
+
2. You may modify your copy or copies of the Program or any portion
|
91
|
+
of it, thus forming a work based on the Program, and copy and
|
92
|
+
distribute such modifications or work under the terms of Section 1
|
93
|
+
above, provided that you also meet all of these conditions:
|
94
|
+
|
95
|
+
a) You must cause the modified files to carry prominent notices
|
96
|
+
stating that you changed the files and the date of any change.
|
97
|
+
|
98
|
+
b) You must cause any work that you distribute or publish, that in
|
99
|
+
whole or in part contains or is derived from the Program or any
|
100
|
+
part thereof, to be licensed as a whole at no charge to all third
|
101
|
+
parties under the terms of this License.
|
102
|
+
|
103
|
+
c) If the modified program normally reads commands interactively
|
104
|
+
when run, you must cause it, when started running for such
|
105
|
+
interactive use in the most ordinary way, to print or display an
|
106
|
+
announcement including an appropriate copyright notice and a
|
107
|
+
notice that there is no warranty (or else, saying that you provide
|
108
|
+
a warranty) and that users may redistribute the program under
|
109
|
+
these conditions, and telling the user how to view a copy of this
|
110
|
+
License. (Exception: if the Program itself is interactive but
|
111
|
+
does not normally print such an announcement, your work based on
|
112
|
+
the Program is not required to print an announcement.)
|
113
|
+
|
114
|
+
These requirements apply to the modified work as a whole. If
|
115
|
+
identifiable sections of that work are not derived from the Program,
|
116
|
+
and can be reasonably considered independent and separate works in
|
117
|
+
themselves, then this License, and its terms, do not apply to those
|
118
|
+
sections when you distribute them as separate works. But when you
|
119
|
+
distribute the same sections as part of a whole which is a work based
|
120
|
+
on the Program, the distribution of the whole must be on the terms of
|
121
|
+
this License, whose permissions for other licensees extend to the
|
122
|
+
entire whole, and thus to each and every part regardless of who wrote it.
|
123
|
+
|
124
|
+
Thus, it is not the intent of this section to claim rights or contest
|
125
|
+
your rights to work written entirely by you; rather, the intent is to
|
126
|
+
exercise the right to control the distribution of derivative or
|
127
|
+
collective works based on the Program.
|
128
|
+
|
129
|
+
In addition, mere aggregation of another work not based on the Program
|
130
|
+
with the Program (or with a work based on the Program) on a volume of
|
131
|
+
a storage or distribution medium does not bring the other work under
|
132
|
+
the scope of this License.
|
133
|
+
|
134
|
+
3. You may copy and distribute the Program (or a work based on it,
|
135
|
+
under Section 2) in object code or executable form under the terms of
|
136
|
+
Sections 1 and 2 above provided that you also do one of the following:
|
137
|
+
|
138
|
+
a) Accompany it with the complete corresponding machine-readable
|
139
|
+
source code, which must be distributed under the terms of Sections
|
140
|
+
1 and 2 above on a medium customarily used for software interchange; or,
|
141
|
+
|
142
|
+
b) Accompany it with a written offer, valid for at least three
|
143
|
+
years, to give any third party, for a charge no more than your
|
144
|
+
cost of physically performing source distribution, a complete
|
145
|
+
machine-readable copy of the corresponding source code, to be
|
146
|
+
distributed under the terms of Sections 1 and 2 above on a medium
|
147
|
+
customarily used for software interchange; or,
|
148
|
+
|
149
|
+
c) Accompany it with the information you received as to the offer
|
150
|
+
to distribute corresponding source code. (This alternative is
|
151
|
+
allowed only for noncommercial distribution and only if you
|
152
|
+
received the program in object code or executable form with such
|
153
|
+
an offer, in accord with Subsection b above.)
|
154
|
+
|
155
|
+
The source code for a work means the preferred form of the work for
|
156
|
+
making modifications to it. For an executable work, complete source
|
157
|
+
code means all the source code for all modules it contains, plus any
|
158
|
+
associated interface definition files, plus the scripts used to
|
159
|
+
control compilation and installation of the executable. However, as a
|
160
|
+
special exception, the source code distributed need not include
|
161
|
+
anything that is normally distributed (in either source or binary
|
162
|
+
form) with the major components (compiler, kernel, and so on) of the
|
163
|
+
operating system on which the executable runs, unless that component
|
164
|
+
itself accompanies the executable.
|
165
|
+
|
166
|
+
If distribution of executable or object code is made by offering
|
167
|
+
access to copy from a designated place, then offering equivalent
|
168
|
+
access to copy the source code from the same place counts as
|
169
|
+
distribution of the source code, even though third parties are not
|
170
|
+
compelled to copy the source along with the object code.
|
171
|
+
|
172
|
+
4. You may not copy, modify, sublicense, or distribute the Program
|
173
|
+
except as expressly provided under this License. Any attempt
|
174
|
+
otherwise to copy, modify, sublicense or distribute the Program is
|
175
|
+
void, and will automatically terminate your rights under this License.
|
176
|
+
However, parties who have received copies, or rights, from you under
|
177
|
+
this License will not have their licenses terminated so long as such
|
178
|
+
parties remain in full compliance.
|
179
|
+
|
180
|
+
5. You are not required to accept this License, since you have not
|
181
|
+
signed it. However, nothing else grants you permission to modify or
|
182
|
+
distribute the Program or its derivative works. These actions are
|
183
|
+
prohibited by law if you do not accept this License. Therefore, by
|
184
|
+
modifying or distributing the Program (or any work based on the
|
185
|
+
Program), you indicate your acceptance of this License to do so, and
|
186
|
+
all its terms and conditions for copying, distributing or modifying
|
187
|
+
the Program or works based on it.
|
188
|
+
|
189
|
+
6. Each time you redistribute the Program (or any work based on the
|
190
|
+
Program), the recipient automatically receives a license from the
|
191
|
+
original licensor to copy, distribute or modify the Program subject to
|
192
|
+
these terms and conditions. You may not impose any further
|
193
|
+
restrictions on the recipients' exercise of the rights granted herein.
|
194
|
+
You are not responsible for enforcing compliance by third parties to
|
195
|
+
this License.
|
196
|
+
|
197
|
+
7. If, as a consequence of a court judgment or allegation of patent
|
198
|
+
infringement or for any other reason (not limited to patent issues),
|
199
|
+
conditions are imposed on you (whether by court order, agreement or
|
200
|
+
otherwise) that contradict the conditions of this License, they do not
|
201
|
+
excuse you from the conditions of this License. If you cannot
|
202
|
+
distribute so as to satisfy simultaneously your obligations under this
|
203
|
+
License and any other pertinent obligations, then as a consequence you
|
204
|
+
may not distribute the Program at all. For example, if a patent
|
205
|
+
license would not permit royalty-free redistribution of the Program by
|
206
|
+
all those who receive copies directly or indirectly through you, then
|
207
|
+
the only way you could satisfy both it and this License would be to
|
208
|
+
refrain entirely from distribution of the Program.
|
209
|
+
|
210
|
+
If any portion of this section is held invalid or unenforceable under
|
211
|
+
any particular circumstance, the balance of the section is intended to
|
212
|
+
apply and the section as a whole is intended to apply in other
|
213
|
+
circumstances.
|
214
|
+
|
215
|
+
It is not the purpose of this section to induce you to infringe any
|
216
|
+
patents or other property right claims or to contest validity of any
|
217
|
+
such claims; this section has the sole purpose of protecting the
|
218
|
+
integrity of the free software distribution system, which is
|
219
|
+
implemented by public license practices. Many people have made
|
220
|
+
generous contributions to the wide range of software distributed
|
221
|
+
through that system in reliance on consistent application of that
|
222
|
+
system; it is up to the author/donor to decide if he or she is willing
|
223
|
+
to distribute software through any other system and a licensee cannot
|
224
|
+
impose that choice.
|
225
|
+
|
226
|
+
This section is intended to make thoroughly clear what is believed to
|
227
|
+
be a consequence of the rest of this License.
|
228
|
+
|
229
|
+
8. If the distribution and/or use of the Program is restricted in
|
230
|
+
certain countries either by patents or by copyrighted interfaces, the
|
231
|
+
original copyright holder who places the Program under this License
|
232
|
+
may add an explicit geographical distribution limitation excluding
|
233
|
+
those countries, so that distribution is permitted only in or among
|
234
|
+
countries not thus excluded. In such case, this License incorporates
|
235
|
+
the limitation as if written in the body of this License.
|
236
|
+
|
237
|
+
9. The Free Software Foundation may publish revised and/or new versions
|
238
|
+
of the General Public License from time to time. Such new versions will
|
239
|
+
be similar in spirit to the present version, but may differ in detail to
|
240
|
+
address new problems or concerns.
|
241
|
+
|
242
|
+
Each version is given a distinguishing version number. If the Program
|
243
|
+
specifies a version number of this License which applies to it and "any
|
244
|
+
later version", you have the option of following the terms and conditions
|
245
|
+
either of that version or of any later version published by the Free
|
246
|
+
Software Foundation. If the Program does not specify a version number of
|
247
|
+
this License, you may choose any version ever published by the Free Software
|
248
|
+
Foundation.
|
249
|
+
|
250
|
+
10. If you wish to incorporate parts of the Program into other free
|
251
|
+
programs whose distribution conditions are different, write to the author
|
252
|
+
to ask for permission. For software which is copyrighted by the Free
|
253
|
+
Software Foundation, write to the Free Software Foundation; we sometimes
|
254
|
+
make exceptions for this. Our decision will be guided by the two goals
|
255
|
+
of preserving the free status of all derivatives of our free software and
|
256
|
+
of promoting the sharing and reuse of software generally.
|
257
|
+
|
258
|
+
NO WARRANTY
|
259
|
+
|
260
|
+
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
261
|
+
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
262
|
+
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
263
|
+
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
264
|
+
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
265
|
+
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
266
|
+
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
267
|
+
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
268
|
+
REPAIR OR CORRECTION.
|
269
|
+
|
270
|
+
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
271
|
+
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
|
272
|
+
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
273
|
+
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
|
274
|
+
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
|
275
|
+
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
|
276
|
+
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
|
277
|
+
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
278
|
+
POSSIBILITY OF SUCH DAMAGES.
|
279
|
+
|
280
|
+
END OF TERMS AND CONDITIONS
|
281
|
+
|
282
|
+
How to Apply These Terms to Your New Programs
|
283
|
+
|
284
|
+
If you develop a new program, and you want it to be of the greatest
|
285
|
+
possible use to the public, the best way to achieve this is to make it
|
286
|
+
free software which everyone can redistribute and change under these terms.
|
287
|
+
|
288
|
+
To do so, attach the following notices to the program. It is safest
|
289
|
+
to attach them to the start of each source file to most effectively
|
290
|
+
convey the exclusion of warranty; and each file should have at least
|
291
|
+
the "copyright" line and a pointer to where the full notice is found.
|
292
|
+
|
293
|
+
{description}
|
294
|
+
Copyright (C) 2015 Carl P. Corliss <rabbitt@gmail.com>
|
295
|
+
|
296
|
+
This program is free software; you can redistribute it and/or modify
|
297
|
+
it under the terms of the GNU General Public License as published by
|
298
|
+
the Free Software Foundation; either version 2 of the License, or
|
299
|
+
(at your option) any later version.
|
300
|
+
|
301
|
+
This program is distributed in the hope that it will be useful,
|
302
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
303
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
304
|
+
GNU General Public License for more details.
|
305
|
+
|
306
|
+
You should have received a copy of the GNU General Public License along
|
307
|
+
with this program; if not, write to the Free Software Foundation, Inc.,
|
308
|
+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
309
|
+
|
310
|
+
Also add information on how to contact you by electronic and paper mail.
|
311
|
+
|
312
|
+
If the program is interactive, make it output a short notice like this
|
313
|
+
when it starts in an interactive mode:
|
314
|
+
|
315
|
+
Gnomovision version 69, Copyright (C) year name of author
|
316
|
+
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
317
|
+
This is free software, and you are welcome to redistribute it
|
318
|
+
under certain conditions; type `show c' for details.
|
319
|
+
|
320
|
+
The hypothetical commands `show w' and `show c' should show the appropriate
|
321
|
+
parts of the General Public License. Of course, the commands you use may
|
322
|
+
be called something other than `show w' and `show c'; they could even be
|
323
|
+
mouse-clicks or menu items--whatever suits your program.
|
324
|
+
|
325
|
+
You should also get your employer (if you work as a programmer) or your
|
326
|
+
school, if any, to sign a "copyright disclaimer" for the program, if
|
327
|
+
necessary. Here is a sample; alter the names:
|
328
|
+
|
329
|
+
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
|
330
|
+
`Gnomovision' (which makes passes at compilers) written by James Hacker.
|
331
|
+
|
332
|
+
{signature of Ty Coon}, 1 April 1989
|
333
|
+
Ty Coon, President of Vice
|
334
|
+
|
335
|
+
This General Public License does not permit incorporating your program into
|
336
|
+
proprietary programs. If your program is a subroutine library, you may
|
337
|
+
consider it more useful to permit linking proprietary applications with the
|
338
|
+
library. If this is what you want to do, use the GNU Lesser General
|
339
|
+
Public License instead of this License.
|
340
|
+
|
data/README.md
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
# FqdnFacts
|
2
|
+
|
3
|
+
FqdnFacts allows you to create fact handlers for different FQDN formats. This is primarily intended for use with Puppet/Facter to facilitate dynamic fact generation based on FQDNs.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'fqdn_facts'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install fqdn_facts
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
An example is (hopefully) worth a thousand words:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
|
25
|
+
%w[
|
26
|
+
facter
|
27
|
+
fqdn_facts
|
28
|
+
awesome_print
|
29
|
+
].each do |lib|
|
30
|
+
begin
|
31
|
+
require lib
|
32
|
+
rescue LoadError
|
33
|
+
puts "Unable to find lib '#{lib}' - try installing it: gem install #{lib}"
|
34
|
+
exit 1
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# setup a baseline handler that we can use as a "catch-all"
|
39
|
+
# as well as a foundation to derive from with other handlers.
|
40
|
+
FqdnFacts.register(:baseline) do
|
41
|
+
priority 1000 # we want this to be used last as a catch-all
|
42
|
+
|
43
|
+
# this defines the order of the components
|
44
|
+
components :host, :sub, :tld
|
45
|
+
|
46
|
+
# if a component is not listed in 'components' it is
|
47
|
+
# assumed that the validation for it is :any. However,
|
48
|
+
# all sub-components /must/ have a validation defined.
|
49
|
+
component :host, {
|
50
|
+
# order of sub-components is important!
|
51
|
+
type: %r'^([a-z][a-z-]*[a-z])',
|
52
|
+
id: %r'(\d{2,})',
|
53
|
+
subtype: %r'([ms]?)'
|
54
|
+
}
|
55
|
+
component :sub, /.+/
|
56
|
+
component :tld, /.+/
|
57
|
+
|
58
|
+
# conversions happen during fact retrieval and
|
59
|
+
# prior to dynamic fact generation.
|
60
|
+
convert :host, {
|
61
|
+
subtype: ->(v) {
|
62
|
+
case v
|
63
|
+
when 'm' then 'master'
|
64
|
+
when 's' then 'slave'
|
65
|
+
end
|
66
|
+
},
|
67
|
+
id: ->(v) { v.to_i }
|
68
|
+
}
|
69
|
+
|
70
|
+
# facts generated dynamically using procs/lambdas are calculated
|
71
|
+
# after all conversions, and in order order of assignment. If you
|
72
|
+
# use a lambda/proc, it is expected to receive at most one parameter.
|
73
|
+
# If you receive for one, then that parameter will contain a hash of
|
74
|
+
# facts generated up until that point.
|
75
|
+
add_fact :hostname, ->(f) { f[:host] }
|
76
|
+
add_fact :domain, ->(f) { [f[:sub], f[:tld]].join('.') }
|
77
|
+
end # this essentially matches ([a-z][a-z-]*[a-z])(\d{2,})([ms]?)\.([^\.]+)\.bar\.com
|
78
|
+
|
79
|
+
# copy baseline to create the qa handler. This means
|
80
|
+
# that the :qa handler will contain all the settings
|
81
|
+
# that we defined in :baseline aside from any settings
|
82
|
+
# that we override in :qa.
|
83
|
+
FqdnFacts.register(:qa, copy: :baseline) do
|
84
|
+
priority 10
|
85
|
+
component :sub, %r:qa\d*:
|
86
|
+
add_fact :env, 'qa'
|
87
|
+
end # this matches ([a-z][a-z-]*[a-z])(\d{2,})([ms]?)\.qa\d+\.bar\.com
|
88
|
+
|
89
|
+
# let's say you don't have a server that conforms to your previously
|
90
|
+
# defined :host component (e.g., it doesn't use id/subtype), but is instead
|
91
|
+
# a static value.
|
92
|
+
FqdnFacts.register(:qa_foo_server, copy: :baseline) do
|
93
|
+
# we can set this to the same priority as the :qa handler because
|
94
|
+
# the :qa handler will fail on the :host component for foo-server
|
95
|
+
# which will allow this handler to pick it up.
|
96
|
+
priority 10
|
97
|
+
component :host, 'foo-server'
|
98
|
+
add_fact :nifty, 'data'
|
99
|
+
end # this matches foo-server\.qa\d+\.bar\.com
|
100
|
+
|
101
|
+
# Now we gather our facts:
|
102
|
+
begin
|
103
|
+
facts = FqdnFacts.handler(ARGV.first || Facter['fqdn'].value).retrieve_facts
|
104
|
+
facts.each do |fact, value|
|
105
|
+
Facter.add(fact) { setcode { value } }
|
106
|
+
end
|
107
|
+
ap facts
|
108
|
+
rescue FqdnFacts::Error::UnresolveableHandler => e
|
109
|
+
puts e.message
|
110
|
+
end
|
111
|
+
|
112
|
+
=begin
|
113
|
+
|
114
|
+
Now given a FQDN of foo-server.qa3.bar.com, we would get the following facts:
|
115
|
+
|
116
|
+
{
|
117
|
+
:fqdn => "foo-server.qa3.bar.com",
|
118
|
+
:handler_name => "qa_foo_server",
|
119
|
+
:hostname => "foo-server",
|
120
|
+
:domain => "qa3.bar.com",
|
121
|
+
:nifty => "data",
|
122
|
+
:host => "foo-server",
|
123
|
+
:sub => "qa3",
|
124
|
+
:tld => "bar.com"
|
125
|
+
}
|
126
|
+
|
127
|
+
And, for foobaz01.qa1.bar.com:
|
128
|
+
|
129
|
+
{
|
130
|
+
:fqdn => "foobaz01.qa1.bar.com",
|
131
|
+
:handler_name => "qa",
|
132
|
+
:hostname => "foobaz01",
|
133
|
+
:domain => "qa1.bar.com",
|
134
|
+
:env => "qa",
|
135
|
+
:host => "foobaz01",
|
136
|
+
:sub => "qa1",
|
137
|
+
:tld => "bar.com",
|
138
|
+
:host_type => "foobaz",
|
139
|
+
:host_id => 1
|
140
|
+
}
|
141
|
+
|
142
|
+
And, now one that baseline should catch, www01.abc.com:
|
143
|
+
|
144
|
+
{
|
145
|
+
:fqdn => "www01.abc.com",
|
146
|
+
:handler_name => "baseline",
|
147
|
+
:hostname => "www01",
|
148
|
+
:domain => "abc.com",
|
149
|
+
:host => "www01",
|
150
|
+
:sub => "abc",
|
151
|
+
:tld => "com",
|
152
|
+
:host_type => "www",
|
153
|
+
:host_id => 1
|
154
|
+
}
|
155
|
+
=end
|
156
|
+
```
|
157
|
+
|
158
|
+
## Contributing
|
159
|
+
|
160
|
+
1. Fork it ( https://github.com/[my-github-username]/fqdn_facts/fork )
|
161
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
162
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
163
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
164
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/fqdn_facts.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'fqdn_facts/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "fqdn_facts"
|
8
|
+
spec.version = FqdnFacts::VERSION
|
9
|
+
spec.authors = ["Carl P. Corliss"]
|
10
|
+
spec.email = ["rabbitt@gmail.com"]
|
11
|
+
spec.summary = %q{Provides a DSL for generating FQDN specific facts that can be used with Facter}
|
12
|
+
spec.description = %q{FqdnFacts allows you to create fact handlers for different FQDN formats. This is primarily intended for use with Puppet/Facter to facilitate dynamic fact generation based on FQDNs.}
|
13
|
+
spec.homepage = "https://github.com/rabbitt/fqdn_facts/"
|
14
|
+
spec.license = "GPLv2"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
@@ -0,0 +1,279 @@
|
|
1
|
+
=begin
|
2
|
+
Copyright (C) 2015 Carl P. Corliss <rabbitt@gmail.com>
|
3
|
+
|
4
|
+
This program is free software; you can redistribute it and/or modify
|
5
|
+
it under the terms of the GNU General Public License as published by
|
6
|
+
the Free Software Foundation; either version 2 of the License, or
|
7
|
+
(at your option) any later version.
|
8
|
+
|
9
|
+
This program is distributed in the hope that it will be useful,
|
10
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
GNU General Public License for more details.
|
13
|
+
|
14
|
+
You should have received a copy of the GNU General Public License along
|
15
|
+
with this program; if not, write to the Free Software Foundation, Inc.,
|
16
|
+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
17
|
+
=end
|
18
|
+
|
19
|
+
require_relative 'core_ext'
|
20
|
+
require_relative 'errors'
|
21
|
+
|
22
|
+
module FqdnFacts
|
23
|
+
#
|
24
|
+
# base class for all handlers.
|
25
|
+
#
|
26
|
+
class BaseHandler
|
27
|
+
class << self
|
28
|
+
# Creates a new instance of this object using the internal state
|
29
|
+
# of another handler object
|
30
|
+
def copy_from(other)
|
31
|
+
new(other.export)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
attr_accessor :priority, :fqdn
|
36
|
+
|
37
|
+
# default validation hash of FQDN components
|
38
|
+
DEFAULT_COMPONENTS = {
|
39
|
+
host: %r'(.+)',
|
40
|
+
sub: %r:(.+):,
|
41
|
+
tld: %r'(.+)'
|
42
|
+
} unless defined? DEFAULT_COMPONENTS
|
43
|
+
private_constant :DEFAULT_COMPONENTS
|
44
|
+
|
45
|
+
# initilalizer
|
46
|
+
def initialize(data = {})
|
47
|
+
@priority = data.delete(:priority) || 1
|
48
|
+
@conversions = data.delete(:conversions) || {}
|
49
|
+
@components = data.delete(:components) || DEFAULT_COMPONENTS.dup
|
50
|
+
@component_order = data.delete(:component_order) || DEFAULT_COMPONENTS.keys
|
51
|
+
@facts = data.delete(:facts) || {}
|
52
|
+
@fqdn = ''
|
53
|
+
|
54
|
+
add_fact :fqdn, ->() { @fqdn }
|
55
|
+
add_fact :handler_name, self.class.to_s.split('::').last.underscore
|
56
|
+
end
|
57
|
+
|
58
|
+
## DSL Methods
|
59
|
+
# @!group DSL
|
60
|
+
|
61
|
+
# The priority of the registered Fqdn Fact handler
|
62
|
+
#
|
63
|
+
# Lower priority fqdn fact handlers win out over
|
64
|
+
# higer priority handlers.
|
65
|
+
#
|
66
|
+
# @return Integer
|
67
|
+
def priority(value = nil)
|
68
|
+
return @priority if value.nil?
|
69
|
+
@priority = value.to_i
|
70
|
+
end
|
71
|
+
|
72
|
+
# Defines the order of the defined components
|
73
|
+
# that make up the FQDN.
|
74
|
+
#
|
75
|
+
# This defaults to: host, :sub, :tld
|
76
|
+
def components(*args)
|
77
|
+
@component_order = args
|
78
|
+
end
|
79
|
+
|
80
|
+
# Define a validation rule for a component.
|
81
|
+
#
|
82
|
+
# Validation rules can be one of the following
|
83
|
+
# * :any (the same as /.+/), Hash
|
84
|
+
# * A Hash of sub_component/regexs
|
85
|
+
# * An array of valid values
|
86
|
+
# * a scalar for exact matching
|
87
|
+
#
|
88
|
+
# @param component Symbol the name of the component to set validation for
|
89
|
+
# @param validate Hash{Symbol=>Symbol,Hash,Array,Scalar}
|
90
|
+
def component(component, validate=:any)
|
91
|
+
v = case validate
|
92
|
+
when :any then %r:(.+):
|
93
|
+
when Hash then
|
94
|
+
if @components[component.to_sym].is_a?(Hash)
|
95
|
+
basis = @components[component.to_sym]
|
96
|
+
else
|
97
|
+
basis = {}
|
98
|
+
end
|
99
|
+
|
100
|
+
basis.merge(
|
101
|
+
Hash[validate.keys.zip(
|
102
|
+
validate.values.collect {|v|
|
103
|
+
case v
|
104
|
+
when :any then %r:(.+):
|
105
|
+
when Regexp then v
|
106
|
+
else Regexp.new(v)
|
107
|
+
end
|
108
|
+
}
|
109
|
+
)]
|
110
|
+
)
|
111
|
+
when Array then
|
112
|
+
%r:(#{validate.join('|')}):
|
113
|
+
else validate
|
114
|
+
end
|
115
|
+
|
116
|
+
if @components[component.to_sym]
|
117
|
+
# if their not the same class, then remove any conversions
|
118
|
+
unless @components[component.to_sym].is_a?(v.class)
|
119
|
+
@conversions.delete(component.to_sym)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
@components[component.to_sym] = v
|
124
|
+
end
|
125
|
+
|
126
|
+
# Defines a conversion rule for a given component.
|
127
|
+
# The conversion must be a Proc/Lambda
|
128
|
+
#
|
129
|
+
# @param component Symbol the name of the component to set validation for
|
130
|
+
# @param conversion Proc the conversion proc/lambda
|
131
|
+
def convert(component, conversion)
|
132
|
+
conversion = case conversion
|
133
|
+
when Hash then
|
134
|
+
@conversions[component.to_sym] ||= {}
|
135
|
+
@conversions[component.to_sym].merge(conversion)
|
136
|
+
else conversion
|
137
|
+
end
|
138
|
+
@conversions[component.to_sym] = conversion
|
139
|
+
end
|
140
|
+
|
141
|
+
# Returns the value of a fact
|
142
|
+
#
|
143
|
+
# @param name String, Symbol name of the fact to retrieve
|
144
|
+
def get_fact(name)
|
145
|
+
retrieve_facts[name.to_sym]
|
146
|
+
end
|
147
|
+
|
148
|
+
# Adds a fact, either using a static value, or a Proc/Lambda
|
149
|
+
# for runtime determination of the value.
|
150
|
+
#
|
151
|
+
# @param name String, Symbol name of the fact to add
|
152
|
+
# @param value
|
153
|
+
def add_fact(name, value)
|
154
|
+
@facts[name.to_sym] = value
|
155
|
+
end
|
156
|
+
|
157
|
+
# Removes a fact from the list of facts.
|
158
|
+
def remove_fact(name)
|
159
|
+
@facts.delete(name.to_sym)
|
160
|
+
end
|
161
|
+
|
162
|
+
### End of DSL Methods
|
163
|
+
# @!endgroup
|
164
|
+
|
165
|
+
# retreives all facts, optionally prefixing the name of each
|
166
|
+
# with a string value.
|
167
|
+
def retrieve_facts(prefix = '')
|
168
|
+
facts = @facts.merge(Hash[fqdn_components])
|
169
|
+
|
170
|
+
# build facts from components
|
171
|
+
@components.each do |name, value|
|
172
|
+
case value
|
173
|
+
when Hash then
|
174
|
+
data = facts[name].scan(Regexp.new(value.values.join)).flatten
|
175
|
+
value.keys.zip(data).each do |key, v|
|
176
|
+
if @conversions[name] && @conversions[name][key]
|
177
|
+
facts["#{name}_#{key}".to_sym] = @conversions[name][key].call(v)
|
178
|
+
else
|
179
|
+
facts["#{name}_#{key}".to_sym] = v
|
180
|
+
end
|
181
|
+
end
|
182
|
+
else
|
183
|
+
if @conversions[name]
|
184
|
+
facts[name] = @conversions[name].call(facts[name])
|
185
|
+
end
|
186
|
+
@facts.merge!(name.to_sym => value)
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
# handle conversions
|
191
|
+
facts.each do |key, value|
|
192
|
+
if value.is_a?(Proc)
|
193
|
+
value = value.arity == 1 ? value.call(facts) : value.call()
|
194
|
+
end
|
195
|
+
facts[key] = value.is_a?(Symbol) ? value.to_s : value
|
196
|
+
end
|
197
|
+
|
198
|
+
# add prefix
|
199
|
+
facts.replace(
|
200
|
+
facts.inject({}) { |h,(k,v)| h["#{prefix}_#{k}"] = v ; h }
|
201
|
+
) unless prefix.empty?
|
202
|
+
|
203
|
+
facts.reject { |k,v| v.empty? }
|
204
|
+
end
|
205
|
+
|
206
|
+
# Checks to see if the fqdn matches this particular FQDN Fact Handler
|
207
|
+
def match?(fqdn)
|
208
|
+
parts = fqdn.split('.', @component_order.size)
|
209
|
+
return unless parts.size == @component_order.size
|
210
|
+
|
211
|
+
debug "Validating #{self.class}:"
|
212
|
+
|
213
|
+
test_data = @component_order.zip(parts)
|
214
|
+
debug " test data -> #{test_data.inspect}"
|
215
|
+
|
216
|
+
test_data.all? do |name, value|
|
217
|
+
debug " validating component '#{name}'"
|
218
|
+
validation = case @components[name]
|
219
|
+
when Hash then Regexp.new(@components[name].values.join)
|
220
|
+
when nil then %r:.+:
|
221
|
+
else @components[name]
|
222
|
+
end
|
223
|
+
|
224
|
+
case validation
|
225
|
+
when Regexp then
|
226
|
+
(value =~ validation).tap { |r|
|
227
|
+
debug " Regexp -> #{value.inspect} =~ #{validation.inspect} == #{r.inspect}"
|
228
|
+
}
|
229
|
+
when Array then
|
230
|
+
validation.include?(value).tap { |r|
|
231
|
+
debug " Array -> #{validation.inspect}.include?(#{value.inspect}) == #{r.inspect}"
|
232
|
+
}
|
233
|
+
else
|
234
|
+
(value == validation).tap { |r|
|
235
|
+
debug " #{validation.class} -> #{value.inspect} == #{validation.inspect} == #{r.inspect}"
|
236
|
+
}
|
237
|
+
end
|
238
|
+
end.tap { |r| debug " ---> validation #{r ? 'successful' : 'failed'} for #{self.class}"}
|
239
|
+
end
|
240
|
+
|
241
|
+
# Compares the priority of this handler to another handler
|
242
|
+
def <=>(other)
|
243
|
+
self.priority <=> other.priority
|
244
|
+
end
|
245
|
+
|
246
|
+
# Exports the internal state as a hash
|
247
|
+
def export
|
248
|
+
instance_variables.inject({}) do |exports, name|
|
249
|
+
varname = name.to_s.tr('@', '').to_sym
|
250
|
+
exports[varname] = begin
|
251
|
+
Marshal.load(Marshal.dump(instance_variable_get(name)))
|
252
|
+
rescue TypeError
|
253
|
+
instance_variable_get(name).dup
|
254
|
+
end
|
255
|
+
exports
|
256
|
+
end
|
257
|
+
end
|
258
|
+
alias_method :to_h, :export
|
259
|
+
|
260
|
+
private
|
261
|
+
|
262
|
+
# @api private
|
263
|
+
def fqdn_data
|
264
|
+
@fqdn.split('.', @component_order.size)
|
265
|
+
end
|
266
|
+
|
267
|
+
# @api private
|
268
|
+
def fqdn_components
|
269
|
+
@component_order.zip(fqdn_data)
|
270
|
+
end
|
271
|
+
|
272
|
+
# @api private
|
273
|
+
def debug(message)
|
274
|
+
if ENV.keys.collect(&:downcase).include? 'debug'
|
275
|
+
STDERR.puts message
|
276
|
+
end
|
277
|
+
end
|
278
|
+
end
|
279
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
=begin
|
2
|
+
Copyright (C) 2015 Carl P. Corliss <rabbitt@gmail.com>
|
3
|
+
|
4
|
+
This program is free software; you can redistribute it and/or modify
|
5
|
+
it under the terms of the GNU General Public License as published by
|
6
|
+
the Free Software Foundation; either version 2 of the License, or
|
7
|
+
(at your option) any later version.
|
8
|
+
|
9
|
+
This program is distributed in the hope that it will be useful,
|
10
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
GNU General Public License for more details.
|
13
|
+
|
14
|
+
You should have received a copy of the GNU General Public License along
|
15
|
+
with this program; if not, write to the Free Software Foundation, Inc.,
|
16
|
+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
17
|
+
=end
|
18
|
+
|
19
|
+
# @see Object
|
20
|
+
class Object
|
21
|
+
# provides an empty? check for all objects
|
22
|
+
# unless an object already has an empty? check
|
23
|
+
#
|
24
|
+
# @return false
|
25
|
+
def empty?
|
26
|
+
return false
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# @see NilClass
|
31
|
+
class NilClass
|
32
|
+
# provides an empty? check for nil
|
33
|
+
# @return true
|
34
|
+
def empty?
|
35
|
+
true
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# @see String
|
40
|
+
class String
|
41
|
+
# converts a camelized string to underscored
|
42
|
+
# @return String
|
43
|
+
def underscore
|
44
|
+
self.split(/([A-Z][a-z0-9]+)/).reject(&:empty?).collect(&:downcase).join('_')
|
45
|
+
end
|
46
|
+
|
47
|
+
# converts an underscored string to a camelized one
|
48
|
+
# @return String
|
49
|
+
def camelize
|
50
|
+
self.split('_').collect(&:capitalize).join
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
=begin
|
2
|
+
Copyright (C) 2015 Carl P. Corliss <rabbitt@gmail.com>
|
3
|
+
|
4
|
+
This program is free software; you can redistribute it and/or modify
|
5
|
+
it under the terms of the GNU General Public License as published by
|
6
|
+
the Free Software Foundation; either version 2 of the License, or
|
7
|
+
(at your option) any later version.
|
8
|
+
|
9
|
+
This program is distributed in the hope that it will be useful,
|
10
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
GNU General Public License for more details.
|
13
|
+
|
14
|
+
You should have received a copy of the GNU General Public License along
|
15
|
+
with this program; if not, write to the Free Software Foundation, Inc.,
|
16
|
+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
17
|
+
=end
|
18
|
+
|
19
|
+
module FqdnFacts
|
20
|
+
# Base Error class for FqdnFacts
|
21
|
+
class Error < StandardError
|
22
|
+
# Error class emitted when a handler is requested that doesn't exist
|
23
|
+
class HandlerNotFound < Error; end
|
24
|
+
|
25
|
+
# Error class emitted when an FQDN is unable to be resolved to a Handler
|
26
|
+
class UnresolveableHandler < Error; end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
=begin
|
2
|
+
Copyright (C) 2015 Carl P. Corliss <rabbitt@gmail.com>
|
3
|
+
|
4
|
+
This program is free software; you can redistribute it and/or modify
|
5
|
+
it under the terms of the GNU General Public License as published by
|
6
|
+
the Free Software Foundation; either version 2 of the License, or
|
7
|
+
(at your option) any later version.
|
8
|
+
|
9
|
+
This program is distributed in the hope that it will be useful,
|
10
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
GNU General Public License for more details.
|
13
|
+
|
14
|
+
You should have received a copy of the GNU General Public License along
|
15
|
+
with this program; if not, write to the Free Software Foundation, Inc.,
|
16
|
+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
17
|
+
=end
|
18
|
+
|
19
|
+
module FqdnFacts
|
20
|
+
# The current FqdnFacts version
|
21
|
+
VERSION = "0.0.1"
|
22
|
+
end
|
data/lib/fqdn_facts.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
=begin
|
2
|
+
Copyright (C) 2015 Carl P. Corliss <rabbitt@gmail.com>
|
3
|
+
|
4
|
+
This program is free software; you can redistribute it and/or modify
|
5
|
+
it under the terms of the GNU General Public License as published by
|
6
|
+
the Free Software Foundation; either version 2 of the License, or
|
7
|
+
(at your option) any later version.
|
8
|
+
|
9
|
+
This program is distributed in the hope that it will be useful,
|
10
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
GNU General Public License for more details.
|
13
|
+
|
14
|
+
You should have received a copy of the GNU General Public License along
|
15
|
+
with this program; if not, write to the Free Software Foundation, Inc.,
|
16
|
+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
17
|
+
=end
|
18
|
+
|
19
|
+
require "fqdn_facts/version"
|
20
|
+
require 'fqdn_facts/core_ext'
|
21
|
+
|
22
|
+
#
|
23
|
+
# Provides a DSL for generating Fact Handlers for different
|
24
|
+
# fully qualified domain names.
|
25
|
+
#
|
26
|
+
module FqdnFacts
|
27
|
+
@registry = {}
|
28
|
+
|
29
|
+
autoload :BaseHandler, 'fqdn_facts/base_handler'
|
30
|
+
autoload :Error, 'fqdn_facts/errors'
|
31
|
+
|
32
|
+
class << self
|
33
|
+
attr_reader :registry
|
34
|
+
|
35
|
+
# Registers a FQDN Fact Handler
|
36
|
+
#
|
37
|
+
# @param klass Symbol name of handler to register
|
38
|
+
# @param options Hash options to pass
|
39
|
+
# @param block Block block of DSL code
|
40
|
+
#
|
41
|
+
# @return FqdnFacts::BaseHandler derived class
|
42
|
+
#
|
43
|
+
def register(klass, options={}, &block)
|
44
|
+
klass_const = klass.to_s.camelize
|
45
|
+
unless BaseHandler.const_defined? klass_const
|
46
|
+
BaseHandler.const_set(klass_const, Class.new(BaseHandler))
|
47
|
+
end
|
48
|
+
|
49
|
+
if other = options.delete(:copy)
|
50
|
+
unless other = @registry[other.to_sym]
|
51
|
+
fail Error::HandlerNotFound, other
|
52
|
+
end
|
53
|
+
@registry[klass.to_sym] = BaseHandler.const_get(klass_const).copy_from(other)
|
54
|
+
else
|
55
|
+
@registry[klass.to_sym] = BaseHandler.const_get(klass_const).new
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
if block.arity == 1
|
60
|
+
yield @registry[klass.to_sym]
|
61
|
+
else
|
62
|
+
@registry[klass.to_sym].instance_eval(&block)
|
63
|
+
end
|
64
|
+
|
65
|
+
@registry[klass.to_sym]
|
66
|
+
end
|
67
|
+
|
68
|
+
# Retrieves a handler that is capable of generating facts
|
69
|
+
# for the given FQDN
|
70
|
+
#
|
71
|
+
# @param fqdn String Fully qualified domain name to resolve into a handler
|
72
|
+
#
|
73
|
+
# @return FqdnFacts::BaseHandler derived class
|
74
|
+
def handler(fqdn)
|
75
|
+
if (handlers = @registry.values.select { |klass| klass.match?(fqdn) }).empty?
|
76
|
+
fail Error::UnresolveableHandler, "unable to find a handler for FQDN:#{fqdn}"
|
77
|
+
end
|
78
|
+
handlers.sort.first.tap { |handler| handler.fqdn = fqdn }
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FqdnFacts do
|
4
|
+
before :all do
|
5
|
+
FqdnFacts.register(:baseline) do
|
6
|
+
priority 10
|
7
|
+
|
8
|
+
components :host, :sub, :tld
|
9
|
+
|
10
|
+
component :host, { type: %r'^([^\d]+)', id: %r'(\d+)', subtype: %r'([ms]?)'}
|
11
|
+
component :tld, 'example.com'
|
12
|
+
|
13
|
+
convert :host, {
|
14
|
+
subtype: ->(v) { v == 'm' ? 'master' : 'slave' },
|
15
|
+
id: ->(v) { v.to_i }
|
16
|
+
}
|
17
|
+
|
18
|
+
add_fact :hostname, ->(f) { f[:host] }
|
19
|
+
add_fact :domain, ->(f) { [f[:sub], f[:tld]].join('.') }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'has a version number' do
|
24
|
+
expect(FqdnFacts::VERSION).not_to be nil
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'registers a handler named baseline' do
|
28
|
+
expect(FqdnFacts.registry).to include(:baseline)
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'with fqdn "foo01m.bar.example.com"' do
|
32
|
+
before do
|
33
|
+
@fqdn = 'foo01m.bar.example.com'
|
34
|
+
@handler = FqdnFacts.handler(@fqdn)
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'returns a valid hash' do
|
38
|
+
expect(@handler.retrieve_facts).to be_a Hash
|
39
|
+
end
|
40
|
+
|
41
|
+
describe 'facts' do
|
42
|
+
let(:facts) { @handler.retrieve_facts }
|
43
|
+
{
|
44
|
+
:host => 'foo01m',
|
45
|
+
:host_type => 'foo',
|
46
|
+
:host_id => 1,
|
47
|
+
:host_subtype => 'master',
|
48
|
+
:hostname => 'foo01m',
|
49
|
+
:sub => 'bar',
|
50
|
+
:domain => 'bar.example.com',
|
51
|
+
:fqdn => 'foo01m.bar.example.com',
|
52
|
+
:handler_name => 'baseline'
|
53
|
+
}.each do |key, value|
|
54
|
+
it "sets #{key.inspect} to #{value.inspect}" do
|
55
|
+
expect(facts).to include(key)
|
56
|
+
expect(facts[key]).to eq(value)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fqdn_facts
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Carl P. Corliss
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: FqdnFacts allows you to create fact handlers for different FQDN formats.
|
56
|
+
This is primarily intended for use with Puppet/Facter to facilitate dynamic fact
|
57
|
+
generation based on FQDNs.
|
58
|
+
email:
|
59
|
+
- rabbitt@gmail.com
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- ".gitignore"
|
65
|
+
- ".rspec"
|
66
|
+
- ".travis.yml"
|
67
|
+
- Gemfile
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- fqdn_facts.gemspec
|
72
|
+
- lib/fqdn_facts.rb
|
73
|
+
- lib/fqdn_facts/base_handler.rb
|
74
|
+
- lib/fqdn_facts/core_ext.rb
|
75
|
+
- lib/fqdn_facts/errors.rb
|
76
|
+
- lib/fqdn_facts/version.rb
|
77
|
+
- spec/fqdn_facts_spec.rb
|
78
|
+
- spec/spec_helper.rb
|
79
|
+
homepage: https://github.com/rabbitt/fqdn_facts/
|
80
|
+
licenses:
|
81
|
+
- GPLv2
|
82
|
+
metadata: {}
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
requirements: []
|
98
|
+
rubyforge_project:
|
99
|
+
rubygems_version: 2.2.2
|
100
|
+
signing_key:
|
101
|
+
specification_version: 4
|
102
|
+
summary: Provides a DSL for generating FQDN specific facts that can be used with Facter
|
103
|
+
test_files:
|
104
|
+
- spec/fqdn_facts_spec.rb
|
105
|
+
- spec/spec_helper.rb
|