Class: Morpheus::Cli::Deploys

Inherits:
Object
  • Object
show all
Includes:
CliCommand, DeploymentsHelper
Defined in:
lib/morpheus/cli/deploys.rb

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from DeploymentsHelper

#deployment_list_key, #deployment_object_key, #deployment_version_list_key, #deployment_version_object_key, #deployments_interface, #find_deployment_by_id, #find_deployment_by_name, #find_deployment_by_name_or_id, #find_deployment_type_by_name, #find_deployment_version_by_id, #find_deployment_version_by_name, #find_deployment_version_by_name_or_id, #format_app_deploy_status, #format_deploy_type, #format_deployment_version_number, included

Methods included from CliCommand

#apply_options, #build_common_options, #build_option_type_options, #build_standard_add_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, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #full_command_usage, #get_subcommand_description, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_bytes_param, #parse_id_list, #parse_list_options, #parse_list_subtitles, #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, params, options) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/morpheus/cli/deploys.rb', line 85

def _get(id, params, options)
  @deploy_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @deploy_interface.dry.get(id, params)
    return 0
  end
  json_response = @deploy_interface.get(id, params)
  app_deploy = json_response[app_deploy_object_key]
  deploy_config = app_deploy['config']
  render_response(json_response, options, app_deploy_object_key) do
    print_h1 "Deploy Details", [], options
    print cyan
    print_description_list(app_deploy_column_definitions, app_deploy)

    if options[:details] && deploy_config && !deploy_config.empty?
      print_h2 "Config", options
      print cyan
      print as_description_list(deploy_config, deploy_config.keys, options)
    end

    print reset,"\n"
  end
  return 0, nil
end

#add(args) ⇒ Object



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
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
# File 'lib/morpheus/cli/deploys.rb', line 110

def add(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[instance] [deployment] [version] [options]")
    build_option_type_options(opts, options, add_app_deploy_option_types)
    build_option_type_options(opts, options, add_app_deploy_advanced_option_types)
    opts.on(nil, "--stage", "Stage Only, do not run the deployment right away.") do |val|
      params['stageOnly'] = true
    end
    opts.on("-c", "--config JSON", String, "Config for deployment") do |val|
      parse_result = parse_json_or_yaml(val)
      config_map = parse_result[:data]
      if config_map.nil?
        # todo: bubble up JSON.parse error message
        raise_command_error "Failed to parse config as YAML or JSON. Error: #{parse_result[:err]}"
      else
        params['config'] = config_map
        options[:options]['config'] = params['config'] # or file_content
      end
    end
    opts.on('--config-file FILE', String, "Config from a local JSON or YAML file") do |val|
      options[:config_file] = val.to_s
      file_content = nil
      full_filename = File.expand_path(options[:config_file])
      if File.exists?(full_filename)
        file_content = File.read(full_filename)
      else
        print_red_alert "File not found: #{full_filename}"
        return 1
      end
      parse_result = parse_json_or_yaml(file_content)
      config_map = parse_result[:data]
      if config_map.nil?
        # todo: bubble up JSON.parse error message
        raise_command_error "Failed to parse config as YAML or JSON. Error: #{parse_result[:err]}"
        #raise_command_error "Failed to parse config as valid YAML or JSON."
      else
        params['config'] = config_map
        options[:options]['config'] = params['config'] # or file_content
      end
    end
    build_standard_add_options(opts, options)
    opts.footer = <<-EOT
Create a new instance deploy.
This deploys a specific deployment version to a target instance.
By default the deployment is run right away, unless the --stage option is used.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:0, max:3)
  options[:options]['instance'] = args[0] if args[0]
  options[:options]['deployment'] = args[1] if args[1]
  options[:options]['version'] = args[2] if args[2]
  connect(options)
  payload = {}
  if options[:payload]
    payload = options[:payload]
    payload.deep_merge!({app_deploy_object_key => parse_passed_options(options)})
  else
    payload.deep_merge!({app_deploy_object_key => parse_passed_options(options)})
    v_prompt = Morpheus::Cli::OptionTypes.prompt(add_app_deploy_option_types, options[:options], @api_client, options[:params])
    params.deep_merge!(v_prompt)
    advanced_config = Morpheus::Cli::OptionTypes.no_prompt(add_app_deploy_advanced_option_types, options[:options], @api_client, options[:params])
    advanced_config.deep_compact!
    params.deep_merge!(advanced_config)
    payload[app_deploy_object_key].deep_merge!(params)
  end
  @deploy_interface.setopts(options)
  instance_id = payload[app_deploy_object_key]['instance']
  if options[:dry_run]
    print_dry_run @deploy_interface.dry.create(instance_id, payload)
    return 0, nil
  end
  # unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to deploy?")
  #   return 9, "aborted command"
  # end
  json_response = @deploy_interface.create(instance_id, payload)
  app_deploy = json_response[app_deploy_object_key]
  render_response(json_response, options, app_deploy_object_key) do
    if app_deploy['status'] == 'staged'
      begin
        print_green_success "Staged Deploy #{app_deploy['deployment']['name']} version #{app_deploy['deploymentVersion']['userVersion']} to instance #{app_deploy['instance']['name']}"
      rescue => ex
        print_green_success "Staged Deploy"
      end
    else
      begin
        print_green_success "Deploying #{app_deploy['deployment']['name']} version #{app_deploy['deploymentVersion']['userVersion']} to instance #{app_deploy['instance']['name']}"
      rescue => ex
        print_green_success "Deploying"
      end
    end
    return _get(app_deploy["id"], {}, options)
  end
  return 0, nil
