Guest User

Untitled

a guest
Feb 28th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.75 KB | None | 0 0
  1. ## RingyDingy's ring_server
  2.  
  3. require 'rinda/ring'
  4. require 'rinda/tuplespace'
  5. require 'ringy_dingy'
  6. require 'optparse'
  7.  
  8. ##
  9. # RingyDingy::RingServer provides a friendly wrapper around Rinda::RingServer.
  10. #
  11. # When running on the command line, RingyDingy::RingServer's verbose mode may
  12. # be toggled remotely via the --set-verbose option.
  13. #
  14. # = Usage
  15. #
  16. # == Starting a RingServer
  17. #
  18. # From the command line:
  19. #
  20. # ring_server -d
  21. #
  22. # or from Ruby:
  23. #
  24. # RingyDingy::RingServer.new.run
  25. #
  26. # == List Services
  27. #
  28. # From the command line (after starting a RingServer):
  29. #
  30. # ring_server -l
  31. #
  32. # or from Ruby:
  33. #
  34. # RingyDingy::RingServer.list
  35. #
  36. # == Verbose mode
  37. #
  38. # Changing verbose mode for the server (when not a daemon).
  39. #
  40. # ring_server --set-verbose=true/false
  41. #
  42. # or from Ruby:
  43. #
  44. # RingyDingy::RingServer.set_verbose true/false
  45.  
  46. class RingyDingy::RingServer
  47.  
  48. ##
  49. # Process +args+ into an options Hash. See also #new.
  50.  
  51. def self.process_args(args)
  52. options = {}
  53. options[:Verbose] = false
  54.  
  55. op = OptionParser.new do |op|
  56. op.program_name = 'ring_server'
  57. op.version = RingyDingy::VERSION
  58. op.release = nil
  59.  
  60. op.banner = "Usage: #{name} [options]"
  61. op.separator ''
  62. op.separator 'Run, find, or modify the behavior of a Rinda::RingServer.'
  63. op.separator ''
  64. op.separator 'With no arguments a Rinda::RingServer is started and runs in the foreground.'
  65. op.separator ''
  66.  
  67. op.separator 'RingServer options:'
  68. op.on("-d", "--daemon",
  69. "Run a RingServer as a daemon") do |val|
  70. options[:Daemon] = val
  71. end
  72.  
  73. op.on("-v", "--verbose",
  74. "Enable verbose mode") do |val|
  75. options[:Verbose] = val
  76. end
  77.  
  78. op.separator ''
  79. op.separator 'Miscellaneous options:'
  80.  
  81. op.on("-l", "--list",
  82. "List services on available RingServers") do |val|
  83. options[:List] = val
  84. end
  85.  
  86. op.on( "--set-verbose=BOOLEAN", TrueClass,
  87. "Enable or disable verbose mode on available",
  88. "RingServers (except daemon RingServers)") do |val|
  89. options[:SetVerbose] = val
  90. end
  91. end
  92.  
  93. op.parse! args
  94.  
  95. return options
  96. end
  97.  
  98. ##
  99. # Run appropriately.
  100.  
  101. def self.run(args = ARGV)
  102. DRb.start_service unless DRb.primary_server
  103.  
  104. options = process_args args
  105.  
  106. if options.include? :List then
  107. print_services
  108. exit
  109. elsif options.include? :SetVerbose then
  110. set_verbose options[:SetVerbose]
  111. exit
  112. elsif options.include? :Daemon then
  113. require 'webrick/server'
  114. WEBrick::Daemon.start
  115. end
  116.  
  117. new(options).run
  118. end
  119.  
  120. ##
  121. # Prints usage message +message+ if present then OptionParser +op+.
  122.  
  123. def self.usage(op, message = nil)
  124. if message then
  125. $stderr.puts message
  126. $stderr.puts
  127. end
  128.  
  129. $stderr.puts op
  130. exit 1
  131. end
  132.  
  133. end
  134.  
  135. ## lib/tinderbox/gem_tinderbox.rb
  136.  
  137. $TESTING = false unless defined? $TESTING
  138. require 'tinderbox'
  139. require 'tinderbox/gem_runner'
  140. require 'tinderbox/build'
  141. require 'rubygems/source_info_cache'
  142.  
  143. require 'optparse'
  144. require 'rbconfig'
  145. require 'socket'
  146.  
  147. require 'rubygems'
  148. require 'firebrigade/cache'
  149.  
  150. class Tinderbox::GemTinderbox
  151.  
  152. attr_accessor :root
  153.  
  154. attr_accessor :timeout
  155.  
  156. def self.process_args(args)
  157. opts_file = File.expand_path '~/.gem_tinderbox'
  158. options = {}
  159.  
  160. if File.exist? opts_file then
  161. File.readlines(opts_file).map { |l| l.chomp.split '=', 2 }.each do |k,v|
  162. v = true if v == 'true'
  163. v = false if v == 'false'
  164. v = Integer(v) if k == 'Timeout'
  165. options[k.intern] = v
  166. end
  167. end
  168.  
  169. options[:Daemon] ||= false
  170. options[:Timeout] ||= 120
  171.  
  172. opts = OptionParser.new do |opts|
  173. opts.banner = "Usage: #{File.basename $0} [options]"
  174. opts.separator ''
  175. opts.separator 'Options may also be set in the options file ~/.gem_tinderbox.'
  176. opts.separator ''
  177. opts.separator 'Example ~/.gem_tinderbox'
  178. opts.separator "\tServer=firebrigade.example.com"
  179. opts.separator "\tUsername=my username"
  180. opts.separator "\tPassword=my password"
  181. opts.separator "\tRoot=/path/to/tinderbox/root"
  182.  
  183. opts.separator ''
  184.  
  185. opts.on("-s", "--server HOST",
  186. "Firebrigade server host",
  187. "Default: #{options[:Server].inspect}",
  188. "Options file name: Server") do |server|
  189. options[:Server] = server
  190. end
  191.  
  192. opts.on("-u", "--username USERNAME",
  193. "Firebrigade username",
  194. "Default: #{options[:Username].inspect}",
  195. "Options file name: Username") do |username|
  196. options[:Username] = username
  197. end
  198.  
  199. opts.on("-p", "--password PASSWORD",
  200. "Firebrigade password",
  201. "Default: Read from ~/.gem_tinderbox",
  202. "Options file name: Password") do |password|
  203. options[:Password] = password
  204. end
  205.  
  206. opts.separator ''
  207.  
  208. opts.on("-t", "--timeout TIMEOUT",
  209. "Maximum time to wait for a gem's tests to",
  210. "finish",
  211. "Default: #{options[:Timeout]}",
  212. Numeric) do |timeout|
  213. options[:Timeout] = timeout
  214. end
  215.  
  216. opts.on("-r", "--root ROOT",
  217. "Root directory for gem tinderbox",
  218. "Default: #{options[:Root]}",
  219. "Gems will be lit on fire here.") do |root|
  220. options[:Root] = root
  221. end
  222.  
  223. opts.on("-d", "--daemonize",
  224. "Run as a daemon process",
  225. "Default: #{options[:Daemon]}") do |daemon|
  226. options[:Daemon] = true
  227. end
  228. end
  229.  
  230. opts.version = Tinderbox::VERSION
  231. opts.release = nil
  232.  
  233. opts.parse! args
  234.  
  235. if options[:Server].nil? or
  236. options[:Username].nil? or
  237. options[:Password].nil? then
  238. $stderr.puts opts
  239. $stderr.puts
  240. $stderr.puts "Firebrigade Server not set" if options[:Server].nil?
  241. $stderr.puts "Firebrigade Username not set" if options[:Username].nil?
  242. $stderr.puts "Firebrigade Password not set" if options[:Password].nil?
  243. exit 1
  244. end
  245.  
  246. return options
  247. rescue OptionParser::ParseError
  248. $stderr.puts opts
  249. exit 1
  250. end
  251.  
  252. def self.run(args = ARGV)
  253. options = process_args args
  254.  
  255. tinderbox = new options[:Server], options[:Username], options[:Password]
  256. tinderbox.root = options[:Root]
  257. tinderbox.timeout = options[:Timeout]
  258.  
  259. if options[:Daemon] then
  260. require 'webrick/server'
  261. WEBrick::Daemon.start
  262. end
  263.  
  264. tinderbox.run
  265.  
  266. rescue Interrupt, SystemExit # ignore
  267. rescue Exception => e
  268. puts "#{e.message}(#{e.class}):"
  269. puts "\t#{e.backtrace.join "\n\t"}"
  270. exit 1
  271. end
  272.  
  273. end
Add Comment
Please, Sign In to add comment