elastic-mapreduce 0.0.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.
Files changed (60) hide show
  1. data/CHANGELOG +51 -0
  2. data/Gemfile +13 -0
  3. data/Gemfile.lock +16 -0
  4. data/LICENSE.txt +393 -0
  5. data/NOTICE.txt +26 -0
  6. data/README +1007 -0
  7. data/Rakefile +35 -0
  8. data/VERSION +1 -0
  9. data/bin/elastic-mapreduce +27 -0
  10. data/cacert.pem +280 -0
  11. data/elastic-mapreduce.gemspec +104 -0
  12. data/lib/amazon/aws/exceptions.rb +211 -0
  13. data/lib/amazon/coral/awsquery.rb +128 -0
  14. data/lib/amazon/coral/awsquerychainhelper.rb +92 -0
  15. data/lib/amazon/coral/awsqueryhandler.rb +170 -0
  16. data/lib/amazon/coral/awsqueryurihandler.rb +34 -0
  17. data/lib/amazon/coral/call.rb +68 -0
  18. data/lib/amazon/coral/dispatcher.rb +33 -0
  19. data/lib/amazon/coral/ec2client.rb +91 -0
  20. data/lib/amazon/coral/elasticmapreduceclient.rb +198 -0
  21. data/lib/amazon/coral/handler.rb +20 -0
  22. data/lib/amazon/coral/httpdelegationhelper.rb +27 -0
  23. data/lib/amazon/coral/httpdestinationhandler.rb +36 -0
  24. data/lib/amazon/coral/httphandler.rb +124 -0
  25. data/lib/amazon/coral/identityhandler.rb +32 -0
  26. data/lib/amazon/coral/job.rb +25 -0
  27. data/lib/amazon/coral/logfactory.rb +35 -0
  28. data/lib/amazon/coral/option.rb +70 -0
  29. data/lib/amazon/coral/orchestrator.rb +49 -0
  30. data/lib/amazon/coral/querystringmap.rb +93 -0
  31. data/lib/amazon/coral/service.rb +130 -0
  32. data/lib/amazon/coral/simplelog.rb +98 -0
  33. data/lib/amazon/coral/urlencoding.rb +19 -0
  34. data/lib/amazon/coral/v0signaturehandler.rb +33 -0
  35. data/lib/amazon/coral/v0signaturehelper.rb +83 -0
  36. data/lib/amazon/coral/v1signaturehandler.rb +32 -0
  37. data/lib/amazon/coral/v1signaturehelper.rb +58 -0
  38. data/lib/amazon/coral/v2signaturehandler.rb +46 -0
  39. data/lib/amazon/coral/v2signaturehelper.rb +76 -0
  40. data/lib/amazon/retry_delegator.rb +66 -0
  41. data/lib/amazon/stderr_logger.rb +23 -0
  42. data/lib/client.rb +117 -0
  43. data/lib/commands.rb +1690 -0
  44. data/lib/credentials.rb +86 -0
  45. data/lib/ec2_client_wrapper.rb +73 -0
  46. data/lib/json/lexer.rb +294 -0
  47. data/lib/json/objects.rb +200 -0
  48. data/lib/json.rb +58 -0
  49. data/lib/simple_executor.rb +11 -0
  50. data/lib/simple_logger.rb +38 -0
  51. data/lib/uuidtools/version.rb +32 -0
  52. data/lib/uuidtools.rb +655 -0
  53. data/run_tests.rb +8 -0
  54. data/samples/freebase/code/freebase_jobflow.json +44 -0
  55. data/samples/similarity/lastfm_jobflow.json +78 -0
  56. data/samples/wordSplitter.py +18 -0
  57. data/tests/commands_test.rb +587 -0
  58. data/tests/credentials.json +7 -0
  59. data/tests/example.json +14 -0
  60. metadata +154 -0
