Class: VagrantPlugins::ApplicationCookbooks::Action::Clone

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-application-cookbooks/action/clone.rb

Overview

This middleware checks if reqiured plugins are present

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Clone

Returns a new instance of Clone.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/vagrant-application-cookbooks/action/clone.rb', line 10

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

  # machine-specific paths to clone git repo and install cookbooks
  @cloned_repo_path = env[:root_path].join('.vagrant', 'app-cookbooks', env[:machine].name.to_s, 'repo')
  @cookbook_install_path = env[:root_path].join('.vagrant', 'app-cookbooks', env[:machine].name.to_s, 'cookbooks')
  
  # shortcut for values from config
  @git_url = env[:machine].config.app_cookbook.url
  @git_ref = env[:machine].config.app_cookbook.ref
end

Instance Attribute Details

#cloned_repo_pathObject (readonly)

Returns the value of attribute cloned_repo_path.



8
9
10
# File 'lib/vagrant-application-cookbooks/action/clone.rb', line 8

def cloned_repo_path
  @cloned_repo_path
end

#cookbook_install_pathObject (readonly)

Returns the value of attribute cookbook_install_path.



8
9
10
# File 'lib/vagrant-application-cookbooks/action/clone.rb', line 8

def cookbook_install_path
  @cookbook_install_path
end

#git_refObject (readonly)

Returns the value of attribute git_ref.



8
9
10
# File 'lib/vagrant-application-cookbooks/action/clone.rb', line 8

def git_ref
  @git_ref
end

#git_urlObject (readonly)

Returns the value of attribute git_url.



8
9
10
# File 'lib/vagrant-application-cookbooks/action/clone.rb', line 8

def git_url
  @git_url
end

Instance Method Details

#app_cookbook_configured?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/vagrant-application-cookbooks/action/clone.rb', line 48

def app_cookbook_configured?
  git_url != nil
end

#call(env) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/vagrant-application-cookbooks/action/clone.rb', line 93

def call(env)

  if app_cookbook_configured? && has_chef_solo_provisioner?

    if not is_cloned?
      log "Cloning application cookbook from '#{git_url}'"
      clean_and_clone_repo
    else
      log "Using application cookbook '#{git_url}'"
    end

    log "Ensuring application cookbook is checked out at '#{git_ref}'"
    checkout_and_update

    log "Installing application cookbook dependencies to '#{cookbook_install_path}'"
    install_cookbooks

    log "Configuring Chef Solo provisioner for application cookbook"
    configure_chef_solo
  end

  # continue if ok
  @app.call(env)
end

#checkout_and_updateObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/vagrant-application-cookbooks/action/clone.rb', line 60

def checkout_and_update
  Dir.chdir(cloned_repo_path) do
    # retrieve all refs
    system("git fetch -q --all")
    # checkout ref
    unless system("git checkout -q #{git_ref}")
      raise "something went wrong while checking out '#{git_ref}'"
    end
    # update ref only if we are on a branch, i.e. not on a detached HEAD
    unless `git symbolic-ref -q HEAD`.empty?
      system("git pull -q")
    end
  end
end

#clean_and_clone_repoObject



52
53
54
55
56
57
58
# File 'lib/vagrant-application-cookbooks/action/clone.rb', line 52

def clean_and_clone_repo
  FileUtils.rm_rf cloned_repo_path
  FileUtils.mkdir_p cloned_repo_path
  unless system("git clone -q #{git_url} #{cloned_repo_path}")
    raise "something went wrong while cloning '#{git_url}'"
  end
end

#configure_chef_soloObject



83
84
85
86
87
88
89
90
# File 'lib/vagrant-application-cookbooks/action/clone.rb', line 83

def configure_chef_solo
  provisioners(:chef_solo).each do |provisioner|
    if cookbooks_path_configured? provisioner
      @env[:ui].warn "WARNING: already configured `cookbooks_path` will be overridden!"
    end
    provisioner.config.cookbooks_path = provisioner.config.send(:prepare_folders_config, cookbook_install_path)
  end
end

#cookbooks_path_configured?(provisioner) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/vagrant-application-cookbooks/action/clone.rb', line 39

def cookbooks_path_configured?(provisioner)
  # see https://github.com/mitchellh/vagrant/blob/master/plugins/provisioners/chef/config/chef_solo.rb#L41-45
  provisioner.config.cookbooks_path != [[:host, "cookbooks"], [:vm, "cookbooks"]]
end

#get_originObject



35
36
37
# File 'lib/vagrant-application-cookbooks/action/clone.rb', line 35

def get_origin
  `cd #{cloned_repo_path} && git config --get remote.origin.url`.strip
end

#has_chef_solo_provisioner?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/vagrant-application-cookbooks/action/clone.rb', line 44

def has_chef_solo_provisioner?
  provisioners(:chef_solo).size > 0
end

#install_cookbooksObject



75
76
77
78
79
80
81
# File 'lib/vagrant-application-cookbooks/action/clone.rb', line 75

def install_cookbooks
  Dir.chdir(cloned_repo_path) do
    require 'berkshelf'
    berksfile = Berkshelf::Berksfile.from_file('Berksfile')
    berksfile.install(path: cookbook_install_path)
  end
end

#is_cloned?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/vagrant-application-cookbooks/action/clone.rb', line 31

def is_cloned?
  File.exist?(cloned_repo_path) && get_origin.eql?(git_url)
end

#log(msg) ⇒ Object



27
28
29
# File 'lib/vagrant-application-cookbooks/action/clone.rb', line 27

def log(msg)
  @env[:ui].info msg
end

#provisioners(name) ⇒ Object



23
24
25
# File 'lib/vagrant-application-cookbooks/action/clone.rb', line 23

def provisioners(name)
  @env[:machine].config.vm.provisioners.select{ |prov| prov.name == name }
end