Remove Swift storage adapter
This commit is contained in:
		
							parent
							
								
									c9d69c706d
								
							
						
					
					
						commit
						b56eb30180
					
				@ -7,7 +7,6 @@ services:
 | 
				
			|||||||
  - redis-server
 | 
					  - redis-server
 | 
				
			||||||
before_script:
 | 
					before_script:
 | 
				
			||||||
  - cp config.yml.example.$BACKEND config.yml
 | 
					  - cp config.yml.example.$BACKEND config.yml
 | 
				
			||||||
  - mkdir -p tmp && echo "swifttoken" > tmp/swift_token.txt
 | 
					 | 
				
			||||||
script: ruby spec/$BACKEND/*
 | 
					script: ruby spec/$BACKEND/*
 | 
				
			||||||
branches:
 | 
					branches:
 | 
				
			||||||
  only:
 | 
					  only:
 | 
				
			||||||
@ -22,6 +21,5 @@ notifications:
 | 
				
			|||||||
    on_failure: always
 | 
					    on_failure: always
 | 
				
			||||||
env:
 | 
					env:
 | 
				
			||||||
  - BACKEND=s3
 | 
					  - BACKEND=s3
 | 
				
			||||||
  - BACKEND=swift
 | 
					 | 
				
			||||||
# Run on Docker infrastructure
 | 
					# Run on Docker infrastructure
 | 
				
			||||||
sudo: false
 | 
					sudo: false
 | 
				
			||||||
 | 
				
			|||||||
@ -106,6 +106,3 @@ DEPENDENCIES
 | 
				
			|||||||
  sinatra
 | 
					  sinatra
 | 
				
			||||||
  sinatra-contrib
 | 
					  sinatra-contrib
 | 
				
			||||||
  webmock
 | 
					  webmock
 | 
				
			||||||
 | 
					 | 
				
			||||||
BUNDLED WITH
 | 
					 | 
				
			||||||
   1.16.0
 | 
					 | 
				
			||||||
 | 
				
			|||||||
@ -1,18 +0,0 @@
 | 
				
			|||||||
development: &defaults
 | 
					 | 
				
			||||||
  maintenance: false
 | 
					 | 
				
			||||||
  swift: &swift_defaults
 | 
					 | 
				
			||||||
    host: "https://swift.example.com"
 | 
					 | 
				
			||||||
  redis:
 | 
					 | 
				
			||||||
    host: localhost
 | 
					 | 
				
			||||||
    port: 6379
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
test:
 | 
					 | 
				
			||||||
  <<: *defaults
 | 
					 | 
				
			||||||
  swift:
 | 
					 | 
				
			||||||
    host: "https://swift.example.com"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
staging:
 | 
					 | 
				
			||||||
  <<: *defaults
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
production:
 | 
					 | 
				
			||||||
  <<: *defaults
 | 
					 | 
				
			||||||
@ -1,63 +0,0 @@
 | 
				
			|||||||
require "rest_client"
 | 
					 | 
				
			||||||
require "active_support/core_ext/time/conversions"
 | 
					 | 
				
			||||||
require "active_support/core_ext/numeric/time"
 | 
					 | 
				
			||||||
require "active_support/core_ext/hash"
 | 
					 | 
				
			||||||
require "remote_storage/rest_provider"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
module RemoteStorage
 | 
					 | 
				
			||||||
  class Swift
 | 
					 | 
				
			||||||
    include RestProvider
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    # Add quotes around the ETag
 | 
					 | 
				
			||||||
    def format_etag(etag)
 | 
					 | 
				
			||||||
      %Q("#{etag}")
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def base_url
 | 
					 | 
				
			||||||
      @base_url ||= settings.swift["host"]
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def container_url_for(user)
 | 
					 | 
				
			||||||
      "#{base_url}/rs:documents:#{settings.environment.to_s}/#{user}"
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def default_headers
 | 
					 | 
				
			||||||
      {"x-auth-token" => swift_token}
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def reload_swift_token
 | 
					 | 
				
			||||||
      server.logger.debug "Reloading swift token. Old token: #{settings.swift_token}"
 | 
					 | 
				
			||||||
      # Remove the line break from the token file. The line break that the
 | 
					 | 
				
			||||||
      # token script is adding to the file was causing Sentry to reject the
 | 
					 | 
				
			||||||
      # token field
 | 
					 | 
				
			||||||
      settings.swift_token           = File.read(swift_token_path).rstrip
 | 
					 | 
				
			||||||
      settings.swift_token_loaded_at = Time.now
 | 
					 | 
				
			||||||
      server.logger.debug "Reloaded swift token. New token: #{settings.swift_token}"
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def swift_token_path
 | 
					 | 
				
			||||||
      "tmp/swift_token.txt"
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def swift_token
 | 
					 | 
				
			||||||
      reload_swift_token if Time.now - settings.swift_token_loaded_at > 1800
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      settings.swift_token
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def deal_with_unauthorized_requests(&block)
 | 
					 | 
				
			||||||
      begin
 | 
					 | 
				
			||||||
        block.call
 | 
					 | 
				
			||||||
      rescue RestClient::Unauthorized => ex
 | 
					 | 
				
			||||||
        Raven.capture_exception(
 | 
					 | 
				
			||||||
          ex,
 | 
					 | 
				
			||||||
          tags: { swift_token:           settings.swift_token[0..19], # send the first 20 characters
 | 
					 | 
				
			||||||
                  swift_token_loaded_at: settings.swift_token_loaded_at }
 | 
					 | 
				
			||||||
        )
 | 
					 | 
				
			||||||
        server.halt 500
 | 
					 | 
				
			||||||
      end
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
  end
 | 
					 | 
				
			||||||
end
 | 
					 | 
				
			||||||
@ -4,7 +4,6 @@ require "json"
 | 
				
			|||||||
require "sinatra/base"
 | 
					require "sinatra/base"
 | 
				
			||||||
require 'sinatra/config_file'
 | 
					require 'sinatra/config_file'
 | 
				
			||||||
require "sinatra/reloader"
 | 
					require "sinatra/reloader"
 | 
				
			||||||
require "remote_storage/swift"
 | 
					 | 
				
			||||||
require "remote_storage/s3"
 | 
					require "remote_storage/s3"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class LiquorCabinet < Sinatra::Base
 | 
					class LiquorCabinet < Sinatra::Base
 | 
				
			||||||
@ -20,10 +19,6 @@ class LiquorCabinet < Sinatra::Base
 | 
				
			|||||||
    register Sinatra::ConfigFile
 | 
					    register Sinatra::ConfigFile
 | 
				
			||||||
    set :environments, %w{development test production staging}
 | 
					    set :environments, %w{development test production staging}
 | 
				
			||||||
    config_file 'config.yml'
 | 
					    config_file 'config.yml'
 | 
				
			||||||
    if settings.respond_to? :swift
 | 
					 | 
				
			||||||
      set :swift_token, File.read("tmp/swift_token.txt")
 | 
					 | 
				
			||||||
      set :swift_token_loaded_at, Time.now
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  configure :development do
 | 
					  configure :development do
 | 
				
			||||||
@ -130,9 +125,7 @@ class LiquorCabinet < Sinatra::Base
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  def storage
 | 
					  def storage
 | 
				
			||||||
    @storage ||= begin
 | 
					    @storage ||= begin
 | 
				
			||||||
      if settings.respond_to? :swift
 | 
					      if settings.respond_to? :s3
 | 
				
			||||||
        RemoteStorage::Swift.new(settings, self)
 | 
					 | 
				
			||||||
      elsif settings.respond_to? :s3
 | 
					 | 
				
			||||||
        RemoteStorage::S3.new(settings, self)
 | 
					        RemoteStorage::S3.new(settings, self)
 | 
				
			||||||
      else
 | 
					      else
 | 
				
			||||||
        puts <<-EOF
 | 
					        puts <<-EOF
 | 
				
			||||||
 | 
				
			|||||||
@ -1,66 +0,0 @@
 | 
				
			|||||||
require_relative "../spec_helper"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
describe "Swift provider" do
 | 
					 | 
				
			||||||
  def container_url_for(user)
 | 
					 | 
				
			||||||
    "#{app.settings.swift["host"]}/rs:documents:test/#{user}"
 | 
					 | 
				
			||||||
  end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  def storage_class
 | 
					 | 
				
			||||||
    RemoteStorage::Swift
 | 
					 | 
				
			||||||
  end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  before do
 | 
					 | 
				
			||||||
    stub_request(:put, "#{container_url_for("phil")}/food/aguacate").
 | 
					 | 
				
			||||||
      to_return(status: 200, headers: { etag: "0815etag", last_modified: "Fri, 04 Mar 2016 12:20:18 GMT" })
 | 
					 | 
				
			||||||
    # Write new content with an If-Match header (a new Etag is returned)
 | 
					 | 
				
			||||||
    stub_request(:put, "#{container_url_for("phil")}/food/aguacate").
 | 
					 | 
				
			||||||
      with(body: "aye").
 | 
					 | 
				
			||||||
      to_return(status: 200, headers: { etag: "0915etag", last_modified: "Fri, 04 Mar 2016 12:20:18 GMT" })
 | 
					 | 
				
			||||||
    stub_request(:put, "#{container_url_for("phil")}/public/shares/example.jpg").
 | 
					 | 
				
			||||||
      to_return(status: 200, headers: { etag: '"0817etag"', content_type: "image/jpeg", last_modified: "Fri, 04 Mar 2016 12:20:18 GMT" })
 | 
					 | 
				
			||||||
    stub_request(:put, "#{container_url_for("phil")}/public/shares/example_partial.jpg").
 | 
					 | 
				
			||||||
      to_return(status: 200, headers: { etag: '"0817etag"', content_type: "image/jpeg", last_modified: "Fri, 04 Mar 2016 12:20:18 GMT" })
 | 
					 | 
				
			||||||
    stub_request(:head, "#{container_url_for("phil")}/food/aguacate").
 | 
					 | 
				
			||||||
      to_return(status: 200, headers: { last_modified: "Fri, 04 Mar 2016 12:20:18 GMT" })
 | 
					 | 
				
			||||||
    stub_request(:get, "#{container_url_for("phil")}/food/aguacate").
 | 
					 | 
				
			||||||
      to_return(status: 200, body: "rootbody", headers: { etag: "0817etag", content_type: "text/plain; charset=utf-8" })
 | 
					 | 
				
			||||||
    stub_request(:delete, "#{container_url_for("phil")}/food/aguacate").
 | 
					 | 
				
			||||||
      to_return(status: 200, headers: { etag: "0815etag" })
 | 
					 | 
				
			||||||
    stub_request(:get, "#{container_url_for("phil")}/public/shares/example.jpg").
 | 
					 | 
				
			||||||
      to_return(status: 200, body: "", headers: { etag: '"0817etag"', content_type: "image/jpeg" })
 | 
					 | 
				
			||||||
    stub_request(:get, "#{container_url_for("phil")}/public/shares/example_partial.jpg").
 | 
					 | 
				
			||||||
      to_return(status: 206, body: "", headers: { etag: '"0817etag"', content_type: "image/jpeg", content_range: "bytes 0-16/128" })
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    # Write new content to check the metadata in Redis
 | 
					 | 
				
			||||||
    stub_request(:put, "#{container_url_for("phil")}/food/banano").
 | 
					 | 
				
			||||||
      with(body: "si").
 | 
					 | 
				
			||||||
      to_return(status: 200, headers: { etag: "0815etag", last_modified: "Fri, 04 Mar 2016 12:20:18 GMT" })
 | 
					 | 
				
			||||||
    stub_request(:put, "#{container_url_for("phil")}/food/banano").
 | 
					 | 
				
			||||||
      with(body: "oh, no").
 | 
					 | 
				
			||||||
      to_return(status: 200, headers: { etag: "0817etag", last_modified: "Fri, 04 Mar 2016 12:20:20 GMT" })
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    stub_request(:put, "#{container_url_for("phil")}/food/camaron").
 | 
					 | 
				
			||||||
      to_return(status: 200, headers: { etag: "0816etag", last_modified: "Fri, 04 Mar 2016 12:20:18 GMT" })
 | 
					 | 
				
			||||||
    stub_request(:delete, "#{container_url_for("phil")}/food/camaron").
 | 
					 | 
				
			||||||
      to_return(status: 200, headers: { etag: "0816etag" })
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    stub_request(:put, "#{container_url_for("phil")}/food/desayunos/bolon").
 | 
					 | 
				
			||||||
      to_return(status: 200, headers: { etag: "0817etag", last_modified: "Fri, 04 Mar 2016 12:20:18 GMT" })
 | 
					 | 
				
			||||||
    stub_request(:delete, "#{container_url_for("phil")}/food/desayunos/bolon").
 | 
					 | 
				
			||||||
      to_return(status: 200, headers: { etag: "0817etag" })
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    # objects in root dir
 | 
					 | 
				
			||||||
    stub_request(:put, "#{container_url_for("phil")}/bamboo.txt").
 | 
					 | 
				
			||||||
      to_return(status: 200, headers: { etag: "0818etag", last_modified: "Fri, 04 Mar 2016 12:20:18 GMT" })
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    # 404
 | 
					 | 
				
			||||||
    stub_request(:head, "#{container_url_for("phil")}/food/steak").
 | 
					 | 
				
			||||||
      to_return(status: 404)
 | 
					 | 
				
			||||||
    stub_request(:get, "#{container_url_for("phil")}/food/steak").
 | 
					 | 
				
			||||||
      to_return(status: 404)
 | 
					 | 
				
			||||||
    stub_request(:delete, "#{container_url_for("phil")}/food/steak").
 | 
					 | 
				
			||||||
      to_return(status: 404)
 | 
					 | 
				
			||||||
  end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  it_behaves_like 'a REST adapter'
 | 
					 | 
				
			||||||
end
 | 
					 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user