class Bosh::Cli::ArchiveBuilder

Attributes

options[R]

Public Class Methods

new(archive_repository_provider, options = {}) click to toggle source
# File lib/cli/archive_builder.rb, line 5
def initialize(archive_repository_provider, options = {})
  @archive_repository_provider = archive_repository_provider
  @options = options
end

Public Instance Methods

build(resource) click to toggle source
# File lib/cli/archive_builder.rb, line 10
def build(resource)
  @archive_repository = @archive_repository_provider.get(resource)
  resource.run_script(:prepare)

  artifact = nil
  with_indent('  ') do
    artifact = locate_artifact(resource)
    if artifact.nil?
      return unless validate(resource)

      say("No artifact found for #{resource.name}".make_red)
      artifact = create_artifact(resource)
      say("Generated version '#{artifact.fingerprint}'".make_green)

      unless dry_run?
        artifact = @archive_repository.install(artifact)
      end
    end

    if final? && !dry_run?
      say("Uploading final version '#{artifact.version}'...")
      artifact, blobstore_id = @archive_repository.upload_to_blobstore(artifact)
      say("Uploaded, blobstore id '#{blobstore_id}'")
    end
  end

  artifact
rescue Bosh::Blobstore::BlobstoreError => e
  raise BlobstoreError, "Blobstore error: #{e}"
end
dry_run?() click to toggle source
# File lib/cli/archive_builder.rb, line 41
def dry_run?
  !!options[:dry_run]
end
final?() click to toggle source
# File lib/cli/archive_builder.rb, line 45
def final?
  !!options[:final]
end

Private Instance Methods

cleanup() click to toggle source
# File lib/cli/archive_builder.rb, line 127
def cleanup
  FileUtils.rm_rf(staging_dir)
end
copy_files(resource) click to toggle source
# File lib/cli/archive_builder.rb, line 51
def copy_files(resource)
  resource.files.each do |src, dest|
    dest_path = Pathname(staging_dir).join(dest)
    if File.directory?(src)
      FileUtils.mkdir_p(dest_path)
    else
      FileUtils.mkdir_p(dest_path.parent)
      FileUtils.cp(src, dest_path, preserve: true)
    end
  end
end
create_artifact(resource) click to toggle source
# File lib/cli/archive_builder.rb, line 78
def create_artifact(resource)
  say('Generating...')

  tarball_path = safe_temp_file(resource.name, '.tgz')

  copy_files(resource)
  resource.run_script(:pre_packaging, staging_dir)

  in_staging_dir do
    tar_out = %x`tar -chzf #{tarball_path} . 2>&1`
    unless $?.exitstatus == 0
      raise PackagingError, "Cannot create tarball: #{tar_out}"
    end
  end

  fingerprint = BuildArtifact.make_fingerprint(resource)

  sha1 = BuildArtifact.checksum(tarball_path)
  BuildArtifact.new(resource.name, fingerprint, tarball_path, sha1, resource.dependencies, true, !final?)
ensure
  cleanup
end
file_checksum(path) click to toggle source
# File lib/cli/archive_builder.rb, line 109
def file_checksum(path)
  Digest::SHA1.file(path).hexdigest
end
in_staging_dir() { || ... } click to toggle source
# File lib/cli/archive_builder.rb, line 117
def in_staging_dir
  Dir.chdir(staging_dir) { yield }
end
locate_artifact(resource) click to toggle source
# File lib/cli/archive_builder.rb, line 63
def locate_artifact(resource)
  artifact = @archive_repository.lookup(resource)

  return nil if artifact.nil?

  if artifact.dev_artifact? && final? && !dry_run?
    @archive_repository.promote_from_dev_to_final(artifact)
  end

  artifact
rescue Bosh::Cli::CorruptedArchive => e
  say "#{"Warning".make_red}: #{e.message}"
  nil
end
safe_temp_file(prefix, suffix, dir = Dir.tmpdir) click to toggle source
# File lib/cli/archive_builder.rb, line 121
def safe_temp_file(prefix, suffix, dir = Dir.tmpdir)
  Dir::Tmpname.create([prefix, suffix], dir) do |tmpname, _, _|
    File.open(tmpname, File::RDWR|File::CREAT|File::EXCL).close
  end
end
staging_dir() click to toggle source
# File lib/cli/archive_builder.rb, line 113
def staging_dir
  @staging_dir ||= Dir.mktmpdir
end
validate(resource) click to toggle source
# File lib/cli/archive_builder.rb, line 101
def validate(resource)
  resource.validate!
  true
rescue Bosh::Cli::MissingLicense => e
  say("#{'Warning'.make_red}: #{e.message}")
  false
end