Class: Morpheus::Cli::CurlCommand

Inherits:
Object
  • Object
show all
Includes:
CliCommand
Defined in:
lib/morpheus/cli/commands/standard/curl_command.rb

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

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!

Instance Method Details

#command_available?(cmd) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
120
121
122
123
124
125
126
# File 'lib/morpheus/cli/commands/standard/curl_command.rb', line 117

def command_available?(cmd)
  has_it = false
  begin
    system("which #{cmd} > /dev/null 2>&1")
    has_it = $?.success?
  rescue => e
    raise e
  end
  return has_it
end

#handle(args) ⇒ Object



11
12
13
14
15
16
17
18
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
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
# File 'lib/morpheus/cli/commands/standard/curl_command.rb', line 11

def handle(args)
  split_args = args.join(" ").split(" -- ")
  args = split_args[0].split(" ")
  curl_args = split_args[1] ? split_args[1].split(" ") : []
  # puts "args is : #{args}"
  # puts "curl_args is : #{curl_args}"
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = "Usage: morpheus curl [path] -- [*args]"
    opts.on( '-p', '--pretty', "Print result as parsed JSON." ) do
      options[:pretty] = true
    end
    build_common_options(opts, options, [:dry_run, :remote])
    opts.add_hidden_option('--curl')
    #opts.add_hidden_option('--scrub')
    opts.footer = <<-EOT
This invokes the `curl` command with url "appliance_url/api/$0
and includes the authorization header -H "Authorization: Bearer access_token"
Arguments for the curl command should be passed after ' -- '
Example: morpheus curl "/api/servers/1" -- -XGET -sv

EOT
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    return false
  end
  
  if !command_available?("curl")
    print "#{red}The 'curl' command is not available on your system.#{reset}\n"
    return false
  end
  
  @api_client = establish_remote_appliance_connection(options.merge({:no_prompt => true, :skip_verify_access_token => true}))

  if !@appliance_name
    print yellow,"Please specify a Morpheus Appliance with -r or see the command `remote use`#{reset}\n"
    return false
  end

  # curry --insecure to curl
  if options[:insecure] || !Morpheus::RestClient.ssl_verification_enabled?
    #curl_args.unshift "-k"
    curl_args.unshift "--insecure"
  end

  if !@appliance_url
    raise "Unable to determine remote appliance url"
    print "#{red}Unable to determine remote appliance url.#{reset}\n"
    return false
  end

  # determine curl url
  url = nil
  api_path = args[0].to_s.strip
  # allow absolute path for the current appliance url only
  if api_path.match(/^#{Regexp.escape(@appliance_url)}/)
    url = api_path
  else
    api_path = api_path.sub(/^\//, "") # strip leading slash
    url = "#{@appliance_url.chomp('/')}/#{api_path}"
  end
  curl_cmd = "curl \"#{url}\""
  if @access_token
    curl_cmd << " -H \"Authorization: Bearer #{@access_token}\""
  end
  if !curl_args.empty?
    curl_cmd << " " + curl_args.join(' ')
  end
  
  # Morpheus::Logging::DarkPrinter.puts "#{curl_cmd}" if Morpheus::Logging.debug?
  curl_cmd_str = options[:scrub] ? Morpheus::Logging.scrub_message(curl_cmd) : curl_cmd

  if options[:dry_run]
    print cyan
    print "#{cyan}#{curl_cmd_str}#{reset}"
    print "\n\n"
    print reset
    return 0
  end
  print cyan
  print "#{cyan}#{curl_cmd_str}#{reset}"
  print "\n\n"
  print reset
  # print result
  curl_output = `#{curl_cmd}`
  if options[:pretty]
    output_lines = curl_output.split("\n")
    last_line = output_lines.pop
    puts output_lines.join("\n")
    begin
      json_data = JSON.parse(last_line)
      json_string = JSON.pretty_generate(json_data)
      puts json_string
    rescue => ex
      Morpheus::Logging::DarkPrinter.puts "failed to parse curl result as JSON data Error: #{ex.message}" if Morpheus::Logging.debug?
      puts last_line
    end
  else
    puts curl_output
  end
  return $?.success?

end