end

#connect(opts) ⇒ Object



12
13
14
15
16
17
# File 'lib/morpheus/cli/deploys.rb', line 12

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @instances_interface = @api_client.instances
  @deploy_interface = @api_client.deploy
  @deployments_interface = @api_client.deployments
end

#deploy(args) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
333
334
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/morpheus/cli/deploys.rb', line 322

def deploy(args)
  options = {}
  params = {}
  payload = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[id] [options]")
    opts.on("-c", "--config JSON", String, "Config for deployment") do |val|
      parse_result = parse_json_or_yaml(val)
      config_map = parse_result[:data]
      if config_map.nil?
        # todo: bubble up JSON.parse error message
        raise_command_error "Failed to parse config as YAML or JSON. Error: #{parse_result[:err]}"
      else
        params['config'] = config_map
        options[:options]['config'] = params['config'] # or file_content
      end
    end
    opts.on('--config-file FILE', String, "Config from a local JSON or YAML file") do |val|
      options[:config_file] = val.to_s
      file_content = nil
      full_filename = File.expand_path(options[:config_file])
      if File.exists?(full_filename)
        file_content = File.read(full_filename)
      else
        print_red_alert "File not found: #{full_filename}"
        return 1
      end
      parse_result = parse_json_or_yaml(file_content)
      config_map = parse_result[:data]
      if config_map.nil?
        # todo: bubble up JSON.parse error message
        raise_command_error "Failed to parse config as YAML or JSON. Error: #{parse_result[:err]}"
        #raise_command_error "Failed to parse config as valid YAML or JSON."
      else
        params['config'] = config_map
        options[:options]['config'] = params['config'] # or file_content
      end
    end
    #build_option_type_options(opts, options, update_app_deploy_option_types)
    #build_option_type_options(opts, options, update_app_deploy_advanced_option_types)
    build_standard_update_options(opts, options, [:auto_confirm])
    opts.footer = <<-EOT
Deploy an instance deploy.
[id] is required. This is the id of an instance deploy.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count:1)
  connect(options)
  app_deploy = find_app_deploy_by_id(args[0])
  return 1 if app_deploy.nil?
  payload = {}
  if options[:payload]
    payload = options[:payload]
    payload.deep_merge!({app_deploy_object_key => parse_passed_options(options)})
  else
    payload.deep_merge!({app_deploy_object_key => parse_passed_options(options)})
    # do not prompt on update
    #v_prompt = Morpheus::Cli::OptionTypes.no_prompt(update_app_deploy_option_types, options[:options], @api_client, options[:params])
    #v_prompt.deep_compact!
    #params.deep_merge!(v_prompt)
    #advanced_config = Morpheus::Cli::OptionTypes.no_prompt(update_app_deploy_advanced_option_types, options[:options], @api_client, options[:params])
    #advanced_config.deep_compact!
    #params.deep_merge!(advanced_config)
    payload.deep_merge!({app_deploy_object_key => params})
    # if payload[app_deploy_object_key].empty? # || options[:no_prompt]
    #   raise_command_error "Specify at least one option to update.\n#{optparse}"
    # end
  end
  unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to deploy #{app_deploy['deployment']['name']} version #{app_deploy['deploymentVersion']['userVersion']} to instance #{app_deploy['instance']['name']}?")
    return 9, "aborted command"
  end
  @deploy_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @deploy_interface.dry.update(app_deploy['id'], payload)
    return
  end
  json_response = @deploy_interface.deploy(app_deploy['id'], payload)
  app_deploy = json_response[app_deploy_object_key]
  render_response(json_response, options, app_deploy_object_key) do
    print_green_success "Deploying..."
    return _get(app_deploy["id"], {}, options)
  end
  return 0, nil
end

#get(args) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/morpheus/cli/deploys.rb', line 65

def get(args)
  params = {}
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[id]")
    build_standard_get_options(opts, options, [:details])
    opts.footer = <<-EOT
Get details about a specific instance deploy.
[id] is required. This is the id of an instance deploy.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:1)
  connect(options)
  id_list = parse_id_list(args)
  return run_command_for_each_arg(id_list) do |arg|
    _get(arg, params, options)
  end
end

#handle(args) ⇒ Object



19
20
21
# File 'lib/morpheus/cli/deploys.rb', line 19

def handle(args)
  handle_subcommand(args)
end

#list(args) ⇒ Object



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
# File 'lib/morpheus/cli/deploys.rb', line 23

