class Bosh::Cli::Command::Stemcell

Constants

STEMCELL_EXISTS_ERROR_CODE

Public Instance Methods

delete(name, version) click to toggle source
# File lib/cli/commands/stemcell.rb, line 158
def delete(name, version)
  auth_required
  show_current_state

  force = !!options[:force]

  err("Stemcell '#{name}/#{version}' does not exist") unless exists?(name, version)

  say("You are going to delete stemcell '#{name}/#{version}'".make_red)

  unless confirmed?
    say('Canceled deleting stemcell'.make_green)
    return
  end

  status, task_id = director.delete_stemcell(name, version, :force => force)

  task_report(status, task_id, "Deleted stemcell '#{name}/#{version}'")
end
download_public(stemcell_filename) click to toggle source
# File lib/cli/commands/stemcell.rb, line 149
def download_public(stemcell_filename)
  public_stemcells = PublicStemcells.new
  public_stemcells_presenter = PublicStemcellPresenter.new(self, public_stemcells)
  public_stemcells_presenter.download(stemcell_filename)
end
list() click to toggle source
# File lib/cli/commands/stemcell.rb, line 108
def list
  auth_required
  show_current_state

  stemcells = director.list_stemcells.sort do |sc1, sc2|
    if sc1['name'] == sc2['name']
      Bosh::Common::Version::StemcellVersion.parse_and_compare(sc1['version'], sc2['version'])
    else
      sc1['name'] <=> sc2['name']
    end
  end

  err('No stemcells') if stemcells.empty?

  stemcells_table = table do |t|
    t.headings = 'Name', 'OS', 'Version', 'CID'
    stemcells.each do |sc|
      t << get_stemcell_table_record(sc)
    end
  end

  nl
  say(stemcells_table)
  nl
  say('(*) Currently in-use')
  nl
  say('Stemcells total: %d' % stemcells.size)
end
list_public() click to toggle source
# File lib/cli/commands/stemcell.rb, line 141
def list_public
  public_stemcells = PublicStemcells.new
  public_stemcells_presenter = PublicStemcellPresenter.new(self, public_stemcells)
  public_stemcells_presenter.list(options)
end
upload(stemcell_location) click to toggle source
# File lib/cli/commands/stemcell.rb, line 38
def upload(stemcell_location)
  auth_required
  show_current_state

  if options[:skip_if_exists] && options[:fix]
    err("Option '--skip-if-exists' and option '--fix' should not be used together")
  end

  if options[:fix] && (options[:name] || options[:version])
    err("Options '--name' and '--version' should not be used together with option '--fix'")
  end

  stemcell_type = stemcell_location =~ /^#{URI::regexp}$/ ? 'remote' : 'local'

  if options[:name] && options[:version]
    return if exists?(options[:name], options[:version])
  end

  if stemcell_type == 'local'
    err("Option '--sha1' is not supported for uploading local stemcell") unless options[:sha1].nil?

    stemcell = Bosh::Cli::Stemcell.new(stemcell_location)

    nl
    say('Verifying stemcell...')
    stemcell.validate
    nl

    unless stemcell.valid?
      err('Stemcell is invalid, please fix, verify and upload again')
    end

    name = stemcell.manifest['name']
    version = stemcell.manifest['version']

    if !options[:fix] && exists?(name, version)
      if options[:skip_if_exists]
        say("Stemcell '#{name}/#{version}' already exists. Skipping upload.")
        return
      else
        err("Stemcell '#{name}/#{version}' already exists. Increment the version if it has changed.")
      end
    end

    stemcell_location = stemcell.stemcell_file

    nl
    say('Uploading stemcell...')
    nl
  else
    nl
    say("Using remote stemcell '#{stemcell_location}'")
  end

  selected_options = {}
  selected_options[:fix] = options[:fix] if options[:fix]
  selected_options[:sha1] = options[:sha1] if options[:sha1]
  status, task_id = apply_upload_stemcell_strategy(stemcell_type, stemcell_location, selected_options)
  success_message = 'Stemcell uploaded and created.'

  if status == :error && options[:skip_if_exists] && last_event(task_id)['error']['code'] == STEMCELL_EXISTS_ERROR_CODE
    status = :done
    success_message = skip_existing_stemcell_message(stemcell_type, stemcell_location)
  end

  task_report(status, task_id, success_message)
end
verify(tarball_path) click to toggle source
# File lib/cli/commands/stemcell.rb, line 10
def verify(tarball_path)
  stemcell = Bosh::Cli::Stemcell.new(tarball_path)

  nl
  say('Verifying stemcell...')
  stemcell.validate
  nl

  if stemcell.valid?
    say("'#{tarball_path}' is a valid stemcell".make_green)
  else
    say('Validation errors:'.make_red)
    stemcell.errors.each do |error|
      say('- %s' % [error])
    end
    err("'#{tarball_path}' is not a valid stemcell")
  end
end

Private Instance Methods

apply_upload_stemcell_strategy(stemcell_type, stemcell_location, options={}) click to toggle source
# File lib/cli/commands/stemcell.rb, line 188
def apply_upload_stemcell_strategy(stemcell_type, stemcell_location, options={})
  if stemcell_type == 'local'
    director.upload_stemcell(stemcell_location, options)
  else
    director.upload_remote_stemcell(stemcell_location, options)
  end
end
exists?(name, version) click to toggle source
# File lib/cli/commands/stemcell.rb, line 201
def exists?(name, version)
  say('Checking if stemcell already exists...')
  existing = director.list_stemcells.select do |sc|
    sc['name'] == name && sc['version'] == version
  end
  existing.empty? ? say('No'):say('Yes')
  !existing.empty?
end
get_stemcell_table_record(sc) click to toggle source
# File lib/cli/commands/stemcell.rb, line 210
def get_stemcell_table_record(sc)
  deployments = sc.fetch('deployments', [])

  [sc['name'], sc['operating_system'], "#{sc['version']}#{deployments.empty? ? '' : '*'}", sc['cid']]
end
last_event(task_id) click to toggle source
# File lib/cli/commands/stemcell.rb, line 196
def last_event(task_id)
  event_log, _ = director.get_task_output(task_id, 0, 'event')
  JSON.parse(event_log.split("\n").last)
end
skip_existing_stemcell_message(stemcell_type, stemcell_location) click to toggle source
# File lib/cli/commands/stemcell.rb, line 180
def skip_existing_stemcell_message(stemcell_type, stemcell_location)
  if stemcell_type == 'local'
    'Stemcell already exists. Skipping upload.'
  else
    "Stemcell at #{stemcell_location} already exists."
  end
end