lbrt 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +4 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +180 -0
  8. data/Rakefile +6 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +7 -0
  11. data/exe/lbrt +15 -0
  12. data/lbrt.gemspec +31 -0
  13. data/lib/lbrt.rb +46 -0
  14. data/lib/lbrt/alert.rb +91 -0
  15. data/lib/lbrt/alert/dsl.rb +9 -0
  16. data/lib/lbrt/alert/dsl/context.rb +32 -0
  17. data/lib/lbrt/alert/dsl/context/alert.rb +75 -0
  18. data/lib/lbrt/alert/dsl/context/alert/condition.rb +61 -0
  19. data/lib/lbrt/alert/dsl/converter.rb +113 -0
  20. data/lib/lbrt/alert/exporter.rb +38 -0
  21. data/lib/lbrt/cli.rb +2 -0
  22. data/lib/lbrt/cli/alert.rb +26 -0
  23. data/lib/lbrt/cli/app.rb +15 -0
  24. data/lib/lbrt/cli/service.rb +26 -0
  25. data/lib/lbrt/cli/space.rb +27 -0
  26. data/lib/lbrt/driver.rb +240 -0
  27. data/lib/lbrt/ext/string_ext.rb +25 -0
  28. data/lib/lbrt/logger.rb +32 -0
  29. data/lib/lbrt/service.rb +73 -0
  30. data/lib/lbrt/service/dsl.rb +9 -0
  31. data/lib/lbrt/service/dsl/context.rb +33 -0
  32. data/lib/lbrt/service/dsl/context/service.rb +32 -0
  33. data/lib/lbrt/service/dsl/converter.rb +38 -0
  34. data/lib/lbrt/service/exporter.rb +37 -0
  35. data/lib/lbrt/space.rb +126 -0
  36. data/lib/lbrt/space/dsl.rb +9 -0
  37. data/lib/lbrt/space/dsl/context.rb +29 -0
  38. data/lib/lbrt/space/dsl/context/space.rb +19 -0
  39. data/lib/lbrt/space/dsl/context/space/chart.rb +44 -0
  40. data/lib/lbrt/space/dsl/context/space/chart/stream.rb +48 -0
  41. data/lib/lbrt/space/dsl/converter.rb +107 -0
  42. data/lib/lbrt/space/exporter.rb +56 -0
  43. data/lib/lbrt/utils.rb +64 -0
  44. data/lib/lbrt/version.rb +3 -0
  45. metadata +202 -0
