Class: EY::Stonith::Commands::Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/ey_stonith/commands/abstract.rb

Direct Known Subclasses

Check, Claim, Commands, Cron, Help, Info, NotFound, Notify, Reset, Resume, Status, Stop, Takeover

Constant Summary collapse

DEFAULT_CONFIG_PATH =
Pathname.new("/etc/stonith.yml")
SCRIPT_NAME =
'stonith'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Abstract

Returns a new instance of Abstract.



15
16
17
18
19
# File 'lib/ey_stonith/commands/abstract.rb', line 15

def initialize(argv)
  @argv = argv
  parse!
  init_logger
end

Class Method Details

.commandObject



11
12
13
# File 'lib/ey_stonith/commands/abstract.rb', line 11

def self.command
  '[COMMAND]'
end

Instance Method Details

#callObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/ey_stonith/commands/abstract.rb', line 39

def call
  invoke
rescue Config::Error => e
  Stonith.logger.error e.message
  abort "#{e.message}\n\n#{parser}"
rescue => e
  error = "#{e.class}: #{e.message}\n#{e.backtrace.join("\n")}"
  Stonith.logger.error error
  abort error
end

#command_optionsObject



54
55
56
57
58
59
60
# File 'lib/ey_stonith/commands/abstract.rb', line 54

def command_options
  if @options[:config_path] != DEFAULT_CONFIG_PATH
    " --config #{@options[:config_path]}"
  else
    ""
  end
end

#configObject



31
32
33
# File 'lib/ey_stonith/commands/abstract.rb', line 31

def config
  @config ||= Config.new(@options[:config_path])
end

#execute(command, args = "") ⇒ Object



66
67
68
# File 'lib/ey_stonith/commands/abstract.rb', line 66

def execute(command, args = "")
  exec "#{script}-#{command}#{command_options} #{args}"
end

#historyObject



35
36
37
# File 'lib/ey_stonith/commands/abstract.rb', line 35

def history
  @history ||= History.new(config.history_path)
end

#init_loggerObject

Load the logger without requiring the config file to be present.



22
23
24
25
26
27
28
29
# File 'lib/ey_stonith/commands/abstract.rb', line 22

def init_logger
  Stonith.log_to begin
                   config.log_path
                 rescue Config::FileNotFound
                   $stderr
                 end
  Stonith.logger.level = Logger::INFO
end

#invokeObject



50
51
52
# File 'lib/ey_stonith/commands/abstract.rb', line 50

def invoke
  puts parser
end

#parse!Object



70
71
72
# File 'lib/ey_stonith/commands/abstract.rb', line 70

def parse!
  parser.parse!(@argv)
end

#parserObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ey_stonith/commands/abstract.rb', line 74

def parser
  @parser ||= OptionParser.new do |parser|
    @options = {:config_path => DEFAULT_CONFIG_PATH}

    parser.banner = "Usage: #{SCRIPT_NAME} #{self.class.command} [FLAGS]"
    parser.separator ""
    parser.separator "COMMANDS"
    parser.separator Stonith::Commands.formatted_command_list
    parser.separator ""
    parser.separator "FLAGS"

    parser.on('-c', '--config [FILE]', "Location of Stonith YAML config file (default #{@options[:config_path]})") do |path|
      @options[:config_path] = Pathname.new(path)
    end

    parser.on_tail("-h", "--help", "Show this message") do
      puts parser
      exit
    end
  end
end

#scriptObject



62
63
64
# File 'lib/ey_stonith/commands/abstract.rb', line 62

def script
  @script ||= File.join(File.dirname($0), SCRIPT_NAME)
end