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.
22
23
24
25
26
27
28
|
# File 'lib/vagrant-lxc/driver.rb', line 22
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.
19
20
21
|
# File 'lib/vagrant-lxc/driver.rb', line 19
def container_name
@container_name
end
|
#customizations ⇒ Object
Returns the value of attribute customizations.
19
20
21
|
# File 'lib/vagrant-lxc/driver.rb', line 19
def customizations
@customizations
end
|
Instance Method Details
#attach(*command) ⇒ Object
100
101
102
|
# File 'lib/vagrant-lxc/driver.rb', line 100
def attach(*command)
@cli.attach(*command)
end
|
#base_path ⇒ Object
34
35
36
|
# File 'lib/vagrant-lxc/driver.rb', line 34
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/vagrant-lxc/driver.rb', line 109
def compress_rootfs
target_path = "#{Dir.mktmpdir}/rootfs.tar.gz"
@logger.info "Compressing '#{rootfs_path}' rootfs to #{target_path}"
cmds = [
"cd #{base_path}",
"rm -f rootfs.tar.gz",
"tar --numeric-owner -czf #{target_path} -C #{rootfs_path} './.'"
]
@sudo_wrapper.su_c(cmds.join(' && '))
@logger.info "Changing rootfs tarball owner"
user_details = Etc.getpwnam(Etc.getlogin)
@sudo_wrapper.run('chown', "#{user_details.uid}:#{user_details.gid}", target_path)
target_path
end
|
#config_string ⇒ Object
46
47
48
|
# File 'lib/vagrant-lxc/driver.rb', line 46
def config_string
@sudo_wrapper.run('cat', base_path.join('config').to_s)
end
|
#create(name, template_path, config_file, template_options = {}) ⇒ Object
50
51
52
53
54
55
56
57
|
# File 'lib/vagrant-lxc/driver.rb', line 50
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
96
97
98
|
# File 'lib/vagrant-lxc/driver.rb', line 96
def destroy
@cli.destroy
end
|
#forced_halt ⇒ Object
88
89
90
91
92
93
94
|
# File 'lib/vagrant-lxc/driver.rb', line 88
def forced_halt
@logger.info('Shutting down container...')
@cli.transition_to(:stopped) { |c| c.shutdown }
rescue CLI::TargetStateNotReached, CLI::ShutdownNotSupported
@cli.transition_to(:stopped) { |c| c.stop }
end
|
#mac_address ⇒ Object
42
43
44
|
# File 'lib/vagrant-lxc/driver.rb', line 42
def mac_address
@mac_address ||= config_string.match(/^lxc\.network\.hwaddr\s*+=\s*+(.+)$/)[1]
end
|
#prune_customizations ⇒ Object
139
140
141
142
143
|
# File 'lib/vagrant-lxc/driver.rb', line 139
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
38
39
40
|
# File 'lib/vagrant-lxc/driver.rb', line 38
def rootfs_path
Pathname.new(config_string.match(/^lxc\.rootfs\s+=\s+(.+)$/)[1])
end
|
#share_folders(folders) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/vagrant-lxc/driver.rb', line 59
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
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/vagrant-lxc/driver.rb', line 75
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.start()
end
|
#state ⇒ Object
133
134
135
136
137
|
# File 'lib/vagrant-lxc/driver.rb', line 133
def state
if @container_name
@cli.state
end
end
|
#validate! ⇒ Object
30
31
32
|
# File 'lib/vagrant-lxc/driver.rb', line 30
def validate!
raise ContainerNotFound if @container_name && ! @cli.list.include?(@container_name)
end
|
#version ⇒ Object
104
105
106
|
# File 'lib/vagrant-lxc/driver.rb', line 104
def version
@version ||= @cli.version
end
|