Class: Morpheus::Cli::Deployments

Inherits:
Object
  • Object
show all
Includes:
CliCommand, DeploymentsHelper
Defined in:
lib/morpheus/cli/commands/deployments.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

#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

Constructor Details

#initializeDeployments

Returns a new instance of Deployments.



14
15
16
# File 'lib/morpheus/cli/commands/deployments.rb', line 14

def initialize()
  # @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
end

Instance Method Details

#_get(id, params, options) ⇒ Object



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
# File 'lib/morpheus/cli/commands/deployments.rb', line 100

def _get(id, params, options)
  @deployments_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @deployments_interface.dry.get(id, params)
    print_dry_run @deployments_interface.dry.list_versions(id, {})
    return
  end
  json_response = @deployments_interface.get(id, params)
  deployment_versions = @deployments_interface.list_versions(id)['versions']
  deployment = json_response['deployment']
  render_response(json_response, options, 'deployment') do
    print_h1 "Deployment Details", [], options
    print cyan
    print_description_list(deployment_column_definitions, deployment)
    print_h2 "Versions", options
    if deployment_versions.empty?
      print cyan,"No versions found.",reset,"\n"
    else
      print as_pretty_table(deployment_versions, deployment_version_column_definitions.upcase_keys!, options)
      print_results_pagination({'size' => deployment_versions.size(), 'total' => deployment['versionCount']}, {:label => "version", :n_label => "versions"})
    end
    print reset,"\n"
  end
  return 0, nil
end

#add(args) ⇒ Object



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
# File 'lib/morpheus/cli/commands/deployments.rb', line 171

def add(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name] [options]")
    build_option_type_options(opts, options, add_deployment_option_types)
    build_option_type_options(opts, options, add_deployment_advanced_option_types)
    build_standard_add_options(opts, options)
    opts.footer = <<-EOT
Create a new deployment.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:0, max:1)
  options[:options]['name'] = args[0] if args[0]
  connect(options)
  payload = {}
  if options[:payload]
    payload = options[:payload]
    payload.deep_merge!({'deployment' => parse_passed_options(options)})
  else
    payload.deep_merge!({'deployment' => parse_passed_options(options)})
    v_prompt = Morpheus::Cli::OptionTypes.prompt(add_deployment_option_types, options[:options], @api_client, options[:params])
    params.deep_merge!(v_prompt)
    advanced_config = Morpheus::Cli::OptionTypes.no_prompt(add_deployment_advanced_option_types, options[:options], @api_client, options[:params])
    advanced_config.deep_compact!
    params.deep_merge!(advanced_config)
    payload['deployment'].deep_merge!(params)
  end
  @deployments_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @deployments_interface.dry.create(payload)
    return 0, nil
  end
  json_response = @deployments_interface.create(payload)
  deployment = json_response['deployment']
  render_response(json_response, options, 'deployment') do
    print_green_success "Added deployment #{deployment['name']}"
    return _get(deployment["id"], {}, options)
  end
  return 0, nil
end

#add_version(args) ⇒ Object



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'lib/morpheus/cli/commands/deployments.rb', line 388

