small-ops 0.0.23 → 0.0.25
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.
- data/bin/docker2etcd +14 -0
- data/bin/log2stash +73 -0
- data/lib/small-setup.rb +8 -0
- metadata +3 -1
data/bin/docker2etcd
CHANGED
|
@@ -16,8 +16,22 @@ OptionParser.new do |opts|
|
|
|
16
16
|
opts.on("-v","--verbose","verboser") do |v|
|
|
17
17
|
@options[:verbose]=v
|
|
18
18
|
end
|
|
19
|
+
opts.on("-c","--clear","clear etcd of previous data (only containers data)") do |c|
|
|
20
|
+
@options[:clear]=c
|
|
21
|
+
end
|
|
19
22
|
end.parse!
|
|
20
23
|
|
|
24
|
+
if @options[:clear] then
|
|
25
|
+
data = nodes2obj( http_get("#{@options[:etcd]}/v2/keys/?recursive=true")["node"]["nodes"] ,"/")
|
|
26
|
+
data.keys.each {|k|
|
|
27
|
+
n = data[k]
|
|
28
|
+
if n.has_key?("host") && n.has_key?("name") && n.has_key?("port") then
|
|
29
|
+
puts "del #{n["name"]}"
|
|
30
|
+
http_delete("#{@options[:etcd]}/v2/keys/#{n["name"]}?dir=true&recursive=true")
|
|
31
|
+
end
|
|
32
|
+
}
|
|
33
|
+
end
|
|
34
|
+
|
|
21
35
|
`docker ps | tail -n+2 | awk '{ print $1 }'`.split("\n")
|
|
22
36
|
.map { |id| `docker inspect #{id}` }
|
|
23
37
|
.map { |inspect| JSON.parse(inspect)[0]}
|
data/bin/log2stash
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require 'small-setup'
|
|
4
|
+
require 'date'
|
|
5
|
+
require 'socket'
|
|
6
|
+
|
|
7
|
+
OptionParser.new do |opts|
|
|
8
|
+
opts.banner = "Usage: command [options]"
|
|
9
|
+
opts.on("-h", "--host host", "Set host IP/domain") do |h|
|
|
10
|
+
@options[:host] = h
|
|
11
|
+
end
|
|
12
|
+
opts.on("-e", "--etcd host:port", "Set etcd host and port") do |e|
|
|
13
|
+
@options[:etcd] = e
|
|
14
|
+
end
|
|
15
|
+
opts.on("-f","--foreground","Keep running on foregroud. Default is run once and exit.") do |f|
|
|
16
|
+
@options[:foreground]=f
|
|
17
|
+
end
|
|
18
|
+
opts.on("-i","--input file","Input file") do |i|
|
|
19
|
+
@options[:input]=i
|
|
20
|
+
end
|
|
21
|
+
opts.on("-l","--logstash host:port", "Logstash tcp HOST:PORT") do |l|
|
|
22
|
+
@options[:logstash] = l
|
|
23
|
+
end
|
|
24
|
+
opts.on("-m","--message message","The message to send") do |m|
|
|
25
|
+
@options[:message]=m
|
|
26
|
+
end
|
|
27
|
+
opts.on("-n","--name name","The program log name") do |n|
|
|
28
|
+
@options[:name]=n
|
|
29
|
+
end
|
|
30
|
+
opts.on("-p","--priority priority","The priority (as syslog) of log") do |p|
|
|
31
|
+
@options[:priority]=p
|
|
32
|
+
end
|
|
33
|
+
end.parse!
|
|
34
|
+
|
|
35
|
+
if @options[:logstash] then
|
|
36
|
+
elsif ENV["LOGSTASH_PORT_9514_TCP_ADDR"] then
|
|
37
|
+
@options[:logstash] = "#{ENV["LOGSTASH_PORT_9514_TCP_ADDR"]}:#{ENV["LOGSTASH_PORT_9514_TCP_PORT"]}"
|
|
38
|
+
elsif ENV["logstash"] then
|
|
39
|
+
@options[:logstash] = ENV['logstash']
|
|
40
|
+
else
|
|
41
|
+
begin
|
|
42
|
+
data = nodes2obj( http_get("#{@options[:etcd]}/v2/keys/?recursive=true")["node"]["nodes"] ,"/")
|
|
43
|
+
@options[:logstash] = "#{data["logstash"]["networksettings"]["ipaddress"]}:9514"
|
|
44
|
+
rescue
|
|
45
|
+
@options[:logstash] = ENV['logstash'] || "#{@options[:host]}:9514"
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def log(m)
|
|
50
|
+
now = DateTime.now.strftime('%b %d %H:%M:%S')
|
|
51
|
+
host = `hostname`.gsub("\n","")
|
|
52
|
+
name = @options[:name] || "default_log"
|
|
53
|
+
priority = @options[:priorty] || '4'
|
|
54
|
+
"<#{priority}>#{now} #{host} #{name}: #{m}"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
puts "Logging to #{@options[:logstash]}"
|
|
58
|
+
s = TCPSocket.new @options[:logstash].split(":").first, @options[:logstash].split(":").last
|
|
59
|
+
|
|
60
|
+
if @options[:foreground] && @options[:input] then
|
|
61
|
+
`tail -f #{@options[:input]} | bin/log2stash --logstash #{@options[:logstash]}`
|
|
62
|
+
elsif @options[:input] then
|
|
63
|
+
`cat #{@options[:input]} | bin/log2stash --logstash #{@options[:logstash]}`
|
|
64
|
+
elsif @options[:message] then
|
|
65
|
+
s.puts log(@options[:message])
|
|
66
|
+
else
|
|
67
|
+
ARGF.each_line {|line|
|
|
68
|
+
s.puts log(line)
|
|
69
|
+
}
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
s.close
|
|
73
|
+
|
data/lib/small-setup.rb
CHANGED
|
@@ -41,6 +41,14 @@ def http_put(uri,doc)
|
|
|
41
41
|
end
|
|
42
42
|
end
|
|
43
43
|
|
|
44
|
+
def http_delete(uri)
|
|
45
|
+
uri = URI.parse(uri)
|
|
46
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
47
|
+
request = Net::HTTP::Delete.new(uri.request_uri)
|
|
48
|
+
response = http.request(request)
|
|
49
|
+
JSON.parse(response.body)
|
|
50
|
+
end
|
|
51
|
+
|
|
44
52
|
def flatten(obj,sub)
|
|
45
53
|
flat={}
|
|
46
54
|
sub=sub.gsub("-","_").downcase
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: small-ops
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.25
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -18,6 +18,7 @@ executables:
|
|
|
18
18
|
- etcd2env
|
|
19
19
|
- etcd2conf
|
|
20
20
|
- fige
|
|
21
|
+
- log2stash
|
|
21
22
|
extensions: []
|
|
22
23
|
extra_rdoc_files: []
|
|
23
24
|
files:
|
|
@@ -26,6 +27,7 @@ files:
|
|
|
26
27
|
- bin/etcd2env
|
|
27
28
|
- bin/etcd2conf
|
|
28
29
|
- bin/fige
|
|
30
|
+
- bin/log2stash
|
|
29
31
|
homepage: https://github.com/
|
|
30
32
|
licenses:
|
|
31
33
|
- MIT
|