604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
|
# File 'lib/morpheus/cli/containers_command.rb', line 604
def execution_request(args)
options = {}
params = {}
script_content = nil
do_refresh = true
optparse = Morpheus::Cli::OptionParser.new do|opts|
opts.banner = subcommand_usage("[id] [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.expand_path(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_common_options(opts, options, [:options, :payload, :json, :dry_run, :quiet, :remote])
opts. = "Execute an arbitrary command or script on a container." + "\n" +
"[id] is required. This is the id a container." + "\n" +
"[script] is required. This is the script that is to be executed."
end
optparse.parse!(args)
connect(options)
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
begin
container = find_container_by_id(args[0])
return 1 if container.nil?
params['containerId'] = container['id']
payload = {}
if options[:payload]
payload = options[:payload]
else
payload.deep_merge!(options[:options].reject {|k,v| k.is_a?(Symbol) }) if options[:options]
if script_content.nil?
v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'script', 'type' => 'code-editor', 'fieldLabel' => 'Script', 'required' => true, 'description' => 'The script content'}], options[:options])
script_content = v_prompt['script']
end
payload['script'] = script_content
end
@execution_request_interface.setopts(options)
if options[:dry_run]
print_dry_run @execution_request_interface.dry.create(params, payload)
return 0
end
json_response = @execution_request_interface.create(params, payload)
if options[:quiet]
return 0
elsif options[:json]
puts as_json(json_response, options)
return 0
end
execution_request = json_response['executionRequest']
print_green_success "Executing request #{execution_request['uniqueId']}"
if do_refresh
Morpheus::Cli::ExecutionRequestCommand.new.handle(["get", execution_request['uniqueId'], "--refresh"]+ (options[:remote] ? ["-r",options[:remote]] : []))
else
Morpheus::Cli::ExecutionRequestCommand.new.handle(["get", execution_request['uniqueId']]+ (options[:remote] ? ["-r",options[:remote]] : []))
end
return 0
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|