def add_version(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[deployment] [version] [options]")
    opts.on('-t', '--type CODE', String, "Deploy Type, file, git or fetch, default is file.") do |val|
      options[:options]['deployType'] = val
    end
    build_option_type_options(opts, options, add_deployment_version_option_types)
    opts.add_hidden_option('--deployType')
    build_option_type_options(opts, options, add_deployment_version_advanced_option_types)
    build_standard_add_options(opts, options)
    opts.footer = <<-EOT
Create a new deployment version.
[deployment] is required. This is the name or id of a deployment.
[version] is required. This is the deployment version identifier
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:0, max:2)
  connect(options)
  deployment = nil
  if args[0]
    deployment = find_deployment_by_name_or_id(args[0])
    return 1 if deployment.nil?
  else
    deployment_id = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'deploymentId', 'fieldLabel' => 'Deployment', 'type' => 'select', 'required' => true, 'description' => 'Deployment to add version to', 'optionSource' => lambda { |api_client, api_params|
      @deployments_interface.list(max:10000)['deployments'].collect {|it|
        {'name' => it['name'], 'value' => it['id']}
      }
    }}], options[:options])['deploymentId']
    deployment = {'id' => deployment_id.to_i}
  end
  options[:options]['userVersion'] = args[1] if args[1]
  payload = {}
  if options[:payload]
    payload = options[:payload]
    payload.deep_merge!({'version' => parse_passed_options(options)})
  else
    payload.deep_merge!({'version' => parse_passed_options(options)})
    v_prompt = Morpheus::Cli::OptionTypes.prompt(add_deployment_version_option_types, options[:options], @api_client, options[:params])
    params.deep_merge!(v_prompt)
    advanced_config = Morpheus::Cli::OptionTypes.no_prompt(add_deployment_version_advanced_option_types, options[:options], @api_client, options[:params])
    advanced_config.deep_compact!
    params.deep_merge!(advanced_config)
    payload['version'].deep_merge!(params)
  end
  @deployments_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @deployments_interface.dry.create_version(deployment['id'], payload)
    return 0, nil
  end
  json_response = @deployments_interface.create_version(deployment['id'], payload)
  deployment_version = json_response['version']
  render_response(json_response, options, 'version') do
    print_green_success "Added deployment version #{deployment_version['userVersion']}"
    return get_version([deployment["id"], deployment_version['id']] + (options[:remote] ? ["-r",options[:remote]] : []))
  end
end

#connect(opts) ⇒ Object



18
19
20
21
# File 'lib/morpheus/cli/commands/deployments.rb', line 18

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @deployments_interface = @api_client.deployments
end

#get(args) ⇒ Object



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
# File 'lib/morpheus/cli/commands/deployments.rb', line 67

def get(args)
  params = {}
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[deployment]")
    build_standard_get_options(opts, options)
    opts.footer = <<-EOT
Get details about a specific deployment.
[deployment] is required. This is the name or id of a deployment.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:1)
  connect(options)
  id_list = parse_id_list(args)
  # lookup IDs if names are given
  id_list = id_list.collect do |id|
    if id.to_s =~ /\A\d{1,}\Z/
      id
    else
      deployment = find_deployment_by_name_or_id(id)
      if deployment
        deployment['id']
      else
        raise_command_error "deployment not found for name '#{id}'"
      end
    end
  end
  return run_command_for_each_arg(id_list) do |arg|
    _get(arg, params, options)
  end
end

#get_version(args) ⇒ Object



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
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
# File 'lib/morpheus/cli/commands/deployments.rb', line 296

def get_version(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[deployment] [version] [options]")
    opts.on(nil, '--no-files', "Do not show files") do
      options[:no_files] = true
    end
    build_option_type_options(opts, options, add_deployment_version_option_types)
    build_option_type_options(opts, options, add_deployment_version_advanced_option_types)
    build_standard_add_options(opts, options)
    opts.footer = <<-EOT
Create a new deployment version.
[deployment] is required. This is the name or id of a deployment.
[version] is required. This is the deployment version identifier
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count:2)
  connect(options)

  deployment = find_deployment_by_name_or_id(args[0])
  return 1 if deployment.nil?
  id = args[1]
  if id.to_s =~ /\A\d{1,}\Z/
    id = id.to_i
  else
    deployment_version = find_deployment_version_by_name_or_id(deployment['id'], id)
    if deployment_version
      id = deployment_version['id']
    else
      # raise_command_error "deployment not found for name '#{id}'"
      return 1, "deployment version not found for name '#{id}'"
    end
  end
  @deployments_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @deployments_interface.dry.get_version(deployment['id'], id, params)
    return
  end
  json_response = @deployments_interface.get_version(deployment['id'], id, params)
  deployment_version = json_response['version']
  deploy_type = deployment_version['deployType'] || deployment_version['type']
  deployment_files_response = nil
  deployment_files = nil
  if options[:no_files] != true
    deployment_files_response = @deployments_interface.list_files(deployment['id'], deployment_version['id'], params)
    deployment_files = deployment_files_response.is_a?(Array) ? deployment_files_response : deployment_files_response['files']
  end
  render_response(json_response, options, 'version') do
    # print_h1 "Deployment Version Details", [deployment['name']], options
    print_h1 "Deployment Version Details", [], options
    print cyan
    #columns = deployment_version_column_definitions
    columns = {
      "ID" => 'id',
      "Deployment" => lambda {|it| deployment['name'] },
      "Version" => lambda {|it| format_deployment_version_number(it) },
      "Deploy Type" => lambda {|it| it['deployType'] },
      "URL" => lambda {|it| it['fetchUrl'] || it['gitUrl'] || it['url'] },
      "Ref" => lambda {|it| it['gitRef'] || it['ref'] },
      "Created" => lambda {|it| format_local_dt(it['dateCreated']) },
      "Updated" => lambda {|it| format_local_dt(it['lastUpdated']) },
    }
    if deployment_version['deployType'] == 'git'
      options[:no_files] = true
    elsif deployment_version['deployType'] == 'fetch'
      options[:no_files] = true
      columns['Fetch URL'] = columns['URL']
      columns.delete('Ref')
    else
      columns.delete('URL')
      columns.delete('Ref')
    end
    print_description_list(columns, deployment_version)
    print reset,"\n"

    if options[:no_files] != true
      print_h2 "Deployment Files", options
      if !deployment_files || deployment_files.empty?
        print cyan,"No files found.",reset,"\n"
      else
        print as_pretty_table(deployment_files, deployment_file_column_definitions.upcase_keys!, options)
        print_results_pagination({size:deployment_files.size,total:deployment_files.size.to_i})
      end
      print reset,"\n"
    end

  end
  return 0, nil
