morpheus-cli 2.10.1 → 2.10.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ad4ff693efbb10952b74f63eafd3ceacfe382d5e
4
- data.tar.gz: f5fc0c7184848496e5fdcfedb6f08c38d0353c81
3
+ metadata.gz: ecd262f3f7122ce8af78b08d5539dc450db6dafa
4
+ data.tar.gz: b0c4136d4746c82f05d1602f10797dd632f7c081
5
5
  SHA512:
6
- metadata.gz: 7193e79c98e5a13b6e7f60a686167eae26872dc8d62410099aa58ded65f73ce2d77430389b822cc818504ef382c837b620ba20ffe34c46315214bc37d020070a
7
- data.tar.gz: 5dd1cde1959ea0dbcc0f2ade4805b2a23959d0edf450069a558eb454f0ca48a70eac75a19695d257a457917db76f783d13098eab72bff2dbcfbe47ab66d0309d
6
+ metadata.gz: 40e0e43ff256acc748caa4577e1231b733c2c25e725a52e122b61602b092a5a541464afbcddf7010494f6e1901c973df6fce291a6961c0ed6100f5e076848f29
7
+ data.tar.gz: 214f1a02d4d192ec428d13a6bec8d1e33de2aa89f05cf51c8f94e7876bca9169a049fe7c8fbcf649ff357e9ec655dd1be00eec4fb5ea499e897a74e0564a8751
@@ -4,15 +4,18 @@ require 'optparse'
4
4
  require 'morpheus/cli'
5
5
  require 'morpheus/rest_client'
6
6
  require 'morpheus/cli/cli_registry'
7
- require 'morpheus/cli/config_file'
7
+ require 'morpheus/cli/dot_file'
8
8
  require 'morpheus/cli/error_handler'
9
9
 
10
+ # setup environment variables
11
+ Morpheus::Cli.home_directory = ENV['MORPHEUS_CLI_HOME'] || File.join(Dir.home, ".morpheus")
12
+
10
13
  # use colors by default
11
14
  Term::ANSIColor::coloring = STDOUT.isatty
12
15
  # include Term::ANSIColor # tempting
13
16
 
14
17
  # short circuit version switch
15
- if ARGV[0] == '-v'
18
+ if ARGV[0] == '-v' || ARGV[0] == '--version'
16
19
  version = Morpheus::Cli::VERSION
17
20
  puts version
18
21
  exit 0
@@ -20,35 +23,71 @@ end
20
23
 
21
24
  args = ARGV.dup
22
25
 
26
+ # global options
27
+
23
28
  # raise log level right away
24
29
  if args.find {|it| it == '-V' || it == '--debug'}
25
30
  Morpheus::Logging.set_log_level(Morpheus::Logging::Logger::DEBUG)
26
31
  end
27
32
 
28
- # load config file(s)
29
- Morpheus::Cli::ConfigFile.init(ENV['MORPHEUS_CLI_HOME'])
33
+ # execute startup script .morpheus_profile unless --noprofile is passed
34
+ noprofile = false
35
+ if args.find {|it| it == '--noprofile' }
36
+ noprofile = true
37
+ args.delete_if {|it| it == '--noprofile' }
38
+ end
39
+ if !noprofile && File.exists?(Morpheus::Cli::DotFile.morpheus_profile_filename)
40
+ Morpheus::Cli::DotFile.new(Morpheus::Cli::DotFile.morpheus_profile_filename).execute()
41
+ end
30
42
 
31
43
  # all commands should be registered commands or aliases
32
- if Morpheus::Cli::CliRegistry.has_command?(args[0]) || Morpheus::Cli::CliRegistry.has_alias?(args[0])
33
- begin
34
- cmd_result = Morpheus::Cli::CliRegistry.exec(args[0], args[1..-1])
35
- if cmd_result == false
36
- exit 1
37
- else
38
- exit 0
44
+ if (args.count == 0) || !(Morpheus::Cli::CliRegistry.has_command?(args[0]) || Morpheus::Cli::CliRegistry.has_alias?(args[0]))
45
+ # just for printing help. todo: start using this. maybe in class Cli::MainCommand
46
+ optparse = Morpheus::Cli::OptionParser.new do|opts|
47
+ opts.banner = "Options:" # hack alert
48
+ opts.on('-v','--version', "Print the version.") do
49
+ puts Morpheus::Cli::VERSION
50
+ exit
51
+ end
52
+ opts.on('--noprofile','--noprofile', "Do not read and execute the personal initialization script .morpheus_profile") do
53
+ @noprofile = true
54
+ end
55
+ opts.on('-C','--nocolor', "Disable ANSI coloring") do
56
+ Term::ANSIColor::coloring = false
57
+ end
58
+ opts.on('-V','--debug', "Print extra output for debugging. ") do |json|
59
+ Morpheus::Logging.set_log_level(Morpheus::Logging::Logger::DEBUG)
60
+ ::RestClient.log = Morpheus::Logging.debug? ? STDOUT : nil
61
+ end
62
+ opts.on( '-h', '--help', "Prints this help" ) do
63
+ puts opts
64
+ exit
39
65
  end
40
- rescue => e
41
- Morpheus::Cli::ErrorHandler.new.handle_error(e)
42
- exit 1
43
66
  end
44
- else
45
- puts "Usage: morpheus [command] [options]"
46
- puts "\nCommands:"
67
+ out = "Usage: morpheus [command] [options]\n"
68
+ out << "Commands:\n"
47
69
  Morpheus::Cli::CliRegistry.all.keys.sort.each {|cmd|
48
- puts "\t#{cmd.to_s}"
70
+ out << "\t#{cmd.to_s}\n"
49
71
  }
50
- puts ""
51
- puts "For more information, see https://github.com/gomorpheus/morpheus-cli/wiki"
52
- puts ""
72
+ # out << "Options:\n"
73
+ out << optparse.to_s
74
+ out << "\n"
75
+ out << "For more information, see https://github.com/gomorpheus/morpheus-cli/wiki"
76
+ out << "\n"
77
+ puts out
78
+ optparse.to_s
53
79
  exit 127
54
80
  end
81
+
82
+ # ok, execute the command (or alias)
83
+ begin
84
+ cmd_result = Morpheus::Cli::CliRegistry.exec(args[0], args[1..-1])
85
+ if cmd_result == false
86
+ exit 1
87
+ else
88
+ exit 0
89
+ end
90
+ rescue => e
91
+ Morpheus::Cli::ErrorHandler.new.handle_error(e)
92
+ exit 1
93
+ end
@@ -26,6 +26,8 @@ class Morpheus::APIClient
26
26
  # JD: could return a Request object instead...
27
27
  return opts
28
28
  end
29
+ # puts "#{Term::ANSIColor.dark} #=> Morpheus::RestClient.execute(#{opts})#{Term::ANSIColor.reset}" if Morpheus::Logging.debug?
30
+ # instead, using ::RestClient.log = STDOUT
29
31
  response = Morpheus::RestClient.execute(opts)
30
32
  if parse_json
31
33
  return JSON.parse(response.to_s)
@@ -138,6 +138,14 @@ class Morpheus::InstancesInterface < Morpheus::APIClient
138
138
  execute(opts)
139
139
  end
140
140
 
141
+ def clone(id, options)
142
+ url = "#{@base_url}/api/instances/#{id}/clone"
143
+ headers = {:authorization => "Bearer #{@access_token}", 'Content-Type' => 'application/json' }
144
+ payload = options
145
+ opts = {method: :put, url: url, headers: headers, payload: payload.to_json}
146
+ execute(opts)
147
+ end
148
+
141
149
  def firewall_disable(id)
142
150
  url = "#{@base_url}/api/instances/#{id}/security-groups/disable"
143
151
  headers = { :authorization => "Bearer #{@access_token}", 'Content-Type' => 'application/json' }