@@ -0,0 +1,19 @@
1
+ class Lbrt::Space::DSL::Context::Space
2
+ def initialize(name_or_id, &block)
3
+ @name_or_id = name_or_id
4
+ @result = {'charts' => {}}
5
+ instance_eval(&block)
6
+ end
7
+
8
+ attr_reader :result
9
+
10
+ private
11
+
12
+ def chart(chart_name_or_id, &block)
13
+ if @result[chart_name_or_id]
14
+ raise "Space `#{@name_or_id}` > Chart `#{chart_name_or_id}` is already defined"
15
+ end
16
+
17
+ @result['charts'][chart_name_or_id] = Lbrt::Space::DSL::Context::Space::Chart.new(@name_or_id, chart_name_or_id, &block).result
18
+ end
19
+ end
@@ -0,0 +1,44 @@
1
+ class Lbrt::Space::DSL::Context::Space::Chart
2
+ REQUIRED_ATTRIBUTES = %w(
3
+ type
4
+ )
5
+
6
+ def initialize(space_name_or_id, name_or_id, &block)
7
+ @space_name_or_id = space_name_or_id
8
+ @name_or_id = name_or_id
9
+ @result = {'streams' => []}
10
+ instance_eval(&block)
11
+ end
12
+
13
+ def result
14
+ REQUIRED_ATTRIBUTES.each do |name|
15
+ unless @result.has_key?(name)
16
+ raise "Space `#{@space_name_or_id}` > Chart `#{@name_or_id}` > `#{name}` is not defined"
17
+ end
18
+ end
19
+
20
+ @result
21
+ end
22
+
23
+ private
24
+
25
+ def type(value)
26
+ @result['type'] = value.to_s
27
+ end
28
+
29
+ def stream(&block)
30
+ @result['streams'] << Lbrt::Space::DSL::Context::Space::Chart::Stream.new(@space_name_or_id, @name_or_id, &block).result
31
+ end
32
+
33
+ def max(value)
34
+ @result['max'] = value.to_f
35
+ end
36
+
37
+ def min(value)
38
+ @result['min'] = value.to_f
39
+ end
40
+
41
+ def label(value)
42
+ @result['label'] = value.to_s
43
+ end
44
+ end
@@ -0,0 +1,48 @@
1
+ class Lbrt::Space::DSL::Context::Space::Chart::Stream
2
+ REQUIRED_ATTRIBUTES = %w(
3
+ metric
4
+ type
5
+ source
6
+ group_function
7
+ summary_function
8
+ )
9
+
10
+ def initialize(space_name_or_id, chart_name_or_id, &block)
11
+ @space_name_or_id = space_name_or_id
12
+ @chart_name_or_id = chart_name_or_id
13
+ @result = {}
14
+ instance_eval(&block)
15
+ end
16
+
17
+ def result
18
+ REQUIRED_ATTRIBUTES.each do |name|
19
+ unless @result.has_key?(name)
20
+ raise "Space `#{@space_name_or_id}` > Chart `#{@chart_name_or_id}` > Stream > `#{name}` is not defined"
21
+ end
22
+ end
23
+
24
+ @result
25
+ end
26
+
27
+ private
28
+
29
+ def metric(value)
30
+ @result['metric'] = value.to_s
31
+ end
32
+
33
+ def type(value)
34
+ @result['type'] = value.to_s
35
+ end
36
+
37
+ def source(value)
38
+ @result['source'] = value.to_s
39
+ end
40
+
41
+ def group_function(value)
42
+ @result['group_function'] = value.to_s
43
+ end
44
+
45
+ def summary_function(value)
46
+ @result['summary_function'] = value.to_s
47
+ end
48
+ end
@@ -0,0 +1,107 @@
1
+ class Lbrt::Space::DSL::Converter
2
+ def self.convert(exported, options = {})
3
+ self.new(exported, options).convert
4
+ end
5
+
6
+ def initialize(exported, options = {})
7
+ @exported = exported
8
+ @options = options
9
+ end
10
+
11
+ def convert
12
+ output_spaces(@exported)
13
+ end
14
+
15
+ private
16
+
17
+
18
+ def output_spaces(space_by_name_or_id)
19
+ spaces = []
20
+
21
+ space_by_name_or_id.sort_by {|k, v| k.to_s }.map do |name_or_id, attrs|
22
+ next unless target_matched?(name_or_id)
23
+ spaces << output_space(name_or_id, attrs)
24
+ end
25
+
26
+ spaces.join("\n")
27
+ end
28
+
29
+ def output_space(name_or_id, attrs)
30
+ chart_by_name_or_id = attrs.fetch('charts')
31
+
32
+ <<-EOS
33
+ space #{name_or_id.inspect} do
34
+ #{output_charts(chart_by_name_or_id)}
35
+ end
36
+ EOS
37
+ end
38
+
39
+ def output_charts(chart_by_name_or_id)
40
+ if chart_by_name_or_id.empty?
41
+ '# no chart'
42
+ else
43
+ # Don't sort!
44
+ chart_by_name_or_id.map {|name_or_id, attrs|
45
+ output_chart(name_or_id, attrs)
46
+ }.join("\n").strip
47
+ end
48
+ end
49
+
50
+ def output_chart(name_or_id, attrs)
51
+ type = attrs.fetch('type')
52
+ streams = attrs.fetch('streams')
53
+
54
+ out = <<-EOS
55
+ chart #{name_or_id.inspect} do
56
+ type #{type.inspect}
57
+ #{output_streams(streams)}
58
+ EOS
59
+
60
+ %w(max min label).each do |key|
61
+ next unless attrs.has_key?(key)
62
+ value = attrs[key]
63
+
64
+ out << <<-EOS
65
+ #{key} #{value.inspect}
66
+ EOS
67
+ end
68
+
69
+ out << <<-EOS
70
+ end
71
+ EOS
72
+ end
73
+
74
+ def output_streams(streams)
75
+ if streams.empty?
76
+ '# no stream'
77
+ else
78
+ streams.map {|i| output_stream(i) }.join.strip
79
+ end
80
+ end
81
+
82
+ def output_stream(stream)
83
+ metric = stream.fetch('metric')
84
+ type = stream.fetch('type')
85
+ source = stream.fetch('source')
86
+ group_function = stream.fetch('group_function')
87
+ summary_function = stream.fetch('summary_function')
88
+
89
+ <<-EOS
90
+ stream do
91
+ metric #{metric.inspect}
92
+ type #{type.inspect}
93
+ source #{source.inspect}
94
+ group_function #{group_function.inspect}
95
+ summary_function #{summary_function.inspect}
96
+ end
97
+ EOS
98
+ end
99
+
100
+ def target_matched?(str)
101
+ if @options[:target]
102
+ str =~ @options[:target]
103
+ else
104
+ true
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,56 @@
1
+ class Lbrt::Space::Exporter
2
+ DEFAULT_CONCURRENCY = 32
3
+
4
+ class << self
5
+ def export(client, options = {})
6
+ self.new(client, options).export
7
+ end
8
+ end # of class methods
9
+
10
+ def initialize(client, options = {})
11
+ @client = client
12
+ @options = options
13
+ end
14
+
15
+ def export
16
+ spaces = @client.spaces.get
17
+ normalize_spaces(spaces)
18
+ end
19
+
20
+ private
21
+
22
+ def normalize_spaces(spaces)
23
+ space_by_name_or_id = {}
24
+ concurrency = @options[:export_concurrency] || DEFAULT_CONCURRENCY
25
+
26
+ Parallel.each(spaces, :in_threads => concurrency) do |spc|
27
+ space_id = spc.fetch('id')
28
+ name_or_id = spc.fetch('name') || space_id
29
+ charts = @client.spaces(space_id).charts.get
30
+
31
+ if space_by_name_or_id[name_or_id]
32
+ raise "Duplicate space name exists: #{name}"
33
+ end
34
+
35
+ space_by_name_or_id[name_or_id] = {
36
+ 'id' => space_id,
37
+ 'charts' => normalize_charts(charts),
38
+ }
39
+ end
40
+
41
+ space_by_name_or_id
42
+ end
43
+
44
+ def normalize_charts(charts)
45
+ chart_by_name_or_id = {}
46
+
47
+ charts.each do |chrt|
48
+ name_or_id = chrt.delete('name')
49
+ name_or_id = chrt['id'] if name_or_id.empty?
50
+ chrt.delete('space_id')
51
+ chart_by_name_or_id[name_or_id] = chrt
52
+ end
53
+
54
+ chart_by_name_or_id
55
+ end
56
+ end
@@ -0,0 +1,64 @@
1
+ class Lbrt::Utils
2
+ class << self
3
+ def unbrace(str)
4
+ str.sub(/\A\s*\{/, '').sub(/\}\s*\z/, '').strip
5
+ end
6
+
7
+ def matched?(str, target)
8
+ str = str.to_s
9
+
10
+ if target
11
+ str =~ target
12
+ else
13
+ true
14
+ end
15
+ end
16
+ end # of class methods
17
+
18
+ module ContextHelper
19
+ def require(file)
20
+ file = (file =~ %r|\A/|) ? file : File.expand_path(File.join(File.dirname(@path), file))
21
+
22
+ if File.exist?(file)
23
+ instance_eval(File.read(file), file)
24
+ elsif File.exist?(file + '.rb')
25
+ instance_eval(File.read(file + '.rb'), file + '.rb')
26
+ else
27
+ Kernel.require(file)
28
+ end
29
+ end
30
+ end
31
+
32
+ module CLIHelper
33
+ REGEXP_OPTIONS = [
34
+ :target
35
+ ]
36
+
37
+ def client(klass)
38
+ librato = Librato::Client.new(
39
+ :user => options.delete(:user),
40
+ :token => options.delete(:token),
41
+ :debug => options[:debug]
42
+ )
43
+
44
+ String.colorize = options[:color]
45
+
46
+
47
+ options.keys.each do |key|
48
+ if key.to_s =~ /-/
49
+ value = options.delete(key)
50
+ key = key.to_s.gsub('-', '_').to_sym
51
+ options[key] = value
52
+ end
53
+ end
54
+
55
+ REGEXP_OPTIONS.each do |key|
56
+ if options[key]
57
+ options[key] = Regexp.new(options[key])
58
+ end
59
+ end
60
+
61
+ klass.new(librato, options)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,3 @@
1
+ module Lbrt
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,202 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lbrt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Genki Sugawara
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: diffy
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: librato-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: parallel
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: term-ansicolor
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: thor
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: 3.0.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: 3.0.0
125
+ description: A tool to manage Librato. It defines the state of Librato using DSL,
126
+ and updates Librato according to DSL.
127
+ email:
128
+ - sgwr_dts@yahoo.co.jp
129
+ executables:
130
+ - lbrt
131
+ extensions: []
132
+ extra_rdoc_files: []
133
+ files:
134
+ - ".gitignore"
135
+ - ".rspec"
136
+ - ".travis.yml"
137
+ - Gemfile
138
+ - LICENSE.txt
139
+ - README.md
140
+ - Rakefile
141
+ - bin/console
142
+ - bin/setup
143
+ - exe/lbrt
144
+ - lbrt.gemspec
145
+ - lib/lbrt.rb
146
+ - lib/lbrt/alert.rb
147
+ - lib/lbrt/alert/dsl.rb
148
+ - lib/lbrt/alert/dsl/context.rb
149
+ - lib/lbrt/alert/dsl/context/alert.rb
150
+ - lib/lbrt/alert/dsl/context/alert/condition.rb
151
+ - lib/lbrt/alert/dsl/converter.rb
152
+ - lib/lbrt/alert/exporter.rb
153
+ - lib/lbrt/cli.rb
154
+ - lib/lbrt/cli/alert.rb
155
+ - lib/lbrt/cli/app.rb
156
+ - lib/lbrt/cli/service.rb
157
+ - lib/lbrt/cli/space.rb
158
+ - lib/lbrt/driver.rb
159
+ - lib/lbrt/ext/string_ext.rb
160
+ - lib/lbrt/logger.rb
161
+ - lib/lbrt/service.rb
162
+ - lib/lbrt/service/dsl.rb
163
+ - lib/lbrt/service/dsl/context.rb
164
+ - lib/lbrt/service/dsl/context/service.rb
165
+ - lib/lbrt/service/dsl/converter.rb
166
+ - lib/lbrt/service/exporter.rb
167
+ - lib/lbrt/space.rb
168
+ - lib/lbrt/space/dsl.rb
169
+ - lib/lbrt/space/dsl/context.rb
170
+ - lib/lbrt/space/dsl/context/space.rb
171
+ - lib/lbrt/space/dsl/context/space/chart.rb
172
+ - lib/lbrt/space/dsl/context/space/chart/stream.rb
173
+ - lib/lbrt/space/dsl/converter.rb
174
+ - lib/lbrt/space/exporter.rb
175
+ - lib/lbrt/utils.rb
176
+ - lib/lbrt/version.rb
177
+ homepage: https://github.com/winebarrel/lbrt
178
+ licenses:
179
+ - MIT
180
+ metadata: {}
181
+ post_install_message:
182
+ rdoc_options: []
183
+ require_paths:
184
+ - lib
185
+ required_ruby_version: !ruby/object:Gem::Requirement
186
+ requirements:
187
+ - - ">="
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ required_rubygems_version: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ requirements: []
196
+ rubyforge_project:
197
+ rubygems_version: 2.4.5
198
+ signing_key:
199
+ specification_version: 4
200
+ summary: A tool to manage Librato. It defines the state of Librato using DSL, and
201
+ updates Librato according to DSL.
202
+ test_files: []