Skip to content

Commit cc89237

Browse files
committed
(PUP-12057) Add rake task to generate resource types overview.md
bundle exec references:type Run puppet strings, convert the `resource_types` and generate the type overview page in `references/types/overview.md`
1 parent 76a4570 commit cc89237

File tree

2 files changed

+296
-0
lines changed

2 files changed

+296
-0
lines changed

rakelib/generate_references.rake

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ require 'tempfile'
22

33
OUTPUT_DIR = 'references'
44
MANDIR = File.join(OUTPUT_DIR, 'man')
5+
TYPES_DIR = File.join(OUTPUT_DIR, 'types')
56

67
CONFIGURATION_ERB = File.join(__dir__, 'references/configuration.erb')
78
CONFIGURATION_MD = File.join(OUTPUT_DIR, 'configuration.md')
@@ -15,6 +16,8 @@ FUNCTION_MD = File.join(OUTPUT_DIR, 'function.md')
1516
MAN_OVERVIEW_ERB = File.join(__dir__, 'references/man/overview.erb')
1617
MAN_OVERVIEW_MD = File.join(MANDIR, "overview.md")
1718
MAN_ERB = File.join(__dir__, 'references/man.erb')
19+
TYPES_OVERVIEW_ERB = File.join(__dir__, 'references/types/overview.erb')
20+
TYPES_OVERVIEW_MD = File.join(TYPES_DIR, 'overview.md')
1821

1922
def render_erb(erb_file, variables)
2023
# Create a binding so only the variables we specify will be visible
@@ -43,6 +46,91 @@ def generate_reference(reference, erb, body, output)
4346
puts "Generated #{output}"
4447
end
4548

49+
# Based on https://github.com/puppetlabs/puppet-docs/blob/1a13be3fc6981baa8a96ff832ab090abc986830e/lib/puppet_references/puppet/type_strings.rb#L19-L99
50+
def extract_resource_types(strings_data)
51+
strings_data['resource_types'].reduce(Hash.new) do |memo, type|
52+
memo[ type['name'] ] = {
53+
'description' => type['docstring']['text'],
54+
'features' => (type['features'] || []).reduce(Hash.new) {|memo, feature|
55+
memo[feature['name']] = feature['description']
56+
memo
57+
},
58+
'providers' => strings_data['providers'].select {|provider|
59+
provider['type_name'] == type['name']
60+
}.reduce(Hash.new) {|memo, provider|
61+
description = provider['docstring']['text']
62+
if provider['commands'] || provider['confines'] || provider['defaults']
63+
description = description + "\n"
64+
end
65+
if provider['commands']
66+
description = description + "\n* Required binaries: `#{provider['commands'].values.sort.join('`, `')}`"
67+
end
68+
if provider['confines']
69+
description = description + "\n* Confined to: `#{provider['confines'].map{|fact,val| "#{fact} == #{val}"}.join('`, `')}`"
70+
end
71+
if provider['defaults']
72+
description = description + "\n* Default for: `#{provider['defaults'].map{|fact,val| "#{fact} == #{val}"}.join('`, `')}`"
73+
end
74+
if provider['features']
75+
description = description + "\n* Supported features: `#{provider['features'].sort.join('`, `')}`"
76+
end
77+
memo[provider['name']] = {
78+
'features' => (provider['features'] || []),
79+
'description' => description
80+
}
81+
memo
82+
},
83+
'attributes' => (type['parameters'] || []).reduce(Hash.new) {|memo, attribute|
84+
description = attribute['description'] || ''
85+
if attribute['default']
86+
description = description + "\n\nDefault: `#{attribute['default']}`"
87+
end
88+
if attribute['values']
89+
description = description + "\n\nAllowed values:\n\n" + attribute['values'].map{|val| "* `#{val}`"}.join("\n")
90+
end
91+
memo[attribute['name']] = {
92+
'description' => description,
93+
'kind' => 'parameter',
94+
'namevar' => attribute['isnamevar'] ? true : false,
95+
'required_features' => attribute['required_features'],
96+
}
97+
memo
98+
}.merge( (type['properties'] || []).reduce(Hash.new) {|memo, attribute|
99+
description = attribute['description'] || ''
100+
if attribute['default']
101+
description = description + "\n\nDefault: `#{attribute['default']}`"
102+
end
103+
if attribute['values']
104+
description = description + "\n\nAllowed values:\n\n" + attribute['values'].map{|val| "* `#{val}`"}.join("\n")
105+
end
106+
memo[attribute['name']] = {
107+
'description' => description,
108+
'kind' => 'property',
109+
'namevar' => false,
110+
'required_features' => attribute['required_features'],
111+
}
112+
memo
113+
}).merge( (type['checks'] || []).reduce(Hash.new) {|memo, attribute|
114+
description = attribute['description'] || ''
115+
if attribute['default']
116+
description = description + "\n\nDefault: `#{attribute['default']}`"
117+
end
118+
if attribute['values']
119+
description = description + "\n\nAllowed values:\n\n" + attribute['values'].map{|val| "* `#{val}`"}.join("\n")
120+
end
121+
memo[attribute['name']] = {
122+
'description' => description,
123+
'kind' => 'check',
124+
'namevar' => false,
125+
'required_features' => attribute['required_features'],
126+
}
127+
memo
128+
})
129+
}
130+
memo
131+
end
132+
end
133+
46134
namespace :references do
47135
desc "Generate configuration reference"
48136
task :configuration do
@@ -198,4 +286,46 @@ namespace :references do
198286
puts "Generated #{output}"
199287
end
200288
end
289+
290+
task :type do
291+
FileUtils.mkdir_p(TYPES_DIR)
292+
293+
# Locate puppet-strings
294+
begin
295+
require 'puppet-strings'
296+
require 'puppet-strings/version'
297+
rescue LoadError
298+
abort("Run `bundle config set with documentation` and `bundle update` to install the `puppet-strings` gem.")
299+
end
300+
301+
sha = %x{git rev-parse HEAD}.chomp
302+
303+
# Based on https://github.com/puppetlabs/puppet-docs/blob/1a13be3fc6981baa8a96ff832ab090abc986830e/lib/puppet_references/puppet/strings.rb#L25-L26
304+
Tempfile.create do |tmpfile|
305+
puts "Running puppet strings #{PuppetStrings::VERSION}"
306+
PuppetStrings.generate(['lib/puppet/type/*.rb'], json: true, path: tmpfile.path)
307+
strings_data = JSON.load_file(tmpfile.path)
308+
309+
# convert strings output to data the overview ERB template expects
310+
type_data = extract_resource_types(strings_data)
311+
312+
# Generate overview.md
313+
# Based on https://github.com/puppetlabs/puppet-docs/blob/1a13be3fc6981baa8a96ff832ab090abc986830e/lib/puppet_references/puppet/type.rb#L40-L47
314+
types = type_data.keys.reject do |type|
315+
type == 'component' || type == 'whit'
316+
end
317+
318+
variables = {
319+
sha: sha,
320+
now: Time.now,
321+
title: 'Resource types overview',
322+
types: types
323+
}
324+
325+
# Render overview page
326+
content = render_erb(TYPES_OVERVIEW_ERB, variables)
327+
File.write(TYPES_OVERVIEW_MD, content)
328+
puts "Generated #{TYPES_OVERVIEW_MD}"
329+
end
330+
end
201331
end

