small-ops 0.0.15 → 0.0.16

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 CHANGED
@@ -2,6 +2,22 @@
2
2
 
3
3
  require 'small-setup'
4
4
 
5
+ OptionParser.new do |opts|
6
+ opts.banner = "Usage: command [options]"
7
+ opts.on("-h", "--host host", "Set host IP/domain") do |h|
8
+ @options[:host] = h
9
+ end
10
+ opts.on("-e", "--etcd host:port", "Set etcd host and port") do |e|
11
+ @options[:etcd] = e
12
+ end
13
+ opts.on("-u","--url","Make container url as host/name instead of host:port") do |u|
14
+ @options[:url]=u
15
+ end
16
+ opts.on("-v","--verbose","verboser") do |v|
17
+ @options[:verbose]=v
18
+ end
19
+ end.parse!
20
+
5
21
  `docker ps | tail -n+2 | awk '{ print $1 }'`.split("\n")
6
22
  .map { |id| `docker inspect #{id}` }
7
23
  .map { |inspect| JSON.parse(inspect)[0]}
@@ -12,18 +28,18 @@ require 'small-setup'
12
28
  hport = container["/#{name}/networksettings/ports/#{port}/tcp/hostport"]
13
29
  container["/#{name}/name"] = name
14
30
  container["/#{name}/port"] = hport
15
- container["/#{name}/host"] = @host
16
- if @as_url then
17
- container["/#{name}/url"] = "http://#{@host}/#{name}"
31
+ container["/#{name}/host"] = @options[:host]
32
+ if @options[:url] then
33
+ container["/#{name}/url"] = "http://#{@options[:host]}/#{name}"
18
34
  else
19
- container["/#{name}/url"] = "http://#{@host}:#{hport}"
35
+ container["/#{name}/url"] = "http://#{@options[:host]}:#{hport}"
20
36
  end
21
37
  container.keys.each {|key|
22
38
  if container[key] != nil then
23
- if @verbose then
39
+ if @options[:verbose] then
24
40
  puts "#{key} = #{container[key]}"
25
41
  end
26
- http_put("#{@etcd}/v2/keys#{@prefix}#{key}","value=#{URI.encode(container[key])}")
42
+ http_put("#{@options[:etcd]}/v2/keys#{key}","value=#{URI.encode(container[key])}")
27
43
  end
28
44
  }
29
45
  puts "-> #{name} : #{container["/#{name}/url"]}"
data/bin/etcd2conf CHANGED
@@ -4,7 +4,29 @@ require 'small-setup'
4
4
  require 'erb'
5
5
  require 'zlib'
6
6
 
7
- if !@input || !@output then
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("-o","--output file","Output file") do |o|
19
+ @options[:output]=o
20
+ end
21
+ opts.on("-i","--input file","Input file") do |o|
22
+ @options[:input]=o
23
+ end
24
+ opts.on("-c","--command cmd","A command to execute after each run") do |c|
25
+ @options[:cmd]=c
26
+ end
27
+ end.parse!
28
+
29
+ if !@options[:input] || !@options[:output] then
8
30
  raise "Need input and output!"
9
31
  end
10
32
 
@@ -13,46 +35,44 @@ class Namespace
13
35
  @data=data
14
36
  @config=config
15
37
  end
16
-
17
38
  def get_binding
18
39
  binding
19
40
  end
20
41
  end
21
42
 
22
-
23
43
  run = true
24
- `bash -c '[[ ! -e #{@output} ]] && touch #{@output}'`
25
- last = File.read(@output)
44
+ `bash -c '[[ ! -e #{@options[:output]} ]] && touch #{@options[:output]}'`
45
+ last = File.read(@options[:output])
26
46
 
27
47
  while run do
28
- r = http_get("#{@etcd}/v2/keys/#{@prefix}?recursive=true")
48
+ r = http_get("#{@options[:etcd]}/v2/keys/?recursive=true")
29
49
 
30
50
  data = nodes2obj(r["node"]["nodes"],"/")
31
51
 
32
- template = File.read(@input)
52
+ template = File.read(@options[:input])
33
53
 
34
- if @prefix.length >= 1 then prefix = "#{prefix}/" end
54
+ if @options[:prefix].length >= 1 then prefix = "#{prefix}/" end
35
55
 
36
- ns = Namespace.new(data,{"host"=>@host,"prefix"=>prefix})
56
+ ns = Namespace.new(data,{"host"=>@options[:host],"prefix"=>prefix})
37
57
  result=ERB.new(template).result(ns.get_binding)
38
58
 
39
59
  actual = Zlib::crc32(result)
40
60
 
41
61
  if actual != last then
42
- puts "Writing #{@output}"
62
+ puts "Writing #{@options[:output]}"
43
63
 
44
- File.open(@output, 'w') { |file| file.write(result) }
64
+ File.open(@options[:output], 'w') { |file| file.write(result) }
45
65
 
46
66
  last = actual
47
67
 
48
- if @cmd then
49
- puts `#{@cmd}`
68
+ if @options[:cmd] then
69
+ puts `#{@options[:cmd]}`
50
70
  end
51
71
  end
52
72
 
53
73
  run = false
54
74
 
55
- if @foreground then
75
+ if @options[:foreground] then
56
76
  sleep 10
57
77
  run = true
58
78
  end
data/bin/etcd2env CHANGED
@@ -2,7 +2,17 @@
2
2
 
3
3
  require 'small-setup'
4
4
 
5
- data = http_get("#{@etcd}/v2/keys/#{@prefix}?recursive=true")
5
+ OptionParser.new do |opts|
6
+ opts.banner = "Usage: command [options]"
7
+ opts.on("-h", "--host host", "Set host IP/domain") do |h|
8
+ @options[:host] = h
9
+ end
10
+ opts.on("-e", "--etcd host:port", "Set etcd host and port") do |e|
11
+ @options[:etcd] = e
12
+ end
13
+ end.parse!
14
+
15
+ data = http_get("#{@options[:etcd]}/v2/keys/?recursive=true")
6
16
 
7
17
  final = eflatten( data["node"] )
