Class: Morpheus::Cli::LibraryContainerScriptsCommand
- Inherits:
-
Object
- Object
- Morpheus::Cli::LibraryContainerScriptsCommand
- Includes:
- CliCommand
- Defined in:
- lib/morpheus/cli/commands/library_container_scripts_command.rb
Instance Attribute Summary
Attributes included from CliCommand
Instance Method Summary collapse
- #_get(id, options) ⇒ Object
- #add(args) ⇒ Object
- #connect(opts) ⇒ Object
- #get(args) ⇒ Object
- #handle(args) ⇒ Object
- #list(args) ⇒ Object
- #remove(args) ⇒ Object
- #update(args) ⇒ Object
Methods included from CliCommand
#add_query_parameter, #apply_options, #build_common_options, #build_get_options, #build_list_options, #build_option_type_options, #build_standard_add_many_options, #build_standard_add_options, #build_standard_api_options, #build_standard_delete_options, #build_standard_get_options, #build_standard_list_options, #build_standard_post_options, #build_standard_put_options, #build_standard_remove_options, #build_standard_update_options, #command_description, #command_name, #confirm, #confirm!, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #execute_api, #execute_api_payload, #execute_api_request, #find_all, #find_all_json, #find_by_id, #find_by_name, #find_by_name_or_id, #find_record, #find_record_json, #full_command_usage, #get_interface, #get_list_key, #get_object_key, #get_subcommand_description, #handle_each_payload, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_array, #parse_bytes_param, #parse_get_options!, #parse_id_list, #parse_labels, #parse_list_options, #parse_list_options!, #parse_list_subtitles, #parse_options, #parse_parameter_as_resource_id!, #parse_passed_options, #parse_payload, #parse_query_options, #print, #print_error, #println, #prog_name, #puts, #puts_error, #raise_args_error, #raise_command_error, #render_response, #run_command_for_each_arg, #subcommand_aliases, #subcommand_description, #subcommand_usage, #subcommands, #usage, #validate_outfile, #verify_args!, #visible_subcommands
Instance Method Details
#_get(id, options) ⇒ Object
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/morpheus/cli/commands/library_container_scripts_command.rb', line 96 def _get(id, ) begin container_script = find_container_script_by_name_or_id(id) if container_script.nil? return 1 end @container_scripts_interface.setopts() if [:dry_run] print_dry_run @container_scripts_interface.dry.get(container_script['id']) return end json_response = @container_scripts_interface.get(container_script['id']) container_script = json_response['containerScript'] instances = json_response['instances'] || [] servers = json_response['servers'] || [] if [:json] puts as_json(json_response, , "containerScript") return 0 elsif [:yaml] puts as_yaml(json_response, , "containerScript") return 0 elsif [:csv] puts records_as_csv([json_response["containerScript"]], ) return 0 end print_h1 "Container Script Details" print cyan description_cols = { "ID" => lambda {|it| it['id'] }, "Name" => lambda {|it| it['name'] }, "Labels" => lambda {|it| format_list(it['labels']) rescue '' }, "Type" => lambda {|it| format_container_script_type(it['scriptType']) }, "Phase" => lambda {|it| format_container_script_phase(it['scriptPhase']) }, "Run As User" => lambda {|it| it['runAsUser'] }, "Sudo" => lambda {|it| format_boolean(it['sudoUser']) }, "Owner" => lambda {|it| it['account'] ? it['account']['name'] : '' }, "Created" => lambda {|it| format_local_dt(it['dateCreated']) }, "Updated" => lambda {|it| format_local_dt(it['lastUpdated']) } } print_description_list(description_cols, container_script) print_h2 "Script" puts container_script['script'] print reset,"\n" rescue RestClient::Exception => e print_rest_exception(e, ) return 1 end end |
#add(args) ⇒ Object
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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/morpheus/cli/commands/library_container_scripts_command.rb', line 153 def add(args) params = {} # {'scriptType' => 'bash', 'scriptPhase' => 'provision'} = {} optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage("[name]") opts.on('--name VALUE', String, "Name") do |val| params['name'] = val end opts.on('-l', '--labels [LIST]', String, "Labels") do |val| params['labels'] = parse_labels(val) end opts.on('-t', '--type TYPE', "Script Type. i.e. bash, powershell. Default is bash.") do |val| params['scriptType'] = val end opts.on('--phase PHASE', String, "Script Phase. i.e. start, stop, preProvision, provision, postProvision, preDeploy, deploy, reconfigure, teardown. Default is provision.") do |val| params['scriptPhase'] = val end opts.on('--script TEXT', String, "Contents of the script.") do |val| params['script'] = 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.exist?(full_filename) params['script'] = File.read(full_filename) else print_red_alert "File not found: #{full_filename}" exit 1 end # use the filename as the name by default. if !params['name'] params['name'] = File.basename(full_filename) end end opts.on("--sudo [on|off]", String, "Run with sudo") do |val| params['sudoUser'] = val.to_s == 'on' || val.to_s == 'true' || val.to_s == '1' || val.to_s == '' end opts.on("--run-as-user VALUE", String, "Run as user") do |val| params['runAsUser'] = val end # opts.on("--run-as-password VALUE", String, "Run as password") do |val| # params['runAsPassword'] = val.to_s == 'on' || val.to_s == 'true' || val.to_s == '1' || val.to_s == '' # end (opts, , [:options, :payload, :json, :dry_run, :remote, :quiet]) opts. = "Create a new container script." + "\n" + "[name] is required and can be passed as --name instead." end optparse.parse!(args) # support [name] as first argument if args[0] params['name'] = args[0] end connect() begin payload = nil = [:options] ? [:options].reject {|k,v| k.is_a?(Symbol) } : {} create_payload = {} create_payload.deep_merge!(params) create_payload.deep_merge!() if [:payload] payload = [:payload] payload.deep_merge!({'containerScript' => create_payload}) unless create_payload.empty? else prompt_result = Morpheus::Cli::OptionTypes.prompt([ {'fieldName' => 'name', 'fieldLabel' => 'Name', 'type' => 'text', 'required' => true}, {'fieldName' => 'scriptType', 'fieldLabel' => 'Type', 'type' => 'select', 'optionSource' => 'scriptTypes', 'defaultValue' => 'bash', 'required' => true}, {'fieldName' => 'scriptPhase', 'fieldLabel' => 'Phase', 'type' => 'select', 'optionSource' => 'containerPhases', 'defaultValue' => 'provision', 'required' => true}, {'fieldName' => 'script', 'fieldLabel' => 'Script', 'type' => 'code-editor', 'required' => true}, {'fieldName' => 'runAsUser', 'fieldLabel' => 'Run As User', 'type' => 'text'}, {'fieldName' => 'sudoUser', 'fieldLabel' => 'Sudo', 'type' => 'checkbox', 'defaultValue' => false}, ], params.deep_merge([:options] || {}), @api_client) create_payload.deep_merge!(prompt_result) payload = {'containerScript' => create_payload} end @container_scripts_interface.setopts() if [:dry_run] print_dry_run @container_scripts_interface.dry.create(payload) return end json_response = @container_scripts_interface.create(payload) if [:json] puts as_json(json_response, ) elsif ![:quiet] container_script = json_response['containerScript'] print_green_success "Added container script #{container_script['name']}" _get(container_script['id'], {}) end return 0 rescue RestClient::Exception => e print_rest_exception(e, ) return 1 end end |
#connect(opts) ⇒ Object
10 11 12 13 |
# File 'lib/morpheus/cli/commands/library_container_scripts_command.rb', line 10 def connect(opts) @api_client = establish_remote_appliance_connection(opts) @container_scripts_interface = @api_client.library_container_scripts end |
#get(args) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/morpheus/cli/commands/library_container_scripts_command.rb', line 78 def get(args) = {} optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage("[name]") (opts, , [:json, :yaml, :csv, :fields, :dry_run, :remote]) end optparse.parse!(args) if args.count < 1 puts optparse return 1 end connect() id_list = parse_id_list(args) return run_command_for_each_arg(id_list) do |arg| _get(arg, ) end end |
#handle(args) ⇒ Object
15 16 17 |
# File 'lib/morpheus/cli/commands/library_container_scripts_command.rb', line 15 def handle(args) handle_subcommand(args) end |
#list(args) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 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 |
# File 'lib/morpheus/cli/commands/library_container_scripts_command.rb', line 19 def list(args) = {} params = {} optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage() opts.on('-l', '--labels LABEL', String, "Filter by labels, can match any of the values") do |val| add_query_parameter(params, 'labels', parse_labels(val)) end opts.on('--all-labels LABEL', String, "Filter by labels, must match all of the values") do |val| add_query_parameter(params, 'allLabels', parse_labels(val)) end (opts, , [:list, :query, :json, :yaml, :csv, :fields, :dry_run, :remote]) opts. = "List container scripts." end optparse.parse!(args) # verify_args!(args:args, optparse:optparse, count:0) if args.count > 0 [:phrase] = args.join(" ") end connect() begin # construct payload params.merge!(()) @container_scripts_interface.setopts() if [:dry_run] print_dry_run @container_scripts_interface.dry.list(params) return end # do it json_response = @container_scripts_interface.list(params) # print result and return output if [:json] puts as_json(json_response, , "containerScripts") return 0 elsif [:csv] puts records_as_csv(json_response['containerScripts'], ) return 0 elsif [:yaml] puts as_yaml(json_response, , "containerScripts") return 0 end container_scripts = json_response['containerScripts'] title = "Morpheus Library - Scripts" subtitles = [] subtitles += parse_list_subtitles() print_h1 title, subtitles if container_scripts.empty? print cyan,"No container scripts found.",reset,"\n" else print_container_scripts_table(container_scripts, ) print_results_pagination(json_response, {:label => "container script", :n_label => "container scripts"}) end print reset,"\n" rescue RestClient::Exception => e print_rest_exception(e, ) return 1 end end |
#remove(args) ⇒ Object
335 336 337 338 339 340 341 342 343 344 345 346 347 348 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 379 380 381 |
# File 'lib/morpheus/cli/commands/library_container_scripts_command.rb', line 335 def remove(args) = {} params = {} optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage("[name]") (opts, , [:json, :dry_run, :quiet, :auto_confirm]) end optparse.parse!(args) if args.count < 1 puts optparse return 127 end connect() begin container_script = find_container_script_by_name_or_id(args[0]) if container_script.nil? return 1 end unless [:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to delete container script '#{container_script['name']}'?", ) return false end # payload = { # 'containerScript' => {id: container_script["id"]} # } # payload['containerScript'].merge!(container_script) payload = params @container_scripts_interface.setopts() if [:dry_run] print_dry_run @container_scripts_interface.dry.destroy(container_script["id"]) return end json_response = @container_scripts_interface.destroy(container_script["id"]) if [:json] puts as_json(json_response, ) elsif ![:quiet] print_green_success "Deleted container script #{container_script['name']}" end return 0, nil rescue RestClient::Exception => e print_rest_exception(e, ) return 1 end end |
#update(args) ⇒ Object
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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/morpheus/cli/commands/library_container_scripts_command.rb', line 247 def update(args) = {} params = {} optparse = Morpheus::Cli::OptionParser.new do |opts| opts. = subcommand_usage("[name]") opts.on('--name VALUE', String, "Name") do |val| params['name'] = val end opts.on('-l', '--labels [LIST]', String, "Labels") do |val| params['labels'] = parse_labels(val) end opts.on('-t', '--type TYPE', "Script Type. i.e. bash, powershell. Default is bash.") do |val| params['scriptType'] = val end opts.on('--phase PHASE', String, "Script Phase. i.e. start, stop, preProvision, provision, postProvision, preDeploy, deploy, reconfigure, teardown. Default is provision.") do |val| params['scriptPhase'] = val end opts.on('--script TEXT', String, "Contents of the script.") do |val| params['script'] = 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.exist?(full_filename) params['script'] = File.read(full_filename) else print_red_alert "File not found: #{full_filename}" exit 1 end end opts.on("--sudo [on|off]", String, "Run with sudo") do |val| params['sudoUser'] = val.to_s == 'on' || val.to_s == 'true' || val.to_s == '1' || val.to_s == '' end opts.on("--run-as-user VALUE", String, "Run as user") do |val| params['runAsUser'] = val end # opts.on("--run-as-password VALUE", String, "Run as password") do |val| # params['runAsPassword'] = val.to_s == 'on' || val.to_s == 'true' || val.to_s == '1' || val.to_s == '' # end (opts, , [:options, :payload, :json, :dry_run, :remote, :quiet]) opts. = "Update a container script." + "\n" + "[name] is required. This is the name or id of a container script." end optparse.parse!(args) if args.count != 1 print_error Morpheus::Terminal.angry_prompt puts_error "wrong number of arguments, expected 1 and got (#{args.count}) #{args.join(', ')}\n#{optparse}" return 1 end connect() begin container_script = find_container_script_by_name_or_id(args[0]) if container_script.nil? return 1 end # construct payload payload = nil if [:payload] payload = [:payload] params.deep_merge!([:options].reject {|k,v| k.is_a?(Symbol) }) if [:options] payload.deep_merge!({'containerScript' => params}) unless params.empty? else # update without prompting params.deep_merge!([:options].reject {|k,v| k.is_a?(Symbol) }) if [:options] script_payload = params if script_payload.empty? raise_command_error "Specify at least one option to update.\n#{optparse}" end payload = {'containerScript' => script_payload} end @container_scripts_interface.setopts() if [:dry_run] print_dry_run @container_scripts_interface.dry.update(container_script["id"], payload) return end json_response = @container_scripts_interface.update(container_script["id"], payload) if [:json] puts as_json(json_response, ) elsif ![:quiet] print_green_success "Updated container script #{container_script['name']}" _get(container_script['id'], {}) end return 0 rescue RestClient::Exception => e print_rest_exception(e, ) return 1 end end |