@@ -10,20 +10,20 @@ module Morpheus
10
10
  module Cli
11
11
 
12
12
  # the home directory, where morpheus-cli stores things
13
+ def self.home_directory=(fn)
14
+ @@home_directory = fn
15
+ end
16
+
13
17
  def self.home_directory
14
- if ENV['MORPHEUS_CLI_HOME']
15
- ENV['MORPHEUS_CLI_HOME']
18
+ if @@home_directory
19
+ @@home_directory
20
+ elsif ENV['MORPHEUS_CLI_HOME']
21
+ @@home_directory = ENV['MORPHEUS_CLI_HOME']
16
22
  else
17
- File.join(Dir.home, ".morpheus")
23
+ @@home_directory = File.join(Dir.home, ".morpheus")
18
24
  end
19
25
  end
20
26
 
21
- # the location of your config file
22
- # this is not configurable right now.
23
- def self.config_filename
24
- File.join(self.home_directory, ".morpheusrc")
25
- end
26
-
27
27
  # load all the well known commands and utilties they need
28
28
  def self.load!()
29
29
  # load interfaces
@@ -37,10 +37,19 @@ module Morpheus
37
37
  # Dir[File.dirname(__FILE__) + "/cli/*.rb"].each {|file| load file }
38
38
 
39
39
  # utilites
40
- load 'morpheus/cli/credentials.rb'
40
+ require 'morpheus/cli/cli_registry.rb'
41
+ require 'morpheus/cli/dot_file.rb'
41
42
  load 'morpheus/cli/cli_command.rb'
42
43
  load 'morpheus/cli/option_types.rb'
44
+ load 'morpheus/cli/credentials.rb'
43
45
 
46
+ # shell scripting commands
47
+ load 'morpheus/cli/source_command.rb'
48
+ load 'morpheus/cli/echo_command.rb'
49
+ load 'morpheus/cli/coloring_command.rb'
50
+ load 'morpheus/cli/log_level_command.rb'
51
+ load 'morpheus/cli/ssl_verification_command.rb'
52
+
44
53
  # all the known commands
45
54
  load 'morpheus/cli/remote.rb'
46
55
  load 'morpheus/cli/login.rb'
@@ -33,7 +33,7 @@ class Morpheus::Cli::Accounts
33
33
  options = {}
34
34
  optparse = OptionParser.new do|opts|
35
35
  opts.banner = subcommand_usage()
36
- build_common_options(opts, options, [:list, :json])
36
+ build_common_options(opts, options, [:list, :json, :remote, :dry_run])
37
37
  end
38
38
  optparse.parse!(args)
39
39
  connect(options)
@@ -42,7 +42,10 @@ class Morpheus::Cli::Accounts
42
42
  [:phrase, :offset, :max, :sort, :direction].each do |k|
43
43
  params[k] = options[k] unless options[k].nil?
44
44
  end
45
-
45
+ if options[:dry_run]
46
+ print_dry_run @accounts_interface.dry.list(params)
47
+ return
48
+ end
46
49
  json_response = @accounts_interface.list(params)
47
50
  accounts = json_response['accounts']
48
51
  if options[:json]
@@ -68,7 +71,7 @@ class Morpheus::Cli::Accounts
68
71
  options = {}
69
72
  optparse = OptionParser.new do|opts|
70
73
  opts.banner = subcommand_usage("[name]")
71
- build_common_options(opts, options, [:json])
74
+ build_common_options(opts, options, [:json, :remote, :dry_run])
72
75
  end
73
76
  optparse.parse!(args)
74
77
  if args.count < 1
@@ -77,7 +80,14 @@ class Morpheus::Cli::Accounts
77
80
  end
78
81
  connect(options)
79
82
  begin
80
-
83
+ if options[:dry_run]
84
+ if args[0].to_s =~ /\A\d{1,}\Z/
85
+ print_dry_run @accounts_interface.dry.get(args[0].to_i)
86
+ else
87
+ print_dry_run @accounts_interface.dry.list({name:args[0]})
88
+ end
89
+ return
90
+ end
81
91
  account = find_account_by_name_or_id(args[0])