def list(args)
  options = {}
  params = {}
  ref_ids = []
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[search]")
    build_standard_list_options(opts, options)
    opts.footer = <<-EOT
List deploys.
EOT
  end
  optparse.parse!(args)
  connect(options)
  # verify_args!(args:args, optparse:optparse, count:0)
  if args.count > 0
    options[:phrase] = args.join(" ")
  end
  params.merge!(parse_list_options(options))
  @deploy_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @deploy_interface.dry.list(params)
    return
  end
  json_response = @deploy_interface.list(params)
  app_deploys = json_response[app_deploy_list_key]
  render_response(json_response, options, app_deploy_list_key) do
    print_h1 "Morpheus Deploys", parse_list_subtitles(options), options
    if app_deploys.empty?
      print cyan,"No deploys found.",reset,"\n"
    else
      print as_pretty_table(app_deploys, app_deploy_column_definitions.upcase_keys!, options)
      print_results_pagination(json_response)
    end
    print reset,"\n"
  end
  if app_deploys.empty?
    return 1, "no deploys found"
  else
    return 0, nil
  end
end

#remove(args) ⇒ Object



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
# File 'lib/morpheus/cli/deploys.rb', line 291

def remove(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[id] [options]")
    build_standard_remove_options(opts, options)
    opts.footer = <<-EOT
Delete an instance deploy.
[id] is required. This is the id of an instance deploy.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count:1)
  connect(options)
  app_deploy = find_app_deploy_by_id(args[0])
  return 1 if app_deploy.nil?
  unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the deploy #{app_deploy['id']}?")
    return 9, "aborted command"
  end
  @deploy_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @deploy_interface.dry.destroy(app_deploy['id'], params)
    return
  end
  json_response = @deploy_interface.destroy(app_deploy['id'], params)
  render_response(json_response, options) do
    print_green_success "Removed deploy #{app_deploy['id']}"
  end
  return 0, nil
end

#update(args) ⇒ Object



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
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
# File 'lib/morpheus/cli/deploys.rb', line 208

def update(args)
  options = {}
  params = {}
  payload = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[id] [options]")
    opts.on("-c", "--config JSON", String, "Config for deployment") do |val|
      parse_result = parse_json_or_yaml(val)
      config_map = parse_result[:data]
      if config_map.nil?
        # todo: bubble up JSON.parse error message
        raise_command_error "Failed to parse config as YAML or JSON. Error: #{parse_result[:err]}"
      else
        params['config'] = config_map
        options[:options]['config'] = params['config'] # or file_content
      end
    end
    opts.on('--config-file FILE', String, "Config from a local JSON or YAML file") do |val|
      options[:config_file] = val.to_s
      file_content = nil
      full_filename = File.expand_path(options[:config_file])
      if File.exists?(full_filename)
        file_content = File.read(full_filename)
      else
        print_red_alert "File not found: #{full_filename}"
        return 1
      end
      parse_result = parse_json_or_yaml(file_content)
      config_map = parse_result[:data]
      if config_map.nil?
        # todo: bubble up JSON.parse error message
        raise_command_error "Failed to parse config as YAML or JSON. Error: #{parse_result[:err]}"
        #raise_command_error "Failed to parse config as valid YAML or JSON."
      else
        params['config'] = config_map
        options[:options]['config'] = params['config'] # or file_content
      end
    end
    #build_option_type_options(opts, options, update_app_deploy_option_types)
    #build_option_type_options(opts, options, update_app_deploy_advanced_option_types)
    build_standard_update_options(opts, options)
    opts.footer = <<-EOT
Update an instance deploy.
[id] is required. This is the id of an instance deploy.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count:1)
  connect(options)
  app_deploy = find_app_deploy_by_id(args[0])
  return 1 if app_deploy.nil?
  payload = {}
  if options[:payload]
    payload = options[:payload]
    payload.deep_merge!({app_deploy_object_key => parse_passed_options(options)})
  else
    payload.deep_merge!({app_deploy_object_key => parse_passed_options(options)})
    # do not prompt on update
    #v_prompt = Morpheus::Cli::OptionTypes.no_prompt(update_app_deploy_option_types, options[:options], @api_client, options[:params])
    #v_prompt.deep_compact!
    #params.deep_merge!(v_prompt)
    #advanced_config = Morpheus::Cli::OptionTypes.no_prompt(update_app_deploy_advanced_option_types, options[:options], @api_client, options[:params])
    #advanced_config.deep_compact!
    #params.deep_merge!(advanced_config)
    payload.deep_merge!({app_deploy_object_key => params})
    if payload[app_deploy_object_key].empty? # || options[:no_prompt]
      raise_command_error "Specify at least one option to update.\n#{optparse}"
    end
  end
  @deploy_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @deploy_interface.dry.update(app_deploy['id'], payload)
    return
  end
  json_response = @deploy_interface.update(app_deploy['id'], payload)
  app_deploy = json_response[app_deploy_object_key]
  render_response(json_response, options, app_deploy_object_key) do
    print_green_success "Deploying..."
    return _get(app_deploy["id"], {}, options)
  end
  return 0, nil
end