Class: Bosh::Cli::Command::Deployment

Inherits:
Base show all
Defined in:
lib/cli/commands/deployment.rb

Constant Summary

Constants inherited from Base

Base::DEFAULT_DIRECTOR_PORT

Instance Attribute Summary

Attributes inherited from Base

#args, #exit_code, #options, #out, #runner, #work_dir

Instance Method Summary collapse

Methods inherited from Base

#add_option, #blob_manager, #blobstore, #cache_dir, #config, #confirmed?, #credentials, #deployment, #director, #initialize, #interactive?, #logged_in?, #non_interactive?, #progress_renderer, #redirect, #release, #remove_option, #show_current_state, #target, #target_name, #verbose?

Methods included from Bosh::Cli::CommandDiscovery

#desc, #method_added, #option, #register_command, #usage

Methods included from DeploymentHelper

#cancel_deployment, #deployment_changed?, #inspect_deployment_changes, #job_exists_in_deployment?, #job_must_exist_in_deployment, #job_unique_in_deployment?, #jobs_and_indexes, #prepare_deployment_manifest, #prompt_for_errand_name, #prompt_for_job_and_index

Constructor Details

This class inherits a constructor from Bosh::Cli::Command::Base

Instance Method Details

#delete(deployment_name) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/cli/commands/deployment.rb', line 123

def delete(deployment_name)
  auth_required
  show_current_state(deployment_name)

  force = !!options[:force]

  say("\nYou are going to delete deployment `#{deployment_name}'.".make_red)
  nl
  say("THIS IS A VERY DESTRUCTIVE OPERATION AND IT CANNOT BE UNDONE!\n".make_red)

  unless confirmed?
    say("Canceled deleting deployment".make_green)
    return
  end

  begin
    status, result = director.delete_deployment(deployment_name, :force => force)
    task_report(status, result, "Deleted deployment `#{deployment_name}'")
  rescue Bosh::Cli::ResourceNotFound
    task_report(:done, nil, "Skipped delete of missing deployment `#{deployment_name}'")
  end
end

#download_manifest(deployment_name, save_as = nil) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/cli/commands/deployment.rb', line 237

def download_manifest(deployment_name, save_as = nil)
  auth_required
  show_current_state(deployment_name)

  if save_as && File.exists?(save_as) &&
     !confirmed?("Overwrite `#{save_as}'?")
    err("Please choose another file to save the manifest to")
  end

  deployment = director.get_deployment(deployment_name)

  if save_as
    File.open(save_as, "w") do |f|
      f.write(deployment["manifest"])
    end
    say("Deployment manifest saved to `#{save_as}'".make_green)
  else
    say(deployment["manifest"])
  end
end

#editObject



73
74
75
76
77
# File 'lib/cli/commands/deployment.rb', line 73

def edit
  deployment_required
  editor = ENV['EDITOR'] || "vi"
  system("#{editor} #{deployment}")
end

#listObject



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/cli/commands/deployment.rb', line 212

def list
  auth_required
  show_current_state

  deployments = director.list_deployments

  err("No deployments") if deployments.empty?

  deployments_table = table do |t|
    t.headings = ['Name', 'Release(s)', 'Stemcell(s)', 'Cloud Config']
    deployments.each do |d|
      t.add_row(row_for_deployments_table(d))
      t.add_separator unless d == deployments.last
    end
  end

  nl
  say(deployments_table)
  nl
  say("Deployments total: %d" % deployments.size)
end

#performObject



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
115
116
117
# File 'lib/cli/commands/deployment.rb', line 85

def perform
  auth_required
  recreate = !!options[:recreate]
  redact_diff = !!options[:redact_diff]

  manifest = prepare_deployment_manifest(resolve_properties: true, show_state: true)

  inspect_deployment_changes(
    manifest.hash,
    interactive: interactive?,
    redact_diff: redact_diff
  )
  say('Please review all changes carefully'.make_yellow) if interactive?

  header('Deploying')

  unless confirmed?('Are you sure you want to deploy?')
    cancel_deployment
  end

  deploy_options = { recreate: recreate }

  if options.has_key?(:skip_drain)
    # when key is present but no jobs specified OptionParser
    # adds a key with nil value, in that case we want to
    # skip drain for all jobs
    deploy_options[:skip_drain] = options[:skip_drain].nil? ? '*' : options[:skip_drain]
  end

  status, task_id = director.deploy(manifest.yaml, deploy_options)

  task_report(status, task_id, "Deployed `#{manifest.name.make_green}' to `#{target_name.make_green}'")
end

#set_current(filename = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
64
65
66
67
68
# File 'lib/cli/commands/deployment.rb', line 7

def set_current(filename = nil)
  if filename.nil?
    show_current
    return
  end

  manifest_filename = find_deployment(filename)

  unless File.exists?(manifest_filename)
    err("Missing manifest for `#{filename}'")
  end

  manifest = load_yaml_file(manifest_filename)

  unless manifest.is_a?(Hash)
    err("Invalid manifest format")
  end

  unless manifest["target"].blank?
    err(Bosh::Cli::Manifest::MANIFEST_TARGET_UPGRADE_NOTICE)
  end

  if manifest["director_uuid"].blank?
    err("Director UUID is not defined in deployment manifest")
  end

  if target
    old_director = Bosh::Cli::Client::Director.new(target, credentials)
    old_director_uuid = old_director.get_status["uuid"] rescue nil
  else
    old_director_uuid = nil
  end

  new_director_uuid = manifest["director_uuid"]

  if old_director_uuid != new_director_uuid
    new_target_url = config.resolve_alias(:target, new_director_uuid)

    if new_target_url.blank?
      err("This manifest references director with UUID " +
          "#{new_director_uuid}.\n" +
          "You've never targeted it before.\n" +
          "Please find your director IP or hostname and target it first.")
    end

    new_director = Bosh::Cli::Client::Director.new(
      new_target_url, credentials)

    status = new_director.get_status

    config.target = new_target_url
    config.target_name = status["name"]
    config.target_version = status["version"]
    config.target_uuid = status["uuid"]
    say("#{"WARNING!".make_red} Your target has been " +
        "changed to `#{target.make_red}'!")
  end

  say("Deployment set to `#{manifest_filename.make_green}'")
  config.set_deployment(manifest_filename)
  config.save
end

#validate_jobsObject



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
207
# File 'lib/cli/commands/deployment.rb', line 150

def validate_jobs
  check_if_release_dir
  manifest = prepare_deployment_manifest(:resolve_properties => true, show_state: true)

  if manifest.hash["release"]
    release_name = manifest.hash["release"]["name"]
  elsif manifest.hash["releases"].count > 1
    err("Cannot validate a deployment manifest with more than 1 release")
  else
    release_name = manifest.hash["releases"].first["name"]
  end
  if release_name == release.dev_name || release_name == release.final_name
    nl
    say("Analyzing release directory...".make_yellow)
  else
    err("This release was not found in deployment manifest")
  end

  say(" - discovering packages")
  packages = Bosh::Cli::Resources::Package.discover(work_dir)

  say(" - discovering jobs")
  jobs = Bosh::Cli::Resources::Job.discover(
    work_dir,
    # TODO: be sure this is covered in integration
    packages.map {|package| package['name']}
  )

  say(" - validating properties")
  validator = Bosh::Cli::JobPropertyValidator.new(jobs, manifest.hash)
  validator.validate

  unless validator.jobs_without_properties.empty?
    nl
    say("Legacy jobs (no properties defined): ".make_yellow)
    validator.jobs_without_properties.sort { |a, b|
      a.name <=> b.name
    }.each do |job|
      say(" - #{job.name}")
    end
  end

  if validator.template_errors.empty?
    nl
    say("No template errors found".make_green)
  else
    nl
    say("Template errors: ".make_yellow)
    validator.template_errors.each do |error|
      nl
      path = Pathname.new(error.template_path)
      rel_path = path.relative_path_from(Pathname.new(release.dir))

      say(" - #{rel_path}:")
      say("     line #{error.line}:".make_yellow + " #{error.exception.to_s}")
    end
  end
end