end

#handle(args) ⇒ Object



23
24
25
# File 'lib/morpheus/cli/commands/deployments.rb', line 23

def handle(args)
  handle_subcommand(args)
end

#list(args) ⇒ Object



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
# File 'lib/morpheus/cli/commands/deployments.rb', line 27

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 = "List deployments."
  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))
  @deployments_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @deployments_interface.dry.list(params)
    return
  end
  json_response = @deployments_interface.list(params)
  deployments = json_response['deployments']
  render_response(json_response, options, 'deployments') do
    print_h1 "Morpheus Deployments", parse_list_subtitles(options), options
    if deployments.empty?
      print cyan,"No deployments found.",reset,"\n"
    else
      print as_pretty_table(deployments, deployment_column_definitions.upcase_keys!, options)
      print_results_pagination(json_response)
    end
    print reset,"\n"
  end
  if deployments.empty?
    return 1, "no deployments found"
  else
    return 0, nil
  end
end

#list_files(args) ⇒ Object



530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
# File 'lib/morpheus/cli/commands/deployments.rb', line 530

def list_files(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[deployment] [version] [path] [options]")
    build_standard_list_options(opts, options)
    opts.footer = <<-EOT
List files in a deployment version.
[deployment] is required. This is the name or id of a deployment.
[version] is required. This is the deployment version identifier
[path] is optional. This is a the directory to search for files under.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:2, max: 3)
  connect(options)
  params.merge!(parse_list_options(options))
  deployment = find_deployment_by_name_or_id(args[0])
  return 1, "deployment not found for '#{args[0]}'" if deployment.nil?
  deployment_version = find_deployment_version_by_name_or_id(deployment['id'], args[1])
  return 1, "deployment version not found for '#{args[1]}'" if deployment_version.nil?
  if args[2]
    params['filePath'] = args[2]
  end
  @deployments_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @deployments_interface.dry.list_files(deployment['id'], deployment_version['id'], params)
    return
  end
  json_response = @deployments_interface.list_files(deployment['id'], deployment_version['id'], params)
  # odd, api used to just return an array
  deployment_files = json_response.is_a?(Array) ? json_response : json_response['files']
  render_response(json_response, options) do
    print_h1 "Deployment Files", ["#{deployment['name']} #{format_deployment_version_number(deployment_version)}"]
    if !deployment_files || deployment_files.empty?
      print cyan,"No files found.",reset,"\n"
    else
      print as_pretty_table(deployment_files, deployment_file_column_definitions.upcase_keys!, options)
      #print_results_pagination(json_response)
      print_results_pagination({size:deployment_files.size,total:deployment_files.size.to_i})
    end
    print reset,"\n"
  end
  return 0, nil
