puppet-lint-legacy_facts-check 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MzMwOGE1YmM4ZWI4ODExNTA1ZGZmYTc2ZDcwNWEyMDA3MmY2NGFkNg==
5
+ data.tar.gz: !binary |-
6
+ OTZmMWQxNTgyZDA2YmZmODI5NTAyMTQyNDA3MWQwMjIxNDA5Y2NhOA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ OGM4ZjI1M2M3NmZkMTYxNGZlZGJkNmFlZjBjZGI0NDI3ODJlYTVkNDlkNGEw
10
+ YmMzNzBkMWEwYzdhYjU3M2VjNDc0MWMzMjY1NDllM2I3YjAwNWZhOWQzZTNj
11
+ MTdmNDI0MmE3ZDYwM2ExZWY5ZmY4OWE5MzBlZGUzNDlmODY5OWI=
12
+ data.tar.gz: !binary |-
13
+ YTViODU4MTU3ZDIzNTliOGMwMGEyYzhkZWVlYTIyMTQxOTgxMzE3ZTA5NDI0
14
+ YjljNzViODZmMjA2ZTI1MGY2N2ZiZTAxNjhiNWU2ZTI5ZmNkYzQyOGNhY2Zi
15
+ ZGMzY2I5MGY0NTMyYjViYTljZWNmMjhmY2ZiNmJiOGUzNThmMzk=
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,184 @@
1
+ [![Build Status](https://travis-ci.org/mmckinst/puppet-lint-legacy_facts-check.svg?branch=master)](https://travis-ci.org/mmckinst/puppet-lint-legacy_facts-check)
2
+ [![Gem](https://img.shields.io/gem/v/puppet-lint-legacy_facts-check.svg?maxAge=2592000)](https://rubygems.org/gems/puppet-lint-legacy_facts-check)
3
+
4
+ A pupet-lint to check you are not using legacy facts like `$::operatingsystem`
5
+ or `$facts['operatingsystem']`. You should use the new structured facts like
6
+ `$facts['os']['name']` instead
7
+
8
+ ## Overview
9
+
10
+ ## Limitations
11
+
12
+ Some facts have no equivalent in the structured fact list:
13
+
14
+ ### `memoryfree_mb`
15
+
16
+ There is no fact that returns exclusively in MiB.
17
+
18
+ The closest equivalent is `$facts['memory']['system'][available']` or
19
+ `$facts['memory']['system']['available_bytes']`.
20
+
21
+ See
22
+ [facter documentation on memory](https://docs.puppet.com/facter/3.4/core_facts.html#memory).
23
+
24
+ ### `memorysize_mb`
25
+
26
+ There is no fact that returns exclusively in MiB.
27
+
28
+ The closest equivalent is `$facts['memory']['system']['total']` or
29
+ `$facts['memory']['system']['total_bytes']`.
30
+
31
+ See [facter documentation on memory](https://docs.puppet.com/facter/3.4/core_facts.html#memory).
32
+
33
+ ### `swapfree_mb`
34
+
35
+ There is no fact that returns exclusively in MiB.
36
+
37
+ The closest equivalent is `$facts['memory']['swap']['available']` or
38
+ `$facts['memory']['swap']['available_bytes']`.
39
+
40
+ See [facter documentation on memory](https://docs.puppet.com/facter/3.4/core_facts.html#memory).
41
+
42
+ ### `swapsize_mb`
43
+
44
+ There is no fact that returns exclusively in MiB.
45
+
46
+ The closest equivalent is `$facts['memory']['swap']['used']` or
47
+ `$facts['memory']['swap']['used_bytes']`.
48
+
49
+ See [facter documentation on memory](https://docs.puppet.com/facter/3.4/core_facts.html#memory).
50
+
51
+ ### `blockdevices`
52
+
53
+ This returns a string containing all block devices separated by a comma.
54
+
55
+ This can be duplicated using
56
+ [puppetlabs/stdlib](https://github.com/puppetlabs/puppetlabs-stdlib) and the
57
+ following: `join(keys($facts['disks']), ',')`
58
+
59
+ ### `interfaces`
60
+
61
+ This returns a string containing all interfaces separated by a comma.
62
+
63
+ This can be duplicated using
64
+ [puppetlabs/stdlib](https://github.com/puppetlabs/puppetlabs-stdlib) and the
65
+ following: `join(keys($facts['networking']['interfaces']), ',')`.
66
+
67
+ ### `zones`
68
+
69
+ This returns a string containing all zone names separated by a comma.
70
+
71
+ This can be duplicated using
72
+ [puppetlabs/stdlib](https://github.com/puppetlabs/puppetlabs-stdlib) and the
73
+ following: `join(keys($facts['solaris_zones']['zones']), ',')`
74
+
75
+ ### `sshfp_dsa`
76
+
77
+ This returns a string containing both the SHA1 and SHA256 fingerprint for the
78
+ DSA algorithm.
79
+
80
+ This can be duplicated using the following string:
81
+ `"$facts['ssh']['dsa']['fingerprints']['sha1']
82
+ $facts['ssh']['dsa']['fingerprints']['sha256']"`
83
+
84
+ ### `sshfp_ecdsa`
85
+
86
+ This returns a string containing both the SHA1 and SHA256 fingerprint for the
87
+ ECDSA algorithm.
88
+
89
+ This can be duplicated using the following string:
90
+ `"$facts['ssh']['ecdsa']['fingerprints']['sha1']
91
+ $facts['ssh']['ecdsa']['fingerprints']['sha256']"`
92
+
93
+ ### `sshfp_ed25519`
94
+
95
+ This returns a string containing both the SHA1 and SHA256 fingerprint for the
96
+ Ed25519 algorithm.
97
+
98
+ This can be duplicated using the following string:
99
+ `"$facts['ssh']['ed25519']['fingerprints']['sha1']
100
+ $facts['ssh']['ed25519']['fingerprints']['sha256']"`
101
+
102
+ ### `sshfp_rsa`
103
+
104
+ This returns a string containing both the SHA1 and SHA256 fingerprint for the
105
+ RSA algorithm.
106
+
107
+ This can be duplicated using the following string:
108
+ `"$facts['ssh']['rsa']['fingerprints']['sha1']
109
+ $facts['ssh']['rsa']['fingerprints']['sha256']"`
110
+
111
+ ## Installing
112
+
113
+ ### From the command line
114
+
115
+ ```shell
116
+ $ gem install puppet-lint-legacy_facts-check
117
+ ```
118
+
119
+ ### In a Gemfile
120
+
121
+ ```ruby
122
+ gem 'puppet-lint-legacy_facts-check', :require => false
123
+ ```
124
+
125
+ ## Checks
126
+
127
+ #### What you have done
128
+
129
+ ```puppet
130
+ $package_name = $::operatingsystem {
131
+ 'CentOS' => 'httpd',
132
+ 'Debian' => 'apache2',
133
+ }
134
+ ```
135
+
136
+ ```puppet
137
+ $package_name = $facts['operatingsystem'] {
138
+ 'CentOS' => 'httpd',
139
+ 'Debian' => 'apache2',
140
+ }
141
+ ```
142
+
143
+ #### What you should have done
144
+
145
+ ```puppet
146
+ $service_name = $facts['os']['name'] {
147
+ 'CentOS' => 'httpd',
148
+ 'Debian' => 'apache2',
149
+ }
150
+ ```
151
+
152
+ #### Disabling the check
153
+
154
+ To disable this check, you can add `--no-legacy_facts` to your puppet-lint
155
+ command line.
156
+
157
+ ```shell
158
+ $ puppet-lint --no-legacy_facts path/to/file.pp
159
+ ```
160
+
161
+ Alternatively, if you’re calling puppet-lint via the Rake task, you should
162
+ insert the following line to your `Rakefile`.
163
+
164
+ ```ruby
165
+ PuppetLint.configuration.send('disable_legacy_facts')
166
+ ```
167
+
168
+ ## License
169
+
170
+ ```
171
+ Copyright 2016 Mark McKinstry
172
+
173
+ Licensed under the Apache License, Version 2.0 (the "License");
174
+ you may not use this file except in compliance with the License.
175
+ You may obtain a copy of the License at
176
+
177
+ http://www.apache.org/licenses/LICENSE-2.0
178
+
179
+ Unless required by applicable law or agreed to in writing, software
180
+ distributed under the License is distributed on an "AS IS" BASIS,
181
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
182
+ See the License for the specific language governing permissions and
183
+ limitations under the License.
184
+ ```
@@ -0,0 +1,121 @@
1
+ PuppetLint.new_check(:legacy_facts) do
2
+ UNCONVERTIBLE_FACTS = ['memoryfree_mb', 'memorysize_mb', 'swapfree_mb',
3
+ 'swapsize_mb', 'blockdevices', 'interfaces', 'zones',
4
+ 'sshfp_dsa', 'sshfp_ecdsa', 'sshfp_ed25519',
5
+ 'sshfp_rsa']
6
+
7
+ REGEX_FACTS = [/^blockdevice_(?<devicename>.*)_(?<attribute>model|size|vendor)$/,
8
+ /^(?<attribute>ipaddress|ipaddress6|macaddress|mtu|netmask|netmask6|network|network6)_(?<interface>.*)$/,
9
+ /^processor(?<id>[0-9]+)$/,
10
+ /^sp_(?<name>.*)$/,
11
+ /^ssh(?<algorithm>.*)key$/,
12
+ /^ldom_(?<name>.*)$/,
13
+ /^zone_(?<name>.*)_(?<attribute>brand|iptype|name|uuid|id|path|status)$/]
14
+
15
+ EASY_FACTS = {
16
+ 'architecture' => "facts['os']['architecture']",
17
+ 'augeasversion' => "facts['augeas']['version']",
18
+ 'bios_release_date' => "facts['dmi']['bios']['release_date']",
19
+ 'bios_vendor' => "facts['dmi']['bios']['vendor']",
20
+ 'bios_version' => "facts['dmi']['bios']['version']",
21
+ 'boardassettag' => "facts['dmi']['board']['asset_tag']",
22
+ 'boardmanufacturer' => "facts['dmi']['board']['manufacturer']",
23
+ 'boardproductname' => "facts['dmi']['board']['product']",
24
+ 'boardserialnumber' => "facts['dmi']['board']['serial_number']",
25
+ 'chassisassettag' => "facts['dmi']['chassis']['asset_tag']",
26
+ 'chassistype' => "facts['dmi']['chassis']['type']",
27
+ 'domain' => "facts['networking']['domain']",
28
+ 'fqdn' => "facts['networking']['fqdn']",
29
+ 'gid' => "facts['identity']['gid']",
30
+ 'hardwareisa' => "facts['processors']['isa']",
31
+ 'hardwaremodel' => "facts['os']['hardware']",
32
+ 'hostname' => "facts['networking']['hostname']",
33
+ 'id' => "facts['identity']['uid']",
34
+ 'ipaddress' => "facts['networking']['ip']",
35
+ 'ipaddress6' => "facts['networking']['ip6']",
36
+ 'lsbdistcodename' => "facts['distro']['codename']",
37
+ 'lsbdistdescription' => "facts['distro']['description']",
38
+ 'lsbdistid' => "facts['distro']['id']",
39
+ 'lsbdistrelease' => "facts['distro']['release']['full']",
40
+ 'lsbmajdistrelease' => "facts['distro']['release']['major']",
41
+ 'lsbminordistrelease' => "facts['distro']['release']['minor']",
42
+ 'lsbrelease' => "facts['distro']['release']['specification']",
43
+ 'macaddress' => "facts['networking']['mac']",
44
+ 'macosx_buildversion' => "facts['os']['build']",
45
+ 'macosx_productname' => "facts['os']['product']",
46
+ 'macosx_productversion' => "facts['os']['version']['full']",
47
+ 'macosx_productversion_major' => "facts['os']['version']['major']",
48
+ 'macosx_productversion_minor' => "facts['os']['version']['minor']",
49
+ 'manufacturer' => "facts['dmi']['manufacturer']",
50
+ 'memoryfree' => "facts['memory']['system']['available']",
51
+ 'memorysize' => "facts['memory']['system']['total']",
52
+ 'netmask' => "facts['networking']['netmask']",
53
+ 'netmask6' => "facts['networking']['netmask6']",
54
+ 'network' => "facts['networking']['network']",
55
+ 'network6' => "facts['networking']['network6']",
56
+ 'operatingsystem' => "facts['os']['name']",
57
+ 'operatingsystemmajrelease' => "facts['os']['release']['major']",
58
+ 'operatingsystemrelease' => "facts['os']['release']['full']",
59
+ 'osfamily' => "facts['os']['family']",
60
+ 'physicalprocessorcount' => "facts['processors']['physicalcount']",
61
+ 'processorcount' => "facts['processors']['count']",
62
+ 'productname' => "facts['dmi']['product']['name']",
63
+ 'rubyplatform' => "facts['ruby']['platform']",
64
+ 'rubysitedir' => "facts['ruby']['sitedir']",
65
+ 'rubyversion' => "facts['ruby']['version']",
66
+ 'selinux' => "facts['os']['selinux']['enabled']",
67
+ 'selinux_config_mode' => "facts['os']['selinux']['config_mode']",
68
+ 'selinux_config_policy' => "facts['os']['selinux']['config_policy']",
69
+ 'selinux_current_mode' => "facts['os']['selinux']['current_mode']",
70
+ 'selinux_enforced' => "facts['os']['selinux']['enforced']",
71
+ 'selinux_policyversion' => "facts['os']['selinux']['policy_version']",
72
+ 'serialnumber' => "facts['dmi']['product']['serial_number']",
73
+ 'swapencrypted' => "facts['memory']['swap']['encrypted']",
74
+ 'swapfree' => "facts['memory']['swap']['available']",
75
+ 'swapsize' => "facts['memory']['swap']['total']",
76
+ 'system32' => "facts['os']['windows']['system32']",
77
+ 'uptime' => "facts['system_uptime']['uptime']",
78
+ 'uptime_days' => "facts['system_uptime']['days']",
79
+ 'uptime_hours' => "facts['system_uptime']['hours']",
80
+ 'uptime_seconds' => "facts['system_uptime']['seconds']",
81
+ 'uuid' => "facts['dmi']['product']['uuid']",
82
+ 'xendomains' => "facts['xen']['domains']",
83
+ 'zonename' => "facts['solaris_zones']['current']",
84
+ }
85
+ def check
86
+ tokens.select { |x| x.type == :VARIABLE}.each do |token|
87
+ fact_name = token.value.sub(/^(::)?(facts|trusted)?\[?['"]?/, '').sub(/['"]?\]?$/ ,'')
88
+ if EASY_FACTS.include?(fact_name) or UNCONVERTIBLE_FACTS.include?(fact_name) or fact_name.match(Regexp.union(REGEX_FACTS)) then
89
+ notify :warning, {
90
+ :message => 'legacy fact',
91
+ :line => token.line,
92
+ :column => token.column,
93
+ :token => token,
94
+ }
95
+ end
96
+ end
97
+ end
98
+
99
+ def fix(problem)
100
+ fact_name = problem[:token].value.sub(/^(::)?(facts|trusted)?\[?['"]?/, '').sub(/['"]?\]?$/ ,'')
101
+ if EASY_FACTS.include?(fact_name)
102
+ problem[:token].value = EASY_FACTS[fact_name]
103
+ elsif fact_name.match(Regexp.union(REGEX_FACTS))
104
+ if m = fact_name.match(/^blockdevice_(?<devicename>.*)_(?<attribute>model|size|vendor)$/)
105
+ problem[:token].value = "facts['disks']['"<< m['devicename'] << "']['" << m['attribute'] << "']"
106
+ elsif m = fact_name.match(/^(?<attribute>ipaddress|ipaddress6|macaddress|mtu|netmask|netmask6|network|network6)_(?<interface>.*)$/)
107
+ problem[:token].value = "facts['networking']['interfaces']['"<< m['interface'] << "']['" << m['attribute'].sub('address', '') << "']"
108
+ elsif m = fact_name.match(/^processor(?<id>[0-9]+)$/)
109
+ problem[:token].value = "facts['processors']['models'][" << m['id'] << "]"
110
+ elsif m = fact_name.match(/^sp_(?<name>.*)$/)
111
+ problem[:token].value = "facts['system_profiler']['" << m['name'] << "']"
112
+ elsif m = fact_name.match(/^ssh(?<algorithm>.*)key$/)
113
+ problem[:token].value = "facts['ssh']['" << m['algorithm'] << "']['key']"
114
+ elsif m = fact_name.match(/^ldom_(?<name>.*)$/)
115
+ problem[:token].value = "facts['ldom']['" << m['name'] << "']"
116
+ elsif m = fact_name.match(/^zone_(?<name>.*)_(?<attribute>brand|iptype|name|uuid|id|path|status)$/)
117
+ problem[:token].value = "facts['solaris_zones']['zones']['" << m['name'] << "']['" << m['attribute'] << "']"
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,232 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'legacy_facts' do
4
+ let(:msg) { 'legacy fact' }
5
+ context 'with fix disabled' do
6
+ context "fact variable using modern $facts['os']['family'] hash" do
7
+ let(:code) { "$facts['os']['family']" }
8
+
9
+ it 'should not detect any problems' do
10
+ expect(problems).to have(0).problem
11
+ end
12
+ end
13
+
14
+ context "fact variable using legacy $facts['osfamily']" do
15
+ let(:code) { "$facts['osfamily']" }
16
+
17
+ it 'should only detect a single problem' do
18
+ expect(problems).to have(1).problem
19
+ end
20
+ end
21
+
22
+ context "fact variable using legacy $osfamily" do
23
+ let(:code) { "$osfamily" }
24
+
25
+ it 'should only detect a single problem' do
26
+ expect(problems).to have(1).problem
27
+ end
28
+ end
29
+
30
+ context "fact variable using legacy $::osfamily" do
31
+ let(:code) { "$::osfamily" }
32
+
33
+ it 'should only detect a single problem' do
34
+ expect(problems).to have(1).problem
35
+ end
36
+ end
37
+
38
+ context "fact variable using legacy $::blockdevice_sda_model" do
39
+ let(:code) { "$::blockdevice_sda_model" }
40
+
41
+ it 'should only detect a single problem' do
42
+ expect(problems).to have(1).problem
43
+ end
44
+ end
45
+
46
+ context "fact variable using legacy $facts['ipaddress6_em2']" do
47
+ let(:code) { "$facts['ipaddress6_em2']" }
48
+
49
+ it 'should only detect a single problem' do
50
+ expect(problems).to have(1).problem
51
+ end
52
+ end
53
+
54
+ context "fact variable using legacy $::zone_foobar_uuid" do
55
+ let(:code) { "$::zone_foobar_uuid" }
56
+
57
+ it 'should only detect a single problem' do
58
+ expect(problems).to have(1).problem
59
+ end
60
+ end
61
+
62
+ context "fact variable using legacy $::processor314" do
63
+ let(:code) { "$::processor314" }
64
+
65
+ it 'should only detect a single problem' do
66
+ expect(problems).to have(1).problem
67
+ end
68
+ end
69
+
70
+ context "fact variable using legacy $::sp_l3_cache" do
71
+ let(:code) { "$::sp_l3_cache" }
72
+
73
+ it 'should only detect a single problem' do
74
+ expect(problems).to have(1).problem
75
+ end
76
+ end
77
+
78
+ context "fact variable using legacy $::sshrsakey" do
79
+ let(:code) { "$::sshrsakey" }
80
+
81
+ it 'should only detect a single problem' do
82
+ expect(problems).to have(1).problem
83
+ end
84
+ end
85
+ end
86
+
87
+
88
+ context 'with fix enabled' do
89
+ before do
90
+ PuppetLint.configuration.fix = true
91
+ end
92
+
93
+ after do
94
+ PuppetLint.configuration.fix = false
95
+ end
96
+
97
+ context "fact variable using modern $facts['os']['family'] hash" do
98
+ let(:code) { "$facts['os']['family']" }
99
+
100
+ it 'should not detect any problems' do
101
+ expect(problems).to have(0).problem
102
+ end
103
+ end
104
+
105
+ context "fact variable using legacy $facts['osfamily']" do
106
+ let(:code) { "$facts['osfamily']" }
107
+
108
+ it 'should only detect a single problem' do
109
+ expect(problems).to have(1).problem
110
+ end
111
+
112
+ it 'should fix the problem' do
113
+ expect(problems).to contain_fixed(msg).on_line(1).in_column(1)
114
+ end
115
+
116
+ it 'should use the facts hash' do
117
+ expect(manifest).to eq("$facts['os']['family']")
118
+ end
119
+ end
120
+
121
+ context "fact variable using legacy $osfamily" do
122
+ let(:code) { "$osfamily" }
123
+
124
+ it 'should only detect a single problem' do
125
+ expect(problems).to have(1).problem
126
+ end
127
+
128
+ it 'should fix the problem' do
129
+ expect(problems).to contain_fixed(msg).on_line(1).in_column(1)
130
+ end
131
+
132
+ it 'should use the facts hash' do
133
+ expect(manifest).to eq("$facts['os']['family']")
134
+ end
135
+
136
+ end
137
+
138
+ context "fact variable using legacy $::osfamily" do
139
+ let(:code) { "$::osfamily" }
140
+
141
+ it 'should only detect a single problem' do
142
+ expect(problems).to have(1).problem
143
+ end
144
+
145
+ it 'should fix the problem' do
146
+ expect(problems).to contain_fixed(msg).on_line(1).in_column(1)
147
+ end
148
+
149
+ it 'should use the facts hash' do
150
+ expect(manifest).to eq("$facts['os']['family']")
151
+ end
152
+ end
153
+
154
+ context "fact variable using legacy $::memoryfree_mb" do
155
+ let(:code) { "$::memoryfree_mb" }
156
+
157
+ it 'should only detect a single problem' do
158
+ expect(problems).to have(1).problem
159
+ end
160
+
161
+ it 'should continue to use the legacy fact' do
162
+ expect(manifest).to eq("$::memoryfree_mb")
163
+ end
164
+ end
165
+
166
+ context "fact variable using legacy $::blockdevice_sda_model" do
167
+ let(:code) { "$::blockdevice_sda_model" }
168
+
169
+ it 'should only detect a single problem' do
170
+ expect(problems).to have(1).problem
171
+ end
172
+ it 'should use the facts hash' do
173
+ expect(manifest).to eq("$facts['disks']['sda']['model']")
174
+ end
175
+ end
176
+
177
+ context "fact variable using legacy $facts['ipaddress6_em2']" do
178
+ let(:code) { "$facts['ipaddress6_em2']" }
179
+
180
+ it 'should only detect a single problem' do
181
+ expect(problems).to have(1).problem
182
+ end
183
+ it 'should use the facts hash' do
184
+ expect(manifest).to eq("$facts['networking']['interfaces']['em2']['ip6']")
185
+ end
186
+ end
187
+
188
+ context "fact variable using legacy $::zone_foobar_uuid" do
189
+ let(:code) { "$::zone_foobar_uuid" }
190
+
191
+ it 'should only detect a single problem' do
192
+ expect(problems).to have(1).problem
193
+ end
194
+ it 'should use the facts hash' do
195
+ expect(manifest).to eq("$facts['solaris_zones']['zones']['foobar']['uuid']")
196
+ end
197
+ end
198
+
199
+ context "fact variable using legacy $::processor314" do
200
+ let(:code) { "$::processor314" }
201
+
202
+ it 'should only detect a single problem' do
203
+ expect(problems).to have(1).problem
204
+ end
205
+ it 'should use the facts hash' do
206
+ expect(manifest).to eq("$facts['processors']['models'][314]")
207
+ end
208
+ end
209
+
210
+ context "fact variable using legacy $::sp_l3_cache" do
211
+ let(:code) { "$::sp_l3_cache" }
212
+
213
+ it 'should only detect a single problem' do
214
+ expect(problems).to have(1).problem
215
+ end
216
+ it 'should use the facts hash' do
217
+ expect(manifest).to eq("$facts['system_profiler']['l3_cache']")
218
+ end
219
+ end
220
+
221
+ context "fact variable using legacy $::sshrsakey" do
222
+ let(:code) { "$::sshrsakey" }
223
+
224
+ it 'should only detect a single problem' do
225
+ expect(problems).to have(1).problem
226
+ end
227
+ it 'should use the facts hash' do
228
+ expect(manifest).to eq("$facts['ssh']['rsa']['key']")
229
+ end
230
+ end
231
+ end
232
+ end
@@ -0,0 +1,3 @@
1
+ require 'puppet-lint'
2
+
3
+ PuppetLint::Plugins.load_spec_helper
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: puppet-lint-legacy_facts-check
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mark McKinstry
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-08-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: puppet-lint
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-its
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-collection_matchers
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: ! " A pupet-lint to check you are not using legacy facts like `$::operatingsystem`\n
84
+ \ or `$facts['operatingsystem']`. You should use the new structured facts like\n
85
+ \ `$facts['os']['name']` instead\n"
86
+ email: mmckinst@umich.edu
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - LICENSE
92
+ - README.md
93
+ - lib/puppet-lint/plugins/legacy_facts.rb
94
+ - spec/puppet-lint/plugins/legacy_facts_spec.rb
95
+ - spec/spec_helper.rb
96
+ homepage: https://github.com/mmckinst/puppet-lint-legacy_facts-check
97
+ licenses:
98
+ - Apache-2.0
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.4.5
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: A puppet-lint plugin to check you are not using legacy facts like $::operatingsystem
120
+ test_files:
121
+ - spec/puppet-lint/plugins/legacy_facts_spec.rb
122
+ - spec/spec_helper.rb