Class: Bosh::Cli::Command::JobRename

Inherits:
Base show all
Defined in:
lib/cli/commands/job_rename.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, #config, #confirmed?, #deployment, #director, #initialize, #interactive?, #logged_in?, #non_interactive?, #password, #redirect, #release, #remove_option, #target, #target_name, #username, #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, #latest_release_versions, #prepare_deployment_manifest, #prompt_for_job_and_index, #resolve_release_aliases, #warn_about_stemcell_changes

Constructor Details

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

Instance Method Details

#rename(old_name, new_name) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cli/commands/job_rename.rb', line 8

def rename(old_name, new_name)
  auth_required
  manifest_yaml = prepare_deployment_manifest(:yaml => true)
  manifest = Psych.load(manifest_yaml)

  force = options[:force]
  say("You are about to rename `#{old_name.make_green}' to `#{new_name.make_green}'")

  unless confirmed?
    nl
    say("Job rename canceled".make_green)
    exit(0)
  end

  sanity_check_job_rename(manifest_yaml, old_name, new_name)

  status, task_id = director.rename_job(
    manifest["name"], manifest_yaml, old_name, new_name, force)

  task_report(status, task_id, "Rename successful")
end

#sanity_check_job_rename(manifest_yaml, old_name, new_name) ⇒ Object



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
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
# File 'lib/cli/commands/job_rename.rb', line 30

def sanity_check_job_rename(manifest_yaml, old_name, new_name)
  # Makes sure the new deployment manifest contains the renamed job
  manifest = Psych.load(manifest_yaml)
  new_jobs = manifest["jobs"].map { |job| job["name"] }
  unless new_jobs.include?(new_name)
    err("Please update your deployment manifest to include the " +
        "new job name `#{new_name}'")
  end

  if new_jobs.include?(old_name)
    err("Old name `#{old_name}' is still being used in the " +
        "deployment file")
  end

  # Make sure that the old deployment manifest contains the old job
  current_deployment = director.get_deployment(manifest["name"])
  if current_deployment["manifest"].nil?
    err("Director could not find manifest for deployment " +
        "`#{manifest["name"]}'")
  end

  current_manifest = Psych.load(current_deployment["manifest"])
  jobs = current_manifest["jobs"].map { |job| job["name"] }
  unless jobs.include?(old_name)
    err("Trying to rename a non existent job `#{old_name}'")
  end

  # Technically we could allow this
  if jobs.include?(new_name)
    err("Trying to reuse an existing job name `#{new_name}' " +
        "to rename job `#{old_name}'")
  end

  # Make sure that only one job has been renamed
  added_jobs = new_jobs - jobs

  if added_jobs.size > 1
    err("Cannot rename more than one job, you are trying to " +
        "add #{added_jobs.inspect}")
  end

  if added_jobs.first != new_name
    err("Manifest does not include new job `#{new_name}'")
  end

  renamed_jobs = jobs - new_jobs

  if renamed_jobs.size > 1
    err("Cannot rename more than one job, you have changes to " +
        "#{renamed_jobs}")
  end

  if renamed_jobs.first != old_name
    err("Manifest does not rename old job `#{old_name}'")
  end

  # Final sanity check, make sure that no
  # other properties or anything other than the names
  # have changed. So update current manifest with new name
  # and check that it matches with the old manifest
  current_manifest["jobs"].each do |job|
    if job["name"] == old_name
      job["name"] = new_name
      break
    end
  end

  # Now the manifests should be the same
  manifest = Psych.load(manifest_yaml)
  if deployment_changed?(current_manifest.dup, manifest.dup)
    err("You cannot have any other changes to your manifest during " +
        "rename. Please revert the above changes and retry.")
  end
end