Class: DevLXC::Server
- Inherits:
-
Object
- Object
- DevLXC::Server
- Defined in:
- lib/dev-lxc/server.rb
Instance Attribute Summary collapse
-
#container ⇒ Object
readonly
Returns the value of attribute container.
Instance Method Summary collapse
- #assign_static_ip_address(hwaddr) ⇒ Object
- #destroy ⇒ Object
-
#initialize(name, ipaddress, additional_fqdn, mounts, ssh_keys) ⇒ Server
constructor
A new instance of Server.
- #install_package(package_path) ⇒ Object
- #name ⇒ Object
- #release_lingering_dhcp_ip_addresses(hwaddr) ⇒ Object
- #remove_static_ip_address(hwaddr) ⇒ Object
- #run_command(command, output_file = nil) ⇒ Object
- #shutdown ⇒ Object
- #snapshot(comment = nil) ⇒ Object
- #snapshot_destroy(snapname = nil) ⇒ Object
- #snapshot_list ⇒ Object
- #snapshot_restore(snapname = nil) ⇒ Object
- #start ⇒ Object
- #status ⇒ Object
Constructor Details
#initialize(name, ipaddress, additional_fqdn, mounts, ssh_keys) ⇒ Server
Returns a new instance of Server.
8 9 10 11 12 13 14 |
# File 'lib/dev-lxc/server.rb', line 8 def initialize(name, ipaddress, additional_fqdn, mounts, ssh_keys) @container = DevLXC::Container.new(name) @ipaddress = ipaddress @additional_fqdn = additional_fqdn @mounts = mounts @ssh_keys = ssh_keys end |
Instance Attribute Details
#container ⇒ Object (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
176 177 178 179 180 181 |
# File 'lib/dev-lxc/server.rb', line 176 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 |
#destroy ⇒ Object
146 147 148 149 150 151 152 153 154 |
# File 'lib/dev-lxc/server.rb', line 146 def destroy if @container.defined? hwaddr = @container.config_item("lxc.network.0.hwaddr") @container.snapshot_list.each { |snapshot| @container.snapshot_destroy(snapshot.first) } end @container.destroy remove_static_ip_address(hwaddr) release_lingering_dhcp_ip_addresses(hwaddr) end |
#install_package(package_path) ⇒ Object
34 35 36 |
# File 'lib/dev-lxc/server.rb', line 34 def install_package(package_path) @container.install_package(package_path) end |
#name ⇒ Object
16 17 18 |
# File 'lib/dev-lxc/server.rb', line 16 def name @container.name end |
#release_lingering_dhcp_ip_addresses(hwaddr) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/dev-lxc/server.rb', line 156 def release_lingering_dhcp_ip_addresses(hwaddr) dhcp_leases = IO.readlines('/var/lib/misc/dnsmasq.lxcbr0.leases') dhcp_leases.each 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 puts "Releasing lingering DHCP lease: #{dhcp_lease}" system("dhcp_release lxcbr0 #{ip_addr} #{mac_addr}") end end end |
#remove_static_ip_address(hwaddr) ⇒ Object
183 184 185 186 187 188 189 190 191 |
# File 'lib/dev-lxc/server.rb', line 183 def remove_static_ip_address(hwaddr) if @ipaddress DevLXC.search_file_delete_line("/etc/lxc/dhcp-hosts.conf", /,#{@ipaddress}$/) end unless hwaddr.nil? DevLXC.search_file_delete_line("/etc/lxc/dhcp-hosts.conf", /^#{hwaddr}/) end DevLXC.reload_dnsmasq end |
#run_command(command, output_file = nil) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/dev-lxc/server.rb', line 24 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 |
#shutdown ⇒ Object
49 50 51 52 53 54 |
# File 'lib/dev-lxc/server.rb', line 49 def shutdown hwaddr = @container.config_item("lxc.network.0.hwaddr") if @container.defined? @container.shutdown remove_static_ip_address(hwaddr) release_lingering_dhcp_ip_addresses(hwaddr) 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_list ⇒ Object
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 |
#start ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/dev-lxc/server.rb', line 38 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) puts end |
#status ⇒ Object
20 21 22 |
# File 'lib/dev-lxc/server.rb', line 20 def status @container.status end |