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



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

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



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
  # TODO: Pass in tmpdir so we can clean up from outside
  target_path    = "#{Dir.mktmpdir}/rootfs.tar.gz"

  @logger.info "Compressing '#{rootfs_path}' rootfs to #{target_path}"
  # "vagrant package" will copy the existing lxc-template in the new box file
  # To keep this function backwards compatible with existing boxes, the path
  # included in the tarball needs to have the same amount of path components (2)
  # that will be stripped before extraction, hence the './.'
  # TODO: This should be reviewed before 1.0
  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_stringObject



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

#destroyObject



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

def destroy
  @cli.destroy
end

#forced_haltObject



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 }
# REFACTOR: Do not use exception to control the flow
rescue CLI::TargetStateNotReached, CLI::ShutdownNotSupported
  @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 ||= config_string.match(/^lxc\.network\.hwaddr\s*+=\s*+(.+)$/)[1]
end

#prune_customizationsObject



139
140
141
142
143
# File 'lib/vagrant-lxc/driver.rb', line 139

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(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']
    extra = ['-o', ENV['LXC_START_LOG_FILE'], '-l', 'DEBUG']
  end

  prune_customizations
  write_customizations(customizations + @customizations)

  @cli.start(extra)
end

#stateObject



133
134
135
136
137
# File 'lib/vagrant-lxc/driver.rb', line 133

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



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

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