Module: Bosh::Cli::DeploymentHelper

Included in:
Command::Base
Defined in:
lib/cli/deployment_helper.rb

Instance Method Summary collapse

Instance Method Details

#cancel_deploymentObject



168
169
170
# File 'lib/cli/deployment_helper.rb', line 168

def cancel_deployment
  quit('Deployment canceled'.make_red)
end

#deployment_changed?(current_manifest, manifest, show = true) ⇒ Boolean

Check if the 2 deployments are different. Print out a summary if “show” is true.

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cli/deployment_helper.rb', line 18

def deployment_changed?(current_manifest, manifest, show = true)
  diff = Bosh::Cli::HashChangeset.new
  diff.add_hash(normalize_deployment_manifest(manifest), :new)
  diff.add_hash(normalize_deployment_manifest(current_manifest), :old)
  changed = diff.changed?

  if changed && show
    @_diff_key_visited = {}
    diff.keys.each do |key|
      unless @_diff_key_visited[key]
        print_summary(diff, key, false)
        nl
      end
    end
  end

  changed
end

#inspect_deployment_changes(manifest, options = {}) ⇒ Object

Interactive walkthrough of deployment changes, expected to bail out of CLI using ‘cancel_deployment’ if something goes wrong, so it doesn’t need to have a meaningful return value.

Returns:

  • Boolean Were there any changes in deployment manifest?



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cli/deployment_helper.rb', line 42

def inspect_deployment_changes(manifest, options = {})
  show_empty_changeset = options.fetch(:show_empty_changeset, true)
  interactive = options.fetch(:interactive, false)
  redact_diff = options.fetch(:redact_diff, false)

  manifest = manifest.dup
  current_deployment = director.get_deployment(manifest['name'])

  # We cannot retrieve current manifest until there was at least one
  # successful deployment. There used to be a warning about that
  # but it turned out to be confusing to many users and thus has
  # been removed.
  return if current_deployment['manifest'].nil?
  current_manifest = Psych.load(current_deployment['manifest'])

  unless current_manifest.is_a?(Hash)
    err('Current deployment manifest format is invalid, check if director works properly')
  end

  diff = Bosh::Cli::HashChangeset.new
  diff.add_hash(normalize_deployment_manifest(manifest), :new)
  diff.add_hash(normalize_deployment_manifest(current_manifest), :old)
  @_diff_key_visited = { 'name' => 1, 'director_uuid' => 1 }

  header('Detecting deployment changes')

  if !diff.changed? && !show_empty_changeset
    return false
  end

  if diff[:release]
    print_summary(diff, :release, redact_diff)
    nl
  end

  if diff[:releases]
    print_summary(diff, :releases, redact_diff)
    nl
  end

  print_summary(diff, :compilation, redact_diff)
  nl

  print_summary(diff, :update, redact_diff)
  nl

  print_summary(diff, :resource_pools, redact_diff)
  nl

  print_summary(diff, :disk_pools, redact_diff)
  nl

  print_summary(diff, :networks, redact_diff)
  nl

  print_summary(diff, :jobs, redact_diff)
  nl

  print_summary(diff, :properties, redact_diff)
  nl

  diff.keys.each do |key|
    unless @_diff_key_visited[key]
      print_summary(diff, key, redact_diff)
      nl
    end
  end

  diff.changed?
rescue Bosh::Cli::ResourceNotFound
  say('Cannot get current deployment information from director, possibly a new deployment'.make_red)
  true
end

#job_exists_in_deployment?(manifest_hash, job_name) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/cli/deployment_helper.rb', line 121

def job_exists_in_deployment?(manifest_hash, job_name)
  !!find_job(manifest_hash, job_name)
end

#job_must_exist_in_deployment(manifest_hash, job) ⇒ Object



125
126
127
# File 'lib/cli/deployment_helper.rb', line 125

def job_must_exist_in_deployment(manifest_hash, job)
  err("Job `#{job}' doesn't exist") unless job_exists_in_deployment?(manifest_hash, job)
end

#job_unique_in_deployment?(manifest_hash, job_name) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
119
# File 'lib/cli/deployment_helper.rb', line 116

def job_unique_in_deployment?(manifest_hash, job_name)
  job = find_job(manifest_hash, job_name)
  job ? job.fetch('instances') == 1 : false
end

#jobs_and_indexesObject



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/cli/deployment_helper.rb', line 156

def jobs_and_indexes
  jobs = prepare_deployment_manifest.hash.fetch('jobs')

  jobs.inject([]) do |jobs_and_indexes, job|
    job_name = job.fetch('name')
    0.upto(job.fetch('instances').to_i - 1) do |index|
      jobs_and_indexes << [job_name, index]
    end
    jobs_and_indexes
  end
end

#prepare_deployment_manifest(options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/cli/deployment_helper.rb', line 3

def prepare_deployment_manifest(options = {})
  deployment_required
  manifest = Manifest.new(deployment, director)
  manifest.load
  if options.fetch(:show_state, false)
    show_current_state(manifest.name)
  end
  manifest.validate(options)

  manifest
end

#prompt_for_errand_nameObject

return a job/errand selected by user by name



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/cli/deployment_helper.rb', line 143

def prompt_for_errand_name
  errands = list_errands

  err('Deployment has no available errands') if errands.size == 0

  choose do |menu|
    menu.prompt = 'Choose an errand: '
    errands.each do |errand, index|
      menu.choice("#{errand['name']}") { errand }
    end
  end
end

#prompt_for_job_and_indexObject



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/cli/deployment_helper.rb', line 129

def prompt_for_job_and_index
  jobs_list = jobs_and_indexes

  return jobs_list.first if jobs_list.size == 1

  choose do |menu|
    menu.prompt = 'Choose an instance: '
    jobs_list.each do |job_name, index|
      menu.choice("#{job_name}/#{index}") { [job_name, index] }
    end
  end
end