Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(PUP-12065) Add task to generate configuration reference #9459

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/puppet/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ def self.default_certname
end

def self.hostname_fact
Puppet.runtime[:facter].value 'networking.hostname'
ENV['PUPPET_REFERENCES_HOSTNAME'] || Puppet.runtime[:facter].value('networking.hostname')
end

def self.domain_fact
Puppet.runtime[:facter].value 'networking.domain'
ENV['PUPPET_REFERENCES_DOMAIN'] || Puppet.runtime[:facter].value('networking.domain')
end

def self.default_config_file_name
Expand Down
43 changes: 43 additions & 0 deletions rakelib/generate_references.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'tempfile'

OUTPUT_DIR = 'references'
CONFIGURATION_ERB = File.join(__dir__, 'references/configuration.erb')
CONFIGURATION_MD = File.join(OUTPUT_DIR, 'configuration.md')

def render_erb(erb_file, variables)
# Create a binding so only the variables we specify will be visible
template_binding = OpenStruct.new(variables).instance_eval {binding}
ERB.new(File.read(erb_file), trim_mode: '-').result(template_binding)
end

def puppet_doc(reference)
body = %x{bundle exec puppet doc -r #{reference}}
# Remove the first H1 with the title, like "# Metaparameter Reference"
body.sub!(/^# \w+ Reference *$/, '')
body.chomp
end

# This is adapted from https://github.com/puppetlabs/puppet-docs/blob/1a13be3fc6981baa8a96ff832ab090abc986830e/lib/puppet_references/puppet/puppet_doc.rb#L22-L36
def generate_reference(reference, erb, body, output)
sha = %x{git rev-parse HEAD}.chomp
now = Time.now
variables = {
sha: sha,
now: now,
body: body
}
content = render_erb(erb, variables)
File.write(output, content)
puts "Generated #{output}"
end

namespace :references do
desc "Generate configuration reference"
task :configuration do
ENV['PUPPET_REFERENCES_HOSTNAME'] = "(the system's fully qualified hostname)"
ENV['PUPPET_REFERENCES_DOMAIN'] = "(the system's own domain)"

body = puppet_doc('configuration')
generate_reference('configuration', CONFIGURATION_ERB, body, CONFIGURATION_MD)
end
end
13 changes: 13 additions & 0 deletions rakelib/references/configuration.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
layout: default
built_from_commit: <%= sha %>
title: Configuration Reference
toc: columns
canonical: "/puppet/latest/configuration.html"
---

# Configuration Reference

> **NOTE:** This page was generated from the Puppet source code on <%= now %>

<%= body %>
Loading