Class: Vagrant::LXC::Driver

Inherits:
Object
  • Object
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_nameObject (readonly)

Returns the value of attribute container_name.



19
20
21
# File 'lib/vagrant-lxc/driver.rb', line 19

def container_name
  @container_name
end

#customizationsObject (readonly)

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



95
96
97
# File 'lib/vagrant-lxc/driver.rb', line 95

def attach(*command)
  @cli.attach(*command)
end

#base_pathObject



34
35
36
# File 'lib/vagrant-lxc/driver.rb', line 34

def base_path
  Pathname.new("#{CONTAINERS_PATH}/#{@container_name}")
end

#compress_rootfsObject

TODO: This needs to be reviewed and specs needs to be written



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/vagrant-lxc/driver.rb', line 104

def compress_rootfs
  # TODO: Pass in tmpdir so we can clean up from outside
  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, 'rootfs')

    @logger.info "Changing rootfs tarball owner"
    user_details = Etc.getpwnam(Etc.getlogin)
    @sudo_wrapper.run('chown', "#{user_details.uid}:#{user_details.gid}", target_path)
  end

  target_path
end

#create(name, template_path, config_file, template_options = {}) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/vagrant-lxc/driver.rb', line 46

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

#destroyObject



91
92
93
# File 'lib/vagrant-lxc/driver.rb', line 91

def destroy
  @cli.destroy
end

#forced_haltObject



84
85
86
87
88
89
# File 'lib/vagrant-lxc/driver.rb', line 84

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_addressObject



42
43
44
# File 'lib/vagrant-lxc/driver.rb', line 42

def mac_address
  @mac_address ||= base_path.join('config').read.match(/^lxc\.network\.hwaddr\s+=\s+(.+)$/)[1]
end

#prune_customizationsObject



127
128
129
130
131
# File 'lib/vagrant-lxc/driver.rb', line 127

def prune_customizations
  # Use sed to just strip out the block of code which was inserted by Vagrant
  @logger.debug 'Prunning vagrant-lxc customizations'
  @sudo_wrapper.su_c("sed -e '/^# VAGRANT-BEGIN/,/^# VAGRANT-END/ d' -ibak #{base_path.join('config')}")
end

#rootfs_pathObject



38
39
40
# File 'lib/vagrant-lxc/driver.rb', line 38

def rootfs_path
  Pathname.new(base_path.join('config').read.match(/^lxc\.rootfs\s+=\s+(.+)$/)[1])
end

#share_folders(folders) ⇒ Object



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

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



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/vagrant-lxc/driver.rb', line 71

def start(customizations)
  @logger.info('Starting container...')

  if ENV['LXC_START_LOG_FILE']
    extra = ['-o', ENV['LXC_START_LOG_FILE'], '-l', 'DEBUG']
  end

  prune_customizations
  write_customizations(customizations + @customizations)

  @cli.start(extra)
end

#stateObject



121
122
123
124
125
# File 'lib/vagrant-lxc/driver.rb', line 121

def state
  if @container_name
    @cli.state
  end
end

#validate!Object

Raises:



30
31
32
# File 'lib/vagrant-lxc/driver.rb', line 30

def validate!
  raise ContainerNotFound if @container_name && ! @cli.list.include?(@container_name)
end

#versionObject



99
100
101
# File 'lib/vagrant-lxc/driver.rb', line 99

def version
  @version ||= @cli.version
end