Class: Berktacular::Cookbook

Inherits:
Object
  • Object
show all
Defined in:
lib/berktacular/cookbook.rb

Overview

This class represents a cookbook entry from a Berksfile

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, version_spec, config = nil, opts = {}) ⇒ Cookbook

Creates a new cookbook entry for a Berksfile.

Parameters:

  • name (String)

    the name of the cookbook to use.

  • version_spec (String)

    the exact version number as in a chef environment file. eg. ‘= 1.2.3’

  • config (Hash, nil) (defaults to: nil)

    the cookbook_location hash to for this cookbook. Optional.

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :git_client (Octokit::Client) — default: nil

    the github client to use.

  • :upgrade (True, False) — default: False

    whether or not to check for updates. auto_upgrade must also be enabled for the updated entry to be used.

  • :verbose (True, False) — default: False

    be more verbose.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/berktacular/cookbook.rb', line 28

def initialize( name, version_spec, config = nil, opts = {} )
  @name             = name          || raise( "Missing cookbook name" )
  @version_spec     = version_spec  || raise( "Missing cookbook version" )
  @version_number   = VERSION_RE.match( version_spec )[0]
  @version_solved   = Semverse::Version.new(@version_number)
  @auto_upgrade     = config && config['auto_upgrade']  || false
  @versions         = config && config['versions']      || {}
  @location         = config ? config.reject{ |k,v| k == 'auto_upgrade' || k == 'versions' } : nil
  @version_only     = opts.has_key?(:versions_only) ? opts[:versions_only]  : false
  @upgrade          = opts.has_key?(:upgrade)      ? opts[:upgrade]         : false
  @git_client       = opts.has_key?(:git_client)   ? opts[:git_client].dup  : nil
  @verbose          = opts.has_key?(:verbose)      ? opts[:verbose]         : false
  @multi_cookbook_dir = opts[:multi_cookbook_dir]
  check_updates if @auto_upgrade && @upgrade
end

Instance Attribute Details

#auto_upgradeTrue, False (readonly)

Returns whether or now this cookbook can autoupgrade.

Returns:

  • (True, False)

    whether or now this cookbook can autoupgrade.



18
# File 'lib/berktacular/cookbook.rb', line 18

attr_reader :name, :version_number, :auto_upgrade, :config

#configObject (readonly)

Returns the value of attribute config.



18
# File 'lib/berktacular/cookbook.rb', line 18

attr_reader :name, :version_number, :auto_upgrade, :config

#nameString (readonly)

Returns the name of the cookbook.

Returns:

  • (String)

    the name of the cookbook.



18
19
20
# File 'lib/berktacular/cookbook.rb', line 18

def name
  @name
end

#version_numberString (readonly)

Returns the exact version of the cookbook.

Returns:

  • (String)

    the exact version of the cookbook.



18
# File 'lib/berktacular/cookbook.rb', line 18

attr_reader :name, :version_number, :auto_upgrade, :config

Instance Method Details

#check_updatesArray

Returns a list of available cookbook version newer then we started with, with most recent first.

Returns:

  • (Array)

    a list of available cookbook version newer then we started with, with most recent first



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/berktacular/cookbook.rb', line 66

def check_updates
  tag_re = Regexp.new(
    "^#{ (@location || {})['tag'] || '%{version}' }$" % { version: "(#{VERSION_RE.source})" }
  )
  @candidates ||= if @location && @location['github']
    get_tags_from_github
  else
    []
  end.collect do |tag|
    m = tag_re.match(tag)
    next unless m
    v = m[1]
    begin
      t = Semverse::Version.new(v)
    rescue Semverse::InvalidVersionFormat
      next
    end
    next unless t > @version_solved
    t.to_s
  end.compact.sort.reverse
end

#latest_versionString

Returns the latest available version number of the cookbook.

Returns:

  • (String)

    the latest available version number of the cookbook



50
51
52
# File 'lib/berktacular/cookbook.rb', line 50

def latest_version
  check_updates.any? ? check_updates.first : @version_number
end

#line(upgrade = @upgrade) ⇒ String

param upgrade [True,False] (‘@upgrade’) whether or not to force the lastest version when @auto_update is enabled

Returns:

  • (String)

    a Berksfile line for this cookbook



61
62
63
# File 'lib/berktacular/cookbook.rb', line 61

def line(upgrade = @upgrade)
  "cookbook \"#{@name}\", #{generate_conf_line(upgrade, @location )}"
end

#to_sString

Returns a Berksfile line for this cookbook.

Returns:

  • (String)

    a Berksfile line for this cookbook



55
56
57
# File 'lib/berktacular/cookbook.rb', line 55

def to_s
  line
end

#version_specifierString

Returns the exact version of the cookbook.

Returns:

  • (String)

    the exact version of the cookbook



45
46
47
# File 'lib/berktacular/cookbook.rb', line 45

def version_specifier
  "= #{(@auto_upgrade && @upgrade && check_updates.any?) ? check_updates.first : @version_number }"
end