Class: Dockershell
- Inherits:
-
Object
- Object
- Dockershell
- Defined in:
- lib/dockershell.rb
Defined Under Namespace
Classes: Wordgen
Instance Method Summary collapse
-
#detached_postrun ⇒ Object
This spawns a detached process to clean up.
-
#initialize(options) ⇒ Dockershell
constructor
A new instance of Dockershell.
- #run! ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize(options) ⇒ Dockershell
Returns a new instance of Dockershell.
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/dockershell.rb', line 5 def initialize() @options = @logger = [:logger] @gempath = File.('..', File.dirname(__FILE__)) @options[:name] ||= wordgen @options[:fqdn] ||= "#{@options[:name]}.#{@options[:domain]}" @options[:profile][:volumes] ||= [] @logger.formatter = proc do |severity, datetime, progname, msg| "#{datetime} #{severity.ljust(5)} [#{@options[:name]}] #{msg}\n" end end |
Instance Method Details
#detached_postrun ⇒ Object
This spawns a detached process to clean up. This is so it doesn’t die when the parent is killed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/dockershell.rb', line 49 def detached_postrun @logger.info 'terminating and cleaning up.' cleaner = Process.fork do Process.setsid output, status = Open3.capture2e('docker', 'kill', @options[:name]) @logger.debug output @logger.warn 'could not stop container' unless status.success? output, status = Open3.capture2e('docker', 'rm', '-f', @options[:name]) @logger.debug output @logger.warn 'could not remove container' unless status.success? postrun end Process.detach cleaner end |
#run! ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/dockershell.rb', line 19 def run! at_exit do detached_postrun end prerun create unless running? setup start end |
#start ⇒ Object
29 30 31 32 33 |
# File 'lib/dockershell.rb', line 29 def start @logger.info 'starting Dockershell.' args = 'docker', 'exec', '-it', @options[:name], 'script', '-qc', 'bash', '/dev/null' bomb 'could not start container.' unless system(*args) end |