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)
  JSON.parse(payload)
end

#json_encode(payload) ⇒ Object



45
46
47
# 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) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 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 = {}) ⇒ 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

#validate_manifest_yml(yml_string) ⇒ Object

Raises:



97
98
99
100
101
102
103
104
105
# 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) ⇒ 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