summaryrefslogtreecommitdiff
path: root/lib/tapicero/user_database.rb
blob: 3cfdd70fb5b0159442d11f734cb089bec800a920 (plain)
1
2
3
4
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
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
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
require 'couchrest'
require 'json'

module Tapicero
  class UserDatabase

    def initialize(host, name)
      @host = host
      @name = name
    end

    def prepare(config)
      create
      secure(config.options[:security])
      add_design_docs
      Tapicero.logger.info "Prepared storage " + name
    end

    def create
      retry_request_once "Creating database" do
        create_db
      end
    end

    def secure(security)
      # let's not overwrite if we have a security doc already
      return if secured? && !Tapicero::FLAGS.include?('--overwrite-security')
      retry_request_once "Writing security to" do
        Tapicero.logger.debug security.to_json
        CouchRest.put security_url, security
      end
    end

    def add_design_docs
      pattern = BASE_DIR + 'designs' + '*.json'
      Tapicero.logger.debug "Looking for design docs in #{pattern}"
      Pathname.glob(pattern).each do |file|
        retry_request_once "Uploading design doc to" do
          upload_design_doc(file)
        end
      end
    end

    def upload_design_doc(file)
      url = design_url(file.basename('.json'))
      CouchRest.put url, JSON.parse(file.read)
    rescue RestClient::Conflict
    end


    def destroy
      retry_request_once "Deleting database" do
        delete_db
      end
    end

    protected

    def create_db
      CouchRest.new(host).create_db(name)
    rescue RestClient::PreconditionFailed  # database already existed
    end

    def delete_db
      db = CouchRest.new(host).database(name)
      db.delete! if db
    rescue RestClient::ResourceNotFound  # no database found
    end

    def retry_request_once(action)
      second_try ||= false
      Tapicero.logger.debug "#{action} #{name}"
      yield
    rescue RestClient::Exception => exc
      if second_try
        log_error "#{action} #{name} failed twice due to:", exc
      else
        log_error "#{action} #{name} failed due to:", exc
        sleep 5
        second_try = true
        retry
      end
    end

    def log_error(message, exc)
      Tapicero.logger.warn message if message
      Tapicero.logger.warn exc.class.name + ': ' + exc.to_s
      Tapicero.logger.debug exc.backtrace.join("\n")
    end

    def secured?
      retry_request_once "Checking security of" do
        CouchRest.get(security_url).keys.any?
      end
    end

    def security_url
      "#{host}/#{name}/_security"
    end

    def design_url(doc_name)
      "#{host}/#{name}/_design/#{doc_name}"
    end

    attr_reader :host, :name
  end
end