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, 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

#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

#deregister_from_dhcp(hwaddr) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'lib/dev-lxc/server.rb', line 151

def deregister_from_dhcp(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

#destroyObject



142
143
144
145
146
147
148
149
# File 'lib/dev-lxc/server.rb', line 142

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
  deregister_from_dhcp(hwaddr)
end

#install_package(package_path) ⇒ Object



33
34
35
# File 'lib/dev-lxc/server.rb', line 33

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

#nameObject



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

def name
  @container.name
end

#run_command(command) ⇒ Object



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

def run_command(command)
  if @container.running?
    puts "Running '#{command}' in '#{@container.name}'"
    @container.run_command(command)
  else
    puts "'#{@container.name}' is not running"
  end
end

#snapshot(comment = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/dev-lxc/server.rb', line 52

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



70
71
72
73
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
# File 'lib/dev-lxc/server.rb', line 70

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



103
104
105
106
107
108
109
110
111
112
# File 'lib/dev-lxc/server.rb', line 103

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



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

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



37
38
39
40
41
42
43
44
# File 'lib/dev-lxc/server.rb', line 37

def start
  hwaddr = @container.config_item("lxc.network.0.hwaddr")
  DevLXC.assign_ip_address(@ipaddress, @container.name, hwaddr) if @ipaddress
  @container.sync_mounts(@mounts)
  @container.start
  @container.sync_ssh_keys(@ssh_keys)
  puts
end

#statusObject



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

def status
  @container.status
end

#stopObject



46
47
48
49
50
# File 'lib/dev-lxc/server.rb', line 46

def stop
  hwaddr = @container.config_item("lxc.network.0.hwaddr") if @container.defined?
  @container.stop
  deregister_from_dhcp(hwaddr)
end