forked from blockbridge/blockbridge-docker-volume
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvolume_driver.rb
85 lines (72 loc) · 2.42 KB
/
volume_driver.rb
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
# Copyright (c) 2015-2017, Blockbridge Networks LLC. All rights reserved.
# Use of this source code is governed by a BSD-style license, found
# in the LICENSE file.
require 'bundler/setup'
require 'goliath'
require 'grape'
require_relative 'volume_driver/version'
require_relative 'volume_driver/helpers'
require_relative 'volume_driver/apis'
require_relative 'volume_driver/plugins'
# do NOT use Goliath's at_exit auto-run handler.
Goliath.run_app_on_exit = false
class VolumeDriver
class Server < Goliath::API
use Goliath::Rack::Render # auto-negotiate response format
use Goliath::Rack::Params # parse & merge query and body parameters
plugin Blockbridge::Startup
plugin Blockbridge::Config
plugin Blockbridge::VolumeCacheMonitor
plugin Blockbridge::VolumeHostinfo
# process api call
def response(env)
API::VolumeDriver.call(env)
end
end
Goliath::Request.log_block = proc do |env, response, elapsed_time|
env.logger.debug do
full_uri = env['PATH_INFO']
if (query_string = env['QUERY_STRING']) && !query_string.empty?
full_uri += "?" + query_string
end
str = '- '
if env['api.request.body']
name = env['api.request.body']['Name'].to_s
str = name.empty? ? '- ' : "#{name} "
end
str.concat "#{env['REMOTE_USER'] || '-'} " \
"\"#{env['REQUEST_METHOD']} #{full_uri} #{env['HTTP_VERSION']}\" " \
"#{response.status} " \
"#{response.headers['Content-Length'] || '-'} " \
"[#{"%.2f" % elapsed_time}ms]"
end
end
class Logger < Log4r::Logger
def initialize(name)
super(name)
pattern = "%d %-7l %c -- %m\n"
datefmt = "%Y-%m-%dT%H:%M:%S.%3N"
format = Log4r::PatternFormatter.new(pattern: pattern, date_pattern: datefmt)
stdout = Log4r::StdoutOutputter.new('console', :formatter => format)
add(stdout)
end
end
class Runner < Goliath::Runner
def initialize
super(ARGV, nil)
@api = VolumeDriver::Server.new
@app = Goliath::Rack::Builder.build(VolumeDriver::Server, api)
@logger = VolumeDriver::Logger.new('blockbridge')
@logger.info "Blockbridge Volume Driver #{Blockbridge::VolumeDriverVersion::VERSION}"
end
def run
super
Signal.trap("TERM", "IGNORE")
Signal.trap("INT", "IGNORE")
end
end
end
driver = VolumeDriver::Runner.new()
driver.load_plugins(VolumeDriver::Server.plugins)
driver.run
exit 0