Last active
November 17, 2016 08:58
-
-
Save zuazo/44308e89328ffb665ae1 to your computer and use it in GitHub Desktop.
This Chef Cookbook metadata.rb example reads the dependencies from Berksfile.lock. (Experimental)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# metadata.rb | |
# This metadata.rb example reads the dependencies from Berksfile.lock. This can | |
# be used to implement the Environment Pattern with Chef Server. | |
# | |
# The Environment Cookbook: | |
# http://blog.vialstudios.com/the-environment-cookbook-pattern/ | |
name 'my_face' | |
# [...] | |
description 'Installs My Face' | |
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) | |
# This version must be in sync with Berksfile.lock: | |
# Use `$ berks update my_face` in each version bump. | |
version '0.1.0' | |
def knife_upload? | |
self.class.name == 'Chef::Cookbook::Metadata' | |
end | |
def berkshelf_load | |
require 'berkshelf' | |
rescue LoadError | |
raise 'You need to install Berkshelf. You can use `$ gem install berkshelf`.' | |
end | |
def berks_upload? | |
# TODO: Find a more elegant solution | |
$LOADED_FEATURES | |
.select { |f| f.include? '/gems/' } | |
.reduce(false) do |memo, f| | |
memo || !f.match(/berkshelf\/uploader\.rb$/).nil? | |
end | |
end | |
def berksfile_lock | |
::File.join(::File.dirname(__FILE__), Berkshelf::Lockfile::DEFAULT_FILENAME) | |
.tap do |file| | |
next if ::File.exist?(file) | |
fail 'Berkshelf lock file not found. You need to run `$ berks install`.' | |
end | |
end | |
# Dependcies read from Berkshelf in berks install/update | |
if !knife_upload? && !berks_upload? | |
depends 'vialstudios-base', '~> 0.1.0' | |
depends 'nginx', '~> 2.7' | |
depends 'postgres', '~> 2.11' | |
depends 'rbenv', '~> 1.7' | |
# Outside Berkshelf, when uploading to Chef Server using knife or berks upload | |
else | |
# Read dependencies from Berksfile.lock | |
berkshelf_load | |
lockfile = Berkshelf::Lockfile.from_file(berksfile_lock) | |
lockfile.parse | |
lockfile.locks.each do |name, lock| | |
next if name.match(/_test$/) # Skip "_test" cookbooks used by test-kitchen | |
depends name, "= #{lock.locked_version}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment