Class: VagrantPlugins::Berkshelf::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-berkshelf/config.rb

Constant Summary collapse

MAYBE =
Object.new.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/vagrant-berkshelf/config.rb', line 29

def initialize
  super

  @berksfile_path = UNSET_VALUE
  @enabled        = UNSET_VALUE
  @except         = Array.new
  @only           = Array.new
  @args           = Array.new

  @__finalized = false
end

Instance Attribute Details

#argsArray<String>

An array of additional arguments to pass to the Berkshelf command.

Returns:

  • (Array<String>)


27
28
29
# File 'lib/vagrant-berkshelf/config.rb', line 27

def args
  @args
end

#berksfile_pathString

The path to the Berksfile to use.

Returns:

  • (String)


11
12
13
# File 'lib/vagrant-berkshelf/config.rb', line 11

def berksfile_path
  @berksfile_path
end

#enabledBoolean

Disable the use of Berkshelf in Vagrant.

Returns:

  • (Boolean)


15
16
17
# File 'lib/vagrant-berkshelf/config.rb', line 15

def enabled
  @enabled
end

#exceptArray<Symbol>

The array of cookbook groups to exclude during provisioning.

Returns:

  • (Array<Symbol>)


23
24
25
# File 'lib/vagrant-berkshelf/config.rb', line 23

def except
  @except
end

#onlyArray<Symbol>

The array of cookbook groups to exclusively install during provisioning.

Returns:

  • (Array<Symbol>)


19
20
21
# File 'lib/vagrant-berkshelf/config.rb', line 19

def only
  @only
end

Instance Method Details

#finalize!Object



41
42
43
44
45
46
# File 'lib/vagrant-berkshelf/config.rb', line 41

def finalize!
  @berksfile_path = nil if @berksfile_path == UNSET_VALUE
  @enabled = MAYBE if @enabled == UNSET_VALUE

  @__finalized = true
end

#missing?(obj) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/vagrant-berkshelf/config.rb', line 102

def missing?(obj)
  obj.to_s.strip.empty?
end

#to_hashObject



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/vagrant-berkshelf/config.rb', line 90

def to_hash
  raise "Must finalize first." if !@__finalized

  {
    enabled:        @enabled,
    berksfile_path: @berksfile_path,
    except:         @except,
    only:           @only,
    args:           @args,
  }
end

#validate(machine) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vagrant-berkshelf/config.rb', line 48

def validate(machine)
  errors = _detected_errors

  if @enabled || @enabled == MAYBE
    # If no Berksfile path was given, check if one is in the working
    # directory
    if !@berksfile_path
      path = File.expand_path("Berksfile", machine.env.root_path)

      if File.exist?(path)
        @enabled = true
        @berksfile_path = path
      else
        # Disable the plugin unless it was specifically set to "true". If
        # the user set the value, we want to return an error, but if the
        # user did not explicitly enable the plugin, we should just
        # disable it automatically.
        @enabled = false unless @enabled == true
      end
    end

    if @enabled
      # Berksfile_path validations
      if missing?(@berksfile_path)
        errors << "berksfile_path must be set"
      else
        # Expand the path unless it is absolute
        if !Pathname.new(@berksfile_path).absolute?
          @berksfile_path = File.expand_path(@berksfile_path, machine.env.root_path)
        end

        # Ensure the path exists
        if !File.exist?(@berksfile_path)
          errors << "Berksfile at '#{@berksfile_path}' does not exist"
        end
      end
    end
  end

  { "Berkshelf" => errors }
end