├── .delivery └── project.toml ├── .editorconfig ├── .gitattributes ├── .github ├── CODEOWNERS └── workflows │ ├── branchcleanup.yml │ └── delivery.yml ├── .gitignore ├── .travis.yml ├── .vscode └── extensions.json ├── Berksfile ├── CHANGELOG.md ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── Gemfile ├── LICENSE ├── README.md ├── TESTING.md ├── appveyor.yml ├── chefignore ├── kitchen.appveyor.yml ├── kitchen.dokken.yml ├── kitchen.yml ├── libraries ├── hint_helpers.rb └── plugin_helpers.rb ├── metadata.rb ├── recipes └── default.rb ├── resources ├── hint.rb └── plugin.rb ├── spec ├── spec_helper.rb └── unit │ ├── libraries │ ├── hint_helper_spec.rb │ └── plugin_helper_spec.rb │ └── recipes │ └── default_spec.rb └── test ├── cookbooks └── ohai_test │ ├── files │ ├── another_test_source_file.rb │ ├── test_status.rb │ ├── tester.rb │ └── tester1.rb │ ├── metadata.rb │ ├── recipes │ └── default.rb │ └── templates │ └── default │ └── template_test.rb └── integration └── default └── default_spec.rb /.delivery/project.toml: -------------------------------------------------------------------------------- 1 | remote_file = "https://raw.githubusercontent.com/chef-cookbooks/community_cookbook_tools/master/delivery/project.toml" 2 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # https://EditorConfig.org 2 | 3 | # top-most EditorConfig file 4 | root=true 5 | 6 | # Unix-style newlines with a newline ending every file 7 | [*] 8 | end_of_line = lf 9 | insert_final_newline = true 10 | 11 | # 2 space indentation 12 | indent_style = space 13 | indent_size = 2 14 | 15 | # Avoid issues parsing cookbook files later 16 | charset = utf-8 17 | 18 | # Avoid cookstyle warnings 19 | trim_trailing_whitespace = true 20 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @chef-cookbooks/cookbook_engineering_team 2 | -------------------------------------------------------------------------------- /.github/workflows/branchcleanup.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Branch Cleanup 3 | # This workflow is triggered on all closed pull requests. 4 | # However the script does not do anything if a merge was not performed. 5 | on: 6 | pull_request: 7 | types: [closed] 8 | 9 | env: 10 | NO_BRANCH_DELETED_EXIT_CODE: 0 11 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 12 | 13 | jobs: 14 | build: 15 | runs-on: ubuntu-latest 16 | steps: 17 | - uses: jessfraz/branch-cleanup-action@master 18 | -------------------------------------------------------------------------------- /.github/workflows/delivery.yml: -------------------------------------------------------------------------------- 1 | name: delivery 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | delivery: 7 | 8 | runs-on: ubuntu-latest 9 | 10 | steps: 11 | - name: Check out code 12 | uses: actions/checkout@master 13 | - name: Run Chef Delivery 14 | uses: actionshub/chef-delivery@master 15 | env: 16 | CHEF_LICENSE: accept-no-persist -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.rbc 2 | .config 3 | InstalledFiles 4 | lib/bundler/man 5 | pkg 6 | test/tmp 7 | test/version_tmp 8 | tmp 9 | _Store 10 | *~ 11 | *# 12 | .#* 13 | \#*# 14 | *.un~ 15 | *.tmp 16 | *.bk 17 | *.bkup 18 | 19 | # editor temp files 20 | .idea 21 | .*.sw[a-z] 22 | 23 | # ruby/bundler files 24 | .ruby-version 25 | .ruby-gemset 26 | .rvmrc 27 | Gemfile.lock 28 | .bundle 29 | *.gem 30 | coverage 31 | spec/reports 32 | 33 | # YARD / rdoc artifacts 34 | .yardoc 35 | _yardoc 36 | doc/ 37 | rdoc 38 | 39 | # chef infra stuff 40 | Berksfile.lock 41 | .kitchen 42 | kitchen.local.yml 43 | vendor/ 44 | .coverage/ 45 | .zero-knife.rb 46 | Policyfile.lock.json 47 | 48 | # vagrant stuff 49 | .vagrant/ 50 | .vagrant.d/ 51 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | 2 | addons: 3 | apt: 4 | sources: 5 | - chef-current-xenial 6 | packages: 7 | - chef-workstation 8 | 9 | install: echo "skip bundle install" 10 | 11 | env: 12 | - CHEF_LICENSE=accept 13 | 14 | branches: 15 | only: 16 | - master 17 | 18 | services: docker 19 | 20 | env: 21 | matrix: 22 | - INSTANCE=default-amazonlinux 23 | - INSTANCE=default-centos-6 24 | - INSTANCE=default-centos-7 25 | - INSTANCE=default-debian-9 26 | - INSTANCE=default-debian-10 27 | - INSTANCE=default-fedora-latest 28 | - INSTANCE=default-opensuse-leap 29 | - INSTANCE=default-ubuntu-1604 30 | - INSTANCE=default-ubuntu-1804 31 | - INSTANCE=default-ubuntu-1604 CHEF_VERSION=12.7.2 32 | 33 | before_script: 34 | - sudo iptables -L DOCKER || ( echo "DOCKER iptables chain missing" ; sudo iptables -N DOCKER ) 35 | - eval "$(chef shell-init bash)" 36 | - chef --version 37 | 38 | script: KITCHEN_LOCAL_YAML=kitchen.dokken.yml CHEF_VERSION=${CHEF_VERSION} kitchen verify ${INSTANCE} 39 | 40 | matrix: 41 | include: 42 | - script: 43 | - chef exec delivery local all 44 | env: 45 | - UNIT_AND_LINT=1 46 | - CHEF_LICENSE=accept 47 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": [ 3 | "chef-software.chef", 4 | "rebornix.ruby", 5 | "editorconfig.editorconfig" 6 | ] 7 | } -------------------------------------------------------------------------------- /Berksfile: -------------------------------------------------------------------------------- 1 | source 'https://supermarket.chef.io' 2 | 3 | metadata 4 | 5 | group :integration do 6 | cookbook 'ohai_test', path: './test/cookbooks/ohai_test' 7 | end 8 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # ohai Cookbook CHANGELOG 2 | 3 | This file is used to list changes made in each version of the ohai cookbook. 4 | 5 | ## 5.3.1 (2022-01-14) 6 | 7 | - Resolve bad notification syntax in the ohai_hint :delete action 8 | - resolved cookstyle error: libraries/plugin_helpers.rb:39:9 refactor: `ChefCorrectness/ChefApplicationFatal` 9 | 10 | ## 5.3.0 (2019-08-29) 11 | 12 | - Add code owners file - [@tas50](https://github.com/tas50) 13 | - Cookstyle fixes - [@tas50](https://github.com/tas50) 14 | - Require Chef 13 or later - [@tas50](https://github.com/tas50) 15 | - Remove the long_description metadata - [@tas50](https://github.com/tas50) 16 | - Add the load_single_plugin option to the ohai_plugin resource - [@MarkGibbons](https://github.com/MarkGibbons) 17 | - Update for Chef 15 license agreement and Chef Workstation - [@tas50](https://github.com/tas50) 18 | - Update the platforms we test on in Travis / Test Kitchen - [@tas50](https://github.com/tas50) 19 | 20 | ## 5.2.5 (2018-09-04) 21 | 22 | - Add note that ohai_hint will be removed April 2019 when Chef 13 goes EOL as this resource now ships in Chef 14+ 23 | 24 | ## 5.2.4 (2018-08-28) 25 | 26 | - Avoid deprecation warnings in Chef 14.3+ by not loading resources already in Chef 27 | 28 | ## 5.2.3 (2018-06-08) 29 | 30 | - Make sure we properly compare a provided plugin path to the path on disk by stripping trailing slashes from the provided directory 31 | - Don't reload ohai when the plugin exists in a subdirectory of the config's set plugin path 32 | 33 | ## 5.2.2 (2018-02-15) 34 | 35 | - Remove ChefSpec matchers we no longer need since they're auto generated 36 | 37 | ## 5.2.1 (2018-01-25) 38 | 39 | - Switch from a .foodcritic file to an inline comments which resolve Supermarket warnings 40 | - Remove unused helper method 41 | 42 | ## 5.2.0 (2017-08-17) 43 | 44 | - Resolve multiple issues with Windows paths that caused the cookbook to converge on every run or fail 45 | - Move maintainer information to the readme 46 | - Add testing on Chef 12.7 in Travis 47 | - Move helpers to their own modules and add testing framework 48 | 49 | ## 5.1.0 (2017-05-06) 50 | 51 | - Workaround action_class bug by requiring Chef 12.7+ 52 | 53 | ## 5.0.4 (2017-04-25) 54 | 55 | - Fix lack of .rb extension when deleting plugins. 56 | 57 | ## 5.0.3 (2017-04-06) 58 | 59 | - Use class_eval again in the custom resource to provide Chef 12.5/12.6 compatibility 60 | - Remove kind_of and use name_property not name_attribute 61 | - Fix failures on Chef 13 62 | 63 | ## 5.0.2 (2017-03-24) 64 | 65 | - Remove class_eval 66 | 67 | ## 5.0.1 (2017-03-14) 68 | 69 | - Test with Delivery Local Mode 70 | - Bump the dependency to 12.7+ due to failures on 12.5-12.6 71 | 72 | ## 5.0.0 (2017-02-23) 73 | 74 | - Require Chef 12.5+ and remove compat_resource dependency 75 | 76 | ## 4.2.3 (2016-12-02) 77 | - Prevent chef_version metadata from failing runs in Opsworks 78 | - Better explain how to resolve the plugin_path issue 79 | - Add suse as a supported platform 80 | - Require at least compat_resource 12.14.7 81 | 82 | ## 4.2.2 (2016-09-19) 83 | - Ignore case in plugin path check on Windows 84 | 85 | ## 4.2.1 (2016-09-08) 86 | - Fix typo in compile warning text 87 | - Depend on the latest compat_resource (12.14) 88 | - Remove Chef 11 compat in the metadata 89 | - Require Chef 12.1 not 12.0 90 | - Define ohai_plugin matcher for Chefspec 91 | 92 | ## v4.2.0 (2016-07-19) 93 | 94 | - Added the ability to specify the source cookbook for the cookbook_file or template used in the ohai_plugin resource. 95 | - Added chef_version to the metadata 96 | - Added testing on openSUSE and switched from Rubocop to Cookstyle 97 | 98 | ## v4.1.1 (2016-06-16) 99 | 100 | - Fixed error in notifies reload for the delete action 101 | - Bump the compat_resource requirement from 12.9+ to 12.10+ to prevent random failures 102 | 103 | ## v4.1.0 (2016-05-26) 104 | 105 | - Added the ability to use templates and pass in variables with the plugin custom resource 106 | 107 | ## v4.0.2 (2016-05-23) 108 | 109 | - Resolve failures on Windows nodes 110 | 111 | ## v4.0.1 (2016-05-19) 112 | 113 | - Added .rb to the name of the plugins so they actually load 114 | - Added testing to ensure the plugins are being loaded in the chef run 115 | 116 | ## v4.0.0 (2016-05-18) 117 | 118 | ### BREAKING CHANGE: 119 | 120 | The 4.0 release of the Ohai cookbook removes the previous cookbook_file behavior that required forking the cookbook and adding your own plugins. Instead the cookbook ships with a new ohai_plugin custom resource for installing plugins. In addition to this new custom resource the cookbook now requires Chef 12+. See the readme and test recipe for examples. If you require Chef 11 support you'll need to pin to version 3.0 in your environment. 121 | 122 | ## v3.0.1 (2016-03-14) 123 | 124 | - Fixed the Chefspec matchers 125 | 126 | ## v3.0.0 (2016-03-14) 127 | 128 | - Change the default value for `node['ohai']['hints_path']` to use the Ohai config value. This should be the same value in most use cases, but if a custom path is specified in the chef client config this value will get used automatically by the cookbook. 129 | - Removed backwards compatibility with Chefspec < 4.1 in the matchers library 130 | - Fix bad link to the custom Ohai plugin documentation in the readme 131 | - Improve documentation for `node['ohai']['plugin_path']` 132 | 133 | ## v2.1.0 (2016-01-26) 134 | 135 | - Properly handle creating ohai hints without specifying the content. Previously if the content wasn't specified a deprecation notice would be thrown and the file would not be created 136 | - Simplified the test suite and added inspec tests to ensure hints are created, especially if the content is not specified 137 | - Added FreeBSD and Windows as supported platform in the metadata and add them to the Test Kitchen config 138 | - Add Test Kitchen integration tests to Travis CI 139 | - Updated testing Gems to the latest releases in the Gemfile 140 | 141 | ## v2.0.4 (2015-10-30) 142 | 143 | - Resolved deprecation warnings with the Chefspec matchers 144 | 145 | ## v2.0.3 (2015-10-21) 146 | 147 | - Validate the hints before loading them to avoid failures 148 | - Added supported platforms to the metadata 149 | - Updated .gitignore file 150 | - Updated Test Kitchen config for the latest platforms 151 | - Added Chef standard Rubocop config 152 | - Added Travis CI testing 153 | - Added Berksfile 154 | - Updated contributing and testing docs 155 | - Added maintainers.md and maintainers.toml files 156 | - Added Travis and cookbook version badges to the readme 157 | - Expanded the requirements section in the readme and clarify the minimum supported Chef release is 11 158 | - Updated Opscode -> Chef Software 159 | - Added a Rakefile for simplified testing 160 | - Added a Chefignore file 161 | - Resolved Rubocop warnings 162 | - Added source_url and issues_url to the metadata 163 | - Added Chefspec matchers 164 | - Added basic convergence Chefspec test 165 | 166 | ## v2.0.1 (2014-06-07) 167 | 168 | - [COOK-4683] Remove warnings about reopening resource 169 | 170 | Please note, this changes the name of a remote_directory resource. It is not expected that anyone would be explicitly notifying this resource but, please review [PR #16](https://github.com/chef-cookbooks/ohai/pull/16/files) for more info. 171 | 172 | ## v2.0.0 (2014-02-25) 173 | 174 | '[COOK-3865] - create lwrp ohai_hint' 175 | 176 | ## v1.1.12 177 | 178 | - Dummy release due to a Community Site upload failure 179 | 180 | ## v1.1.10 181 | 182 | ### Bug 183 | 184 | - **[COOK-3091](https://tickets.chef.io/browse/COOK-3091)** - Fix checking `Chef::Config[:config_file]` 185 | 186 | ## v1.1.8 187 | 188 | - [COOK-1918] - Ohai cookbook to distribute plugins fails on windows 189 | - [COOK-2096] - Ohai cookbook sets unix-only default path attribute 190 | 191 | ## v1.1.6 192 | 193 | - [COOK-2057] - distribution from another cookbok fails if ohai attributes are loaded after the other cookbook 194 | 195 | ## v1.1.4 196 | 197 | - [COOK-1128] - readme update, Replace reference to deprecated chef cookbook with one to chef-client 198 | 199 | ## v1.1.2 200 | 201 | - [COOK-1424] - prevent plugin_path growth to infinity 202 | 203 | ## v1.1.0 204 | 205 | - [COOK-1174] - custom_plugins is only conditionally available 206 | - [COOK-1383] - allow plugins from other cookbooks 207 | 208 | ## v1.0.2 209 | 210 | - [COOK-463] ohai cookbook default recipe should only reload plugins if there were updates 211 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | Please refer to the Chef Community Code of Conduct at https://www.chef.io/code-of-conduct/ 2 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Please refer to 2 | https://github.com/chef-cookbooks/community_cookbook_documentation/blob/master/CONTRIBUTING.MD 3 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | # This gemfile provides additional gems for testing and releasing this cookbook 2 | # It is meant to be installed on top of ChefDK / Chef Workstation which provide the majority 3 | # of the necessary gems for testing this cookbook 4 | # 5 | # Run 'chef exec bundle install' to install these dependencies 6 | 7 | source 'https://rubygems.org' 8 | 9 | gem 'community_cookbook_releaser' 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 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. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ohai Cookbook 2 | 3 | [![Build Status](https://travis-ci.org/chef-cookbooks/ohai.svg?branch=master)](https://travis-ci.org/chef-cookbooks/ohai) [![Build status](https://ci.appveyor.com/api/projects/status/lgok2kr6l007s8hf/branch/master?svg=true)](https://ci.appveyor.com/project/ChefWindowsCookbooks/ohai/branch/master) [![Cookbook Version](https://img.shields.io/cookbook/v/ohai.svg)](https://supermarket.chef.io/cookbooks/ohai) 4 | 5 | Contains custom resources for adding Ohai hints and installing custom Ohai plugins. Handles path creation as well as the reloading of Ohai so that new data will be available during the same run. 6 | 7 | # Deprecation 8 | 9 | This cookbook has been deprecated as all functionality here is now built into the Chef Infra Client itself. The `ohai_hint` resource now ships in Chef Infra Client and any plugins can be installed by placing them in an `ohai` directory within a cookbook in your runlist. 10 | 11 | ## Requirements 12 | 13 | ### Platforms 14 | 15 | - Debian/Ubuntu 16 | - RHEL/CentOS/Scientific/Amazon/Oracle 17 | - openSUSE / SUSE Enterprise Linux 18 | - FreeBSD 19 | - Windows 20 | 21 | ### Chef 22 | 23 | - Chef 13+ 24 | 25 | ### Cookbooks 26 | 27 | - none 28 | 29 | ## Custom Resources 30 | 31 | ### `ohai_hint` 32 | 33 | Creates Ohai hint files, which are consumed by Ohai plugins in order to determine if they should run or not. 34 | 35 | #### Resource Properties 36 | 37 | - `hint_name` - The name of hints file and key. Should be string, default is name of resource. 38 | - `content` - Values of hints. It will be used as automatic attributes. Should be Hash, default is empty Hash 39 | - `compile_time` - Should the resource run at compile time. This defaults to true 40 | 41 | #### Examples 42 | 43 | Hint file installed to the default directory: 44 | 45 | ```ruby 46 | ohai_hint 'ec2' 47 | ``` 48 | 49 | Hint file not installed at compile time: 50 | 51 | ```ruby 52 | ohai_hint 'ec2' do 53 | compile_time false 54 | end 55 | ``` 56 | 57 | Hint file installed with content: 58 | 59 | ```ruby 60 | ohai_hint 'raid_present' do 61 | content Hash[:a, 'test_content'] 62 | end 63 | ``` 64 | 65 | #### ChefSpec Matchers 66 | 67 | You can check for the creation or deletion of ohai hints with chefspec using these custom matches: 68 | 69 | - create_ohai_hint 70 | - delete_ohai_hint 71 | 72 | ### `ohai_plugin` 73 | 74 | Installs custom Ohai plugins. 75 | 76 | #### Resource Properties 77 | 78 | - `plugin_name` - The name to give the plugin on the filesystem. Should be string, default is name of resource. 79 | - `path` - The path to your custom plugin directory. Defaults to a directory named 'plugins' under the directory 'ohai' in the Chef config dir. 80 | - `source_file` - The source file for the plugin in your cookbook if not NAME.rb. 81 | - `cookbook` - The cookbook where the source file exists if not the cookbook where the ohai_plugin resource is running from. 82 | - `resource` - The resource type for the plugin file. Either `:cookbook_file` or `:template`. Defaults to `:cookbook_file`. 83 | - `variables` - Usable only if `resource` is `:template`. Defines the template's variables. 84 | - `compile_time` - Should the resource run at compile time. This defaults to `true`. 85 | - `load_single_plugin` - Reload all plugins unless this value is set to true. Load only the named plugin. 86 | 87 | #### examples 88 | 89 | Simple Ohai plugin installation: 90 | 91 | ```ruby 92 | ohai_plugin 'my_custom_plugin' 93 | ``` 94 | 95 | Installation where the resource doesn't match the filename and you install to a custom plugins dir: 96 | 97 | ```ruby 98 | ohai_plugin 'My Ohai Plugin' do 99 | name 'my_custom_plugin' 100 | path '/my/custom/path/' 101 | end 102 | ``` 103 | 104 | Installation using a template: 105 | 106 | ```ruby 107 | ohai_plugin 'My Templated Plugin' do 108 | name 'templated_plugin' 109 | resource :template 110 | variables node_type: :web_server 111 | end 112 | ``` 113 | 114 | #### ChefSpec Matchers 115 | 116 | You can check for the creation or deletion of ohai plugins with chefspec using these custom matches: 117 | 118 | - create_ohai_plugin 119 | - delete_ohai_plugin 120 | 121 | ## Maintainers 122 | 123 | This cookbook is maintained by Chef's Community Cookbook Engineering team. Our goal is to improve cookbook quality and to aid the community in contributing to cookbooks. To learn more about our team, process, and design goals see our [team documentation](https://github.com/chef-cookbooks/community_cookbook_documentation/blob/master/COOKBOOK_TEAM.MD). To learn more about contributing to cookbooks like this see our [contributing documentation](https://github.com/chef-cookbooks/community_cookbook_documentation/blob/master/CONTRIBUTING.MD), or if you have general questions about this cookbook come chat with us in #cookbok-engineering on the [Chef Community Slack](http://community-slack.chef.io/) 124 | 125 | ## License 126 | 127 | **Copyright:** 2011-2016, Chef Software, Inc. 128 | 129 | ``` 130 | Licensed under the Apache License, Version 2.0 (the "License"); 131 | you may not use this file except in compliance with the License. 132 | You may obtain a copy of the License at 133 | 134 | http://www.apache.org/licenses/LICENSE-2.0 135 | 136 | Unless required by applicable law or agreed to in writing, software 137 | distributed under the License is distributed on an "AS IS" BASIS, 138 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 139 | See the License for the specific language governing permissions and 140 | limitations under the License. 141 | ``` 142 | -------------------------------------------------------------------------------- /TESTING.md: -------------------------------------------------------------------------------- 1 | Please refer to 2 | https://github.com/chef-cookbooks/community_cookbook_documentation/blob/master/TESTING.MD 3 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | environment: 2 | machine_user: vagrant 3 | machine_pass: vagrant 4 | KITCHEN_YAML: kitchen.appveyor.yml 5 | 6 | branches: 7 | only: 8 | - master 9 | 10 | # Do not build on tags (GitHub only) 11 | skip_tags: true 12 | 13 | #faster cloning 14 | clone_depth: 1 15 | 16 | # Install the latest current build of Chef Workstation 17 | install: 18 | - ps: (& cmd /c); iex (irm https://omnitruck.chef.io/install.ps1); Install-Project -Project chef-workstation -channel current 19 | - ps: 'Get-CimInstance win32_operatingsystem -Property Caption, OSArchitecture, Version | fl Caption, OSArchitecture, Version' 20 | - ps: $PSVersionTable 21 | - c:\opscode\chef-workstation\bin\chef.bat exec ruby --version 22 | - ps: secedit /export /cfg $env:temp/export.cfg 23 | - ps: ((get-content $env:temp/export.cfg) -replace ('PasswordComplexity = 1', 'PasswordComplexity = 0')) | Out-File $env:temp/export.cfg 24 | - ps: ((get-content $env:temp/export.cfg) -replace ('MinimumPasswordLength = 8', 'MinimumPasswordLength = 0')) | Out-File $env:temp/export.cfg 25 | - ps: secedit /configure /db $env:windir/security/new.sdb /cfg $env:temp/export.cfg /areas SECURITYPOLICY 26 | - ps: net user /add $env:machine_user $env:machine_pass 27 | - ps: net localgroup administrators $env:machine_user /add 28 | 29 | build_script: 30 | - ps: c:\opscode\chef-workstation\bin\chef.bat shell-init powershell | iex; cmd /c c:\opscode\chef-workstation\bin\chef.bat --version 31 | 32 | test_script: 33 | - c:\opscode\chef-workstation\bin\cookstyle --version 34 | - c:\opscode\chef-workstation\bin\chef.bat exec foodcritic --version 35 | - c:\opscode\chef-workstation\bin\chef.bat exec delivery local all 36 | - c:\opscode\chef-workstation\bin\chef.bat exec kitchen verify 37 | 38 | deploy: off 39 | -------------------------------------------------------------------------------- /chefignore: -------------------------------------------------------------------------------- 1 | # Put files/directories that should be ignored in this file when uploading 2 | # to a Chef Infra Server or Supermarket. 3 | # Lines that start with '# ' are comments. 4 | 5 | # OS generated files # 6 | ###################### 7 | .DS_Store 8 | ehthumbs.db 9 | Icon? 10 | nohup.out 11 | Thumbs.db 12 | 13 | # SASS # 14 | ######## 15 | .sass-cache 16 | 17 | # EDITORS # 18 | ########### 19 | .#* 20 | .project 21 | .settings 22 | *_flymake 23 | *_flymake.* 24 | *.bak 25 | *.sw[a-z] 26 | *.tmproj 27 | *~ 28 | \#* 29 | mkmf.log 30 | REVISION 31 | TAGS* 32 | tmtags 33 | 34 | ## COMPILED ## 35 | ############## 36 | *.class 37 | *.com 38 | *.dll 39 | *.exe 40 | *.o 41 | *.pyc 42 | *.so 43 | */rdoc/ 44 | a.out 45 | 46 | # Testing # 47 | ########### 48 | .circleci/* 49 | .codeclimate.yml 50 | .foodcritic 51 | .kitchen* 52 | .rspec 53 | .rubocop.yml 54 | .travis.yml 55 | .watchr 56 | azure-pipelines.yml 57 | examples/* 58 | features/* 59 | Guardfile 60 | kitchen.yml* 61 | Procfile 62 | Rakefile 63 | spec/* 64 | test/* 65 | 66 | # SCM # 67 | ####### 68 | .git 69 | .gitattributes 70 | .gitconfig 71 | .github/* 72 | .gitignore 73 | .gitmodules 74 | .svn 75 | */.bzr/* 76 | */.git 77 | */.hg/* 78 | */.svn/* 79 | 80 | # Berkshelf # 81 | ############# 82 | Berksfile 83 | Berksfile.lock 84 | cookbooks/* 85 | tmp 86 | 87 | # Bundler # 88 | ########### 89 | vendor/* 90 | Gemfile 91 | Gemfile.lock 92 | 93 | # Policyfile # 94 | ############## 95 | Policyfile.rb 96 | Policyfile.lock.json 97 | 98 | # Cookbooks # 99 | ############# 100 | CHANGELOG* 101 | CONTRIBUTING* 102 | TESTING* 103 | CODE_OF_CONDUCT* 104 | 105 | # Vagrant # 106 | ########### 107 | .vagrant 108 | Vagrantfile 109 | -------------------------------------------------------------------------------- /kitchen.appveyor.yml: -------------------------------------------------------------------------------- 1 | --- 2 | driver: 3 | name: proxy 4 | host: localhost 5 | reset_command: "exit 0" 6 | port: 5985 7 | username: <%= ENV["machine_user"] %> 8 | password: <%= ENV["machine_pass"] %> 9 | 10 | verifier: 11 | name: inspec 12 | 13 | provisioner: 14 | name: chef_zero 15 | deprecations_as_errors: true 16 | chef_license: accept-no-persist 17 | 18 | platforms: 19 | - name: windows-2012R2 20 | 21 | suites: 22 | - name: default 23 | run_list: 24 | - "recipe[ohai_test::default]" 25 | -------------------------------------------------------------------------------- /kitchen.dokken.yml: -------------------------------------------------------------------------------- 1 | driver: 2 | name: dokken 3 | privileged: true # because Docker and SystemD/Upstart 4 | chef_version: 'current' 5 | 6 | transport: 7 | name: dokken 8 | 9 | provisioner: 10 | name: dokken 11 | deprecations_as_errors: true 12 | 13 | verifier: 14 | name: inspec 15 | 16 | platforms: 17 | - name: amazonlinux 18 | driver: 19 | image: dokken/amazonlinux 20 | pid_one_command: /sbin/init 21 | 22 | - name: amazonlinux-2 23 | driver: 24 | image: dokken/amazonlinux-2 25 | pid_one_command: /usr/lib/systemd/systemd 26 | 27 | - name: debian-9 28 | driver: 29 | image: dokken/debian-9 30 | pid_one_command: /bin/systemd 31 | intermediate_instructions: 32 | - RUN /usr/bin/apt-get update 33 | 34 | - name: debian-10 35 | driver: 36 | image: dokken/debian-10 37 | pid_one_command: /bin/systemd 38 | intermediate_instructions: 39 | - RUN /usr/bin/apt-get update 40 | 41 | - name: centos-6 42 | driver: 43 | image: dokken/centos-6 44 | pid_one_command: /sbin/init 45 | 46 | - name: centos-7 47 | driver: 48 | image: dokken/centos-7 49 | pid_one_command: /usr/lib/systemd/systemd 50 | 51 | - name: fedora-latest 52 | driver: 53 | image: dokken/fedora-latest 54 | pid_one_command: /usr/lib/systemd/systemd 55 | 56 | - name: ubuntu-16.04 57 | driver: 58 | image: dokken/ubuntu-16.04 59 | pid_one_command: /bin/systemd 60 | intermediate_instructions: 61 | - RUN /usr/bin/apt-get update 62 | 63 | - name: ubuntu-18.04 64 | driver: 65 | image: dokken/ubuntu-18.04 66 | pid_one_command: /bin/systemd 67 | intermediate_instructions: 68 | - RUN /usr/bin/apt-get update 69 | 70 | - name: opensuse-leap 71 | driver: 72 | image: dokken/opensuse-leap-15 73 | pid_one_command: /bin/systemd 74 | -------------------------------------------------------------------------------- /kitchen.yml: -------------------------------------------------------------------------------- 1 | driver: 2 | name: vagrant 3 | 4 | provisioner: 5 | name: chef_zero 6 | deprecations_as_errors: true 7 | chef_license: accept-no-persist 8 | 9 | verifier: 10 | name: inspec 11 | 12 | platforms: 13 | - name: amazonlinux 14 | driver_config: 15 | box: mvbcoding/awslinux 16 | - name: centos-6 17 | - name: centos-7 18 | - name: centos-8 19 | - name: debian-9 20 | - name: debian-10 21 | - name: freebsd-11 22 | - name: fedora-latest 23 | - name: opensuse-leap-15 24 | - name: ubuntu-16.04 25 | - name: ubuntu-18.04 26 | - name: windows-2012r2 27 | driver_config: 28 | box: tas50/windows_2012r2 29 | 30 | suites: 31 | - name: default 32 | run_list: 33 | - recipe[ohai_test::default] 34 | -------------------------------------------------------------------------------- /libraries/hint_helpers.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook:: ohai 3 | # Library:: hint_helpers 4 | # 5 | # Author:: Tim Smith () 6 | # 7 | # Copyright:: 2017, Chef Software, Inc. 8 | # 9 | # Licensed under the Apache License, Version 2.0 (the "License"); 10 | # you may not use this file except in compliance with the License. 11 | # You may obtain a copy of the License at 12 | # 13 | # http://www.apache.org/licenses/LICENSE-2.0 14 | # 15 | # Unless required by applicable law or agreed to in writing, software 16 | # distributed under the License is distributed on an "AS IS" BASIS, 17 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | # See the License for the specific language governing permissions and 19 | # limitations under the License. 20 | # 21 | 22 | module OhaiCookbook 23 | module HintHelpers 24 | def ohai_hint_file_path(filename) 25 | path = ::File.join(::Ohai::Config.ohai.hints_path.first, filename) 26 | path << '.json' unless path.end_with?('.json') 27 | path 28 | end 29 | 30 | def format_content(content) 31 | return '' if content.nil? || content.empty? 32 | JSON.pretty_generate(content) 33 | end 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /libraries/plugin_helpers.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook:: ohai 3 | # Library:: plugin_helpers 4 | # 5 | # Author:: Tim Smith () 6 | # 7 | # Copyright:: 2017-2018, Chef Software, Inc. 8 | # 9 | # Licensed under the Apache License, Version 2.0 (the "License"); 10 | # you may not use this file except in compliance with the License. 11 | # You may obtain a copy of the License at 12 | # 13 | # http://www.apache.org/licenses/LICENSE-2.0 14 | # 15 | # Unless required by applicable law or agreed to in writing, software 16 | # distributed under the License is distributed on an "AS IS" BASIS, 17 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | # See the License for the specific language governing permissions and 19 | # limitations under the License. 20 | # 21 | 22 | module OhaiCookbook 23 | module PluginHelpers 24 | # return the path property if specified or 25 | # CHEF_CONFIG_PATH/ohai/plugins if a path isn't specified 26 | def desired_plugin_path 27 | if new_resource.path 28 | new_resource.path.chomp('/') # if the user gave us /foo/bar/ we need /foo/bar for later comparison 29 | else 30 | ::File.join(chef_config_path, 'ohai', 'plugins') 31 | end 32 | end 33 | 34 | # return the chef config files dir or fail hard 35 | def chef_config_path 36 | if Chef::Config['config_file'] 37 | ::File.dirname(Chef::Config['config_file']) 38 | else 39 | raise("No chef config file defined. Are you running \ 40 | chef-solo? If so you will need to define a path for the ohai_plugin as the \ 41 | path cannot be determined") 42 | end 43 | end 44 | 45 | # is the desired plugin dir in the ohai config plugin dir array? 46 | def in_plugin_path?(path) 47 | normalized_path = normalize_path(path) 48 | # get the directory where we plan to stick the plugin (not the actual file path) 49 | desired_dir = ::File.directory?(normalized_path) ? normalized_path : ::File.dirname(normalized_path) 50 | ::Ohai::Config.ohai['plugin_path'].map { |x| normalize_path(x) }.any? do |d| 51 | desired_dir.start_with?(d) 52 | end 53 | end 54 | 55 | # return path to lower and with forward slashes so we can compare it 56 | # this works around the 3 different way we can represent windows paths 57 | def normalize_path(path) 58 | path.downcase.gsub(/\\+/, '/') 59 | end 60 | 61 | def add_to_plugin_path(path) 62 | ::Ohai::Config.ohai['plugin_path'] << path # new format 63 | end 64 | 65 | # we need to warn the user that unless the path for this plugin is in Ohai's 66 | # plugin path already we're going to have to reload Ohai on every Chef Infra Client run. 67 | # Ideally in future versions of Ohai /etc/chef/ohai/plugins is in the path. 68 | def plugin_path_warning 69 | Chef::Log.warn("The Ohai plugin_path does not include #{desired_plugin_path}. \ 70 | Ohai will reload on each chef-client run in order to add this directory to the \ 71 | path unless you modify your client.rb configuration to add this directory to \ 72 | plugin_path. The plugin_path can be set via the chef-client::config recipe. \ 73 | See 'Ohai Settings' at https://docs.chef.io/config_rb_client.html#ohai-settings \ 74 | for more details.") 75 | end 76 | end 77 | end 78 | -------------------------------------------------------------------------------- /metadata.rb: -------------------------------------------------------------------------------- 1 | name 'ohai' 2 | maintainer 'Chef Software, Inc.' 3 | maintainer_email 'cookbooks@chef.io' 4 | license 'Apache-2.0' 5 | description 'Provides custom resources for installing Ohai hints and plugins' 6 | 7 | version '5.3.1' 8 | 9 | %w(ubuntu debian centos redhat amazon scientific fedora oracle suse opensuse opensuseleap freebsd windows zlinux).each do |os| 10 | supports os 11 | end 12 | 13 | source_url 'https://github.com/chef-cookbooks/ohai' 14 | issues_url 'https://github.com/chef-cookbooks/ohai/issues' 15 | chef_version '>= 13' 16 | -------------------------------------------------------------------------------- /recipes/default.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Cookbook:: ohai 3 | # Recipe:: default 4 | # 5 | # Copyright:: 2011-2017, Chef Software, Inc 6 | # 7 | # Licensed under the Apache License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | # 19 | 20 | Chef::Log.warn('The Ohai cookbook default recipe has no content as of the 4.0 release. See the readme for instructions on using the custom resources.') 21 | -------------------------------------------------------------------------------- /resources/hint.rb: -------------------------------------------------------------------------------- 1 | resource_name :ohai_hint 2 | chef_version_for_provides '< 14.0' if respond_to?(:chef_version_for_provides) 3 | 4 | property :hint_name, String, name_property: true 5 | property :content, Hash 6 | property :compile_time, [true, false], default: true 7 | 8 | action :create do 9 | directory ::Ohai::Config.ohai.hints_path.first do 10 | action :create 11 | recursive true 12 | end 13 | 14 | file ohai_hint_file_path(new_resource.hint_name) do 15 | action :create 16 | content format_content(new_resource.content) 17 | end 18 | end 19 | 20 | action :delete do 21 | file ohai_hint_file_path(new_resource.hint_name) do 22 | action :delete 23 | notifies :reload, 'ohai[reload ohai post hint removal]', :immediately 24 | end 25 | 26 | ohai 'reload ohai post hint removal' do 27 | action :nothing 28 | end 29 | end 30 | 31 | action_class do 32 | include OhaiCookbook::HintHelpers 33 | end 34 | 35 | # this resource forces itself to run at compile_time 36 | def after_created 37 | return unless compile_time 38 | Array(action).each do |action| 39 | run_action(action) 40 | end 41 | end 42 | -------------------------------------------------------------------------------- /resources/plugin.rb: -------------------------------------------------------------------------------- 1 | property :plugin_name, String, name_property: true 2 | property :path, String 3 | property :source_file, String 4 | property :cookbook, String 5 | property :resource, [:cookbook_file, :template], default: :cookbook_file 6 | property :variables, Hash 7 | property :compile_time, [true, false], default: true 8 | property :load_single_plugin, [true, false], default: false 9 | 10 | action :create do 11 | # why create_if_missing you ask? 12 | # no one can agree on perms and this allows them to manage the perms elsewhere 13 | directory desired_plugin_path do 14 | action :create 15 | recursive true 16 | not_if { ::File.exist?(desired_plugin_path) } 17 | end 18 | 19 | if new_resource.resource.eql?(:cookbook_file) 20 | cookbook_file ::File.join(desired_plugin_path, new_resource.plugin_name + '.rb') do 21 | cookbook new_resource.cookbook 22 | source new_resource.source_file || "#{new_resource.plugin_name}.rb" 23 | notifies :reload, "ohai[#{new_resource.plugin_name}]", :immediately 24 | end 25 | elsif new_resource.resource.eql?(:template) 26 | template ::File.join(desired_plugin_path, new_resource.plugin_name + '.rb') do 27 | cookbook new_resource.cookbook 28 | source new_resource.source_file || "#{new_resource.plugin_name}.rb" 29 | variables new_resource.variables 30 | notifies :reload, "ohai[#{new_resource.plugin_name}]", :immediately 31 | end 32 | end 33 | 34 | # Add the plugin path to the ohai plugin path if need be and warn 35 | # the user that this is going to result in a reload every run 36 | unless in_plugin_path?(desired_plugin_path) 37 | plugin_path_warning 38 | Chef::Log.warn("Adding #{desired_plugin_path} to the Ohai plugin path for this chef-client run only") 39 | add_to_plugin_path(desired_plugin_path) 40 | reload_required = true 41 | end 42 | 43 | ohai new_resource.plugin_name do 44 | action :nothing 45 | action :reload if reload_required 46 | plugin new_resource.plugin_name if new_resource.load_single_plugin 47 | end 48 | end 49 | 50 | action :delete do 51 | file ::File.join(desired_plugin_path, new_resource.plugin_name + '.rb') do 52 | action :delete 53 | notifies :reload, 'ohai[reload ohai post plugin removal]' 54 | end 55 | 56 | ohai 'reload ohai post plugin removal' do 57 | action :nothing 58 | end 59 | end 60 | 61 | action_class do 62 | include OhaiCookbook::PluginHelpers 63 | end 64 | 65 | # this resource forces itself to run at compile_time 66 | def after_created 67 | return unless compile_time 68 | Array(action).each do |action| 69 | run_action(action) 70 | end 71 | end 72 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'chefspec' 2 | require 'chefspec/berkshelf' 3 | 4 | RSpec.configure do |config| 5 | config.color = true # Use color in STDOUT 6 | config.formatter = :documentation # Use the specified formatter 7 | config.log_level = :error # Avoid deprecation notice SPAM 8 | end 9 | -------------------------------------------------------------------------------- /spec/unit/libraries/hint_helper_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | require_relative '../../../libraries/hint_helpers' 4 | 5 | describe OhaiCookbook::HintHelpers do 6 | let(:helper) do 7 | Object.new.extend(OhaiCookbook::HintHelpers) 8 | end 9 | 10 | describe '#ohai_hint_file_path' do 11 | it 'returns a full path given a hint name' do 12 | ::Ohai::Config.ohai.hints_path = ['/foo/bar/ohai/hints/'] 13 | expect(helper.ohai_hint_file_path('mycloud')).to eq('/foo/bar/ohai/hints/mycloud.json') 14 | end 15 | 16 | it 'handles hint names that end in .json' do 17 | ::Ohai::Config.ohai.hints_path = ['/foo/bar/ohai/hints/'] 18 | expect(helper.ohai_hint_file_path('mycloud.json')).to eq('/foo/bar/ohai/hints/mycloud.json') 19 | end 20 | end 21 | 22 | describe '#format_content' do 23 | it 'returns JSON given a simple hash' do 24 | expect(helper.format_content(foo: 'bar')).to eq("{\n \"foo\": \"bar\"\n}") 25 | end 26 | 27 | it 'returns empty string if content is nil to avoid deprecation warnings' do 28 | expect(helper.format_content(nil)).to eq('') 29 | end 30 | 31 | it 'returns empty string if content is empty hash' do 32 | expect(helper.format_content({})).to eq('') 33 | end 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /spec/unit/libraries/plugin_helper_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | require_relative '../../../libraries/plugin_helpers' 4 | 5 | describe OhaiCookbook::PluginHelpers do 6 | let(:helper) do 7 | Object.new.extend(OhaiCookbook::PluginHelpers) 8 | end 9 | 10 | describe('#normalize_path') do 11 | it 'does nothing to a unix path' do 12 | expect(helper.normalize_path('/foo/bar/')).to eq('/foo/bar/') 13 | end 14 | 15 | it 'lowers the case of a forward slash windows path' do 16 | expect(helper.normalize_path('C:/foo/bar')).to eq('c:/foo/bar') 17 | end 18 | 19 | it 'converts single backslash to a single forward slash' do 20 | expect(helper.normalize_path('C:\foo\bar')).to eq('c:/foo/bar') 21 | end 22 | 23 | it 'converts double backslashes to a single forward slash' do 24 | expect(helper.normalize_path('C:\\foo\\bar')).to eq('c:/foo/bar') 25 | end 26 | 27 | it 'converts an insane numbers of backslashes to a single forward slash' do 28 | expect(helper.normalize_path('C:\\\\\\\foo\\\\\\\\bar')).to eq('c:/foo/bar') 29 | end 30 | end 31 | 32 | describe('#in_plugin_path?') do 33 | it 'returns false if the path is not in the config' do 34 | ::Ohai::Config.ohai['plugin_path'] = ['/foo/bar'] 35 | expect(helper.in_plugin_path?('/foo/baz/plugin.rb')).to eq(false) 36 | end 37 | 38 | it 'returns true if the path is in the config' do 39 | ::Ohai::Config.ohai['plugin_path'] = ['/foo/bar'] 40 | expect(helper.in_plugin_path?('/foo/bar/plugin.rb')).to eq(true) 41 | end 42 | 43 | it 'returns true if the path is a subdirectory of the path in the config' do 44 | ::Ohai::Config.ohai['plugin_path'] = ['/foo/bar'] 45 | expect(helper.in_plugin_path?('/foo/bar/baz/plugin.rb')).to eq(true) 46 | end 47 | 48 | it 'returns true if the path is in the config regardless of format' do 49 | ::Ohai::Config.ohai['plugin_path'] = ['C:/foo/bar'] 50 | expect(helper.in_plugin_path?('C:\\foo\\bar\\plugin.rb')).to eq(true) 51 | end 52 | end 53 | 54 | describe('#chef_config_path') do 55 | it 'returns a path given the chef-client config file' do 56 | Chef::Config['config_file'] = '/foo/bar/client.rb' 57 | expect(helper.chef_config_path).to eq('/foo/bar') 58 | end 59 | end 60 | end 61 | -------------------------------------------------------------------------------- /spec/unit/recipes/default_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe 'ohai_test::default' do 4 | let(:runner) { ChefSpec::ServerRunner.new(platform: 'ubuntu', version: '16.04') } 5 | let(:chef_run) { runner.converge('described_recipe') } 6 | 7 | it 'converges successfully' do 8 | expect { :chef_run }.to_not raise_error 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /test/cookbooks/ohai_test/files/another_test_source_file.rb: -------------------------------------------------------------------------------- 1 | Ohai.plugin(:Anothertest) do 2 | provides 'another_test' 3 | 4 | collect_data do 5 | another_test Mash.new 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /test/cookbooks/ohai_test/files/test_status.rb: -------------------------------------------------------------------------------- 1 | Ohai.plugin(:TestStatus) do 2 | provides 'test_status' 3 | 4 | collect_data do 5 | test_status Mash.new 6 | test_status['status'] = ::File.read('/status') 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /test/cookbooks/ohai_test/files/tester.rb: -------------------------------------------------------------------------------- 1 | Ohai.plugin(:Tester) do 2 | provides 'tester' 3 | 4 | collect_data do 5 | tester Mash.new 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /test/cookbooks/ohai_test/files/tester1.rb: -------------------------------------------------------------------------------- 1 | Ohai.plugin(:Tester1) do 2 | provides 'tester1' 3 | 4 | collect_data do 5 | tester1 Mash.new 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /test/cookbooks/ohai_test/metadata.rb: -------------------------------------------------------------------------------- 1 | name 'ohai_test' 2 | license 'Apache-2.0' 3 | description 'Tests the Ohai cookbook' 4 | version '0.1.0' 5 | depends 'ohai' 6 | -------------------------------------------------------------------------------- /test/cookbooks/ohai_test/recipes/default.rb: -------------------------------------------------------------------------------- 1 | ohai_hint 'hint_at_compile_time' 2 | 3 | ohai_hint 'not_at_compile_time' do 4 | compile_time false 5 | end 6 | 7 | ohai_hint 'hint_with_content' do 8 | content Hash[:a, 'test_content'] 9 | end 10 | 11 | ohai_hint 'hint_without_content' 12 | 13 | ohai_hint 'hint_with_json_in_resource_name.json' 14 | 15 | ohai_plugin 'create tester' do 16 | plugin_name 'tester' 17 | end 18 | 19 | ohai_plugin 'another_test' do 20 | source_file 'another_test_source_file.rb' 21 | cookbook 'ohai_test' 22 | end 23 | 24 | ohai_plugin 'template_test' do 25 | resource :template 26 | variables(plugin_name: 'template_test') 27 | end 28 | 29 | # node['test'] comes from the ohai plugin 30 | file '/expected_file' do 31 | action :create 32 | only_if { node['tester'] } 33 | end 34 | 35 | ohai_plugin 'tester' do 36 | action :delete 37 | end 38 | 39 | file 'Initial status file' do 40 | path '/status' 41 | content 'Initial' 42 | end.run_action(:create) 43 | 44 | ohai_plugin 'load test_status' do 45 | plugin_name 'test_status' 46 | end 47 | 48 | ruby_block 'save loaded status' do 49 | block do 50 | ::File.write('/status', node['test_status'].inspect) 51 | end 52 | end.run_action(:run) 53 | 54 | file 'Next status' do 55 | path '/status' 56 | content 'Second' 57 | end.run_action(:create) 58 | 59 | ohai_plugin 'Tester and reload all plugins' do 60 | plugin_name 'tester' 61 | end 62 | 63 | ruby_block 'save loaded status second time' do 64 | block do 65 | ::File.write('/status2', node['test_status'].inspect) 66 | end 67 | end.run_action(:run) 68 | 69 | file 'Third status' do 70 | path '/status' 71 | content 'Updated by tester1 load' 72 | end.run_action(:create) 73 | 74 | ohai_plugin 'Install Tester1 and load only tester1' do 75 | plugin_name 'tester1' 76 | load_single_plugin true 77 | end 78 | 79 | ruby_block 'save loaded status third time' do 80 | block do 81 | ::File.write('/status3', node['test_status'].inspect) 82 | end 83 | end.run_action(:run) 84 | -------------------------------------------------------------------------------- /test/cookbooks/ohai_test/templates/default/template_test.rb: -------------------------------------------------------------------------------- 1 | Ohai.plugin(:Templatetest) do 2 | provides '<%= @plugin_name -%>' 3 | 4 | collect_data do 5 | template_test Mash.new 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /test/integration/default/default_spec.rb: -------------------------------------------------------------------------------- 1 | hint_path = case os[:family] 2 | when 'windows' 3 | 'C:/chef/ohai/hints' 4 | else 5 | '/etc/chef/ohai/hints' 6 | end 7 | 8 | describe file("#{hint_path}/hint_at_compile_time.json") do 9 | it { should be_file } 10 | end 11 | 12 | describe file("#{hint_path}/hint_with_content.json") do 13 | it { should be_file } 14 | end 15 | 16 | describe file("#{hint_path}/hint_without_content.json") do 17 | it { should be_file } 18 | end 19 | 20 | describe file("#{hint_path}/hint_with_json_in_resource_name.json") do 21 | it { should be_file } 22 | end 23 | 24 | # we created this file if ohai data from a loaded plugin existed 25 | describe file('/expected_file') do 26 | it { should be_file } 27 | end 28 | 29 | # Shows that running with load_single_plugin does not reload all of the plugins 30 | describe file('/status') do 31 | its(:content) { should match /Updated by tester1 load/ } 32 | end 33 | 34 | describe file('/status2') do 35 | its(:content) { should match /status\"=>\"Second/ } 36 | end 37 | 38 | describe file('/status3') do 39 | its(:content) { should match /status\"=>\"Second/ } 40 | end 41 | --------------------------------------------------------------------------------