Class: Visor::Image::Store::Lunacloud

Inherits:
Object
  • Object
show all
Includes:
Common::Exception
Defined in:
lib/image/store/lunacloud.rb

Overview

The Lunacloud backend store.

This class handles the management of image files located in the Lunacloud storage system, based on a URI like *lunacloud://<access_key>:<secret_key>@<host>:<port>/<bucket>/<image>*.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, config) ⇒ Object

Initializes a new Lunacloud store client object. Lunacloud credentials are loaded from the URI, on GET and DELETE operations, or from the configuration file for POST and PUT operation.

Parameters:

  • uri (String)

    The URI of the file location.

  • config (Hash)

    A set of configurations for the wanted store, loaded from VISoR configuration file.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/image/store/lunacloud.rb', line 29

def initialize(uri, config)
  @uri    = URI(uri)
  @config = config[:lunacloud]

  if @uri.scheme
    @access_key = @uri.user
    @secret_key = @uri.password
    @bucket     = @uri.path.split('/')[1]
    @file       = @uri.path.split('/')[2]
  else
    @access_key = @config[:access_key]
    @secret_key = @config[:secret_key]
    @bucket     = @config[:bucket]
  end
  @host = 'lcs.lunacloud.com'
  @port = 80
end

Instance Attribute Details

#access_keyObject

Returns the value of attribute access_key.



18
19
20
# File 'lib/image/store/lunacloud.rb', line 18

def access_key
  @access_key
end

#bucketObject

Returns the value of attribute bucket.



18
19
20
# File 'lib/image/store/lunacloud.rb', line 18

def bucket
  @bucket
end

#configObject

Returns the value of attribute config.



18
19
20
# File 'lib/image/store/lunacloud.rb', line 18

def config
  @config
end

#fileObject

Returns the value of attribute file.



18
19
20
# File 'lib/image/store/lunacloud.rb', line 18

def file
  @file
end

#hostObject

Returns the value of attribute host.



18
19
20
# File 'lib/image/store/lunacloud.rb', line 18

def host
  @host
end

#portObject

Returns the value of attribute port.



18
19
20
# File 'lib/image/store/lunacloud.rb', line 18

def port
  @port
end

#secret_keyObject

Returns the value of attribute secret_key.



18
19
20
# File 'lib/image/store/lunacloud.rb', line 18

def secret_key
  @secret_key
end

#uriObject

Returns the value of attribute uri.



18
19
20
# File 'lib/image/store/lunacloud.rb', line 18

def uri
  @uri
end

Instance Method Details

#connectionS3restful::S3::Item

Returns a Lunacloud connection object.

Returns:

  • (S3restful::S3::Item)

    A new Lunacloud connection object.



51
52
53
54
# File 'lib/image/store/lunacloud.rb', line 51

def connection
  S3restful::S3::Item.new(bucket, file, server: host, port: port, protocol: 'http',
                          aws_access_key_id:    access_key, aws_secret_access_key: secret_key)
end

#deleteObject

Deletes the image file from its location.

Raises:

  • (NotFound)

    If the image file was not found.



97
98
99
# File 'lib/image/store/lunacloud.rb', line 97

def delete
  connection.delete
end

#file_exists?(raise_exc = true) ⇒ True, False

Check if the image file exists.

Parameters:

  • raise_exc (True, False) (defaults to: true)

    (true) If it should raise exception or return true/false whether the file exists or not.

Returns:

  • (True, False)

    If raise_exc is false, return true/false whether the file exists or not.

Raises:

  • (NotFound)

    If the image file was not found.



111
112
113
114
115
116
117
118
119
# File 'lib/image/store/lunacloud.rb', line 111

def file_exists?(raise_exc=true)
  exist   = nil
  error   = proc { exist = false }
  success = proc { |res| exist = true if res.response_header.status == 200 }

  connection.head(on_error: error, on_success: success)
  raise NotFound, "No image file found at #{uri}" if raise_exc && !exist
  exist
end

#getObject

Returns the image file to clients, streamed in chunks.

Returns:

  • (Object)

    Yields the file, a chunk at time.



60
61
62
63
64
65
66
67
# File 'lib/image/store/lunacloud.rb', line 60

def get
  s3     = connection.aget
  finish = proc { yield nil }

  s3.stream { |chunk| yield chunk }
  s3.callback &finish
  s3.errback &finish
end

#save(id, tmp_file, format) ⇒ String, Integer

Saves the image file to the its final destination, based on the temporary file created by the server at data reception time.

Parameters:

  • id (String)

    The image id.

  • tmp_file (File)

    The temporary file descriptor.

  • format (String)

    The image file format.

Returns:

  • (String, Integer)

    The generated file location URI and image file size.

Raises:

  • (Duplicated)

    If the image file already exists.



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/image/store/lunacloud.rb', line 80

def save(id, tmp_file, format)
  @file = "#{id}.#{format}"
  uri   = "lunacloud://#{access_key}:#{secret_key}@#{host}:#{port}/#{bucket}/#{file}"
  size  = tmp_file.size

  raise Duplicated, "The image file #{fp} already exists" if file_exists?(false)
  STDERR.puts "COPYING!!"

  connection.store tmp_file.path

  [uri, size]
end