82
92
  exit 1 if account.nil?
83
93
 
@@ -119,7 +129,8 @@ class Morpheus::Cli::Accounts
119
129
  options = {}
120
130
  optparse = OptionParser.new do|opts|
121
131
  opts.banner = subcommand_usage("[options]")
122
- build_common_options(opts, options, [:options, :json])
132
+ build_option_type_options(opts, options, add_account_option_types)
133
+ build_common_options(opts, options, [:options, :json, :remote, :dry_run])
123
134
  end
124
135
  optparse.parse!(args)
125
136
  connect(options)
@@ -141,6 +152,10 @@ class Morpheus::Cli::Accounts
141
152
  account_payload['role'] = {id: role['id']}
142
153
  end
143
154
  request_payload = {account: account_payload}
155
+ if options[:dry_run]
156
+ print_dry_run @accounts_interface.dry.create(request_payload)
157
+ return
158
+ end
144
159
  json_response = @accounts_interface.create(request_payload)
145
160
  if options[:json]
146
161
  print JSON.pretty_generate(json_response)
@@ -160,10 +175,12 @@ class Morpheus::Cli::Accounts
160
175
  options = {}
161
176
  optparse = OptionParser.new do|opts|
162
177
  opts.banner = subcommand_usage("[name] [options]")
163
- build_common_options(opts, options, [:options, :json])
178
+ build_option_type_options(opts, options, update_account_option_types)
179
+ build_common_options(opts, options, [:options, :json, :remote, :dry_run])
164
180
  end
165
181
  optparse.parse!(args)
166
182
  if args.count < 1
183
+ print_red_alert "Specify atleast one option to update"
167
184
  puts optparse
168
185
  exit 1
169
186
  end
@@ -177,8 +194,6 @@ class Morpheus::Cli::Accounts
177
194
 
178
195
  if params.empty?
179
196
  puts optparse
180
- option_lines = update_account_option_types.collect {|it| "\t-O #{it['fieldName']}=\"value\"" }.join("\n")
181
- puts "\nAvailable Options:\n#{option_lines}\n\n"
182
197
  exit 1
183
198
  end
184
199
 
@@ -198,6 +213,10 @@ class Morpheus::Cli::Accounts
198
213
  account_payload['role'] = {id: role['id']}
199
214
  end
200
215
  request_payload = {account: account_payload}
216
+ if options[:dry_run]
217
+ print_dry_run @accounts_interface.dry.update(account['id'], request_payload)
218
+ return
219
+ end
201
220
  json_response = @accounts_interface.update(account['id'], request_payload)
202
221
 
203
222
  if options[:json]
@@ -218,7 +237,7 @@ class Morpheus::Cli::Accounts
218
237
  options = {}
219
238
  optparse = OptionParser.new do|opts|
220
239
  opts.banner = subcommand_usage("[name]")
221
- build_common_options(opts, options, [:auto_confirm, :json])
240
+ build_common_options(opts, options, [:auto_confirm, :json, :remote, :dry_run])
222
241
  end
223
242
  optparse.parse!(args)
224
243
  if args.count < 1
@@ -233,6 +252,10 @@ class Morpheus::Cli::Accounts
233
252
  unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the account #{account['name']}?")
234
253
  exit
235
254
  end
255
+ if options[:dry_run]
256
+ print_dry_run @accounts_interface.dry.destroy(account['id'])
257
+ return
258
+ end
236
259
  json_response = @accounts_interface.destroy(account['id'])
237
260
  if options[:json]
238
261
  print JSON.pretty_generate(json_response)
@@ -5,12 +5,12 @@ require 'json'
5
5
 
6
6
  # This command allows the creation of an alias
7
7
  # these aliases are stored in the $MORPHEUS_CLI_HOME/.morpheusrc
