Skip to content

Instantly share code, notes, and snippets.

@jtimberman
Created September 11, 2012 04:29
Show Gist options
  • Save jtimberman/3695966 to your computer and use it in GitHub Desktop.
Save jtimberman/3695966 to your computer and use it in GitHub Desktop.
class Chef
class Recipe
def skip_unsupported_platform
unless supported_platform?
Chef::Log.info("Unsupported platform #{node[:platform]} in #{cookbook_name}::#{recipe_name}, skipping recipe")
Chef::Log.debug("You said to skip unsupported platforms in #{cookbook_name}::#{recipe_name}")
Chef::Log.debug("and #{node['platform']} is unsupported by cookbook #{cookbook_name}. Update the cookbook's metadata")
Chef::Log.debug("if this is a supported platform, or remove #{cookbook_name}::#{recipe_name} from the node.")
end
end
def supported_platform?
run_context.cookbook_collection[cookbook_name].metadata.platforms.include?(node['platform'])
end
end
end
# Put this at the top of a recipe if you want to enforce that it should only be run on
# platforms that are supported per the cookbook's metadata
return if skip_unsupported_platform
@jtimberman
Copy link
Author

Hat tip to Alex Howells and Jay Feldblum for the run_context.cookbook_collection reminder for inspecting cookbook metadata.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment