bosh_cli 1.0.3 → 1.5.0.pre.1113

Sign up to get free protection for your applications and to get access to all the features.
Files changed (144) hide show
  1. data/bin/bosh +0 -9
  2. data/lib/cli.rb +69 -64
  3. data/lib/cli/backup_destination_path.rb +33 -0
  4. data/lib/cli/base_command.rb +57 -56
  5. data/lib/cli/blob_manager.rb +12 -12
  6. data/lib/cli/changeset_helper.rb +6 -7
  7. data/lib/cli/client/director.rb +724 -0
  8. data/lib/cli/command_handler.rb +6 -7
  9. data/lib/cli/commands/backup.rb +39 -0
  10. data/lib/cli/commands/biff.rb +42 -21
  11. data/lib/cli/commands/blob_management.rb +1 -1
  12. data/lib/cli/commands/cloudcheck.rb +11 -13
  13. data/lib/cli/commands/deployment.rb +53 -37
  14. data/lib/cli/commands/help.rb +3 -2
  15. data/lib/cli/commands/job_management.rb +67 -103
  16. data/lib/cli/commands/job_rename.rb +6 -8
  17. data/lib/cli/commands/log_management.rb +78 -55
  18. data/lib/cli/commands/maintenance.rb +36 -30
  19. data/lib/cli/commands/misc.rb +72 -51
  20. data/lib/cli/commands/package.rb +2 -2
  21. data/lib/cli/commands/property_management.rb +10 -12
  22. data/lib/cli/commands/release.rb +236 -133
  23. data/lib/cli/commands/snapshot.rb +93 -0
  24. data/lib/cli/commands/ssh.rb +216 -213
  25. data/lib/cli/commands/stemcell.rb +46 -34
  26. data/lib/cli/commands/task.rb +2 -2
  27. data/lib/cli/commands/user.rb +27 -3
  28. data/lib/cli/commands/vm.rb +28 -0
  29. data/lib/cli/commands/vms.rb +81 -23
  30. data/lib/cli/config.rb +6 -2
  31. data/lib/cli/core_ext.rb +31 -30
  32. data/lib/cli/deployment_helper.rb +134 -159
  33. data/lib/cli/deployment_manifest.rb +66 -0
  34. data/lib/cli/deployment_manifest_compiler.rb +0 -3
  35. data/lib/cli/event_log_renderer.rb +10 -10
  36. data/lib/cli/file_with_progress_bar.rb +52 -0
  37. data/lib/cli/job_builder.rb +1 -1
  38. data/lib/cli/job_command_args.rb +23 -0
  39. data/lib/cli/job_property_collection.rb +4 -7
  40. data/lib/cli/job_property_validator.rb +22 -12
  41. data/lib/cli/job_state.rb +54 -0
  42. data/lib/cli/line_wrap.rb +54 -0
  43. data/lib/cli/packaging_helper.rb +10 -10
  44. data/lib/cli/release.rb +18 -15
  45. data/lib/cli/release_builder.rb +9 -4
  46. data/lib/cli/release_compiler.rb +9 -9
  47. data/lib/cli/release_tarball.rb +3 -6
  48. data/lib/cli/resurrection.rb +31 -0
  49. data/lib/cli/runner.rb +56 -30
  50. data/lib/cli/stemcell.rb +25 -10
  51. data/lib/cli/task_log_renderer.rb +1 -1
  52. data/lib/cli/task_tracker.rb +10 -9
  53. data/lib/cli/validation.rb +3 -1
  54. data/lib/cli/version.rb +1 -1
  55. data/lib/cli/version_calc.rb +5 -18
  56. data/lib/cli/versions_index.rb +1 -1
  57. data/lib/cli/vm_state.rb +43 -0
  58. data/lib/cli/yaml_helper.rb +26 -35
  59. metadata +75 -208
  60. data/Rakefile +0 -56
  61. data/lib/cli/director.rb +0 -628
  62. data/spec/assets/biff/bad_gateway_config.yml +0 -28
  63. data/spec/assets/biff/good_simple_config.yml +0 -63
  64. data/spec/assets/biff/good_simple_golden_config.yml +0 -63
  65. data/spec/assets/biff/good_simple_template.erb +0 -69
  66. data/spec/assets/biff/ip_out_of_range.yml +0 -63
  67. data/spec/assets/biff/multiple_subnets_config.yml +0 -40
  68. data/spec/assets/biff/network_only_template.erb +0 -34
  69. data/spec/assets/biff/no_cc_config.yml +0 -27
  70. data/spec/assets/biff/no_range_config.yml +0 -27
  71. data/spec/assets/biff/no_subnet_config.yml +0 -16
  72. data/spec/assets/biff/ok_network_config.yml +0 -30
  73. data/spec/assets/biff/properties_template.erb +0 -6
  74. data/spec/assets/config/atmos/config/final.yml +0 -6
  75. data/spec/assets/config/atmos/config/private.yml +0 -4
  76. data/spec/assets/config/bad-providers/config/final.yml +0 -5
  77. data/spec/assets/config/bad-providers/config/private.yml +0 -4
  78. data/spec/assets/config/deprecation/config/final.yml +0 -5
  79. data/spec/assets/config/deprecation/config/private.yml +0 -2
  80. data/spec/assets/config/local/config/final.yml +0 -5
  81. data/spec/assets/config/local/config/private.yml +0 -1
  82. data/spec/assets/config/s3/config/final.yml +0 -5
  83. data/spec/assets/config/s3/config/private.yml +0 -5
  84. data/spec/assets/config/swift-hp/config/final.yml +0 -6
  85. data/spec/assets/config/swift-hp/config/private.yml +0 -7
  86. data/spec/assets/config/swift-rackspace/config/final.yml +0 -6
  87. data/spec/assets/config/swift-rackspace/config/private.yml +0 -6
  88. data/spec/assets/deployment.MF +0 -0
  89. data/spec/assets/plugins/bosh/cli/commands/echo.rb +0 -43
  90. data/spec/assets/plugins/bosh/cli/commands/ruby.rb +0 -24
  91. data/spec/assets/release/jobs/cacher.tgz +0 -0
  92. data/spec/assets/release/jobs/cacher/config/file1.conf +0 -0
  93. data/spec/assets/release/jobs/cacher/config/file2.conf +0 -0
  94. data/spec/assets/release/jobs/cacher/job.MF +0 -6
  95. data/spec/assets/release/jobs/cacher/monit +0 -1
  96. data/spec/assets/release/jobs/cleaner.tgz +0 -0
  97. data/spec/assets/release/jobs/cleaner/job.MF +0 -4
  98. data/spec/assets/release/jobs/cleaner/monit +0 -1
  99. data/spec/assets/release/jobs/sweeper.tgz +0 -0
  100. data/spec/assets/release/jobs/sweeper/config/test.conf +0 -1
  101. data/spec/assets/release/jobs/sweeper/job.MF +0 -5
  102. data/spec/assets/release/jobs/sweeper/monit +0 -1
  103. data/spec/assets/release/packages/mutator.tar.gz +0 -0
  104. data/spec/assets/release/packages/stuff.tgz +0 -0
  105. data/spec/assets/release/release.MF +0 -17
  106. data/spec/assets/release_invalid_checksum.tgz +0 -0
  107. data/spec/assets/release_invalid_jobs.tgz +0 -0
  108. data/spec/assets/release_no_name.tgz +0 -0
  109. data/spec/assets/release_no_version.tgz +0 -0
  110. data/spec/assets/stemcell/image +0 -1
  111. data/spec/assets/stemcell/stemcell.MF +0 -6
  112. data/spec/assets/stemcell_invalid_mf.tgz +0 -0
  113. data/spec/assets/stemcell_no_image.tgz +0 -0
  114. data/spec/assets/valid_release.tgz +0 -0
  115. data/spec/assets/valid_stemcell.tgz +0 -0
  116. data/spec/spec_helper.rb +0 -28
  117. data/spec/unit/base_command_spec.rb +0 -87
  118. data/spec/unit/biff_spec.rb +0 -172
  119. data/spec/unit/blob_manager_spec.rb +0 -288
  120. data/spec/unit/cache_spec.rb +0 -36
  121. data/spec/unit/cli_commands_spec.rb +0 -356
  122. data/spec/unit/config_spec.rb +0 -125
  123. data/spec/unit/core_ext_spec.rb +0 -81
  124. data/spec/unit/dependency_helper_spec.rb +0 -52
  125. data/spec/unit/deployment_manifest_compiler_spec.rb +0 -63
  126. data/spec/unit/deployment_manifest_spec.rb +0 -153
  127. data/spec/unit/director_spec.rb +0 -471
  128. data/spec/unit/director_task_spec.rb +0 -48
  129. data/spec/unit/event_log_renderer_spec.rb +0 -171
  130. data/spec/unit/hash_changeset_spec.rb +0 -73
  131. data/spec/unit/job_builder_spec.rb +0 -455
  132. data/spec/unit/job_property_collection_spec.rb +0 -111
  133. data/spec/unit/job_property_validator_spec.rb +0 -7
  134. data/spec/unit/job_rename_spec.rb +0 -200
  135. data/spec/unit/package_builder_spec.rb +0 -593
  136. data/spec/unit/release_builder_spec.rb +0 -120
  137. data/spec/unit/release_spec.rb +0 -173
  138. data/spec/unit/release_tarball_spec.rb +0 -29
  139. data/spec/unit/runner_spec.rb +0 -7
  140. data/spec/unit/ssh_spec.rb +0 -84
  141. data/spec/unit/stemcell_spec.rb +0 -17
  142. data/spec/unit/task_tracker_spec.rb +0 -131
  143. data/spec/unit/version_calc_spec.rb +0 -27
  144. data/spec/unit/versions_index_spec.rb +0 -144
