Module: Bosh::Director::Api::ApiHelper

Included in:
Controllers::BaseController, DeploymentManager, ReleaseManager, StemcellManager
Defined in:
lib/bosh/director/api/api_helper.rb

Defined Under Namespace

Classes: DisposableFile

Constant Summary collapse

READ_CHUNK_SIZE =
16384

Instance Method Summary collapse

Instance Method Details

#check_available_disk_space(dir, size) ⇒ Object



61
62
63
64
65
66
67
68
69
# 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) ⇒ Object



49
50
51
# File 'lib/bosh/director/api/api_helper.rb', line 49

def json_decode(payload)
  Yajl::Parser.parse(payload)
end

#json_encode(payload) ⇒ Object



45
46
47
# File 'lib/bosh/director/api/api_helper.rb', line 45

def json_encode(payload)
  Yajl::Encoder.encode(payload)
end

#send_disposable_file(path, opts = {}) ⇒ Object

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.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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_taskObject



53
54
55
56
57
58
59
# 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

#write_file(path, stream, chunk_size = READ_CHUNK_SIZE) ⇒ Object



71
72
73
74
75
76
77
78
# 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