Class: HP::Cloud::RemoteResource

Inherits:
Resource
  • Object
show all
Defined in:
lib/hpcloud/remote_resource.rb

Direct Known Subclasses

ContainerResource, SharedResource

Constant Summary collapse

DEFAULT_STORAGE_MAX_SIZE =
5368709120
DEFAULT_STORAGE_SEGMENT_SIZE =
1073741824
DEFAULT_STORAGE_PAGE_LENGTH =
10000
CONTAINER_META =
[ 'Authorization', 'X-Auth-Token', 'X-Container-Read', 'X-Container-Write', 'X-Container-Sync-To', 'X-Container-Sync-Key', 'X-Container-Meta-Web-Index', 'X-Container-Meta-Web-Error', 'X-Container-Meta-Web-Listings', 'X-Container-Meta-Web-Listings-CSS', 'X-Versions-Location' ]
OBJECT_META =
[ 'Content-Type', 'Content-Disposition', 'X-Delete-At', 'X-Delete-After', 'X-Object-Manifest' ]
@@storage_max_size =
nil
@@storage_segment_size =
nil
@@storage_chunk_size =
nil

Instance Attribute Summary collapse

Attributes inherited from Resource

#container, #cstatus, #destination, #fname, #ftype, #path, #public, #public_url, #readers, #restart, #writers

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#copy, #copy_all, #get_mime_type, #initialize, #isDirectory, #isFile, #isLocal, #isMulti, #isObject, #isRemote, #is_container?, #is_object_store?, #is_shared?, #is_valid?, #not_implemented, #set_error, #set_mime_type, #set_restart, #to_hash

Constructor Details

This class inherits a constructor from HP::Cloud::Resource

Instance Attribute Details

#etagObject

Returns the value of attribute etag.



25
26
27
# File 'lib/hpcloud/remote_resource.rb', line 25

def etag
  @etag
end

#headersObject (readonly)

Returns the value of attribute headers.



26
27
28
# File 'lib/hpcloud/remote_resource.rb', line 26

def headers
  @headers
end

#modifiedObject

Returns the value of attribute modified.



25
26
27
# File 'lib/hpcloud/remote_resource.rb', line 25

def modified
  @modified
end

#sizeObject

Returns the value of attribute size.



25
26
27
# File 'lib/hpcloud/remote_resource.rb', line 25

def size
  @size
end

#synckeyObject

Returns the value of attribute synckey.



25
26
27
# File 'lib/hpcloud/remote_resource.rb', line 25

def synckey
  @synckey
end

#synctoObject

Returns the value of attribute syncto.



25
26
27
# File 'lib/hpcloud/remote_resource.rb', line 25

def syncto
  @syncto
end

#typeObject

Returns the value of attribute type.



25
26
27
# File 'lib/hpcloud/remote_resource.rb', line 25

def type
  @type
end

Class Method Details

.VALID_CONTAINER_METAObject



211
212
213
# File 'lib/hpcloud/remote_resource.rb', line 211

def self.VALID_CONTAINER_META
  "  * " + CONTAINER_META.join("\n  * ") + "\n"
end

.VALID_OBJECT_METAObject



207
208
209
# File 'lib/hpcloud/remote_resource.rb', line 207

def self.VALID_OBJECT_META
  "  * " + OBJECT_META.join("\n  * ") + "\n"
end

Instance Method Details

#cdn_public_ssl_urlObject



280
281
282
283
# File 'lib/hpcloud/remote_resource.rb', line 280

def cdn_public_ssl_url
    @cdn_public_ssl_url = @storage.directories.get(@container).files.get(@path).cdn_public_ssl_url
    @cdn_public_ssl_url = @cdn_public_ssl_url.gsub(/%2F/, '/') unless @cdn_public_ssl_url.nil?
end

#cdn_public_urlObject



275
276
277
278
# File 'lib/hpcloud/remote_resource.rb', line 275