data/lib/uuidtools.rb ADDED
@@ -0,0 +1,655 @@
1
+ #--
2
+ # Copyright (c) 2005-2008 Robert Aman
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ $:.unshift(File.dirname(__FILE__))
25
+
26
+ require 'uri'
27
+ require 'time'
28
+ require 'thread'
29
+ require 'digest/sha1'
30
+ require 'digest/md5'
31
+
32
+ require 'uuidtools/version'
33
+
34
+ # Backwards compatibility with old method of versioning.
35
+ UUID_TOOLS_VERSION = UUID::UUID_TOOLS_VERSION::STRING
36
+
37
+ # Because it's impossible to hype a UUID generator on its genuine merits,
38
+ # I give you... Really bad ASCII art in the comments:
39
+ #
40
+ #
41
+ # \
42
+ # /
43
+ # +
44
+ # ]
45
+ # ]
46
+ # |
47
+ # /
48
+ # Mp___
49
+ # `~0NNp,
50
+ # __ggM'
51
+ # g0M~"`
52
+ # ]0M*-
53
+ #
54
+ # ___
55
+ # _g000M00g,
56
+ # j0M~ ~M&
57
+ # j0M" ~N,
58
+ # j0P M&
59
+ # jM 1
60
+ # j0 ]1
61
+ # .0P 0,
62
+ # 00' M&
63
+ # 0M ]0L
64
+ # ]0f ___ M0
65
+ # M0NN0M00MMM~"'M 0&
66
+ # `~ ~0 ]0,
67
+ # ]M ]0&
68
+ # M& M0,
69
+ # ____gp_ M& M0_
70
+ # __p0MPM8MM&_ M/ ^0&_
71
+ # gN"` M0N_j0, MM&__
72
+ # _gF `~M0P` __ M00g
73
+ # g0' gM0&, ~M0&
74
+ # _pM` 0, ]M1 "00&
75
+ # _00 /g1MMgj01 ]0MI
76
+ # _0F t"M,7MMM 00I
77
+ # g0' _ N&j& 40'
78
+ # g0' _p0Mq_ ' N0QQNM#g,
79
+ # 0' _g0000000g__ ~M@MMM000g
80
+ # f _jM00@` ~M0000Mgppg, "P00&
81
+ # | g000~ `~M000000&_ ~0&
82
+ # ]M _M00F "00MM` ~#&
83
+ # `0L m000F #E "0f
84
+ # 9r j000M` 40, 00
85
+ # ]0g_ j00M` ^M0MNggp#gqpg M0&
86
+ # ~MPM0f ~M000000000g_ ,_ygg&M00f
87
+ # `~~~M00000000000000
88
+ # `M0000000000f
89
+ # ~@@@MF~`
90
+ #
91
+ #
92
+
93
+ #= uuidtools.rb
94
+ #
95
+ # UUIDTools was designed to be a simple library for generating any
96
+ # of the various types of UUIDs. It conforms to RFC 4122 whenever
97
+ # possible.
98
+ #
99
+ #== Example
100
+ # UUID.md5_create(UUID_DNS_NAMESPACE, "www.widgets.com")
101
+ # => #<UUID:0x287576 UUID:3d813cbb-47fb-32ba-91df-831e1593ac29>
102
+ # UUID.sha1_create(UUID_DNS_NAMESPACE, "www.widgets.com")
103
+ # => #<UUID:0x2a0116 UUID:21f7f8de-8051-5b89-8680-0195ef798b6a>
104
+ # UUID.timestamp_create
105
+ # => #<UUID:0x2adfdc UUID:64a5189c-25b3-11da-a97b-00c04fd430c8>
106
+ # UUID.random_create
107
+ # => #<UUID:0x19013a UUID:984265dc-4200-4f02-ae70-fe4f48964159>
108
+ class UUID
109
+ @@mac_address = nil
110
+ @@last_timestamp = nil
111
+ @@last_node_id = nil
112
+ @@last_clock_sequence = nil
113
+ @@state_file = nil
114
+ @@mutex = Mutex.new
115
+
116
+ def initialize(time_low, time_mid, time_hi_and_version,
117
+ clock_seq_hi_and_reserved, clock_seq_low, nodes)
118
+ unless time_low >= 0 && time_low < 4294967296
119
+ raise ArgumentError,
120
+ "Expected unsigned 32-bit number for time_low, got #{time_low}."
121
+ end
122
+ unless time_mid >= 0 && time_mid < 65536
123
+ raise ArgumentError,
124
+ "Expected unsigned 16-bit number for time_mid, got #{time_mid}."
125
+ end
126
+ unless time_hi_and_version >= 0 && time_hi_and_version < 65536
127
+ raise ArgumentError,
128
+ "Expected unsigned 16-bit number for time_hi_and_version, " +
129
+ "got #{time_hi_and_version}."
130
+ end
131
+ unless clock_seq_hi_and_reserved >= 0 && clock_seq_hi_and_reserved < 256
132
+ raise ArgumentError,
133
+ "Expected unsigned 8-bit number for clock_seq_hi_and_reserved, " +
134
+ "got #{clock_seq_hi_and_reserved}."
135
+ end
136
+ unless clock_seq_low >= 0 && clock_seq_low < 256
137
+ raise ArgumentError,
138
+ "Expected unsigned 8-bit number for clock_seq_low, " +
139
+ "got #{clock_seq_low}."
140
+ end
141
+ unless nodes.respond_to? :size
142
+ raise ArgumentError,
143
+ "Expected nodes to respond to :size."
144
+ end
145
+ unless nodes.size == 6
146
+ raise ArgumentError,
147
+ "Expected nodes to have size of 6."
148
+ end
149
+ for node in nodes
150
+ unless node >= 0 && node < 256
151
+ raise ArgumentError,
152
+ "Expected unsigned 8-bit number for each node, " +
153
+ "got #{node}."
154
+ end
155
+ end
156
+ @time_low = time_low
157
+ @time_mid = time_mid
158
+ @time_hi_and_version = time_hi_and_version
159
+ @clock_seq_hi_and_reserved = clock_seq_hi_and_reserved
160
+ @clock_seq_low = clock_seq_low
161
+ @nodes = nodes
162
+ end
163
+
164
+ attr_accessor :time_low
165
+ attr_accessor :time_mid
166
+ attr_accessor :time_hi_and_version
167
+ attr_accessor :clock_seq_hi_and_reserved
168
+ attr_accessor :clock_seq_low
169
+ attr_accessor :nodes
170
+
171
+ # Parses a UUID from a string.
172
+ def self.parse(uuid_string)
173
+ unless uuid_string.kind_of? String
174
+ raise ArgumentError,
175
+ "Expected String, got #{uuid_string.class.name} instead."
176
+ end
177
+ uuid_components = uuid_string.downcase.scan(
178
+ Regexp.new("^([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-" +
179
+ "([0-9a-f]{2})([0-9a-f]{2})-([0-9a-f]{12})$")).first
180
+ raise ArgumentError, "Invalid UUID format." if uuid_components.nil?
181
+ time_low = uuid_components[0].to_i(16)
182
+ time_mid = uuid_components[1].to_i(16)
183
+ time_hi_and_version = uuid_components[2].to_i(16)
184
+ clock_seq_hi_and_reserved = uuid_components[3].to_i(16)
185
+ clock_seq_low = uuid_components[4].to_i(16)
186
+ nodes = []
187
+ for i in 0..5
188
+ nodes << uuid_components[5][(i * 2)..(i * 2) + 1].to_i(16)
189
+ end
190
+ return self.new(time_low, time_mid, time_hi_and_version,
191
+ clock_seq_hi_and_reserved, clock_seq_low, nodes)
192
+ end
193
+
194
+ # Parses a UUID from a raw byte string.
195
+ def self.parse_raw(raw_string)
196
+ unless raw_string.kind_of? String
197
+ raise ArgumentError,
198
+ "Expected String, got #{raw_string.class.name} instead."
199
+ end
200
+ integer = self.convert_byte_string_to_int(raw_string)
201
+
202
+ time_low = (integer >> 96) & 0xFFFFFFFF
203
+ time_mid = (integer >> 80) & 0xFFFF
204
+ time_hi_and_version = (integer >> 64) & 0xFFFF
205
+ clock_seq_hi_and_reserved = (integer >> 56) & 0xFF
206
+ clock_seq_low = (integer >> 48) & 0xFF
207
+ nodes = []
208
+ for i in 0..5
209
+ nodes << ((integer >> (40 - (i * 8))) & 0xFF)
210
+ end
211
+ return self.new(time_low, time_mid, time_hi_and_version,
212
+ clock_seq_hi_and_reserved, clock_seq_low, nodes)
213
+ end
214
+
215
+ # Creates a UUID from a random value.
216
+ def self.random_create()
217
+ new_uuid = self.parse_raw(self.random_128)
218
+ new_uuid.time_hi_and_version &= 0x0FFF
219
+ new_uuid.time_hi_and_version |= (4 << 12)
220
+ new_uuid.clock_seq_hi_and_reserved &= 0x3F
221
+ new_uuid.clock_seq_hi_and_reserved |= 0x80
222
+ return new_uuid
223
+ end
224
+
225
+ # Creates a UUID from a timestamp.
226
+ def self.timestamp_create(timestamp=nil)
227
+ # We need a lock here to prevent two threads from ever
228
+ # getting the same timestamp.
229
+ @@mutex.synchronize do
230
+ # Always use GMT to generate UUIDs.
231
+ if timestamp.nil?
232
+ gmt_timestamp = Time.now.gmtime
233
+ else
234
+ gmt_timestamp = timestamp.gmtime
235
+ end
236
+ # Convert to 100 nanosecond blocks
237
+ gmt_timestamp_100_nanoseconds = (gmt_timestamp.tv_sec * 10000000) +
238
+ (gmt_timestamp.tv_usec * 10) + 0x01B21DD213814000
239
+ mac_address = self.get_mac_address
240
+ if mac_address == nil || mac_address == ""
241
+ raise StandardError,
242
+ "MAC address could not be autodetected. " +
243
+ "Set the MAC address manually."
244
+ end
245
+ nodes = mac_address.split(":").collect do |octet|
246
+ octet.to_i(16)
247
+ end
248
+ node_id = 0
249
+ for i in 0..5
250
+ node_id += (nodes[i] << (40 - (i * 8)))
251
+ end
252
+ clock_sequence = @@last_clock_sequence
253
+ if clock_sequence.nil?
254
+ clock_sequence = self.convert_byte_string_to_int(self.random_128)
255
+ end
256
+ if @@last_node_id != nil && @@last_node_id != node_id
257
+ # The node id has changed. Change the clock id.
258
+ clock_sequence = self.convert_byte_string_to_int(self.random_128)
259
+ elsif @@last_timestamp != nil &&
260
+ gmt_timestamp_100_nanoseconds <= @@last_timestamp
261
+ clock_sequence = clock_sequence + 1
262
+ end
263
+ @@last_timestamp = gmt_timestamp_100_nanoseconds
264
+ @@last_node_id = node_id
265
+ @@last_clock_sequence = clock_sequence
266
+
267
+ time_low = gmt_timestamp_100_nanoseconds & 0xFFFFFFFF
268
+ time_mid = ((gmt_timestamp_100_nanoseconds >> 32) & 0xFFFF)
269
+ time_hi_and_version = ((gmt_timestamp_100_nanoseconds >> 48) & 0x0FFF)
270
+ time_hi_and_version |= (1 << 12)
271
+ clock_seq_low = clock_sequence & 0xFF;
272
+ clock_seq_hi_and_reserved = (clock_sequence & 0x3F00) >> 8
273
+ clock_seq_hi_and_reserved |= 0x80
274
+
275
+ return self.new(time_low, time_mid, time_hi_and_version,
276
+ clock_seq_hi_and_reserved, clock_seq_low, nodes)
277
+ end
278
+ end
279
+
280
+ # Creates a UUID using the MD5 hash. (Version 3)
281
+ def self.md5_create(namespace, name)
282
+ return self.create_from_hash(Digest::MD5, namespace, name)
283
+ end
284
+
285
+ # Creates a UUID using the SHA1 hash. (Version 5)
286
+ def self.sha1_create(namespace, name)
287
+ return self.create_from_hash(Digest::SHA1, namespace, name)
288
+ end
289
+
290
+ # This method applies only to version 1 UUIDs.
291
+ # Checks if the node ID was generated from a random number
292
+ # or from an IEEE 802 address (MAC address).
293
+ # Always returns false for UUIDs that aren't version 1.
294
+ # This should not be confused with version 4 UUIDs where
295
+ # more than just the node id is random.
296
+ def random_node_id?
297
+ return false if self.version != 1
298
+ return ((self.nodes.first & 0x01) == 1)
299
+ end
300
+
301
+ # Returns true if this UUID is the
302
+ # nil UUID (00000000-0000-0000-0000-000000000000).
303
+ def nil_uuid?
304
+ return false if self.time_low != 0
305
+ return false if self.time_mid != 0
306
+ return false if self.time_hi_and_version != 0
307
+ return false if self.clock_seq_hi_and_reserved != 0
308
+ return false if self.clock_seq_low != 0
309
+ self.nodes.each do |node|
310
+ return false if node != 0
311
+ end
312
+ return true
313
+ end
314
+
315
+ # Returns the UUID version type.
316
+ # Possible values:
317
+ # 1 - Time-based with unique or random host identifier
318
+ # 2 - DCE Security version (with POSIX UIDs)
319
+ # 3 - Name-based (MD5 hash)
320
+ # 4 - Random
321
+ # 5 - Name-based (SHA-1 hash)
322
+ def version
323
+ return (time_hi_and_version >> 12)
324
+ end
325
+
326
+ # Returns the UUID variant.
327
+ # Possible values:
328
+ # 0b000 - Reserved, NCS backward compatibility.
329
+ # 0b100 - The variant specified in this document.
330
+ # 0b110 - Reserved, Microsoft Corporation backward compatibility.
331
+ # 0b111 - Reserved for future definition.
332
+ def variant
333
+ variant_raw = (clock_seq_hi_and_reserved >> 5)
334
+ result = nil
335
+ if (variant_raw >> 2) == 0
336
+ result = 0x000
337
+ elsif (variant_raw >> 1) == 2
338
+ result = 0x100
339
+ else
340
+ result = variant_raw
341
+ end
342
+ return (result >> 6)
343
+ end
344
+
345
+ # Returns true if this UUID is valid.
346
+ def valid?
347
+ if [0b000, 0b100, 0b110, 0b111].include?(self.variant) &&
348
+ (1..5).include?(self.version)
349
+ return true
350
+ else
351
+ return false
352
+ end
353
+ end
354
+
355
+ # Returns the IEEE 802 address used to generate this UUID or
356
+ # nil if a MAC address was not used.
357
+ def mac_address
358
+ return nil if self.version != 1
359
+ return nil if self.random_node_id?
360
+ return (self.nodes.collect do |node|
361
+ sprintf("%2.2x", node)
362
+ end).join(":")
363
+ end
364
+
365
+ # Returns the timestamp used to generate this UUID
366
+ def timestamp
367
+ return nil if self.version != 1
368
+ gmt_timestamp_100_nanoseconds = 0
369
+ gmt_timestamp_100_nanoseconds +=
370
+ ((self.time_hi_and_version & 0x0FFF) << 48)
371
+ gmt_timestamp_100_nanoseconds += (self.time_mid << 32)
372
+ gmt_timestamp_100_nanoseconds += self.time_low
373
+ return Time.at(
374
+ (gmt_timestamp_100_nanoseconds - 0x01B21DD213814000) / 10000000.0)
375
+ end
376
+
377
+ # Compares two UUIDs lexically
378
+ def <=>(other_uuid)
379
+ check = self.time_low <=> other_uuid.time_low
380
+ return check if check != 0
381
+ check = self.time_mid <=> other_uuid.time_mid
382
+ return check if check != 0
383
+ check = self.time_hi_and_version <=> other_uuid.time_hi_and_version
384
+ return check if check != 0
385
+ check = self.clock_seq_hi_and_reserved <=>
386
+ other_uuid.clock_seq_hi_and_reserved
387
+ return check if check != 0
388
+ check = self.clock_seq_low <=> other_uuid.clock_seq_low
389
+ return check if check != 0
390
+ for i in 0..5
391
+ if (self.nodes[i] < other_uuid.nodes[i])
392
+ return -1
393
+ end
394
+ if (self.nodes[i] > other_uuid.nodes[i])
395
+ return 1
396
+ end
397
+ end
398
+ return 0
399
+ end
400
+
401
+ # Returns a representation of the object's state
402
+ def inspect
403
+ return "#<UUID:0x#{self.object_id.to_s(16)} UUID:#{self.to_s}>"
404
+ end
405
+
406
+ # Returns the hex digest of the UUID object.
407
+ def hexdigest
408
+ return self.to_i.to_s(16)
409
+ end
410
+
411
+ # Returns the raw bytes that represent this UUID.
412
+ def raw
413
+ return self.class.convert_int_to_byte_string(self.to_i, 16)
414
+ end
415
+
416
+ # Returns a string representation for this UUID.
417
+ def to_s
418
+ result = sprintf("%8.8x-%4.4x-%4.4x-%2.2x%2.2x-", @time_low, @time_mid,
419
+ @time_hi_and_version, @clock_seq_hi_and_reserved, @clock_seq_low);
420
+ for i in 0..5
421
+ result << sprintf("%2.2x", @nodes[i])
422
+ end
423
+ return result.downcase
424
+ end
425
+
426
+ # Returns an integer representation for this UUID.
427
+ def to_i
428
+ bytes = (time_low << 96) + (time_mid << 80) +
429
+ (time_hi_and_version << 64) + (clock_seq_hi_and_reserved << 56) +
430
+ (clock_seq_low << 48)
431
+ for i in 0..5
432
+ bytes += (nodes[i] << (40 - (i * 8)))
433
+ end
434
+ return bytes
435
+ end
436
+
437
+ # Returns a URI string for this UUID.
438
+ def to_uri
439
+ return "urn:uuid:#{self.to_s}"
440
+ end
441
+
442
+ # Returns an integer hash value.
443
+ def hash
444
+ return self.to_i
445
+ end
446
+
447
+ # Returns true if this UUID is exactly equal to the other UUID.
448
+ def eql?(other)
449
+ return (self <=> other) == 0
450
+ end
451
+
452
+ def self.create_from_hash(hash_class, namespace, name) #:nodoc:
453
+ if hash_class == Digest::MD5
454
+ version = 3
455
+ elsif hash_class == Digest::SHA1
456
+ version = 5
457
+ else
458
+ raise ArgumentError,
459
+ "Expected Digest::SHA1 or Digest::MD5, got #{hash_class.name}."
460
+ end
461
+ hash = hash_class.new
462
+ hash.update(namespace.raw)
463
+ hash.update(name)
464
+ hash_string = hash.to_s[0..31]
465
+ new_uuid = self.parse("#{hash_string[0..7]}-#{hash_string[8..11]}-" +
466
+ "#{hash_string[12..15]}-#{hash_string[16..19]}-#{hash_string[20..31]}")
467
+
468
+ new_uuid.time_hi_and_version &= 0x0FFF
469
+ new_uuid.time_hi_and_version |= (version << 12)
470
+ new_uuid.clock_seq_hi_and_reserved &= 0x3F
471
+ new_uuid.clock_seq_hi_and_reserved |= 0x80
472
+ return new_uuid
473
+ end
474
+
475
+ # Returns the MAC address of the current computer's network card.
476
+ # Returns nil if a MAC address could not be found.
477
+ def self.mac_address #:nodoc:
478
+ if @@mac_address.nil?
479
+ require 'rbconfig'
480
+ os_platform = Config::CONFIG['target_os']
481
+ if os_platform =~ /win/ && !(os_platform =~ /darwin/)
482
+ script_in_path = true
483
+ else
484
+ script_in_path = !(`which ifconfig`.strip =~ /no .+ in/)
485
+ end
486
+ if os_platform =~ /solaris/
487
+ begin
488
+ ifconfig_output =
489
+ (script_in_path ? `ifconfig -a` : `/sbin/ifconfig -a`)
490
+ ip_addresses = ifconfig_output.scan(
491
+ /inet\s?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/)
492
+ ip = ip_addresses.find {|addr| addr[0] != '127.0.0.1'}[0]
493
+ @@mac_address = `/usr/sbin/arp #{ip}`.split(' ')[3]
494
+ rescue Exception
495
+ end
496
+ if @@mac_address == "" || @@mac_address == nil
497
+ begin
498
+ ifconfig_output =
499
+ (script_in_path ?
500
+ `ifconfig -a` : `/sbin/ifconfig -a`).split(' ')
501
+ index = ifconfig_output.index("inet") + 1
502
+ ip = ifconfig_output[index]
503
+ @@mac_address = `arp #{ip}`.split(' ')[3]
504
+ rescue Exception
505
+ end
506
+ end
507
+ elsif os_platform =~ /win/ && !(os_platform =~ /darwin/)
508
+ begin
509
+ ifconfig_output = `ipconfig /all`
510
+ mac_addresses = ifconfig_output.scan(
511
+ Regexp.new("(#{(["[0-9a-fA-F]{2}"] * 6).join("-")})"))
512
+ if mac_addresses.size > 0
513
+ @@mac_address = mac_addresses.first.first.downcase.gsub(/-/, ":")
514
+ end
515
+ rescue
516
+ end
517
+ else
518
+ begin
519
+ mac_addresses = []
520
+ if os_platform =~ /netbsd/
521
+ ifconfig_output =
522
+ (script_in_path ? `ifconfig -a 2>&1` : `/sbin/ifconfig -a 2>&1`)
523
+ mac_addresses = ifconfig_output.scan(
524
+ Regexp.new("address\: (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"))
525
+ elsif os_platform =~ /openbsd/
526
+ ifconfig_output = `/sbin/ifconfig -a 2>&1`
527
+ ifconfig_output =
528
+ (script_in_path ? `ifconfig -a 2>&1` : `/sbin/ifconfig -a 2>&1`)
529
+ mac_addresses = ifconfig_output.scan(
530
+ Regexp.new("addr (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"))
531
+ elsif File.exists?('/sbin/ifconfig')
532
+ ifconfig_output =
533
+ (script_in_path ? `ifconfig 2>&1` : `/sbin/ifconfig 2>&1`)
534
+ mac_addresses = ifconfig_output.scan(
535
+ Regexp.new("ether (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"))
536
+ if mac_addresses.size == 0
537
+ ifconfig_output =
538
+ (script_in_path ?
539
+ `ifconfig -a 2>&1` : `/sbin/ifconfig -a 2>&1`)
540
+ mac_addresses = ifconfig_output.scan(
541
+ Regexp.new("ether (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"))
542
+ end
543
+ if mac_addresses.size == 0
544
+ ifconfig_output =
545
+ (script_in_path ?
546
+ `ifconfig | grep HWaddr | cut -c39- 2>&1` :
547
+ `/sbin/ifconfig | grep HWaddr | cut -c39- 2>&1`)
548
+ mac_addresses = ifconfig_output.scan(
549
+ Regexp.new("(#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"))
550
+ end
551
+ else
552
+ ifconfig_output =
553
+ (script_in_path ? `ifconfig 2>&1` : `/sbin/ifconfig 2>&1`)
554
+ mac_addresses = ifconfig_output.scan(
555
+ Regexp.new("ether (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"))
556
+ if mac_addresses.size == 0
557
+ ifconfig_output =
558
+ (script_in_path ?
559
+ `ifconfig -a 2>&1` : `/sbin/ifconfig -a 2>&1`)
560
+ mac_addresses = ifconfig_output.scan(
561
+ Regexp.new("ether (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"))
562
+ end
563
+ if mac_addresses.size == 0
564
+ ifconfig_output =
565
+ (script_in_path ?
566
+ `ifconfig | grep HWaddr | cut -c39- 2>&1` :
567
+ `/sbin/ifconfig | grep HWaddr | cut -c39- 2>&1`)
568
+ mac_addresses = ifconfig_output.scan(
569
+ Regexp.new("(#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"))
570
+ end
571
+ end
572
+ if mac_addresses.size > 0
573
+ @@mac_address = mac_addresses.first.first
574
+ end
575
+ rescue
576
+ end
577
+ end
578
+ if @@mac_address != nil
579
+ if @@mac_address.respond_to?(:to_str)
580
+ @@mac_address = @@mac_address.to_str
581
+ else
582
+ @@mac_address = @@mac_address.to_s
583
+ end
584
+ @@mac_address.downcase!
585
+ @@mac_address.strip!
586
+ end
587
+
588
+ # Verify that the MAC address is in the right format.
589
+ # Nil it out if it isn't.
590
+ unless @@mac_address.respond_to?(:scan) &&
591
+ @@mac_address.scan(/#{(["[0-9a-f]{2}"] * 6).join(":")}/)
592
+ @@mac_address = nil
593
+ end
594
+ end
595
+ return @@mac_address
596
+ end
597
+ class <<self
598
+ alias_method :get_mac_address, :mac_address
599
+ end
600
+
601
+ # Allows users to set the MAC address manually in cases where the MAC
602
+ # address cannot be obtained programatically.
603
+ def self.mac_address=(new_mac_address)
604
+ @@mac_address = new_mac_address
605
+ end
606
+
607
+ # 128 bits of unpredictable data.
608
+ def self.random_128 #:nodoc:
609
+ if !defined?(@random_device) || @random_device == nil
610
+ begin
611
+ @random_device = nil
612
+ if File.exists? "/dev/urandom"
613
+ @random_device = File.open "/dev/urandom", "r"
614
+ elsif File.exists? "/dev/random"
615
+ @random_device = File.open "/dev/random", "r"
616
+ end
617
+ rescue Exception
618
+ end
619
+ end
620
+ begin
621
+ return @random_device.read(16) if @random_device != nil
622
+ rescue Exception
623
+ end
624
+ return (1..8).to_a.map { rand(0x10000) }.pack("n8")
625
+ end
626
+
627
+ def self.convert_int_to_byte_string(integer, size) #:nodoc:
628
+ byte_string = ""
629
+ if byte_string.respond_to?(:force_encoding)
630
+ byte_string.force_encoding(Encoding::ASCII_8BIT)
631
+ end
632
+ for i in 0..(size - 1)
633
+ byte_string << ((integer >> (((size - 1) - i) * 8)) & 0xFF)
634
+ end
635
+ return byte_string
636
+ end
637
+
638
+ def self.convert_byte_string_to_int(byte_string) #:nodoc:
639
+ if byte_string.respond_to?(:force_encoding)
640
+ byte_string.force_encoding(Encoding::ASCII_8BIT)
641
+ end
642
+ integer = 0
643
+ size = byte_string.size
644
+ for i in 0..(size - 1)
645
+ ordinal = (byte_string[i].respond_to?(:ord) ? byte_string[i].ord : byte_string[i])
646
+ integer += (ordinal << (((size - 1) - i) * 8))
647
+ end
648
+ return integer
649
+ end
650
+ end
651
+
652
+ UUID_DNS_NAMESPACE = UUID.parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
653
+ UUID_URL_NAMESPACE = UUID.parse("6ba7b811-9dad-11d1-80b4-00c04fd430c8")
654
+ UUID_OID_NAMESPACE = UUID.parse("6ba7b812-9dad-11d1-80b4-00c04fd430c8")
655
+ UUID_X500_NAMESPACE = UUID.parse("6ba7b814-9dad-11d1-80b4-00c04fd430c8")
data/run_tests.rb ADDED
@@ -0,0 +1,8 @@
1
+ #
2
+ # Copyright 2008-2010 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+
4
+ $LOAD_PATH << File.dirname(__FILE__)
5
+
6
+ require 'tests/commands_test'
7
+
8
+