docker-builder 0.1.50 → 0.1.55
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/{Rakefile → Rakefile_temp} +8 -7
- data/install_local.sh +1 -1
- data/lib/docker_builder.rb +4 -1
- data/lib/docker_builder/cli.rb +23 -56
- data/lib/docker_builder/config.rb +75 -34
- data/lib/docker_builder/config/dsl.rb +17 -1
- data/lib/docker_builder/{manager.rb → manager_container.rb} +25 -308
- data/lib/docker_builder/manager_image.rb +103 -0
- data/lib/docker_builder/provisioner/base.rb +188 -0
- data/lib/docker_builder/provisioner/{provisioner_chef.rb → chef.rb} +5 -1
- data/lib/docker_builder/server_settings.rb +34 -64
- data/lib/docker_builder/version.rb +1 -1
- data/readme.md +118 -79
- data/temp_config.rb +17 -0
- metadata +8 -58
- data/examples/example-apps-php/.chef/knife.rb +0 -6
- data/examples/example-apps-php/config.rb +0 -19
- data/examples/example-apps-php/servers/apps-php/.chef/knife.rb +0 -2
- data/examples/example-apps-php/servers/apps-php/config.rb +0 -69
- data/examples/example-apps-php/servers/apps-php/cookbooks/apps-php/README.md +0 -1
- data/examples/example-apps-php/servers/apps-php/cookbooks/apps-php/metadata.rb +0 -9
- data/examples/example-apps-php/servers/apps-php/cookbooks/apps-php/recipes/build.rb +0 -43
- data/examples/example-apps-php/servers/apps-php/cookbooks/apps-php/recipes/install.rb +0 -55
- data/examples/example-apps-php/servers/apps-php/cookbooks/apps-php/recipes/install_app.rb +0 -27
- data/examples/example-apps-php/servers/apps-php/cookbooks/apps-php/recipes/install_host.rb +0 -9
- data/examples/example-apps-php/servers/apps-php/cookbooks/apps-php/templates/index.html.erb +0 -4
- data/examples/example-apps-php/servers/apps-php/cookbooks/apps-php/templates/nginx-sites/app.conf.erb +0 -55
- data/examples/example-apps-php/servers/apps-php/cookbooks/apps-php/templates/nginx-sites/app.conf.erb.1 +0 -55
- data/examples/example-apps-php/servers/apps-php/cookbooks/apps-php/templates/nginx-sites/default.conf.erb +0 -45
- data/examples/example-apps-php/servers/apps-php/readme.md +0 -0
- data/examples/example-apps-php/temp/apps-php.1.json +0 -14
- data/examples/example-base-chef/.chef/knife.rb +0 -6
- data/examples/example-base-chef/config.rb +0 -19
- data/examples/example-base-chef/readme.md +0 -8
- data/examples/example-base-chef/servers/my/.chef/knife.rb +0 -2
- data/examples/example-base-chef/servers/my/config.rb +0 -53
- data/examples/example-base-chef/servers/my/cookbooks/my/README.md +0 -1
- data/examples/example-base-chef/servers/my/cookbooks/my/metadata.rb +0 -9
- data/examples/example-base-chef/servers/my/cookbooks/my/recipes/build.rb +0 -23
- data/examples/example-base-chef/servers/my/cookbooks/my/recipes/install.rb +0 -0
- data/examples/example-base-chef/servers/my/cookbooks/my/recipes/install_host.rb +0 -9
- data/examples/example-base-chef/servers/my/readme.md +0 -0
- data/examples/example-kafka-zookeeper/config.rb +0 -87
- data/examples/example-kafka-zookeeper/servers/kafka/Dockerfile +0 -21
- data/examples/example-kafka-zookeeper/servers/kafka/README.md +0 -78
- data/examples/example-kafka-zookeeper/servers/kafka/files/LICENSE +0 -202
- data/examples/example-kafka-zookeeper/servers/kafka/files/broker-list.sh +0 -5
- data/examples/example-kafka-zookeeper/servers/kafka/files/create-topics.sh +0 -32
- data/examples/example-kafka-zookeeper/servers/kafka/files/download-kafka.sh +0 -5
- data/examples/example-kafka-zookeeper/servers/kafka/files/start-kafka-shell.sh +0 -2
- data/examples/example-kafka-zookeeper/servers/kafka/files/start-kafka.sh +0 -67
- data/examples/example-nginx/config.rb +0 -19
- data/examples/example-nginx/servers/nginx/.chef/knife.rb +0 -8
- data/examples/example-nginx/servers/nginx/config.rb +0 -55
- data/examples/example-nginx/servers/nginx/cookbooks/nginx/README.md +0 -1
- data/examples/example-nginx/servers/nginx/cookbooks/nginx/metadata.rb +0 -9
- data/examples/example-nginx/servers/nginx/cookbooks/nginx/recipes/build.rb +0 -10
- data/examples/example-nginx/servers/nginx/cookbooks/nginx/recipes/install.rb +0 -28
- data/examples/example-nginx/servers/nginx/cookbooks/nginx/recipes/install_host.rb +0 -9
- data/examples/example-nginx/servers/nginx/cookbooks/nginx/templates/index.html.erb +0 -4
- data/examples/example-nginx/servers/nginx/cookbooks/nginx/templates/nginx-sites/default.conf.erb +0 -45
- data/examples/example-nginx/servers/nginx/readme.md +0 -0
- data/examples/example-nginx/servers/nginx/scripts/install.sh +0 -3
- data/examples/example-nginx/temp/ex_nginx.json +0 -20
- data/examples/example-nginx/temp/nginx.json +0 -1
- data/examples/example-swarm-redis/config.rb +0 -61
- data/examples/example-swarm-redis/readme.md +0 -10
- data/lib/docker_builder/temp.rb +0 -1
@@ -0,0 +1,103 @@
|
|
1
|
+
module DockerBuilder
|
2
|
+
class ManagerImage
|
3
|
+
|
4
|
+
###
|
5
|
+
def self.build_image(server_name, settings=nil)
|
6
|
+
puts "building image for #{server_name}..."
|
7
|
+
#puts "settings: #{settings}"
|
8
|
+
#puts "debug: #{settings['properties']}"
|
9
|
+
|
10
|
+
#settings = load_settings(server_name)
|
11
|
+
|
12
|
+
t = settings['build']['build_type']
|
13
|
+
if t=='' || t=='none'
|
14
|
+
#
|
15
|
+
puts "no build needed..."
|
16
|
+
|
17
|
+
elsif t.downcase=='dockerfile'
|
18
|
+
return build_image_with_dockerfile(settings)
|
19
|
+
elsif t=='chef'
|
20
|
+
return build_image_with_chef(settings)
|
21
|
+
elsif t=='packer'
|
22
|
+
return build_image_with_packer(settings)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.build_image_with_dockerfile(settings)
|
27
|
+
puts "build image with Dockerfile"
|
28
|
+
|
29
|
+
#cmd %Q(cd #{name} && docker build -t #{settings.image_name} . )
|
30
|
+
cmd %Q(docker build -t #{settings.image_name} #{settings.dir_server_root} )
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.build_image_with_chef(settings)
|
35
|
+
puts "build image with chef"
|
36
|
+
|
37
|
+
# config json
|
38
|
+
save_chef_config(settings)
|
39
|
+
|
40
|
+
# check node
|
41
|
+
cmd %Q(cd #{Config.root_path} && chef exec knife node show #{settings.chef_node_name} -c #{chef_config_knife_path})
|
42
|
+
|
43
|
+
|
44
|
+
#cmd %Q(SERVER_NAME=#{settings.name} SERVER_PATH=#{settings.dir_server_root} chef exec chef-client -z -N #{settings.image_name} -j #{settings.filename_config_json} -c #{chef_config_knife_path} #{chef_recipe_path('chef_build_image.rb')} )
|
45
|
+
res_recipe = run_chef_recipe(settings, 'chef_build_image.rb')
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
def self.build_image_with_packer(settings)
|
50
|
+
require_relative '../../lib/docker_builder/builder/packer'
|
51
|
+
|
52
|
+
puts "build image with packer"
|
53
|
+
|
54
|
+
builder = DockerBuilder::Builder::Packer.new(settings)
|
55
|
+
builder.build
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
def self.destroy_image(server_name, settings={})
|
60
|
+
puts "destroying image for server #{server_name}"
|
61
|
+
|
62
|
+
cmd %Q(docker rmi #{settings.image_name} )
|
63
|
+
cmd %Q(docker rm -f chef.converge.#{settings.image_name} )
|
64
|
+
|
65
|
+
# delete chef data
|
66
|
+
if settings['build']['build_type']=='chef'
|
67
|
+
return destroy_image_chef(settings)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
def self.destroy_image_chef(settings)
|
73
|
+
puts "destroying image with chef..."
|
74
|
+
|
75
|
+
# config json
|
76
|
+
save_chef_config(settings)
|
77
|
+
|
78
|
+
# destroy temp container
|
79
|
+
cmd %Q(docker rm -f chef-converge.#{settings.image_name} )
|
80
|
+
|
81
|
+
#
|
82
|
+
cmd %Q(cd #{Config.root_path} && chef exec knife node delete #{settings.chef_node_name} -y -c #{chef_config_knife_path})
|
83
|
+
|
84
|
+
res_recipe = run_chef_recipe(settings, 'chef_destroy_image.rb')
|
85
|
+
|
86
|
+
chef_remove_data(settings)
|
87
|
+
|
88
|
+
# work - before 2016-nov-19
|
89
|
+
#cmd %Q(cd #{Config.root_path} && chef exec knife node delete #{settings.chef_node_name} -y -c #{chef_config_knife_path})
|
90
|
+
|
91
|
+
# clean chef client, node
|
92
|
+
#cmd %Q(cd #{Config.root_path} && rm -f #{settings.filename_chef_node_json} )
|
93
|
+
#cmd %Q(cd #{Config.root_path} && rm -f #{settings.filename_chef_client_json} )
|
94
|
+
end
|
95
|
+
|
96
|
+
|
97
|
+
###
|
98
|
+
def self.cmd(s)
|
99
|
+
Command.cmd(s)
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,188 @@
|
|
1
|
+
module DockerBuilder
|
2
|
+
module Provisioner
|
3
|
+
class Base
|
4
|
+
|
5
|
+
def self.run_provision_scripts_setup(settings)
|
6
|
+
#
|
7
|
+
setup_scripts = (settings['provision']['setup'] rescue [])
|
8
|
+
if setup_scripts
|
9
|
+
setup_scripts.each do |script|
|
10
|
+
_run_setup_script(settings, script)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
def self.run_provision_scripts_bootstrap(settings)
|
18
|
+
#
|
19
|
+
bootstrap_scripts = (settings['provision']['bootstrap'] rescue [])
|
20
|
+
if bootstrap_scripts
|
21
|
+
bootstrap_scripts.each do |script|
|
22
|
+
_run_bootstrap_script(settings, script)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
=begin
|
28
|
+
# commented - 2017-02-22
|
29
|
+
|
30
|
+
#
|
31
|
+
install_node_script_type = (settings['install']['node']['script_type'] rescue nil)
|
32
|
+
install_bootstrap_script = (settings['install']['bootstrap']['script'] rescue nil)
|
33
|
+
|
34
|
+
if install_node_script_type && install_node_script_type=='chef_recipe'
|
35
|
+
# run container and provision with chef
|
36
|
+
#_run_container_chef(settings)
|
37
|
+
|
38
|
+
# ???
|
39
|
+
#_provision_container_chef_recipe(settings)
|
40
|
+
|
41
|
+
elsif install_node_script_type && install_node_script_type=='shell'
|
42
|
+
# docker run
|
43
|
+
#create_and_run_container(settings)
|
44
|
+
|
45
|
+
# provision with shell script
|
46
|
+
run_shell_script_in_container(settings, "install.sh")
|
47
|
+
|
48
|
+
else
|
49
|
+
# no script for provision
|
50
|
+
#_run_container_docker(settings)
|
51
|
+
|
52
|
+
# docker run
|
53
|
+
#create_and_run_container(settings)
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
# bootstrap
|
58
|
+
if install_bootstrap_script
|
59
|
+
#script = settings['install']['bootstrap']['script'] || '/opt/bootstrap/bootstrap.sh'
|
60
|
+
|
61
|
+
# bootstsrap with shell script
|
62
|
+
run_bootstrap_shell_script_in_container(settings, install_bootstrap_script)
|
63
|
+
end
|
64
|
+
=end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
def self._run_bootstrap_script(settings, script)
|
69
|
+
puts "run BS script #{script}"
|
70
|
+
|
71
|
+
if script['type']=='shell' && script['run_from']=='host'
|
72
|
+
return _run_bootstrap_script_shell_from_host(settings, script)
|
73
|
+
elsif script['type']=='shell' && (script['run_from'].nil? || script['run_from']=='')
|
74
|
+
_run_bootstrap_script_shell_in_container(settings, script)
|
75
|
+
elsif script['type']=='chef'
|
76
|
+
# TODO: do it
|
77
|
+
_run_bootstrap_script_chef(settings, script)
|
78
|
+
end
|
79
|
+
|
80
|
+
return nil
|
81
|
+
end
|
82
|
+
|
83
|
+
|
84
|
+
### provision - setup
|
85
|
+
|
86
|
+
def self._run_setup_script(settings, script)
|
87
|
+
puts "run script #{script}"
|
88
|
+
if script['type']=='shell'
|
89
|
+
return _run_setup_script_shell_from_host(settings, script)
|
90
|
+
end
|
91
|
+
|
92
|
+
return nil
|
93
|
+
end
|
94
|
+
|
95
|
+
def self._run_setup_script_shell_from_host(settings, script)
|
96
|
+
cmd %Q(cd #{settings.dir_server_root} && #{script['script']} )
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
def self._provision_container_chef_recipe(settings)
|
101
|
+
puts "provisioning container #{settings.container_name}"
|
102
|
+
|
103
|
+
#cmd %Q(SERVER_NAME=#{settings.name} chef-client -z -N #{settings.name} #{settings.name}/cookbooks/#{settings.name}/ )
|
104
|
+
end
|
105
|
+
|
106
|
+
|
107
|
+
|
108
|
+
### provision - bootstrap
|
109
|
+
|
110
|
+
def self._run_bootstrap_script_shell_from_host(settings, script)
|
111
|
+
cmd %Q(cd #{settings.dir_server_root} && #{script['script']} )
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
|
116
|
+
def self._run_bootstrap_script_shell_in_container(settings, script)
|
117
|
+
script_path = script['script']
|
118
|
+
# exec
|
119
|
+
cmd %Q(docker exec #{settings.container_name} #{script_path} )
|
120
|
+
end
|
121
|
+
|
122
|
+
=begin
|
123
|
+
def self._install_container_provision_host(settings)
|
124
|
+
script_type = (settings['install']['host']['script_type'] rescue nil)
|
125
|
+
return true unless script_type
|
126
|
+
|
127
|
+
# run provision script on the host machine
|
128
|
+
if script_type=='chef_recipe'
|
129
|
+
return _install_container_provision_host_chef_recipe(settings)
|
130
|
+
else
|
131
|
+
# do nothing
|
132
|
+
end
|
133
|
+
|
134
|
+
true
|
135
|
+
end
|
136
|
+
|
137
|
+
#
|
138
|
+
def self._install_container_provision_host_chef_recipe(settings)
|
139
|
+
# run script on host machine
|
140
|
+
script_name = settings['install']['host']['script'] || 'install_host'
|
141
|
+
|
142
|
+
# check script exists
|
143
|
+
#script_path = "#{settings.name}/cookbooks/#{settings.name}/recipes/#{script_name}.rb"
|
144
|
+
#f = File.expand_path('.', script_path)
|
145
|
+
|
146
|
+
#if !File.exists?(f)
|
147
|
+
# puts "script not found: #{f}. Skipping"
|
148
|
+
# return false
|
149
|
+
#end
|
150
|
+
|
151
|
+
#puts "pwd= #{Dir.pwd}"
|
152
|
+
#puts "root = #{Config.root_path}"
|
153
|
+
#exit
|
154
|
+
|
155
|
+
#
|
156
|
+
res_chef = run_chef_recipe_server_recipe(settings, script_name)
|
157
|
+
#cmd %Q(SERVER_NAME=#{settings.name} chef-client -z -N #{settings.name} --override-runlist 'recipe[#{settings.name}::#{script_name}]' )
|
158
|
+
|
159
|
+
return true
|
160
|
+
end
|
161
|
+
|
162
|
+
|
163
|
+
=end
|
164
|
+
|
165
|
+
### helpers - shell
|
166
|
+
|
167
|
+
def self.run_shell_script_in_container(settings, script_name)
|
168
|
+
script_path = settings.make_path_full("scripts/#{script_name}")
|
169
|
+
|
170
|
+
# copy
|
171
|
+
cmd %Q(cd #{Config.root_path} && docker cp #{script_path} #{settings.container_name}:/tmp/#{script_name} )
|
172
|
+
|
173
|
+
# exec
|
174
|
+
cmd %Q(docker exec #{settings.container_name} chmod +x /tmp/#{script_name} )
|
175
|
+
cmd %Q(docker exec #{settings.container_name} /tmp/#{script_name} )
|
176
|
+
end
|
177
|
+
|
178
|
+
|
179
|
+
###
|
180
|
+
def self.cmd(s)
|
181
|
+
Command.cmd(s)
|
182
|
+
end
|
183
|
+
|
184
|
+
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
@@ -2,6 +2,7 @@ module DockerBuilder
|
|
2
2
|
|
3
3
|
class ServerSettings
|
4
4
|
attr_accessor :properties
|
5
|
+
attr_accessor :common_config
|
5
6
|
|
6
7
|
def get_binding
|
7
8
|
return binding()
|
@@ -42,6 +43,23 @@ class ServerSettings
|
|
42
43
|
end
|
43
44
|
|
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
|
+
|
45
63
|
def set(name, v)
|
46
64
|
properties[name] = v
|
47
65
|
end
|
@@ -91,20 +109,25 @@ class ServerSettings
|
|
91
109
|
|
92
110
|
|
93
111
|
###
|
112
|
+
def properties_common(opt_name)
|
113
|
+
common_config.options[opt_name] || common_config.send(opt_name)
|
114
|
+
end
|
115
|
+
|
94
116
|
def prefix
|
95
|
-
properties['common']['prefix']
|
117
|
+
#properties['common']['prefix']
|
118
|
+
properties_common('prefix')
|
96
119
|
end
|
97
120
|
|
98
121
|
def container_prefix
|
99
|
-
"#{
|
122
|
+
"#{prefix}#{properties_common('container_prefix')}"
|
100
123
|
end
|
101
124
|
|
102
125
|
def image_prefix
|
103
|
-
"#{
|
126
|
+
"#{prefix}#{properties_common('image_prefix')}"
|
104
127
|
end
|
105
128
|
|
106
129
|
def service_prefix
|
107
|
-
"#{
|
130
|
+
"#{prefix}#{properties_common('service_prefix')}"
|
108
131
|
end
|
109
132
|
|
110
133
|
|
@@ -159,9 +182,10 @@ class ServerSettings
|
|
159
182
|
|
160
183
|
#res = "$PWD/servers/#{self.name}/#{s}"
|
161
184
|
res = File.expand_path(s, dir_server_root)
|
185
|
+
#res = File.expand_path(s, properties_common('root_path'))
|
162
186
|
|
163
187
|
elsif v =~ /^\/\//
|
164
|
-
res =
|
188
|
+
res = dir_data_base+(v.gsub /^\/\//, '')
|
165
189
|
elsif v =~ /^\//
|
166
190
|
res = v
|
167
191
|
else
|
@@ -171,8 +195,12 @@ class ServerSettings
|
|
171
195
|
res
|
172
196
|
end
|
173
197
|
|
198
|
+
def dir_data_base
|
199
|
+
"#{properties_common('dir_data')}"
|
200
|
+
end
|
201
|
+
|
174
202
|
def dir_data
|
175
|
-
"#{
|
203
|
+
"#{properties_common('dir_data')}#{self.name}/"
|
176
204
|
end
|
177
205
|
|
178
206
|
|
@@ -189,8 +217,6 @@ class ServerSettings
|
|
189
217
|
def docker_volumes
|
190
218
|
a = properties['docker']['volumes'] || []
|
191
219
|
|
192
|
-
#puts "volumes a=#{a}"
|
193
|
-
|
194
220
|
# fix paths
|
195
221
|
res = a.map do |r|
|
196
222
|
path_local = volume_path_local(r[0])
|
@@ -198,8 +224,6 @@ class ServerSettings
|
|
198
224
|
[path_local, r[1]]
|
199
225
|
end
|
200
226
|
|
201
|
-
#puts "volumes: #{res}"
|
202
|
-
|
203
227
|
res
|
204
228
|
end
|
205
229
|
|
@@ -334,58 +358,4 @@ class ServerSettings
|
|
334
358
|
|
335
359
|
end
|
336
360
|
|
337
|
-
|
338
|
-
|
339
|
-
class Settings
|
340
|
-
|
341
|
-
def self.load_settings_for_server(name, opts={})
|
342
|
-
settings = ServerSettings.new
|
343
|
-
|
344
|
-
settings.set 'name', name
|
345
|
-
|
346
|
-
# set from main Config
|
347
|
-
Config.servers[name].each do |k,v|
|
348
|
-
settings.properties[k]=v
|
349
|
-
end
|
350
|
-
|
351
|
-
|
352
|
-
#puts "current=#{File.dirname(__FILE__)}"
|
353
|
-
#puts "ff=#{file_base_settings}"
|
354
|
-
|
355
|
-
#
|
356
|
-
#t = File.read(file_base_settings) rescue ''
|
357
|
-
#eval(t, settings.get_binding)
|
358
|
-
|
359
|
-
|
360
|
-
#
|
361
|
-
f = file_settings_for_server(name)
|
362
|
-
#puts "loading server settings from #{f}"
|
363
|
-
t = File.read(f) rescue ''
|
364
|
-
eval(t, settings.get_binding)
|
365
|
-
|
366
|
-
#
|
367
|
-
settings.properties['name'] ||= name
|
368
|
-
|
369
|
-
# from common config
|
370
|
-
settings.properties['common'] = Config.options[:common]
|
371
|
-
|
372
|
-
settings
|
373
|
-
end
|
374
|
-
|
375
|
-
|
376
|
-
### helpers
|
377
|
-
|
378
|
-
def self.file_settings_for_server(name)
|
379
|
-
#File.join(File.dirname(__FILE__), '..', 'config', "#{name}.rb")
|
380
|
-
File.join(Config.root_path, 'servers', name, 'config.rb')
|
381
|
-
end
|
382
|
-
|
383
|
-
def self.file_base_settings
|
384
|
-
File.join(File.dirname(__FILE__), '..', 'config' ,'common.rb')
|
385
|
-
end
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
end
|
391
361
|
end
|