8
18
  final.keys.each {|k|
data/bin/fige CHANGED
@@ -3,22 +3,44 @@
3
3
  require 'small-setup'
4
4
  require 'yaml'
5
5
 
6
- yml = YAML.load_file('fig.yml')
6
+ OptionParser.new do |opts|
7
+ opts.banner = "Usage: command [options]"
8
+ opts.on("-h", "--host host", "Set host IP/domain") do |h|
9
+ @options[:host] = h
10
+ end
11
+ opts.on("-e", "--etcd host:port", "Set etcd host and port") do |e|
12
+ @options[:etcd] = e
13
+ end
14
+ opts.on("-c","--command cmd","A command to execute after each run") do |c|
15
+ @options[:cmd]=c
16
+ end
17
+ opts.on("-f","--foreground","Keep running on foregroud. Default is run once and exit.") do |f|
18
+ @options[:foreground]=f
19
+ end
20
+ opts.on("-t","--target name","Target a single container") do |t|
21
+ @options[:target]=t
22
+ end
23
+ opts.on("-i","--input file","Input file") do |i|
24
+ @options[:input]=i
25
+ end
26
+ opts.on("-u","--update","pull container") do |p|
27
+ @options[:update]=p
28
+ end
29
+ end.parse!
30
+
31
+ file = @options[:input] || 'fig.yml'
32
+ yml = YAML.load_file(file)
7
33
 
8
34
  env = {
9
- 'HOST'=>@host,
10
- 'ETCD'=>@etcd
35
+ 'HOST'=>@options[:host],
36
+ 'ETCD'=>@options[:etcd]
11
37
  }
12
38
 
13
- if @prefix.length >= 1 then
14
- env['PREFIX']=@prefix
15
- end
16
-
17
- @containers=[]
39
+ @options[:containers]=[]
18
40
 
19
41
  apps=[]
20
- if @target then
21
- apps=yml.keys.select {|k| k == @target}
42
+ if @options[:target] then
43
+ apps=yml.keys.select {|k| k == @options[:target]}
22
44
  else
23
45
  apps=yml.keys
24
46
  end
@@ -40,10 +62,6 @@ apps.each {|k|
40
62
  yml[k]['environment'][ke] = env[ke]
41
63
  }
42
64
 
43
- if @prefix.length >= 1 then
44
- yml[k]['name'] = "#{@prefix}_#{yml[k]['name']}"
45
- end
46
-
47
65
  cmd = "docker run -d "
48
66
  cmd << " --name #{yml[k]['name']} "
49
67
 
@@ -77,24 +95,26 @@ apps.each {|k|
77
95
  cmd << " #{yml[k]["command"]} "
78
96
  end
79
97
 
80
- #`docker pull #{yml[k]['image']}`
98
+ if @options[:update] then
99
+ `docker pull #{yml[k]['image']} > /dev/null 2>&1`
100
+ end
81
101
 
82
102
  cid = `#{cmd}`
83
- @containers.push(cid)
103
+ @options[:containers].push(cid)
84
104
  puts "#{yml[k]['name']} = #{cid}"
85
105
 
86
- if @cmd then
87
- puts `#{@cmd}`
106
+ if @options[:cmd] then
107
+ puts `#{@options[:cmd]}`
88
108
  end
89
109
 
90
110
  }
91
111
 
92
112
  puts `docker ps`
93
113
 
94
- if @foreground then
114
+ if @options[:foreground] then
95
115
  def stop()
96
116
  puts "Stopping..."
97
- @containers.each {|cid|
117
+ @options[:containers].each {|cid|
98
118
  puts `docker stop #{cid}`
99
119
  puts `docker rm #{cid}`
100
120
  }
data/lib/small-setup.rb CHANGED
@@ -4,58 +4,18 @@ require 'json'
4
4
  require 'net/http'
5
5
  require 'optparse'
6
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
- opts.on("-c","--command cmd","A command to execute at certain point") do |c|
30
- options[:cmd]=c
31
- end
32
- opts.on("-t","--target name","Target a single container") do |t|
33
- options[:target]=t
34
- end
35
- opts.on("-u","--as_url","Make url as host/name instead of host:port") do |u|
36
- options[:as_url]=u
37
- end
38
- opts.on("-v","--verbose","verboser") do |v|
39
- options[:verbose]=v
40
- end
41
- end.parse!
7
+ @options = {}
8
+ if ENV["HOST"] then
9
+ @options[:host] = ENV["HOST"]
10
+ else
11
+ @options[:host] = `hostname -I | awk '{ print $1 }'`.gsub("\n","")
12
+ end
42
13
 
43
- @host = options[:host] || ENV['HOST'] || `hostname -I | awk '{ print $1 }'`.gsub("\n","")
44
- if options[:etcd] then
45
- @etcd = options[:etcd]
46
- elsif ENV["ETCD_PORT_4001_TCP_ADDR"] then
47
- @etcd = "http://#{ENV["ETCD_PORT_4001_TCP_ADDR"]}:#{ENV["ETCD_PORT_4001_TCP_PORT"]}"
14
+ if ENV["ETCD_PORT_4001_TCP_ADDR"] then
15
+ @options[:etcd] = "http://#{ENV["ETCD_PORT_4001_TCP_ADDR"]}:#{ENV["ETCD_PORT_4001_TCP_PORT"]}"
48
16
  else
49
- @etcd = ENV['ETCD'] || "http://#{@host}:4001"
17
+ @options[:etcd] = ENV['ETCD'] || "http://#{@options[:host]}:4001"
50
18
  end
51
- @prefix = options[:prefix] || ENV['PREFIX'] || ""
52
- @foreground = !options[:daemon]
53
- @output = options[:output] || false
54
- @input = options[:input] || false
55
- @cmd = options[:cmd] || false
56
- @target = options[:target] || false
57
- @as_url = options[:as_url] || false
58
- @verbose = options[:verbose] || false
59
19
 
60
20
  def http_get(uri)
61
21
  JSON.parse(Net::HTTP.get(URI(uri)))
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.15
4
+ version: 0.0.16
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: