class Roma::Mkconfig
Constants
- BNUM_COEFFICIENT
- CONFIG_OUT_PATH
- CONFIG_TEMPLATE_PATH
- DEFAULT_ROMA_CONNECTION
- END_MSG
- GB
- JAVA_CONNECTION
- KB
- LIB_PATH
- OS_MEMORY_SIZE
- PHP_CONNECTION
- PLUGIN_DIR
- REDUNDANCY
- RUBY_CONNECTION
- TC_FILE
- TREE_TOP
Public Class Methods
new(mode = :no_menu)
click to toggle source
# File lib/roma/tools/mkconfig.rb 317 def initialize(mode = :no_menu) 318 # confirming overwrite 319 if File.exist?(CONFIG_OUT_PATH) 320 print("Config.rb already exist in current directory. \nWill you overwrite?[y/n]") 321 if gets.chomp! != "y" 322 p "config.rb were not created!" 323 exit 324 end 325 end 326 327 @base = Base.new 328 @results = Hash::new 329 @next_hash = TREE_TOP 330 331 begin 332 @defaults = load_config([:STORAGE_CLASS, :STORAGE_OPTION, :PLUGIN_FILES]) 333 rescue LoadError 334 puts 'Not found config.rb file.' 335 return 336 rescue 337 p $! 338 puts "Content of config.rb is wrong." 339 return 340 end 341 mkconfig(mode) 342 end
Public Instance Methods
ch_assign(text, exp, sep = " = ", str)
click to toggle source
sep means separating right and left part(config.rb style)
# File lib/roma/tools/mkconfig.rb 577 def ch_assign(text, exp, sep = " = ", str) 578 sep = " = " if sep == "=" 579 text = text.gsub(/(\s*#{exp}).*/) do |s| 580 name = $1 581 if str.class == String 582 if str =~ /::/ || str =~ /^\d+$/ 583 # storage type 584 name + sep + str 585 else 586 # require & storage option 587 name + sep + str.inspect 588 end 589 else 590 # plugin 591 # "to_s" equal "inspect" in Ruby 1.9 592 name + sep + str.to_s.sub("\\", "") 593 end 594 end 595 end
clear_screen()
click to toggle source
# File lib/roma/tools/mkconfig.rb 386 def clear_screen 387 print "\e[H\e[2J" 388 end
correct_in?(hash,input)
click to toggle source
# File lib/roma/tools/mkconfig.rb 452 def correct_in?(hash,input) 453 if END_MSG.include?(input) 454 return true 455 end 456 457 if hash["next"] == "continue" 458 if (input == "y" || input == "n") 459 return true 460 end 461 else 462 if hash.key?('choice') 463 if hash['choice'].count >= input.to_i && input.to_i > 0 464 return true 465 end 466 else 467 if hash["float_flg"] 468 if 0 < input.to_f 469 return true 470 end 471 else 472 if 0 < input.to_i 473 return true 474 end 475 end 476 end 477 end 478 479 return false 480 end
end?(s)
click to toggle source
judge whether data inputting finish or not
# File lib/roma/tools/mkconfig.rb 422 def end?(s) 423 if s == "END" 424 save_data(@results) 425 true 426 end 427 end
get_input(hash)
click to toggle source
# File lib/roma/tools/mkconfig.rb 437 def get_input(hash) 438 receiver = Input.new 439 input = "" 440 441 while !correct_in?(hash,input) 442 input = receiver.get_line 443 if input == "" 444 #set defaults value 445 input = hash["default"] 446 end 447 end 448 449 input 450 end
load_config(targets)
click to toggle source
# File lib/roma/tools/mkconfig.rb 344 def load_config(targets) 345 require CONFIG_TEMPLATE_PATH 346 d_value = Hash.new 347 Config.constants.each do |cnst| 348 if targets.include?(cnst) 349 d_value[cnst] = Config.const_get(cnst) 350 end 351 end 352 return d_value 353 end
mkconfig(mode)
click to toggle source
# File lib/roma/tools/mkconfig.rb 355 def mkconfig(mode) 356 skip = skip_menu!(mode) 357 358 while true 359 clear_screen 360 361 if @next_hash == "add_plugin" 362 @results["plugin"].value.unshift("plugin_storage.rb") unless @results["plugin"].value.include?("plugin_storage.rb") 363 @next_hash = "menu" 364 end 365 366 skip.call if @next_hash == "menu" || @next_hash == "server" || @next_hash == "fd_server" || @next_hash == "check_plugin" 367 break if end?(@base[@next_hash]) 368 puts "if you doesn't input anything, default value is set." 369 Box.print_with_box(@defaults) 370 print_status(@results) 371 @base.print_question(@next_hash) 372 input = get_input(@base[@next_hash]) 373 374 # if specific words(balse, exit, quit) was inputed, mkconfig.rb was finished. 375 if END_MSG.include?(input) 376 p "config.rb were not created!" 377 break 378 379 else 380 @results = store_result(@results, @base, @next_hash, input) 381 @next_hash = @base.next(@next_hash, input) 382 end 383 end 384 end
print_status(results)
click to toggle source
# File lib/roma/tools/mkconfig.rb 429 def print_status(results) 430 strs = Array.new 431 results.each_value do |v| 432 strs << "#{v.name} : #{v.value}" 433 end 434 Box.print_with_box(strs) 435 end
re_require(path, c_obj)
click to toggle source
# File lib/roma/tools/mkconfig.rb 597 def re_require(path, c_obj) 598 $".delete(File.expand_path(path)) 599 c_obj.constants.each do |cnst| 600 c_obj.class_eval { remove_const cnst } 601 end 602 require path 603 end
save_data(res)
click to toggle source
make config.rb based on input data
# File lib/roma/tools/mkconfig.rb 511 def save_data(res) 512 if res.key?("storage") 513 case res["storage"].value 514 when "Ruby Hash" 515 req = "rh_storage" 516 storage = "RubyHashStorage" 517 when "Tokyo Cabinet" 518 req = "tc_storage" 519 storage = "TCStorage" 520 bnum = Calculate.get_bnum(res) 521 bnum = 5000000 if bnum < 5000000 522 xmsiz = Calculate.get_xmsize_max(res) 523 when "Groonga" 524 req = "groonga_storage" 525 storage = "GroongaStorage" 526 bnum = Calculate.get_bnum(res) 527 bnum = 5000000 if bnum < 5000000 528 xmsiz = Calculate.get_xmsize_max(res) 529 end 530 end 531 532 if res.key?("language") 533 fd = Calculate.get_fd(res) 534 print "\r\nPlease set FileDescriptor bigger than #{fd}.\r\n\r\n" 535 end 536 537 body = "" 538 open(CONFIG_TEMPLATE_PATH, "r") do |f| 539 body = f.read 540 end 541 542 if req 543 body = ch_assign(body, "require", " ", "roma\/storage\/#{req}") 544 body = ch_assign(body, "STORAGE_CLASS", "Roma::Storage::#{storage}") 545 546 case req 547 when "rh_storage" 548 body = ch_assign(body, "STORAGE_OPTION","") 549 when /^(tc_storage|groonga_storage)$/ 550 body = ch_assign(body, "STORAGE_OPTION", "bnum=#{bnum}\#xmsiz=#{xmsiz}\#opts=d#dfunit=10") 551 end 552 end 553 554 if res.key?("plugin") 555 body = ch_assign(body, "PLUGIN_FILES", res["plugin"].value) 556 end 557 558 open(CONFIG_OUT_PATH, "w") do |f| 559 f.flock(File::LOCK_EX) 560 f.puts body 561 f.truncate(f.tell) 562 f.flock(File::LOCK_UN) 563 end 564 565 puts "Before" 566 Box.print_with_box(@defaults) 567 568 re_require(CONFIG_OUT_PATH, Config) 569 results = load_config([:STORAGE_CLASS, :STORAGE_OPTION, :PLUGIN_FILES]) 570 print "\r\nAfter\r\n" 571 Box.print_with_box(results) 572 print "\r\nMkconfig is finish.\r\n" 573 print "\r\nIf you need, change directory path about LOG, RTTABLE, STORAGE, WB and other setting.\r\n\r\n" 574 end
store_result(results, base, hash, input)
click to toggle source
# File lib/roma/tools/mkconfig.rb 482 def store_result(results, base, hash, input) 483 target = base[hash] 484 485 return results if !target["name"] 486 487 if target.key?("choice") 488 if target["store_type"] == "Array" 489 if base.flag 490 results[hash].value << target["choice"][input.to_i - 1] if !results[hash].value.include?(target["choice"][input.to_i - 1]) 491 else 492 results[hash] = Config_status.new(target, target["choice"][input.to_i - 1], target["store_type"]) 493 base.flag = true 494 end 495 496 if input.to_i == target["choice"].count 497 results[hash].value = target["choice"][0..-2] 498 end 499 else 500 results[hash] = Config_status.new(target, target["choice"][input.to_i - 1]) 501 end 502 else 503 results[hash] = Config_status.new(target, input) 504 end 505 506 base.flag = false if hash == "menu" 507 return results 508 end