hotdog 0.0.5 → 0.0.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 87916b1a0fd595019d2e97021bd753cc95ea39ad
4
- data.tar.gz: 7f525b438a8ea90084f1a22b6277e863f32061be
3
+ metadata.gz: 7300d7cfb838327013fb49f3012e15a606cb7af4
4
+ data.tar.gz: 38666c025172a7873a7746027d64d6a388cd38b5
5
5
  SHA512:
6
- metadata.gz: dbc182c9a25be6d3116065930968379095b0248cbd4bc386f26f6c4860d9e3ed73322ae298e44192e5a16a3cbb5401ec08490fe2f7c717fa480d321e75011df6
7
- data.tar.gz: 3a8d9bc7ce921d4f9fbd2ffde40ae2468119a5d082c32551ae4aa494df969dca4757f00ac9fa25a6e339deb1ca8d2fe060ae8a174420b124fe569ba757e8f650
6
+ metadata.gz: c3e9cd9dff9174e95ba9343f4af840437b721b2ad3e0c46e4bf03ab3433f54d66ea2676c0600f5f30b5278315aa48d3943480a7f40b0e5b43737be7fbb70acb3
7
+ data.tar.gz: 86870002dea94e2cefc19ac9c3f028f394d001bab658bb875ee2bbb86a68e5feb2e47bb7cbd59e5465f5416da1f88f0336ce14799a122d9e3f600a1ccf6c6f20
data/README.md CHANGED
@@ -22,13 +22,30 @@ Or install it yourself as:
22
22
  $ gem install hotdog
23
23
  ```
24
24
 
25
+ Then, setup API key and application key of Datadog. The keys can be configured in environment variables or configuration file.
26
+
27
+ ```sh
28
+ export DATADOG_API_KEY="abcdefghijklmnopqrstuvwxyzabcdef"
29
+ export DATADOG_APPLICATION_KEY="abcdefghijklmnopqrstuvwxyzabcdefghijklmn"
30
+ ```
31
+
32
+ Or,
33
+
34
+ ```
35
+ $ mkdir ~/.hotdog
36
+ $ cat <<EOF
37
+ ---
38
+ api_key: abcdefghijklmnopqrstuvwxyzabcdef
39
+ application_key: abcdefghijklmnopqrstuvwxyzabcdefghijklmn
40
+ EOF
41
+ ```
42
+
25
43
  ## Usage
26
44
 
27
- Setup environment variables of `DATADOG_API_KEY` and `DATADOG_APPLICATION_KEY`.
28
- Then, create and initialize host information. This may take several minutes.
45
+ Initialize host information. This may take several minutes.
29
46
 
30
47
  ```sh
