docker-app 0.2.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 (44) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/CODE_OF_CONDUCT.md +49 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +21 -0
  6. data/bin/console +14 -0
  7. data/bin/setup +8 -0
  8. data/docker-app.gemspec +36 -0
  9. data/exe/docker-app +20 -0
  10. data/lib/docker_app.rb +23 -0
  11. data/lib/docker_app/builder/packer.rb +105 -0
  12. data/lib/docker_app/chef/.chef/knife.rb +118 -0
  13. data/lib/docker_app/chef/chef_build_image.rb +55 -0
  14. data/lib/docker_app/chef/chef_destroy_container.rb +13 -0
  15. data/lib/docker_app/chef/chef_destroy_image.rb +17 -0
  16. data/lib/docker_app/chef/chef_exec_container.rb +16 -0
  17. data/lib/docker_app/chef/chef_run_container.rb +64 -0
  18. data/lib/docker_app/chef/install_container_service.rb +14 -0
  19. data/lib/docker_app/cli.rb +502 -0
  20. data/lib/docker_app/command.rb +16 -0
  21. data/lib/docker_app/config.rb +249 -0
  22. data/lib/docker_app/config/dsl.rb +64 -0
  23. data/lib/docker_app/config/helpers.rb +99 -0
  24. data/lib/docker_app/manager_container.rb +376 -0
  25. data/lib/docker_app/manager_image.rb +119 -0
  26. data/lib/docker_app/manager_swarm.rb +66 -0
  27. data/lib/docker_app/provisioner/base.rb +179 -0
  28. data/lib/docker_app/provisioner/chef.rb +93 -0
  29. data/lib/docker_app/server_settings.rb +361 -0
  30. data/lib/docker_app/version.rb +3 -0
  31. data/lib/templates/example-chef/.chef/knife.rb +5 -0
  32. data/lib/templates/example-chef/config.rb.erb +18 -0
  33. data/lib/templates/example-chef/servers/server1/.chef/knife.rb +8 -0
  34. data/lib/templates/example-chef/servers/server1/config.rb.erb +54 -0
  35. data/lib/templates/example-chef/servers/server1/cookbooks/server1/README.md +1 -0
  36. data/lib/templates/example-chef/servers/server1/cookbooks/server1/metadata.rb.erb +8 -0
  37. data/lib/templates/example-chef/servers/server1/cookbooks/server1/recipes/build.rb +10 -0
  38. data/lib/templates/example-chef/servers/server1/cookbooks/server1/recipes/install.rb +36 -0
  39. data/lib/templates/example-chef/servers/server1/cookbooks/server1/recipes/install_host.rb +9 -0
  40. data/lib/templates/example-chef/servers/server1/cookbooks/server1/templates/index.html.erb +5 -0
  41. data/lib/templates/example-chef/servers/server1/cookbooks/server1/templates/nginx-sites/default.conf.erb +45 -0
  42. data/readme.md +853 -0
  43. data/readme_developers.md +54 -0
  44. metadata +129 -0
