prometheus-splash 0.4.5 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +64 -2
- data/README.md +58 -60
- data/bin/splash +13 -5
- data/config/splash.yml +77 -7
- data/lib/splash/backends.rb +6 -0
- data/lib/splash/backends/file.rb +40 -0
- data/lib/splash/backends/redis.rb +47 -4
- data/lib/splash/cli.rb +31 -3
- data/lib/splash/cli/commands.rb +82 -33
- data/lib/splash/cli/config.rb +25 -4
- data/lib/splash/cli/daemon.rb +11 -14
- data/lib/splash/cli/documentation.rb +5 -2
- data/lib/splash/cli/logs.rb +27 -17
- data/lib/splash/cli/process.rb +103 -0
- data/lib/splash/cli/sequences.rb +112 -0
- data/lib/splash/cli/webadmin.rb +46 -0
- data/lib/splash/commands.rb +146 -120
- data/lib/splash/config.rb +170 -15
- data/lib/splash/config/flush.rb +30 -0
- data/lib/splash/config/sanitycheck.rb +7 -2
- data/lib/splash/config/service.rb +33 -0
- data/lib/splash/config/setup.rb +8 -3
- data/lib/splash/constants.rb +37 -8
- data/lib/splash/daemon.rb +6 -0
- data/lib/splash/daemon/controller.rb +136 -0
- data/lib/splash/daemon/metrics.rb +103 -0
- data/lib/splash/daemon/orchestrator.rb +182 -0
- data/lib/splash/daemon/orchestrator/grammar.rb +76 -0
- data/lib/splash/daemon/orchestrator/hooks.rb +28 -0
- data/lib/splash/dependencies.rb +17 -2
- data/lib/splash/exiter.rb +15 -0
- data/lib/splash/helpers.rb +91 -50
- data/lib/splash/loggers.rb +48 -17
- data/lib/splash/loggers/cli.rb +18 -3
- data/lib/splash/loggers/daemon.rb +23 -4
- data/lib/splash/loggers/dual.rb +35 -2
- data/lib/splash/loggers/web.rb +51 -0
- data/lib/splash/logs.rb +67 -53
- data/lib/splash/processes.rb +83 -0
- data/lib/splash/sequences.rb +105 -0
- data/lib/splash/templates.rb +10 -0
- data/lib/splash/transports.rb +9 -0
- data/lib/splash/transports/rabbitmq.rb +33 -4
- data/lib/splash/webadmin.rb +122 -0
- data/lib/splash/webadmin/api/routes/commands.rb +28 -0
- data/lib/splash/webadmin/api/routes/config.rb +10 -0
- data/lib/splash/webadmin/api/routes/init.rb +2 -0
- data/lib/splash/webadmin/api/routes/logs.rb +59 -0
- data/lib/splash/webadmin/api/routes/process.rb +60 -0
- data/lib/splash/webadmin/main.rb +26 -0
- data/lib/splash/webadmin/portal/controllers/commands.rb +7 -0
- data/lib/splash/webadmin/portal/controllers/documentation.rb +6 -0
- data/lib/splash/webadmin/portal/controllers/home.rb +12 -0
- data/lib/splash/webadmin/portal/controllers/logs.rb +14 -0
- data/lib/splash/webadmin/portal/controllers/notfound.rb +5 -0
- data/lib/splash/webadmin/portal/controllers/processes.rb +14 -0
- data/lib/splash/webadmin/portal/controllers/proxy.rb +28 -0
- data/lib/splash/webadmin/portal/controllers/restclient.rb +19 -0
- data/lib/splash/webadmin/portal/init.rb +11 -0
- data/lib/splash/webadmin/portal/public/css/ultragreen.css +8544 -0
- data/lib/splash/webadmin/portal/public/fonts/FontAwesome.otf +0 -0
- data/lib/splash/webadmin/portal/public/fonts/fontawesome-webfont.ttf +0 -0
- data/lib/splash/webadmin/portal/public/fonts/fontawesome-webfont.woff +0 -0
- data/lib/splash/webadmin/portal/public/fonts/fontawesome-webfont.woff2 +0 -0
- data/lib/splash/webadmin/portal/public/images/logo_splash.png +0 -0
- data/lib/splash/webadmin/portal/public/images/logo_splash_reduce.png +0 -0
- data/lib/splash/webadmin/portal/public/images/logo_splash_tiny.png +0 -0
- data/lib/splash/webadmin/portal/views/commands.slim +49 -0
- data/lib/splash/webadmin/portal/views/documentation.slim +3 -0
- data/lib/splash/webadmin/portal/views/home.slim +78 -0
- data/lib/splash/webadmin/portal/views/layout.slim +43 -0
- data/lib/splash/webadmin/portal/views/logs.slim +32 -0
- data/lib/splash/webadmin/portal/views/nav.slim +17 -0
- data/lib/splash/webadmin/portal/views/not_found.slim +3 -0
- data/lib/splash/webadmin/portal/views/processes.slim +29 -0
- data/lib/splash/webadmin/portal/views/proxy.slim +13 -0
- data/lib/splash/webadmin/portal/views/restclient.slim +41 -0
- data/lib/splash/webadmin/portal/views/restclient_result.slim +29 -0
- data/prometheus-splash.gemspec +8 -1
- data/spec/helpers_spec.rb +119 -0
- data/templates/ansible-splash/Vagrantfile +1 -1
- data/templates/ansible-splash/deploy.yml +28 -1
- data/templates/ansible-splash/group_vars/DEV.yml +2 -0
- data/templates/ansible-splash/roles/mq/handlers/main.yml +1 -1
- data/templates/ansible-splash/roles/mq/tasks/main.yml +17 -1
- data/templates/ansible-splash/roles/mq/templates/rabbitmq-env.conf.j2 +13 -0
- data/templates/ansible-splash/roles/splash/handlers/main.yml +4 -0
- data/templates/ansible-splash/roles/splash/tasks/main.yml +25 -1
- data/templates/ansible-splash/roles/splash/templates/logrotate.splash.j2 +11 -0
- data/templates/ansible-splash/roles/splash/templates/splash.yml.j2 +1 -1
- data/templates/splashd.service +1 -1
- data/test.sh +42 -0
- metadata +159 -11
- data/lib/splash/controller.rb +0 -90
- data/lib/splash/orchestrator.rb +0 -100
- data/lib/splash/orchestrator/grammar.rb +0 -53
- data/lib/splash/orchestrator/hooks.rb +0 -16
- data/templates/ansible-splash/inventory.prod +0 -26
@@ -0,0 +1,76 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
# base Splash module
|
4
|
+
module Splash
|
5
|
+
|
6
|
+
# global daemon module
|
7
|
+
module Daemon
|
8
|
+
|
9
|
+
# orchestrator specific module
|
10
|
+
module Orchestrator
|
11
|
+
|
12
|
+
# Orchestrator grammar method defiition module
|
13
|
+
module Grammar
|
14
|
+
|
15
|
+
include Splash::Config
|
16
|
+
include Splash::Loggers
|
17
|
+
|
18
|
+
# list of known verbs for Splash orchestrator
|
19
|
+
VERBS=[:ping,:list_commands,:execute_command,:ack_command, :shutdown]
|
20
|
+
|
21
|
+
# shutdown verb : stop the Splash daemon gracefully
|
22
|
+
# @param [Hash] content message content Hash Structure, ignored
|
23
|
+
# @return [Hash] Exiter Case :quiet_exit
|
24
|
+
def shutdown(content)
|
25
|
+
terminate
|
26
|
+
end
|
27
|
+
|
28
|
+
# ping verb : return pong to hostname precise in payload
|
29
|
+
# @param [Hash] content message content Hash Structure, include mandatory payload[:hostname]
|
30
|
+
# @return [String] the pong
|
31
|
+
def ping(content)
|
32
|
+
return "Pong : #{content[:payload][:hostname]} !"
|
33
|
+
end
|
34
|
+
|
35
|
+
# list_commands verb : return the list of specified commands in local Splash
|
36
|
+
# @param [Hash] content message content Hash Structure, ignored
|
37
|
+
# @return [Hash] structure of commands
|
38
|
+
def list_commands(content)
|
39
|
+
return get_config.commands
|
40
|
+
end
|
41
|
+
|
42
|
+
# ack_command verb : send ack to Prometheus, for command specified in payload
|
43
|
+
# @param [Hash] content message content Hash Structure, include mandatory payload[:name]
|
44
|
+
# @return [Hash] Exiter case
|
45
|
+
def ack_command(content)
|
46
|
+
return execute command: content[:payload][:name], ack: true
|
47
|
+
end
|
48
|
+
|
49
|
+
# execute_command verb : execute command specified in payload
|
50
|
+
# @param [Hash] content message content Hash Structure, include mandatory payload[:name]
|
51
|
+
# @return [Hash] Exiter case
|
52
|
+
def execute_command(content)
|
53
|
+
payload = content[:payload]
|
54
|
+
unless get_config.commands.include? payload[:name].to_sym
|
55
|
+
@log.item "Command not found", content[:session]
|
56
|
+
return { :case => :not_found }
|
57
|
+
end
|
58
|
+
if payload.include? :schedule then
|
59
|
+
sched,value = payload[:schedule].flatten
|
60
|
+
@log.schedule "remote call command #{payload[:name]}, scheduling : #{sched.to_s} #{value}", content[:session]
|
61
|
+
@server.send sched,value do
|
62
|
+
@log.trigger "Executing Scheduled command #{payload[:name]} for Scheduling : #{sched.to_s} #{value}", content[:session]
|
63
|
+
execute command: payload[:name], session: content[:session]
|
64
|
+
end
|
65
|
+
return { :case => :quiet_exit }
|
66
|
+
else
|
67
|
+
@log.info "Execute direct command", content[:session]
|
68
|
+
res = execute command: payload[:name], session: content[:session]
|
69
|
+
return res
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
# base Splash module
|
4
|
+
module Splash
|
5
|
+
|
6
|
+
# global daemon module
|
7
|
+
module Daemon
|
8
|
+
|
9
|
+
# orchestrator specific module
|
10
|
+
module Orchestrator
|
11
|
+
|
12
|
+
# Rufus Scheduler Hooks
|
13
|
+
module SchedulerHooks
|
14
|
+
|
15
|
+
# not yet implemented
|
16
|
+
def on_pre_trigger(job, trigger_time)
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
# not yet implemented
|
21
|
+
def on_post_trigger(job, trigger_time)
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/splash/dependencies.rb
CHANGED
@@ -12,6 +12,8 @@ module Splash
|
|
12
12
|
require 'fileutils'
|
13
13
|
require 'etc'
|
14
14
|
require 'forwardable'
|
15
|
+
require 'json'
|
16
|
+
require 'uri'
|
15
17
|
|
16
18
|
|
17
19
|
|
@@ -26,6 +28,14 @@ module Splash
|
|
26
28
|
require 'tty-pager'
|
27
29
|
require 'colorize'
|
28
30
|
require "redis"
|
31
|
+
require 'ps-ruby'
|
32
|
+
require 'sinatra/base'
|
33
|
+
require 'thin'
|
34
|
+
require 'slim'
|
35
|
+
require 'rest-client'
|
36
|
+
require 'kramdown'
|
37
|
+
require 'rack/reverse_proxy'
|
38
|
+
|
29
39
|
|
30
40
|
rescue Gem::GemNotFoundException
|
31
41
|
$stderr.puts "Loadind error, it's like you try to run Splash, with a lake of dependencies."
|
@@ -46,9 +56,14 @@ module Splash
|
|
46
56
|
|
47
57
|
|
48
58
|
require 'splash/commands'
|
59
|
+
require 'splash/sequences'
|
60
|
+
|
49
61
|
require 'splash/logs'
|
50
|
-
require 'splash/
|
51
|
-
|
62
|
+
require 'splash/processes'
|
63
|
+
|
64
|
+
require 'splash/daemon'
|
65
|
+
require 'splash/webadmin'
|
66
|
+
|
52
67
|
|
53
68
|
end
|
54
69
|
end
|
data/lib/splash/exiter.rb
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# coding: utf-8
|
2
|
+
|
3
|
+
# base Splash namespace
|
2
4
|
module Splash
|
5
|
+
|
6
|
+
# Exiter namespace
|
3
7
|
module Exiter
|
4
8
|
include Splash::Loggers
|
5
9
|
EXIT_MAP= {
|
@@ -20,6 +24,7 @@ module Splash
|
|
20
24
|
|
21
25
|
# global
|
22
26
|
:quiet_exit => {:code => 0},
|
27
|
+
:error_exit => {:code => 99, :message => "Operation failure"},
|
23
28
|
|
24
29
|
# events
|
25
30
|
:interrupt => {:message => "Splash user operation interrupted", :code => 33},
|
@@ -34,6 +39,10 @@ module Splash
|
|
34
39
|
|
35
40
|
}
|
36
41
|
|
42
|
+
# exiter wrapper
|
43
|
+
# @param [Hash] options
|
44
|
+
# @option options [Symbol] :case an exit case
|
45
|
+
# @option options [String] :more a complementary string to display
|
37
46
|
def splash_exit(options = {})
|
38
47
|
log = get_logger
|
39
48
|
mess = ""
|
@@ -49,7 +58,13 @@ module Splash
|
|
49
58
|
end
|
50
59
|
end
|
51
60
|
|
61
|
+
def splash_return(options = {})
|
52
62
|
|
63
|
+
data = EXIT_MAP[options[:case]]
|
64
|
+
data[:status] = (data[:code]>0)? :failure : :success
|
65
|
+
data[:more] = options[:more] if options[:more]
|
66
|
+
return data
|
67
|
+
end
|
53
68
|
|
54
69
|
end
|
55
70
|
end
|
data/lib/splash/helpers.rb
CHANGED
@@ -1,34 +1,47 @@
|
|
1
|
-
# @option options [String] :stdout_trace the path of the file where to redirect STDOUT
|
2
1
|
# coding: utf-8
|
2
|
+
|
3
|
+
# base Splash Module
|
3
4
|
module Splash
|
4
|
-
module Helpers
|
5
5
|
|
6
|
+
# Helpers namespace
|
7
|
+
module Helpers
|
6
8
|
|
7
9
|
|
10
|
+
# return the 'root' name
|
11
|
+
# @return [String] name
|
8
12
|
def user_root
|
9
13
|
return Etc.getpwuid(0).name
|
10
14
|
end
|
11
15
|
|
16
|
+
# return the 'root' group name : root or wheel
|
17
|
+
# @return [String] name
|
12
18
|
def group_root
|
13
19
|
return Etc.getgrgid(0).name
|
14
20
|
end
|
15
21
|
|
16
|
-
#
|
22
|
+
# facility for retreiving PID from process query
|
17
23
|
# @param [Hash] options
|
18
|
-
# @option options [String] :pattern
|
19
|
-
# @
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
24
|
+
# @option options [String] :pattern a regexp to search
|
25
|
+
# @option options [Array] :patterns an array of regexp to search
|
26
|
+
# @option options [Bool] :full flag to retrieve all process data not only PID
|
27
|
+
# @return [String|Array] PID or data structure
|
28
|
+
def get_processes(options = {})
|
29
|
+
patterns = []
|
30
|
+
patterns = options[:patterns] if options[:patterns]
|
31
|
+
patterns << options[:pattern] if options[:pattern]
|
32
|
+
res = PS.get_all_processes
|
33
|
+
patterns.each do |item|
|
34
|
+
res = res.find_processes item
|
35
|
+
end
|
36
|
+
if options[:full] then
|
37
|
+
return res
|
25
38
|
else
|
26
|
-
return ''
|
39
|
+
return res.pick_attr('PID')
|
27
40
|
end
|
28
41
|
end
|
29
42
|
|
30
43
|
|
31
|
-
#
|
44
|
+
# facility to find a file in gem path
|
32
45
|
# @param [String] _gem a Gem name
|
33
46
|
# @param [String] _file a file relative path in the gem
|
34
47
|
# @return [String] the path of the file, if found.
|
@@ -60,8 +73,8 @@ module Splash
|
|
60
73
|
|
61
74
|
|
62
75
|
|
63
|
-
#
|
64
|
-
# @return [Bool]
|
76
|
+
# facility to verifyingif the active process run as root
|
77
|
+
# @return [Bool] status
|
65
78
|
def is_root?
|
66
79
|
case (Process.uid)
|
67
80
|
when 0
|
@@ -71,9 +84,9 @@ module Splash
|
|
71
84
|
end
|
72
85
|
end
|
73
86
|
|
74
|
-
#
|
87
|
+
# wrapping execution if process run as root
|
75
88
|
# @param [Symbol] method a method name th wrap
|
76
|
-
# @return [void]
|
89
|
+
# @return [void] return of wrapped method
|
77
90
|
def run_as_root(method, options = {})
|
78
91
|
unless is_root?
|
79
92
|
return {:case => :not_root, :more => "subcommands : #{method.to_s}"}
|
@@ -156,6 +169,7 @@ module Splash
|
|
156
169
|
}
|
157
170
|
if options[:foreground]
|
158
171
|
change_logger logger: :dual
|
172
|
+
Process.setproctitle options[:description] if options[:description]
|
159
173
|
return yield
|
160
174
|
end
|
161
175
|
fork do
|
@@ -180,15 +194,15 @@ module Splash
|
|
180
194
|
return 0
|
181
195
|
end
|
182
196
|
|
183
|
-
# @!group
|
197
|
+
# @!group facilities for file system commands
|
184
198
|
|
185
|
-
#
|
199
|
+
# facility for file installation
|
186
200
|
# @param [Hash] options
|
187
|
-
# @option options [String] :source
|
188
|
-
# @option options [String] :target
|
189
|
-
# @option options [String] :mode
|
190
|
-
# @option options [String] :owner
|
191
|
-
# @option options [String] :group
|
201
|
+
# @option options [String] :source file source path
|
202
|
+
# @option options [String] :target file target path
|
203
|
+
# @option options [String] :mode String for OCTAL rights like "644"
|
204
|
+
# @option options [String] :owner file owner for target
|
205
|
+
# @option options [String] :group file group for target
|
192
206
|
def install_file(options = {})
|
193
207
|
#begin
|
194
208
|
FileUtils::copy options[:source], options[:target] #unless File::exist? options[:target]
|
@@ -200,12 +214,12 @@ module Splash
|
|
200
214
|
#end
|
201
215
|
end
|
202
216
|
|
203
|
-
#
|
217
|
+
# facility for folder creation
|
204
218
|
# @param [Hash] options
|
205
|
-
# @option options [String] :path
|
206
|
-
# @option options [String] :mode
|
207
|
-
# @option options [String] :owner
|
208
|
-
# @option options [String] :group
|
219
|
+
# @option options [String] :path folder path (relative or absolute)
|
220
|
+
# @option options [String] :mode String for OCTAL rights like "644"
|
221
|
+
# @option options [String] :owner file owner for folder
|
222
|
+
# @option options [String] :group file group for folder
|
209
223
|
def make_folder(options = {})
|
210
224
|
begin
|
211
225
|
FileUtils::mkdir_p options[:path] unless File::exist? options[:path]
|
@@ -217,10 +231,10 @@ module Splash
|
|
217
231
|
end
|
218
232
|
end
|
219
233
|
|
220
|
-
#
|
234
|
+
# facility for Symbolic link
|
221
235
|
# @param [Hash] options
|
222
|
-
# @option options [String] :source
|
223
|
-
# @option options [String] :link
|
236
|
+
# @option options [String] :source path of the file
|
237
|
+
# @option options [String] :link path of the symlink
|
224
238
|
def make_link(options = {})
|
225
239
|
begin
|
226
240
|
FileUtils::rm options[:link] if (File::symlink? options[:link] and not File::exist? options[:link])
|
@@ -233,15 +247,15 @@ module Splash
|
|
233
247
|
# @!endgroup
|
234
248
|
|
235
249
|
|
236
|
-
#@!group
|
250
|
+
#@!group Verifiers for application : FS and TCP/IP services
|
237
251
|
|
238
|
-
#
|
252
|
+
# check folder
|
239
253
|
# @return [Array] of Symbol with error type : [:inexistant,:mode,:owner,:group]
|
240
254
|
# @param [Hash] options
|
241
|
-
# @option options [String] :path
|
242
|
-
# @option options [String] :mode
|
243
|
-
# @option options [String] :owner owner
|
244
|
-
# @option options [String] :group
|
255
|
+
# @option options [String] :path folder path (relative or absolute)
|
256
|
+
# @option options [String] :mode String for OCTAL rights like "644", optionnal
|
257
|
+
# @option options [String] :owner file owner for folder, optionnal
|
258
|
+
# @option options [String] :group file group for folder, optionnal
|
245
259
|
def verify_folder(options ={})
|
246
260
|
res = Array::new
|
247
261
|
return [:inexistant] unless File.directory?(options[:name])
|
@@ -259,21 +273,21 @@ module Splash
|
|
259
273
|
return res
|
260
274
|
end
|
261
275
|
|
262
|
-
#
|
263
|
-
# @return [
|
276
|
+
# check symlink
|
277
|
+
# @return [Boolean]
|
264
278
|
# @param [Hash] options
|
265
|
-
# @option options [String] :name path
|
279
|
+
# @option options [String] :name path of the link
|
266
280
|
def verify_link(options ={})
|
267
281
|
return File.file?(options[:name])
|
268
282
|
end
|
269
283
|
|
270
|
-
#
|
284
|
+
# check file
|
271
285
|
# @return [Array] of Symbol with error type : [:inexistant,:mode,:owner,:group]
|
272
286
|
# @param [Hash] options
|
273
|
-
# @option options [String] :name path
|
274
|
-
# @option options [String] :mode
|
275
|
-
# @option options [String] :owner owner
|
276
|
-
# @option options [String] :group
|
287
|
+
# @option options [String] :name path of file
|
288
|
+
# @option options [String] :mode String for OCTAL rights like "644", optionnal
|
289
|
+
# @option options [String] :owner file owner for file, optionnal
|
290
|
+
# @option options [String] :group file group for file, optionnal
|
277
291
|
def verify_file(options ={})
|
278
292
|
res = Array::new
|
279
293
|
return [:inexistant] unless File.file?(options[:name])
|
@@ -291,16 +305,25 @@ module Splash
|
|
291
305
|
return res
|
292
306
|
end
|
293
307
|
|
294
|
-
#
|
295
|
-
# @return [Bool]
|
308
|
+
# TCP/IP service checker
|
309
|
+
# @return [Bool] status
|
296
310
|
# @param [Hash] options
|
297
|
-
# @option options [String] :host
|
298
|
-
# @option options [String] :port
|
311
|
+
# @option options [String] :host hostname
|
312
|
+
# @option options [String] :port TCP port
|
313
|
+
# @option options [String] :url full URL, priority on :host and :port
|
299
314
|
def verify_service(options ={})
|
300
315
|
begin
|
316
|
+
if options[:url] then
|
317
|
+
uri = URI.parse(options[:url])
|
318
|
+
host = uri.host
|
319
|
+
port = uri.port
|
320
|
+
else
|
321
|
+
host = options[:host]
|
322
|
+
port = options[:port]
|
323
|
+
end
|
301
324
|
Timeout::timeout(1) do
|
302
325
|
begin
|
303
|
-
s = TCPSocket.new(
|
326
|
+
s = TCPSocket.new(host, port)
|
304
327
|
s.close
|
305
328
|
return true
|
306
329
|
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
|
@@ -314,6 +337,24 @@ module Splash
|
|
314
337
|
#!@endgroup
|
315
338
|
|
316
339
|
|
340
|
+
def format_response(data, format)
|
341
|
+
response = case format
|
342
|
+
when 'application/json' then JSON.pretty_generate(data)
|
343
|
+
when 'text/x-yaml' then data.to_yaml
|
344
|
+
else JSON.pretty_generate(data)
|
345
|
+
end
|
346
|
+
return response
|
347
|
+
end
|
348
|
+
|
349
|
+
def format_by_extensions(extension)
|
350
|
+
result = {
|
351
|
+
'json' => 'application/json',
|
352
|
+
'yaml' => 'text/x-yaml',
|
353
|
+
'yml' => 'text/x-yaml'
|
354
|
+
}
|
355
|
+
return result[extension]
|
356
|
+
end
|
357
|
+
|
317
358
|
|
318
359
|
end
|
319
360
|
end
|
data/lib/splash/loggers.rb
CHANGED
@@ -1,55 +1,78 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
|
3
|
-
|
3
|
+
# base Splash module
|
4
4
|
module Splash
|
5
|
+
|
6
|
+
# Loggers namespace
|
5
7
|
module Loggers
|
6
8
|
include Splash::Config
|
7
9
|
|
8
10
|
@@logger=nil
|
9
11
|
|
12
|
+
|
13
|
+
# factory for Loggers
|
14
|
+
# @param [Hash] options
|
15
|
+
# @option options [Symbol] :logger the name of the logger actually in [:cli, :daemon, :dual]
|
16
|
+
# @option options [Boolean] :force to force new instance creation (Logger is class variable)
|
17
|
+
# @return [SPlash::Loggers::<type>] type is Cli, Dual, Daemon, Web
|
10
18
|
def get_logger(options = {})
|
11
19
|
logger = (get_config.loggers[:list].include? options[:logger])? options[:logger].to_s : get_config.loggers[:default].to_s
|
12
20
|
aclass = "Splash::Loggers::#{logger.capitalize}"
|
13
|
-
|
21
|
+
begin
|
14
22
|
return @@logger = Kernel.const_get(aclass)::new if options[:force]
|
15
23
|
return @@logger ||= Kernel.const_get(aclass)::new
|
16
|
-
|
17
|
-
|
18
|
-
|
24
|
+
rescue
|
25
|
+
splash_exit case: :configuration_error, more: "Logger specified inexistant : #{logger}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# build a session number
|
30
|
+
# @return [String] Session number
|
31
|
+
def get_session
|
32
|
+
return "#{Time.now.to_i.to_s}#{rand(999)}"
|
19
33
|
end
|
20
34
|
|
21
35
|
|
36
|
+
# wrapper to change logger, call get_logger with force: true
|
37
|
+
# @option options [Symbol] :logger the name of the logger actually in [:cli, :daemon, :dual]
|
22
38
|
def change_logger(options = {})
|
39
|
+
level = get_logger.level
|
23
40
|
options[:force] = true
|
24
|
-
get_logger(options)
|
41
|
+
get_logger(options).level = level
|
25
42
|
end
|
26
43
|
|
44
|
+
LEVELS = [:debug, :warn, :info, :result, :fatal, :unknown]
|
45
|
+
ALIAS = {:flat => :info, :item => :info, :ok => :info, :ko => :info, :trigger => :info,
|
46
|
+
:schedule => :info, :arrow => :info, :send => :info, :call => :info,
|
47
|
+
:receive => :info, :error => :result, :success => :result }
|
27
48
|
|
28
|
-
|
49
|
+
# class template for loggers
|
29
50
|
class LoggerTemplate
|
30
51
|
include Splash::Config
|
31
52
|
|
32
|
-
|
33
|
-
ALIAS = {:flat => :info, :item => :info, :ok => :info, :ko => :info, :trigger => :info,
|
34
|
-
:schedule => :info, :arrow => :info, :send => :info,
|
35
|
-
:receive => :info, :error => :result, :success => :result }
|
53
|
+
|
36
54
|
LEVELS.each do |method|
|
37
|
-
define_method(method) do |message|
|
38
|
-
self.log({ :level => method, :message => message})
|
55
|
+
define_method(method) do |message,session = ''|
|
56
|
+
self.log({ :level => method, :message => message, :session => session})
|
39
57
|
end
|
40
58
|
end
|
41
59
|
ALIAS.keys.each do |method|
|
42
|
-
define_method(method) do |message|
|
43
|
-
self.log({ :level => method, :message => message})
|
60
|
+
define_method(method) do |message,session = ''|
|
61
|
+
self.log({ :level => method, :message => message, :session => session})
|
44
62
|
end
|
45
63
|
end
|
64
|
+
|
65
|
+
# constructor
|
46
66
|
def initialize
|
47
67
|
self.level = get_config.loggers[:level]
|
48
68
|
|
49
69
|
|
50
70
|
end
|
51
71
|
|
52
|
-
|
72
|
+
# abstract method for log wrapper
|
73
|
+
# @param [Hash] options
|
74
|
+
# @option options [Symbol] :level, a valid level in LEVELS or ALIAS
|
75
|
+
# @option options [String] :message text
|
53
76
|
def log(options)
|
54
77
|
level = (ALIAS.keys.include? options[:level])? ALIAS[options[:level]] : options[:level]
|
55
78
|
if @active_levels.include? level then
|
@@ -57,11 +80,15 @@ module Splash
|
|
57
80
|
end
|
58
81
|
end
|
59
82
|
|
60
|
-
|
83
|
+
# getter for the current level
|
84
|
+
# @return [Symbol] level
|
61
85
|
def level
|
62
86
|
return @active_levels.first
|
63
87
|
end
|
64
88
|
|
89
|
+
# virtual setter for level, set the current level
|
90
|
+
# @raise a badLevel in case of bad level
|
91
|
+
# @param [Symbol] level
|
65
92
|
def level=(level)
|
66
93
|
if LEVELS.include? level then
|
67
94
|
@active_levels = LEVELS.dup
|
@@ -73,6 +100,9 @@ module Splash
|
|
73
100
|
|
74
101
|
|
75
102
|
private
|
103
|
+
# mapper for symbol Symbol to String
|
104
|
+
# @param [Symbol] symbol
|
105
|
+
# @return [String] in upcase, exception :arrow give '=>', :flat give ''
|
76
106
|
def alt(symbol)
|
77
107
|
return "=>" if symbol == :arrow
|
78
108
|
return '' if symbol == :flat
|
@@ -81,6 +111,7 @@ module Splash
|
|
81
111
|
|
82
112
|
end
|
83
113
|
|
114
|
+
# badLevel Exception
|
84
115
|
class BadLevel < Exception; end
|
85
116
|
|
86
117
|
end
|