fluent-exec 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -10,7 +10,7 @@ Install fluent-exec as a gem:
10
10
 
11
11
  ## Usage
12
12
 
13
- $ fluent-exec echo.test echo 'testing...'
13
+ $ fluent-exec -t echo.test echo 'testing...'
14
14
  testing...
15
15
 
16
16
  will log this record with tag `echo.test`:
@@ -23,6 +23,14 @@ will log this record with tag `echo.test`:
23
23
  "runtime": 0.004784
24
24
  }
25
25
 
26
+ Lookup `fluent-exec --help` for more info.
27
+
28
+ Usage: fluent-exec [options] [--] command...
29
+
30
+ -t, --tag TAG Tag to use (default fluent.exec)
31
+ -h, --host HOST Host to send (default localhost)
32
+ -p, --port PORT Port to send (default 24224)
33
+
26
34
  ## Contributing
27
35
 
28
36
  1. Fork it
@@ -1,5 +1,5 @@
1
1
  module Fluent
2
2
  module Exec
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
data/lib/fluent-exec.rb CHANGED
@@ -1,27 +1,57 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  require "fluent-exec/version"
3
3
  require "fluent-logger"
4
+ require "optparse"
4
5
  require "open3"
5
6
 
6
7
  module Fluent
7
8
  module Exec
9
+ DEFAULT_OPTS = {
10
+ :host => 'localhost',
11
+ :port => 24224,
12
+ :tag => 'fluent.exec'
13
+ }
14
+
8
15
  class CLI
9
- HOST = 'localhost'
10
- PORT = 24224
16
+ def initialize
17
+ @host = DEFAULT_OPTS[:host]
18
+ @port = DEFAULT_OPTS[:port]
19
+ @tag = DEFAULT_OPTS[:tag]
20
+ init_optp
21
+ end
22
+
23
+ def init_optp
24
+ @optp = OptionParser.new
25
+ @optp.banner = "Usage: fluent-exec [options] [--] command..."
26
+ @optp.separator ""
27
+ @optp.on('-t', '--tag TAG', "Tag to use (default #{DEFAULT_OPTS[:tag]})") {|v| @tag = v}
28
+ @optp.on('-h', '--host HOST', "Host to send (default #{DEFAULT_OPTS[:host]})") {|v| @host = v}
29
+ @optp.on('-p', '--port PORT', /^\d+$/, "Port to send (default #{DEFAULT_OPTS[:port]})") {|v| @port = v}
30
+ end
11
31
 
12
- def run(args)
13
- tag = args.shift
14
- cmd = args
32
+ def parse!(args)
33
+ begin
34
+ @cmd = @optp.order args
35
+ rescue => ex
36
+ $stderr.puts ex.to_s
37
+ $stderr.puts @optp.help
38
+ exit 1
39
+ end
40
+ end
41
+
42
+ def exec
15
43
  env = {}
16
44
  in_str = ''
17
45
  out_str = ''
18
46
  err_str = ''
19
47
 
20
48
  begin_time = Time.now
21
- sin, sout, serr, thr = Open3.popen3(env, *cmd)
49
+ sin, sout, serr, thr = Open3.popen3(env, *@cmd)
22
50
 
23
51
  sin_t = Thread.new do
24
- sin.write s while s = $stdin.read(1024)
52
+ while s = $stdin.read(1024)
53
+ sin.write s
54
+ end
25
55
  sin.close
26
56
  end
27
57
 
@@ -45,21 +75,27 @@ module Fluent
45
75
  sout_t.join
46
76
  serr_t.join
47
77
  es = status.exitstatus
78
+ {
79
+ :command => @cmd,
80
+ :exitstatus => es,
81
+ :stdout => out_str,
82
+ :stderr => err_str,
83
+ :runtime => end_time - begin_time,
84
+ }
85
+ end
86
+
87
+ def run(args)
88
+ parse! args
89
+ record = exec
48
90
  #コマンドの実行を優先?
49
91
  #fluentdサーバに接続できない場合はどうする?
50
92
  # failover (file|stdout)?
51
93
  begin
52
- log = Fluent::Logger::FluentLogger.new(nil, :host => HOST, :port => PORT)
53
- log.post tag, {
54
- :command => ARGV,
55
- :exitstatus => es,
56
- :stdout => out_str,
57
- :stderr => err_str,
58
- :runtime => end_time - begin_time,
59
- }
94
+ log = Fluent::Logger::FluentLogger.new(nil, :host => @host, :port => @port)
95
+ log.post @tag, record
60
96
  rescue => ex
61
97
  end
62
- exit es
98
+ exit record[:exitstatus]
63
99
  end
64
100
  end
65
101
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-exec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-17 00:00:00.000000000 Z
12
+ date: 2012-12-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: fluent-logger