end

#list_versions(args) ⇒ Object



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
# File 'lib/morpheus/cli/commands/deployments.rb', line 126

def list_versions(args)
  params = {}
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[deployment] [search]")
    build_standard_list_options(opts, options)
    opts.footer = <<-EOT
List versions of a specific deployment.
[deployment] is required. This is the name or id of a deployment.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:1,max:2)
  deployment_name  = args[0]
  if args.count > 1
    options[:phrase] = args[1]
  end
  connect(options)
  params.merge!(parse_list_options(options))
  @deployments_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @deployments_interface.dry.list(params)
    return
  end
  deployment = find_deployment_by_name_or_id(deployment_name)
  exit 1 if deployment.nil?
  json_response = @deployments_interface.list_versions(deployment['id'], params)
  deployment_versions = json_response['versions']
  render_response(json_response, options, 'versions') do
    print_h1 "Deployment Versions", ["#{deployment['name']}"] + parse_list_subtitles(options), options
    if deployment_versions.empty?
      print cyan,"No versions found.",reset,"\n"
    else
      print as_pretty_table(deployment_versions, deployment_version_column_definitions.upcase_keys!, options)
      print_results_pagination(json_response)
    end
    print reset,"\n"
  end
  if deployment_versions.empty?
    return 1, "no versions found"
  else
    return 0, nil
  end
end

#remove(args) ⇒ Object



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
# File 'lib/morpheus/cli/commands/deployments.rb', line 265

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

#remove_file(args) ⇒ Object



764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
# File 'lib/morpheus/cli/commands/deployments.rb', line 764

def remove_file(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[deployment] [version] [file] [options]")
    opts.on( '-R', '--recursive', "Delete a directory and all of its files. This must be passed if specifying a directory." ) do
      # do_recursive = true
      params['force'] = true
    end
    opts.on( '-f', '--force', "Force delete, this will do a recursive delete of directories." ) do
      params['force'] = true
    end
    build_standard_remove_options(opts, options)
    opts.footer = <<-EOT
Delete a deployment file.
[deployment] is required. This is the name or id of a deployment.
[version] is required. This is the version identifier of a deployment version.
[file] is required. This is the name of the file to be deleted.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:2, max:3)
  connect(options)
  deployment = find_deployment_by_name_or_id(args[0])
  return 1, "deployment not found" if deployment.nil?
  id = args[1]
  deployment_version = find_deployment_version_by_name_or_id(deployment['id'], id)
  return 1, "version not found" if deployment_version.nil?
  # could look it up here, or allow a directory instead of a single file
  filename = args[2]
  if filename.nil?
    #raise_command_error "Files not specified. Please specify files array, each item may specify a path or pattern of file(s) to upload", args, optparse
    filename = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'file', 'fieldLabel' => 'Files', 'type' => 'text', 'required' => true, 'description' => 'Files or directories to upload'}], options[:options])['file'].to_s #.split(",").collect {|it| it.to_s.strip }.select { |it| it != "" }
  end
  unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the file #{filename}?")
    return 9, "aborted command"
  end
  @deployments_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @deployments_interface.dry.destroy_file(deployment['id'], deployment_version['id'], filename, params)
    return
  end
  json_response = @deployments_interface.destroy_file(deployment['id'], deployment_version['id'], filename, params)
  render_response(json_response, options) do
    print_green_success "Removed deployment file #{filename}"
  end
  return 0, nil
end

#remove_version(args) ⇒ Object



495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'lib/morpheus/cli/commands/deployments.rb', line 495

