docker_toolkit 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +10 -1
  3. data/Gemfile.lock +1 -1
  4. data/bin/terminator.rb +56 -0
  5. data/docker/Dockerfile +4 -1
  6. data/docker_toolkit.gemspec +1 -0
  7. data/lib/docker_toolkit/childprocess/abstract_io.rb +38 -0
  8. data/lib/docker_toolkit/childprocess/abstract_process.rb +194 -0
  9. data/lib/docker_toolkit/childprocess/errors.rb +28 -0
  10. data/lib/docker_toolkit/childprocess/jruby/io.rb +17 -0
  11. data/lib/docker_toolkit/childprocess/jruby/process.rb +161 -0
  12. data/lib/docker_toolkit/childprocess/jruby/pump.rb +55 -0
  13. data/lib/docker_toolkit/childprocess/jruby.rb +58 -0
  14. data/lib/docker_toolkit/childprocess/tools/generator.rb +148 -0
  15. data/lib/docker_toolkit/childprocess/unix/fork_exec_process.rb +72 -0
  16. data/lib/docker_toolkit/childprocess/unix/io.rb +22 -0
  17. data/lib/docker_toolkit/childprocess/unix/lib.rb +188 -0
  18. data/lib/docker_toolkit/childprocess/unix/platform/i386-linux.rb +14 -0
  19. data/lib/docker_toolkit/childprocess/unix/platform/i386-solaris.rb +13 -0
  20. data/lib/docker_toolkit/childprocess/unix/platform/x86_64-linux.rb +14 -0
  21. data/lib/docker_toolkit/childprocess/unix/platform/x86_64-macosx.rb +13 -0
  22. data/lib/docker_toolkit/childprocess/unix/posix_spawn_process.rb +135 -0
  23. data/lib/docker_toolkit/childprocess/unix/process.rb +91 -0
  24. data/lib/docker_toolkit/childprocess/unix.rb +11 -0
  25. data/lib/docker_toolkit/childprocess/version.rb +5 -0
  26. data/lib/docker_toolkit/childprocess/windows/handle.rb +93 -0
  27. data/lib/docker_toolkit/childprocess/windows/io.rb +25 -0
  28. data/lib/docker_toolkit/childprocess/windows/lib.rb +418 -0
  29. data/lib/docker_toolkit/childprocess/windows/process.rb +132 -0
  30. data/lib/docker_toolkit/childprocess/windows/process_builder.rb +177 -0
  31. data/lib/docker_toolkit/childprocess/windows/structs.rb +151 -0
  32. data/lib/docker_toolkit/childprocess/windows.rb +35 -0
  33. data/lib/docker_toolkit/childprocess.rb +208 -0
  34. data/lib/docker_toolkit/version.rb +1 -1
  35. data/lib/docker_toolkit/watcher.rb +188 -0
  36. data/lib/docker_toolkit.rb +5 -0
  37. metadata +33 -2
