Skip to content

Commit

Permalink
Fix some issues with the atomic counter and chef-solo
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Aug 4, 2011
1 parent 8ff269c commit 7df5cf6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
12 changes: 9 additions & 3 deletions lib/vagrant/provisioners/chef.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ module Provisioners
# chef-solo and chef-client provisioning are stored. This is **not an actual
# provisioner**. Instead, {ChefSolo} or {ChefServer} should be used.
class Chef < Base
include Util::Counter

def initialize(env, config)
super

config.provisioning_path ||= "/tmp/vagrant-chef-#{get_and_update_counter(:provisioning_path)}"
end

def prepare
raise ChefError, :invalid_provisioner
end
Expand Down Expand Up @@ -76,8 +84,6 @@ class ChefError < Errors::VagrantError
class Chef < Base
# This is the configuration which is available through `config.chef`
class Config < Vagrant::Config::Base
extend Util::Counter

# Shared config
attr_accessor :node_name
attr_accessor :provisioning_path
Expand All @@ -95,7 +101,7 @@ class Config < Vagrant::Config::Base
attr_writer :run_list

def initialize
@provisioning_path = "/tmp/vagrant-chef-#{self.class.get_and_update_counter}"
@provisioning_path = nil
@log_level = :info
@json = {}
@http_proxy = nil
Expand Down
5 changes: 3 additions & 2 deletions lib/vagrant/provisioners/chef_solo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class ChefSolo < Chef
register :chef_solo

extend Util::Counter
include Util::Counter

class Config < Chef::Config
attr_accessor :cookbooks_path
Expand Down Expand Up @@ -71,7 +72,7 @@ def expanded_folders(paths)
remote_path = nil
if type == :host
# Path exists on the host, setup the remote path
remote_path = "#{config.provisioning_path}/chef-solo-#{self.class.get_and_update_counter}"
remote_path = "#{config.provisioning_path}/chef-solo-#{get_and_update_counter(:cookbooks_path)}"
else
# Path already exists on the virtual machine. Expand it
# relative to where we're provisioning.
Expand All @@ -88,7 +89,7 @@ def expanded_folders(paths)
def share_folders(prefix, folders)
folders.each do |type, local_path, remote_path|
if type == :host
env.config.vm.share_folder("v-#{prefix}-#{self.class.get_and_update_counter}",
env.config.vm.share_folder("v-#{prefix}-#{self.class.get_and_update_counter(:shared_folder)}",
remote_path, local_path, :nfs => config.nfs)
end
end
Expand Down
10 changes: 6 additions & 4 deletions lib/vagrant/util/counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ module Util
# Atomic counter implementation. This is useful for incrementing
# a counter which is guaranteed to only be used once in its class.
module Counter
def get_and_update_counter
def get_and_update_counter(name=nil)
name ||= :global

mutex.synchronize do
@__counter ||= 1
result = @__counter
@__counter += 1
@__counter ||= Hash.new(1)
result = @__counter[name]
@__counter[name] += 1
result
end
end
Expand Down

0 comments on commit 7df5cf6

Please sign in to comment.