small-ops 0.0.1.beta

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.
Files changed (4) hide show
  1. data/bin/docker2etcd +24 -0
  2. data/bin/etcd2env +12 -0
  3. data/lib/small-setup.rb +107 -0
  4. metadata +50 -0
data/bin/docker2etcd ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'small-setup'
4
+
5
+ `docker ps | tail -n+2 | awk '{ print $1 }'`.split("\n")
6
+ .map { |id| `docker inspect #{id}` }
7
+ .map { |inspect| JSON.parse(inspect)[0]}
8
+ .map { |data| flatten(data,data["Name"]) }
9
+ .each { |container|
10
+ name = container.keys.first.split("/")[1]
11
+ port = `docker inspect #{container["/#{name}/id"]} | grep -o '[0-9]\\+/tcp' | head -n1 | grep -o '[0-9]\\+'`.gsub("\n","")
12
+ hport = container["/#{name}/networksettings/ports/#{port}/tcp/hostport"]
13
+ container["/#{name}/name"] = name
14
+ container["/#{name}/port"] = hport
15
+ container["/#{name}/host"] = @host
16
+ container["/#{name}/url"] = "http://#{@host}:#{hport}"
17
+ container.keys.each {|key|
18
+ if container[key] != nil then
19
+ puts "#{key} = #{container[key]}"
20
+ http_put("#{@etcd}/v2/keys#{@prefix}#{key}","value=#{URI.encode(container[key])}")
21
+ end
22
+ }
23
+ }
24
+
data/bin/etcd2env ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'small-setup'
4
+
5
+ data = http_get("#{@etcd}/v2/keys/#{@prefix}?recursive=true")
6
+
7
+ final = eflatten( data["node"] )
8
+ final.keys.each {|k|
9
+ puts "export #{k}=\"#{final[k]}\""
10
+ puts "export #{k.upcase}=\"#{final[k]}\""
11
+ }
12
+
@@ -0,0 +1,107 @@
1
+
2
+ require 'uri'
3
+ require 'json'
4
+ require 'net/http'
5
+ require 'optparse'
6
+
7
+ options = {}
8
+
9
+ OptionParser.new do |opts|
10
+ opts.banner = "Usage: command [options]"
11
+ opts.on("-h", "--host host", "Set host IP/domain") do |h|
12
+ options[:host] = h
13
+ end
14
+ opts.on("-e", "--etcd host:port", "Set etcd host and port") do |e|
15
+ options[:etcd] = e
16
+ end
17
+ opts.on("-p", "--prefix prefix", "Set etcd prefix path") do |p|
18
+ options[:prefix] = p
19
+ end
20
+ opts.on("-d","--daemon","Run in background") do |d|
21
+ options[:daemon]=d
22
+ end
23
+ opts.on("-o","--output file","Output file") do |o|
24
+ options[:output]=o
25
+ end
26
+ opts.on("-i","--input file","Input file") do |o|
27
+ options[:input]=o
28
+ end
29
+ end.parse!
30
+
31
+
32
+ @host = options[:host] || ENV['HOST'] || `hostname -I | awk '{ print $1 }'`.gsub("\n","")
33
+ @etcd = options[:etcd] || ENV['ETCD'] || "http://#{@host}:4001"
34
+ @prefix = options[:prefix] || ENV['PREFIX'] || ""
35
+ @foreground = !options[:daemon]
36
+ @output = options[:output] || false
37
+ @input = options[:input] || false
38
+
39
+ def http_get(uri)
40
+ JSON.parse(Net::HTTP.get(URI(uri)))
41
+ end
42
+
43
+ def http_put(uri,doc)
44
+ uri = URI.parse(uri)
45
+ header = {'Content-Type'=> 'application/x-www-form-urlencoded'}
46
+ if doc.class == Hash then
47
+ header = {'Content-Type'=> 'application/json'}
48
+ end
49
+ http = Net::HTTP.new(uri.host, uri.port)
50
+ request = Net::HTTP::Put.new(uri.request_uri, header)
51
+ request.body = doc
52
+ if doc.class == Hash then
53
+ request.body=doc.to_json
54
+ end
55
+ response = http.request(request)
56
+ if response.body.length >= 2 then
57
+ JSON.parse(response.body)
58
+ else
59
+ {}
60
+ end
61
+ end
62
+
63
+ def flatten(obj,sub)
64
+ flat={}
65
+ sub=sub.gsub("-","_").downcase
66
+ obj.keys.each {|k|
67
+ key=k.gsub("-","_").gsub("/","_").downcase
68
+ if obj[k].class == Array then
69
+ if(obj[k][0].class == Hash) then
70
+ flat=flat.merge(flatten(obj[k][0],"#{sub}/#{k}"))
71
+ end
72
+ elsif obj[k].class == Hash then
73
+ flat=flat.merge(flatten(obj[k],"#{sub}/#{k}"))
74
+ else
75
+ flat["#{sub}/#{key}"] = obj[k].to_s
76
+ end
77
+ }
78
+ flat
79
+ end
80
+
81
+ def eflatten(obj)
82
+ flat = {}
83
+ if obj["dir"] then
84
+ if obj["nodes"] then
85
+ obj["nodes"].each { |n|
86
+ flat = flat.merge(eflatten(n))
87
+ }
88
+ end
89
+ else
90
+ key = obj["key"].gsub("/","_").gsub("-","_")
91
+ flat[key[1..key.length]]=obj["value"]
92
+ end
93
+ flat
94
+ end
95
+
96
+ def nodes2obj(nodes,prefix)
97
+ obj={}
98
+ nodes.each {|node|
99
+ if node['dir'] && node['nodes'] then
100
+ obj[node['key'].gsub(prefix,'')]=nodes2obj( node['nodes'],"#{ node['key'] }/" )
101
+ elsif node['value'] then
102
+ obj[node['key'].gsub(prefix,'')]=node['value']
103
+ end
104
+ }
105
+ obj
106
+ end
107
+
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: small-ops
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.beta
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - Diogo Silva
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-07-09 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Docker, etcd, confd...
15
+ email: diogo@diogok.net
16
+ executables:
17
+ - docker2etcd
18
+ - etcd2env
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/small-setup.rb
23
+ - bin/docker2etcd
24
+ - bin/etcd2env
25
+ homepage: https://github.com/
26
+ licenses:
27
+ - MIT
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>'
42
+ - !ruby/object:Gem::Version
43
+ version: 1.3.1
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 1.8.23
47
+ signing_key:
48
+ specification_version: 3
49
+ summary: Small Docker ops utils
50
+ test_files: []