Class: Lxc::Ephemeral
- Inherits:
-
Object
- Object
- Lxc::Ephemeral
- Includes:
- Helpers, Helpers::Copies, Helpers::Options
- Defined in:
- lib/elecksee/ephemeral.rb
Overview
Create ephemeral containers
Constant Summary
Constants included from Helpers::Copies
Helpers::Copies::HOSTNAME_FILES, Helpers::Copies::NAME_FILES
Instance Attribute Summary collapse
-
#cli ⇒ TrueClass, FalseClass
readonly
Enable CLI output.
-
#ephemeral_binds ⇒ Array<Storage::VirtualDevice]
readonly
Array<Storage::VirtualDevice].
- #ephemeral_device ⇒ Storage::OverlayDirectory, Storage::VirtualDevice readonly
- #ephemeral_overlay ⇒ Storage::OverlayMount readonly
-
#hostname ⇒ String
readonly
Hostname of container.
-
#lxc ⇒ Lxc
readonly
Instance of ephemeral.
-
#name ⇒ String
readonly
Name of container.
-
#path ⇒ String
readonly
Path to container.
Instance Method Summary collapse
-
#cleanup ⇒ TrueClass, FalseClass
Stop container and cleanup ephemeral items.
-
#cli_output ⇒ TrueClass, FalseClass
Write output to CLI.
-
#create! ⇒ TrueClass
Create the ephemeral container.
-
#initialize(args = {}) ⇒ Ephemeral
constructor
Create new instance.
-
#register_traps ⇒ TrueClass
Trap signals to force cleanup.
-
#shell_wrapper ⇒ Tempfile
Bash based wrapper script to start ephemeral and clean up ephemeral resources on exit.
-
#start!(*args) ⇒ TrueClass
Start the ephemeral container.
-
#start_action ⇒ TrueClass
Start the ephemeral container.
Methods included from Helpers::Copies
#apply_custom_networking, #el_platform?, #update_naming, #update_net_hwaddr, #update_rootfs, #write_file
Methods included from Helpers::Options
Methods included from Helpers
#child_process_command, #detect_home, #log, #mixlib_shellout_command, #run_command, #sudo
Constructor Details
#initialize(args = {}) ⇒ Ephemeral
Create new instance
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/elecksee/ephemeral.rb', line 52 def initialize(args={}) configure!(args) @cli = args[:cli] @path = command("mktemp -d -p #{lxc_dir} #{original}-XXXXXXXXXXXX", :sudo => true).stdout.strip command("chmod 0755 #{@path}", :sudo => true) @name = File.basename(@path) @hostname = @name.gsub(%r{[^A-Za-z0-9\-]}, '') @ephemeral_binds = [] @lxc = nil end |
Instance Attribute Details
#cli ⇒ TrueClass, FalseClass (readonly)
Returns enable CLI output.
34 35 36 |
# File 'lib/elecksee/ephemeral.rb', line 34 def cli @cli end |
#ephemeral_binds ⇒ Array<Storage::VirtualDevice] (readonly)
Returns Array<Storage::VirtualDevice].
46 47 48 |
# File 'lib/elecksee/ephemeral.rb', line 46 def ephemeral_binds @ephemeral_binds end |
#ephemeral_device ⇒ Storage::OverlayDirectory, Storage::VirtualDevice (readonly)
42 43 44 |
# File 'lib/elecksee/ephemeral.rb', line 42 def ephemeral_device @ephemeral_device end |
#ephemeral_overlay ⇒ Storage::OverlayMount (readonly)
44 45 46 |
# File 'lib/elecksee/ephemeral.rb', line 44 def @ephemeral_overlay end |
#hostname ⇒ String (readonly)
Returns hostname of container.
36 37 38 |
# File 'lib/elecksee/ephemeral.rb', line 36 def hostname @hostname end |
#lxc ⇒ Lxc (readonly)
Returns instance of ephemeral.
40 41 42 |
# File 'lib/elecksee/ephemeral.rb', line 40 def lxc @lxc end |
#name ⇒ String (readonly)
Returns name of container.
32 33 34 |
# File 'lib/elecksee/ephemeral.rb', line 32 def name @name end |
#path ⇒ String (readonly)
Returns path to container.
38 39 40 |
# File 'lib/elecksee/ephemeral.rb', line 38 def path @path end |
Instance Method Details
#cleanup ⇒ TrueClass, FalseClass
Stop container and cleanup ephemeral items
210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/elecksee/ephemeral.rb', line 210 def cleanup lxc.stop if lxc.running? @ephemeral_overlay.unmount @ephemeral_binds.map(&:destroy) @ephemeral_device.destroy if(lxc.path.to_path.split('/').size > 1) command("rm -rf #{lxc.path.to_path}", :sudo => true) true else $stderr.puts "This path seems bad and I won't remove it: #{lxc.path.to_path}" false end end |
#cli_output ⇒ TrueClass, FalseClass
Write output to CLI
76 77 78 79 80 81 82 83 84 |
# File 'lib/elecksee/ephemeral.rb', line 76 def cli_output if(cli) puts "New ephemeral container started. (#{name})" puts " - Connect using: sudo ssh -i #{ssh_key} root@#{lxc.container_ip(10)}" true else false end end |
#create! ⇒ TrueClass
Create the ephemeral container
110 111 112 113 |
# File 'lib/elecksee/ephemeral.rb', line 110 def create! setup true end |
#register_traps ⇒ TrueClass
Trap signals to force cleanup
66 67 68 69 70 71 |
# File 'lib/elecksee/ephemeral.rb', line 66 def register_traps %w(TERM INT QUIT).each do |sig| Signal.trap(sig){ cleanup && raise } end true end |
#shell_wrapper ⇒ Tempfile
Bash based wrapper script to start ephemeral and clean up ephemeral resources on exit
149 150 151 152 153 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/elecksee/ephemeral.rb', line 149 def shell_wrapper content = ['#!/bin/bash'] content << 'scrub()' << '{' content << "umount #{.target}" ephemeral_binds.map do |bind| unless(bind.device_path == :none) if(File.file?(bind.device_path)) content << "rm #{bind.device_path}" elsif(File.directory?(bind.device_path)) content << "rm -rf #{bind.device_path}" end end unless(bind.mount_path == :none) if(File.directory?(bind.mount_path)) content << "rmdir #{bind.mount_path}" end end end case ephemeral_device when Storage::OverlayDirectory if(File.directory?(ephemeral_device.)) content << "rm -rf #{ephemeral_device.}" end when Storage::VirtualDevice if(ephemeral_device.mounted?) content << "umount #{ephemeral_device.mount_path}" end unless(ephemeral_device.device_path == :none) if(File.file?(ephemeral_device.device_path)) content << "rm #{ephemeral_device.device_path}" elsif(File.directory?(ephemeral_device.device_path)) content << "rm -rf #{ephemeral_device.device_path}" end end unless(ephemeral_device.mount_path == :none) if(File.directory?(ephemeral_device.mount_path)) content << "rmdir #{ephemeral_device.mount_path}" end end end if(lxc.path.to_path.split('/').size > 1) content << "rm -rf #{lxc.path.to_path}" end content << '}' content << 'trap scrub SIGTERM SIGINT SIGQUIT' content << "lxc-start -n #{lxc.name} -d" content << 'sleep 1' content << 'until [ `lxc-wait -n #{lxc.name} -s STOPPED -t 2` ]' content << 'do' content << 'done' content << 'scrub' tmp = Tempfile.new('elecksee') tmp.chmod(0700) tmp.puts content.join("\n") tmp.close tmp end |
#start!(*args) ⇒ TrueClass
use :fork to fork startup
Start the ephemeral container
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/elecksee/ephemeral.rb', line 120 def start!(*args) setup if(daemon) if(args.include?(:fork)) register_traps fork do start_action end elsif(args.include?(:detach)) cmd = [sudo, shell_wrapper.path].compact.map(&:strip) process = ChildProcess.build(*cmd) process.detach = true process.start shell_wrapper.delete else register_traps Process.daemon start_action end else start_action end true end |
#start_action ⇒ TrueClass
generally should not be called directly
Start the ephemeral container
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/elecksee/ephemeral.rb', line 91 def start_action begin lxc.start if(ephemeral_command) lxc.wait_for_state(:running) lxc.container_command(ephemeral_command, 0) # no retries on ephemeral commands else cli_output lxc.wait_for_state(:stopped) end ensure cleanup end true end |