vx-lib-spawn 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c2397cf1ffe6f77161c5efe128ee21c37218fde8
4
+ data.tar.gz: efbb33cf0a8d3656fee8748874e99962033d65e0
5
+ SHA512:
6
+ metadata.gz: 4e70ba6efadae82c0c17c0e582797b1986f7523957b4479b105e8ecf32734dbb2537cbb61119d0f56afb39ec2644f4ec6d1d3bda14eec82239144e2cab6c1e23
7
+ data.tar.gz: 828d65f3947d6d43a5b6ab9bf7918bf638a57dcc28cf490a470d3c5a4b0005695e1ecac68e3cb55699d5909797bf8adc45d7fde2694597bce0e734375b225121
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --order=rand
3
+ -fd
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ rvm:
2
+ - 1.9.3
3
+ - 2.0
4
+ - 2.1
5
+ before_install:
6
+ - echo 'travis:travis' | sudo chpasswd
7
+ - echo 'PasswordAuthentication yes' | sudo tee -a /etc/ssh/sshd_config
8
+ - sudo service ssh restart
9
+ before_script:
10
+ - export SSH_USER=travis
11
+ - export SSH_PASS=travis
12
+ - export SSH_HOST=localhost
13
+ - export SSH_PORT=22
14
+ script: bundle exec rake SPEC_OPTS='-fd --color --order=rand'
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in vx-common-spawn.gemspec
4
+ gemspec
5
+
6
+ gem 'docker-api', '1.15.0'
data/LICENSE.txt ADDED
@@ -0,0 +1,276 @@
1
+ Copyright (c) 2013 Dmitry Galinsky
2
+
3
+ The GNU General Public License, Version 2, June 1991 (GPLv2)
4
+ ============================================================
5
+
6
+ > Copyright (C) 1989, 1991 Free Software Foundation, Inc.
7
+ > 59 Temple Place, Suite 330
8
+ > Boston, MA 02111-1307 USA
9
+
10
+ Everyone is permitted to copy and distribute verbatim copies of this license
11
+ document, but changing it is not allowed.
12
+
13
+
14
+ Preamble
15
+ --------
16
+
17
+ The licenses for most software are designed to take away your freedom to share
18
+ and change it. By contrast, the GNU General Public License is intended to
19
+ guarantee your freedom to share and change free software--to make sure the
20
+ software is free for all its users. This General Public License applies to most
21
+ of the Free Software Foundation's software and to any other program whose
22
+ authors commit to using it. (Some other Free Software Foundation software is
23
+ covered by the GNU Library General Public License instead.) You can apply it to
24
+ your programs, too.
25
+
26
+ When we speak of free software, we are referring to freedom, not price. Our
27
+ General Public Licenses are designed to make sure that you have the freedom to
28
+ distribute copies of free software (and charge for this service if you wish),
29
+ that you receive source code or can get it if you want it, that you can change
30
+ the software or use pieces of it in new free programs; and that you know you can
31
+ do these things.
32
+
33
+ To protect your rights, we need to make restrictions that forbid anyone to deny
34
+ you these rights or to ask you to surrender the rights. These restrictions
35
+ translate to certain responsibilities for you if you distribute copies of the
36
+ software, or if you modify it.
37
+
38
+ For example, if you distribute copies of such a program, whether gratis or for a
39
+ fee, you must give the recipients all the rights that you have. You must make
40
+ sure that they, too, receive or can get the source code. And you must show them
41
+ these terms so they know their rights.
42
+
43
+ We protect your rights with two steps: (1) copyright the software, and (2) offer
44
+ you this license which gives you legal permission to copy, distribute and/or
45
+ modify the software.
46
+
47
+ Also, for each author's protection and ours, we want to make certain that
48
+ everyone understands that there is no warranty for this free software. If the
49
+ software is modified by someone else and passed on, we want its recipients to
50
+ know that what they have is not the original, so that any problems introduced by
51
+ others will not reflect on the original authors' reputations.
52
+
53
+ Finally, any free program is threatened constantly by software patents. We wish
54
+ to avoid the danger that redistributors of a free program will individually
55
+ obtain patent licenses, in effect making the program proprietary. To prevent
56
+ this, we have made it clear that any patent must be licensed for everyone's free
57
+ use or not licensed at all.
58
+
59
+ The precise terms and conditions for copying, distribution and modification
60
+ follow.
61
+
62
+
63
+ Terms And Conditions For Copying, Distribution And Modification
64
+ ---------------------------------------------------------------
65
+ 0. This License applies to any program or other work which contains a notice
66
+ placed by the copyright holder saying it may be distributed under the terms
67
+ of this General Public License. The "Program", below, refers to any such
68
+ program or work, and a "work based on the Program" means either the Program
69
+ or any derivative work under copyright law: that is to say, a work
70
+ containing the Program or a portion of it, either verbatim or with
71
+ modifications and/or translated into another language. (Hereinafter,
72
+ translation is included without limitation in the term "modification".)
73
+ Each licensee is addressed as "you".
74
+
75
+ Activities other than copying, distribution and modification are not
76
+ covered by this License; they are outside its scope. The act of running the
77
+ Program is not restricted, and the output from the Program is covered only
78
+ if its contents constitute a work based on the Program (independent of
79
+ having been made by running the Program). Whether that is true depends on
80
+ what the Program does.
81
+
82
+ 1. You may copy and distribute verbatim copies of the Program's source code as
83
+ you receive it, in any medium, provided that you conspicuously and
84
+ appropriately publish on each copy an appropriate copyright notice and
85
+ disclaimer of warranty; keep intact all the notices that refer to this
86
+ License and to the absence of any warranty; and give any other recipients
87
+ of the Program a copy of this License along with the Program.
88
+
89
+ You may charge a fee for the physical act of transferring a copy, and you
90
+ may at your option offer warranty protection in exchange for a fee.
91
+
92
+ 2. You may modify your copy or copies of the Program or any portion of it,
93
+ thus forming a work based on the Program, and copy and distribute such
94
+ modifications or work under the terms of Section 1 above, provided that you
95
+ also meet all of these conditions:
96
+
97
+ a) You must cause the modified files to carry prominent notices stating
98
+ that you changed the files and the date of any change.
99
+
100
+ b) You must cause any work that you distribute or publish, that in whole
101
+ or in part contains or is derived from the Program or any part thereof,
102
+ to be licensed as a whole at no charge to all third parties under the
103
+ terms of this License.
104
+
105
+ c) If the modified program normally reads commands interactively when run,
106
+ you must cause it, when started running for such interactive use in the
107
+ most ordinary way, to print or display an announcement including an
108
+ appropriate copyright notice and a notice that there is no warranty (or
109
+ else, saying that you provide a warranty) and that users may
110
+ redistribute the program under these conditions, and telling the user
111
+ how to view a copy of this License. (Exception: if the Program itself
112
+ is interactive but does not normally print such an announcement, your
113
+ work based on the Program is not required to print an announcement.)
114
+
115
+ These requirements apply to the modified work as a whole. If identifiable
116
+ sections of that work are not derived from the Program, and can be
117
+ reasonably considered independent and separate works in themselves, then
118
+ this License, and its terms, do not apply to those sections when you
119
+ distribute them as separate works. But when you distribute the same
120
+ sections as part of a whole which is a work based on the Program, the
121
+ distribution of the whole must be on the terms of this License, whose
122
+ permissions for other licensees extend to the entire whole, and thus to
123
+ each and every part regardless of who wrote it.
124
+
125
+ Thus, it is not the intent of this section to claim rights or contest your
126
+ rights to work written entirely by you; rather, the intent is to exercise
127
+ the right to control the distribution of derivative or collective works
128
+ based on the Program.
129
+
130
+ In addition, mere aggregation of another work not based on the Program with
131
+ the Program (or with a work based on the Program) on a volume of a storage
132
+ or distribution medium does not bring the other work under the scope of
133
+ this License.
134
+
135
+ 3. You may copy and distribute the Program (or a work based on it, under
136
+ Section 2) in object code or executable form under the terms of Sections 1
137
+ and 2 above provided that you also do one of the following:
138
+
139
+ a) Accompany it with the complete corresponding machine-readable source
140
+ code, which must be distributed under the terms of Sections 1 and 2
141
+ above on a medium customarily used for software interchange; or,
142
+
143
+ b) Accompany it with a written offer, valid for at least three years, to
144
+ give any third party, for a charge no more than your cost of physically
145
+ performing source distribution, a complete machine-readable copy of the
146
+ corresponding source code, to be distributed under the terms of
147
+ Sections 1 and 2 above on a medium customarily used for software
148
+ interchange; or,
149
+
150
+ c) Accompany it with the information you received as to the offer to
151
+ distribute corresponding source code. (This alternative is allowed only
152
+ for noncommercial distribution and only if you received the program in
153
+ object code or executable form with such an offer, in accord with
154
+ Subsection b above.)
155
+
156
+ The source code for a work means the preferred form of the work for making
157
+ modifications to it. For an executable work, complete source code means all
158
+ the source code for all modules it contains, plus any associated interface
159
+ definition files, plus the scripts used to control compilation and
160
+ installation of the executable. However, as a special exception, the source
161
+ code distributed need not include anything that is normally distributed (in
162
+ either source or binary form) with the major components (compiler, kernel,
163
+ and so on) of the operating system on which the executable runs, unless
164
+ that component itself accompanies the executable.
165
+
166
+ If distribution of executable or object code is made by offering access to
167
+ copy from a designated place, then offering equivalent access to copy the
168
+ source code from the same place counts as distribution of the source code,
169
+ even though third parties are not compelled to copy the source along with
170
+ the object code.
171
+
172
+ 4. You may not copy, modify, sublicense, or distribute the Program except as
173
+ expressly provided under this License. Any attempt otherwise to copy,
174
+ modify, sublicense or distribute the Program is void, and will
175
+ automatically terminate your rights under this License. However, parties
176
+ who have received copies, or rights, from you under this License will not
177
+ have their licenses terminated so long as such parties remain in full
178
+ compliance.
179
+
180
+ 5. You are not required to accept this License, since you have not signed it.
181
+ However, nothing else grants you permission to modify or distribute the
182
+ Program or its derivative works. These actions are prohibited by law if you
183
+ do not accept this License. Therefore, by modifying or distributing the
184
+ Program (or any work based on the Program), you indicate your acceptance of
185
+ this License to do so, and all its terms and conditions for copying,
186
+ distributing or modifying the Program or works based on it.
187
+
188
+ 6. Each time you redistribute the Program (or any work based on the Program),
189
+ the recipient automatically receives a license from the original licensor
190
+ to copy, distribute or modify the Program subject to these terms and
191
+ conditions. You may not impose any further restrictions on the recipients'
192
+ exercise of the rights granted herein. You are not responsible for
193
+ enforcing compliance by third parties to this License.
194
+
195
+ 7. If, as a consequence of a court judgment or allegation of patent
196
+ infringement or for any other reason (not limited to patent issues),
197
+ conditions are imposed on you (whether by court order, agreement or
198
+ otherwise) that contradict the conditions of this License, they do not
199
+ excuse you from the conditions of this License. If you cannot distribute so
200
+ as to satisfy simultaneously your obligations under this License and any
201
+ other pertinent obligations, then as a consequence you may not distribute
202
+ the Program at all. For example, if a patent license would not permit
203
+ royalty-free redistribution of the Program by all those who receive copies
204
+ directly or indirectly through you, then the only way you could satisfy
205
+ both it and this License would be to refrain entirely from distribution of
206
+ the Program.
207
+
208
+ If any portion of this section is held invalid or unenforceable under any
209
+ particular circumstance, the balance of the section is intended to apply
210
+ and the section as a whole is intended to apply in other circumstances.
211
+
212
+ It is not the purpose of this section to induce you to infringe any patents
213
+ or other property right claims or to contest validity of any such claims;
214
+ this section has the sole purpose of protecting the integrity of the free
215
+ software distribution system, which is implemented by public license
216
+ practices. Many people have made generous contributions to the wide range
217
+ of software distributed through that system in reliance on consistent
218
+ application of that system; it is up to the author/donor to decide if he or
219
+ she is willing to distribute software through any other system and a
220
+ licensee cannot impose that choice.
221
+
222
+ This section is intended to make thoroughly clear what is believed to be a
223
+ consequence of the rest of this License.
224
+
225
+ 8. If the distribution and/or use of the Program is restricted in certain
226
+ countries either by patents or by copyrighted interfaces, the original
227
+ copyright holder who places the Program under this License may add an
228
+ explicit geographical distribution limitation excluding those countries, so
229
+ that distribution is permitted only in or among countries not thus excluded.
230
+ In such case, this License incorporates the limitation as if written in
231
+ the body of this License.
232
+
233
+ 9. The Free Software Foundation may publish revised and/or new versions of the
234
+ General Public License from time to time. Such new versions will be similar
235
+ in spirit to the present version, but may differ in detail to address new
236
+ problems or concerns.
237
+
238
+ Each version is given a distinguishing version number. If the Program
239
+ specifies a version number of this License which applies to it and "any
240
+ later version", you have the option of following the terms and conditions
241
+ either of that version or of any later version published by the Free
242
+ Software Foundation. If the Program does not specify a version number of
243
+ this License, you may choose any version ever published by the Free
244
+ Software Foundation.
245
+
246
+ 10. If you wish to incorporate parts of the Program into other free programs
247
+ whose distribution conditions are different, write to the author to ask for
248
+ permission. For software which is copyrighted by the Free Software
249
+ Foundation, write to the Free Software Foundation; we sometimes make
250
+ exceptions for this. Our decision will be guided by the two goals of
251
+ preserving the free status of all derivatives of our free software and of
252
+ promoting the sharing and reuse of software generally.
253
+
254
+
255
+ No Warranty
256
+ -----------
257
+
258
+ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR
259
+ THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
260
+ OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
261
+ PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
262
+ OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
263
+ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO
264
+ THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM
265
+ PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
266
+ CORRECTION.
267
+
268
+ 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL
269
+ ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE
270
+ THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
271
+ GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
272
+ USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
273
+ DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
274
+ PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
275
+ EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
276
+ SUCH DAMAGES.
data/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # Vx::Lib::Spawn
2
+
3
+ This gem helps to spawn processes in a shell capturing output in realtime.
4
+ It also allows to set the temeouts.
5
+
6
+ ## Requirements
7
+
8
+ MRI 1.9.3, 2.0.0, 2.1.x.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'vx-lib-spawn'
15
+
16
+ And then execute the bundler:
17
+
18
+ $ bundle
19
+
20
+ Or install it via `gem` command:
21
+
22
+ $ gem install vx-lib-spawn
23
+
24
+ ## Quick Start
25
+
26
+ The following snippet demonstrates the usage:
27
+
28
+ ```ruby
29
+ include Vx::Lib::Spawn
30
+
31
+ spawn "ls -la" do |output|
32
+ print output
33
+ # prints directory listing
34
+ end
35
+
36
+ spawn("echo value", timeout: 10) do |output|
37
+ print output
38
+ # its print "value\n"
39
+ end
40
+ ```
41
+
42
+ ```ruby
43
+ # Spawn remote processes example
44
+
45
+ open_ssh('localhost', 'user') do |ssh|
46
+ ssh.spawn("ls -la") do |output|
47
+ print output
48
+ # prints directory listing
49
+ end
50
+
51
+ spawn("echo value", read_timeout: 10) do |output|
52
+ print output
53
+ # its print "value\n"
54
+ end
55
+ end
56
+
57
+ ```
58
+
59
+ ### Timeouts
60
+
61
+ When a timeout is reached spawn raises ```Vx::Lib::Spawn::TimeoutError``` or
62
+ ```Vx::Lib::Spawn::ReadTimeoutError```. Both exceptions inherit
63
+ from Timeout::Error
64
+
65
+ ### Return values
66
+
67
+ Both ```spawn``` methods return process exit code. If a process was terminated by a signal, for example
68
+ KILL or INT, the methods return negative number identical to a signal number (-9 for KILL, etc.)
69
+
70
+ ## Contributing
71
+
72
+ 1. Fork it
73
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
74
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
75
+ 4. Push to the branch (`git push origin my-new-feature`)
76
+ 5. Create new Pull Request
77
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,36 @@
1
+ require File.expand_path("../spawn/version", __FILE__)
2
+
3
+ module Vx
4
+ module Lib
5
+ module Spawn
6
+
7
+ autoload :Process, File.expand_path("../spawn/process", __FILE__)
8
+ autoload :SSH, File.expand_path("../spawn/ssh", __FILE__)
9
+ autoload :Timeout, File.expand_path("../spawn/timeout", __FILE__)
10
+ autoload :ReadTimeout, File.expand_path("../spawn/read_timeout", __FILE__)
11
+ autoload :TimeoutError, File.expand_path("../spawn/error", __FILE__)
12
+ autoload :ReadTimeoutError, File.expand_path("../spawn/error", __FILE__)
13
+
14
+ class << self
15
+ @@pool_interval = 1.0
16
+
17
+ def pool_interval
18
+ @@pool_interval
19
+ end
20
+
21
+ def pool_interval=(val)
22
+ @@pool_interval = val
23
+ end
24
+ end
25
+
26
+ def open_ssh(*args, &block)
27
+ Lib::Spawn::SSH.open(*args, &block)
28
+ end
29
+
30
+ def spawn(*args, &block)
31
+ Lib::Spawn::Process.spawn(*args, &block)
32
+ end
33
+
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,34 @@
1
+ require 'timeout'
2
+
3
+ module Vx
4
+ module Lib
5
+ module Spawn
6
+
7
+ class TimeoutError < ::Timeout::Error
8
+
9
+ def initialize(cmd, seconds)
10
+ @cmd = cmd
11
+ @seconds = seconds
12
+ end
13
+
14
+ def to_s
15
+ "Execution expired, command did not finish within #{@seconds} seconds"
16
+ end
17
+
18
+ end
19
+
20
+ class ReadTimeoutError < ::Timeout::Error
21
+
22
+ def initialize(cmd, seconds)
23
+ @cmd = cmd
24
+ @seconds = seconds
25
+ end
26
+
27
+ def to_s
28
+ "No output has been received in the last #{@seconds} seconds"
29
+ end
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,106 @@
1
+ require 'timeout'
2
+ require 'pty'
3
+ require 'io/console'
4
+
5
+ module Vx
6
+ module Lib
7
+ module Spawn
8
+ module Process
9
+
10
+ extend self
11
+
12
+ def spawn(command, options = {}, &block)
13
+ select_timeout = options.delete(:pool_interval) || Spawn.pool_interval
14
+ timeout = Spawn::Timeout.new options.delete(:timeout)
15
+ read_timeout = Spawn::ReadTimeout.new options.delete(:read_timeout)
16
+
17
+ status = spawn_command_internal(command, options) do |r|
18
+ read_loop r, timeout, read_timeout, select_timeout, &block
19
+ end
20
+
21
+ compute_exit_code command, status, timeout, read_timeout
22
+ end
23
+
24
+ private
25
+
26
+ def request_pipes(options)
27
+ if options[:pty]
28
+ m,s = PTY.open
29
+ r1,w1 = IO.pipe
30
+
31
+ s.raw! # disable newline conversion.
32
+ m.sync = true
33
+ s.sync = true
34
+
35
+ [m, s, r1, w1]
36
+ else
37
+ r1,w1 = IO.pipe
38
+ r2,w2 = IO.pipe
39
+
40
+ w1.sync = true
41
+ r1.sync = true
42
+
43
+ [r1, w1, r2, w2]
44
+ end
45
+ end
46
+
47
+ def spawn_command_internal(command, options)
48
+ r1, w1, r2, w2 = request_pipes(options)
49
+
50
+ pid = ::Process.spawn(command, in: r2, out: w1, err: w1)
51
+
52
+ begin
53
+ if i = options[:stdin]
54
+ IO.copy_stream i, w2
55
+ end
56
+ w2.close
57
+ w1.close
58
+
59
+ yield r1
60
+ rescue Errno::EIO
61
+ end
62
+
63
+ ::Process.kill 'KILL', pid
64
+ _, status = ::Process.wait2(pid)
65
+
66
+ r1.close
67
+ r2.close
68
+
69
+ status
70
+ end
71
+
72
+ def compute_exit_code(command, status, timeout, read_timeout)
73
+ case
74
+ when read_timeout.happened?
75
+ raise Spawn::ReadTimeoutError.new command, read_timeout.value
76
+ when timeout.happened?
77
+ raise Spawn::TimeoutError.new command, timeout.value
78
+ else
79
+ termsig = status && status.termsig
80
+ exit_code = status && status.exitstatus
81
+ exit_code || (termsig && termsig * -1) || -1
82
+ end
83
+ end
84
+
85
+ def read_loop(reader, timeout, read_timeout, interval, &block)
86
+ read_timeout.reset
87
+
88
+ loop do
89
+ break if timeout.happened?
90
+
91
+ rs, _, _ = IO.select([reader], nil, nil, interval)
92
+
93
+ if rs
94
+ break if rs[0].eof?
95
+ yield rs[0].readpartial(8192) if block_given?
96
+ read_timeout.reset
97
+ else
98
+ break if read_timeout.happened?
99
+ end
100
+ end
101
+ end
102
+
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,30 @@
1
+ module Vx
2
+ module Lib
3
+ module Spawn
4
+ class ReadTimeout
5
+
6
+ def initialize(val)
7
+ @value = val.to_f > 0 ? val.to_f : nil
8
+ @happened = false
9
+ @tm = nil
10
+ end
11
+
12
+ def reset
13
+ @tm = Time.new if @value
14
+ end
15
+
16
+ def happened?
17
+ return true if @happened
18
+ return false unless @tm
19
+
20
+ @happened = Time.now > (@tm + @value)
21
+ end
22
+
23
+ def value
24
+ @value
25
+ end
26
+
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,116 @@
1
+ require 'net/ssh'
2
+ require 'timeout'
3
+
4
+ module Vx
5
+ module Lib
6
+ module Spawn
7
+ class SSH
8
+
9
+ class << self
10
+ def open(host, user, options = {}, &block)
11
+ ::Net::SSH.start(host, user, {
12
+ paranoid: false
13
+ }.merge(options)) do |ssh|
14
+ yield new(ssh)
15
+ end
16
+ end
17
+ end
18
+
19
+ attr_reader :host, :user, :options, :connection
20
+
21
+ def initialize(ssh)
22
+ @connection = ssh
23
+ end
24
+
25
+ def spawn(command, options = {}, &block)
26
+ exit_code = nil
27
+ timeout = Spawn::Timeout.new options.delete(:timeout)
28
+ read_timeout = Spawn::ReadTimeout.new options.delete(:read_timeout)
29
+
30
+ channel = spawn_channel command, read_timeout, options, &block
31
+
32
+ channel.on_request("exit-status") do |_,data|
33
+ exit_code = data.read_long
34
+ end
35
+
36
+ channel.on_request("exit-signal") do |_,data|
37
+ exit_code = data.read_long * -1
38
+ end
39
+
40
+ pool channel, timeout, read_timeout
41
+
42
+ compute_exit_code command, exit_code, timeout, read_timeout
43
+ end
44
+
45
+ private
46
+
47
+ def request_pty(channel, options)
48
+ if options[:pty]
49
+ channel.request_pty do |_, pty_status|
50
+ raise StandardError, "could not obtain pty" unless pty_status
51
+ yield if block_given?
52
+ end
53
+ else
54
+ yield if block_given?
55
+ end
56
+ end
57
+
58
+ def pool(channel, timeout, read_timeout)
59
+ @connection.loop Spawn.pool_interval do
60
+ if read_timeout.happened? || timeout.happened?
61
+ false
62
+ else
63
+ channel.active?
64
+ end
65
+ end
66
+ end
67
+
68
+ def compute_exit_code(command, exit_code, timeout, read_timeout)
69
+ case
70
+ when read_timeout.happened?
71
+ raise Spawn::ReadTimeoutError.new command, read_timeout.value
72
+ when timeout.happened?
73
+ raise Spawn::TimeoutError.new command, timeout.value
74
+ else
75
+ exit_code || -1 # nil exit_code means that the process is killed
76
+ end
77
+ end
78
+
79
+ def spawn_channel(command, read_timeout, options, &block)
80
+ @connection.open_channel do |channel|
81
+
82
+ request_pty channel, options do
83
+
84
+ read_timeout.reset
85
+
86
+ channel.exec command do |_, success|
87
+
88
+ unless success
89
+ yield "FAILED: couldn't execute command (ssh.channel.exec)\n" if block_given?
90
+ end
91
+
92
+ channel.on_data do |_, data|
93
+ yield data if block_given?
94
+ read_timeout.reset
95
+ end
96
+
97
+ channel.on_extended_data do |_, _, data|
98
+ yield data if block_given?
99
+ read_timeout.reset
100
+ end
101
+
102
+ if i = options[:stdin]
103
+ channel.send_data i.read
104
+ channel.eof!
105
+ end
106
+
107
+ end
108
+ end
109
+ end
110
+
111
+ end
112
+
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,26 @@
1
+ module Vx
2
+ module Lib
3
+ module Spawn
4
+ class Timeout
5
+ def initialize(value)
6
+ @value = (value.to_f > 0) ? value.to_f : nil
7
+ if @value
8
+ @time_end = Time.now + @value
9
+ end
10
+ @happened = false
11
+ end
12
+
13
+ def happened?
14
+ return false unless @value
15
+ return true if @happened
16
+
17
+ @happened = Time.now > @time_end
18
+ end
19
+
20
+ def value
21
+ @value
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,7 @@
1
+ module Vx
2
+ module Lib
3
+ module Spawn
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,102 @@
1
+ require 'spec_helper'
2
+ require 'timeout'
3
+
4
+ describe Vx::Lib::Spawn::Process do
5
+ let(:collected) { "" }
6
+ let(:user) { ENV['USER'] }
7
+
8
+ subject { collected }
9
+
10
+ [true, false].each do |pty|
11
+ context "with pty: #{pty.inspect}" do
12
+
13
+ it "run command successfuly" do
14
+ code = run "echo $USER", pty: pty
15
+ expect(subject.strip).to eq "#{user}"
16
+ expect(code).to eq 0
17
+ end
18
+
19
+ it "run command with error" do
20
+ code = run "false", pty: pty
21
+ expect(subject).to eq ""
22
+ expect(code).to eq 1
23
+ end
24
+
25
+ context "timeout" do
26
+ it 'run command with timeout' do
27
+ expect {
28
+ run("echo $USER && sleep 0.5", timeout: 0.2, pty: pty)
29
+ }.to raise_error(Vx::Lib::Spawn::TimeoutError)
30
+ expect(subject.strip).to eq "#{user}"
31
+ end
32
+
33
+ it 'run command with timeout successfuly' do
34
+ code = run("echo BAR && sleep 0.1", timeout: 0.5, pty: pty)
35
+ expect(subject.strip).to eq "BAR"
36
+ expect(code).to eq 0
37
+ end
38
+ end
39
+
40
+ context "read_timeout" do
41
+ it 'run command with read timeout' do
42
+ expect{
43
+ run('sleep 0.5', read_timeout: 0.2, pty: pty)
44
+ }.to raise_error(Vx::Lib::Spawn::ReadTimeoutError)
45
+ expect(collected).to eq ""
46
+ end
47
+
48
+ it 'run command with read timeout in loop' do
49
+ expect{
50
+ run('sleep 0.1 ; echo $USER ; sleep 0.5', read_timeout: 0.3, pty: pty)
51
+ }.to raise_error(Vx::Lib::Spawn::ReadTimeoutError)
52
+ expect(collected.strip).to eq "#{user}"
53
+ end
54
+
55
+ it 'run command with read timeout successfuly' do
56
+ code = run('echo $USER; sleep 0.1', read_timeout: 0.5, pty: pty)
57
+ expect(collected.strip).to eq "#{user}"
58
+ expect(code).to eq 0
59
+ end
60
+
61
+ it 'run command with read timeout in loop successfuly' do
62
+ code = run('sleep 0.3 ; echo $USER; sleep 0.3 ; echo $USER', read_timeout: 0.5, pty: pty)
63
+ expect(collected).to eq "#{user}\n#{user}\n"
64
+ expect(code).to eq 0
65
+ end
66
+ end
67
+
68
+ it 'run and kill process' do
69
+ code = run( "echo $USER; kill -KILL $$", pty: pty)
70
+ expect(subject).to eq "#{user}\n"
71
+ expect(code).to eq(-9)
72
+ end
73
+
74
+ it 'run and interupt process' do
75
+ code = run( "echo $USER; kill -INT $$", pty: pty)
76
+ expect(subject).to eq "#{user}\n"
77
+ expect(code).to eq(-2)
78
+ end
79
+
80
+ it "should copy stdin" do
81
+ io = StringIO.new("echo foo")
82
+ code = run("/bin/sh", stdin: io, pty: pty)
83
+ expect(subject).to eq "foo\n"
84
+ expect(code).to eq 0
85
+ end
86
+ end
87
+ end
88
+
89
+ def run(*args, &block)
90
+ timeout do
91
+ described_class.spawn(*args) do |out|
92
+ collected << out
93
+ end
94
+ end
95
+ end
96
+
97
+ def timeout
98
+ Timeout.timeout(3) do
99
+ yield
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vx::Lib::Spawn::ReadTimeout do
4
+ subject { described_class.new 0.2 }
5
+
6
+ it "just created" do
7
+ expect(subject.value).to eq 0.2
8
+ expect(subject).to_not be_happened
9
+ end
10
+
11
+ it "should be work" do
12
+ subject.reset
13
+ sleep 0.1
14
+ expect(subject).to_not be_happened
15
+
16
+ subject.reset
17
+ sleep 0.3
18
+ expect(subject).to be_happened
19
+ end
20
+
21
+ it "do nothing unless value" do
22
+ expect(subject).to_not be_happened
23
+ end
24
+
25
+ it "do nothing unless timeout" do
26
+ subject.reset
27
+ sleep 0.1
28
+ expect(subject).to_not be_happened
29
+ end
30
+ end
@@ -0,0 +1,121 @@
1
+ require 'spec_helper'
2
+ require 'timeout'
3
+
4
+ describe Vx::Lib::Spawn::SSH, ssh: true do
5
+
6
+ let(:user) { ENV['SSH_USER'] || 'vagrant' }
7
+ let(:host) { ENV['SSH_HOST'] || 'localhost' }
8
+ let(:pass) { ENV['SSH_PASS'] || 'vagrant' }
9
+ let(:port) { ENV['SSH_PORT'] || 2222 }
10
+ let(:collected) { '' }
11
+
12
+ it "run command successfuly" do
13
+ code = run_ssh 'echo $USER'
14
+ expect(collected).to eq "#{user}\n"
15
+ expect(code).to eq 0
16
+ end
17
+
18
+ it "run command successfuly with pty" do
19
+ code = run_ssh 'env', pty: true
20
+ expect(collected).to match(/SSH_TTY=/)
21
+ expect(code).to eq 0
22
+ end
23
+
24
+ it "run command with error" do
25
+ code = run_ssh 'false'
26
+ expect(collected).to eq ""
27
+ expect(code).to eq 1
28
+ end
29
+
30
+ context "timeout" do
31
+ it 'run command with timeout' do
32
+ expect{
33
+ run_ssh('echo $USER; sleep 0.5', timeout: 0.2)
34
+ }.to raise_error(Vx::Lib::Spawn::TimeoutError)
35
+ end
36
+
37
+ it 'run command with timeout successfuly' do
38
+ code = run_ssh('echo $USER; sleep 0.2', timeout: 1)
39
+ expect(collected).to eq "#{user}\n"
40
+ expect(code).to eq 0
41
+ end
42
+ end
43
+
44
+ context "read_timeout" do
45
+ it 'run command with read timeout' do
46
+ expect{
47
+ run_ssh('sleep 0.5', read_timeout: 0.2)
48
+ }.to raise_error(Vx::Lib::Spawn::ReadTimeoutError)
49
+ expect(collected).to eq ""
50
+ end
51
+
52
+ it 'run command with read timeout in loop' do
53
+ expect{
54
+ run_ssh('sleep 0.1 ; echo $USER ; sleep 0.5', read_timeout: 0.3)
55
+ }.to raise_error(Vx::Lib::Spawn::ReadTimeoutError)
56
+ expect(collected).to eq "#{user}\n"
57
+ end
58
+
59
+ it 'run command with read timeout successfuly' do
60
+ code = run_ssh('echo $USER; sleep 0.1', read_timeout: 0.5)
61
+ expect(collected).to eq "#{user}\n"
62
+ expect(code).to eq 0
63
+ end
64
+
65
+ it 'run command with read timeout in loop successfuly' do
66
+ code = run_ssh('sleep 0.3 ; echo $USER; sleep 0.3 ; echo $USER', read_timeout: 0.5)
67
+ expect(collected).to eq "#{user}\n#{user}\n"
68
+ expect(code).to eq 0
69
+ end
70
+ end
71
+
72
+ it 'run and kill process' do
73
+ code = run_ssh("echo $USER; kill -9 $$")
74
+ expect(collected).to eq "#{user}\n"
75
+ expect(code).to eq(-4)
76
+ end
77
+
78
+ it 'run and interupt process' do
79
+ code = run_ssh("echo $USER; kill -9 $$")
80
+ expect(collected).to eq "#{user}\n"
81
+ expect(code).to eq(-4)
82
+ end
83
+
84
+ it "should copy stdin with pty" do
85
+ io = StringIO.new("echo foo ; exit 0\n")
86
+ code = run_ssh("/bin/sh", stdin: io, pty: true)
87
+ expect(collected).to match "foo\r\n"
88
+ expect(code).to eq 0
89
+ end
90
+
91
+ it "should copy stdin" do
92
+ io = StringIO.new("echo foo")
93
+ code = run_ssh("/bin/sh", stdin: io)
94
+ expect(collected).to match "foo\n"
95
+ expect(code).to eq 0
96
+ end
97
+
98
+ def open_ssh(&block)
99
+ described_class.open(host, user, password: pass, paranoid: false, verbose: 2, port: port, &block)
100
+ end
101
+
102
+ def re(s)
103
+ Regexp.escape s
104
+ end
105
+
106
+ def run_ssh(*args)
107
+ timeout do
108
+ open_ssh do |ssh|
109
+ ssh.spawn(*args) do |s|
110
+ collected << s
111
+ end
112
+ end
113
+ end
114
+ end
115
+
116
+ def timeout
117
+ Timeout.timeout(3) do
118
+ yield
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vx::Lib::Spawn do
4
+
5
+ subject { Object.new }
6
+
7
+ before { subject.extend described_class }
8
+
9
+ context "spawn" do
10
+ it "should be" do
11
+ expect(subject.spawn 'true').to eq 0
12
+ end
13
+ end
14
+
15
+ context "open_ssh" do
16
+ let(:user) { ENV['SSH_USER'] || 'vagrant' }
17
+ let(:host) { ENV['SSH_HOST'] || 'localhost' }
18
+ let(:pass) { ENV['SSH_PASS'] || 'vagrant' }
19
+ let(:port) { ENV['SSH_PORT'] || 2222 }
20
+ let(:ssh) { nil }
21
+
22
+ it "should be" do
23
+ subject.open_ssh(host, user, password: pass, port: port) do |ssh|
24
+ expect(ssh.spawn 'true').to eq 0
25
+ end
26
+ end
27
+ end
28
+ end
29
+
@@ -0,0 +1,5 @@
1
+ require File.expand_path('../../lib/vx/lib/spawn', __FILE__)
2
+
3
+ RSpec.configure do |c|
4
+ end
5
+
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'vx/lib/spawn/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "vx-lib-spawn"
8
+ spec.version = Vx::Lib::Spawn::VERSION
9
+ spec.authors = ["Dmitry Galinsky"]
10
+ spec.email = ["dima@vexor.io"]
11
+ spec.description = %q{ Spawn processes in a shell capturing output in realtime. It also allows to set the temeouts. }
12
+ spec.summary = %q{ This gem helps to spawn processes in a shell capturing output in realtime. It also allows to set the temeouts. }
13
+ spec.homepage = "https://github.com/vexor/vx-lib-spawn"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "net-ssh", "~> 2.6"
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "pry"
26
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vx-lib-spawn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dmitry Galinsky
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: net-ssh
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: " Spawn processes in a shell capturing output in realtime. It also allows
84
+ to set the temeouts. "
85
+ email:
86
+ - dima@vexor.io
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - lib/vx/lib/spawn.rb
99
+ - lib/vx/lib/spawn/error.rb
100
+ - lib/vx/lib/spawn/process.rb
101
+ - lib/vx/lib/spawn/read_timeout.rb
102
+ - lib/vx/lib/spawn/ssh.rb
103
+ - lib/vx/lib/spawn/timeout.rb
104
+ - lib/vx/lib/spawn/version.rb
105
+ - spec/lib/spawn/process_spec.rb
106
+ - spec/lib/spawn/read_timeout_spec.rb
107
+ - spec/lib/spawn/ssh_spec.rb
108
+ - spec/lib/spawn_spec.rb
109
+ - spec/spec_helper.rb
110
+ - vx-lib-spawn.gemspec
111
+ homepage: https://github.com/vexor/vx-lib-spawn
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.2.2
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: This gem helps to spawn processes in a shell capturing output in realtime.
135
+ It also allows to set the temeouts.
136
+ test_files:
137
+ - spec/lib/spawn/process_spec.rb
138
+ - spec/lib/spawn/read_timeout_spec.rb
139
+ - spec/lib/spawn/ssh_spec.rb
140
+ - spec/lib/spawn_spec.rb
141
+ - spec/spec_helper.rb