Umbrella Project: Chef Infra
Project State: Active
Issues Response Time Maximum: 14 days
Pull Request Response Time Maximum: 14 days
Chef Utils gem contains common code and mixins for the core Chef Infra Ruby gems. This is intended to be a "core" or "foundations" library for the chef ecosystem (and external related gems) which allows the use of core code and utility functions of the chef gem without requiring all the heaviness of the chef gem.
The Platform Family helpers provide an alternative to comparing values from node['platform_family'] or using the platform_family?() helper method. They allow you to write simpler logic that checks for specific platform family. Additionally we've provided several super families listed below, which bundle together common platform families into a single helper.
aix?amazon?arch?- includes arch, manjaro, and antergos platformsdebian?- includes debian, ubuntu, linuxmint, raspbian, and devuan platformsdragonflybsd?fedora?- includes arista_eos, fedora, and pidora platformsfreebsd?gentoo?macos?macos_ruby?- this is always true if the ruby VM is running on a mac host and is not stubbed by ChefSpecnetbsd?openbsd?rhel?- includes redhat, centos, scientific, oracle, and clearos platformsrhel6?- includes redhat6, centos6, scientific6, oracle6, and clearos6 platformsrhel7?- includes redhat7, centos7, scientific7, oracle7, and clearos7 platformsrhel8?- includes redhat8, centos8, scientific8, oracle8, and clearos8 platformssmartos?solaris2?suse?- includes suse and opensuseleap platformswindows?- NOTE: in a class context when called without a node object (ChefUtils.windows?) this is not stubbable by ChefSpec, but when called with a node as the first argument or when called from the DSL it is stubabble by chefspecwindows_ruby?- this is always true if the ruby VM is running on a windows host and is not stubbed by ChefSpec
Super Families:
fedora_derived?- anything of fedora lineage (fedora, redhat, centos, amazon, pidora, etc)redhat_based?- fedora and rhel platform_families, nothing else. This is most likely not as useful as thefedora_derived?helper.rpm_based?- allfedora_basedsystems plussuseand any future linux distros based on RPM (excluding AIX)solaris_based?- all solaris-derived systems (omnios, smartos, openindiana, etc)bsd_based?- all bsd-derived systems (freebsd, netbsd, openbsd, dragonflybsd).
The Platform helpers provide an alternative to comparing values from node['platform'] or using the platform?() helper method. In general we'd highly suggest writing code using the Platform Family helpers, but these helpers can be used when it's necessary to target specific platforms.
aix_platform?amazon_platform?arch_platform?centos_platform?clearos_platform?debian_platform?dragonfly_platform?fedora_platform?freebsd_platform?gentoo_platform?leap_platform?linuxmint_platform?macos_platform?netbsd_platform?omnios_platform?openbsd_platform?openindiana_platform?opensuse_platform?oracle_platform?raspbian_platform?redhat_platform?scientific_platform?slackware_platform?smartos_platform?solaris2_platform?suse_platform?ubuntu_platform?windows_platform?
For compatibility with old chef-sugar code the following aliases work for backwards compatibility, but will be DEPRECATED in the future.
centos?clearos?linuxmint?omnios?openindiana?opensuse?oracle?raspbian?redhat?scientific?ubuntu?
The OS helpers provide an alternative to comparing data from node['os'].
linux?- Any Linux distributiondarwin?- Darwin or macOS platforms
Architecture Helpers allow you to determine the processor architecture of your node.
_32_bit?_64_bit?arm?armhf?i386?intel?powerpc?ppc64?ppc64le?s390?s390x?sparc?
cloud?- if the node is running in any cloud, including internal onesalibaba?- if the node is running in alibaba cloudec2?- if the node is running in ec2gce?- if the node is running in gcerackspace?- if the node is running in rackspaceeucalyptus?- if the node is running under eucalyptuslinode?- if the node is running in linodeopenstack?- if the node is running under openstackazure?- if the node is running in azuredigital_ocean?- if the node is running in digital oceansoftlayer?- if the node is running in softlayeroci?- if the node is running in oracle cloud infrastructure
kvm?- if the node is a kvm guestkvm_host?- if the node is a kvm hostlxc?- if the node is an lxc guestlxc_host?- if the node is an lxc hostparallels?- if the node is a parallels guestparallels_host?- if the node is a parallels hostvbox?- if the node is a virtualbox guestvbox_host?- if the node is a virtualbox hostvmware?- if the node is a vmware guestvmware_host?- if the node is a vmware hostopenvz?- if the node is an openvz guestopenvz_host?- if the node is an openvz hostguest?- if the node is detected as any kind of guesthypervisor?- if the node is detected as being any kind of hypervisorphysical?- the node is not running as a guest (may be a hypervisor or may be bare-metal)vagrant?- attempts to identify the node as a vagrant guest (this check may be error prone)
EXPERIMENTAL: APIs may have breaking changes any time without warning
file_exist?file_open
docker?- if the node is running inside of dockersystemd?- if the init system is systemdkitchen?- if ENV['TEST_KITCHEN'] is setci?- if ENV['CI'] is setinclude_recipe?(recipe_name)- if therecipe_nameis in the run list, the expanded run list, or has beeninclude_recipe'd.
-
debianrcd?- if theupdate-rc.dbinary is present -
invokercd?- if theinvoke-rc.dbinary is present -
upstart?- if theinitctlbinary is present -
insserv?- if theinsservbinary is present -
redhatrcd?- if thechkconfigbinary is present -
service_script_exist?(type, service)
whichwhere
sanitized_path
render_jsonrender_tomlrender_yaml
To use the helpers in a class or module in a cookbook library file you can include the ChefUtils DSL:
module MyHelper
include ChefUtils # or any individual module with DSL methods in it
def do_something
puts "RHEL" if rhel?
end
extend self
endNow you can include MyHelper in another class/module, or you can call MyHelper.do_something directly.
The design of the DSL helper libraries in this gem are designed around the Chef Infra Client use cases. Most of the helpers are
accessible through the Chef DSL directly via the ChefUtils module. They are also available via class method calls on
the ChefUtils module directly (e.g. ChefUtils.debian?). For that to be possible there is Chef Infra Client specific wiring in
the ChefUtils::Internal class allowing the helpers to access the Chef.run_context global values. This allows them to be
used from library helpers in cookbooks inside Chef Infra Client.
For external use in other gems, this automatic wiring will not work correctly, and so it will not generally be possible to
call helpers off of the ChefUtils class (some exceptions that do not require a node-like object or a train connection will
may still work). For use in other gems you should create your own module and mixin the helper class. If you have a node
method in your class/module then that method will be used.
You can wire up a module which implements the Chef DSL with your own wiring using this template:
module MyDSL
include ChefUtils # or any individual module with DSL methods in it
private
def __getnode
# return something Mash-like with node semantics with a node["platform"], etc.
end
def __transport_connection
# return a Train::Transport connection
end
extend self # if your wiring is to global state to make `MyDSL.helper?` work.
endThose methods are marked API private for the purposes of end-users, but are public APIs for the purposes of software development.
We'd love to have your help developing Chef Infra. See our Contributing Document for more information on getting started.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
See COPYRIGHT.md.