Skip to content

Commit c5c7fc8

Browse files
committed
(PUP-12065) Add task to generate configuration reference
bundle exec rake references:configuration It runs `puppet doc -r configuration` to generate references/configuration.md. In order for the output to not be dependent on the hostname/domain facts, allow them to be overridden as an environment variable. This can't be done using FACTER_*, because structured facts like `networking.hostname` can't be overridden by an env variable. To determine if any meaningful changes have been made, you can run: git diff -I built_from_commit -I 'This page was generated from the Puppet source code' references/
1 parent 267684a commit c5c7fc8

File tree

4 files changed

+2302
-2
lines changed

4 files changed

+2302
-2
lines changed

lib/puppet/settings.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ def self.default_certname
8181
end
8282

8383
def self.hostname_fact
84-
Puppet.runtime[:facter].value 'networking.hostname'
84+
ENV['PUPPET_REFERENCES_HOSTNAME'] || Puppet.runtime[:facter].value('networking.hostname')
8585
end
8686

8787
def self.domain_fact
88-
Puppet.runtime[:facter].value 'networking.domain'
88+
ENV['PUPPET_REFERENCES_DOMAIN'] || Puppet.runtime[:facter].value('networking.domain')
8989
end
9090

9191
def self.default_config_file_name

rakelib/generate_references.rake

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require 'tempfile'
2+
3+
OUTPUT_DIR = 'references'
4+
CONFIGURATION_ERB = File.join(__dir__, 'references/configuration.erb')
5+
CONFIGURATION_MD = File.join(OUTPUT_DIR, 'configuration.md')
6+
7+
def render_erb(erb_file, variables)
8+
# Create a binding so only the variables we specify will be visible
9+
template_binding = OpenStruct.new(variables).instance_eval {binding}
10+
ERB.new(File.read(erb_file), trim_mode: '-').result(template_binding)
11+
end
12+
13+
def puppet_doc(reference)
14+
body = %x{bundle exec puppet doc -r #{reference}}
15+
# Remove the first H1 with the title, like "# Metaparameter Reference"
16+
body.sub!(/^# \w+ Reference *$/, '')
17+
body.chomp
18+
end
19+
20+
# This is adapted from https://github.com/puppetlabs/puppet-docs/blob/1a13be3fc6981baa8a96ff832ab090abc986830e/lib/puppet_references/puppet/puppet_doc.rb#L22-L36
21+
def generate_reference(reference, erb, body, output)
22+
sha = %x{git rev-parse HEAD}.chomp
23+
now = Time.now
24+
variables = {
25+
sha: sha,
26+
now: now,
27+
body: body
28+
}
29+
content = render_erb(erb, variables)
30+
File.write(output, content)
31+
puts "Generated #{output}"
32+
end
33+
34+
namespace :references do
35+
desc "Generate configuration reference"
36+
task :configuration do
37+
ENV['PUPPET_REFERENCES_HOSTNAME'] = "(the system's fully qualified hostname)"
38+
ENV['PUPPET_REFERENCES_DOMAIN'] = "(the system's own domain)"
39+
40+
body = puppet_doc('configuration')
41+
generate_reference('configuration', CONFIGURATION_ERB, body, CONFIGURATION_MD)
42+
end
43+
end

rakelib/references/configuration.erb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
layout: default
3+
built_from_commit: <%= sha %>
4+
title: Configuration Reference
5+
toc: columns
6+
canonical: "/puppet/latest/configuration.html"
7+
---
8+
9+
# Configuration Reference
10+
11+
> **NOTE:** This page was generated from the Puppet source code on <%= now %>
12+
13+
<%= body %>

0 commit comments

Comments
 (0)