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, #info, #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, #run_nested_command, #show_current_state, #target, #target_name, #verbose?

Methods included from Bosh::Cli::CommandDiscovery

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

Methods included from DeploymentHelper

#build_manifest, #cancel_deployment, #deployment_changed?, #inspect_deployment_changes, #job_exists_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


170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/cli/commands/deployment.rb', line 170

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


221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/cli/commands/deployment.rb', line 221

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


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

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

#listObject


196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/cli/commands/deployment.rb', line 196

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


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
118
119
120
121
122
123
124
125
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
# File 'lib/cli/commands/deployment.rb', line 86

def perform
  auth_required

  recreate = !!options[:recreate]
  redact_diff = !!options[:no_redact].nil?

  manifest = build_manifest

  if manifest.hash['releases']
    manifest.hash['releases'].each do |release|
      if release['url'].blank?
        if release['version'] == 'create'
          err("Expected URL when specifying release version 'create'")
        end
      else
        parsed_uri = URI.parse(release['url'])
        case parsed_uri.scheme
        when 'file'
          if release['version'] == 'create'
            path_is_reasonable!(parsed_uri.path)
            _, info = run_nested_command "create", "release", "--name", release['name'], "--dir", parsed_uri.path, "--timestamp-version", "--force"
            release['version'] = info[:generated_version]
            run_nested_command "upload", "release", "--dir", parsed_uri.path, info[:generated_manifest_path]
          else
            run_nested_command "upload", "release", parsed_uri.path, '--name', release['name'], '--version', release['version'].to_s
          end
        when 'http', 'https'
          err("Path must be a local release directory when version is 'create'") if release['version'] == 'create'
          err("Expected SHA1 when specifying remote URL for release '#{release["name"]}'") if release['sha1'].blank?
          run_nested_command "upload", "release", release['url'], "--sha1", release['sha1'], "--name", release['name'], "--version", release['version'].to_s
        else
          err("Invalid URL format for release '#{release['name']}' with URL '#{release['url']}'. Supported schemes: file, http, https.")
        end
      end
    end
  end

  if manifest.hash['resource_pools']
    manifest.hash['resource_pools'].each do |resource_pool|
      unless resource_pool['stemcell']['url'].blank?
        parsed_uri = URI.parse(resource_pool['stemcell']['url'])
        case parsed_uri.scheme
        when 'file'
          run_nested_command "upload", "stemcell", parsed_uri.path, "--name", resource_pool['stemcell']['name'], "--version", resource_pool['stemcell']['version'].to_s, "--skip-if-exists"
        when 'http', 'https'
          err("Expected SHA1 when specifying remote URL for stemcell '#{resource_pool['stemcell']['name']}'") if resource_pool['stemcell']['sha1'].blank?
          run_nested_command "upload", "stemcell", resource_pool['stemcell']['url'], "--sha1", resource_pool['stemcell']['sha1'],
            "--name", resource_pool['stemcell']['name'], "--version", resource_pool['stemcell']['version'].to_s, "--skip-if-exists"
        else
          err("Invalid URL format for stemcell '#{resource_pool['stemcell']['name']}' with URL '#{resource_pool['stemcell']['url']}'. Supported schemes: file, http, https.")
        end
      end
    end
  end

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

  context = DeploymentDiff.new(director, manifest).print({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, context: context }

  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
69
# 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, ca_cert: config.ca_cert)
    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

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

    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