Class: VagrantPlugins::ProviderZone::Action::Package

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-zone/action/package.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Package

Returns a new instance of Package.



7
8
9
10
11
# File 'lib/vagrant-zone/action/package.rb', line 7

def initialize(app, env)
	@logger = Log4r::Logger.new("vagrant_zone::action::import")
	@app = app
	env['package.output'] ||= 'package.box'
end

Instance Method Details

#assemble_box(boxname, extra) ⇒ Object



102
103
104
# File 'lib/vagrant-zone/action/package.rb', line 102

def assemble_box(boxname, extra)
	`tar cvzf "#{boxname}" ./metadata.json ./Vagrantfile ./box.zss #{extra}`
end

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/vagrant-zone/action/package.rb', line 15

def call(env)
	@machine = env[:machine]
	@driver  = @machine.provider.driver

	boxname = env['package.output']
	raise "#{boxname}: Already exists" if File.exist?(boxname)

	tmp_dir = Dir.pwd + '/_tmp_package'
	tmp_img = tmp_dir + '/box.zss'
	Dir.mkdir(tmp_dir) unless File.exists?(tmp_dir)

	zonepath = @machine.provider_config.zonepath.sub!(/^\//, '')
	brand  = @machine.provider_config.brand
	kernel = @machine.provider_config.kernel

	snapshot_create(zonepath)
	snapshot_send(zonepath, tmp_img)
	snapshot_delete(zonepath)

	extra = ''
	@tmp_include = tmp_dir + '/_include'
	if env['package.include']
		extra = './_include'
		Dir.mkdir(@tmp_include)
		env['package.include'].each do |f|
			env[:ui].info("Including user file: #{f}")
			FileUtils.cp(f, @tmp_include)
		end
	end
	if env['package.vagrantfile']
		extra = './_include'
		Dir.mkdir(@tmp_include) unless File.directory?(@tmp_include)
		env[:ui].info('Including user Vagrantfile')
		FileUtils.cp(env['package.vagrantfile'], @tmp_include + '/Vagrantfile')
	end

	File.write(tmp_dir + '/metadata.json', (brand, kernel))
	File.write(tmp_dir + '/Vagrantfile', vagrantfile_content(brand, kernel, zonepath))

	Dir.chdir(tmp_dir)
	assemble_box(boxname, extra)

	FileUtils.mv(tmp_dir + '/' + boxname, '../' + boxname)
	FileUtils.rm_rf(tmp_dir)

	env[:ui].info('Box created')
	env[:ui].info('You can now add the box:')
	env[:ui].info("vagrant box add #{boxname} --name any_comfortable_name")

	@app.call(env)
end

#metadata_content(brand, kernel) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/vagrant-zone/action/package.rb', line 77

def (brand, kernel)
	<<-EOF
	{
		"provider": "zone",
		"format": "zss",
		"brand": "#{brand}",
		"kernel": "#{kernel}"
	}
	EOF
end

#snapshot_create(zonepath) ⇒ Object



67
68
69
# File 'lib/vagrant-zone/action/package.rb', line 67

def snapshot_create(zonepath)
	`pfexec zfs snapshot -r #{zonepath}@vagrant_boxing`
end

#snapshot_delete(zonepath) ⇒ Object



70
71
72
# File 'lib/vagrant-zone/action/package.rb', line 70

def snapshot_delete(zonepath)
	`pfexec zfs destroy #{zonepath}@vagrant_boxing`
end

#snapshot_send(zonepath, destination) ⇒ Object



73
74
75
# File 'lib/vagrant-zone/action/package.rb', line 73

def snapshot_send(zonepath, destination)
	`pfexec zfs send #{zonepath}@vagrant_boxing > #{destination}`
end

#vagrantfile_content(brand, kernel, zonepath) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/vagrant-zone/action/package.rb', line 88

def vagrantfile_content(brand, kernel, zonepath)
	<<-EOF
	Vagrant.configure("2") do |config|
		config.vm.provider :zone do |zone|
			zone.brand = "#{brand}"
			zone.kernel = "#{kernel}"
			zone.zonepath = "#{zonepath}"
		end
	end
	user_vagrantfile = File.expand_path('../_include/Vagrantfile', __FILE__)
	load user_vagrantfile if File.exists?(user_vagrantfile)
	EOF
end