def cdn_public_url
    @cdn_public_url = @storage.directories.get(@container).files.get(@path).cdn_public_url
    @cdn_public_url = @cdn_public_url.gsub(/%2F/, '/') unless @cdn_public_url.nil?
end

#closeObject



260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/hpcloud/remote_resource.rb', line 260

def close
  @write_io.close unless @write_io.nil?
  @write_io = nil
  @write_thread.join unless @write_thread.nil?
  @write_thread = nil
  @read_io.close unless @read_io.nil?
  @read_io = nil
  @pbar.increment(@lastread) unless @pbar.nil?
  @pbar.finish unless @pbar.nil?
  @lastread = 0
  @pbar = nil

  return true
end

#container_head(force = false) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/hpcloud/remote_resource.rb', line 88

def container_head(force=false)
  begin
    unless force
      return true unless @size.nil?
    end
    @size = 0
    begin
      data = @storage.head_container(@container)
    rescue NoMethodError => e
      data = @storage.directories.get(@container)
      @count = data.files.size.to_s
      return true
    end
    if data.nil? || data.headers.nil?
      @cstatus = CliStatus.new("Cannot find container ':#{@container}'.", :not_found)
      return false
    end
    @tainer_head = data.headers
    return parse_container_headers(@tainer_head)
  rescue Excon::Errors::Forbidden => e
    resp = ErrorResponse.new(e)
    @cstatus = CliStatus.new(resp.error_string, :permission_denied)
    return false
  rescue Fog::Storage::HP::NotFound => error
    @cstatus = CliStatus.new("Cannot find container ':#{@container}'.", :not_found)
    return false
  rescue Fog::HP::Errors::Forbidden => error
    @cstatus = CliStatus.new("Permission denied trying to access '#{@fname}'.", :permission_denied)
    return false
  rescue Exception => error
    @cstatus = CliStatus.new("Error reading '#{@fname}': " + error.to_s, :general_error)
    return false
  end
end

#copy_file(from) ⇒ Object



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/hpcloud/remote_resource.rb', line 314

def copy_file(from)
  result = true
  return true if from.fname.end_with?("/") # application/directory
  return false if (from.open() == false)
  if from.isLocal()
    if @@storage_segment_size.nil?
      config = Config.new
      @@storage_max_size = config.get_i(:storage_max_size, DEFAULT_STORAGE_MAX_SIZE)
      @@storage_segment_size = config.get_i(:storage_segment_size, DEFAULT_STORAGE_SEGMENT_SIZE)
      @@storage_chunk_size = config.get_i(:storage_chunk_size, Excon::DEFAULT_CHUNK_SIZE)
      @@storage_chunk_size = @@storage_segment_size if @@storage_segment_size < @@storage_chunk_size
    end
    @options = { 'Content-Type' => from.get_mime_type() }
    count = 0
    segment = i=10000000001
    total = from.get_size()
    if total > @@storage_max_size
      prefix = @destination + '.segment.'
      begin
        bytes_read = 0
        bytes_to_read = total - count
        bytes_to_read = @@storage_segment_size if bytes_to_read > @@storage_segment_size
        tmppath = prefix + segment.to_s[1..10]
        already_exists = false
        begin
          if @restart == true
            response = @storage.head_object(@container, tmppath)
            segsiz = response.headers['Content-Length'].to_i
            if segsiz == bytes_to_read
              already_exists = true
            end
          end
        rescue
        end
        if already_exists
          # skip the bytes
          while bytes_to_read > 0 do
            body = from.read(@@storage_chunk_size)
            bytes_read += body.length
            bytes_to_read -= body.length
          end
        else
          chunk_size = @@storage_chunk_size
          @storage.put_object(@container, tmppath, nil, @options) {
            chunk_size = bytes_to_read if bytes_to_read < chunk_size
            body = from.read(chunk_size)
            bytes_read += body.length
            bytes_to_read -= body.length
            body
          }
        end
        count = count + bytes_read
        segment = segment + 1
      end until count >= total
      manifest = @destination
      @options['x-object-manifest'] = @container + '/' + prefix
      @storage.put_object(@container, manifest, nil, @options)
    else
      @storage.put_object(@container, @destination, nil, @options) {
        from.read(@@storage_chunk_size)
      }
    end
  else
    begin
      if from.(@storage)
        @storage.put_object(@container, @destination, nil, {'X-Copy-From' => "/#{from.container}/#{from.path}" })
      else
        @lastread = 0
        siz = from.get_size()
        @pbar = Progress.new(@destination, from.get_size())
        @options = { 'Content-Type' => from.get_mime_type() }
        from.read() { |chunk|
          if ! write(chunk)
            result = false
            break
          end
          @pbar.increment(@lastread) unless @pbar.nil?
          @lastread = chunk.length
        }
      end
    rescue Fog::Storage::HP::NotFound => e
      @cstatus = CliStatus.new("The specified object does not exist.", :not_found)
      result = false
    end
  end
  result = false if ! from.close()
  result = false unless close()
  return result
end

#foreach(&block) ⇒ Object



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'lib/hpcloud/remote_resource.rb', line 404

def foreach(&block)
  case @ftype
  when :container_directory
    regex = "^" + path + ".*"
  when :container
    regex = ".*"
  else
    regex = "^" + path + '$'
  end
  if @@limit.nil?
    @@limit = Config.new.get_i(:storage_page_length, DEFAULT_STORAGE_PAGE_LENGTH)
  end
  total = 0
  count = 0
  marker = nil
  begin
    options = { :limit => @@limit, :marker => marker }
    begin
      result = @storage.get_container(@container, options)
    rescue NoMethodError => e
      @storage.directories.get(@container).files.each { |file|
        yield ResourceFactory.create(@storage, ':' + @container + '/' + file.key)
      }
      return
    end
    total = result.headers['X-Container-Object-Count'].to_i
    lode = result.body.length
    count += lode
    result.body.each { |x|
      name = x['name']
      if ! name.match(regex).nil?
        res = ResourceFactory.create(@storage, ':' + @container + '/' + name)
        res.path = name
        res.etag = x['hash']
        res.modified = x['last_modified']
        res.size = x['bytes']
        res.type = x['content_type']
        yield res
      end
      marker = name
    }
    break if lode < @@limit
  end until count >= total
end

#get_destinationObject



449
450
451
# File 'lib/hpcloud/remote_resource.rb', line 449

def get_destination()
  return ':' + @container.to_s + '/' + @destination.to_s
end

#get_sizeObject



52
53
54
55
56
# File 'lib/hpcloud/remote_resource.rb', line 52

def get_size()
  @size = nil if @size == 0 # manifest file
  return 0 unless object_head()
  return @size
end

#grant(acl) ⇒ Object



497
498
499
500
# File 'lib/hpcloud/remote_resource.rb', line 497

def grant(acl)
  @cstatus = CliStatus.new("ACLs are only supported on containers (e.g. :container).", :not_supported)
  return false
end

#has_same_account(storage) ⇒ Object



507
508
509
# File 'lib/hpcloud/remote_resource.rb', line 507

def (storage)
  return storage == @storage
end

#headObject



58
59
60
# File 'lib/hpcloud/remote_resource.rb', line 58

def head
  return object_head()
end

#object_headObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/hpcloud/remote_resource.rb', line 139

def object_head()
  begin
    return true unless @size.nil?
    @size = 0
    data = @storage.head_object(@container, @path)
    if data.nil? || data.headers.nil?
      @cstatus = CliStatus.new("Cannot find object ':#{@container}/#{@path}'.", :not_found)
      return false
    end
    return parse_object_headers(data.headers)
  rescue Fog::Storage::HP::NotFound => error
    @cstatus = CliStatus.new("Cannot find object ':#{@container}/#{@path}'.", :not_found)
    return false
  rescue Excon::Errors::Forbidden => e
    resp = ErrorResponse.new(e)
    @cstatus = CliStatus.new(resp.error_string, :permission_denied)
    return false
  rescue Fog::HP::Errors::Forbidden => error
    @cstatus = CliStatus.new("Permission denied trying to access '#{@fname}'.", :permission_denied)
    return false
  rescue Exception => error
    @cstatus = CliStatus.new("Error oh reading '#{@fname}': " + error.to_s, :general_error)
    return false
  end
end

#open(output = false, siz = 0) ⇒ Object



219
220
221
# File 'lib/hpcloud/remote_resource.rb', line 219

def open(output=false, siz=0)
  return object_head()
end

#parseObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/hpcloud/remote_resource.rb', line 36

def parse
  super
  
  unless @fname.index('<').nil?
    raise Exception.new("Valid object names do not contain the '<' character: #{@fname}")
  end
  unless @fname.index('>').nil?
    raise Exception.new("Valid object names do not contain the '>' character: #{@fname}")
  end
  unless @fname.index('"').nil?
    raise Exception.new("Valid object names do not contain the '\"' character: #{@fname}")
  end
  @lname = @fname
  @sname = @path
end

#parse_container_headers(headers) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/hpcloud/remote_resource.rb', line 62

def parse_container_headers(headers)
  @headers = headers
  @size = headers['X-Container-Bytes-Used']
  @size = 0 if @size.nil?
  @count = headers['X-Container-Object-Count']
  @synckey = headers['X-Container-Sync-Key']
  @syncto = headers['X-Container-Sync-To']
  @writeacl = AclWriter.new(headers)
  @readacl = AclReader.new(headers)
  @public = @readacl.public
  @readers = @readacl.users.join(",") unless @readacl.users.nil?
  @writers = @writeacl.users.join(",") unless @writeacl.users.nil?
  @versions = headers['X-Versions-Location']
  @public_url = ""
  begin
    if @path.nil? || @path.empty?
      @public_url = "#{@storage.url}/#{@container}"
    else
      @public_url = "#{@storage.url}/#{@container}/#{@path}"
    end
  rescue
  end
  @public_url = @public_url.gsub(/%2F/, '/') unless @public_url.nil?
  return true
end

#parse_object_headers(headers) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/hpcloud/remote_resource.rb', line 123

def parse_object_headers(headers)
  @headers = headers
  @size = headers['Content-Length'].to_i
  @size = 0 if @size.nil?
  @modified = headers['Last-Modified']
  @etag = headers['Etag']
  @type = headers['Content-Type']
  @public_url = ""
  begin
    @public_url = "#{@storage.url}/#{@container}/#{@path}"
    @public_url = @public_url.gsub(/%2F/, '/') unless @public_url.nil?
  rescue
  end
  return true
end

#printable_container_headersObject



181
182
183
184
185
186
187
188
189
190
# File 'lib/hpcloud/remote_resource.rb', line 181

def printable_container_headers
  hsh = @headers.dup
  hsh.delete('Accept-Ranges')
  hsh.delete('Content-Length')
  hsh.delete('Content-Type')
  hsh.delete('Date')
  hsh.delete('X-Timestamp')
  hsh.delete('X-Trans-Id')
  hsh
end

#printable_headersObject



165
166
167
# File 'lib/hpcloud/remote_resource.rb', line 165

def printable_headers
  printable_object_headers
end

#printable_object_headersObject



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/hpcloud/remote_resource.rb', line 169

def printable_object_headers
  hsh = @headers.dup
  hsh.delete('Accept-Ranges')
  hsh.delete('Content-Length')
  hsh.delete('Date')
  hsh.delete('Etag')
  hsh.delete('Last-Modified')
  hsh.delete('X-Timestamp')
  hsh.delete('X-Trans-Id')
  hsh
end

#readObject



223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/hpcloud/remote_resource.rb', line 223

def read
  begin
    yielded_something = false
    @storage.get_object(@container, @path) { |chunk, remain, tot|
      yield chunk
      yielded_something = true
    }
    yield '' unless yielded_something
  rescue Fog::Storage::HP::NotFound => e
    @cstatus = CliStatus.new("The specified object does not exist.", :not_found)
    result = false
  end
end