@@ -78,6 +78,8 @@ module Bosh::Cli::Command
78
78
  end
79
79
  end
80
80
 
81
+ err("No help found for command `#{keywords.join(' ')}'. Run 'bosh help --all' to see all available BOSH commands.") if good_matches.empty?
82
+
81
83
  self.class.list_commands(good_matches)
82
84
  end
83
85
 
@@ -87,8 +89,7 @@ module Bosh::Cli::Command
87
89
 
88
90
  commands.each_with_index do |command, i|
89
91
  nl if i > 0
90
- margin = command.usage.size + 1
91
- say("#{command.usage_with_params.columnize(70, margin).green}")
92
+ say("#{command.usage_with_params.columnize(70).make_green}")
92
93
  say(command.desc.columnize(help_column_width).indent(help_indent))
93
94
  if command.has_options?
94
95
  say(command.options_summary.indent(help_indent))
@@ -1,125 +1,89 @@
1
1
  # Copyright (c) 2009-2012 VMware, Inc.
2
+ require 'cli/job_state'
3
+ require 'cli/vm_state'
4
+
5
+ module Bosh::Cli
6
+ module Command
7
+ class JobManagement < Base
8
+ FORCE = 'Proceed even when there are other manifest changes'
9
+
10
+ # bosh start
11
+ usage 'start'
12
+ desc 'Start job/instance'
13
+ option '--force', FORCE
14
+ def start_job(job, index = nil)
15
+ change_job_state(:start, job, index)
16
+ end
2
17
 
3
- module Bosh::Cli::Command
4
- class JobManagement < Base
5
- include Bosh::Cli::DeploymentHelper
6
-
7
- FORCE = "Proceed even when there are other manifest changes"
8
-
9
- # bosh start
10
- usage "start"
11
- desc "Start job/instance"
12
- option "--force", FORCE
13
- def start_job(job, index = nil)
14
- change_job_state(:start, job, index)
15
- end
16
-
17
- # bosh stop
18
- usage "stop"
19
- desc "Stop job/instance"
20
- option "--soft", "Stop process only"
21
- option "--hard", "Power off VM"
22
- option "--force", FORCE
23
- def stop_job(job, index = nil)
24
- change_job_state(:stop, job, index)
25
- end
26
-
27
- # bosh restart
28
- usage "restart"
29
- desc "Restart job/instance (soft stop + start)"
30
- option "--force", FORCE
31
- def restart_job(job, index = nil)
32
- change_job_state(:restart, job, index)
33
- end
34
-
35
- # bosh recreate
36
- usage "recreate"
37
- desc "Recreate job/instance (hard stop + start)"
38
- option "--force", FORCE
39
- def recreate_job(job, index = nil)
40
- change_job_state(:recreate, job, index)
41
- end
18
+ # bosh stop
19
+ usage 'stop'
20
+ desc 'Stop job/instance'
21
+ option '--soft', 'Stop process only'
22
+ option '--hard', 'Power off VM'
23
+ option '--force', FORCE
24
+ def stop_job(job, index = nil)
25
+ if hard?
26
+ change_job_state(:detach, job, index)
27
+ else
28
+ change_job_state(:stop, job, index)
29
+ end
30
+ end
42
31
 
43
- def change_job_state(operation, job, index)
44
- auth_required
45
- manifest_yaml = prepare_deployment_manifest(:yaml => true)
46
- manifest = YAML.load(manifest_yaml)
32
+ # bosh restart
33
+ usage 'restart'
34
+ desc 'Restart job/instance (soft stop + start)'
35
+ option '--force', FORCE
36
+ def restart_job(job, index = nil)
37
+ change_job_state(:restart, job, index)
38
+ end
47
39
 
48
- unless [:start, :stop, :restart, :recreate].include?(operation)
49
- err("Unknown operation `#{operation}': supported operations are " +
50
- "`start', `stop', `restart', `recreate'")
40
+ # bosh recreate
41
+ usage 'recreate'
42
+ desc 'Recreate job/instance (hard stop + start)'
43
+ option '--force', FORCE
44
+ def recreate_job(job, index = nil)
45
+ change_job_state(:recreate, job, index)
51
46
  end
52
47
 
53
- hard = options[:hard]
54
- soft = options[:soft]
55
- force = options[:force]
48
+ private
56
49
 
57
- if hard && soft
58
- err("Cannot handle both --hard and --soft options, please choose one")
50
+ def change_job_state(state, job, index = nil)
51
+ check_arguments(state, job)
52
+ index = valid_index_for(job, index)
53
+ vm_state = VmState.new(self, force?)
54
+ job_state = JobState.new(self, vm_state)
55
+ status, task_id, completion_desc =job_state.change(state, job, index)
56
+ task_report(status, task_id, completion_desc)
59
57
  end
60
58
 
61
- if operation != :stop && (hard || soft)
62
- err("--hard and --soft options only make sense for `stop' operation")
59
+ def hard?
60
+ options[:hard]
63
61
  end
64
62
 
65
- job_desc = index ? "#{job}/#{index}" : "#{job}"
66
-
67
- op_desc = nil
68
- new_state = nil
69
- completion_desc = nil
70
-
71
- case operation
72
- when :start
73
- op_desc = "start #{job_desc}"
74
- new_state = "started"
75
- completion_desc = "#{job_desc.green} has been started"
76
- when :stop
77
- if hard
78
- op_desc = "stop #{job_desc} and power off its VM(s)"
79
- completion_desc = "#{job_desc.green} has been stopped, " +
80
- "VM(s) powered off"
81
- new_state = "detached"
82
- else
83
- op_desc = "stop #{job_desc}"
84
- completion_desc = "#{job_desc.green} has been stopped, " +
85
- "VM(s) still running"
86
- new_state = "stopped"
87
- end
88
- when :restart
89
- op_desc = "restart #{job_desc}"
90
- new_state = "restart"
91
- completion_desc = "#{job_desc.green} has been restarted"
92
- when :recreate
93
- op_desc = "recreate #{job_desc}"
94
- new_state = "recreate"
95
- completion_desc = "#{job_desc.green} has been recreated"
96
- else
97
- err("Unknown operation: `#{operation}'")
63
+ def soft?
64
+ options[:soft]
98
65
  end
99
66
 
100
- say("You are about to #{op_desc.green}")
67
+ def force?
68
+ options[:force]
69
+ end
101
70
 
102
- if interactive?
103
- other_changes_present = inspect_deployment_changes(
104
- manifest, :show_empty_changeset => false)
71
+ def check_arguments(operation, job)
72
+ auth_required
73
+ job_must_exist_in_deployment(job)
105
74
 
106
- if other_changes_present && !force
107
- err("Cannot perform job management when other deployment changes " +
108
- "are present. Please use `--force' to override.")
75
+ if hard? && soft?
76
+ err('Cannot handle both --hard and --soft options, please choose one')
109
77
  end
110
- unless confirmed?("#{op_desc.capitalize}?")
111
- cancel_deployment
78
+
79
+ if !hard_and_soft_options_allowed?(operation) && (hard? || soft?)
80
+ err("--hard and --soft options only make sense for `stop' operation")
112
81
  end
113
82
  end
114
83
 
115
- nl
116
- say("Performing `#{op_desc}'...")
117
-
118
- status, task_id = director.change_job_state(
119
- manifest["name"], manifest_yaml, job, index, new_state)
120
-
121
- task_report(status, task_id, completion_desc)
84
+ def hard_and_soft_options_allowed?(operation)
85
+ operation == :stop || operation == :detach
86
+ end
122
87
  end
123
-
124
88
  end
125
89
  end
@@ -2,8 +2,6 @@
2
2
 
3
3
  module Bosh::Cli::Command
4
4
  class JobRename < Base
5
- include Bosh::Cli::DeploymentHelper
6
-
7
5
  # bosh rename
8
6
  usage "rename job"
9
7
  desc "Renames a job. NOTE, your deployment manifest must also be " +
@@ -12,14 +10,14 @@ module Bosh::Cli::Command
12
10
  def rename(old_name, new_name)
13
11
  auth_required
14
12
  manifest_yaml = prepare_deployment_manifest(:yaml => true)
15
- manifest = YAML.load(manifest_yaml)
13
+ manifest = Psych.load(manifest_yaml)
16
14
 
17
15
  force = options[:force]
18
- say("You are about to rename `#{old_name.green}' to `#{new_name.green}'")
16
+ say("You are about to rename `#{old_name.make_green}' to `#{new_name.make_green}'")
19
17
 
20
18
  unless confirmed?
21
19
  nl
22
- say("Job rename canceled".green)
20
+ say("Job rename canceled".make_green)
23
21
  exit(0)
24
22
  end
25
23
 
@@ -33,7 +31,7 @@ module Bosh::Cli::Command
33
31
 
34
32
  def sanity_check_job_rename(manifest_yaml, old_name, new_name)
35
33
  # Makes sure the new deployment manifest contains the renamed job
36
- manifest = YAML.load(manifest_yaml)
34
+ manifest = Psych.load(manifest_yaml)
37
35
  new_jobs = manifest["jobs"].map { |job| job["name"] }
38
36
  unless new_jobs.include?(new_name)
39
37
  err("Please update your deployment manifest to include the " +
@@ -52,7 +50,7 @@ module Bosh::Cli::Command
52
50
  "`#{manifest["name"]}'")
53
51
  end
54
52
 
55
- current_manifest = YAML.load(current_deployment["manifest"])
53
+ current_manifest = Psych.load(current_deployment["manifest"])
56
54
  jobs = current_manifest["jobs"].map { |job| job["name"] }
57
55
  unless jobs.include?(old_name)
58
56
  err("Trying to rename a non existent job `#{old_name}'")
@@ -99,7 +97,7 @@ module Bosh::Cli::Command
99
97
  end
100
98
 
101
99
  # Now the manifests should be the same
102
- manifest = YAML.load(manifest_yaml)
100
+ manifest = Psych.load(manifest_yaml)
103
101
  if deployment_changed?(current_manifest.dup, manifest.dup)
104
102
  err("You cannot have any other changes to your manifest during " +
105
103
  "rename. Please revert the above changes and retry.")
@@ -2,78 +2,101 @@
2
2
 
3
3
  module Bosh::Cli::Command
4
4
  class LogManagement < Base
5
- include Bosh::Cli::DeploymentHelper
6
-
7
5
  # bosh logs
8
- usage "logs"
9
- desc "Fetch job or agent logs from a BOSH-managed VM"
10
- option "--agent", "fetch agent logs"
11
- option "--job", "fetch job logs"
12
- option "--only filter1,filter2,...", Array,
13
- "only fetch logs that satisfy",
14
- "given filters (defined in job spec)"
15
- option "--all", "fetch all files in the job or agent log directory"
16
- def fetch_logs(job, index)
17
- auth_required
18
- target_required
19
- no_track_unsupported
6
+ usage 'logs'
7
+ desc 'Fetch job or agent logs from a BOSH-managed VM'
8
+ option '--agent', 'fetch agent logs'
9
+ option '--job', 'fetch job logs'
10
+ option '--only filter1,filter2,...', Array,
11
+ 'only fetch logs that satisfy',
12
+ 'given filters (defined in job spec)'
13
+ option '--all', 'fetch all files in the job or agent log directory'
14
+ option '--dir destination_directory', String, 'download directory'
15
+ def fetch_logs(job, index = nil)
16
+ index = valid_index_for(job, index)
17
+ check_arguments(index)
18
+
19
+ resource_id = fetch_log_resource_id(index, job)
20
+ log_file_path = log_file_destination(index, job)
21
+ download_logs(log_file_path, resource_id)
22
+ end
20
23
 
21
- if index !~ /^\d+$/
22
- err("Job index is expected to be a positive integer")
23
- end
24
+ def fetch_log_resource_id(index, job)
25
+ resource_id = director.fetch_logs(deployment_name, job, index, log_type, filters)
26
+ err('Error retrieving logs') if resource_id.nil?
24
27
 
25
- if options[:agent]
26
- if options[:job]
27
- err("You can't use --job and --agent together")
28
- end
29
- log_type = "agent"
30
- else
31
- log_type = "job"
32
- end
33
-
34
- if options[:only]
35
- if options[:all]
36
- err("You can't use --only and --all together")
37
- end
38
- filters = options[:only].join(",")
39
- elsif options[:all]
40
- filters = "all"
41
- else
42
- filters = nil
43
- end
44
-
45
- if options[:agent] && filters && filters != "all"
46
- err("Custom filtering is not supported for agent logs")
47
- end
28
+ resource_id
29
+ end
48
30
 
49
- manifest = prepare_deployment_manifest
31
+ private
50
32
 
51
- resource_id = director.fetch_logs(
52
- manifest["name"], job, index, log_type, filters)
33
+ def agent_logs_wanted?
34
+ options[:agent]
35
+ end
53
36
 
54
- if resource_id.nil?
55
- err("Error retrieving logs")
56
- end
37
+ def job_logs_wanted?
38
+ options[:job]
39
+ end
57
40
 
58
- nl
59
- say("Downloading log bundle (#{resource_id.to_s.green})...")
41
+ def download_logs(log_file_path, resource_id)
42
+ say("Downloading log bundle (#{resource_id.to_s.make_green})...")
60
43
 
61
44
  begin
62
- time = Time.now.strftime("%Y-%m-%d@%H-%M-%S")
63
- log_file = File.join(Dir.pwd, "#{job}.#{index}.#{time}.tgz")
64
-
65
45
  tmp_file = director.download_resource(resource_id)
66
-
67
- FileUtils.mv(tmp_file, log_file)
68
- say("Logs saved in `#{log_file.green}'")
46
+ FileUtils.mv(tmp_file, log_file_path)
47
+ say("Logs saved in `#{log_file_path.make_green}'")
69
48
  rescue Bosh::Cli::DirectorError => e
70
49
  err("Unable to download logs from director: #{e}")
71
50
  ensure
72
- FileUtils.rm_rf(tmp_file) if File.exists?(tmp_file)
51
+ FileUtils.rm_rf(tmp_file) if tmp_file && File.exists?(tmp_file)
52
+ end
53
+ end
54
+
55
+ def check_arguments(index)
56
+ auth_required
57
+ no_track_unsupported
58
+
59
+ err('Job index is expected to be a positive integer') if index !~ /^\d+$/
60
+
61
+ if agent_logs_wanted? && options[:only]
62
+ err('Custom filtering is not supported for agent logs')
73
63
  end
64
+ end
65
+
66
+ def log_type
67
+ err("You can't use --job and --agent together") if job_logs_wanted? && agent_logs_wanted?
74
68
 
69
+ if agent_logs_wanted?
70
+ 'agent'
71
+ else
72
+ 'job'
73
+ end
75
74
  end
76
75
 
76
+ def filters
77
+ if options[:only]
78
+ err("You can't use --only and --all together") if options[:all]
79
+ filter = options[:only].join(',')
80
+ elsif options[:all]
81
+ filter = 'all'
82
+ else
83
+ filter = nil
84
+ end
85
+ filter
86
+ end
87
+
88
+ def log_file_destination(index, job)
89
+ time = Time.now.strftime('%Y-%m-%d@%H-%M-%S')
90
+ File.join(log_directory, "#{job}.#{index}.#{time}.tgz")
91
+ end
92
+
93
+ def log_directory
94
+ options[:dir] || Dir.pwd
95
+ end
96
+
97
+ def deployment_name
98
+ prepare_deployment_manifest['name']
99
+ end
77
100
  end
78
101
  end
79
102
 
@@ -8,8 +8,8 @@ module Bosh::Cli::Command
8
8
  STEMCELLS_TO_KEEP = 2
9
9
 
10
10
  # bosh cleanup
11
- usage "cleanup"
12
- desc "Cleanup releases and stemcells"
11
+ usage 'cleanup'
12
+ desc 'Cleanup releases and stemcells'
13
13
  def cleanup
14
14
  target_required
15
15
  auth_required
@@ -17,16 +17,16 @@ module Bosh::Cli::Command
17
17
  releases_to_keep = RELEASES_TO_KEEP
18
18
  stemcells_to_keep = STEMCELLS_TO_KEEP
19
19
 
20
- release_wording = pluralize(releases_to_keep, "latest version")
21
- stemcell_wording = pluralize(stemcells_to_keep, "latest version")
20
+ release_wording = pluralize(releases_to_keep, 'latest version')
21
+ stemcell_wording = pluralize(stemcells_to_keep, 'latest version')
22
22
 
23
23
  desc = <<-EOS.gsub(/^ */, "")
24
24
  Cleanup command will attempt to delete old unused
25
25
  release versions and stemcells from your currently
26
- targeted director at #{target_name.green}.
26
+ targeted director at #{target_name.make_green}.
27
27
 
28
- Only #{release_wording.green} of each release
29
- and #{stemcell_wording.green} of each stemcell will be kept.
28
+ Only #{release_wording.make_green} of each release
29
+ and #{stemcell_wording.make_green} of each stemcell will be kept.
30
30
 
31
31
  Releases and stemcells that are in use will not be affected.
32
32
  EOS
@@ -35,7 +35,7 @@ module Bosh::Cli::Command
35
35
  say(desc)
36
36
  nl
37
37
 
38
- err("Cleanup canceled") unless confirmed?
38
+ err('Cleanup canceled') unless confirmed?
39
39
 
40
40
  nl
41
41
  cleanup_stemcells(stemcells_to_keep)
@@ -43,48 +43,54 @@ module Bosh::Cli::Command
43
43
  cleanup_releases(releases_to_keep)
44
44
 
45
45
  nl
46
- say("Cleanup complete".green)
46
+ say('Cleanup complete'.make_green)
47
47
  end
48
48
 
49
49
  private
