Class: Mist::LXCContainer
- Inherits:
-
Object
- Object
- Mist::LXCContainer
- Defined in:
- lib/mist/lxc_container.rb
Instance Attribute Summary collapse
-
#distro ⇒ Object
readonly
Returns the value of attribute distro.
-
#ips ⇒ Object
readonly
Returns the value of attribute ips.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#release ⇒ Object
readonly
Returns the value of attribute release.
Instance Method Summary collapse
- #create(config, startup_script) ⇒ Object
- #destroy ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(name, distro = nil, release = nil) ⇒ LXCContainer
constructor
A new instance of LXCContainer.
Constructor Details
#initialize(name, distro = nil, release = nil) ⇒ LXCContainer
Returns a new instance of LXCContainer.
21 22 23 24 25 26 27 28 |
# File 'lib/mist/lxc_container.rb', line 21 def initialize(name, distro = nil, release = nil) @name = name @distro = distro @release = release @container = LXC::Container.new(@name) @ips = [] end |
Instance Attribute Details
#distro ⇒ Object (readonly)
Returns the value of attribute distro.
19 20 21 |
# File 'lib/mist/lxc_container.rb', line 19 def distro @distro end |
#ips ⇒ Object (readonly)
Returns the value of attribute ips.
19 20 21 |
# File 'lib/mist/lxc_container.rb', line 19 def ips @ips end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
19 20 21 |
# File 'lib/mist/lxc_container.rb', line 19 def name @name end |
#release ⇒ Object (readonly)
Returns the value of attribute release.
19 20 21 |
# File 'lib/mist/lxc_container.rb', line 19 def release @release end |
Instance Method Details
#create(config, startup_script) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/mist/lxc_container.rb', line 34 def create(config, startup_script) raise "Container #{@name} already exists!" if exists? # Find the template for this container; if one does not exist on the host, # create it. template = LXCTemplate.new(distro, release) unless template.exists? Mist.logger.info "Template for #{distro}-#{release} does not exist" template.create(config) end # Fast-clone a new container from the template Mist.logger.info 'Cloning template...' container = template.clone(@name) begin # Start the container Mist.logger.info "Starting #{@name}" container.start container.wait(:running, 30) # Wait for the network to start Mist.logger.info 'Waiting for network...' @ips = [] start = Time.now loop do @ips = container.ip_addresses break unless @ips.empty? sleep 1 raise 'timed out waiting for network' \ if (Time.now - start) >= 30 end # Give the container a few more seconds to allow SSH to start socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0) sockaddr = Socket.sockaddr_in(22, @ips.first) start = Time.now loop do begin if socket.connect_nonblock(sockaddr) == 0 Mist.logger.info 'SSH started' socket.close break else sleep 0.5 end raise 'timed out waiting for SSH' \ if (Time.now - start) >= 30 rescue Errno::ECONNREFUSED, Errno::EWOULDBLOCK, Errno::EINPROGRESS # Ignored end end # Find the rootfs on the host so that we can copy in the startup-script rootfs = container.config_item('lxc.rootfs') match = rootfs.match(/\Aoverlayfs:(.*):(.*)\Z/) rootfs = match[2] if match Mist.logger.debug "rootfs=#{rootfs}" internal_path = File.join('/', 'var', 'lib', 'mist') internal_file = File.join(internal_path, 'startup') external_path = File.join(rootfs, internal_path) external_file = File.join(external_path, 'startup') begin ::FileUtils.mkdir_p(external_path) ::FileUtils.cp startup_script, external_file rescue StandardError => ex Mist.logger.error "failed to copy startup script: #{ex}" raise 'could not copy startup script to container' end # Run the startup script Mist.logger.debug 'running startup script' container.attach(wait: true, stdin: File.open('/dev/null')) do LXC.run_command("/bin/bash #{internal_file}") end rescue StandardError => ex Mist.logger.error "Failed to start container #{@name}: #{ex}" # Attempt to clean up container.stop if container.running? container.destroy if container.defined? raise end @container = container end |
#destroy ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/mist/lxc_container.rb', line 130 def destroy raise "Container #{@name} does not exist!" unless exists? begin @container.stop @container.wait(:stopped, 60) @container.destroy @container = nil @ips = [] rescue StandardError => ex Mist.logger.error "Failed to destroy container #{@name}: #{ex}" raise end end |
#exists? ⇒ Boolean
30 31 32 |
# File 'lib/mist/lxc_container.rb', line 30 def exists? @container.defined? end |