Class: DevLXC::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/dev-lxc/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, ipaddress, additional_fqdn, memory_per_server, mounts, ssh_keys) ⇒ Server

Returns a new instance of Server.



8
9
10
11
12
13
14
15
# File 'lib/dev-lxc/server.rb', line 8

def initialize(name, ipaddress, additional_fqdn, memory_per_server, mounts, ssh_keys)
  @container = DevLXC::Container.new(name)
  @ipaddress = ipaddress
  @additional_fqdn = additional_fqdn
  @memory_per_server = memory_per_server
  @mounts = mounts
  @ssh_keys = ssh_keys
end

Instance Attribute Details

#containerObject (readonly)

Returns the value of attribute container.



6
7
8
# File 'lib/dev-lxc/server.rb', line 6

def container
  @container
end

Instance Method Details

#assign_static_ip_address(hwaddr) ⇒ Object



182
183
184
185
186
187
# File 'lib/dev-lxc/server.rb', line 182

def assign_static_ip_address(hwaddr)
  puts "Assigning IP address #{@ipaddress} to '#{@container.name}' container's lxc.network.hwaddr #{hwaddr}"
  DevLXC.search_file_delete_line("/etc/lxc/dhcp-hosts.conf", /(^#{hwaddr}|,#{@ipaddress}$)/)
  DevLXC.append_line_to_file("/etc/lxc/dhcp-hosts.conf", "#{hwaddr},#{@ipaddress}\n")
  DevLXC.reload_dnsmasq
end

#destroyObject



146
147
148
149
150
151
152
# File 'lib/dev-lxc/server.rb', line 146

def destroy
  return unless @container.defined?
  @container.snapshot_list.each { |snapshot| @container.snapshot_destroy(snapshot.first) }
  hwaddr = @container.config_item("lxc.network.0.hwaddr")
  @container.destroy
  remove_static_ip_address(hwaddr)
end

#install_package(package_path) ⇒ Object



35
36
37
# File 'lib/dev-lxc/server.rb', line 35

def install_package(package_path)
  @container.install_package(package_path)
end

#nameObject



17
18
19
# File 'lib/dev-lxc/server.rb', line 17

def name
  @container.name
end

#release_lingering_dhcp_ip_addresses(hwaddr) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/dev-lxc/server.rb', line 154

def release_lingering_dhcp_ip_addresses(hwaddr)
  dhcp_leases = IO.readlines('/var/lib/misc/dnsmasq.lxcbr0.leases')
  leases_to_release = dhcp_leases.map do |dhcp_lease|
    if m = dhcp_lease.match(/ #{hwaddr} (\d+\.\d+\.\d+\.\d+) /)
      mac_addr = hwaddr
      ip_addr = m[1]
    elsif m = dhcp_lease.match(/ (\w\w:\w\w:\w\w:\w\w:\w\w:\w\w) #{@ipaddress} /)
      mac_addr = m[1]
      ip_addr = @ipaddress
    elsif m = dhcp_lease.match(/ (\w\w:\w\w:\w\w:\w\w:\w\w:\w\w) (\d+\.\d+\.\d+\.\d+) #{@container.name.sub(/\.lxc$/, '')} /)
      mac_addr = m[1]
      ip_addr = m[2]
    end
    if mac_addr && ip_addr
      { dhcp_lease: dhcp_lease, mac_addr: mac_addr, ip_addr: ip_addr }
    end
  end
  leases_to_release.compact!
  unless leases_to_release.empty?
    system("systemctl stop lxc-net.service")
    leases_to_release.each do |l|
      puts "Releasing lingering DHCP lease: #{l[:dhcp_lease]}"
      DevLXC.search_file_delete_line("/var/lib/misc/dnsmasq.lxcbr0.leases", /( #{l[:mac_addr]} #{l[:ip_addr]} )/)
    end
    system("systemctl start lxc-net.service")
  end
end

#remove_static_ip_address(hwaddr = nil) ⇒ Object



189
190
191
192
193
194
# File 'lib/dev-lxc/server.rb', line 189

def remove_static_ip_address(hwaddr=nil)
  if hwaddr
    DevLXC.search_file_delete_line("/etc/lxc/dhcp-hosts.conf", /^#{hwaddr}/)
    DevLXC.reload_dnsmasq
  end
end

#run_command(command, output_file = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/dev-lxc/server.rb', line 25

def run_command(command, output_file=nil)
  if @container.running?
    puts "Running '#{command}' in '#{@container.name}'"
    puts "Saving output to #{output_file}" if output_file
    @container.run_command(command, output_file)
  else
    puts "'#{@container.name}' is not running"
  end
end

#shutdownObject



51
52
53
54
# File 'lib/dev-lxc/server.rb', line 51

def shutdown
  @container.shutdown if @container.running?
  remove_static_ip_address(@container.config_item("lxc.network.0.hwaddr")) if @container.defined?
end

#snapshot(comment = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/dev-lxc/server.rb', line 56

def snapshot(comment=nil)
  unless @container.defined?
    puts "WARNING: Skipping snapshot of '#{@container.name}' because it does not exist"
    return
  end
  if @container.running?
    puts "WARNING: Skipping snapshot of '#{@container.name}' because it is running"
    return
  end
  puts "Creating snapshot of container '#{@container.name}'"
  snapname = @container.snapshot
  unless comment.nil?
    snapshot = @container.snapshot_list.select { |sn| sn.first == snapname }
    snapshot_comment_file = snapshot.flatten[1]
    IO.write(snapshot_comment_file, comment) unless snapshot_comment_file.nil?
  end
end

#snapshot_destroy(snapname = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/dev-lxc/server.rb', line 74

def snapshot_destroy(snapname=nil)
  unless @container.defined?
    puts "Skipping container '#{@container.name}' because it does not exist"
    return
  end
  if snapname == "ALL"
    if @container.snapshot_list.empty?
      puts "Container '#{@container.name}' does not have any snapshots"
    else
      @container.snapshot_list.each do |snapshot|
        puts "Destroying snapshot '#{snapshot.first}' of container '#{@container.name}'"
        @container.snapshot_destroy(snapshot.first)
      end
    end
  elsif snapname == "LAST"
    if @container.snapshot_list.empty?
      puts "Container '#{@container.name}' does not have any snapshots"
    else
      snapname = @container.snapshot_list.last.first
      puts "Destroying snapshot '#{snapname}' of container '#{@container.name}'"
      @container.snapshot_destroy(snapname)
    end
  else
    snapshot = @container.snapshot_list.select { |sn| sn.first == snapname }
    if snapshot.flatten.empty?
      puts "Container '#{@container.name}' does not have a '#{snapname}' snapshot"
    else
      puts "Destroying snapshot '#{snapname}' of container '#{@container.name}'"
      @container.snapshot_destroy(snapname)
    end
  end
end

#snapshot_listObject



107
108
109
110
111
112
113
114
115
116
# File 'lib/dev-lxc/server.rb', line 107

def snapshot_list
  snapshots = Array.new
  return snapshots unless @container.defined?
  @container.snapshot_list.each do |snapshot|
    (snapname, snap_comment_file, snaptime) = snapshot
    snap_comment = IO.read(snap_comment_file).chomp if File.exist?(snap_comment_file)
    snapshots << [snapname, snaptime, snap_comment]
  end
  snapshots
end

#snapshot_restore(snapname = nil) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/dev-lxc/server.rb', line 118

def snapshot_restore(snapname=nil)
  unless @container.defined?
    puts "WARNING: Skipping container '#{@container.name}' because it does not exist"
    return
  end
  if @container.running?
    puts "WARNING: Skipping container '#{@container.name}' because it is running"
    return
  end
  if snapname == "LAST"
    if @container.snapshot_list.empty?
      puts "WARNING: Skipping container '#{@container.name}' because it does not have any snapshots"
    else
      snapname = @container.snapshot_list.last.first
      puts "Restoring snapshot '#{snapname}' of container '#{@container.name}'"
      @container.snapshot_restore(snapname)
    end
  else
    snapshot = @container.snapshot_list.select { |sn| sn.first == snapname }
    if snapshot.flatten.empty?
      puts "WARNING: Skipping container '#{@container.name}' because it does not have a '#{snapname}' snapshot"
    else
      puts "Restoring snapshot '#{snapname}' of container '#{@container.name}'"
      @container.snapshot_restore(snapname)
    end
  end
end

#startObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dev-lxc/server.rb', line 39

def start
  return if @container.running?
  hwaddr = @container.config_item("lxc.network.0.hwaddr")
  release_lingering_dhcp_ip_addresses(hwaddr)
  assign_static_ip_address(hwaddr) if @ipaddress
  @container.sync_mounts(@mounts)
  @container.start
  @container.sync_ssh_keys(@ssh_keys)
  @container.set_cgroup_item('memory.limit_in_bytes', @memory_per_server) if @memory_per_server
  puts
end

#statusObject



21
22
23
# File 'lib/dev-lxc/server.rb', line 21

def status
  @container.status
end