summaryrefslogtreecommitdiff
path: root/tasks/docs.rb
blob: 3d93a4493d2e2703fec1ceb53ae54cdf9eb963eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
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"

    # @param version String
    # @return String Chef Infra Client or Chef Client depending on version
    def branded_chef_client_name(version)
      return "Chef Infra Client" if Gem::Version.new(version) >= Gem::Version.new('15')
      "Chef Client"
    end

    # @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 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 <%= branded_chef_client_name(@introduced) %> <%= @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 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 <%= branded_chef_client_name(@introduced) %> <%= p['introduced'] -%>.*<% end -%>
<% end %>
<% if @properties.empty? %>This resource does not have any properties.\n<% end -%>
<%= boilerplate_content %>
Examples
==========================================
<%= @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)
      @examples = data["examples"]

      t = ERB.new(template, nil, '-')
      File.open("docs_site/resource_#{@name}.rst", "w") do |f|
        f.write t.result(binding)
      end
    end
  end
end