50
50
 
51
51
  def cleanup_stemcells(n_to_keep)
52
52
  stemcells_by_name = director.list_stemcells.inject({}) do |h, stemcell|
53
- h[stemcell["name"]] ||= []
54
- h[stemcell["name"]] << stemcell
53
+ h[stemcell['name']] ||= []
54
+ h[stemcell['name']] << stemcell
55
55
  h
56
56
  end
57
57
 
58
58
  delete_list = []
59
- say("Deleting old stemcells")
59
+ say('Deleting old stemcells')
60
60
 
61
61
  stemcells_by_name.each_pair do |name, stemcells|
62
62
  stemcells.sort! do |sc1, sc2|
63
- version_cmp(sc1["version"], sc2["version"])
63
+ version_cmp(sc1['version'], sc2['version'])
64
64
  end
65
65
  delete_list += stemcells[0...(-n_to_keep)]
66
66
  end
67
67
 
68
68
  if delete_list.size > 0
69
69
  delete_list.each do |stemcell|
70
- name, version = stemcell["name"], stemcell["version"]
70
+ name, version = stemcell['name'], stemcell['version']
71
71
  desc = "#{name}/#{version}"
72
72
  perform(desc) do
73
73
  director.delete_stemcell(name, version, :quiet => true)
74
74
  end
75
75
  end
76
76
  else
77
- say(" none found".yellow)
77
+ say(' none found'.make_yellow)
78
78
  end
79
79
  end
80
80
 
81
81
  def cleanup_releases(n_to_keep)
82
82
  delete_list = []
83
- say("Deleting old release versions")
83
+ say('Deleting old release versions')
84
84
 
85
85
  director.list_releases.each do |release|
86
- name = release["name"]
87
- versions = release["versions"].sort { |v1, v2| version_cmp(v1, v2) }
86
+ name = release['name']
87
+ versions = if release['versions']
88
+ release['versions']
89
+ else
90
+ release['release_versions'].map { |release_version| release_version['version'] }
91
+ end
92
+
93
+ versions.sort! { |v1, v2| version_cmp(v1, v2) }
88
94
 
89
95
  versions[0...(-n_to_keep)].each do |version|
90
96
  delete_list << [name, version]
@@ -100,33 +106,33 @@ module Bosh::Cli::Command
100
106
  end
101
107
  end
102
108
  else
103
- say(" none found".yellow)
109
+ say(' none found'.make_yellow)
104
110
  end
105
111
  end
106
112
 
107
113
  def refresh(message)
108
- say("\r", "")
109
- say(" " * 80, "")
110
- say("\r#{message}", "")
114
+ say("\r", '')
115
+ say(' ' * 80, '')
116
+ say("\r#{message}", '')
111
117
  end
112
118
 
113
119
  def perform(desc)
114
- say(" #{desc.yellow.ljust(40)}", "")
115
- say("IN PROGRESS...".yellow, "")
120
+ say(" #{desc.make_yellow.ljust(40)}", '')
121
+ say(' IN PROGRESS...'.make_yellow, '')
116
122
 
117
123
  status, task_id = yield
118
124
  responses = {
119
- :done => "DELETED".green,
120
- :non_trackable => "CANNOT TRACK".red,
121
- :track_timeout => "TIMED OUT".red,
122
- :error => "ERROR".red,
125
+ :done => 'DELETED'.make_green,
126
+ :non_trackable => 'CANNOT TRACK'.make_red,
127
+ :track_timeout => 'TIMED OUT'.make_red,
128
+ :error => 'ERROR'.make_red,
123
129
  }
124
130
 
125
- refresh(" #{desc.yellow.ljust(40)}#{responses[status]}\n")
131
+ refresh(" #{desc.make_yellow.ljust(50)}#{responses[status]}\n")
126
132
 
127
133
  if status == :error
128
134
  result = director.get_task_result(task_id)
129
- say(" #{result.to_s.red}")
135
+ say(" #{result.to_s.make_red}")
130
136
  end
131
137
 
132
138
  status == :done