Class: DevLXC::Container

Inherits:
LXC::Container
  • Object
show all
Defined in:
lib/dev-lxc/container.rb

Instance Method Summary collapse

Instance Method Details

#destroyObject



40
41
42
43
44
# File 'lib/dev-lxc/container.rb', line 40

def destroy
  stop if running?
  puts "Destroying container '#{self.name}'"
  super if self.defined?
end

#install_package(package_path) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/dev-lxc/container.rb', line 99

def install_package(package_path)
  unless run_command("test -e #{package_path}") == 0
    puts "ERROR: File #{package_path} does not exist in container '#{self.name}'"
    exit 1
  end
  puts "Installing #{package_path} in container '#{self.name}'"
  case File.extname(package_path)
  when ".deb"
    install_command = "dpkg -D10 -i --skip-same-version #{package_path}"
  when ".rpm"
    install_command = "rpm -Uvh #{package_path}"
  end
  run_command(install_command)
end

#run_command(command) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/dev-lxc/container.rb', line 88

def run_command(command)
  unless running?
    puts "ERROR: Container '#{self.name}' must be running first"
    exit 1
  end
  attach_opts = { wait: true, env_policy: LXC::LXC_ATTACH_CLEAR_ENV, extra_env_vars: ['HOME=/root'] }
  attach(attach_opts) do
    LXC.run_command(command)
  end
end

#startObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dev-lxc/container.rb', line 13

def start
  unless self.defined?
    puts "ERROR: Container '#{self.name}' does not exist."
    exit 1
  end
  puts "Starting container '#{self.name}'"
  super
  wait(:running, 3)
  puts "Waiting for '#{self.name}' container's network"
  ips = nil
  30.times do
    ips = self.ip_addresses
    break unless ips.empty?
    sleep 1
  end
  if ips.empty?
    puts "ERROR: Container '#{self.name}' network is not available."
    exit 1
  end
end

#statusObject



3
4
5
6
7
8
9
10
11
# File 'lib/dev-lxc/container.rb', line 3

def status
  if self.defined?
    state = self.state
    ip_addresses = self.ip_addresses.join(" ") if self.state == :running
  else
    state = "not_created"
  end
  { 'name' => self.name, 'state' => state, 'ip_addresses' => ip_addresses }
end

#stopObject



34
35
36
37
38
# File 'lib/dev-lxc/container.rb', line 34

def stop
  puts "Stopping container '#{self.name}'"
  super
  wait("STOPPED", 3)
end

#sync_mounts(mounts) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dev-lxc/container.rb', line 46

def sync_mounts(mounts)
  existing_mounts = self.config_item("lxc.mount.entry")
  unless existing_mounts.nil?
    preserved_mounts = existing_mounts.delete_if { |m| m.end_with?("## dev-lxc ##") }
    self.clear_config_item('lxc.mount.entry')
    self.set_config_item("lxc.mount.entry", preserved_mounts)
  end
  unless mounts.nil?
    mounts.each do |mount|
      if ! preserved_mounts.nil? && preserved_mounts.any? { |m| m.start_with?("#{mount} ") }
        puts "Skipping mount entry #{mount}, it already exists"
        next
      else
        puts "Adding mount entry #{mount}"
        self.set_config_item("lxc.mount.entry", "#{mount} none bind,optional,create=dir 0 0     ## dev-lxc ##")
      end
    end
  end
  self.save_config
end

#sync_ssh_keys(ssh_keys) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dev-lxc/container.rb', line 67

def sync_ssh_keys(ssh_keys)
  dot_ssh_path = "/home/dev-lxc/.ssh"
  unless File.exist?("#{config_item('lxc.rootfs')}#{dot_ssh_path}/authorized_keys")
    run_command("sudo -u dev-lxc mkdir -p #{dot_ssh_path}")
    run_command("sudo -u dev-lxc chmod 700 #{dot_ssh_path}")
    run_command("sudo -u dev-lxc touch #{dot_ssh_path}/authorized_keys")
    run_command("sudo -u dev-lxc chmod 600 #{dot_ssh_path}/authorized_keys")
  end
  authorized_keys = IO.read("#{config_item('lxc.rootfs')}#{dot_ssh_path}/authorized_keys").split("\n")
  authorized_keys.delete_if { |m| m.end_with?("## dev-lxc ##") }
  unless ssh_keys.nil?
    ssh_keys.each do |ssh_key|
      puts "Adding SSH key #{ssh_key} to #{dot_ssh_path}/authorized_keys"
      authorized_keys << IO.read(ssh_key).chomp + "     ## dev-lxc ##"
    end
  end
  authorized_keys_content = String.new
  authorized_keys_content = authorized_keys.join("\n") + "\n" unless authorized_keys.empty?
  IO.write("#{config_item('lxc.rootfs')}#{dot_ssh_path}/authorized_keys", authorized_keys_content)
end