#remove(force, at = nil, after = nil) ⇒ Object



453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/hpcloud/remote_resource.rb', line 453

def remove(force, at=nil, after=nil)
  begin
    return false unless container_head()
    if at.nil?
      if after.nil?
        @storage.delete_object(@container, @path)
      else
        hsh = { 'X-Delete-After' => after}
        @storage.post_object(@container, @path, hsh)
      end
    else
      hsh = { 'X-Delete-At' => at}
      @storage.post_object(@container, @path, hsh)
    end
  rescue Fog::Storage::HP::NotFound => error
    @cstatus = CliStatus.new("You don't have an object named '#{@fname}'.", :not_found)
    return false
  rescue Excon::Errors::Forbidden => error
    @cstatus = CliStatus.new("Permission denied for '#{@fname}.", :permission_denied)
    return false
  rescue Exception => e
    @cstatus = CliStatus.new("Exception removing '#{@fname}': " + e.to_s, :general_error)
    return false
  end
  return true
end

#revoke(acl) ⇒ Object



502
503
504
505
# File 'lib/hpcloud/remote_resource.rb', line 502

def revoke(acl)
  @cstatus = CliStatus.new("ACLs are only supported on containers (e.g. :container).", :not_supported)
  return false
end

#set_destination(name) ⇒ Object



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/hpcloud/remote_resource.rb', line 298

def set_destination(name)
  return false unless container_head()
  if (@path.empty?)
    @destination = name
  else
    if isObject()
      @destination = @path
    else
      @destination = @path
      @destination += '/' unless @destination.end_with?('/')
      @destination += name
    end
  end
  return true
end

#set_metadata(key, value) ⇒ Object



201
202
203
204
205
# File 'lib/hpcloud/remote_resource.rb', line 201

def (key, value)
  hsh = printable_headers()
  hsh[key] = value
  @storage.post_object(@container, @path, hsh)
end

#tempurl(period, for_update = false) ⇒ Object



480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'lib/hpcloud/remote_resource.rb', line 480

def tempurl(period, for_update=false)
  begin
    period = 172800 if period.nil?
    return nil unless object_head()

    return @storage.get_object_temp_url(@container, @path, period, "PUT") if (for_update)
    return @storage.get_object_temp_url(@container, @path, period, "GET")
  rescue Excon::Errors::Forbidden => error
    @cstatus = CliStatus.new("Permission denied for '#{@fname}.", :permission_denied)
    return nil
  rescue Exception => e
    @cstatus = CliStatus.new("Exception getting temporary URL for '#{@fname}': " + e.to_s, :general_error)
    return nil
  end
  return nil
end

#valid_destination(source) ⇒ Object



289
290
291
292
293
294
295
296
# File 'lib/hpcloud/remote_resource.rb', line 289

def valid_destination(source)
  return false unless container_head()
  if ((source.isMulti() == true) && (isDirectory() == false))
    @cstatus = CliStatus.new("Invalid target for directory/multi-file copy '#{@fname}'.", :incorrect_usage)
    return false
  end
  return true
end

#valid_metadata_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


195
196
197
198
199
# File 'lib/hpcloud/remote_resource.rb', line 195

def (key)
  return true if key.start_with?("X-Object-Meta-")
  return true unless OBJECT_META.index(key).nil?
  return false
end

#valid_sourceObject



285
286
287
# File 'lib/hpcloud/remote_resource.rb', line 285

def valid_source()
  return container_head()
end

#write(chunk) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/hpcloud/remote_resource.rb', line 237

def write(chunk)
  if @write_io.nil?
    begin
      @read_io, @write_io = IO.pipe
      @write_thread = Thread.new {
        @storage.put_object(@container, @destination, {}, @options) {
          @read_io.read
        }
      }
    rescue Exception => e
      @cstatus = CliStatus.new("Error writing object creating thread.")
      return false
    end
  end
  begin
    @write_io.write(chunk)
  rescue Exception => e
    @cstatus = CliStatus.new("Error writing object.")
    return false
  end
  return true
end