Class: VagrantPlugins::ProviderZone::Driver

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(machine) ⇒ Driver

Returns a new instance of Driver.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/vagrant-zone/driver.rb', line 14

def initialize(machine)
	@logger = Log4r::Logger.new("vagrant_zone::driver")
	@machine = machine
	@executor = Executor::Exec.new

	if Process.uid == 0
		@pfexec = ''
	else
		sudo = system('sudo -v')
		if sudo
			@pfexec = 'sudo'
		else
			@pfexec = 'pfexec'
		end
	end
end

Instance Attribute Details

#executorObject

Returns the value of attribute executor.



12
13
14
# File 'lib/vagrant-zone/driver.rb', line 12

def executor
  @executor
end

Instance Method Details

#boot(machine, ui) ⇒ Object



70
71
72
73
# File 'lib/vagrant-zone/driver.rb', line 70

def boot(machine, ui)
	name = @machine.name
	execute(false, "#{@pfexec} zoneadm -z #{name} boot")
end

#create_dataset(machine, ui) ⇒ Object



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

def create_dataset(machine, ui)
	config  = machine.provider_config
	dataset = config.zonepath.delete_prefix("/").to_s + "/data"
	execute(false, "#{@pfexec} zfs create -o zoned=on -p #{dataset}")

#				machine.config.vm.disks.each do |_type, opts|
#					if _type.to_s = "disk"
#						name = opts[:name] if !opts[:name].nil?
#						size = Vagrant::Util::Numeric.string_to_bytes(opts[:size].to_s) if !opts[:size].nil?
#						quota = "-o quota=" + size.to_s if size
#
#						execute(false, "#{@pfexec} zfs create #{quota} #{dataset}/#{name}")
#					end
#				end
end

#create_vnic(machine, ui) ⇒ Object



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

def create_vnic(machine, ui)
	machine.config.vm.networks.each do |_type, opts|
		if _type.to_s == "public_network"
			link = opts[:bridge]
			mac  = 'auto'
			if !opts[:mac].nil?
				mac  = opts[:mac]
			end
			execute(false, "#{@pfexec} dladm create-vnic -l #{link} -m #{mac} #{machine.name}0")
		end
	end
end

#delete_dataset(machine, ui) ⇒ Object



104
105
106
107
# File 'lib/vagrant-zone/driver.rb', line 104

def delete_dataset(machine, ui)
	config = machine.provider_config
	execute(false, "#{@pfexec} zfs destroy -r #{config.zonepath.delete_prefix("/")}/data")
end

#destroy(machine, id) ⇒ Object



216
217
218
219
220
221
222
223
# File 'lib/vagrant-zone/driver.rb', line 216

def destroy(machine, id)
	name = @machine.name
	execute(false, "#{@pfexec} zoneadm -z #{name} halt")
	execute(false, "#{@pfexec} zoneadm -z #{name} uninstall -F")
	execute(false, "#{@pfexec} zonecfg -z #{name} delete -F")
	execute(false, "#{@pfexec} dladm delete-vnic #{name}0")

end

#execute(*cmd, **opts, &block) ⇒ Object



46
47
48
# File 'lib/vagrant-zone/driver.rb', line 46

def execute(*cmd, **opts, &block)
	@executor.execute(*cmd, **opts, &block)
end

#get_ip_address(machine) ⇒ Object



50
51
52
53
54
55
# File 'lib/vagrant-zone/driver.rb', line 50

def get_ip_address(machine)
	name = @machine.name
	ip   = execute(false, "#{@pfexec} zonecfg -z #{name} info net | sed -n 's|property: (name=ips,value=\"\\(.*\\)/.*\")|\\1|p'")
	return nil if ip.length == 0
	return ip.gsub /\t/, ''
end

#halt(machine, ui) ⇒ Object



211
212
213
214
# File 'lib/vagrant-zone/driver.rb', line 211

def halt(machine, ui)
	name = @machine.name
	execute(false, "#{@pfexec} zoneadm -z #{name} halt")
end

#install(machine, ui) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/vagrant-zone/driver.rb', line 57

