Class: VagrantPlugins::TopLevelCookbooks::Action::Clone

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-toplevel-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-toplevel-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', 'toplevel-cookbooks', env[:machine].name.to_s, 'repo')
  @cookbook_install_path = env[:root_path].join('.vagrant', 'toplevel-cookbooks', env[:machine].name.to_s, 'cookbooks')
  
  # shortcut for values from config
  @git_url = env[:machine].config.toplevel_cookbook.url
  @git_ref = env[:machine].config.toplevel_cookbook.ref
end

Instance Attribute Details

#cloned_repo_pathObject (readonly)

Returns the value of attribute cloned_repo_path.



8
9
10
# File 'lib/vagrant-toplevel-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-toplevel-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-toplevel-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-toplevel-cookbooks/action/clone.rb', line 8

def git_url
  @git_url
end

Instance Method Details

#app_cookbook_configured?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/vagrant-toplevel-cookbooks/action/clone.rb', line 55

def app_cookbook_configured?
  git_url != nil
end

#call(env) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/vagrant-toplevel-cookbooks/action/clone.rb', line 102

def call(env)

  if app_cookbook_configured? && has_chef_solo_provisioner?

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

    log "Ensuring top-level cookbook is checked out at '#{git_ref}'"
    checkout_and_update

    log "Installing top-level cookbook dependencies to '#{cookbook_install_path}'"
    install_cookbooks

    log "Configuring Chef Solo provisioner for top-level cookbook"
    configure_chef_solo
  end

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

#checkout_and_updateObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vagrant-toplevel-cookbooks/action/clone.rb', line 67

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



59
60
61
62
63
64
65
# File 'lib/vagrant-toplevel-cookbooks/action/clone.rb', line 59

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



91
92
93
94
95
96
97
98
99
# File 'lib/vagrant-toplevel-cookbooks/action/clone.rb', line 91

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.provisioning_path = "/tmp/vagrant-toplevel-cookbooks"
    provisioner.config.cookbooks_path = provisioner.config.send(:prepare_folders_config, cookbook_install_path)
  end
end

#cookbooks_path_configured?(provisioner) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
# File 'lib/vagrant-toplevel-cookbooks/action/clone.rb', line 46

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



42
43
44
# File 'lib/vagrant-toplevel-cookbooks/action/clone.rb', line 42

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

#has_chef_solo_provisioner?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/vagrant-toplevel-cookbooks/action/clone.rb', line 51

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

#install_cookbooksObject



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

def install_cookbooks
  Dir.chdir(cloned_repo_path) do
    FileUtils.rm_rf cookbook_install_path
    Bundler.with_clean_env do
      system "berks vendor #{cookbook_install_path}"
    end
  end
end

#is_cloned?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/vagrant-toplevel-cookbooks/action/clone.rb', line 38

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

#log(msg) ⇒ Object



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

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

#provisioners(type) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/vagrant-toplevel-cookbooks/action/clone.rb', line 23

def provisioners(type)
  @env[:machine].config.vm.provisioners.select do |provisioner|
    if (provisioner.respond_to? :type)
      # Vagrant 1.7+ compatibility
      provisioner.type == type
    else
      provisioner.name == type
    end
  end
end