Class: Vagrant::LXC::Driver
- Inherits:
-
Object
- Object
- Vagrant::LXC::Driver
show all
- Defined in:
- lib/vagrant-lxc/driver.rb,
lib/vagrant-lxc/driver/cli.rb
Defined Under Namespace
Classes: CLI, ContainerNotFound
Constant Summary
collapse
- CONTAINERS_PATH =
Root folder where container configs are stored
'/var/lib/lxc'
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(container_name, sudo_wrapper, cli = nil) ⇒ Driver
Returns a new instance of Driver.
20
21
22
23
24
25
26
|
# File 'lib/vagrant-lxc/driver.rb', line 20
def initialize(container_name, sudo_wrapper, cli = nil)
@container_name = container_name
@sudo_wrapper = sudo_wrapper
@cli = cli || CLI.new(sudo_wrapper, container_name)
@logger = Log4r::Logger.new("vagrant::provider::lxc::driver")
@customizations = []
end
|
Instance Attribute Details
#container_name ⇒ Object
Returns the value of attribute container_name.
17
18
19
|
# File 'lib/vagrant-lxc/driver.rb', line 17
def container_name
@container_name
end
|
#customizations ⇒ Object
Returns the value of attribute customizations.
17
18
19
|
# File 'lib/vagrant-lxc/driver.rb', line 17
def customizations
@customizations
end
|
Instance Method Details
#attach(*command) ⇒ Object
93
94
95
|
# File 'lib/vagrant-lxc/driver.rb', line 93
def attach(*command)
@cli.attach(*command)
end
|
#base_path ⇒ Object
32
33
34
|
# File 'lib/vagrant-lxc/driver.rb', line 32
def base_path
Pathname.new("#{CONTAINERS_PATH}/#{@container_name}")
end
|
#compress_rootfs ⇒ Object
TODO: This needs to be reviewed and specs needs to be written
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/vagrant-lxc/driver.rb', line 102
def compress_rootfs
rootfs_dirname = File.dirname rootfs_path
basename = rootfs_path.to_s.gsub(/^#{Regexp.escape rootfs_dirname}\//, '')
target_path = "#{Dir.mktmpdir}/rootfs.tar.gz"
Dir.chdir base_path do
@logger.info "Compressing '#{rootfs_path}' rootfs to #{target_path}"
@sudo_wrapper.run('rm', '-f', 'rootfs.tar.gz')
@sudo_wrapper.run('tar', '--numeric-owner', '-czf', target_path, "#{basename}/*")
@logger.info "Changing rootfs tarbal owner"
@sudo_wrapper.run('chown', "#{ENV['USER']}:#{ENV['USER']}", target_path)
end
target_path
end
|
#create(name, template_path, config_file, template_options = {}) ⇒ Object
44
45
46
47
48
49
50
51
|
# File 'lib/vagrant-lxc/driver.rb', line 44
def create(name, template_path, config_file, template_options = {})
@cli.name = @container_name = name
import_template(template_path) do |template_name|
@logger.debug "Creating container..."
@cli.create template_name, config_file, template_options
end
end
|
#destroy ⇒ Object
89
90
91
|
# File 'lib/vagrant-lxc/driver.rb', line 89
def destroy
@cli.destroy
end
|
#forced_halt ⇒ Object
82
83
84
85
86
87
|
# File 'lib/vagrant-lxc/driver.rb', line 82
def forced_halt
@logger.info('Shutting down container...')
@cli.transition_to(:stopped) { |c| c.shutdown }
rescue CLI::TargetStateNotReached
@cli.transition_to(:stopped) { |c| c.stop }
end
|
#mac_address ⇒ Object
40
41
42
|
# File 'lib/vagrant-lxc/driver.rb', line 40
def mac_address
@mac_address ||= base_path.join('config').read.match(/^lxc\.network\.hwaddr\s+=\s+(.+)$/)[1]
end
|
#prune_customizations ⇒ Object
126
127
128
129
130
|
# File 'lib/vagrant-lxc/driver.rb', line 126
def prune_customizations
@logger.debug 'Prunning vagrant-lxc customizations'
@sudo_wrapper.su_c("sed -e '/^# VAGRANT-BEGIN/,/^# VAGRANT-END/ d' -ibak #{base_path.join('config')}")
end
|
#rootfs_path ⇒ Object
36
37
38
|
# File 'lib/vagrant-lxc/driver.rb', line 36
def rootfs_path
Pathname.new(base_path.join('config').read.match(/^lxc\.rootfs\s+=\s+(.+)$/)[1])
end
|
#share_folders(folders) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/vagrant-lxc/driver.rb', line 53
def share_folders(folders)
folders.each do |folder|
guestpath = rootfs_path.join(folder[:guestpath].gsub(/^\//, ''))
unless guestpath.directory?
begin
@logger.debug("Guest path doesn't exist, creating: #{guestpath}")
@sudo_wrapper.run('mkdir', '-p', guestpath.to_s)
rescue Errno::EACCES
raise Vagrant::Errors::SharedFolderCreateFailed, :path => guestpath.to_s
end
end
@customizations << ['mount.entry', "#{folder[:hostpath]} #{guestpath} none bind 0 0"]
end
end
|
#start(customizations) ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/vagrant-lxc/driver.rb', line 69
def start(customizations)
@logger.info('Starting container...')
if ENV['LXC_START_LOG_FILE']
= ['-o', ENV['LXC_START_LOG_FILE'], '-l', 'DEBUG']
end
prune_customizations
write_customizations(customizations + @customizations)
@cli.transition_to(:running) { |c| c.start() }
end
|
#state ⇒ Object
120
121
122
123
124
|
# File 'lib/vagrant-lxc/driver.rb', line 120
def state
if @container_name
@cli.state
end
end
|
#validate! ⇒ Object
28
29
30
|
# File 'lib/vagrant-lxc/driver.rb', line 28
def validate!
raise ContainerNotFound if @container_name && ! @cli.list.include?(@container_name)
end
|
#version ⇒ Object
97
98
99
|
# File 'lib/vagrant-lxc/driver.rb', line 97
def version
@version ||= @cli.version
end
|