def install(machine, ui)
                             config = machine.provider_config
	box  = @machine.data_dir.to_s + '/' + @machine.config.vm.box
	name = @machine.name

	if config.brand == 'lx'
		execute(false, "#{@pfexec} zoneadm -z #{name} install -s #{box}")
	end
	if config.brand == 'bhyve'
		execute(false, "#{@pfexec} zoneadm -z #{name} install")
	end
end

#setup(machine, ui) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
# File 'lib/vagrant-zone/driver.rb', line 185

def setup(machine, ui)
	zlogin(machine, %('echo nameserver 1.1.1.1 >> /etc/resolv.conf'))
	if not user_exists?(machine)
		zlogin(machine, "useradd -m -s /bin/bash -U vagrant")
	end
	zlogin(machine, %('echo "vagrant ALL=(ALL:ALL) NOPASSWD:ALL" > /etc/sudoers.d/vagrant'))
	zlogin(machine, "mkdir -p /home/vagrant/.ssh")
	zlogin(machine, %('echo "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key" > /home/vagrant/.ssh/authorized_keys'))
	zlogin(machine, "chown -R vagrant:vagrant /home/vagrant/.ssh")
	zlogin(machine, "chmod 600 /home/vagrant/.ssh/authorized_keys")
end

#state(machine) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vagrant-zone/driver.rb', line 31

def state(machine)
	uuid = machine.id
	name = machine.name
	vm_state = execute(false, "#{@pfexec} zoneadm -z #{name} list -p | awk -F: '{ print $3 }'")
	if vm_state == 'running'
		:running
	elsif vm_state == 'configured'
		:preparing
	elsif vm_state == 'installed'
		:stopped
	else
		:not_created
	end
end

#user_exists?(machine, user = 'vagrant') ⇒ Boolean

Returns:

  • (Boolean)


202
203
204
205
206
207
208
209
# File 'lib/vagrant-zone/driver.rb', line 202

def user_exists?(machine, user = 'vagrant')
	name = @machine.name
	ret  = execute(true, "#{@pfexec} zlogin #{name} id -u #{user}")
	if ret == 0
		return true
	end
	return false
end

#zlogin(machine, cmd) ⇒ Object



197
198
199
200
# File 'lib/vagrant-zone/driver.rb', line 197

def zlogin(machine, cmd)
	name = @machine.name
	execute(false, "#{@pfexec} zlogin #{name} #{cmd}")
end

#zonecfg(machine, ui) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/vagrant-zone/driver.rb', line 109

def zonecfg(machine, ui)
	config = machine.provider_config
	machine.config.vm.networks.each do |_type, opts|
		if _type.to_s == "public_network"
			@ip        = opts[:ip].to_s
			@network   = NetAddr.parse_net(opts[:ip].to_s + '/' + opts[:netmask].to_s)
			@defrouter = opts[:gateway]
		end
	end

	allowed_address  = @ip + @network.netmask.to_s
	lofs_current_dir = Dir.pwd

	attr = ''
	if config.brand == 'lx'
		attr = %{
			add attr
				set name=kernel-version
				set type=string
				set value=#{config.kernel}
			end
		}
	end
	if config.brand == 'bhyve'
		attr = %{
			add device
				set match=/dev/zvol/rdsk#{config.zonepath}
			end
			add attr
				set name=bootrom
				set type=string
				set value=BHYVE_RELEASE
			end
			add attr
				set name=bootdisk
				set type=string
				set value=#{config.zonepath}
			end
		}
	end

	data = %{
		create
		set zonepath=#{config.zonepath}
		set brand=#{config.brand}
		set autoboot=false
		add net
			set physical=#{machine.name}0
			set global-nic=auto
			add property (name=gateway,value="#{@defrouter.to_s}")
			add property (name=ips,value="#{allowed_address}")
			add property (name=primary,value="true")
		end
		add capped-memory
			set physical=#{config.memory}
			set swap=#{config.memory}
			set locked=#{config.memory}
		end
		#{attr}
		add fs
			set dir=/vagrant
			set special=#{lofs_current_dir}
			set type=lofs
		end
		add dataset
			set name=#{config.zonepath.delete_prefix("/")}/data
		end
		set max-lwps=2000
		exit
	}
	File.open('zone_config', 'w') do |f|
		f.puts data
	end
	execute(false, "cat zone_config | #{@pfexec} zonecfg -z #{machine.name}")
end