This repository has been archived by the owner on Sep 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
134 lines (117 loc) · 2.98 KB
/
Rakefile
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
require 'bundler/gem_tasks'
require 'elasticsearch'
require 'yaml'
config = begin
YAML.load_file('config.yml')
rescue Errno::ENOENT => e
YAML.load_file('./spec/config.yml')
end
def get_pid(config)
begin
open(config['proxy']['pid'] || 'kusabana.pid').read
rescue Errno::ENOENT
nil
end
end
def alive?(pid)
begin
Process.kill(0, pid)
rescue Errno::ESRCH
nil
end
end
desc 'Start Kusabana'
task :start do
if config['proxy']['daemonize']
pid = get_pid(config)
if pid && alive?(pid.to_i)
fail('Kusabana is already running')
end
end
sh 'bundle exec bin/kusabana'
end
desc 'Stop Kusabana running as a daemon'
task :stop do
pid = get_pid(config)
if pid
if alive?(pid.to_i)
sh "kill #{pid}"
sleep 1
sh "kill -9 #{pid}" if alive?(pid.to_i)
end
end
end
desc 'Restart Kusabana running as a daemon'
task :restart => [:stop, :start]
desc 'Run RSpec'
task :test do
sh 'bundle exec rspec'
end
# Tasks for Docker
namespace :docker do
def get_container_id
begin
open('docker.id').read
rescue Errno::ENOENT
nil
end
end
def container_alive?(id)
alives = `docker ps -a -q`
alives.each_line do |alive|
return true if id.index(alive[0..-2]) == 0
end
return false
end
desc 'Build Docker image'
task :build do
sh 'docker build -t kusabana .'
end
desc 'Run Docker container'
task :run => 'docker:build' do
sh 'docker run -p 9292:9292 -t -i --rm kusabana'
end
desc 'Start Docker container'
task :start => 'docker:build' do
if last_id = get_container_id
fail('Container is already running') if container_alive?(last_id)
end
sh 'docker run -p 9292:9292 -d kusabana > docker.id'
end
desc 'Stop Docker container running as a daemon'
task :stop do
if last_id = get_container_id
sh "docker rm -f #{last_id}" if container_alive?(last_id)
else
fail('Container isn\'t running')
end
end
desc 'Restart Docker container running as a daemon'
task :restart => [:stop, :start]
desc 'Run RSpec within Docker container'
task :test => 'docker:build' do
sh 'docker run -p 9292:9292 --rm kusabana test'
end
end
namespace :dashboard do
desc 'Create dashboard into output elasticsearch'
task :create do
hosts = config['es']['output']['hosts'].map do |v|
v.inject({}) {|memo,(k,v)| memo[k.to_sym] = v; memo}
end
es = Elasticsearch::Client.new(hosts: hosts)
body = open('kibana-dashboard.json').read
es.index(index: 'kibana-int', type: 'dashboard', id: 'kusabana', body: body)
end
end
namespace :template do
desc 'Create index template into output elasticsearch'
task :create do
hosts = config['es']['output']['hosts'].map do |v|
v.inject({}) {|memo,(k,v)| memo[k.to_sym] = v; memo}
end
es = Elasticsearch::Client.new(hosts: hosts)
body = open('kusabana_log_template.json').read
es.index(index: '_template', type: 'kusabana-template-1', body: body)
end
end