openvz 1.1

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.
@@ -0,0 +1,6 @@
1
+ require 'systemu'
2
+
3
+
4
+ systemu 'date' do |cid|
5
+ p cid
6
+ end
@@ -0,0 +1,23 @@
1
+ lib, version = File::basename(File::dirname(File::expand_path(__FILE__))).split %r/-/, 2
2
+
3
+ require 'rubygems'
4
+
5
+ Gem::Specification::new do |spec|
6
+ spec.name = lib
7
+ spec.version = version
8
+ spec.platform = Gem::Platform::RUBY
9
+ spec.summary = lib
10
+
11
+ spec.files = Dir::glob "**/**"
12
+ spec.executables = Dir::glob("bin/*").map{|exe| File::basename exe}
13
+
14
+ spec.require_path = "lib"
15
+ spec.autorequire = lib
16
+
17
+ spec.has_rdoc = File::exist? "doc"
18
+ spec.test_suite_file = "test/#{ lib }.rb" if File::directory? "test"
19
+
20
+ spec.author = "Ara T. Howard"
21
+ spec.email = "ara.t.howard@noaa.gov"
22
+ spec.homepage = "http://codeforpeople.com/lib/ruby/#{ lib }/"
23
+ end
@@ -0,0 +1,32 @@
1
+ require 'pathname'
2
+
3
+ $VERBOSE=nil
4
+
5
+ def indent s, n = 2
6
+ ws = ' ' * n
7
+ s.gsub %r/^/, ws
8
+ end
9
+
10
+ template = IO::read 'README.tmpl'
11
+
12
+ samples = ''
13
+ prompt = '~ > '
14
+
15
+ Dir['sample*/*'].sort.each do |sample|
16
+ samples << "\n" << " <========< #{ sample } >========>" << "\n\n"
17
+
18
+ cmd = "cat #{ sample }"
19
+ samples << indent(prompt + cmd, 2) << "\n\n"
20
+ samples << indent(`#{ cmd }`, 4) << "\n"
21
+
22
+ cmd = "ruby #{ sample }"
23
+ samples << indent(prompt + cmd, 2) << "\n\n"
24
+
25
+ cmd = "ruby -W0 -Ilib #{ sample }"
26
+ samples << indent(`#{ cmd } 2>&1`, 4) << "\n"
27
+ end
28
+
29
+ #samples.gsub! %r/^/, ' '
30
+
31
+ readme = template.gsub %r/^\s*@samples\s*$/, samples
32
+ print readme
@@ -0,0 +1,206 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rbconfig'
3
+ require 'find'
4
+ require 'ftools'
5
+ require 'tempfile'
6
+ include Config
7
+
8
+ LIBDIR = "lib"
9
+ LIBDIR_MODE = 0644
10
+
11
+ BINDIR = "bin"
12
+ BINDIR_MODE = 0755
13
+
14
+
15
+ $srcdir = CONFIG["srcdir"]
16
+ $version = CONFIG["MAJOR"]+"."+CONFIG["MINOR"]
17
+ $libdir = File.join(CONFIG["libdir"], "ruby", $version)
18
+ $archdir = File.join($libdir, CONFIG["arch"])
19
+ $site_libdir = $:.find {|x| x =~ /site_ruby$/}
20
+ $bindir = CONFIG["bindir"] || CONFIG['BINDIR']
21
+ $ruby_install_name = CONFIG['ruby_install_name'] || CONFIG['RUBY_INSTALL_NAME'] || 'ruby'
22
+ $ruby_ext = CONFIG['EXEEXT'] || ''
23
+ $ruby = File.join($bindir, ($ruby_install_name + $ruby_ext))
24
+
25
+ if !$site_libdir
26
+ $site_libdir = File.join($libdir, "site_ruby")
27
+ elsif $site_libdir !~ %r/#{Regexp.quote($version)}/
28
+ $site_libdir = File.join($site_libdir, $version)
29
+ end
30
+
31
+ def install_rb(srcdir=nil, destdir=nil, mode=nil, bin=nil)
32
+ #{{{
33
+ path = []
34
+ dir = []
35
+ Find.find(srcdir) do |f|
36
+ next unless FileTest.file?(f)
37
+ next if (f = f[srcdir.length+1..-1]) == nil
38
+ next if (/CVS$/ =~ File.dirname(f))
39
+ next if f =~ %r/\.lnk/
40
+ path.push f
41
+ dir |= [File.dirname(f)]
42
+ end
43
+ for f in dir
44
+ next if f == "."
45
+ next if f == "CVS"
46
+ File::makedirs(File.join(destdir, f))
47
+ end
48
+ for f in path
49
+ next if (/\~$/ =~ f)
50
+ next if (/^\./ =~ File.basename(f))
51
+ unless bin
52
+ File::install(File.join(srcdir, f), File.join(destdir, f), mode, true)
53
+ else
54
+ from = File.join(srcdir, f)
55
+ to = File.join(destdir, f)
56
+ shebangify(from) do |sf|
57
+ $deferr.print from, " -> ", File::catname(from, to), "\n"
58
+ $deferr.printf "chmod %04o %s\n", mode, to
59
+ File::install(sf, to, mode, false)
60
+ end
61
+ end
62
+ end
63
+ #}}}
64
+ end
65
+ def shebangify f
66
+ #{{{
67
+ open(f) do |fd|
68
+ buf = fd.read 42
69
+ if buf =~ %r/^\s*#\s*!.*ruby/o
70
+ ftmp = Tempfile::new("#{ $$ }_#{ File::basename(f) }")
71
+ begin
72
+ fd.rewind
73
+ ftmp.puts "#!#{ $ruby }"
74
+ while((buf = fd.read(8192)))
75
+ ftmp.write buf
76
+ end
77
+ ftmp.close
78
+ yield ftmp.path
79
+ ensure
80
+ ftmp.close!
81
+ end
82
+ else
83
+ yield f
84
+ end
85
+ end
86
+ #}}}
87
+ end
88
+ def ARGV.switch
89
+ #{{{
90
+ return nil if self.empty?
91
+ arg = self.shift
92
+ return nil if arg == '--'
93
+ if arg =~ /^-(.)(.*)/
94
+ return arg if $1 == '-'
95
+ raise 'unknown switch "-"' if $2.index('-')
96
+ self.unshift "-#{$2}" if $2.size > 0
97
+ "-#{$1}"
98
+ else
99
+ self.unshift arg
100
+ nil
101
+ end
102
+ #}}}
103
+ end
104
+ def ARGV.req_arg
105
+ #{{{
106
+ self.shift || raise('missing argument')
107
+ #}}}
108
+ end
109
+ def linkify d, linked = []
110
+ #--{{{
111
+ if test ?d, d
112
+ versioned = Dir[ File::join(d, "*-[0-9].[0-9].[0-9].rb") ]
113
+ versioned.each do |v|
114
+ src, dst = v, v.gsub(%r/\-[\d\.]+\.rb$/, '.rb')
115
+ lnk = nil
116
+ begin
117
+ if test ?l, dst
118
+ lnk = "#{ dst }.lnk"
119
+ puts "#{ dst } -> #{ lnk }"
120
+ File::rename dst, lnk
121
+ end
122
+ unless test ?e, dst
123
+ puts "#{ src } -> #{ dst }"
124
+ File::copy src, dst
125
+ linked << dst
126
+ end
127
+ ensure
128
+ if lnk
129
+ at_exit do
130
+ puts "#{ lnk } -> #{ dst }"
131
+ File::rename lnk, dst
132
+ end
133
+ end
134
+ end
135
+ end
136
+ end
137
+ linked
138
+ #--}}}
139
+ end
140
+
141
+
142
+ #
143
+ # main program
144
+ #
145
+
146
+ libdir = $site_libdir
147
+ bindir = $bindir
148
+ no_linkify = false
149
+ linked = nil
150
+ help = false
151
+
152
+ usage = <<-usage
153
+ #{ File::basename $0 }
154
+ -d, --destdir <destdir>
155
+ -l, --libdir <libdir>
156
+ -b, --bindir <bindir>
157
+ -r, --ruby <ruby>
158
+ -n, --no_linkify
159
+ -s, --sudo
160
+ -h, --help
161
+ usage
162
+
163
+ begin
164
+ while switch = ARGV.switch
165
+ case switch
166
+ when '-d', '--destdir'
167
+ libdir = ARGV.req_arg
168
+ when '-l', '--libdir'
169
+ libdir = ARGV.req_arg
170
+ when '-b', '--bindir'
171
+ bindir = ARGV.req_arg
172
+ when '-r', '--ruby'
173
+ $ruby = ARGV.req_arg
174
+ when '-n', '--no_linkify'
175
+ no_linkify = true
176
+ when '-s', '--sudo'
177
+ sudo = 'sudo'
178
+ when '-h', '--help'
179
+ help = true
180
+ else
181
+ raise "unknown switch #{switch.dump}"
182
+ end
183
+ end
184
+ rescue
185
+ STDERR.puts $!.to_s
186
+ STDERR.puts usage
187
+ exit 1
188
+ end
189
+
190
+ if help
191
+ STDOUT.puts usage
192
+ exit
193
+ end
194
+
195
+ unless no_linkify
196
+ linked = linkify('lib') + linkify('bin')
197
+ end
198
+
199
+ system "#{ $ruby } extconf.rb && make && #{ sudo } make install" if test(?s, 'extconf.rb')
200
+
201
+ install_rb(LIBDIR, libdir, LIBDIR_MODE)
202
+ install_rb(BINDIR, bindir, BINDIR_MODE, bin=true)
203
+
204
+ if linked
205
+ linked.each{|path| File::rm_f path}
206
+ end
@@ -0,0 +1,299 @@
1
+ # vim: ts=2:sw=2:sts=2:et:fdm=marker
2
+ require 'tmpdir'
3
+ require 'socket'
4
+ require 'fileutils'
5
+ require 'rbconfig'
6
+ require 'thread'
7
+ require 'yaml'
8
+
9
+ class Object
10
+ def systemu(*a, &b) SystemUniversal.new(*a, &b).systemu end
11
+ end
12
+
13
+ class SystemUniversal
14
+ #
15
+ # constants
16
+ #
17
+ SystemUniversal::VERSION = '1.2.0' unless defined? SystemUniversal::VERSION
18
+ def version() SystemUniversal::VERSION end
19
+ #
20
+ # class methods
21
+ #
22
+
23
+ @host = Socket.gethostname
24
+ @ppid = Process.ppid
25
+ @pid = Process.pid
26
+ @turd = ENV['SYSTEMU_TURD']
27
+
28
+ c = ::Config::CONFIG
29
+ ruby = File.join(c['bindir'], c['ruby_install_name']) << c['EXEEXT']
30
+ @ruby = if system('%s -e 42' % ruby)
31
+ ruby
32
+ else
33
+ system('%s -e 42' % 'ruby') ? 'ruby' : warn('no ruby in PATH/CONFIG')
34
+ end
35
+
36
+ class << self
37
+ %w( host ppid pid ruby turd ).each{|a| attr_accessor a}
38
+ end
39
+
40
+ #
41
+ # instance methods
42
+ #
43
+
44
+ def initialize argv, opts = {}, &block
45
+ getopt = getopts opts
46
+
47
+ @argv = argv
48
+ @block = block
49
+
50
+ @stdin = getopt[ ['stdin', 'in', '0', 0] ]
51
+ @stdout = getopt[ ['stdout', 'out', '1', 1] ]
52
+ @stderr = getopt[ ['stderr', 'err', '2', 2] ]
53
+ @env = getopt[ 'env' ]
54
+ @cwd = getopt[ 'cwd' ]
55
+
56
+ @host = getopt[ 'host', self.class.host ]
57
+ @ppid = getopt[ 'ppid', self.class.ppid ]
58
+ @pid = getopt[ 'pid', self.class.pid ]
59
+ @ruby = getopt[ 'ruby', self.class.ruby ]
60
+ end
61
+
62
+ def systemu
63
+ tmpdir do |tmp|
64
+ c = child_setup tmp
65
+ status = nil
66
+
67
+ begin
68
+ thread = nil
69
+
70
+ quietly{
71
+ IO.popen "#{ @ruby } #{ c['program'] }", 'r+' do |pipe|
72
+ line = pipe.gets
73
+ case line
74
+ when %r/^pid: \d+$/
75
+ cid = Integer line[%r/\d+/]
76
+ else
77
+ begin
78
+ buf = pipe.read
79
+ buf = "#{ line }#{ buf }"
80
+ e = Marshal.load buf
81
+ raise unless Exception === e
82
+ raise e
83
+ rescue
84
+ raise "wtf?\n#{ buf }\n"
85
+ end
86
+ end
87
+ thread = new_thread cid, @block if @block
88
+ pipe.read rescue nil
89
+ end
90
+ }
91
+ status = $?
92
+ ensure
93
+ if thread
94
+ begin
95
+ class << status
96
+ attr 'thread'
97
+ end
98
+ status.instance_eval{ @thread = thread }
99
+ rescue
100
+ 42
101
+ end
102
+ end
103
+ end
104
+
105
+ if @stdout or @stderr
106
+ open(c['stdout']){|f| relay f => @stdout} if @stdout
107
+ open(c['stderr']){|f| relay f => @stderr} if @stderr
108
+ status
109
+ else
110
+ [status, IO.read(c['stdout']), IO.read(c['stderr'])]
111
+ end
112
+ end
113
+ end
114
+
115
+ def new_thread cid, block
116
+ q = Queue.new
117
+ Thread.new(cid) do |cid|
118
+ current = Thread.current
119
+ current.abort_on_exception = true
120
+ q.push current
121
+ block.call cid
122
+ end
123
+ q.pop
124
+ end
125
+
126
+ def child_setup tmp
127
+ stdin = File.expand_path(File.join(tmp, 'stdin'))
128
+ stdout = File.expand_path(File.join(tmp, 'stdout'))
129
+ stderr = File.expand_path(File.join(tmp, 'stderr'))
130
+ program = File.expand_path(File.join(tmp, 'program'))
131
+ config = File.expand_path(File.join(tmp, 'config'))
132
+
133
+ if @stdin
134
+ open(stdin, 'w'){|f| relay @stdin => f}
135
+ else
136
+ FileUtils.touch stdin
137
+ end
138
+ FileUtils.touch stdout
139
+ FileUtils.touch stderr
140
+
141
+ c = {}
142
+ c['argv'] = @argv
143
+ c['env'] = @env
144
+ c['cwd'] = @cwd
145
+ c['stdin'] = stdin
146
+ c['stdout'] = stdout
147
+ c['stderr'] = stderr
148
+ c['program'] = program
149
+ open(config, 'w'){|f| YAML.dump c, f}
150
+
151
+ open(program, 'w'){|f| f.write child_program(config)}
152
+
153
+ c
154
+ end
155
+
156
+ def quietly
157
+ v = $VERBOSE
158
+ $VERBOSE = nil
159
+ yield
160
+ ensure
161
+ $VERBOSE = v
162
+ end
163
+
164
+ def child_program config
165
+ <<-program
166
+ PIPE = STDOUT.dup
167
+ begin
168
+ require 'yaml'
169
+
170
+ config = YAML.load(IO.read('#{ config }'))
171
+
172
+ argv = config['argv']
173
+ env = config['env']
174
+ cwd = config['cwd']
175
+ stdin = config['stdin']
176
+ stdout = config['stdout']
177
+ stderr = config['stderr']
178
+
179
+ Dir.chdir cwd if cwd
180
+ env.each{|k,v| ENV[k.to_s] = v.to_s} if env
181
+
182
+ STDIN.reopen stdin
183
+ STDOUT.reopen stdout
184
+ STDERR.reopen stderr
185
+
186
+ PIPE.puts "pid: \#{ Process.pid }"
187
+ PIPE.flush ### the process is ready yo!
188
+ PIPE.close
189
+
190
+ exec *argv
191
+ rescue Exception => e
192
+ PIPE.write Marshal.dump(e) rescue nil
193
+ exit 42
194
+ end
195
+ program
196
+ end
197
+
198
+ def relay srcdst
199
+ src, dst, ignored = srcdst.to_a.first
200
+ if src.respond_to? 'read'
201
+ while((buf = src.read(8192))); dst << buf; end
202
+ else
203
+ src.each{|buf| dst << buf}
204
+ end
205
+ end
206
+
207
+ def tmpdir d = Dir.tmpdir, max = 42, &b
208
+ i = -1 and loop{
209
+ i += 1
210
+
211
+ tmp = File.join d, "systemu_#{ @host }_#{ @ppid }_#{ @pid }_#{ rand }_#{ i += 1 }"
212
+
213
+ begin
214
+ Dir.mkdir tmp
215
+ rescue Errno::EEXIST
216
+ raise if i >= max
217
+ next
218
+ end
219
+
220
+ break(
221
+ if b
222
+ begin
223
+ b.call tmp
224
+ ensure
225
+ FileUtils.rm_rf tmp unless SystemU.turd
226
+ end
227
+ else
228
+ tmp
229
+ end
230
+ )
231
+ }
232
+ end
233
+
234
+ def getopts opts = {}
235
+ lambda do |*args|
236
+ keys, default, ignored = args
237
+ catch('opt') do
238
+ [keys].flatten.each do |key|
239
+ [key, key.to_s, key.to_s.intern].each do |key|
240
+ throw 'opt', opts[key] if opts.has_key?(key)
241
+ end
242
+ end
243
+ default
244
+ end
245
+ end
246
+ end
247
+ end
248
+
249
+ SystemU = SystemUniversal unless defined? SystemU
250
+
251
+
252
+
253
+
254
+
255
+
256
+
257
+
258
+
259
+
260
+
261
+
262
+
263
+ if $0 == __FILE__
264
+ #
265
+ # date
266
+ #
267
+ date = %q( ruby -e" t = Time.now; STDOUT.puts t; STDERR.puts t " )
268
+
269
+ status, stdout, stderr = systemu date
270
+ p [status, stdout, stderr]
271
+
272
+ status = systemu date, 1=>(stdout = '')
273
+ p [status, stdout]
274
+
275
+ status = systemu date, 2=>(stderr = '')
276
+ p [status, stderr]
277
+ #
278
+ # sleep
279
+ #
280
+ sleep = %q( ruby -e" p(sleep(1)) " )
281
+ status, stdout, stderr = systemu sleep
282
+ p [status, stdout, stderr]
283
+
284
+ sleep = %q( ruby -e" p(sleep(42)) " )
285
+ status, stdout, stderr = systemu(sleep){|cid| Process.kill 9, cid}
286
+ p [status, stdout, stderr]
287
+ #
288
+ # env
289
+ #
290
+ env = %q( ruby -e" p ENV['A'] " )
291
+ status, stdout, stderr = systemu env, :env => {'A' => 42}
292
+ p [status, stdout, stderr]
293
+ #
294
+ # cwd
295
+ #
296
+ env = %q( ruby -e" p Dir.pwd " )
297
+ status, stdout, stderr = systemu env, :cwd => Dir.tmpdir
298
+ p [status, stdout, stderr]
299
+ end