summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2019-06-27 09:53:20 -0700
committerGitHub <noreply@github.com>2019-06-27 09:53:20 -0700
commit5e2ca501715a7614c64de62d9a396f11de986dbe (patch)
treedb6e3d72355ea1ba66ca408d89dc4dbebbf77c7b
parent5d07ddc8c210724f634389c64e649449b9fb7d5d (diff)
parent028d2db5a428efd6f80b56b7e8eac0f2c4ee824e (diff)
downloadchef-5e2ca501715a7614c64de62d9a396f11de986dbe.tar.gz
Merge pull request #8694 from chef/doc_generator
Add rake task for generating the docs site content
-rw-r--r--.gitignore3
-rw-r--r--Rakefile1
-rw-r--r--docs/dev/how_to/releasing_chef_infra.md7
-rwxr-xr-xtasks/docs.rb337
4 files changed, 343 insertions, 5 deletions
diff --git a/.gitignore b/.gitignore
index d88442ba7c..179f62af56 100644
--- a/.gitignore
+++ b/.gitignore
@@ -68,3 +68,6 @@ chef-config/pkg
chef-bin/.bundle
chef-bin/Gemfile.lock
chef-bin/pkg
+
+# auto generated docs from the docs.rb rake task
+docs_site
diff --git a/Rakefile b/Rakefile
index 2d8d5c9593..063abe1a85 100644
--- a/Rakefile
+++ b/Rakefile
@@ -20,6 +20,7 @@
require_relative "tasks/rspec"
require_relative "tasks/dependencies"
require_relative "tasks/announce"
+require_relative "tasks/docs"
ENV["CHEF_LICENSE"] = "accept-no-persist"
diff --git a/docs/dev/how_to/releasing_chef_infra.md b/docs/dev/how_to/releasing_chef_infra.md
index 022cb6d3ef..eee8d9abba 100644
--- a/docs/dev/how_to/releasing_chef_infra.md
+++ b/docs/dev/how_to/releasing_chef_infra.md
@@ -28,11 +28,8 @@ If there are any new or updated resources, the docs site will need to be updated
#### Resource Documentation Automation
-1. Checkout the `doc_generator` branch in `chef/chef`
-2. Rebase the branch against master
-3. Run `bundle install`
-4. Run `bundle exec ./docs.rb`, which generates resource_FOO.rst files in the root of the git repo.
-5. Compare the relevant generated files with the docs site content in the `chef_master/source` directory. The generated files are missing some content, such as action descriptions, and don't have perfect formatting, so this is a bit of an art form.
+1. Run `rake docs_site:resources` to generate content to a `docs_site` directory
+2. Compare the relevant generated files with the docs site content in the `chef_master/source` directory. The generated files are missing some content, such as action descriptions, and don't have perfect formatting, so this is a bit of an art form.
## Release Chef Infra Client
diff --git a/tasks/docs.rb b/tasks/docs.rb
new file mode 100755
index 0000000000..0adebce0b6
--- /dev/null
+++ b/tasks/docs.rb
@@ -0,0 +1,337 @@
+namespace :docs_site do
+
+ desc "Generate resource documentation .rst pages in a docs_site directory"
+
+ task :resources do
+ Encoding.default_external = Encoding::UTF_8
+
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "lib")))
+
+ require "chef/resource_inspector"
+ require "erb"
+ require "fileutils"
+
+ # @return [String, nil] a pretty defaul value string or nil if we want to skip it
+ def pretty_default(default)
+ return nil if default.nil? || default == "" || default == "lazy default"
+ if default.is_a?(String)
+ return default.inspect unless default[0] == ":"
+ end
+ default
+ end
+
+ # generate the top example resource block example text
+ # @param properties Array<Hash>
+ # @return String
+ def generate_resource_block(resource_name, properties)
+ padding_size = largest_property_name(properties) + 6
+
+ # build the resource string with property spacing between property names and comments
+ text = " #{resource_name} 'name' do\n"
+ properties.each do |p|
+ text << " #{p['name'].ljust(padding_size)}"
+ text << friendly_types_list(p["is"])
+ text << " # default value: 'name' unless specified" if p["name_property"]
+ text << " # default value: #{pretty_default(p['default'])}" unless pretty_default(p["default"]).nil?
+ text << "\n"
+ end
+ text << " #{'action'.ljust(padding_size)}Symbol # defaults to :#{@default_action.first} if not specified\n"
+ text << " end"
+ text
+ end
+
+ # we need to know how much space to leave so columns line up
+ # @return String
+ def largest_property_name(properties)
+ if properties.empty?
+ 6 # we'll include "action" even without properties and it's 6 chars long
+ else
+ properties.max_by { |x| x["name"].size }["name"].size
+ end
+ end
+
+ # given an array of properties print out a single comma separated string
+ # handling commas / and properly and plural vs. singular wording depending
+ # on the number of properties
+ # @return String
+ def friendly_properly_list(arr)
+ return nil if arr.empty? # resources w/o properties
+
+ props = arr.map { |x| "``#{x['name']}``" }
+
+ # build the text string containing all properties bolded w/ punctuation
+ if props.size > 1
+ props[-1] = "and #{props[-1]}"
+ end
+ text = props.size == 2 ? props.join(" ") : props.join(", ")
+ text << ( props.size > 1 ? " are the properties" : " is the property" )
+ text << " available to this resource."
+ text
+ end
+
+ # given an array of types print out a single comma separated string
+ # handling a nil value that needs to be printed as "nil" and TrueClass/FalseClass
+ # which needs to be "true" and "false"
+ # @return String
+ def friendly_types_list(arr)
+ fixed_arr = Array(arr).map do |x|
+ case x
+ when "TrueClass"
+ "true"
+ when "FalseClass"
+ "false"
+ else
+ x
+ end
+ end
+ fixed_arr.compact.join(", ")
+ end
+
+ # Makes sure the resource name is bolded within the description
+ # @return String
+ def bolded_description(name, description)
+ return nil if description.nil? # handle resources missing descriptions
+ description.gsub( "#{name} ", "**#{name}** ").split("Note: ").first.strip
+ end
+
+ def note_text(description)
+ return nil if description.nil?
+ note = description.split("Note: ")[1]
+ if note
+ <<-HEREDOC
+
+ .. note::
+
+ #{note}
+ HEREDOC
+ end
+ end
+
+ def boilerplate_content
+ <<~HEREDOC
+ Common Resource Functionality
+ =====================================================
+
+ Chef resources include common properties, notifications, and resource guards.
+
+ Common Properties
+ -----------------------------------------------------
+
+ .. tag resources_common_properties
+
+ The following properties are common to every resource:
+
+ ``ignore_failure``
+ **Ruby Type:** true, false | **Default Value:** ``false``
+
+ Continue running a recipe if a resource fails for any reason.
+
+ ``retries``
+ **Ruby Type:** Integer | **Default Value:** ``0``
+
+ The number of attempts to catch exceptions and retry the resource.
+
+ ``retry_delay``
+ **Ruby Type:** Integer | **Default Value:** ``2``
+
+ The retry delay (in seconds).
+
+ ``sensitive``
+ **Ruby Type:** true, false | **Default Value:** ``false``
+
+ Ensure that sensitive resource data is not logged by the Chef Infra Client.
+
+ .. end_tag
+
+ Notifications
+ -----------------------------------------------------
+ ``notifies``
+ **Ruby Type:** Symbol, 'Chef::Resource[String]'
+
+ .. tag resources_common_notification_notifies
+
+ A resource may notify another resource to take action when its state changes. Specify a ``'resource[name]'``, the ``:action`` that resource should take, and then the ``:timer`` for that action. A resource may notify more than one resource; use a ``notifies`` statement for each resource to be notified.
+
+ .. end_tag
+
+ .. tag resources_common_notification_timers
+
+ A timer specifies the point during the Chef Infra Client run at which a notification is run. The following timers are available:
+
+ ``:before``
+ Specifies that the action on a notified resource should be run before processing the resource block in which the notification is located.
+
+ ``:delayed``
+ Default. Specifies that a notification should be queued up, and then executed at the end of the Chef Infra Client run.
+
+ ``:immediate``, ``:immediately``
+ Specifies that a notification should be run immediately, per resource notified.
+
+ .. end_tag
+
+ .. tag resources_common_notification_notifies_syntax
+
+ The syntax for ``notifies`` is:
+
+ .. code-block:: ruby
+
+ notifies :action, 'resource[name]', :timer
+
+ .. end_tag
+
+ ``subscribes``
+ **Ruby Type:** Symbol, 'Chef::Resource[String]'
+
+ .. tag resources_common_notification_subscribes
+
+ A resource may listen to another resource, and then take action if the state of the resource being listened to changes. Specify a ``'resource[name]'``, the ``:action`` to be taken, and then the ``:timer`` for that action.
+
+ Note that ``subscribes`` does not apply the specified action to the resource that it listens to - for example:
+
+ .. code-block:: ruby
+
+ file '/etc/nginx/ssl/example.crt' do
+ mode '0600'
+ owner 'root'
+ end
+
+ service 'nginx' do
+ subscribes :reload, 'file[/etc/nginx/ssl/example.crt]', :immediately
+ end
+
+ In this case the ``subscribes`` property reloads the ``nginx`` service whenever its certificate file, located under ``/etc/nginx/ssl/example.crt``, is updated. ``subscribes`` does not make any changes to the certificate file itself, it merely listens for a change to the file, and executes the ``:reload`` action for its resource (in this example ``nginx``) when a change is detected.
+
+ .. end_tag
+
+ .. tag resources_common_notification_timers
+
+ A timer specifies the point during the Chef Infra Client run at which a notification is run. The following timers are available:
+
+ ``:before``
+ Specifies that the action on a notified resource should be run before processing the resource block in which the notification is located.
+
+ ``:delayed``
+ Default. Specifies that a notification should be queued up, and then executed at the end of the Chef Infra Client run.
+
+ ``:immediate``, ``:immediately``
+ Specifies that a notification should be run immediately, per resource notified.
+
+ .. end_tag
+
+ .. tag resources_common_notification_subscribes_syntax
+
+ The syntax for ``subscribes`` is:
+
+ .. code-block:: ruby
+
+ subscribes :action, 'resource[name]', :timer
+
+ .. end_tag
+
+ Guards
+ -----------------------------------------------------
+
+ .. tag resources_common_guards
+
+ A guard property can be used to evaluate the state of a node during the execution phase of the Chef Infra Client run. Based on the results of this evaluation, a guard property is then used to tell the Chef Infra Client if it should continue executing a resource. A guard property accepts either a string value or a Ruby block value:
+
+ * A string is executed as a shell command. If the command returns ``0``, the guard is applied. If the command returns any other value, then the guard property is not applied. String guards in a **powershell_script** run Windows PowerShell commands and may return ``true`` in addition to ``0``.
+ * A block is executed as Ruby code that must return either ``true`` or ``false``. If the block returns ``true``, the guard property is applied. If the block returns ``false``, the guard property is not applied.
+
+ A guard property is useful for ensuring that a resource is idempotent by allowing that resource to test for the desired state as it is being executed, and then if the desired state is present, for the Chef Infra Client to do nothing.
+
+ .. end_tag
+ .. tag resources_common_guards_properties
+
+ The following properties can be used to define a guard that is evaluated during the execution phase of the Chef Infra Client run:
+
+ ``not_if``
+ Prevent a resource from executing when the condition returns ``true``.
+
+ ``only_if``
+ Allow a resource to execute only if the condition returns ``true``.
+
+ .. end_tag
+ HEREDOC
+ end
+
+ template = %{=====================================================
+ <%= @name %> resource
+ =====================================================
+ `[edit on GitHub] <https://github.com/chef/chef-web-docs/blob/master/chef_master/source/resource_<%= @name %>.rst>`__
+
+ <%= bolded_description(@name, @description) %>
+ <%= note_text(@description) %>
+ <% unless @introduced.nil? %>
+ **New in Chef Infra Client <%= @introduced %>.**
+ <% end %>
+ Syntax
+ =====================================================
+ The <%= @name %> resource has the following syntax:
+
+ .. code-block:: ruby
+
+ <%= @resource_block %>
+
+ where:
+
+ * ``<%= @name %>`` is the resource.
+ * ``name`` is the name given to the resource block.
+ * ``action`` identifies which steps the Chef Infra Client will take to bring the node into the desired state.
+ <% unless @property_list.nil? %>* <%= @property_list %><% end %>
+
+ Actions
+ =====================================================
+
+ The <%= @name %> resource has the following actions:
+ <% @actions.each do |a| %>
+ ``:<%= a %>``
+ <% if a == @default_action %>Default. <% end %>Description here.
+ <% end %>
+ ``:nothing``
+ .. tag resources_common_actions_nothing
+
+ This resource block does not act unless notified by another resource to take action. Once notified, this resource block either runs immediately or is queued up to run at the end of the Chef Infra Client run.
+
+ .. end_tag
+
+ Properties
+ =====================================================
+
+ The <%= @name %> resource has the following properties:
+ <% @properties.each do |p| %>
+ ``<%= p['name'] %>``
+ **Ruby Type:** <%= friendly_types_list(p['is']) %><% unless pretty_default(p['default']).nil? %> | **Default Value:** ``<%= pretty_default(p['default']) %>``<% end %><% if p['required'] %> | ``REQUIRED``<% end %><% if p['deprecated'] %> | ``DEPRECATED``<% end %><% if p['name_property'] %> | **Default Value:** ``The resource block's name``<% end %>
+
+ <%= p['description'] %>
+ <% unless p['introduced'].nil? %>\n *New in Chef Infra Client <%= p['introduced'] %>.*<% end %>
+ <% end %>
+ <% if @properties.empty? %>This resource does not have any properties.\n<% end %>
+ <%= boilerplate_content %>
+ Examples
+ ==========================================
+ }
+
+ FileUtils.mkdir_p "docs_site"
+ resources = Chef::JSONCompat.parse(ResourceInspector.inspect)
+ resources.each do |resource, data|
+ next if ["scm", "whyrun_safe_ruby_block", "l_w_r_p_base", "user_resource_abstract_base_class", "linux_user", "pw_user", "aix_user", "dscl_user", "solaris_user", "windows_user", ""].include?(resource)
+ puts "Writing out #{resource}."
+ @name = resource
+ @description = data["description"]
+ @default_action = data["default_action"]
+ @actions = (data["actions"] - ["nothing"]).sort
+ @examples = data["examples"]
+ @introduced = data["introduced"]
+ @preview = data["preview"]
+ @properties = data["properties"].reject { |v| v["name"] == "name" }.sort_by! { |v| v["name"] }
+ @resource_block = generate_resource_block(resource, @properties)
+ @property_list = friendly_properly_list(@properties)
+
+ t = ERB.new(template)
+ File.open("docs_site/resource_#{@name}.rst", "w") do |f|
+ f.write t.result(binding)
+ end
+ end
+ end
+end