@@ -0,0 +1,188 @@
1
+ #!/usr/bin/env ruby
2
+ require 'English'
3
+
4
+ module DockerToolkit
5
+
6
+ class Watcher
7
+ def initialize
8
+ @lock = Mutex.new
9
+
10
+ @procs = []
11
+ @code = 0
12
+ @crashed = false
13
+ @threads = []
14
+ @who = nil
15
+ @stopping = false
16
+ end
17
+
18
+ def stop_all
19
+ @stopping = true
20
+
21
+ terminating = @procs.reject do |meta|
22
+ meta[:handled] || meta[:process].exited?
23
+ end
24
+
25
+ terminating.each do |meta|
26
+ ::Process.kill 'TERM', meta[:process].pid
27
+ end
28
+
29
+ terminating.each do |meta|
30
+ begin
31
+ meta[:process].poll_for_exit(10)
32
+ rescue ChildProcess::TimeoutError
33
+ meta[:process].stop(1)
34
+ meta[:stdout].close
35
+ meta[:stderr].close
36
+ end
37
+ end
38
+
39
+ if !@crashed
40
+ meta = @procs.detect do |meta|
41
+ meta[:process].crashed?
42
+ end
43
+
44
+ if meta
45
+ @crashed = true
46
+ @code = meta[:process].exit_code
47
+ @who = meta[:cmd]
48
+ end
49
+ end
50
+
51
+ @procs.each do |meta|
52
+ meta[:stdout].close rescue nil
53
+ meta[:stderr].close rescue nil
54
+ end
55
+
56
+ Thread.new do
57
+ sleep 2
58
+ @threads.each(&:terminate)
59
+ end
60
+
61
+
62
+ end
63
+
64
+
65
+ def add *cmd
66
+ process = ChildProcess.build(*cmd)
67
+
68
+ rerr, werr = IO.pipe
69
+ rout, wout = IO.pipe
70
+
71
+ process.io.stdout = wout
72
+ process.io.stderr = werr
73
+
74
+ meta = {
75
+ cmd: cmd,
76
+ process: process,
77
+ stdout: rout,
78
+ stderr: rerr
79
+ }
80
+
81
+ @threads << Thread.new(meta[:stdout], STDOUT) do |io, out|
82
+ loop do
83
+ break unless synchro_readline(io, out)
84
+ end
85
+ end
86
+
87
+ @threads << Thread.new(meta[:stderr], STDERR) do |io, out|
88
+ loop do
89
+ break unless synchro_readline(io, out)
90
+ end
91
+ end
92
+
93
+ log "Starting #{meta[:cmd]}"
94
+ meta[:pid] = meta[:process].start.pid
95
+
96
+ @procs.push(meta)
97
+ meta
98
+ end
99
+
100
+ def synchro_readline io, out
101
+ str = io.gets
102
+ @lock.synchronize{out.puts str}
103
+ true
104
+ rescue => e
105
+ false
106
+ end
107
+
108
+ def log msg
109
+ puts "[watcher]: #{msg}"
110
+ end
111
+
112
+ def error msg
113
+ STDERR.puts "[watcher]: Error: #{msg}"
114
+ end
115
+
116
+
117
+ def exec &block
118
+ %w[EXIT QUIT].each do |sig|
119
+ trap(sig) do
120
+ stop_all
121
+ end
122
+ end
123
+
124
+
125
+ %w[INT TERM].each do |sig|
126
+ trap(sig) do
127
+ log "Catch #{sig}: try exits gracefully.."
128
+ stop_all
129
+ end
130
+ end
131
+
132
+ trap("CLD") do |*args|
133
+ unhandled = @procs.reject do |meta|
134
+ meta[:handled] || !meta[:process].exited?
135
+ end
136
+
137
+ unhandled.any? do |meta|
138
+ log "Child finished"
139
+ log " Process[#{meta[:pid]}]: #{meta[:cmd]}"
140
+ log " status: #{meta[:process].crashed? ? 'crashed' : 'exited'}"
141
+ log " code: #{meta[:process].exit_code}"
142
+
143
+ meta[:handled] = true
144
+ meta[:stdout].close
145
+ meta[:stderr].close
146
+
147
+ if !@crashed && meta[:process].crashed?
148
+ @crashed = true
149
+ @code = meta[:process].exit_code
150
+ @who = meta[:cmd]
151
+ end
152
+
153
+ if !@stopping
154
+ log "Try exits gracefully.."
155
+ stop_all
156
+ end
157
+
158
+ true
159
+ end
160
+ end
161
+
162
+ begin
163
+ yield(self)
164
+ rescue => e
165
+ @crashed = true
166
+ @code = 1
167
+ error e.inspect
168
+ error e.backtrace.last(20).join("\n")
169
+ log "Try exits gracefully.."
170
+ stop_all
171
+ end
172
+
173
+ @threads.map(&:join)
174
+
175
+ if @crashed
176
+ @code = [@code || 0, 1].max
177
+ end
178
+
179
+ exit(@code || 0)
180
+ end
181
+
182
+
183
+ end
184
+
185
+ end
186
+
187
+
188
+
@@ -2,9 +2,14 @@ require 'diplomat'
2
2
  require 'English'