rakelib/references/types/overview.erb

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
---
2+
layout: default
3+
built_from_commit: <%= sha %>
4+
title: <%= title %>
5+
canonical: "/puppet/latest/types/overview.md"
6+
---
7+
8+
# <%= title %>
9+
10+
> **NOTE:** This page was generated from the Puppet source code on <%= now %>
11+
12+
## List of resource types
13+
14+
15+
<% types.each do |type| -%>
16+
* [<%= type %>](./<%= type %>.md)
17+
<% end -%>
18+
19+
20+
## About resource types
21+
22+
### Built-in types and custom types
23+
24+
This is the documentation for Puppet's built-in resource types and providers. Additional resource types are distributed in Puppet modules.
25+
26+
You can find and install modules by browsing the
27+
[Puppet Forge](http://forge.puppet.com). See each module's documentation for
28+
information on how to use its custom resource types. For more information about creating custom types, see [Custom resources](/docs/puppet/latest/custom_resources.html).
29+
30+
> As of Puppet 6.0, some resource types were removed from Puppet and repackaged as individual modules. These supported type modules are still included in the `puppet-agent` package, so you don't have to download them from the Forge. See the complete list of affected types in the [supported type modules](#supported-type-modules-in-puppet-agent) section.
31+
32+
### Declaring resources
33+
34+
To manage resources on a target system, declare them in Puppet
35+
manifests. For more details, see
36+
[the resources page of the Puppet language reference.](/docs/puppet/latest/lang_resources.html)
37+
38+
You can also browse and manage resources interactively using the
39+
`puppet resource` subcommand; run `puppet resource --help` for more information.
40+
41+
### Namevars and titles
42+
43+
All types have a special attribute called the _namevar_. This is the attribute
44+
used to uniquely identify a resource on the target system.
45+
46+
Each resource has a specific namevar attribute, which is listed on this page in
47+
each resource's reference. If you don't specify a value for the namevar, its
48+
value defaults to the resource's _title_.
49+
50+
**Example of a title as a default namevar:**
51+
52+
```puppet
53+
file { '/etc/passwd':
54+
owner => 'root',
55+
group => 'root',
56+
mode => '0644',
57+
}
58+
```
59+
60+
In this code, `/etc/passwd` is the _title_ of the file resource.
61+
62+
The file type's namevar is `path`. Because we didn't provide a `path` value in
63+
this example, the value defaults to the title, `/etc/passwd`.
64+
65+
**Example of a namevar:**
66+
67+
```puppet
68+
file { 'passwords':
69+
path => '/etc/passwd',
70+
owner => 'root',
71+
group => 'root',
72+
mode => '0644',
73+
}
74+
```
75+
76+
This example is functionally similar to the previous example. Its `path`
77+
namevar attribute has an explicitly set value separate from the title, so
78+
its name is still `/etc/passwd`.
79+
80+
Other Puppet code can refer to this resource as `File['/etc/passwd']` to
81+
declare relationships.
82+
83+
### Attributes, parameters, properties
84+
85+
The _attributes_ (sometimes called _parameters_) of a resource determine its
86+
desired state. They either directly modify the system (internally, these are
87+
called "properties") or they affect how the resource behaves (for instance,
88+
adding a search path for `exec` resources or controlling directory recursion
89+
on `file` resources).
90+
91+
### Providers
92+
93+
_Providers_ implement the same resource type on different kinds of systems.
94+
They usually do this by calling out to external commands.
95+
96+
Although Puppet automatically selects an appropriate default provider, you
97+
can override the default with the `provider` attribute. (For example, `package`
98+
resources on Red Hat systems default to the `yum` provider, but you can specify
99+
`provider => gem` to install Ruby libraries with the `gem` command.)
100+
101+
Providers often specify binaries that they require. Fully qualified binary
102+
paths indicate that the binary must exist at that specific path, and
103+
unqualified paths indicate that Puppet searches for the binary using the
104+
shell path.
105+
106+
### Features
107+
108+
_Features_ are abilities that some providers might not support. Generally, a
109+
feature corresponds to some allowed values for a resource attribute.
110+
111+
This is often the case with the `ensure` attribute. In most types, Puppet
112+
doesn't create new resources when omitting `ensure` but still modifies existing
113+
resources to match specifications in the manifest. However, in some types this
114+
isn't always the case, or additional values provide more granular control. For
115+
example, if a `package` provider supports the `purgeable` feature, you can
116+
specify `ensure => purged` to delete configuration files installed by the
117+
package.
118+
119+
Resource types define the set of features they can use, and providers can
120+
declare which features they provide.
121+
122+
## Puppet 6.0 type changes
123+
124+
In Puppet 6.0, we removed some of Puppet's built-in types and moved them into individual modules.
125+
126+
### Supported type modules in `puppet-agent`
127+
128+
The following types are included in supported modules on the Forge. However, they are also included in the `puppet-agent` package, so you do not have to install them separately. See each module's README for detailed information about that type.
129+
130+
- [`augeas`](https://forge.puppet.com/puppetlabs/augeas_core)
131+
- [`cron`](https://forge.puppet.com/puppetlabs/cron_core)
132+
- [`host`](https://forge.puppet.com/puppetlabs/host_core)
133+
- [`mount`](https://forge.puppet.com/puppetlabs/mount_core)
134+
- [`scheduled_task`](https://forge.puppet.com/puppetlabs/scheduled_task)
135+
- [`selboolean`](https://forge.puppet.com/puppetlabs/selinux_core)
136+
- [`selmodule`](https://forge.puppet.com/puppetlabs/selinux_core)
137+
- [`ssh_authorized_key`](https://forge.puppet.com/puppetlabs/sshkeys_core)
138+
- [`sshkey`](https://forge.puppet.com/puppetlabs/sshkeys_core)
139+
- [`yumrepo`](https://forge.puppet.com/puppetlabs/yumrepo_core)
140+
- [`zfs`](https://forge.puppet.com/puppetlabs/zfs_core)
141+
- [`zone`](https://forge.puppet.com/puppetlabs/zone_core)
142+
- [`zpool`](https://forge.puppet.com/puppetlabs/zfs_core)
143+
144+
### Type modules available on the Forge
145+
146+
The following types are contained in modules that are maintained, but are not repackaged into Puppet agent. If you need to use them, you must install the modules separately.
147+
148+
- [`k5login`](https://forge.puppet.com/puppetlabs/k5login_core)
149+
- [`mailalias`](https://forge.puppet.com/puppetlabs/mailalias_core)
150+
- [`maillist`](https://forge.puppet.com/puppetlabs/maillist_core)
151+
152+
### Deprecated types
153+
154+
The following types were deprecated with Puppet 6.0.0. They are available in modules, but are not updated. If you need to use them, you must install the modules separately.
155+
156+
- [`computer`](https://forge.puppet.com/puppetlabs/macdslocal_core)
157+
- [`interface`](https://github.com/puppetlabs/puppetlabs-network_device_core) (Use the updated [`cisco_ios module`](https://forge.puppet.com/puppetlabs/cisco_ios/readme) instead.
158+
- [`macauthorization`](https://forge.puppet.com/puppetlabs/macdslocal_core)
159+
- [`mcx`](https://forge.puppet.com/puppetlabs/macdslocal_core)
160+
- [The Nagios types](https://forge.puppet.com/puppetlabs/nagios_core)
161+
- [`router`](https://github.com/puppetlabs/puppetlabs-network_device_core) (Use the updated [`cisco_ios module`](https://forge.puppet.com/puppetlabs/cisco_ios/readme) instead.
162+
- [`vlan`](https://github.com/puppetlabs/puppetlabs-network_device_core) (Use the updated [`cisco_ios module`](https://forge.puppet.com/puppetlabs/cisco_ios/readme) instead.
163+
164+
## Puppet core types
165+
166+
For a list of core Puppet types, see the [core types cheat sheet][core-types-cheatsheet].

0 commit comments

Comments
 (0)