hotdog 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d5814468d246a0a23d4258b4a371ebf2b5f59dc0
4
- data.tar.gz: 3911eafd56a1e4b63d379cb3a252af932daf2b3d
3
+ metadata.gz: 4a8a2587c731d0636d060338297376ef857135d4
4
+ data.tar.gz: 4ec81a26bfa37aa4a2352a3879526d86aa023a60
5
5
  SHA512:
6
- metadata.gz: 54d974364fc3fcf36157aa8caabdf88c86b77c2fb7e6ab7e91c7e911a31ea3d76f40fe11ae1d18b123a196a1070c0ed43a84b6f87c8e97d4f7360725db00fc89
7
- data.tar.gz: a30026d51aa4fe2134ad8d3b392f3c7b4664c15abbbe90f6e384251cc7fbd79b49b8ed6b95b413929231b4208f0200b9923f92fee930177c6e4c701d4ab6e7cd
6
+ metadata.gz: c718fa212bd63300938cbfb78b624b24b4df25da7ee351f586f51e4b357d1fc2bf5ff8f7b2144b4d531804e61bb13c5cd4f20dc179b38b6bc5933bc296ba8975
7
+ data.tar.gz: 6cfaa906fc298e47913400612584eba9d94877ad7addc9f52ac5768229864ece0699387cff42d0022207e043fcb78a86e309d7001785c913c42fa6dd5a6f3fb3
@@ -29,11 +29,13 @@ module Hotdog
29
29
  print0: false,
30
30
  print1: true,
31
31
  tags: [],
32
+ display_search_tags: false,
32
33
  verbose: false,
33
34
  }
34
35
  define_options
35
36
  end
36
37
  attr_reader :options
38
+ attr_reader :optparse
37
39
 
38
40
  def main(argv=[])
39
41
  config = File.join(options[:confdir], "config.yml")
@@ -43,7 +45,7 @@ module Hotdog
43
45
  @options = @options.merge(Hash[loaded.map { |key, value| [Symbol === key ? key : key.to_s.to_sym, value] }])
44
46
  end
45
47
  end
46
- args = @optparse.parse(argv)
48
+ args = @optparse.order(argv)
47
49
 
48
50
  unless options[:api_key]
49
51
  raise("DATADOG_API_KEY is not set")
@@ -63,7 +65,7 @@ module Hotdog
63
65
 
64
66
  begin
65
67
  command = ( args.shift || "help" )
66
- get_command(command).new(@options.dup).tap do |cmd|
68
+ get_command(command).new(self).tap do |cmd|
67
69
  cmd.run(args)
68
70
  end
69
71
  rescue Errno::EPIPE
@@ -106,6 +108,9 @@ module Hotdog
106
108
  @optparse.on("-a TAG", "-t TAG", "--tag TAG", "Use specified tag name/value") do |tag|
107
109
  options[:tags] += [tag]
108
110
  end
111
+ @optparse.on("-x", "--display-search-tags", "Show tags used in search expression") do |v|
112
+ options[:display_search_tags] = v
113
+ end
109
114
  @optparse.on("-V", "--[no-]verbose", "Enable verbose mode") do |v|
110
115
  options[:verbose] = v
111
116
  end
@@ -6,14 +6,25 @@ module Hotdog
6
6
  module Commands
7
7
  class Down < BaseCommand
8
8
  def run(args=[])
9
+ if args.index("--start").nil?
10
+ start = Time.new
11
+ else
12
+ start = Time.parse(args[args.index("--start") + 1])
13
+ args.slice!(args.index("--start"), 2)
14
+ end
15
+ if args.index("--downtime").nil?
16
+ downtime = 86400
17
+ else
18
+ downtime = args[args.index("--downtime") + 1].to_i
19
+ args.slice!(args.index("--downtime"), 2)
20
+ end
21
+
9
22
  args.each do |arg|
10
23
  if arg.index(":").nil?
11
24
  scope = "host:#{arg}"
12
25
  else
13
26
  scope = arg
14
27
  end
15
- start = Time.new
16
- downtime = 86400 # TODO: make configurable
17
28
  code, schedule = @dog.schedule_downtime(scope, :start => start.to_i, :end => (start+downtime).to_i)
18
29
  logger.debug("dog.schedule_donwtime(%s, :start => %s, :end => %s) #==> [%s, %s]" % [scope.inspect, start.to_i, (start+downtime).to_i, code.inspect, schedule.inspect])
19
30
  if code.to_i / 100 != 2
@@ -24,7 +35,7 @@ module Hotdog
24
35
  # Remove persistent.db to schedule update on next invocation
25
36
  if not @db.nil?
26
37
  @db.close
27
- FileUtils.rm_f(File.join(confdir, PERSISTENT_DB))
38
+ FileUtils.rm_f(File.join(@options[:confdir], PERSISTENT_DB))
28
39
  end
29
40
  end
30
41
  end
@@ -4,6 +4,7 @@ module Hotdog
4
4
  module Commands
5
5
  class Hosts < BaseCommand
6
6
  def run(args=[])
7
+ args = optparse.parse(args)
7
8
  if args.empty?
8
9
  result = execute("SELECT DISTINCT host_id FROM hosts_tags").to_a.reduce(:+)
9
10
  else
@@ -7,6 +7,7 @@ module Hotdog
7
7
  module Commands
8
8
  class Search < BaseCommand
9
9
  def run(args=[])
10
+ args = optparse.parse(args)
10
11
  expression = args.join(" ").strip
11
12
  if expression.empty?
12
13
  exit(1)
@@ -19,9 +20,19 @@ module Hotdog
19
20
  exit(1)
20
21
  end
21
22
 
23
+ drilldown = ->(n){
24
+ case
25
+ when n[:left] && n[:right] then drilldown.(n[:left]) + drilldown.(n[:right])
26
+ when n[:expression] then drilldown.(n[:expression])
27
+ when n[:identifier] then [n[:identifier]]
28
+ else []
29
+ end
30
+ }
31
+ identifiers = drilldown.call(node).map(&:to_s)
32
+
22
33
  result = evaluate(node, self).sort
23
34
  if 0 < result.length
24
- result, fields = get_hosts(result)
35
+ result, fields = get_hosts(result, @options[:display_search_tags] ? identifiers : [])
25
36
  STDOUT.print(format(result, fields: fields))
26
37
  logger.info("found %d host(s)." % result.length)
27
38
  else
@@ -59,7 +70,7 @@ module Hotdog
59
70
  rule(:unary_expression) {
60
71
  ( spacing.maybe >> str('!').as(:unary_op) >> atom.as(:expression) \
61
72
  | spacing.maybe >> str('~').as(:unary_op) >> atom.as(:expression) \
62
- | spacing.maybe >> str('not').as(:unary_op) >> atom.as(:expression) \
73
+ | spacing.maybe >> (match('[Nn]') >> match('[Oo]') >> match('[Tt]')).as(:unary_op) >> atom.as(:expression) \
63
74
  )
64
75
  }
65
76
  rule(:term) {
@@ -157,6 +168,8 @@ module Hotdog
157
168
  end
158
169
 
159
170
  class BinaryExpressionNode < ExpressionNode
171
+ attr_reader :left, :right
172
+
160
173
  def initialize(op, left, right)
161
174
  @op = op
162
175
  @left = left
@@ -183,6 +196,8 @@ module Hotdog
183
196
  end
184
197
 
185
198
  class UnaryExpressionNode < ExpressionNode
199
+ attr_reader :expression
200
+
186
201
  def initialize(op, expression)
187
202
  @op = op
188
203
  @expression = expression
@@ -4,6 +4,7 @@ module Hotdog
4
4
  module Commands
5
5
  class Tags < BaseCommand
6
6
  def run(args=[])
7
+ args = optparse.parse(args)
7
8
  if 0 < tags.length
8
9
  fields = tags.map { |tag|
9
10
  tag_name, tag_value = tag.split(":", 2)
@@ -32,7 +32,7 @@ module Hotdog
32
32
  # Remove persistent.db to schedule update on next invocation
33
33
  if not @db.nil?
34
34
  @db.close
35
- FileUtils.rm_f(File.join(confdir, PERSISTENT_DB))
35
+ FileUtils.rm_f(File.join(@options[:confdir], PERSISTENT_DB))
36
36
  end
37
37
  end
38
38
  end
@@ -10,27 +10,14 @@ module Hotdog
10
10
  class BaseCommand
11
11
  PERSISTENT_DB = "persistent.db"
12
12
 
13
- def initialize(options={})
14
- @application = options[:application]
15
- @confdir = options[:confdir]
16
- @expiry = options[:expiry]
17
- @fixed_string = options[:fixed_string]
18
- @force = options[:force]
19
- @formatter = options[:formatter]
20
- @listing = options[:listing]
21
- @logger = options[:logger]
22
- @tags = options[:tags]
23
- @options = options
13
+ def initialize(application)
14
+ @application = application
15
+ @logger = application.options[:logger]
16
+ @options = application.options
24
17
  @dog = Dogapi::Client.new(options[:api_key], options[:application_key])
25
18
  end
26
19
  attr_reader :application
27
- attr_reader :confdir
28
- attr_reader :expiry
29
- attr_reader :force
30
- attr_reader :formatter
31
- attr_reader :listing
32
20
  attr_reader :logger
33
- attr_reader :tags
34
21
  attr_reader :options
35
22
 
36
23
  def run(args=[])
@@ -48,12 +35,16 @@ module Hotdog
48
35
  end
49
36
 
50
37
  def fixed_string?()
51
- @fixed_string
38
+ @options[:fixed_string]
52
39
  end
53
40
 
54
41
  private
55
42
  def format(result, options={})
56
- @formatter.format(result, @options.merge(options))
43
+ @options[:formatter].format(result, @options.merge(options))
44
+ end
45
+
46
+ def optparse()
47
+ @application.optparse
57
48
  end
58
49
 
59
50
  def glob?(s)
@@ -65,9 +56,14 @@ module Hotdog
65
56
  not host_id.nil?
66
57
  end
67
58
 
68
- def get_hosts(hosts=[])
59
+ def get_hosts(hosts=[], identifiers=[])
69
60
  update_db
70
- if 0 < tags.length
61
+ if 0 < @options[:tags].length || 0 < identifiers.length
62
+ tags = if 0 < @options[:tags].length
63
+ @options[:tags] + identifiers
64
+ else
65
+ ["host"] + identifiers
66
+ end
71
67
  result = hosts.map { |host_id|
72
68
  tags.map { |tag|
73
69
  tag_name, tag_value = tag.split(":", 2)
@@ -85,7 +81,7 @@ module Hotdog
85
81
  }
86
82
  fields = tags
87
83
  else
88
- if @listing
84
+ if @options[:listing]
89
85
  fields = []
90
86
  hosts = execute("SELECT id, name FROM hosts WHERE id IN (%s)" % hosts.map { "?" }.join(", "), hosts)
91
87
  result = hosts.map { |host_id, host_name|
@@ -108,10 +104,10 @@ module Hotdog
108
104
 
109
105
  def update_db(options={})
110
106
  if @db.nil?
111
- FileUtils.mkdir_p(confdir)
112
- persistent = File.join(confdir, PERSISTENT_DB)
107
+ FileUtils.mkdir_p(@options[:confdir])
108
+ persistent = File.join(@options[:confdir], PERSISTENT_DB)
113
109
 
114
- if not @force and File.exist?(persistent) and Time.new < File.mtime(persistent) + expiry
110
+ if not @options[:force] and File.exist?(persistent) and Time.new < File.mtime(persistent) + @options[:expiry]
115
111
  begin
116
112
  persistent_db = SQLite3::Database.new(persistent)
117
113
  persistent_db.execute("SELECT id, name FROM hosts LIMIT 1")
@@ -1,3 +1,3 @@
1
1
  module Hotdog
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hotdog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yamashita Yuu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-30 00:00:00.000000000 Z
11
+ date: 2015-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -132,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
132
132
  version: '0'
133
133
  requirements: []
134
134
  rubyforge_project:
135
- rubygems_version: 2.4.4
135
+ rubygems_version: 2.2.2
136
136
  signing_key:
137
137
  specification_version: 4
138
138
  summary: Yet another command-line tool for Datadog