Class: FileFM::Uploaders::Swift

Inherits:
Object
  • Object
show all
Defined in:
lib/filefm/uploaders/swift.rb

Class Method Summary collapse

Class Method Details

.authenticate_keystone(username, tenant, api_key, auth_url) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/filefm/uploaders/swift.rb', line 5

def self.authenticate_keystone(username, tenant, api_key, auth_url)
  Log.debug "Using Swift keystone authentication"
  rackspace_auth_url = auth_url
  uri = URI.parse(rackspace_auth_url)
  connection = Fog::Connection.new(rackspace_auth_url, false)
  req_body= {
    'auth' => {
      'passwordCredentials'  => {
        'username' => username,
        'password' => api_key
      }
    }
  }
  req_body['auth']['tenantName'] = tenant

  response = connection.request({
    :expects  => [200, 204],
    :headers => {'Content-Type' => 'application/json'},
    :body  => MultiJson.encode(req_body),
    :host     => uri.host,
    :method   => 'POST',
    :path     =>  (uri.path and not uri.path.empty?) ? uri.path : 'v2.0'
  })
  body=MultiJson.decode(response.body)

  if svc = body['access']['serviceCatalog'].detect{ |x| x['name'] == 'swift' }
    mgmt_url = svc['endpoints'].detect{|x| x['publicURL']}['publicURL']
    token = body['access']['token']['id']
    r = {
      "X-Storage-Url" => mgmt_url,
      "X-Auth-Token" => token,
      "X-Server-Management-Url" => svc['endpoints'].detect{|x| x['adminURL']}['adminURL']
    } 
    return r
  else
    raise "Unable to parse service catalog."
  end
end

.upload(source, destination, options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/filefm/uploaders/swift.rb', line 44

def self.upload(source, destination, options = {})
  uri = URI.parse(destination)
  if uri.path.split("/").size == 2
    uri.path = File.join(uri.path,File.basename(source))
  end
  if (not options[:username] or not options[:password])
    raise "Invalid Credentials"
  end
  secure = options[:secure] == true 
  scheme = secure ? "https" : "http"
  username = options[:username]
  password = options[:password]

  if options[:auth_url]
    auth_url = options[:auth_url]
    username,tenant = username.split(":")
    out = authenticate_keystone(username, tenant, password, auth_url)
    storage_url = out["X-Storage-Url"]
    auth_token = out["X-Auth-Token"]
  else
    Log.debug "Using Swift legacy auth"
    Log.debug "Legacy auth URL #{scheme}://#{uri.host}:#{uri.port}/auth/v1.0"
    out = RestClient.get "#{scheme}://#{uri.host}:#{uri.port}/auth/v1.0", 'X-Storage-User' => username, 'X-Storage-Pass' => password
    storage_url = out.headers[:x_storage_url]
    auth_token = out.headers[:x_auth_token]
    raise "Error authenticating" unless out.code == 200
  end

  Log.debug "authentication OK"
  Log.debug "X-Storage-Url: #{storage_url}"

  begin
    container = "#{uri.path.split("/")[1]}"
    out = RestClient.get storage_url + "/#{container}", 'X-Storage-User' => username, 'X-Auth-Token' => auth_token
  rescue
    raise "Error accessing the container: #{container}"  
  end

  if options[:progressbar]
    pbar = ProgressBar.new "Progress", 100
    fsize = File.size(source)
    count = 0
  end

  res = FileFM::StreamingUploader.put(
      storage_url + uri.path,
      :headers => { 'X-Auth-Token' => auth_token }, :file => File.open(source) 
  ) do |size|
      if block_given?
        yield size
      elsif options[:progressbar]
       count += size
       # take care of divide by zero 
       per = 0
       if fsize == 0
         per = 100
       else
         per = (100*count)/fsize rescue 100
       end
       per = 100 if per > 100
       pbar.set per
      else
      end
    end
  if options[:progressbar]
    pbar.finish
  end
end