Module: ChefTLCWorkflow::Helpers

Defined in:
lib/chef-tlc-workflow/helpers.rb

Class Method Summary collapse

Class Method Details

.parse_name_and_version(string) ⇒ Object

parse input string “<app-cookbook-name>@<version>” into a ‘[name, version]` pair by splitting at the ’@‘ character



61
62
63
64
65
66
67
# File 'lib/chef-tlc-workflow/helpers.rb', line 61

def self.parse_name_and_version(string)
  if string == nil
    [nil, nil]
  else
    string.split '@'
  end
end

.read_app_cookbooks(yml_file, name = nil, version = nil) ⇒ Object

reads in the YAML file containing the application cookbooks and returns either all application cookbooks defined there or a specific ones if the ‘name` and/or `version` parameters are given to filter the selection.

Example .yml file:

- name: "sample-app"
  version: "0.1.0"
  git: "https://github.com/tknerr/sample-app-tlc.git"
  ref: "v0.1.0"
- name: "sample-app"
  version: "0.2.0"
  path: "../sample-app"


48
49
50
51
52
53
54
55
# File 'lib/chef-tlc-workflow/helpers.rb', line 48

def self.read_app_cookbooks(yml_file, name = nil, version = nil)
  # TODO: validate format
  require 'yaml'
  app_cookbooks = YAML.load_file yml_file
  app_cookbooks.select! { |ac| ac['name'] == name } if name
  app_cookbooks.select! { |ac| ac['version'] == version } if version
  app_cookbooks
end

.read_berkshelf_depsObject

returns the resolved dependencies as specified in ‘Berksfile` as a map of dependency => version (including the transitive ones), e.g.:

{ 'foo' => '1.0.0', 'bar' => '0.1.0', 'baz_which_depends_on_bar' => '1.5.0' }


24
25
26
27
28
29
# File 'lib/chef-tlc-workflow/helpers.rb', line 24

def self.read_berkshelf_deps
  require 'json'
  deps = JSON.parse(`berks list --format=json`)
  fail "error resolving cookbooks: #{deps['errors']}" unless deps['errors'].empty?
  Hash[deps['cookbooks'].map { |cb| [cb['name'], cb['version']] }]
end

.read_metadata_depsObject

reads the direct dependencies defined in ‘metadata.rb` and returns a map of dependency => version, e.g.:

{ 'foo' => '1.0.0', 'bar' => '0.1.0' }


11
12
13
14
15
16
# File 'lib/chef-tlc-workflow/helpers.rb', line 11

def self.
  require 'chef/cookbook/metadata'
   = ::Chef::Cookbook::Metadata.new
  .from_file "metadata.rb"
  .dependencies
end