3
3
  require 'optparse'
4
4
 
5
+
6
+
5
7
  require 'docker_toolkit/version'
8
+ require 'docker_toolkit/watcher'
9
+ require 'docker_toolkit/childprocess'
6
10
 
7
11
  module DockerToolkit
8
12
 
9
13
 
14
+
10
15
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: docker_toolkit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Godko Ivan
@@ -74,6 +74,7 @@ email:
74
74
  executables:
75
75
  - consul.rb
76
76
  - merger.rb
77
+ - terminator.rb
77
78
  - waiter.rb
78
79
  extensions: []
79
80
  extra_rdoc_files: []
@@ -88,6 +89,7 @@ files:
88
89
  - README.md
89
90
  - bin/consul.rb
90
91
  - bin/merger.rb
92
+ - bin/terminator.rb
91
93
  - bin/waiter.rb
92
94
  - docker/Dockerfile
93
95
  - docker/docker-compose.yml
@@ -95,9 +97,38 @@ files:
95
97
  - docker/run_tests.sh
96
98
  - docker_toolkit.gemspec
97
99
  - lib/docker_toolkit.rb
100
+ - lib/docker_toolkit/childprocess.rb
101
+ - lib/docker_toolkit/childprocess/abstract_io.rb
102
+ - lib/docker_toolkit/childprocess/abstract_process.rb
103
+ - lib/docker_toolkit/childprocess/errors.rb
104
+ - lib/docker_toolkit/childprocess/jruby.rb
105
+ - lib/docker_toolkit/childprocess/jruby/io.rb
106
+ - lib/docker_toolkit/childprocess/jruby/process.rb
107
+ - lib/docker_toolkit/childprocess/jruby/pump.rb
108
+ - lib/docker_toolkit/childprocess/tools/generator.rb
109
+ - lib/docker_toolkit/childprocess/unix.rb
110
+ - lib/docker_toolkit/childprocess/unix/fork_exec_process.rb
111
+ - lib/docker_toolkit/childprocess/unix/io.rb
112
+ - lib/docker_toolkit/childprocess/unix/lib.rb
113
+ - lib/docker_toolkit/childprocess/unix/platform/i386-linux.rb
114
+ - lib/docker_toolkit/childprocess/unix/platform/i386-solaris.rb
115
+ - lib/docker_toolkit/childprocess/unix/platform/x86_64-linux.rb
116
+ - lib/docker_toolkit/childprocess/unix/platform/x86_64-macosx.rb
117
+ - lib/docker_toolkit/childprocess/unix/posix_spawn_process.rb
118
+ - lib/docker_toolkit/childprocess/unix/process.rb
119
+ - lib/docker_toolkit/childprocess/version.rb
120
+ - lib/docker_toolkit/childprocess/windows.rb
121
+ - lib/docker_toolkit/childprocess/windows/handle.rb
122
+ - lib/docker_toolkit/childprocess/windows/io.rb
123
+ - lib/docker_toolkit/childprocess/windows/lib.rb
124
+ - lib/docker_toolkit/childprocess/windows/process.rb
125
+ - lib/docker_toolkit/childprocess/windows/process_builder.rb
126
+ - lib/docker_toolkit/childprocess/windows/structs.rb
98
127
  - lib/docker_toolkit/version.rb
128
+ - lib/docker_toolkit/watcher.rb
99
129
  homepage: https://github.com/RnD-Soft/docker_toolkit
100
- licenses: []
130
+ licenses:
131
+ - MIT
101
132
  metadata: {}
102
133
  post_install_message:
103
134
  rdoc_options: []