Class: Roma::Mkconfig

Inherits:
Object
  • Object
show all
Defined in:
lib/roma/tools/mkconfig.rb

Defined Under Namespace

Classes: Base, Box, Calculate, Config_status, Input

Constant Summary collapse

TREE_TOP =
"menu"
LIB_PATH =
Pathname(__FILE__).dirname.parent.parent
CONFIG_TEMPLATE_PATH =
File.expand_path(File.join(LIB_PATH, "roma/config.rb"))
CONFIG_OUT_PATH =
File.expand_path(File.join(Pathname.pwd, "config.rb"))
PLUGIN_DIR =
File.expand_path(File.join(LIB_PATH, File.join("roma", "plugin")))
BNUM_COEFFICIENT =

reccomend1-4.

2
TC_FILE =
10
REDUNDANCY =
2
DEFAULT_ROMA_CONNECTION =
12
RUBY_CONNECTION =
10
JAVA_CONNECTION =
30
PHP_CONNECTION =
256
KB =
1024
GB =
1024 * 1024 * 1024
OS_MEMORY_SIZE =
1 * GB
END_MSG =
["exit", "quit", "balse"]

Instance Method Summary collapse

Constructor Details

#initialize(mode = :no_menu) ⇒ Mkconfig

Returns a new instance of Mkconfig.



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/roma/tools/mkconfig.rb', line 311

def initialize(mode = :no_menu)
  # confirming overwrite
  if File.exist?(CONFIG_OUT_PATH)
    print("Config.rb already exist in current directory. \nWill you overwrite?[y/n]")
    if gets.chomp! != "y"
      p "config.rb  were not created!"
      exit
    end
  end

  @base = Base.new
  @results = Hash::new
  @next_hash = TREE_TOP

  begin
    @defaults = load_config([:STORAGE_CLASS, :STORAGE_OPTION, :PLUGIN_FILES])
  rescue LoadError
    puts 'Not found config.rb file.'
    return
  rescue
    p $!
    puts "Content of config.rb is wrong."
    return
  end
  mkconfig(mode)
end

Instance Method Details

#ch_assign(text, exp, sep = " = ", str) ⇒ Object

sep means separating right and left part(config.rb style)



568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# File 'lib/roma/tools/mkconfig.rb', line 568

