Class: PushmiPullyu::SwiftDepositer

Inherits:
Object
  • Object
show all
Defined in:
lib/pushmi_pullyu/swift_depositer.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ SwiftDepositer

Returns a new instance of SwiftDepositer.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pushmi_pullyu/swift_depositer.rb', line 5

def initialize(connection)
  # Generic authentication parameters
  swift_connection_parameters = {
    username: connection[:username],
    api_key: connection[:password],
    auth_url: connection[:auth_url],
    project_name: connection[:project_name],
    auth_method: 'password',
    service_type: 'object-store',
    # The retry_auth value should be true by default. It is currently set
    # to nil which causes the connection to die without giving more error
    # details.
    retry_auth: true
  }

  if connection[:auth_version] == 'v3'
    swift_connection_parameters[:user_domain] = connection[:user_domain]
  elsif connection[:auth_version] == 'v1'
    swift_connection_parameters[:project_domain_name] = connection[:project_domain_name]
    swift_connection_parameters[:authtenant_name] = connection[:tenant]
  end

  @swift_connection = OpenStack::Connection.create(swift_connection_parameters)
end

Instance Method Details

#deposit_file(file_name, swift_container) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
# File 'lib/pushmi_pullyu/swift_depositer.rb', line 30

def deposit_file(file_name, swift_container)
  file_base_name = File.basename(file_name, '.*')
  checksum = Digest::MD5.file(file_name).hexdigest
  era_container = @swift_connection.container(swift_container)

  # Add swift metadata with in accordance to AIP spec:
  # https://docs.google.com/document/d/154BqhDPAdGW-I9enrqLpBYbhkF9exX9lV3kMaijuwPg/edit#
   = {
    project: 'ERA',
    project_id: file_base_name,
    promise: 'bronze',
    aip_version: '1.0'
  }

  # ruby-openstack wants all keys of the metadata to be named like "X-Object-Meta-{{Key}}", so update them
  .transform_keys! { |key| "X-Object-Meta-#{key}" }

  # ruby-openstack expects the header hash in different structure for write vs create_object methods
  # details see: https://github.com/ualbertalib/pushmi_pullyu/issues/105
  if era_container.object_exists?(file_base_name)
    # temporary solution until fixed in upstream:
    # for update: construct hash for key/value pairs as strings,
    # and metadata as additional key/value string pairs in the hash
    headers = { 'etag' => checksum,
                'content-type' => 'application/x-tar' }.merge()
    deposited_file = era_container.object(file_base_name)
    File.open(file_name) do |file|
      deposited_file.write(file, headers)
    end
  else
    # for creating new: construct hash with symbols as keys, add metadata as a hash within the header hash
    headers = { etag: checksum,
                content_type: 'application/x-tar',
                metadata:  }
    File.open(file_name) do |file|
      deposited_file = era_container.create_object(file_base_name, headers, file)
    end
  end

  deposited_file
end