Class: Exec::ExecutableCommand Abstract
- Inherits:
-
Object
- Object
- Exec::ExecutableCommand
- Defined in:
- lib/exec/executable_command.rb
Overview
Subclass and override exec and set_options to implement
Basic class of an executable command. It contains every attributes and methods all executable command needs.
Direct Known Subclasses
ClusterAllocate, ClusterCreate, ClusterDelete, ClusterDesallocate, ClusterInfo, ClusterLs, NodeDestroy, NodeDetach, NodeInfo, NodeInstall, NodeLs, ServiceAdd, ServiceComponentAdd, ServiceComponentDelete, ServiceComponentDetach, ServiceConfApply, ServiceConfCreate, ServiceConfGet, ServiceConfLs, ServiceFollowRequest, ServiceInfo, ServiceInstall, ServiceLs, ServiceStart, ServiceStop
Defined Under Namespace
Classes: CustomCommandOption
Instance Attribute Summary collapse
-
#argv ⇒ Object
protected
The passed arguments when calling the command.
-
#command_name ⇒ Object
protected
The name of the command.
-
#logger ⇒ Object
protected
The cloudbox logger object, used for logging.
-
#options ⇒ Object
protected
The CommandOption object which describe option behavior.
-
#stderr ⇒ Object
protected
The error stream of the command.
-
#stdin ⇒ Object
protected
The input stream of the command.
-
#stdout ⇒ Object
protected
The output stream of the command.
-
#values ⇒ Object
protected
The values Hash obtained with option passed to the command.
Instance Method Summary collapse
-
#check_parameters ⇒ Object
protected
This method just call setOptions method and assumes print infos into logger and stdout.
-
#create_logger ⇒ Object
protected
Create a logger which logs into the default log path/command_name.
- #exec ⇒ Object protected
-
#initialize(argv, stdin, stdout, stderr, command_name, command_option_class = CustomCommandOption) ⇒ ExecutableCommand
constructor
protected
Default constructor of the class.
-
#run ⇒ Object
This function is the common execution of an executable command.
-
#set_options ⇒ Object
protected
Declare specific options needed by the command.
Constructor Details
#initialize(argv, stdin, stdout, stderr, command_name, command_option_class = CustomCommandOption) ⇒ ExecutableCommand (protected)
Default constructor of the class.
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/exec/executable_command.rb', line 101 def initialize(argv, stdin, stdout, stderr, command_name, command_option_class = CustomCommandOption) @command_name = command_name @argv = argv @stdin = stdin @stdout = stdout @stderr = stderr @cloudbox_home = ENV['CLOUDBOX_HOME'] = command_option_class.new(@command_name, @argv) .add_option("h", "help", "Display help.", false) end |
Instance Attribute Details
#argv ⇒ Object (protected)
The passed arguments when calling the command.
25 26 27 |
# File 'lib/exec/executable_command.rb', line 25 def argv @argv end |
#command_name ⇒ Object (protected)
The name of the command.
23 24 25 |
# File 'lib/exec/executable_command.rb', line 23 def command_name @command_name end |
#logger ⇒ Object (protected)
The cloudbox logger object, used for logging.
21 22 23 |
# File 'lib/exec/executable_command.rb', line 21 def logger @logger end |
#options ⇒ Object (protected)
The CommandOption object which describe option behavior
33 34 35 |
# File 'lib/exec/executable_command.rb', line 33 def end |
#stderr ⇒ Object (protected)
The error stream of the command.
31 32 33 |
# File 'lib/exec/executable_command.rb', line 31 def stderr @stderr end |
#stdin ⇒ Object (protected)
The input stream of the command.
27 28 29 |
# File 'lib/exec/executable_command.rb', line 27 def stdin @stdin end |
#stdout ⇒ Object (protected)
The output stream of the command.
29 30 31 |
# File 'lib/exec/executable_command.rb', line 29 def stdout @stdout end |
#values ⇒ Object (protected)
The values Hash obtained with option passed to the command
35 36 37 |
# File 'lib/exec/executable_command.rb', line 35 def values @values end |
Instance Method Details
#check_parameters ⇒ Object (protected)
This method just call setOptions method and assumes print infos into logger and stdout.
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/exec/executable_command.rb', line 150 def check_parameters @logger.begin_main_step("parameters checking") Color::print_log("NONE", "checking parameters...", @stdout) begin () @values = .verify() rescue CloudboxError => e Color::echo_fail(@stdout) raise e end @logger.end_main_step("parameters checking") Color::echo_ok(@stdout) end |
#create_logger ⇒ Object (protected)
Create a logger which logs into the default log path/command_name. If an error occurred, it will create a mocked logger.
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/exec/executable_command.rb', line 135 def create_logger begin Color::print_log("NONE", "Loading logger...", @stdout) @logger = Common::CloudboxLogger.new(@command_name) Color::echo_ok(@stdout) rescue => e Color::echo_fail @logger = Common::CloudboxLoggerMock.new Color::color_red("Init CloudboxLogger error: #{e.message}", @stdout) end end |
#exec ⇒ Object (protected)
127 128 129 |
# File 'lib/exec/executable_command.rb', line 127 def exec raise NotImplementedError end |
#run ⇒ Object
This function is the common execution of an executable command. It loads the librairies, creater the logger, calls the setOptions function and finally calls the exec function (the effective execution of the command).
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/exec/executable_command.rb', line 43 def run begin begin create_logger() #check_environment_variables() check_parameters() @logger.begin_execution(self.argv) if @values["help"] @stdout.puts(.get_help()) else exec() end rescue StandardError => e @logger.error(e.) @logger.error(e.backtrace.join("\n")) @stdout.puts(Color::color_red("Execution failed")) @stdout.puts(e.) raise e end rescue LoadError @logger.error(e.) @logger.error(e.backtrace.join("\n")) exit 254 rescue Common::ParameterError => e @stdout.puts e.usage @logger.error(e.) @logger.error(e.backtrace.join("\n")) exit 1 rescue Common::CloudboxError @logger.error(e.) @logger.error(e.backtrace.join("\n")) exit 2 rescue StandardError => e @stdout.puts e.class @logger.error(e.) @logger.error(e.backtrace.join("\n")) exit 254 else @logger.end_execution() end @stdout.puts(Color::color_green("Execution success")) end |
#set_options ⇒ Object (protected)
This method has to be overrided by the inheriting class to add some options.
Declare specific options needed by the command.
118 119 120 |
# File 'lib/exec/executable_command.rb', line 118 def raise NotImplementedError end |