quixoten-puppetdb-terminus 2.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +23 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +202 -0
- data/README.md +29 -0
- data/Rakefile +16 -0
- data/lib/puppet/application/storeconfigs.rb +4 -0
- data/lib/puppet/face/node/deactivate.rb +38 -0
- data/lib/puppet/face/node/status.rb +83 -0
- data/lib/puppet/face/storeconfigs.rb +179 -0
- data/lib/puppet/indirector/catalog/puppetdb.rb +350 -0
- data/lib/puppet/indirector/facts/puppetdb.rb +134 -0
- data/lib/puppet/indirector/facts/puppetdb_apply.rb +25 -0
- data/lib/puppet/indirector/node/puppetdb.rb +22 -0
- data/lib/puppet/indirector/resource/puppetdb.rb +107 -0
- data/lib/puppet/reports/puppetdb.rb +186 -0
- data/lib/puppet/util/puppetdb/blacklist.rb +35 -0
- data/lib/puppet/util/puppetdb/char_encoding.rb +212 -0
- data/lib/puppet/util/puppetdb/command.rb +113 -0
- data/lib/puppet/util/puppetdb/command_names.rb +8 -0
- data/lib/puppet/util/puppetdb/config.rb +112 -0
- data/lib/puppet/util/puppetdb/global_check.rb +31 -0
- data/lib/puppet/util/puppetdb.rb +108 -0
- data/lib/puppetdb/terminus/version.rb +5 -0
- data/lib/puppetdb/terminus.rb +6 -0
- data/lib/puppetdb-terminus.rb +1 -0
- data/lib/quixoten-puppetdb-terminus.rb +1 -0
- data/puppetdb-terminus.gemspec +23 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 93b5e73c9a812b65d7a4c748ffe8e7630c8e5539
|
4
|
+
data.tar.gz: 8e15bc1afe25ae2b18a187c1d3aac23b43550df5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b7586a4405e67cba5ec10e03f929fcf3594bb34720302c99e06b13c7890cbf82a4f4b8930833b70675e0a3397ca6a22b038716b2f7fb7e1d3bde82c7ab7495bd
|
7
|
+
data.tar.gz: 32222466369bfc4608c82f17edea290ea4c15c26bc1386f8006476b530ba9c6deabdf77fc6e3aa081c0b2414804f1892a30f31d20bc056c93674d87a8715d9b2
|
data/.gitignore
ADDED
@@ -0,0 +1,23 @@
|
|
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
|
23
|
+
*.tar.gz
|
data/Gemfile
ADDED
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 2014 MoneyDesktop, Inc.
|
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/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# PuppetDB Terminus
|
2
|
+
|
3
|
+
PuppetDB Terminus packaged as a gem
|
4
|
+
|
5
|
+
The version of PuppetDB used to build the gem is reflected in the gems version,
|
6
|
+
e.g., version 2.0.0.3 of PuppetDB Terminus is the third build against the
|
7
|
+
source from PuppetDB 2.0.0
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'quixoten-puppetdb-terminus'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install quixoten-puppetdb-terminus
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( https://github.com/[my-github-username]/puppetdb-terminus/fork )
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "fileutils"
|
3
|
+
require "puppetdb-terminus"
|
4
|
+
|
5
|
+
desc "Download source from PuppetDB #{PuppetDB::Terminus::VERSION}"
|
6
|
+
task :download_source do
|
7
|
+
version = ::PuppetDB::Terminus::VERSION.sub(/\.[^.]+$/, '')
|
8
|
+
archive = "#{version}.tar.gz"
|
9
|
+
url = "https://github.com/puppetlabs/puppetdb/archive/#{archive}"
|
10
|
+
|
11
|
+
`curl -sLO #{url}`
|
12
|
+
FileUtils::rm_rf File.expand_path('../lib/puppet', __FILE__)
|
13
|
+
`tar axf #{archive} puppetdb-#{version}/puppet/lib/ --strip-components=2`
|
14
|
+
end
|
15
|
+
|
16
|
+
task :build => :download_source
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'puppet/util/puppetdb'
|
2
|
+
require 'puppet/util/puppetdb/command_names'
|
3
|
+
|
4
|
+
Puppet::Face.define(:node, '0.0.1') do
|
5
|
+
|
6
|
+
CommandDeactivateNode = Puppet::Util::Puppetdb::CommandNames::CommandDeactivateNode
|
7
|
+
|
8
|
+
action :deactivate do
|
9
|
+
summary "Deactivate a set of nodes in PuppetDB"
|
10
|
+
arguments "<node> [<node> ...]"
|
11
|
+
description <<-DESC
|
12
|
+
This will issue '#{CommandDeactivateNode}' commands to the PuppetDB server for
|
13
|
+
each node specified. The server is found by looking in
|
14
|
+
$confdir/puppetdb.conf. If any command submissions fail, the process will
|
15
|
+
be aborted.
|
16
|
+
DESC
|
17
|
+
|
18
|
+
when_invoked do |*args|
|
19
|
+
Puppet::Util::Puppetdb::GlobalCheck.run
|
20
|
+
|
21
|
+
opts = args.pop
|
22
|
+
raise ArgumentError, "Please provide at least one node for deactivation" if args.empty?
|
23
|
+
|
24
|
+
Puppet::Node.indirection.terminus_class = :puppetdb
|
25
|
+
Puppet::Node.indirection.cache_class = nil
|
26
|
+
|
27
|
+
args.inject({}) do |results,node|
|
28
|
+
results.merge node => Puppet::Node.indirection.destroy(node)['uuid']
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
when_rendering(:console) do |value|
|
33
|
+
value.map do |node,uuid|
|
34
|
+
"Submitted '#{CommandDeactivateNode}' for #{node} with UUID #{uuid}"
|
35
|
+
end.join("\n")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
Puppet::Face.define(:node, '0.0.1') do
|
2
|
+
action :status do
|
3
|
+
summary "Fetch the current status for a set of nodes in PuppetDB"
|
4
|
+
arguments "<node> [<node> ...]"
|
5
|
+
description <<-DESC
|
6
|
+
This action will retrieve the current status of a set of nodes from
|
7
|
+
PuppetDB. The information provided includes whether the node is active,
|
8
|
+
and the timestamps of its last catalog and facts. The PuppetDB server is
|
9
|
+
found by looking in $confdir/puppetdb.conf.
|
10
|
+
DESC
|
11
|
+
|
12
|
+
when_invoked do |*args|
|
13
|
+
require 'puppet/network/http_pool'
|
14
|
+
require 'puppet/util/puppetdb'
|
15
|
+
require 'json'
|
16
|
+
|
17
|
+
Puppet::Util::Puppetdb::GlobalCheck.run
|
18
|
+
|
19
|
+
opts = args.pop
|
20
|
+
raise ArgumentError, "Please provide at least one node" if args.empty?
|
21
|
+
|
22
|
+
server = Puppet::Util::Puppetdb.server
|
23
|
+
port = Puppet::Util::Puppetdb.port
|
24
|
+
|
25
|
+
http = Puppet::Network::HttpPool.http_instance(server, port)
|
26
|
+
|
27
|
+
args.map do |node|
|
28
|
+
begin
|
29
|
+
response = http.get("/v3/nodes/#{CGI.escape(node)}", headers)
|
30
|
+
if response.is_a? Net::HTTPSuccess
|
31
|
+
result = JSON.parse(response.body)
|
32
|
+
elsif response.is_a? Net::HTTPNotFound
|
33
|
+
result = {'name' => node}
|
34
|
+
else
|
35
|
+
raise "[#{response.code} #{response.message}] #{response.body.gsub(/[\r\n]/, '')}"
|
36
|
+
end
|
37
|
+
rescue => e
|
38
|
+
Puppet.err "Could not retrieve status for #{node}: #{e}"
|
39
|
+
end
|
40
|
+
result
|
41
|
+
end.compact
|
42
|
+
end
|
43
|
+
|
44
|
+
when_rendering(:console) do |value|
|
45
|
+
value.map do |status|
|
46
|
+
lines = [status['name']]
|
47
|
+
|
48
|
+
# If all we have is the name, then we made this hash because of a 404
|
49
|
+
if status.keys == ['name']
|
50
|
+
lines << "No information known"
|
51
|
+
next lines.join("\n")
|
52
|
+
end
|
53
|
+
|
54
|
+
if status['deactivated']
|
55
|
+
lines << "Deactivated at #{status['deactivated']}"
|
56
|
+
else
|
57
|
+
lines << "Currently active"
|
58
|
+
end
|
59
|
+
|
60
|
+
if status['catalog_timestamp']
|
61
|
+
lines << "Last catalog: #{status['catalog_timestamp']}"
|
62
|
+
else
|
63
|
+
lines << "No catalog received"
|
64
|
+
end
|
65
|
+
|
66
|
+
if status['facts_timestamp']
|
67
|
+
lines << "Last facts: #{status['facts_timestamp']}"
|
68
|
+
else
|
69
|
+
lines << "No facts received"
|
70
|
+
end
|
71
|
+
|
72
|
+
lines.join("\n")
|
73
|
+
end.join("\n\n")
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def headers
|
78
|
+
{
|
79
|
+
"Accept" => "application/json",
|
80
|
+
"Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8",
|
81
|
+
}
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,179 @@
|
|
1
|
+
require 'puppet/face'
|
2
|
+
require 'tmpdir'
|
3
|
+
|
4
|
+
Puppet::Face.define(:storeconfigs, '0.0.1') do
|
5
|
+
copyright "Puppet Labs", 2011
|
6
|
+
license "Apache 2 license"
|
7
|
+
|
8
|
+
summary "Interact with the storeconfigs database"
|
9
|
+
description <<-DESC
|
10
|
+
This subcommand interacts with the ActiveRecord storeconfigs database, and
|
11
|
+
can be used to export a dump of that data which is suitable for import by
|
12
|
+
PuppetDB.
|
13
|
+
DESC
|
14
|
+
|
15
|
+
action :export do
|
16
|
+
summary "Export the storeconfigs database"
|
17
|
+
description <<-DESC
|
18
|
+
Generate a dump of all catalogs from the storeconfigs database, as a
|
19
|
+
tarball which can be imported by PuppetDB. Only exported resources are
|
20
|
+
included; non-exported resources, edges, facts, or other data are
|
21
|
+
omitted. Returns the location of the output.
|
22
|
+
DESC
|
23
|
+
|
24
|
+
when_invoked do |options|
|
25
|
+
require 'puppet/util/puppetdb/global_check'
|
26
|
+
Puppet::Util::Puppetdb::GlobalCheck.run
|
27
|
+
|
28
|
+
require 'puppet/rails'
|
29
|
+
|
30
|
+
tmpdir = Dir.mktmpdir
|
31
|
+
workdir = File.join(tmpdir, 'puppetdb-bak')
|
32
|
+
Dir.mkdir(workdir)
|
33
|
+
|
34
|
+
begin
|
35
|
+
Puppet::Rails.connect
|
36
|
+
|
37
|
+
# Fetch all nodes, including exported resources and their params
|
38
|
+
nodes = Puppet::Rails::Host.all(:include => {:resources => [:param_values, :puppet_tags]},
|
39
|
+
:conditions => {:resources => {:exported => true}})
|
40
|
+
|
41
|
+
catalogs = nodes.map { |node| node_to_catalog_hash(node) }
|
42
|
+
|
43
|
+
catalog_dir = File.join(workdir, 'catalogs')
|
44
|
+
FileUtils.mkdir(catalog_dir)
|
45
|
+
|
46
|
+
catalogs.each do |catalog|
|
47
|
+
filename = File.join(catalog_dir, "#{catalog[:data][:name]}.json")
|
48
|
+
|
49
|
+
File.open(filename, 'w') do |file|
|
50
|
+
file.puts catalog.to_pson
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
node_names = nodes.map(&:name).sort
|
55
|
+
|
56
|
+
timestamp = Time.now
|
57
|
+
|
58
|
+
File.open(File.join(workdir, 'export-metadata.json'), 'w') do |file|
|
59
|
+
metadata = {
|
60
|
+
'timestamp' => timestamp,
|
61
|
+
'command-versions' => {
|
62
|
+
'replace-catalog' => 2,
|
63
|
+
}
|
64
|
+
}
|
65
|
+
|
66
|
+
file.puts metadata.to_pson
|
67
|
+
end
|
68
|
+
|
69
|
+
tarfile = destination_file(timestamp)
|
70
|
+
|
71
|
+
if tar = Puppet::Util.which('tar')
|
72
|
+
execute("cd #{tmpdir} && #{tar} -cf #{tarfile} puppetdb-bak")
|
73
|
+
|
74
|
+
FileUtils.rm_rf(workdir)
|
75
|
+
|
76
|
+
if gzip = Puppet::Util.which('gzip')
|
77
|
+
execute("#{gzip} #{tarfile}")
|
78
|
+
"#{tarfile}.gz"
|
79
|
+
else
|
80
|
+
Puppet.warning "Can't find the `gzip` command to compress the tarball; output will not be compressed"
|
81
|
+
tarfile
|
82
|
+
end
|
83
|
+
else
|
84
|
+
Puppet.warning "Can't find the `tar` command to produce a tarball; output will remain in the temporary working directory"
|
85
|
+
workdir
|
86
|
+
end
|
87
|
+
rescue => e
|
88
|
+
# Clean up if something goes wrong. We don't want to ensure this,
|
89
|
+
# because we want the directory to stick around in the case where they
|
90
|
+
# don't have tar.
|
91
|
+
FileUtils.rm_rf(workdir)
|
92
|
+
raise
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
when_rendering :console do |filename|
|
97
|
+
"Exported storeconfigs data to #{filename}"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
# Returns the location to leave the output. This is really only here for testing. :/
|
102
|
+
def destination_file(timestamp)
|
103
|
+
File.expand_path("storeconfigs-#{timestamp.strftime('%Y%m%d%H%M%S')}.tar")
|
104
|
+
end
|
105
|
+
|
106
|
+
# Execute a command using Puppet's execution static method.
|
107
|
+
#
|
108
|
+
# @param command [Array<String>, String] the command to execute. If it is
|
109
|
+
# an Array the first element should be the executable and the rest of the
|
110
|
+
# elements should be the individual arguments to that executable.
|
111
|
+
# @return [Puppet::Util::Execution::ProcessOutput] output as specified by options
|
112
|
+
# @raise [Puppet::ExecutionFailure] if the executed chiled process did not exit with status == 0 and `failonfail` is
|
113
|
+
# `true`.
|
114
|
+
def execute(command)
|
115
|
+
Puppet::Util::Execution.execute(command)
|
116
|
+
end
|
117
|
+
|
118
|
+
def node_to_catalog_hash(node)
|
119
|
+
resources = node.resources.map { |resource| resource_to_hash(resource) }
|
120
|
+
edges = node.resources.map { |resource| resource_to_edge_hash(resource) }
|
121
|
+
|
122
|
+
{
|
123
|
+
:metadata => {
|
124
|
+
:api_version => 1,
|
125
|
+
},
|
126
|
+
:data => {
|
127
|
+
:name => node.name,
|
128
|
+
:version => node.last_compile || Time.now,
|
129
|
+
:edges => edges,
|
130
|
+
:resources => resources + [stage_main_hash],
|
131
|
+
},
|
132
|
+
}
|
133
|
+
end
|
134
|
+
|
135
|
+
def resource_to_hash(resource)
|
136
|
+
parameters = resource.param_values.inject({}) do |params,param_value|
|
137
|
+
if params.has_key?(param_value.param_name.name)
|
138
|
+
value = [params[param_value.param_name.name],param_value.value].flatten
|
139
|
+
else
|
140
|
+
value = param_value.value
|
141
|
+
end
|
142
|
+
params.merge(param_value.param_name.name => value)
|
143
|
+
end
|
144
|
+
|
145
|
+
tags = resource.puppet_tags.map(&:name).uniq.sort
|
146
|
+
|
147
|
+
hash = {
|
148
|
+
:type => resource.restype,
|
149
|
+
:title => resource.title,
|
150
|
+
:exported => true,
|
151
|
+
:parameters => parameters,
|
152
|
+
:tags => tags,
|
153
|
+
}
|
154
|
+
|
155
|
+
hash[:file] = resource.file if resource.file
|
156
|
+
hash[:line] = resource.line if resource.line
|
157
|
+
|
158
|
+
hash
|
159
|
+
end
|
160
|
+
|
161
|
+
# The catalog *must* have edges, so everything is contained by Stage[main]!
|
162
|
+
def resource_to_edge_hash(resource)
|
163
|
+
{
|
164
|
+
'source' => {'type' => 'Stage', 'title' => 'main'},
|
165
|
+
'target' => {'type' => resource.restype, 'title' => resource.title},
|
166
|
+
'relationship' => 'contains',
|
167
|
+
}
|
168
|
+
end
|
169
|
+
|
170
|
+
def stage_main_hash
|
171
|
+
{
|
172
|
+
:type => 'Stage',
|
173
|
+
:title => 'main',
|
174
|
+
:exported => false,
|
175
|
+
:parameters => {},
|
176
|
+
:tags => ['stage', 'main'],
|
177
|
+
}
|
178
|
+
end
|
179
|
+
end
|