Class: VagrantAWS::Action::PrepareProvisioners::ChefSolo

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-aws/action/prepare_provisioners.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ ChefSolo

Returns a new instance of ChefSolo.



44
45
46
# File 'lib/vagrant-aws/action/prepare_provisioners.rb', line 44

def initialize(config)
	@config = config
end

Class Method Details

.prepare(config) ⇒ Object



37
38
39
40
41
42
# File 'lib/vagrant-aws/action/prepare_provisioners.rb', line 37

def self.prepare(config)
	my_preparer = new(config)
	my_preparer.bootstrap_if_needed
	my_preparer.chown_provisioning_folder
	my_preparer.copy_and_update_paths	
end

Instance Method Details

#bootstrap_if_neededObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/vagrant-aws/action/prepare_provisioners.rb', line 48

def bootstrap_if_needed
	begin
		@config.env.vm.ssh.execute do |ssh|
			ssh.sudo!("which chef-solo")
		end
	rescue Vagrant::Errors::VagrantError 
		# Bootstrap chef-solo
		@config.env.ui.info I18n.t("vagrant.plugins.aws.actions.prepare_provisioners.chef_not_detected", :binary => "chef-solo")
		@config.env.vm.system.bootstrap_chef
	end
end

#chown_provisioning_folderObject



61
62
63
64
65
66
# File 'lib/vagrant-aws/action/prepare_provisioners.rb', line 61

def chown_provisioning_folder
	@config.env.vm.ssh.execute do |ssh|
		ssh.sudo!("mkdir -p #{@config.provisioning_path}")
		ssh.sudo!("chown #{@config.env.config.ssh.username} #{@config.provisioning_path}")
	end
end

#copy_and_update_pathsObject



68
69
70
71
72
73
74
75
# File 'lib/vagrant-aws/action/prepare_provisioners.rb', line 68

def copy_and_update_paths
	# Copy relevant host paths to remote instance and update provisioner config
	# to point to new "vm" paths for cookbooks, etc.
	%w{ cookbooks_path roles_path data_bags_path }.each do |path|
		copy_host_paths(@config.send(path), path)
		@config.send "#{path}=", strip_host_paths(@config.send(path)).push([:vm, path])
	end
end

#copy_host_paths(paths, target_directory) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/vagrant-aws/action/prepare_provisioners.rb', line 77

def copy_host_paths(paths, target_directory)
	archive  = tar_host_folder_paths(paths)
	
	target_d = "#{@config.provisioning_path}/#{target_directory}"
	target_f = target_d + '.tar'
	
	@config.env.vm.ssh.upload!(archive.path, target_f)
	@config.env.vm.ssh.execute do |ssh|
		ssh.sudo!([
			"mkdir -p #{target_d}",
			"chown #{@config.env.config.ssh.username} #{target_d}",
			"tar -C #{target_d} -xf #{target_f}"
		])
	end

	target_directory
end

#host_folder_paths(paths) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/vagrant-aws/action/prepare_provisioners.rb', line 108

def host_folder_paths(paths)
	paths = [paths] if paths.is_a?(String) || paths.first.is_a?(Symbol)
	paths.inject([]) do |acc, path|
		path = [:host, path] if !path.is_a?(Array)
		type, path = path
		acc << File.expand_path(path, @config.env.root_path) if type == :host
		acc
	end
end

#strip_host_paths(paths) ⇒ Object



118
119
120
121
# File 'lib/vagrant-aws/action/prepare_provisioners.rb', line 118

def strip_host_paths(paths)
	paths = [paths] if paths.is_a?(String) || paths.first.is_a?(Symbol)
	paths.delete_if { |path| !path.is_a?(Array) || path[0] == :host }
end

#tar_host_folder_paths(paths) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/vagrant-aws/action/prepare_provisioners.rb', line 95

def tar_host_folder_paths(paths)
	tarf = Tempfile.new(['vagrant-chef-solo','.tar']) 
	Archive::Tar::Minitar::Output.open(tarf) do |outp|
		host_folder_paths(paths).each do |full_path|
			Dir.chdir(full_path) do |ignored| 
				Dir.glob("**#{File::SEPARATOR}**") { |entry| Archive::Tar::Minitar.pack_file(entry, outp) }
			end
		end
	end
	tarf
end