def ch_assign(text, exp, sep = " = ", str)
  sep = " = " if sep == "="
  text = text.gsub(/(\s*#{exp}).*/) do |s|
    name = $1
    if str.class == String
      if str =~ /::/ || str =~ /^\d+$/
        # storage type
       name + sep + str
      else
        # require & storage option
       name + sep + str.inspect
      end
    else
      # plugin
      # "to_s" equal "inspect" in Ruby 1.9
      name + sep + str.to_s.sub("\\", "")
    end
  end
end

#clear_screenObject



380
381
382
# File 'lib/roma/tools/mkconfig.rb', line 380

def clear_screen
  print "\e[H\e[2J"
end

#correct_in?(hash, input) ⇒ Boolean

Returns:

  • (Boolean)


446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# File 'lib/roma/tools/mkconfig.rb', line 446

def correct_in?(hash,input)
  if END_MSG.include?(input)
    return true
  end

  if hash["next"] == "continue"
    if (input == "y" || input == "n")
      return true
    end
  else
    if hash.key?('choice')
      if hash['choice'].count >= input.to_i && input.to_i > 0
        return true
      end
    else
      if hash["float_flg"]
        if 0 < input.to_f
          return true
        end
      else  
        if 0 < input.to_i
          return true
        end
      end
    end
  end

  return false
end

#end?(s) ⇒ Boolean

judge whether data inputting finish or not

Returns:

  • (Boolean)


416
417
418
419
420
421
# File 'lib/roma/tools/mkconfig.rb', line 416

def end?(s)
  if s == "END"
    save_data(@results)
    true
  end
end

#get_input(hash) ⇒ Object



431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/roma/tools/mkconfig.rb', line 431

def get_input(hash)
  receiver = Input.new
  input = ""

  while !correct_in?(hash,input)
    input = receiver.get_line
    if input == ""
      #set defaults value
      input = hash["default"]
    end
  end

  input
end

#load_config(targets) ⇒ Object



338
339
340
341
342
343
344
345
346
347
# File 'lib/roma/tools/mkconfig.rb', line 338

def load_config(targets)
  require CONFIG_TEMPLATE_PATH
  d_value = Hash.new
  Config.constants.each do |cnst|
    if targets.include?(cnst)
      d_value[cnst] = Config.const_get(cnst)
    end
  end
  return d_value
end

#mkconfig(mode) ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/roma/tools/mkconfig.rb', line 349

def mkconfig(mode)
  skip = skip_menu!(mode)

  while true
    clear_screen

    if @next_hash == "add_plugin"
      @results["plugin"].value.unshift("plugin_storage.rb")
      @next_hash = "menu"
    end

    skip.call if @next_hash == "menu" || @next_hash == "server" || @next_hash == "fd_server" || @next_hash == "check_plugin"
    break if end?(@base[@next_hash])
    puts "if you doesn't input anything, default value is set."
    Box.print_with_box(@defaults)
    print_status(@results)
    @base.print_question(@next_hash)
    input = get_input(@base[@next_hash])

    # if specific words(balse, exit, quit) was inputed, mkconfig.rb was finished.
    if END_MSG.include?(input)
      p "config.rb  were not created!"
      break

    else
      @results = store_result(@results, @base, @next_hash, input)
      @next_hash = @base.next(@next_hash, input)
    end
  end
end


423
424
425
426
427
428
429
# File 'lib/roma/tools/mkconfig.rb', line 423

def print_status(results)
  strs = Array.new
  results.each_value do |v|
    strs << "#{v.name} : #{v.value}"
  end
  Box.print_with_box(strs)
end

#re_require(path, c_obj) ⇒ Object



588
589
590
591
592
593
594
# File 'lib/roma/tools/mkconfig.rb', line 588

def re_require(path, c_obj)
  $".delete(File.expand_path(path))
  c_obj.constants.each do |cnst|
    c_obj.class_eval { remove_const cnst }
  end
  require path
end

#save_data(res) ⇒ Object

make config.rb based on input data



505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# File 'lib/roma/tools/mkconfig.rb', line 505

def save_data(res)
  if res.key?("storage")
    if res["storage"].value == "Ruby Hash"
      req = "rh_storage"
      storage = "RubyHashStorage"
    end

    if res["storage"].value == "Tokyo Cabinet"
      req = "tc_storage"
      storage = "TCStorage"
      bnum = Calculate.get_bnum(res)
      bnum = 5000000 if bnum < 5000000
      xmsiz = Calculate.get_xmsize_max(res)
    end
  end

  if res.key?("language")
    fd = Calculate.get_fd(res)
    print "\r\nPlease set FileDescriptor bigger than #{fd}.\r\n\r\n" 
  end

  body = ""
  open(CONFIG_TEMPLATE_PATH, "r") do |f|
    body = f.read
  end

  if req
    body = ch_assign(body, "require", " ", "roma\/storage\/#{req}")
    body = ch_assign(body, "STORAGE_CLASS", "Roma::Storage::#{storage}")

    if req == "rh_storage"
      body = ch_assign(body, "STORAGE_OPTION","")
    end

    if req == "tc_storage"
      body = ch_assign(body, "STORAGE_OPTION", "bnum=#{bnum}\#xmsiz=#{xmsiz}\#opts=d#dfunit=10")
    end
  end

  if res.key?("plugin")
    res["plugin"].value.unshift("plugin_storage.rb")
    body = ch_assign(body, "PLUGIN_FILES", res["plugin"].value)
  end

  open(CONFIG_OUT_PATH, "w") do |f|
    f.flock(File::LOCK_EX)
    f.puts body
    f.truncate(f.tell)
    f.flock(File::LOCK_UN)
  end

  puts "Before"
  Box.print_with_box(@defaults)

  re_require(CONFIG_OUT_PATH, Config)
  results = load_config([:STORAGE_CLASS, :STORAGE_OPTION, :PLUGIN_FILES])
  print "\r\nAfter\r\n"
  Box.print_with_box(results)
  print "\r\nMkconfig is finish.\r\n"
  print "\r\nIf you need, change directory path about LOG, RTTABLE, STORAGE, WB and other setting.\r\n\r\n"
end

#skip_menu!(menu) ⇒ Object



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/roma/tools/mkconfig.rb', line 384

def skip_menu!(menu)
  # in case of "-m" or "--with_menu" option was used
  if menu == :with_menu
    return Proc.new do
      if @next_hash == "server" && @results["fd_server"]
        @next_hash = @base["server"]["next"]
      elsif @next_hash == "fd_server" && @results["server"]
        @next_hash = "fd_client"
      elsif @next_hash == "check_plugin" && @results["plugin"].value.include?("plugin_storage.rb")
        @next_hash = "menu"
      end
    end
  end

  # in case of "-m" or "--with_menu" option was NOT used
  i = 0
  return Proc.new do
    if @next_hash == "menu"
      @next_hash = @base["menu"]["next"][i]
      i += 1
    elsif @next_hash == "server" && @results["fd_server"]
      @next_hash = @base["server"]["next"]
    elsif @next_hash == "fd_server" && @results["server"]
      @next_hash = "fd_client"
    elsif @next_hash == "check_plugin" && @results["plugin"].value.include?("plugin_storage.rb")
      @next_hash = "language"
      i += 1
    end
  end
end

#store_result(results, base, hash, input) ⇒ Object



476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/roma/tools/mkconfig.rb', line 476

def store_result(results, base, hash, input)
  target = base[hash]

  return results if !target["name"]

  if target.key?("choice")
    if target["store_type"] == "Array"
      if base.flag
        results[hash].value << target["choice"][input.to_i - 1] if !results[hash].value.include?(target["choice"][input.to_i - 1])
      else
        results[hash] = Config_status.new(target, target["choice"][input.to_i - 1], target["store_type"])
        base.flag = true
      end

      if input.to_i == target["choice"].count
        results[hash].value = target["choice"][0..-2]
      end
    else
      results[hash] = Config_status.new(target, target["choice"][input.to_i - 1])
    end
  else
    results[hash] = Config_status.new(target, input)
  end

  base.flag = false if hash == "menu"
  return results
end