chef-resource 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +23 -0
- data/LICENSE +201 -0
- data/README.md +264 -0
- data/Rakefile +8 -0
- data/files/lib/chef_resource.rb +24 -0
- data/files/lib/chef_resource/camel_case.rb +23 -0
- data/files/lib/chef_resource/chef.rb +102 -0
- data/files/lib/chef_resource/chef_dsl/chef_cookbook_compiler.rb +44 -0
- data/files/lib/chef_resource/chef_dsl/chef_recipe.rb +10 -0
- data/files/lib/chef_resource/chef_dsl/chef_recipe_dsl_extensions.rb +84 -0
- data/files/lib/chef_resource/chef_dsl/chef_resource_base.rb +12 -0
- data/files/lib/chef_resource/chef_dsl/chef_resource_class_extensions.rb +30 -0
- data/files/lib/chef_resource/chef_dsl/chef_resource_extensions.rb +224 -0
- data/files/lib/chef_resource/chef_dsl/chef_resource_log.rb +54 -0
- data/files/lib/chef_resource/chef_dsl/resource_container_module.rb +80 -0
- data/files/lib/chef_resource/chef_dsl/resource_definition_dsl.rb +128 -0
- data/files/lib/chef_resource/constants.rb +8 -0
- data/files/lib/chef_resource/errors.rb +31 -0
- data/files/lib/chef_resource/lazy_proc.rb +82 -0
- data/files/lib/chef_resource/output/nested_converge.rb +91 -0
- data/files/lib/chef_resource/output/nested_converge/open_resource.rb +113 -0
- data/files/lib/chef_resource/output/region_stream.rb +145 -0
- data/files/lib/chef_resource/output/simple_output.rb +83 -0
- data/files/lib/chef_resource/resource.rb +428 -0
- data/files/lib/chef_resource/resource/resource_log.rb +197 -0
- data/files/lib/chef_resource/resource/resource_type.rb +74 -0
- data/files/lib/chef_resource/resource/struct_property.rb +39 -0
- data/files/lib/chef_resource/resource/struct_property_type.rb +185 -0
- data/files/lib/chef_resource/resource/struct_resource.rb +410 -0
- data/files/lib/chef_resource/resource/struct_resource_base.rb +11 -0
- data/files/lib/chef_resource/resource/struct_resource_type.rb +275 -0
- data/files/lib/chef_resource/simple_struct.rb +121 -0
- data/files/lib/chef_resource/type.rb +371 -0
- data/files/lib/chef_resource/types.rb +4 -0
- data/files/lib/chef_resource/types/boolean.rb +16 -0
- data/files/lib/chef_resource/types/byte_size.rb +10 -0
- data/files/lib/chef_resource/types/date_time_type.rb +18 -0
- data/files/lib/chef_resource/types/date_type.rb +18 -0
- data/files/lib/chef_resource/types/float_type.rb +28 -0
- data/files/lib/chef_resource/types/integer_type.rb +53 -0
- data/files/lib/chef_resource/types/interval.rb +21 -0
- data/files/lib/chef_resource/types/path.rb +39 -0
- data/files/lib/chef_resource/types/pathname_type.rb +34 -0
- data/files/lib/chef_resource/types/string_type.rb +16 -0
- data/files/lib/chef_resource/types/symbol_type.rb +18 -0
- data/files/lib/chef_resource/types/uri_type.rb +37 -0
- data/files/lib/chef_resource/version.rb +3 -0
- data/spec/integration/chef.rb +81 -0
- data/spec/integration/struct_spec.rb +611 -0
- data/spec/integration/struct_state_spec.rb +538 -0
- data/spec/integration/type_spec.rb +1123 -0
- data/spec/integration/validation_spec.rb +207 -0
- data/spec/support/spec_support.rb +7 -0
- metadata +167 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a27971d67bec23229aaee17383b7e44a965a92b3
|
4
|
+
data.tar.gz: 804df782696fee0a92b2c3ff67497f65b12c88ea
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b922d78fda97186c833ec62ac4ddf8bdce5bbdf9b8b94d1971381ca12c18e4a2e93073ccbad51aeea50b52208be78eb9c0a657a14dd655e3adc0ac7e8fb656b8
|
7
|
+
data.tar.gz: 35d4b22cc5c8b1df343e87fa6dc5cb9a5cbdbb3b836cbd769530e58414e68c6c3d28299b125f945f69a02324afb7f98c1b5034687ca46e7a78817fdb3f63ed6c
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Resource Cookbook CHANGELOG
|
2
|
+
===========================
|
3
|
+
|
4
|
+
0.2.2 (4/1/2015)
|
5
|
+
-----------------
|
6
|
+
|
7
|
+
- Fix issue with uploading via berks (@damascus)
|
8
|
+
|
9
|
+
0.2.1 (2/19/2015)
|
10
|
+
-----------------
|
11
|
+
|
12
|
+
- Rename ChefResource.resource -> Chef.resource
|
13
|
+
- Get cookbook include working again
|
14
|
+
|
15
|
+
0.2 (2/19/2015)
|
16
|
+
---------------
|
17
|
+
|
18
|
+
- Initial publication
|
19
|
+
- Single-file resources
|
20
|
+
- depends "resource" is how you use it
|
21
|
+
- Read/write resources with load/converge
|
22
|
+
- define, defaults, resource keywords
|
23
|
+
- strongly typed properties and coercion
|
data/LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,264 @@
|
|
1
|
+
[![Stories in Ready](https://badge.waffle.io/chef-cookbooks/resource.png?label=ready&title=Ready)](https://waffle.io/chef-cookbooks/resource)[![Build Status](https://travis-ci.org/chef-cookbooks/resource.svg?branch=master)](https://travis-ci.org/chef-cookbooks/resource)[![Gitter chat](https://badges.gitter.im/chef-cookbooks/resource.png)](https://gitter.im/chef-cookbooks/resource)
|
2
|
+
|
3
|
+
The Resource Cookbook
|
4
|
+
=====================
|
5
|
+
|
6
|
+
Chef Resources are incredibly important to creating good, useful, reusable cookbooks. Yet people often don't create them because it's too hard. The resource cookbook aims to change that.
|
7
|
+
|
8
|
+
The resource cookbook is an attempt to make Chef Resources significantly easier and more fun to create, while being even more powerful. It does this by:
|
9
|
+
|
10
|
+
- Vastly simplifying resource writing so you just make a "resource" and "recipe" in a single file.
|
11
|
+
- Making good primitive resources easier to build with builtin test-and-set support.
|
12
|
+
- Allowing users to *read* data from resources, making them significantly more useful.
|
13
|
+
- Allowing users to easily customize resource definitions in-place, adding defaults and tweaks.
|
14
|
+
|
15
|
+
I am looking for people to try this out and give feedback. IT IS EXPERIMENTAL. There are bugs (though I generally don't know what they are). Things will change. There is a lot of feedback to gather and that will affect what it does. But this has been revised many, many times in an attempt to produce something close enough to right that I hope it won't change *much,* or in fundamental ways.
|
16
|
+
|
17
|
+
This is also unfinished in that there are more features to be added: chief among them are nested properties (Hash, Array, Set and Struct), nested resources (github.organization.repository) and recipe semantics (immediate mode and parallel recipes). 0.1 is a stop along the way, but a very significant one that defines the basis for Resources.
|
18
|
+
|
19
|
+
For an in-depth comparison of Chef Resources and ChefResource Resources, see the [0.1 release notes](docs/0.1-release.md).
|
20
|
+
|
21
|
+
Getting Started
|
22
|
+
---------------
|
23
|
+
To get started, upload the resource cookbook and add this to your `metadata.rb`:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
depends "resource"
|
27
|
+
```
|
28
|
+
|
29
|
+
ChefResource is now loaded in, and the following features are available:
|
30
|
+
- `Chef.resource`, `ChefResource.define` and `ChefResource.defaults` will define and customize resources
|
31
|
+
- All recipes everywhere can call ChefResource resources
|
32
|
+
- The `resources` directory in your cookbook now creates ChefResource resources (via `Chef.resource`).
|
33
|
+
- Recipes in your cookbook have `resource`, `define` and `defaults` available (which call the `ChefResource.` equivalent)
|
34
|
+
|
35
|
+
NOTE: cookbooks that depend on your cookbook will *not* automatically be ChefResourceed. Only cookbooks that explicitly depend on the resource cookbook will be transformed.
|
36
|
+
|
37
|
+
Compatibility
|
38
|
+
-------------
|
39
|
+
|
40
|
+
ChefResource works with Chef 12. It also does not change *anything* about existing cookbook behavior, and only affects cookbooks who have `depends "resource"` in their metadata. Resources you create with ChefResource are available to all cookbooks, including existing cookbooks which do not directly depend on the "resource" cookbook.
|
41
|
+
|
42
|
+
Define: Dashing Off a Quick Resource
|
43
|
+
------------------------------------
|
44
|
+
|
45
|
+
Say you noticed that you're creating a series of "user home directories," like this:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
# Old recipe
|
49
|
+
user 'jkeiser' do
|
50
|
+
group 'users'
|
51
|
+
end
|
52
|
+
|
53
|
+
directory "/home/jkeiser" do
|
54
|
+
owner 'jkeiser'
|
55
|
+
group 'users'
|
56
|
+
mode 0700
|
57
|
+
end
|
58
|
+
|
59
|
+
file "/home/jkeiser/.bashrc" do
|
60
|
+
owner 'jkeiser'
|
61
|
+
group 'users'
|
62
|
+
mode 0700
|
63
|
+
content "sh /sys/global_bashrc"
|
64
|
+
end
|
65
|
+
|
66
|
+
user 'fnichol' do
|
67
|
+
group 'users'
|
68
|
+
end
|
69
|
+
|
70
|
+
directory "/home/fnichol" do
|
71
|
+
owner 'fnichol'
|
72
|
+
group 'users'
|
73
|
+
mode 0700
|
74
|
+
end
|
75
|
+
|
76
|
+
file "/home/fnichol/.bashrc" do
|
77
|
+
owner 'fnichol'
|
78
|
+
group 'users'
|
79
|
+
mode 0700
|
80
|
+
content "sh /sys/global_bashrc"
|
81
|
+
end
|
82
|
+
|
83
|
+
...
|
84
|
+
```
|
85
|
+
|
86
|
+
That's a lot of repetition! How do you make repetition better in Chef? A resource! Just write this in your recipe:
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
define :user_bundle, :username, primary_group: 'users' do
|
90
|
+
user username do
|
91
|
+
group primary_group
|
92
|
+
end
|
93
|
+
|
94
|
+
directory "/home/#{username}" do
|
95
|
+
owner username
|
96
|
+
group primary_group
|
97
|
+
mode 0700
|
98
|
+
end
|
99
|
+
|
100
|
+
file "/home/#{username}/.bashrc" do
|
101
|
+
content "sh /sys/global_bashrc"
|
102
|
+
|
103
|
+
owner username
|
104
|
+
group primary_group
|
105
|
+
mode 0700
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
# Now let's use our resource!
|
110
|
+
user_bundle 'jkeiser' do
|
111
|
+
end
|
112
|
+
user_bundle 'fnichol' do
|
113
|
+
end
|
114
|
+
user_bundle 'blargh' do
|
115
|
+
end
|
116
|
+
```
|
117
|
+
|
118
|
+
Much more concise, much more readable, much easier to change!
|
119
|
+
|
120
|
+
Creating a Simple Resource
|
121
|
+
--------------------------
|
122
|
+
If you want to really customize the properties of a resource, or want to do more interesting things, you can always create a `resources` file. ChefResource appropriates the LWRP `resources` directory, so you create a `resources/user_bundle.rb`:
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
# resources/user_bundle.rb
|
126
|
+
property :username, String, identity: true
|
127
|
+
property :primary_group, String
|
128
|
+
property :home_dir, Path, relative_to: '/home' do
|
129
|
+
default { username }
|
130
|
+
end
|
131
|
+
|
132
|
+
recipe do
|
133
|
+
user username do
|
134
|
+
group primary_group
|
135
|
+
end
|
136
|
+
|
137
|
+
directory home_dir do
|
138
|
+
owner username
|
139
|
+
group primary_group
|
140
|
+
mode 0700
|
141
|
+
end
|
142
|
+
|
143
|
+
file "#{home_dir}/.bashrc" do
|
144
|
+
content "sh /sys/global_bashrc"
|
145
|
+
|
146
|
+
owner username
|
147
|
+
group primary_group
|
148
|
+
mode 0700
|
149
|
+
end
|
150
|
+
end
|
151
|
+
```
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
# recipes/default.rb
|
155
|
+
test_user_bundle 'jkeiser' do
|
156
|
+
end
|
157
|
+
test_user_bundle 'fnichol' do
|
158
|
+
end
|
159
|
+
test_user_bundle 'blargh' do
|
160
|
+
end
|
161
|
+
```
|
162
|
+
|
163
|
+
Some features here:
|
164
|
+
- `property` is how you define a named thing
|
165
|
+
- `identity: true` means that when you write `user_bundle 'jkeiser'`, `username` will be set to `jkeiser`.
|
166
|
+
- `String` and `Path` are property *types*. It means the resource will not allow the user to set the property to something else, like 1 or false, which isn't a valid path or username.
|
167
|
+
- `relative_to: '/home'` is a modifier for `Path` saying "when the user says `home_dir 'jkeiser2'`, set `home_dir` to `/home/jkeiser2`."
|
168
|
+
- `default { username }` is a *computed default*: if the user does not set `home_dir`, `home_dir` will be `/home/<username>`
|
169
|
+
|
170
|
+
NOTE: you can define a resource *anywhere* by writing `Chef.resource :name do ... end`, and writing `property` and `recipe` statements inside. You can do this in `libraries`, `recipes` or even outside Chef.
|
171
|
+
|
172
|
+
Building Primitive Resources: Load and Converge
|
173
|
+
-----------------------------------------------
|
174
|
+
|
175
|
+
Up until now, we've been showing "compound" resources whose primary job is to wrap other resources like `file`, `package` and `service`. This is enough for huge numbers of people, and the primitive resources handle the work of "test-and-set," showing the "(up to date)" if nothing needs to change, or the green text when a change occurs.
|
176
|
+
|
177
|
+
Sometimes you need to build a real primitive resource, when `file` `package` and `service` aren't enough. When this comes up, ChefResource handles the work of test-and-set for you with the `load` and `converge` methods.
|
178
|
+
|
179
|
+
Consider a simple file resource:
|
180
|
+
|
181
|
+
```ruby
|
182
|
+
# resources/file.rb
|
183
|
+
property :path, Path, identity: true
|
184
|
+
property :mode, Integer
|
185
|
+
property :content, String
|
186
|
+
|
187
|
+
recipe do
|
188
|
+
converge do
|
189
|
+
File.chmod(mode, path)
|
190
|
+
IO.write(path, content)
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
def load
|
195
|
+
mode File.stat(path).mode
|
196
|
+
content IO.read(path)
|
197
|
+
end
|
198
|
+
```
|
199
|
+
|
200
|
+
The The resource cookbook things here:
|
201
|
+
|
202
|
+
- `converge do ...` handles test-and-set. It will check to see if the user has changed `mode` or `content` from its real value (as read in by `load`). If so, it will print an appropriate message in green text showing what's changed, and mark the resource as updated.
|
203
|
+
- `def load` loads the *current values* of the actual resource. This is called when `converge` happens, or when the user asks for a value that hasn't been filled in (like if they ask for `mode` and haven't set it yet).
|
204
|
+
|
205
|
+
What's also interesting is you have now defined a *read API*. If the user does `file('/x.txt').content`, then it will show you the file contents of `/x.txt`.
|
206
|
+
|
207
|
+
Customizing Resources in Cookbooks: Defaults
|
208
|
+
--------------------------------------------
|
209
|
+
|
210
|
+
As a *user* of a resource, there are a number of times where you're repeating something over and over. How many of us have typed this:
|
211
|
+
|
212
|
+
```ruby
|
213
|
+
file '/x.txt' do
|
214
|
+
owner 'jkeiser'
|
215
|
+
group 'users'
|
216
|
+
mode 0755
|
217
|
+
content 'x'
|
218
|
+
end
|
219
|
+
file '/y.txt' do
|
220
|
+
owner 'jkeiser'
|
221
|
+
group 'users'
|
222
|
+
mode 0755
|
223
|
+
content 'y'
|
224
|
+
end
|
225
|
+
file '/z.txt' do
|
226
|
+
owner 'jkeiser'
|
227
|
+
group 'users'
|
228
|
+
mode 0755
|
229
|
+
content 'z'
|
230
|
+
end
|
231
|
+
```
|
232
|
+
|
233
|
+
ChefResource gives you a quick way to redefine the defaults of a resource:
|
234
|
+
|
235
|
+
```ruby
|
236
|
+
defaults :my_file, :file, owner: 'jkeiser', group: 'users', mode: 0755
|
237
|
+
my_file '/x.txt' do
|
238
|
+
content 'x'
|
239
|
+
end
|
240
|
+
my_file '/y.txt' do
|
241
|
+
content 'y'
|
242
|
+
end
|
243
|
+
my_file '/z.txt' do
|
244
|
+
content 'z'
|
245
|
+
end
|
246
|
+
```
|
247
|
+
|
248
|
+
You can also specialize resources with more complex behavior:
|
249
|
+
|
250
|
+
```ruby
|
251
|
+
# A version of "file" that assumes the group == the username
|
252
|
+
resource :my_file, :file do
|
253
|
+
attribute :group, String do
|
254
|
+
default { username }
|
255
|
+
end
|
256
|
+
end
|
257
|
+
```
|
258
|
+
|
259
|
+
Contributing
|
260
|
+
------------
|
261
|
+
|
262
|
+
PRs are welcome at [github](https://github.com/chef-cookbooks/resource)!
|
263
|
+
|
264
|
+
To give feedback, file issues in [github](https://github.com/chef-cookbooks/resource/issues) or chat on [Gitter](https://gitter.im/chef-cookbooks/resource).
|