@@ -0,0 +1,361 @@
1
+ module DockerApp
2
+
3
+ class ServerSettings
4
+ attr_accessor :properties
5
+ attr_accessor :common_config
6
+
7
+ def get_binding
8
+ return binding()
9
+ end
10
+
11
+ def properties
12
+ @properties ||= {}
13
+ @properties
14
+ end
15
+
16
+
17
+ def all_attributes
18
+ res = properties.clone
19
+
20
+ res['base'] = {
21
+ 'image_name'=> image_name,
22
+ 'container_name'=> container_name,
23
+
24
+ }
25
+
26
+ # docker
27
+ res['docker']['ports'] = docker_ports
28
+ res['docker']['volumes'] = docker_volumes
29
+ res['docker']['links'] = docker_links
30
+
31
+ res
32
+ end
33
+
34
+
35
+ #
36
+ def node
37
+ res = properties['attributes'] || {}
38
+
39
+ res['name'] = name
40
+ res['container_name'] = container_name
41
+
42
+ res
43
+ end
44
+
45
+ ### DSL
46
+
47
+
48
+ def build=(opts={})
49
+ build(opts)
50
+ end
51
+ def docker=(opts={})
52
+ docker(opts)
53
+ end
54
+ def provision=(opts={})
55
+ provision(opts)
56
+ end
57
+
58
+ def attributes=(opts={})
59
+ attributes(opts)
60
+ end
61
+
62
+
63
+ def set(name, v)
64
+ properties[name] = v
65
+ end
66
+
67
+ def add(name, v)
68
+ properties[name] = {} if properties[name].nil?
69
+
70
+ properties[name].merge!(v)
71
+ end
72
+
73
+
74
+ def add_config(a)
75
+ # merge
76
+ build(a['build']) if a['build']
77
+ provision(a['provision']) if a['provision']
78
+ docker(a['docker']) if a['docker']
79
+ attributes(a['attributes']) if a['attributes']
80
+
81
+
82
+ end
83
+
84
+ def build(v)
85
+ properties['build'] = v
86
+ end
87
+
88
+ def provision(v)
89
+ properties['provision'] = v
90
+ end
91
+
92
+ def docker(a)
93
+ # merge
94
+ properties['docker'] ||= {}
95
+
96
+ a.each do |k,v|
97
+ properties['docker'][k] = v
98
+ end
99
+ end
100
+
101
+ def attributes(a)
102
+ # merge
103
+ properties['attributes'] ||= {}
104
+
105
+ a.each do |k,v|
106
+ properties['attributes'][k] = v
107
+ end
108
+ end
109
+
110
+
111
+ ###
112
+ def properties_common(opt_name)
113
+ common_config.options[opt_name] || common_config.send(opt_name)
114
+ end
115
+
116
+ def prefix
117
+ #properties['common']['prefix']
118
+ properties_common('prefix')
119
+ end
120
+
121
+ def container_prefix
122
+ "#{prefix}#{properties_common('container_prefix')}"
123
+ end
124
+
125
+ def image_prefix
126
+ "#{prefix}#{properties_common('image_prefix')}"
127
+ end
128
+
129
+ def service_prefix
130
+ "#{prefix}#{properties_common('service_prefix')}"
131
+ end
132
+
133
+
134
+ ###
135
+
136
+ def name
137
+ properties['name']
138
+ end
139
+
140
+
141
+
142
+ def image_name
143
+ if !need_build?
144
+ bi = properties['build']['base_image']
145
+ return "#{bi['name']}:#{bi['tag']}"
146
+ end
147
+
148
+ #
149
+ s = properties['name']
150
+
151
+ if properties['build']['image_name']
152
+ s = "#{properties['build']['image_name']}"
153
+ end
154
+
155
+ "#{image_prefix}#{s}"
156
+ end
157
+
158
+ def need_build?
159
+ build_type = properties['build']['build_type']
160
+ return false if build_type=='' || build_type=='none'
161
+
162
+
163
+ true
164
+ end
165
+
166
+ def container_name(name=nil)
167
+ name ||= properties['name']
168
+ s = name
169
+
170
+ "#{container_prefix}#{s}"
171
+ end
172
+
173
+ def chef_node_name
174
+ "#{prefix}#{name}"
175
+ end
176
+
177
+ def volume_path_local(v)
178
+ res = v.to_s
179
+
180
+ if v =~ /^\./
181
+ s = v.gsub /^\.\//, ''
182
+
183
+ #res = "$PWD/servers/#{self.name}/#{s}"
184
+ res = File.expand_path(s, dir_server_root)
185
+ #res = File.expand_path(s, properties_common('root_path'))
186
+
187
+ elsif v =~ /^\/\//
188
+ res = dir_data_base+(v.gsub /^\/\//, '')
189
+ elsif v =~ /^\//
190
+ res = v
191
+ else
192
+ res = self.dir_data+v
193
+ end
194
+
195
+ res
196
+ end
197
+
198
+ def dir_data_base
199
+ "#{properties_common('dir_data')}"
200
+ end
201
+
202
+ def dir_data
203
+ "#{properties_common('dir_data')}#{self.name}/"
204
+ end
205
+
206
+
207
+ ### docker swarm services
208
+
209
+ def service_name(name=nil)
210
+ name ||= properties['name']
211
+ s = name
212
+
213
+ "#{service_prefix}#{s}"
214
+ end
215
+
216
+ ###
217
+ def docker_volumes
218
+ a = properties['docker']['volumes'] || []
219
+
220
+ # fix paths
221
+ res = a.map do |r|
222
+ path_local = volume_path_local(r[0])
223
+
224
+ [path_local, r[1]]
225
+ end
226
+
227
+ res
228
+ end
229
+
230
+ def docker_volumes_string
231
+ docker_volumes.map{|r| "-v #{r[0]}:#{r[1]}"}.join(' ')
232
+ end
233
+
234
+ def docker_volumes_array
235
+ docker_volumes.map{|d| "#{d[0]}:#{d[1]}"}
236
+ end
237
+
238
+ ###
239
+
240
+ def docker_volumes_from
241
+ a = properties['docker']['volumes_from'] || []
242
+
243
+ # fix paths
244
+ res = a.map do |r|
245
+ "#{prefix}#{r}"
246
+ end
247
+
248
+ res
249
+ end
250
+
251
+ def docker_volumes_from_string
252
+ docker_volumes_from.map{|d| "--volumes-from #{d}"}.join(' ')
253
+ end
254
+
255
+ def docker_volumes_from_array
256
+ docker_volumes_from.map{|d| "#{d}"}
257
+ end
258
+
259
+ ###
260
+ def docker_ports
261
+ a = properties['docker']['ports'] || []
262
+ a
263
+ end
264
+
265
+ def docker_ports_string
266
+ docker_ports.map{|r| "-p #{r[0]}:#{r[1]}"}.join(' ')
267
+ end
268
+
269
+ def docker_ports_array
270
+ docker_ports.map{|d| "#{d[0]}:#{d[1]}"}
271
+ end
272
+
273
+ ###
274
+ def docker_links
275
+ a = properties['docker']['links'] || []
276
+
277
+ a
278
+ end
279
+
280
+ def docker_links_string
281
+ docker_links_array.map{|s| "--link #{s}"}.join(' ')
282
+ end
283
+
284
+ def docker_links_array
285
+ docker_links.map{|d| docker_link_build(d)}
286
+ end
287
+
288
+ def docker_link_build(r)
289
+ # fix
290
+ "#{container_prefix}#{r[0]}:#{r[1]}"
291
+ end
292
+
293
+ ###
294
+
295
+ def run_env_variables
296
+ a = properties['docker']['run_env'] || []
297
+
298
+ a
299
+ end
300
+
301
+ def run_env_variables_string
302
+ run_env_variables.map{|r| "-e #{r[0]}=#{r[1]}"}.join(' ')
303
+ end
304
+
305
+
306
+ ###
307
+
308
+ def run_extra_options_string
309
+ s = properties['docker']['run_extra_options'] || ''
310
+ s
311
+ end
312
+
313
+
314
+ ###
315
+ def is_swarm_mode?
316
+ v = properties["docker"]["swarm_mode"]
317
+ return false if v.nil?
318
+ return v
319
+ end
320
+
321
+ ###
322
+ def [](key)
323
+ properties[key]
324
+ end
325
+
326
+
327
+ ###
328
+ def filename_chef_config
329
+ #File.join(File.dirname(__FILE__), '..', 'config' ,"config-#{name}.json")
330
+
331
+ File.join(Config.root_path, 'temp', "#{name}.json")
332
+ end
333
+
334
+ def filename_config_json
335
+ File.join(Config.root_path, 'temp', "#{name}.json")
336
+ end
337
+
338
+ def dir_cookbooks
339
+ File.expand_path("servers/#{name}/cookbooks", Config.root_path)
340
+ end
341
+
342
+ def dir_server_root
343
+ File.expand_path("servers/#{name}", Config.root_path)
344
+ end
345
+
346
+ def filename_chef_node_json
347
+ File.expand_path("nodes/#{image_name}.json", dir_server_root)
348
+ end
349
+ def filename_chef_client_json
350
+ File.expand_path("clients/#{image_name}.json", dir_server_root)
351
+ end
352
+
353
+ ###
354
+
355
+ def make_path_full(path)
356
+ File.expand_path("#{path}", dir_server_root)
357
+ end
358
+
359
+ end
360
+
361
+ end
@@ -0,0 +1,3 @@
1
+ module DockerApp
2
+ VERSION = "0.2.1"
3
+ end
@@ -0,0 +1,5 @@
1
+ ### add cookbooks
2
+ #cookbook_path cookbook_path+[
3
+ # '/work/chef-repo/cookbooks-common',
4
+ # '/work/chef-repo/cookbooks',
5
+ #]
@@ -0,0 +1,18 @@
1
+ common({
2
+ 'prefix' => "example-",
3
+ 'image_prefix' => 'example-',
4
+ 'dir_data' => '/path/to/data/',
5
+
6
+ })
7
+
8
+ servers({
9
+ '<%=@name%>'=>{
10
+ # some server options here
11
+ }
12
+
13
+ })
14
+
15
+
16
+ base({
17
+
18
+ })
@@ -0,0 +1,8 @@
1
+ #node_name ENV["NODE_NAME"] || ENV['SERVER_NAME'] || 'nginx-front'
2
+
3
+ # cookbooks
4
+ #cookbook_path [
5
+ # File.expand_path("../cookbooks", __FILE__)
6
+ #]
7
+
8
+
@@ -0,0 +1,54 @@
1
+
2
+ add 'build', {
3
+ "image_name" => "<%=@name%>",
4
+ 'build_type' => 'chef',
5
+
6
+ "base_image" => {
7
+ "name" => "nginx",
8
+ "repository" => "nginx",
9
+ "tag" => "1.10"
10
+
11
+ },
12
+
13
+ }
14
+
15
+ add 'install', {
16
+ "host" => {
17
+ 'script_type' => 'chef_recipe',
18
+ 'script' => 'install_host',
19
+ },
20
+ "node" => {
21
+ 'script_type' => 'chef_recipe',
22
+ 'script' => 'install',
23
+ }
24
+
25
+
26
+ }
27
+
28
+
29
+ add 'docker', {
30
+ #"command"=> "",
31
+ #'command' => "/sbin/my_init",
32
+ "command"=> "nginx -g 'daemon off;'",
33
+ 'ports' => [
34
+ #[8080,80],
35
+ ],
36
+ 'volumes' => [
37
+ #['html', '/usr/share/nginx/html'],
38
+ #['log/nginx', '/var/log/nginx/'],
39
+ ],
40
+ 'links' => [
41
+ ]
42
+ }
43
+
44
+
45
+
46
+ add 'attributes', {
47
+ '<%=@name%>' =>{
48
+ "sitename" =>"mysite.local"
49
+ },
50
+
51
+
52
+ }
53
+
54
+