def remove_version(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[deployment] [version] [options]")
    build_standard_remove_options(opts, options)
    opts.footer = <<-EOT
Delete a deployment version.
[deployment] is required. This is the name or id of a deployment.
[version] is required. This is the version identifier of a deployment version.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count:2)
  connect(options)
  deployment = find_deployment_by_name_or_id(args[0])
  return 1, "deployment not found" if deployment.nil?
  id = args[1]
  deployment_version = find_deployment_version_by_name_or_id(deployment['id'], id)
  return 1, "version not found" if deployment_version.nil?
  unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the deployment version #{format_deployment_version_number(deployment_version)}?")
    return 9, "aborted command"
  end
  @deployments_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @deployments_interface.dry.destroy_version(deployment['id'], deployment_version['id'], params)
    return
  end
  json_response = @deployments_interface.destroy_version(deployment['id'], deployment_version['id'], params)
  render_response(json_response, options) do
    print_green_success "Removed deployment #{deployment['name']} version #{format_deployment_version_number(deployment_version)}"
  end
  return 0, nil
end

#update(args) ⇒ Object



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
# File 'lib/morpheus/cli/commands/deployments.rb', line 214

def update(args)
  options = {}
  params = {}
  payload = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[deployment] [options]")
    build_option_type_options(opts, options, update_deployment_option_types)
    build_option_type_options(opts, options, update_deployment_advanced_option_types)
    build_standard_update_options(opts, options)
    opts.footer = <<-EOT
Update a deployment.
[deployment] is required. This is the name or id of a deployment.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count:1)
  connect(options)
  deployment = find_deployment_by_name_or_id(args[0])
  return 1 if deployment.nil?
  payload = {}
  if options[:payload]
    payload = options[:payload]
    payload.deep_merge!({'deployment' => parse_passed_options(options)})
  else
    payload.deep_merge!({'deployment' => parse_passed_options(options)})
    # do not prompt on update
    v_prompt = Morpheus::Cli::OptionTypes.no_prompt(update_deployment_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_deployment_advanced_option_types, options[:options], @api_client, options[:params])
    advanced_config.deep_compact!
    params.deep_merge!(advanced_config)
    payload.deep_merge!({'deployment' => params})
    if payload['deployment'].empty? # || options[:no_prompt]
      raise_command_error "Specify at least one option to update.\n#{optparse}"
    end
  end
  @deployments_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @deployments_interface.dry.update(deployment['id'], payload)
    return
  end
  json_response = @deployments_interface.update(deployment['id'], payload)
  deployment = json_response['deployment']
  render_response(json_response, options, 'deployment') do
    print_green_success "Updated deployment #{deployment['name']}"
    return _get(deployment["id"], {}, options)
  end
  return 0, nil
end

#update_version(args) ⇒ Object



448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/morpheus/cli/commands/deployments.rb', line 448

def update_version(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[deployment] [version] [options]")
    build_option_type_options(opts, options, update_deployment_version_option_types)
    build_option_type_options(opts, options, update_deployment_version_advanced_option_types)
    build_standard_update_options(opts, options)
    opts.footer = <<-EOT
Update a deployment version.
[deployment] is required. This is the name or id of a deployment.
[version] is required. This is the deployment version identifier
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, min:0, max:2)
  connect(options)
  deployment = find_deployment_by_name_or_id(args[0])
  return 1 if deployment.nil?
  deployment_version = find_deployment_version_by_name_or_id(deployment['id'], args[1])
  return 1 if deployment_version.nil?
  payload = {}
  if options[:payload]
    payload = options[:payload]
    payload.deep_merge!({'version' => parse_passed_options(options)})
  else
    payload.deep_merge!({'version' => parse_passed_options(options)})
    v_prompt = Morpheus::Cli::OptionTypes.no_prompt(update_deployment_version_option_types, options[:options], @api_client, options[:params])
    params.deep_merge!(v_prompt)
    advanced_config = Morpheus::Cli::OptionTypes.no_prompt(update_deployment_version_advanced_option_types, options[:options], @api_client, options[:params])
    advanced_config.deep_compact!
    params.deep_merge!(advanced_config)
    payload['version'].deep_merge!(params)
  end
  @deployments_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @deployments_interface.dry.update_version(deployment['id'], deployment_version['id'], payload)
    return 0, nil
  end
  json_response = @deployments_interface.update_version(deployment['id'], deployment_version['id'], payload)
  deployment_version = json_response['version']
  render_response(json_response, options, 'version') do
    print_green_success "Updated deployment version #{deployment_version['userVersion']}"
    return get_version([deployment["id"], deployment_version['id']] + (options[:remote] ? ["-r",options[:remote]] : []))
  end