8
- # See Morpheus::Cli::ConfigFile
8
+ # See Morpheus::Cli::DotFile
9
9
  #
10
10
  class Morpheus::Cli::AliasCommand
11
11
  include Morpheus::Cli::CliCommand
12
12
  set_command_name :alias
13
- register_subcommands :add, :remove, :list
13
+ register_subcommands :add, :export, :remove, :list
14
14
  #set_default_subcommand :add
15
15
 
16
16
  def initialize()
@@ -37,12 +37,15 @@ class Morpheus::Cli::AliasCommand
37
37
 
38
38
  def add(args)
39
39
  options = {}
40
- do_remove = false
40
+ do_export = false
41
41
  optparse = Morpheus::Cli::OptionParser.new do|opts|
42
42
  opts.banner = usage
43
43
  #build_common_options(opts, options, [])
44
+ opts.on( '-e', '--export', "Export this alias to your .morpheus_profile for future use" ) do
45
+ do_export = true
46
+ end
44
47
  opts.on('-h', '--help', "Prints this help" ) do
45
- puts opts.banner
48
+ puts opts
46
49
  puts "Commands:"
47
50
  subcommands.sort.each {|cmd, method|
48
51
  puts "\t#{cmd.to_s}"
@@ -53,7 +56,7 @@ class Morpheus::Cli::AliasCommand
53
56
  "The [command] must be quoted if it is more than one word.\n" +
54
57
  "The [command] may include multiple commands, semicolon delimited.\n" +
55
58
  #"Example: alias cloud='clouds' .\n" +
56
- "Aliases are preserved for future use in your config.\n" +
59
+ "Aliases can be exported for future use with the -e option.\n" +
57
60
  "You can use just `alias` instead of `alias add`.\n" +
58
61
  "For more information, see https://github.com/gomorpheus/morpheus-cli/wiki/Alias"
59
62
  exit
@@ -76,24 +79,31 @@ class Morpheus::Cli::AliasCommand
76
79
  begin
77
80
  Morpheus::Cli::CliRegistry.instance.add_alias(alias_name, command_string)
78
81
  #print "registered alias #{alias_name}", "\n"
82
+ if do_export
83
+ puts "exporting alias '#{alias_name}' now..."
84
+ morpheus_profile = Morpheus::Cli::DotFile.new(Morpheus::Cli::DotFile.morpheus_profile_filename)
85
+ morpheus_profile.export_aliases({(alias_name) => command_string})
86
+ end
79
87
  rescue => err
88
+ raise err
80
89
  print_red_alert "#{err.message}"
81
90
  return false
82
91
  end
83
- Morpheus::Cli::ConfigFile.instance.save_file()
84
92
  end
85
93
 
86
- Morpheus::Cli::Shell.instance.recalculate_auto_complete_commands()
94
+ if Morpheus::Cli::Shell.instance
95
+ Morpheus::Cli::Shell.instance.recalculate_auto_complete_commands()
96
+ end
87
97
 
88
98
  end
89
99
 
90
- def remove(args)
100
+ def export(args)
91
101
  options = {}
92
- do_remove = false
102
+ do_export = false
93
103
  optparse = Morpheus::Cli::OptionParser.new do|opts|
94
- opts.banner = subcommand_usage("[alias1] [alias2]")
104
+ opts.banner = subcommand_usage("[alias] [alias2] [alias3]")
95
105
  build_common_options(opts, options, [])
96
- opts.footer = "This is how you remove alias definitions from your config."
106
+ opts.footer = "Export an alias, saving it to your .morpheus_profile for future use"
97
107
  end
98
108
  optparse.parse!(args)
99
109
  if args.count < 1
@@ -104,20 +114,56 @@ class Morpheus::Cli::AliasCommand
104
114
  alias_names.each do |arg|
105
115
  if !Morpheus::Cli::CliRegistry.has_alias?(arg)
106
116
  print_red_alert "alias not found by name '#{arg}'"
107
- exit 1
117
+ return false
108
118
  end
109
119
  end
120
+ alias_definitions = {}
121
+ alias_names.each do |alias_name|
122
+ alias_definitions[alias_name] = Morpheus::Cli::CliRegistry.instance.get_alias(alias_name)
123
+ end
124
+ morpheus_profile = Morpheus::Cli::DotFile.new(Morpheus::Cli::DotFile.morpheus_profile_filename)
125
+ morpheus_profile.export_aliases(alias_definitions)
126
+
127
+ # Morpheus::Cli::Shell.instance.recalculate_auto_complete_commands() if Morpheus::Cli::Shell.instance
110
128
 
129
+ end
130
+
131
+ def remove(args)
132
+ options = {}
133
+ optparse = Morpheus::Cli::OptionParser.new do|opts|
134
+ opts.banner = subcommand_usage("[alias1] [alias2]")
135
+ build_common_options(opts, options, [])
136
+ opts.footer = "This is how you remove alias definitions from your .morpheus_profile" + "\n"
137
+ "Pass one or more alias names to remove."
138
+ end
139
+ optparse.parse!(args)
140
+ if args.count < 1
141
+ puts optparse
142
+ exit 1
143
+ end
144
+ alias_names = args
111
145
  alias_names.each do |arg|
112
- Morpheus::Cli::CliRegistry.instance.remove_alias(arg)
146
+ if !Morpheus::Cli::CliRegistry.has_alias?(arg)
147
+ print_red_alert "alias not found by name '#{arg}'"
148
+ return false
149
+ end
113
150
  end
151
+ morpheus_profile = Morpheus::Cli::DotFile.new(Morpheus::Cli::DotFile.morpheus_profile_filename)
152
+ morpheus_profile.remove_aliases(alias_names)
114
153
 
115
- Morpheus::Cli::ConfigFile.instance.save_file()
116
- if args.count == 1
117
- puts "removed alias '#{alias_names[0]}'"
118
- else
119
- puts "removed aliases '#{alias_names.join(', ')}'"
154
+ # unregister them
155
+ alias_names.each do |alias_name|
156
+ Morpheus::Cli::CliRegistry.instance.remove_alias(alias_name)
120
157
  end
158
+
159
+ # if args.count == 1
160
+ # puts "removed alias '#{alias_names[0]}'"
161
+ # else
162
+ # puts "removed aliases '#{alias_names.join(', ')}'"
163
+ # end
164
+
165
+ Morpheus::Cli::Shell.instance.recalculate_auto_complete_commands() if Morpheus::Cli::Shell.instance
166
+
121
167
  end
122
168
 
123
169
  def list(args)
@@ -125,8 +171,8 @@ class Morpheus::Cli::AliasCommand
125
171
  do_remove = false
126
172
  optparse = Morpheus::Cli::OptionParser.new do|opts|
127
173
  opts.banner = subcommand_usage()
128
- opts.on( '-E', '--export', "Generate output that can be used verbatim in a .morpheusrc config file." ) do
129
- options[:format] = 'export'
174
+ opts.on( '-f', '--format FORMAT', "The format for the output: export, json, friendly (default)." ) do |val|
175
+ options[:format] = val
130
176
  end
131
177
  build_common_options(opts, options, [:list, :json])
132
178
  opts.footer = "This outputs a list of your defined aliases."
@@ -184,9 +230,9 @@ class Morpheus::Cli::AliasCommand
184
230
  out << "\n"
185
231
  elsif options[:format] == 'export' || options[:format] == 'config'
186
232
  # out << "# morpheus aliases for #{`whoami`}\n" # windows!
187
- out << "# morpheus aliases\n"
233
+ #out << "# morpheus aliases\n"
188
234
  my_aliases.each do |it|
189
- out << "#{it[:name]}='#{it[:command_string]}'"
235
+ out << "alias #{it[:name]}='#{it[:command_string]}'"
190
236
  out << "\n"
191
237
  end
192
238
  else