cloudbox-server 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.yardopts +3 -0
- data/README.md +17 -0
- data/bin/cb-cluster-allocate +5 -0
- data/bin/cb-cluster-create +5 -0
- data/bin/cb-cluster-delete +5 -0
- data/bin/cb-cluster-desallocate +5 -0
- data/bin/cb-cluster-info +5 -0
- data/bin/cb-cluster-ls +5 -0
- data/bin/cb-node-destroy +5 -0
- data/bin/cb-node-detach +6 -0
- data/bin/cb-node-info +5 -0
- data/bin/cb-node-install +5 -0
- data/bin/cb-node-ls +5 -0
- data/bin/cb-service-add +5 -0
- data/bin/cb-service-component-add +5 -0
- data/bin/cb-service-component-delete +5 -0
- data/bin/cb-service-component-detach +5 -0
- data/bin/cb-service-conf-apply +5 -0
- data/bin/cb-service-conf-create +5 -0
- data/bin/cb-service-conf-get +5 -0
- data/bin/cb-service-conf-ls +5 -0
- data/bin/cb-service-follow-request +5 -0
- data/bin/cb-service-info +5 -0
- data/bin/cb-service-install +5 -0
- data/bin/cb-service-ls +5 -0
- data/bin/cb-service-start +5 -0
- data/bin/cb-service-stop +5 -0
- data/lib/command.rb +61 -0
- data/lib/command/ambari_add_host.rb +19 -0
- data/lib/command/ambari_cluster_command.rb +26 -0
- data/lib/command/ambari_cluster_component_command.rb +26 -0
- data/lib/command/ambari_cluster_component_host_command.rb +26 -0
- data/lib/command/ambari_cluster_create.rb +19 -0
- data/lib/command/ambari_cluster_host_command.rb +27 -0
- data/lib/command/ambari_cluster_list.rb +20 -0
- data/lib/command/ambari_cluster_service_command.rb +26 -0
- data/lib/command/ambari_cluster_service_host_command.rb +26 -0
- data/lib/command/ambari_command.rb +21 -0
- data/lib/command/ambari_create_cluster_configuration.rb +39 -0
- data/lib/command/ambari_get_cluster_configuration.rb +29 -0
- data/lib/command/ambari_host_list.rb +19 -0
- data/lib/command/ambari_install_cluster.rb +19 -0
- data/lib/command/ambari_install_component.rb +19 -0
- data/lib/command/ambari_install_host.rb +19 -0
- data/lib/command/ambari_install_host_component.rb +19 -0
- data/lib/command/ambari_install_host_components.rb +19 -0
- data/lib/command/ambari_install_service_components.rb +19 -0
- data/lib/command/ambari_service_add.rb +27 -0
- data/lib/command/ambari_service_component_add.rb +21 -0
- data/lib/command/ambari_service_component_delete.rb +21 -0
- data/lib/command/ambari_service_component_detach.rb +21 -0
- data/lib/command/ambari_service_component_info.rb +29 -0
- data/lib/command/ambari_service_conf_apply.rb +31 -0
- data/lib/command/ambari_service_conf_ls.rb +30 -0
- data/lib/command/ambari_service_delete.rb +21 -0
- data/lib/command/ambari_service_info.rb +20 -0
- data/lib/command/ambari_service_list.rb +21 -0
- data/lib/command/ambari_start_cluster.rb +19 -0
- data/lib/command/ambari_start_component.rb +19 -0
- data/lib/command/ambari_start_host.rb +19 -0
- data/lib/command/ambari_start_host_component.rb +19 -0
- data/lib/command/ambari_start_host_components.rb +19 -0
- data/lib/command/ambari_start_service_components.rb +19 -0
- data/lib/command/ambari_stop_cluster.rb +19 -0
- data/lib/command/ambari_stop_component.rb +19 -0
- data/lib/command/ambari_stop_host.rb +19 -0
- data/lib/command/ambari_stop_host_component.rb +19 -0
- data/lib/command/ambari_stop_host_components.rb +19 -0
- data/lib/command/ambari_stop_service_components.rb +19 -0
- data/lib/command/basic_command.rb +44 -0
- data/lib/command/cluster_create.rb +50 -0
- data/lib/command/cluster_delete.rb +30 -0
- data/lib/command/cluster_info.rb +30 -0
- data/lib/command/cluster_ls.rb +30 -0
- data/lib/command/crowbar_cluster_allocate.rb +42 -0
- data/lib/command/crowbar_command.rb +30 -0
- data/lib/command/crowbar_get_barclamps_list.rb +30 -0
- data/lib/command/crowbar_node_command.rb +21 -0
- data/lib/command/crowbar_node_list.rb +40 -0
- data/lib/command/crowbar_vcluster_command.rb +20 -0
- data/lib/command/filter.rb +15 -0
- data/lib/command/node_destroy.rb +58 -0
- data/lib/command/node_detach.rb +44 -0
- data/lib/command/node_info.rb +27 -0
- data/lib/command/node_install.rb +46 -0
- data/lib/command/node_reinstall.rb +44 -0
- data/lib/command/state_filter.rb +16 -0
- data/lib/common.rb +11 -0
- data/lib/common/cb-lib-node-erase-hard-disk.sh +29 -0
- data/lib/common/cloudbox_exceptions.rb +631 -0
- data/lib/common/cloudbox_logger.rb +81 -0
- data/lib/common/cloudbox_logger_mock.rb +43 -0
- data/lib/common/color.rb +267 -0
- data/lib/common/config_properties.rb +2027 -0
- data/lib/common/services_description.rb +192 -0
- data/lib/exec.rb +35 -0
- data/lib/exec/check_parameter.rb +208 -0
- data/lib/exec/cluster_allocate.rb +163 -0
- data/lib/exec/cluster_create.rb +99 -0
- data/lib/exec/cluster_delete.rb +38 -0
- data/lib/exec/cluster_desallocate.rb +37 -0
- data/lib/exec/cluster_info.rb +177 -0
- data/lib/exec/cluster_ls.rb +150 -0
- data/lib/exec/command_option.rb +222 -0
- data/lib/exec/executable_command.rb +194 -0
- data/lib/exec/node_destroy.rb +101 -0
- data/lib/exec/node_detach.rb +98 -0
- data/lib/exec/node_info.rb +280 -0
- data/lib/exec/node_install.rb +234 -0
- data/lib/exec/node_ls.rb +160 -0
- data/lib/exec/service_add.rb +224 -0
- data/lib/exec/service_component_add.rb +39 -0
- data/lib/exec/service_component_delete.rb +37 -0
- data/lib/exec/service_component_detach.rb +37 -0
- data/lib/exec/service_conf_apply.rb +57 -0
- data/lib/exec/service_conf_create.rb +80 -0
- data/lib/exec/service_conf_get.rb +165 -0
- data/lib/exec/service_conf_ls.rb +103 -0
- data/lib/exec/service_follow_request.rb +49 -0
- data/lib/exec/service_info.rb +346 -0
- data/lib/exec/service_install.rb +87 -0
- data/lib/exec/service_ls.rb +124 -0
- data/lib/exec/service_start.rb +110 -0
- data/lib/exec/service_stop.rb +112 -0
- data/lib/receiver.rb +14 -0
- data/lib/receiver/ambari_receiver.rb +812 -0
- data/lib/receiver/ambari_rest_api_connector.rb +599 -0
- data/lib/receiver/basic_receiver.rb +28 -0
- data/lib/receiver/connector.rb +28 -0
- data/lib/receiver/crowbar_receiver.rb +588 -0
- data/lib/receiver/crowbar_rest_api_connector.rb +518 -0
- data/lib/receiver/crowbar_shell_api.rb +643 -0
- data/lib/receiver/rest_api_connector.rb +275 -0
- data/resources/ambari-configurations/HBASE/default_hbase-site +33 -0
- data/resources/ambari-configurations/HDFS/default_hdfs-site +49 -0
- data/resources/ambari-configurations/HIVE/default_hive-site +33 -0
- data/resources/ambari-configurations/MAPREDUCE/default_mapred-site +72 -0
- data/resources/ambari-configurations/OOZIE/default_oozie-site +28 -0
- data/resources/ambari-configurations/WEBHCAT/default_webhcat-site +18 -0
- data/resources/ambari-configurations/ZOOKEEPER/default_zookeeper-site +8 -0
- data/resources/ambari-configurations/default_core-site +22 -0
- data/resources/ambari-configurations/default_global +141 -0
- data/resources/cloudbox-server.conf +10 -0
- data/spec/common/services_description.rb +130 -0
- data/spec/exec/check_parameter.rb +152 -0
- data/spec/exec/command_option_spec.rb +97 -0
- metadata +328 -0
@@ -0,0 +1,222 @@
|
|
1
|
+
# This file is part of cloudbox-server project
|
2
|
+
# @author tmarmin (INGENSI)
|
3
|
+
|
4
|
+
require "common/cloudbox_exceptions"
|
5
|
+
require 'rubygems'
|
6
|
+
require 'optparse'
|
7
|
+
require 'pathname'
|
8
|
+
require 'json'
|
9
|
+
|
10
|
+
module Exec
|
11
|
+
# This class is an helper to the ParserOption class which manages ans parse options given to a linux command.
|
12
|
+
# @author tmarmin
|
13
|
+
class CommandOption
|
14
|
+
|
15
|
+
public
|
16
|
+
# Get the values
|
17
|
+
attr_reader :values
|
18
|
+
|
19
|
+
protected
|
20
|
+
# The OptionParser object
|
21
|
+
@opt_parser
|
22
|
+
# The options list
|
23
|
+
@options
|
24
|
+
# The argv tab
|
25
|
+
@argv
|
26
|
+
# The command name
|
27
|
+
@command_name
|
28
|
+
|
29
|
+
public
|
30
|
+
# Default constructor
|
31
|
+
# @author tmarmin
|
32
|
+
def initialize(command_name, argv)
|
33
|
+
@options = {}
|
34
|
+
@argv = argv
|
35
|
+
@command_name = command_name
|
36
|
+
end
|
37
|
+
|
38
|
+
public
|
39
|
+
# Declare new option
|
40
|
+
# @param short_name [String] Short name (String.length=1) of the option
|
41
|
+
# @param long_name [String] The long name of the option
|
42
|
+
# @param description [String] Description of the option
|
43
|
+
# @param mandatory [Boolean] If true verify method will raise an error if the option is not
|
44
|
+
# @param require_value [Boolean] If true, a value is needed for this option.
|
45
|
+
# @param check_format [Method] A method which take only one string in parameter and return a boolean (it will be
|
46
|
+
# used to validate the option) in argv.
|
47
|
+
def add_option(short_name, long_name, description, mandatory, require_value = false, check_format = nil)
|
48
|
+
raise NameError.new("Duplicate option name") if @options.include?(long_name)
|
49
|
+
@options.store(long_name, Option.new(short_name, long_name, description, mandatory, require_value, check_format))
|
50
|
+
end
|
51
|
+
|
52
|
+
public
|
53
|
+
# @return The option parser help.
|
54
|
+
# @author tmarmin
|
55
|
+
def get_help
|
56
|
+
return @opt_parser.help
|
57
|
+
end
|
58
|
+
|
59
|
+
public
|
60
|
+
# Generate a basic usage.
|
61
|
+
# @note Should be overrided in inherited classes to generate a consistent banner.
|
62
|
+
# A banner generaly looks like :
|
63
|
+
# Usage: command_name -arg1 VALUE_1 [-optional1 OPT_VAL_1]
|
64
|
+
# @return The help banner
|
65
|
+
# @author tmarmin
|
66
|
+
def get_banner
|
67
|
+
usage = "Usage: #@command_name "
|
68
|
+
get_sorted_options().each do |option|
|
69
|
+
usage += option.to_usage_s + " "
|
70
|
+
end
|
71
|
+
return usage
|
72
|
+
end
|
73
|
+
|
74
|
+
public
|
75
|
+
# @return [Array<Option>] a sorted array where mandatory are in first place.
|
76
|
+
def get_sorted_options
|
77
|
+
return @options.values.sort { |x, y| y.mandatory.to_s <=> x.mandatory.to_s }
|
78
|
+
end
|
79
|
+
|
80
|
+
public
|
81
|
+
# This method verifies if given arguments are valid.
|
82
|
+
# @return [Hash] The arguments passed
|
83
|
+
# @raise ...
|
84
|
+
def verify()
|
85
|
+
initialize_option_values()
|
86
|
+
|
87
|
+
@opt_parser = OptionParser.new do |opt|
|
88
|
+
opt.banner = get_banner()
|
89
|
+
opt.separator "Options"
|
90
|
+
|
91
|
+
@options.each_value do |option|
|
92
|
+
|
93
|
+
# adding the option value
|
94
|
+
if option.require_value
|
95
|
+
opt.on("-#{option.short_name}", "--#{option.long_name} #{option.value_name}", option.description) do |value|
|
96
|
+
@values[option.long_name] = value
|
97
|
+
end
|
98
|
+
else
|
99
|
+
opt.on("-#{option.short_name}", "--#{option.long_name}", option.description) do
|
100
|
+
@values[option.long_name] = true
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
begin
|
107
|
+
@opt_parser.parse(@argv)
|
108
|
+
rescue OptionParser::InvalidArgument, OptionParser::InvalidOption => e
|
109
|
+
raise Common::InvalidParameter.new(e.message, @opt_parser.to_s)
|
110
|
+
rescue OptionParser::ParseError => e
|
111
|
+
raise Common::ParameterError.new(e.message, @opt_parser.to_s)
|
112
|
+
end
|
113
|
+
@options.each_value do |option|
|
114
|
+
if @values.include?(option.long_name) && option.require_value && !option.check_format.nil?
|
115
|
+
valid, msg = option.valid_value?(@values[option.long_name])
|
116
|
+
raise Common::InvalidParameter.new("-#{option.short_name}", "Bad Format: #{msg}") unless valid
|
117
|
+
#end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
check_mandatory()
|
122
|
+
|
123
|
+
return @values
|
124
|
+
end
|
125
|
+
|
126
|
+
protected
|
127
|
+
# Default behaviour for commandeOption: raise an exception if a mandatory is missing.
|
128
|
+
# @note You should override this method to implement a custom behaviour.
|
129
|
+
# @raise [Common::MissingParameter] If a mandatory option is missing.
|
130
|
+
def check_mandatory
|
131
|
+
@options.each_value do |option|
|
132
|
+
if option.mandatory
|
133
|
+
raise Common::MissingParameter.new("-#{option.short_name}", "Mandatory option.") if @values[option.long_name].nil?
|
134
|
+
end
|
135
|
+
end
|
136
|
+
return true
|
137
|
+
end
|
138
|
+
|
139
|
+
public
|
140
|
+
# Indicates if the command has arguments.
|
141
|
+
# @param arg_short_name The short named arguments.
|
142
|
+
# @param arg_long_name The long named arguments.
|
143
|
+
# @return True if the command has arguments, false otherwise.
|
144
|
+
def has_argument(arg_short_name, arg_long_name)
|
145
|
+
return @argv.include?('-' + arg_short_name) || @argv.include?('--' + arg_long_name)
|
146
|
+
end
|
147
|
+
|
148
|
+
private
|
149
|
+
# Initialize the return hash
|
150
|
+
# @return [Hash,OptionParser] The values list and the OptionParser object
|
151
|
+
# @author tmarmin
|
152
|
+
def initialize_option_values
|
153
|
+
@values = {}
|
154
|
+
|
155
|
+
# set to false all boolean options which are not already set
|
156
|
+
@options.each_value do |option|
|
157
|
+
@values.store(option.long_name, false) if !option.require_value
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
|
162
|
+
protected
|
163
|
+
# This subclass represents a command option.
|
164
|
+
class Option
|
165
|
+
|
166
|
+
public
|
167
|
+
attr_reader :short_name
|
168
|
+
attr_reader :long_name
|
169
|
+
attr_reader :description
|
170
|
+
attr_reader :require_value
|
171
|
+
attr_reader :mandatory
|
172
|
+
attr_reader :check_format
|
173
|
+
|
174
|
+
# Adds _NAME to the long_name.
|
175
|
+
def value_name
|
176
|
+
@long_name.upcase + "_NAME"
|
177
|
+
end
|
178
|
+
|
179
|
+
public
|
180
|
+
def initialize(short_name, long_name, description, mandatory = false, require_value = false, check_format = nil)
|
181
|
+
|
182
|
+
raise NameError.new("Short value has to be a String of one character") unless short_name.instance_of?(String) && short_name.length == 1
|
183
|
+
raise NameError.new("Long value has to be a non empty String") unless long_name.instance_of?(String) && long_name.length > 0
|
184
|
+
raise NameError.new("Description value has to be a non empty String") unless description.instance_of?(String) && description.length > 0
|
185
|
+
raise NameError.new("Check format method has to be of type Method class which get one string and returns a boolean,String.") unless check_format.nil? || (check_format.instance_of?(Method) && check_format.arity == 1)
|
186
|
+
|
187
|
+
@short_name = short_name
|
188
|
+
@long_name = long_name
|
189
|
+
@description = description
|
190
|
+
@require_value = require_value
|
191
|
+
@mandatory = mandatory
|
192
|
+
@check_format = check_format
|
193
|
+
end
|
194
|
+
|
195
|
+
public
|
196
|
+
# @return true if given value is valid
|
197
|
+
def valid_value?(value)
|
198
|
+
msg = ""
|
199
|
+
|
200
|
+
if require_value
|
201
|
+
valid, msg = check_format.call(value)
|
202
|
+
else
|
203
|
+
valid = value == true || value == false
|
204
|
+
end
|
205
|
+
|
206
|
+
return valid, msg
|
207
|
+
end
|
208
|
+
|
209
|
+
public
|
210
|
+
# @return A string that describes option for usage generating
|
211
|
+
def to_usage_s
|
212
|
+
s = ""
|
213
|
+
s += "[" unless @mandatory
|
214
|
+
s += "-" + @short_name
|
215
|
+
s += " " + value_name if @require_value
|
216
|
+
s += "]" unless @mandatory
|
217
|
+
return s
|
218
|
+
end
|
219
|
+
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
@@ -0,0 +1,194 @@
|
|
1
|
+
# This file is part of cloudbox-service project
|
2
|
+
# @author tnoguer (INGENSI)
|
3
|
+
|
4
|
+
require 'common/color'
|
5
|
+
require 'common/cloudbox_logger'
|
6
|
+
require 'common/cloudbox_logger_mock'
|
7
|
+
require 'common/cloudbox_exceptions'
|
8
|
+
require 'exec/command_option'
|
9
|
+
|
10
|
+
include Common
|
11
|
+
include Common::Color
|
12
|
+
|
13
|
+
module Exec
|
14
|
+
# Basic class of an executable command. It contains every attributes and methods all executable command needs.
|
15
|
+
# @abstract Subclass and override exec and set_options to implement
|
16
|
+
# @author tnoguer
|
17
|
+
class ExecutableCommand
|
18
|
+
|
19
|
+
protected
|
20
|
+
# The cloudbox logger object, used for logging.
|
21
|
+
attr_accessor :logger
|
22
|
+
# The name of the command.
|
23
|
+
attr_accessor :command_name
|
24
|
+
# The passed arguments when calling the command.
|
25
|
+
attr_accessor :argv
|
26
|
+
# The input stream of the command.
|
27
|
+
attr_accessor :stdin
|
28
|
+
# The output stream of the command.
|
29
|
+
attr_accessor :stdout
|
30
|
+
# The error stream of the command.
|
31
|
+
attr_accessor :stderr
|
32
|
+
# The CommandOption object which describe option behavior
|
33
|
+
attr_accessor :options
|
34
|
+
# The values Hash obtained with option passed to the command
|
35
|
+
attr_accessor :values
|
36
|
+
# The CLOUDBOX_HOME environment variable value
|
37
|
+
# attr_accessor :cloudbox_home
|
38
|
+
|
39
|
+
public
|
40
|
+
# This function is the common execution of an executable command. It loads the librairies, creater the logger,
|
41
|
+
# calls the setOptions function and finally calls the exec function (the effective execution of the command).
|
42
|
+
# @author tnoguer
|
43
|
+
def run
|
44
|
+
begin
|
45
|
+
begin
|
46
|
+
|
47
|
+
create_logger()
|
48
|
+
#check_environment_variables()
|
49
|
+
check_parameters()
|
50
|
+
|
51
|
+
@logger.begin_execution(self.argv)
|
52
|
+
|
53
|
+
if @values["help"]
|
54
|
+
@stdout.puts(@options.get_help())
|
55
|
+
else
|
56
|
+
exec()
|
57
|
+
end
|
58
|
+
|
59
|
+
rescue StandardError => e
|
60
|
+
@logger.error(e.message)
|
61
|
+
@logger.error(e.backtrace.join("\n"))
|
62
|
+
@stdout.puts(Color::color_red("Execution failed"))
|
63
|
+
@stdout.puts(e.message)
|
64
|
+
raise e
|
65
|
+
end
|
66
|
+
rescue LoadError
|
67
|
+
@logger.error(e.message)
|
68
|
+
@logger.error(e.backtrace.join("\n"))
|
69
|
+
exit 254
|
70
|
+
rescue Common::ParameterError => e
|
71
|
+
@stdout.puts e.usage
|
72
|
+
@logger.error(e.message)
|
73
|
+
@logger.error(e.backtrace.join("\n"))
|
74
|
+
exit 1
|
75
|
+
rescue Common::CloudboxError
|
76
|
+
@logger.error(e.message)
|
77
|
+
@logger.error(e.backtrace.join("\n"))
|
78
|
+
exit 2
|
79
|
+
rescue StandardError => e
|
80
|
+
@stdout.puts e.class
|
81
|
+
@logger.error(e.message)
|
82
|
+
@logger.error(e.backtrace.join("\n"))
|
83
|
+
exit 254
|
84
|
+
else
|
85
|
+
@logger.end_execution()
|
86
|
+
end
|
87
|
+
|
88
|
+
@stdout.puts(Color::color_green("Execution success"))
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
protected
|
93
|
+
# Default constructor of the class.
|
94
|
+
# @param argv The arguments of the command.
|
95
|
+
# @param stdin [IO] The input stream of the command.
|
96
|
+
# @param stdout [IO] The output stream of the command.
|
97
|
+
# @param stderr [IO] The error stream of the command.
|
98
|
+
# @param command_name [String] The name of the executed command.
|
99
|
+
# @param command_option_class [CommandOption] The CommandOption object.
|
100
|
+
# @author tnoguer
|
101
|
+
def initialize(argv, stdin, stdout, stderr, command_name, command_option_class = CustomCommandOption)
|
102
|
+
@command_name = command_name
|
103
|
+
@argv = argv
|
104
|
+
@stdin = stdin
|
105
|
+
@stdout = stdout
|
106
|
+
@stderr = stderr
|
107
|
+
@cloudbox_home = ENV['CLOUDBOX_HOME']
|
108
|
+
|
109
|
+
@options = command_option_class.new(@command_name, @argv)
|
110
|
+
@options.add_option("h", "help", "Display help.", false)
|
111
|
+
end
|
112
|
+
|
113
|
+
protected
|
114
|
+
# Declare specific options needed by the command.
|
115
|
+
# @note This method has to be overrided by the inheriting class to add some options.
|
116
|
+
# @raise [NotImplementedError] When the method is not overrided by the inheriting class.
|
117
|
+
# @author tmarmin
|
118
|
+
def set_options
|
119
|
+
raise NotImplementedError
|
120
|
+
end
|
121
|
+
|
122
|
+
# The execution of the command.
|
123
|
+
# @note This method must be overrided by the inheriting class.
|
124
|
+
# @raise [NotImplementedError] When the method is not overrided by the inheriting class.
|
125
|
+
# @author tnoguer
|
126
|
+
protected
|
127
|
+
def exec
|
128
|
+
raise NotImplementedError
|
129
|
+
end
|
130
|
+
|
131
|
+
protected
|
132
|
+
# Create a logger which logs into the default log path/command_name.
|
133
|
+
# If an error occurred, it will create a mocked logger.
|
134
|
+
# @author tmarmin
|
135
|
+
def create_logger
|
136
|
+
begin
|
137
|
+
Color::print_log("NONE", "Loading logger...", @stdout)
|
138
|
+
@logger = Common::CloudboxLogger.new(@command_name)
|
139
|
+
Color::echo_ok(@stdout)
|
140
|
+
rescue => e
|
141
|
+
Color::echo_fail
|
142
|
+
@logger = Common::CloudboxLoggerMock.new
|
143
|
+
Color::color_red("Init CloudboxLogger error: #{e.message}", @stdout)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
protected
|
148
|
+
# This method just call setOptions method and assumes print infos into logger and stdout.
|
149
|
+
# @author tmarmin
|
150
|
+
def check_parameters
|
151
|
+
@logger.begin_main_step("parameters checking")
|
152
|
+
Color::print_log("NONE", "checking parameters...", @stdout)
|
153
|
+
|
154
|
+
begin
|
155
|
+
set_options()
|
156
|
+
|
157
|
+
@values = @options.verify()
|
158
|
+
rescue CloudboxError => e
|
159
|
+
Color::echo_fail(@stdout)
|
160
|
+
raise e
|
161
|
+
end
|
162
|
+
|
163
|
+
@logger.end_main_step("parameters checking")
|
164
|
+
Color::echo_ok(@stdout)
|
165
|
+
end
|
166
|
+
|
167
|
+
#protected
|
168
|
+
# This method checks if mandatory environment variables are set..
|
169
|
+
# @author tmarmin
|
170
|
+
#def check_environment_variables
|
171
|
+
# @logger.begin_main_step("parameters checking")
|
172
|
+
# Color::print_log("NONE", "checking environment variable...", @stdout)
|
173
|
+
|
174
|
+
# raise Common::CloudboxError.new("CLOUDBOX_HOME environment variable is not set") if @cloudbox_home.nil?
|
175
|
+
|
176
|
+
# Color::echo_ok(@stdout)
|
177
|
+
#end
|
178
|
+
|
179
|
+
private
|
180
|
+
|
181
|
+
# Inherited CommandOption used only by NodeLs.
|
182
|
+
class CustomCommandOption < CommandOption
|
183
|
+
|
184
|
+
protected
|
185
|
+
# This method overrides the default behaviour.
|
186
|
+
# @raise [Common::MissingParameter] Never raise exception for the moment :)
|
187
|
+
def check_mandatory
|
188
|
+
unless @values["help"]
|
189
|
+
super()
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
# This file is part of Devcloudbox project
|
2
|
+
# @author aboudot (INGENSI)
|
3
|
+
|
4
|
+
# @todo use CommandOption to check parameters format
|
5
|
+
# @author aboudot
|
6
|
+
|
7
|
+
require 'exec/executable_command'
|
8
|
+
require 'common/cloudbox_exceptions'
|
9
|
+
require 'optparse'
|
10
|
+
require 'command/node_destroy'
|
11
|
+
|
12
|
+
include Exec::CheckParameter
|
13
|
+
|
14
|
+
module Exec
|
15
|
+
# Allows the user to destroy node's of crowbar server
|
16
|
+
class NodeDestroy < ExecutableCommand
|
17
|
+
|
18
|
+
public
|
19
|
+
# Default constructor of the class.
|
20
|
+
# @note Overrides default constructor by passing CustomCommandOption to super().
|
21
|
+
# @author mbretaud
|
22
|
+
def initialize(argv, stdin, stdout, stderr, command_name)
|
23
|
+
super(argv, stdin, stdout, stderr, command_name)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
# @author aboudot
|
28
|
+
def set_options()
|
29
|
+
@logger.info("Exec::NodeDestroy Setting options")
|
30
|
+
@options.add_option("n", "node", "The Node name that will be Destroy.", true, true, method(:check_crowbar_node_name))
|
31
|
+
end
|
32
|
+
|
33
|
+
# The execution of the command.
|
34
|
+
# @author mbretaud
|
35
|
+
def exec
|
36
|
+
@logger.info("Exec::NodeDestroy Executing NodeDestroy")
|
37
|
+
destroy_node(@values['node'])
|
38
|
+
end
|
39
|
+
|
40
|
+
public
|
41
|
+
# Destroy a node from the server Crowbar
|
42
|
+
# @param node_name [String] the name node
|
43
|
+
# @raise NodeError If the node don't exists or is not Ready
|
44
|
+
# @author mbretaud
|
45
|
+
def destroy_node(node_name)
|
46
|
+
@logger.info("Exec::NodeDestroy destroy_node(#{node_name})")
|
47
|
+
Color::print_log("NONE", "Destroy the node '#{node_name}'...", @stdout)
|
48
|
+
|
49
|
+
output = ""
|
50
|
+
unless node_name.nil?
|
51
|
+
if if_node_exist(node_name, "Ready")
|
52
|
+
cmd = Command::NodeDestroy.new(node_name)
|
53
|
+
cmd.exec
|
54
|
+
#output += "Destroy the node '#{node_name}'.\n"
|
55
|
+
else
|
56
|
+
raise NodeDestroyError.new("The node '#{node_name}' doesn't exists or is not 'Ready'.")
|
57
|
+
end
|
58
|
+
else
|
59
|
+
raise NodeDestroyError.new("The node name is nil.")
|
60
|
+
end
|
61
|
+
|
62
|
+
Color::echo_ok(@stdout)
|
63
|
+
@logger.info(output)
|
64
|
+
if !output.empty? && output.length > 1
|
65
|
+
@stdout.print output
|
66
|
+
else
|
67
|
+
@logger.info("Exec::NodeLs No machines available.")
|
68
|
+
@stdout.printf_red "No machines available.\n"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
# Check if the node exists or not
|
74
|
+
# @param node_name [String] the name node
|
75
|
+
# @return [Boolean] true if the node exists, false else
|
76
|
+
# @todo refactor in a library
|
77
|
+
# @author mbretaud
|
78
|
+
def if_node_exist(node_name, status)
|
79
|
+
@logger.info("Exec::NodeDestroy Check if the node '#{node_name}' exists or not.")
|
80
|
+
|
81
|
+
# Retrieves the list of nodes.
|
82
|
+
begin
|
83
|
+
@logger.info("Exec::NodeDestroy Getting the nodes list.")
|
84
|
+
cmd = Command::CrowbarNodeList.new(@logger, status)
|
85
|
+
list_nodes = cmd.exec
|
86
|
+
rescue => e
|
87
|
+
raise NodeListError.new("Retrieves the list of nodes with the status '#{status}'.")
|
88
|
+
end
|
89
|
+
|
90
|
+
if !list_nodes.nil? && list_nodes.length > 0
|
91
|
+
list_nodes.each { |node|
|
92
|
+
if node_name == node.split(" ").at(0).strip.to_s()
|
93
|
+
return true
|
94
|
+
end
|
95
|
+
}
|
96
|
+
end
|
97
|
+
|
98
|
+
return false
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|