end

#upload(args) ⇒ Object



576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
# File 'lib/morpheus/cli/commands/deployments.rb', line 576

def upload(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[deployment] [version] [files]")
    opts.on('--files LIST', String, "Files to upload") do |val|
      val_list = val.to_s.split(",").collect {|it| it.to_s.strip }.select { |it| it != "" }
      options[:files] ||= []
      options[:files] += val_list
    end
    opts.on('--workdir DIRECTORY', String, "Working directory to switch to before uploading files, determines the paths of the uploaded files. The current working directory of your terminal is used by default.") do |val|
      options[:workdir] = File.expand_path(val)
      if !File.directory?(options[:workdir])
        raise_command_error "invalid directory: #{val}"
      end
    end
    opts.on('--destination FILEPATH', String, "Destination filepath for file being uploaded, should include full filename and extension. Only applies when uploading a single file.") do |val|
      options[:destination] = val
    end
    build_standard_update_options(opts, options, [:auto_confirm])
    opts.footer = <<-EOT
Upload one or more files or directories to a deployment version.
[deployment] is required. This is the name or id of a deployment.
[version] is required. This is the deployment version identifier
[files] is required. This is a list of files or directories to be uploaded. Glob pattern format supported eg. build/*.html
EOT
  end
  optparse.parse!(args)
  # verify_args!(args:args, optparse:optparse, min:0, max:2)
  connect(options)

  # fetch deployment
  deployment = nil
  if args[0]
    deployment = find_deployment_by_name_or_id(args[0])
    return 1 if deployment.nil?
  else
    all_deployments = @deployments_interface.list(max:10000)['deployments']
    deployment_id = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'deployment', 'fieldLabel' => 'Deployment', 'type' => 'select', 'required' => true, 'description' => 'Deployment identifier (name or ID)', 'optionSource' => lambda { |api_client, api_params|
      all_deployments.collect {|it| {'name' => it['name'], 'value' => it['id']} }
    }}], options[:options])['deployment']
    deployment = all_deployments.find {|it| deployment_id == it['id'] || deployment_id == it['name'] }
    raise_command_error "Deployment not found for '#{deployment_id}'" if deployment.nil?
  end

  # fetch deployment version
  deployment_version = nil
  if args[1]
    deployment_version = find_deployment_version_by_name_or_id(deployment['id'], args[1])
    return 1 if deployment_version.nil?
  else
    all_deployment_versions = @deployments_interface.list_versions(deployment['id'], {max:10000})['versions']
    deployment_version_id = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'version', 'fieldLabel' => 'Version', 'type' => 'select', 'required' => true, 'description' => 'Deployment Version identifier (version or ID) to upload files to', 'optionSource' => lambda { |api_client, api_params|
      all_deployment_versions.collect {|it| {'name' => it['version'] || it['userVersion'], 'value' => it['id']} }
    }}], options[:options])['version']
    deployment_version = all_deployment_versions.find {|it| deployment_version_id == it['id'] || deployment_version_id == it['userVersion'] || deployment_version_id == it['version'] }
    raise_command_error "Deployment Version not found for '#{deployment_version_id}'" if deployment_version.nil?
  end


  # Determine which files to find
  file_patterns = []
  # [files] is args 3 - N
  if args.size > 2
    file_patterns += args[2..-1]
  end
  if options[:files]
    file_patterns += options[:files]
  end
  if file_patterns.empty?
    #raise_command_error "Files not specified. Please specify files array, each item may specify a path or pattern of file(s) to upload", args, optparse
    file_patterns = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'files', 'fieldLabel' => 'Files', 'type' => 'text', 'required' => true, 'description' => 'Files or directories to upload'}], options[:options])['files'].to_s.split(",").collect {|it| it.to_s.strip }.select { |it| it != "" }
  end

  # Find Files to Upload
  deploy_files = []
  
  #print "\n",cyan, "Finding Files...", reset, "\n" unless options[:quiet]
  original_working_dir = Dir.pwd
  base_working_dir = options[:workdir] || original_working_dir
  begin
    file_patterns.each do |file_pattern|
      # start in the working directory
      # to preserve relative paths in upload file destinations
      # allow passing just build  instead  build/**/*
      Dir.chdir(base_working_dir)
      fmap = nil
      full_file_pattern = File.expand_path(file_pattern)
      if File.exist?(full_file_pattern)
        if File.directory?(full_file_pattern)
          fmap = {'path' => full_file_pattern, 'pattern' => '**/*'}
        else
          fmap = {'path' => File.dirname(full_file_pattern), 'pattern' => File.basename(full_file_pattern)}
        end
      else
        fmap = {'path' => nil, 'pattern' => file_pattern}
      end
      if fmap['path']
        Dir.chdir(File.expand_path(fmap['path']))
      end
      files = Dir.glob(fmap['pattern'] || '**/*')
      if files.empty?
        raise_command_error "Found 0 files for file pattern '#{file_pattern}'"
      end
      files.each do |file|
        if File.file?(file)
          destination = file.split("/")[0..-2].join("/")
          # deploy_files << {filepath: File.expand_path(file), destination: destination}
          # absolute path was given, so no path is given to the destination file
          # maybe apply options[:destination] as prefix here
          # actually just do destination.sub!(base_working_dir, '')
          if file[0].chr == "/"
            deploy_files << {filepath: File.expand_path(file), destination: File.basename(file)}
          else
            deploy_files << {filepath: File.expand_path(file), destination: file}
          end
        end
      end
    end
    #print cyan, "Found #{deploy_files.size} Files to Upload!", reset, "\n"
  rescue => ex
    # does not happen, just in case
    #print_error "An error occured while searching for files to upload: #{ex}"
    raise ex
  ensure
    Dir.chdir(original_working_dir)
  end
    
  # make sure we have something to upload.
  if deploy_files.empty?
    raise_command_error "0 files found for: #{file_patterns.join(', ')}"
  else
    unless options[:quiet]
      print cyan, "Found #{deploy_files.size} Files to Upload!", reset, "\n"
    end
  end

  # support uploading a local file to a custom destination
  # this only works for a single file right now, should be better
  # could try to add destination + filename
  # for now expect filename to be included in destination
  if options[:destination]
    if deploy_files.size == 1
      deploy_files[0][:destination] = options[:destination]
    else
      raise_command_error "--destination can only specified for a single file upload, not #{deploy_files} files.", args, optparse
    end
  end

  confirm_message = "Are you sure you want to upload #{deploy_files.size} files to deployment #{deployment['name']} #{format_deployment_version_number(deployment_version)}?"
  if deploy_files.size == 1
    confirm_message = "Are you sure you want to upload file #{deploy_files[0][:destination]} to deployment #{deployment['name']} #{format_deployment_version_number(deployment_version)}?"
  end
  unless options[:yes] || Morpheus::Cli::OptionTypes.confirm(confirm_message)
    return 9, "aborted command"
  end

  @deployments_interface.setopts(options)

  # Upload Files
  if deploy_files && !deploy_files.empty?
    print "\n",cyan, "Uploading #{deploy_files.size} Files...", reset, "\n" if !options[:quiet]
    deploy_files.each do |f|
      destination = f[:destination]
      if options[:dry_run]
        print_dry_run @deployments_interface.upload_file(deployment['id'], deployment_version['id'], f[:filepath], f[:destination])
      else
        print cyan,"  - Uploading #{f[:destination]} ...", reset if !options[:quiet]
        upload_result = @deployments_interface.upload_file(deployment['id'], deployment_version['id'], f[:filepath], f[:destination])
        #print green + "SUCCESS" + reset + "\n" if !options[:quiet]
        print reset, "\n" if !options[:quiet]
      end
    end
    if options[:dry_run]
      return 0, nil
    end
    #print cyan, "Upload Complete!", reset, "\n" if !options[:quiet]
    if options[:quiet]
      return 0, nil
    else
      print_green_success "Upload Complete!"
      return get_version([deployment["id"], deployment_version['id']] + (options[:remote] ? ["-r",options[:remote]] : []))
    end
  else
    raise_command_error "No files to upload!"
  end
end