module Bosh::Director::Api::ApiHelper

Constants

READ_CHUNK_SIZE

Public Instance Methods

check_available_disk_space(dir, size) click to toggle source
# File lib/bosh/director/api/api_helper.rb, line 61
def check_available_disk_space(dir, size)
  begin
    stat = Sys::Filesystem.stat(dir)
    available_space = stat.block_size * stat.blocks_available
    available_space > size ? true : false
  rescue
    false
  end
end
json_decode(payload) click to toggle source
# File lib/bosh/director/api/api_helper.rb, line 49
def json_decode(payload)
  JSON.parse(payload)
end
json_encode(payload) click to toggle source
# File lib/bosh/director/api/api_helper.rb, line 45
def json_encode(payload)
  JSON.generate(payload)
end
prepare_yml_file(yml_stream, manifest_type, skip_validation = false) click to toggle source
# File lib/bosh/director/api/api_helper.rb, line 80
def prepare_yml_file(yml_stream, manifest_type, skip_validation = false)
  random_file_name = "#{manifest_type}-#{SecureRandom.uuid}"
  tmp_manifest_dir = Dir::tmpdir

  manifest_file_path = File.join(tmp_manifest_dir, random_file_name)
  unless check_available_disk_space(tmp_manifest_dir, yml_stream.size)
    raise NotEnoughDiskSpace, 'Uploading manifest failed. ' +
        "Insufficient space on BOSH director in #{tmp_manifest_dir}"
  end

  write_file(manifest_file_path, yml_stream)

  validate_manifest_yml(File.read(manifest_file_path)) unless skip_validation

  manifest_file_path
end
send_disposable_file(path, opts = {}) click to toggle source

Adapted from Sinatra::Base#send_file. There is one difference: it uses DisposableFile instead of Rack::File. DisposableFile gets removed on “close” call. This is primarily meant to serve temporary files fetched from the blobstore. We CANNOT use a Sinatra after filter, as the filter is called before the contents of the file is sent to the client.

# File lib/bosh/director/api/api_helper.rb, line 22
def send_disposable_file(path, opts = {})
  if opts[:type] || !response['Content-Type']
    content_type opts[:type] || File.extname(path), :default => 'application/octet-stream'
  end

  disposition = opts[:disposition]
  filename    = opts[:filename]
  disposition = 'attachment' if disposition.nil? && filename
  filename    = path         if filename.nil?
  attachment(filename, disposition) if disposition

  last_modified opts[:last_modified] if opts[:last_modified]

  file      = DisposableFile.new nil
  file.path = path
  result    = file.serving env
  result[1].each { |k,v| headers[k] ||= v }
  headers['Content-Length'] = result[1]['Content-Length']
  halt opts[:status] || result[0], result[2]
rescue Errno::ENOENT
  not_found
end
start_task() { || ... } click to toggle source
# File lib/bosh/director/api/api_helper.rb, line 53
def start_task
  task = yield
  unless task.kind_of?(Models::Task)
    raise "Block didn't return Task object"
  end
  redirect "/tasks/#{task.id}"
end
validate_manifest_yml(yml_string) click to toggle source
# File lib/bosh/director/api/api_helper.rb, line 97
def validate_manifest_yml(yml_string)
  raise BadManifest, 'Manifest should not be empty' unless yml_string.to_s != ''

  begin
    Psych.parse(yml_string)
  rescue Exception => e
    raise BadManifest, "Incorrect YAML structure of the uploaded manifest: #{e.inspect}"
  end
end
write_file(path, stream, chunk_size = READ_CHUNK_SIZE) click to toggle source
# File lib/bosh/director/api/api_helper.rb, line 71
def write_file(path, stream, chunk_size = READ_CHUNK_SIZE)
  buffer = ""
  File.open(path, "w") do |file|
    file.write(buffer) until stream.read(chunk_size, buffer).nil?
  end
rescue SystemCallError => e
  raise SystemError, e.message
end