31
- $ hotdog init
48
+ $ hotdog update
32
49
  ```
33
50
 
34
51
  List all registered hosts.
@@ -4,21 +4,23 @@ require "logger"
4
4
  require "optparse"
5
5
  require "shellwords"
6
6
  require "sqlite3"
7
+ require "yaml"
7
8
  require "hotdog/commands"
8
9
  require "hotdog/formatters"
9
10
 
10
11
  module Hotdog
11
12
  class Application
12
13
  def initialize()
13
- @confdir = File.join(ENV["HOME"], ".hotdog")
14
+ @confdir = find_confdir(File.expand_path("."))
14
15
  @optparse = OptionParser.new
15
16
  @options = {
17
+ debug: false,
16
18
  environment: "default",
17
19
  minimum_expiry: 3600, # 1 hour
18
20
  random_expiry: 604800, # 7 days
19
21
  fixed_string: false,
20
22
  force: false,
21
- formatter: get_formatter("plain").new,
23
+ format: "plain",
22
24
  headers: false,
23
25
  listing: false,
24
26
  logger: Logger.new(STDERR),
@@ -28,6 +30,7 @@ module Hotdog
28
30
  print0: false,
29
31
  print1: true,
30
32
  tags: [],
33
+ verbose: false,
31
34
  }
32
35
  @options[:logger].level = Logger::INFO
33
36
  define_options
@@ -37,7 +40,10 @@ module Hotdog
37
40
  def main(argv=[])
38
41
  config = File.join(@confdir, "config.yml")
39
42
  if File.file?(config)
40
- @options = @options.merge(YAML.load(File.read(config)))
43
+ loaded = YAML.load(File.read(config))
44
+ if Hash === loaded
45
+ @options = @options.merge(Hash[loaded.map { |key, value| [Symbol === key ? key : key.to_s.to_sym, value] }])
46
+ end
41
47
  end
42
48
  args = @optparse.parse(argv)
43
49
 
@@ -49,6 +55,14 @@ module Hotdog
49
55
  raise("DATADOG_APPLICATION_KEY is not set")
50
56
  end
51
57
 
58
+ options[:formatter] = get_formatter(options[:format]).new
59
+
60
+ if options[:debug] or options[:verbose]
61
+ options[:logger].level = Logger::DEBUG
62
+ else
63
+ options[:logger].level = Logger::INFO
64
+ end
65
+
52
66
  sqlite = File.expand_path(File.join(@confdir, "#{options[:environment]}.db"))
53
67
  FileUtils.mkdir_p(File.dirname(sqlite))
54
68
  @db = SQLite3::Database.new(sqlite)
@@ -85,8 +99,11 @@ module Hotdog
85
99
  @optparse.on("-1", "Use newline as separator") do |v|
86
100
  options[:print1] = v
87
101
  end
102
+ @optparse.on("-B", "--blocking", "Enable blocking mode") do
103
+ options[:max_time] = -1
104
+ end
88
105
  @optparse.on("-d", "--[no-]debug", "Enable debug mode") do |v|
89
- options[:logger].level = v ? Logger::DEBUG : Logger::INFO
106
+ options[:debug] = v
90
107
  end
91
108
  @optparse.on("-E ENVIRONMENT", "--environment ENVIRONMENT", "Specify environment") do |environment|
92
109
  options[:environment] = environment
@@ -98,7 +115,7 @@ module Hotdog
98
115
  options[:force] = v
99
116
  end
100
117
  @optparse.on("-F FORMAT", "--format FORMAT", "Specify output format") do |format|
101
- options[:formatter] = get_formatter(format).new
118
+ options[:format] = format
102
119
  end
103
120
  @optparse.on("-h", "--[no-]headers", "Display headeres for each columns") do |v|
104
121
  options[:headers] = v
@@ -113,7 +130,7 @@ module Hotdog
113
130
  options[:max_time] = seconds
114
131
  end
115
132
  @optparse.on("-V", "--[no-]verbose", "Enable verbose mode") do |v|
116
- options[:logger].level = v ? Logger::DEBUG : Logger::INFO
133
+ options[:verbose] = v
117
134
  end
118
135
  end
119
136
 
@@ -162,6 +179,24 @@ module Hotdog
162
179
  end
163
180
  end
164
181
  end
182
+
183
+ def find_confdir(path)
184
+ if path == "/"
185
+ # default
186
+ if ENV.has_key?("HOTDOG_CONFDIR")
187
+ ENV["HOTDOG_CONFDIR"]
188
+ else
189
+ File.join(ENV["HOME"], ".hotdog")
190
+ end
191
+ else
192
+ confdir = File.join(path, ".hotdog")
193
+ if File.directory?(confdir)
194
+ confdir
195
+ else
196
+ find_confdir(File.dirname(path))
197
+ end
198
+ end
199
+ end
165
200
  end
166
201
  end
167
202
 
@@ -168,7 +168,7 @@ module Hotdog
168
168
  ( SELECT id FROM hosts WHERE LOWER(name) IN ( %s ) );
169
169
  EOS
170
170
 
171
- result["results"]["hosts"].each do |host_name|
171
+ result["results"]["hosts"].each_with_index do |host_name, i|
172
172
  @update_hosts_q2 ||= @db.prepare("INSERT OR IGNORE INTO hosts (name) VALUES (?);")
173
173
  logger.debug("update_hosts_q2(%s)" % [host_name.inspect])
174
174
  @update_hosts_q2.execute(host_name)
@@ -176,7 +176,8 @@ module Hotdog
176
176
 
177
177
  elapsed_time = Time.new - @started_at
178
178
  if 0 < options[:max_time] and options[:max_time] < elapsed_time
179
- logger.info("update host tags exceeded the maximum time (#{options[:max_time]} < #{elapsed_time}). will resume on next run.")
179
+ length = result["results"]["hosts"].length
180
+ logger.info("update_host_tags: exceeded maximum time (#{options[:max_time]} < #{elapsed_time}) after #{i+1}/#{length}. will resume on next run.")
180
181
  suspend_host_tags
181
182
  break
182
183
  end
@@ -205,7 +206,7 @@ module Hotdog
205
206
  logger.debug("update_tags_q2()")
206
207
  hosts = @update_tags_q2.execute(Time.new.to_i)
207
208
  end
208
- hosts.each do |host_id|
209
+ hosts.each_with_index do |host_id, i|
209
210
  @update_tags_q3 ||= @db.prepare("DELETE FROM hosts_tags WHERE host_id = ? AND hosts_tags.expires_at < ?;")
210
211
  logger.debug("update_tags_q3(%s, %s)" % [host_id.inspect, Time.new.to_i])
211
212
  @update_tags_q3.execute(host_id, Time.new.to_i)
@@ -214,7 +215,8 @@ module Hotdog
214
215
 
215
216
  elapsed_time = Time.new - @started_at
216
217
  if 0 < options[:max_time] and options[:max_time] < elapsed_time
217
- logger.info("update host tags exceeded the maximum time (#{options[:max_time]} < #{elapsed_time}). will resume on next run.")
218
+ length = hosts.length
219
+ logger.info("update_host_tags: exceeded maximum time (#{options[:max_time]} < #{elapsed_time}) after #{i+1}/#{length}. will resume on next run.")
218
220
  suspend_host_tags
219
221
  break
220
222
  end
@@ -4,6 +4,7 @@ module Hotdog
4
4
  module Commands
5
5
  class Gc < BaseCommand
6
6
  def run(args=[])
7
+ application.run_command("init")
7
8
  execute(<<-EOS)
8
9
  DELETE FROM hosts WHERE id NOT IN ( SELECT DISTINCT host_id FROM hosts_tags );
9
10
  EOS
@@ -4,6 +4,7 @@ module Hotdog
4
4
  module Commands
5
5
  class Hosts < BaseCommand
6
6
  def run(args=[])
7
+ application.run_command("init")
7
8
  update_hosts(@options.dup)
8
9
 
9
10
  if args.empty?
@@ -36,6 +37,7 @@ module Hotdog
36
37
  if 0 < result.length
37
38
  result, fields = get_hosts(result)
38
39
  STDOUT.print(format(result, fields: fields))
40
+ logger.info("found %d host(s)." % result.length)
39
41
  else
40
42
  STDERR.puts("no match found: #{args.join(" ")}")
41
43
  exit(1)
@@ -39,7 +39,6 @@ module Hotdog
39
39
  execute(<<-EOS)
40
40
  CREATE INDEX IF NOT EXISTS hosts_tags_host_id_expires_at ON hosts_tags ( host_id, expires_at );
41
41
  EOS
42
- application.run_command("update")
43
42
  end
44
43
  end
45
44
  end
@@ -7,6 +7,7 @@ module Hotdog
7
7
  module Commands
8
8
  class Search < BaseCommand
9
9
  def run(args=[])
10
+ application.run_command("init")
10
11
  expression = args.join(" ").strip
11
12
  if expression.empty?
12
13
  exit(1)
@@ -24,6 +25,7 @@ module Hotdog
24
25
  if 0 < result.length
25
26
  result, fields = get_hosts(result)
26
27
  STDOUT.print(format(result, fields: fields))
28
+ logger.info("found %d host(s)." % result.length)
27
29
  else
28
30
  STDERR.puts("no match found: #{args.join(" ")}")
29
31
  exit(1)
@@ -4,6 +4,7 @@ module Hotdog
4
4
  module Commands
5
5
  class Tags < BaseCommand
6
6
  def run(args=[])
7
+ application.run_command("init")
7
8
  update_tags(@options.dup)
8
9
  if 0 < tags.length
9
10
  fields = tags.map { |tag|
@@ -43,6 +44,7 @@ module Hotdog
43
44
  end
44
45
  if 0 < result.length
45
46
  STDOUT.print(format(result, fields: fields))
47
+ logger.info("found %d tag(s)." % result.length)
46
48
  end
47
49
  end
48
50
  end
@@ -4,7 +4,8 @@ module Hotdog
4
4
  module Commands
5
5
  class Update < BaseCommand
6
6
  def run(args=[])
7
- options[:force] = true unless ARGV.include?("--no-force")
7
+ application.run_command("init")
8
+ options[:max_time] = -1
8
9
  if 0 < args.length
9
10
  args.each do |host_name|
10
11
  update_host_tags(host_name, @options.dup)
@@ -6,6 +6,10 @@ module Hotdog
6
6
  module Formatters
7
7
  class Json < BaseFormatter
8
8
  def format(result, options={})
9
+ result = result.dup
10
+ if options[:headers] and options[:fields]
11
+ result.unshift(options[:fields])
12
+ end
9
13
  JSON.pretty_generate(result) + "\n"
10
14
  end
11
15
  end
@@ -6,6 +6,10 @@ module Hotdog
6
6
  module Formatters
7
7
  class Yaml < BaseFormatter
8
8
  def format(result, options={})
9
+ result = result.dup
10
+ if options[:headers] and options[:fields]
11
+ result.unshift(options[:fields])
12
+ end
9
13
  result.to_yaml
10
14
  end
11
15
  end
@@ -1,3 +1,3 @@
1
1
  module Hotdog
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
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.0.5
4
+ version: 0.0.6
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-02 00:00:00.000000000 Z
11
+ date: 2015-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler