Class: Morpheus::Cli::ExecutionRequestCommand
- Inherits:
-
Object
- Object
- Morpheus::Cli::ExecutionRequestCommand
- Includes:
- CliCommand
- Defined in:
- lib/morpheus/cli/execution_request_command.rb
Overview
require ‘morpheus/cli/mixins/infrastructure_helper’
Instance Attribute Summary
Attributes included from CliCommand
Instance Method Summary collapse
- #connect(opts) ⇒ Object
- #execute(args) ⇒ Object
- #execute_against_lease(args) ⇒ Object
- #format_execution_request_status(execution_request, return_color = cyan) ⇒ Object
- #get(args) ⇒ Object
- #handle(args) ⇒ Object
-
#initialize ⇒ ExecutionRequestCommand
constructor
A new instance of ExecutionRequestCommand.
Methods included from CliCommand
#build_common_options, #build_option_type_options, #command_name, #default_refresh_interval, #default_subcommand, #establish_remote_appliance_connection, #full_command_usage, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_id_list, #parse_list_options, #parse_list_subtitles, #print, #print_error, #puts, #puts_error, #raise_command_error, #render_with_format, #run_command_for_each_arg, #subcommand_aliases, #subcommand_usage, #subcommands, #usage, #verify_access_token!
Constructor Details
#initialize ⇒ ExecutionRequestCommand
Returns a new instance of ExecutionRequestCommand.
18 19 20 |
# File 'lib/morpheus/cli/execution_request_command.rb', line 18 def initialize() # @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance end |
Instance Method Details
#connect(opts) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/morpheus/cli/execution_request_command.rb', line 22 def connect(opts) @api_client = establish_remote_appliance_connection(opts) # @instances_interface = @api_client.instances # @containers_interface = @api_client.containers # @servers_interface = @api_client.servers @execution_request_interface = @api_client.execution_request end |
#execute(args) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/morpheus/cli/execution_request_command.rb', line 133 def execute(args) = {} params = {} script_content = nil do_refresh = true optparse = Morpheus::Cli::OptionParser.new do|opts| opts. = subcommand_usage("[options]") opts.on('--server ID', String, "Server ID") do |val| params['serverId'] = val end opts.on('--instance ID', String, "Instance ID") do |val| params['instanceId'] = val end opts.on('--container ID', String, "Container ID") do |val| params['containerId'] = val end opts.on('--request ID', String, "Execution Request ID") do |val| params['requestId'] = val end opts.on('--script SCRIPT', "Script to be executed" ) do |val| script_content = val end opts.on('--file FILE', "File containing the script. This can be used instead of --script" ) do |filename| full_filename = File.(filename) if File.exists?(full_filename) script_content = File.read(full_filename) else print_red_alert "File not found: #{full_filename}" exit 1 end end opts.on(nil, '--no-refresh', "Do not refresh until finished" ) do do_refresh = false end #build_option_type_options(opts, options, add_user_source_option_types()) (opts, , [:options, :payload, :json, :dry_run, :quiet, :remote]) opts. = "Execute an arbitrary script." + "\n" + "[server] or [instance] or [container] is required. This is the id of a server, instance or container." + "\n" + "[script] is required. This is the script that is to be executed." end optparse.parse!(args) connect() if args.count != 0 print_error Morpheus::Terminal.angry_prompt puts_error "wrong number of arguments, expected 0 and got (#{args.count}) #{args.inspect}\n#{optparse}" return 1 end if params['serverId'].nil? && params['instanceId'].nil? && params['containerId'].nil? && params['requestId'].nil? puts_error "#{Morpheus::Terminal.angry_prompt}missing required option: --server or --instance or --container\n#{optparse}" return 1 end begin # construct payload payload = {} if [:payload] payload = [:payload] else payload.deep_merge!([:options].reject {|k,v| k.is_a?(Symbol) }) if [:options] # could prompt for Server or Container or Instance # prompt for Script if script_content.nil? v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'script', 'type' => 'code-editor', 'fieldLabel' => 'Script', 'required' => true, 'description' => 'The script content'}], [:options]) script_content = v_prompt['script'] end payload['script'] = script_content end @execution_request_interface.setopts() if [:dry_run] print_dry_run @execution_request_interface.dry.create(params, payload) return 0 end # do it json_response = @execution_request_interface.create(params, payload) # print and return result if [:quiet] return 0 elsif [:json] puts as_json(json_response, ) return 0 end execution_request = json_response['executionRequest'] print_green_success "Executing request #{execution_request['uniqueId']}" if do_refresh get([execution_request['uniqueId'], "--refresh"]) else get([execution_request['uniqueId']]) end return 0 rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |
#execute_against_lease(args) ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/morpheus/cli/execution_request_command.rb', line 227 def execute_against_lease(args) = {} params = {} do_refresh = true script_content = nil optparse = Morpheus::Cli::OptionParser.new do|opts| opts. = subcommand_usage("[uid] [options]") opts.on('--script SCRIPT', "Script to be executed" ) do |val| script_content = val end opts.on('--file FILE', "File containing the script. This can be used instead of --script" ) do |filename| full_filename = File.(filename) if File.exists?(full_filename) script_content = File.read(full_filename) else print_red_alert "File not found: #{full_filename}" exit 1 end end opts.on(nil, '--no-refresh', "Do not refresh until finished" ) do do_refresh = false end #build_option_type_options(opts, options, add_user_source_option_types()) (opts, , [:options, :payload, :json, :dry_run, :quiet, :remote]) opts. = "Execute request against lease.\n" + "[uid] is required. This is the unique id of the execution request.\n" + "[script] is required. This is the script that is to be executed." end optparse.parse!(args) connect() if args.count != 1 print_error Morpheus::Terminal.angry_prompt puts_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args.inspect}\n#{optparse}" return 1 end execution_request_id = args[0] begin # construct payload payload = {} if [:payload] payload = [:payload] else payload.deep_merge!([:options].reject {|k,v| k.is_a?(Symbol) }) if [:options] if script_content payload['script'] = script_content end end @execution_request_interface.setopts() if [:dry_run] print_dry_run @execution_request_interface.dry.execute_against_lease(execution_request_id, params, payload) return 0 end # do it json_response = @execution_request_interface.execute_against_lease(execution_request_id, params, payload) # print and return result if [:quiet] return 0 elsif [:json] puts as_json(json_response, ) return 0 end execution_request = json_response['executionRequest'] print_green_success "Executing request #{execution_request['uniqueId']} against lease" if do_refresh get([execution_request['uniqueId'], "--refresh"]) else get([execution_request['uniqueId']]) end return 0 rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |
#format_execution_request_status(execution_request, return_color = cyan) ⇒ Object
302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/morpheus/cli/execution_request_command.rb', line 302 def format_execution_request_status(execution_request, return_color=cyan) out = "" status_str = execution_request['status'] if status_str == 'complete' out << "#{green}#{status_str.upcase}#{return_color}" elsif status_str == 'failed' || status_str == 'expired' out << "#{red}#{status_str.upcase}#{return_color}" else out << "#{cyan}#{status_str.upcase}#{return_color}" end out end |
#get(args) ⇒ Object
34 35 36 37 38 39 40 41 42 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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/morpheus/cli/execution_request_command.rb', line 34 def get(args) raw_args = args = {} params = {} optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage("[uid]") (opts, , [:query, :json, :yaml, :csv, :fields, :dry_run, :remote]) opts.on('--refresh [SECONDS]', String, "Refresh until execution is finished. Default interval is #{default_refresh_interval} seconds.") do |val| [:refresh_until_finished] = true if !val.to_s.empty? [:refresh_interval] = val.to_f end end opts. = "Get details about an execution request." + "\n" + "[uid] is required. This is the unique id of an execution request." end optparse.parse!(args) connect() if args.count != 1 print_error Morpheus::Terminal.angry_prompt puts_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args.inspect}\n#{optparse}" return 1 end execution_request_id = args[0] begin params.merge!(()) @execution_request_interface.setopts() if [:dry_run] print_dry_run @execution_request_interface.dry.get(execution_request_id, params) return end json_response = @execution_request_interface.get(execution_request_id, params) if [:json] puts as_json(json_response, , "executionRequest") return 0 elsif [:yaml] puts as_yaml(json_response, , "executionRequest") return 0 elsif [:csv] puts records_as_csv([json_response['executionRequest']], ) return 0 end execution_request = json_response['executionRequest'] # refresh until a status is reached if [:refresh_until_finished] if [:refresh_interval].nil? || [:refresh_interval].to_f < 0 [:refresh_interval] = default_refresh_interval end if execution_request['exitCode'] || ['complete','failed','expired'].include?(execution_request['status']) # it is finished else print cyan print "Execution request has not yet finished. Refreshing every #{[:refresh_interval]} seconds" while execution_request['exitCode'].nil? do sleep([:refresh_interval]) print cyan,".",reset json_response = @execution_request_interface.get(execution_request_id, params) execution_request = json_response['executionRequest'] end #sleep_with_dots(options[:refresh_interval]) print "\n", reset # get(raw_args) end end print_h1 "Execution Request Details" print cyan description_cols = { #"ID" => lambda {|it| it['id'] }, "Unique ID" => lambda {|it| it['uniqueId'] }, "Server ID" => lambda {|it| it['serverId'] }, "Instance ID" => lambda {|it| it['instanceId'] }, "Container ID" => lambda {|it| it['containerId'] }, "Expires At" => lambda {|it| format_local_dt it['expiresAt'] }, "Exit Code" => lambda {|it| it['exitCode'] }, "Status" => lambda {|it| format_execution_request_status(it) }, #"Created By" => lambda {|it| it['createdById'] }, #"Subdomain" => lambda {|it| it['subdomain'] }, } print_description_list(description_cols, execution_request) if execution_request['stdErr'] print_h2 "Error" puts execution_request['stdErr'].to_s.strip end if execution_request['stdOut'] print_h2 "Output" puts execution_request['stdOut'].to_s.strip end print reset, "\n" return 0 rescue RestClient::Exception => e print_rest_exception(e, ) return 1 end end |
#handle(args) ⇒ Object
30 31 32 |
# File 'lib/morpheus/cli/execution_request_command.rb', line 30 def handle(args) handle_subcommand(args) end |