fige 0.0.1

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 (2) hide show
  1. data/bin/fige +167 -0
  2. metadata +47 -0
data/bin/fige ADDED
@@ -0,0 +1,167 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'yaml'
4
+ require 'optparse'
5
+
6
+ @options = {}
7
+
8
+ OptionParser.new do |opts|
9
+ opts.banner = "Usage: command [options]"
10
+ opts.on("-h", "--host host", "Set host IP/domain") do |h|
11
+ @options[:host] = h
12
+ end
13
+ opts.on("-d", "--data FOO=BAR,FUZ=BAZ",Array, "Insert data into each env") do |d|
14
+ @options[:data] = d
15
+ end
16
+ opts.on("-c","--command cmd","A command to execute after each run") do |c|
17
+ @options[:cmd]=c
18
+ end
19
+ opts.on("-f","--foreground","Keep running on foregroud. Default is run once and exit.") do |f|
20
+ @options[:foreground]=f
21
+ end
22
+ opts.on("-t","--target name","Target a single container") do |t|
23
+ @options[:target]=t
24
+ end
25
+ opts.on("-i","--input file","Input file") do |i|
26
+ @options[:input]=i
27
+ end
28
+ opts.on("-u","--update","pull container") do |p|
29
+ @options[:update]=p
30
+ end
31
+ opts.on("-a","--apend cmd","Append cmd to docker command, before the '-t repo/image'") do |a|
32
+ @options[:append]=a
33
+ end
34
+ opts.on("-n","--dry","Dry run, does no execute only echo commands") do |n|
35
+ @options[:dry]=true
36
+ end
37
+ end.parse!
38
+
39
+ file = @options[:input] || 'docker-compose.yml'
40
+ yml = YAML.load_file(file)
41
+
42
+ env = { }
43
+
44
+ @options[:containers]=[]
45
+
46
+ apps=[]
47
+ if @options[:target] then
48
+ apps=yml.keys.select {|k| k == @options[:target]}
49
+ else
50
+ apps=yml.keys
51
+ end
52
+
53
+ apps.each {|k|
54
+
55
+ if !yml[k].has_key? "name" then
56
+ yml[k]['name'] = yml[k]['image'].gsub(/[^\/]+\//,'')
57
+ end
58
+
59
+ if !@options[:dry] then
60
+ puts `docker stop #{yml[k]['name']} 2> /dev/null`
61
+ puts `docker rm #{yml[k]['name']} 2> /dev/null`
62
+ end
63
+
64
+ if !yml[k].has_key? "environment" then
65
+ yml[k]['environment'] = {}
66
+ end
67
+
68
+ env.keys.each{ |ke|
69
+ yml[k]['environment'][ke] = env[ke]
70
+ }
71
+
72
+ cmd = "docker run -d "
73
+ cmd << " --name #{yml[k]['name']} "
74
+
75
+ if yml[k].has_key? "ports" then
76
+ yml[k]['ports'].each {|p|
77
+ cmd << " -p #{p} "
78
+ }
79
+ end
80
+
81
+ if yml[k].has_key? "links" then
82
+ yml[k]['links'].each {|l|
83
+ cmd << " --link #{l} "
84
+ }
85
+ end
86
+
87
+ if yml[k].has_key? "volumes" then
88
+ yml[k]['volumes'].each {|v|
89
+ cmd << " -v #{v} "
90
+ }
91
+ end
92
+
93
+ if yml[k].has_key? "env" then
94
+ yml[k]['env'].each {|e|
95
+ cmd << " -e \"#{e}\" "
96
+ }
97
+ end
98
+
99
+ if yml[k].has_key? "environment" then
100
+ yml[k]['environment'].keys.each {|ke|
101
+ cmd << " -e \"#{ke}=#{yml[k]['environment'][ke].gsub('"','\"')}\" "
102
+ }
103
+ end
104
+
105
+ if yml[k].has_key? "hostname" then
106
+ if @options[:host] then
107
+ cmd << " --hostname=\"#{@options[:host]}_#{yml[k]["hostname"]}\" "
108
+ else
109
+ cmd << " --hostname=\"#{yml[k]["hostname"]}\" "
110
+ end
111
+ elsif @options[:host] then
112
+ cmd << " --hostname=\"#{@options[:host]}_#{yml[k]["name"]}\" "
113
+ end
114
+
115
+ if yml[k].has_key? "restart" then
116
+ cmd << " --restart #{yml[k]["restart"]} "
117
+ end
118
+
119
+ if @options[:data] then
120
+ @options[:data].each {|d|
121
+ cmd << " -e \"#{d}\" "
122
+ }
123
+ end
124
+
125
+ if @options[:append] then
126
+ cmd << @options[:append]
127
+ end
128
+
129
+ cmd << " #{yml[k]['image']} "
130
+
131
+ if yml[k].has_key? "command" then
132
+ cmd << " #{yml[k]["command"]} "
133
+ end
134
+
135
+ if !@options[:dry] && @options[:update] then
136
+ `docker pull #{yml[k]['image']} > /dev/null 2>&1`
137
+ end
138
+
139
+ puts cmd
140
+ if !@options[:dry] then
141
+ cid = `#{cmd}`
142
+ @options[:containers].push(cid)
143
+ puts "#{yml[k]['name']} = #{cid}"
144
+ end
145
+
146
+ if @options[:cmd] then
147
+ puts `#{@options[:cmd]}`
148
+ end
149
+
150
+ }
151
+
152
+ if @options[:foreground] then
153
+ def stop()
154
+ puts "Stopping..."
155
+ @options[:containers].each {|cid|
156
+ puts `docker stop #{cid}`
157
+ puts `docker rm #{cid}`
158
+ }
159
+ exit
160
+ end
161
+ trap('SIGINT') do stop() end
162
+ trap('SIGTERM') do stop() end
163
+ while true do
164
+ sleep 5
165
+ end
166
+ end
167
+
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fige
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Diogo Silva
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-05-25 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Run docker containers out of a docker-compose.yml
15
+ email: diogo@diogok.net
16
+ executables:
17
+ - fige
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - bin/fige
22
+ homepage: https://github.com/diogok/fige
23
+ licenses:
24
+ - MIT
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.23
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: Docker-compose/fig clone, simplified.
47
+ test_files: []