Class: Berkshelf::Vagrant::Action::LoadShelf

Inherits:
Object
  • Object
show all
Includes:
EnvHelpers
Defined in:
lib/berkshelf/vagrant/action/load_shelf.rb

Overview

Author:

Instance Method Summary collapse

Methods included from EnvHelpers

#berkshelf_enabled?, #cache_file, #chef_client?, #chef_solo?, #provision_disabled?, #provisioners

Constructor Details

#initialize(app, env) ⇒ LoadShelf

Returns a new instance of LoadShelf.



8
9
10
# File 'lib/berkshelf/vagrant/action/load_shelf.rb', line 8

def initialize(app, env)
  @app = app
end

Instance Method Details

#cache_shelf(path, env) ⇒ String

Parameters:

  • path (String)

Returns:

  • (String)


34
35
36
37
38
39
40
41
42
# File 'lib/berkshelf/vagrant/action/load_shelf.rb', line 34

def cache_shelf(path, env)
  FileUtils.mkdir_p(File.dirname(path))

  File.open((cache_file(env)), 'w+') do |f|
    f.write(path)
  end

  path
end

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/berkshelf/vagrant/action/load_shelf.rb', line 12

def call(env)
  unless berkshelf_enabled?(env)
    return @app.call(env)
  end

  # Make sure that Berkshelf itself uses distinct directories for each vagrant run.
  ENV['BERKSHELF_PATH'] = File.join(Berkshelf.berkshelf_path, env[:machine].name.to_s) unless ENV['BERKSHELF_PATH']

  shelf = load_shelf env

  if shelf.nil?
    shelf = cache_shelf(Berkshelf::Vagrant.mkshelf(env[:machine].name), env)
  end

  env[:berkshelf].shelf = shelf

  @app.call(env)
end

#load_shelf(env) ⇒ String?

Returns:

  • (String, nil)


45
46
47
48
49
# File 'lib/berkshelf/vagrant/action/load_shelf.rb', line 45

def load_shelf(env)
  return nil unless File.exist?(cache_file(env))

  File.read(cache_file(env)).chomp
end