2 # Cookbook Name:: docker-grid
5 # Copyright 2016-2018, whitestar
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
20 # https://dcos.io/docs/1.8/administration/installing/custom/system-requirements/
22 platform = node['platform']
23 platform_family = node['platform_family']
24 platform_version = node['platform_version']
26 install_flavor = node['docker-grid']['install_flavor']
27 override_apt_line = node['docker-grid']['apt_repo']['override_apt_line']
28 if !override_apt_line.nil? && !override_apt_line.empty? \
29 && override_apt_line.include?(node['docker-grid']['dockerproject']['apt_new_repo_url'])
30 Chef::Log.warn('This docker-grid::engine recipe uses the Docker new repository by the `override_apt_line` attribute.')
31 node.force_override['docker-grid']['dockerproject']['enable_new_repo'] = true
33 enable_new_repo = node['docker-grid']['dockerproject']['enable_new_repo']
35 engine_conf = node['docker-grid']['engine']
36 docker_ver = engine_conf['version']
37 docker_ver = '' if docker_ver.nil? || docker_ver == 'latest'
39 Chef::Log.warn('This docker-grid::engine installs the latest Docker engine.')
41 Chef::Log.info("This docker-grid::engine installs the Docker engine: #{docker_ver}.")
43 storage_driver = engine_conf['storage-driver']
44 userns_remap = engine_conf['userns-remap']
46 if engine_conf['skip_setup']
47 log 'Skip the Docker Engine setup.'
51 ::Chef::Recipe.send(:include, DockerGrid::Helper)
52 ::Chef::Recipe.send(:include, PlatformUtils::Helper)
53 ::Chef::Recipe.send(:include, PlatformUtils::VirtUtils)
58 resources(package: pkg) rescue package pkg do
63 systemctl_daemon_reload = 'systemctl_daemon-reload'
64 resources(bash: systemctl_daemon_reload) rescue bash systemctl_daemon_reload do
66 systemctl daemon-reload
71 # https://docs.docker.com/engine/userguide/storagedriver/selectadriver/
72 if shell_out("cat /etc/mtab | grep -E '\s+/\s+zfs\s+'").exitstatus.zero?
73 if container_guest_node?
74 Chef::Log.warn('This node is running in the Linux container with ZFS, set the storage-driver to vfs as a fallback.')
75 node.override['docker-grid']['engine']['storage-driver'] = 'vfs'
77 Chef::Log.warn('This node is running on ZFS, set the storage-driver to zfs.')
78 node.override['docker-grid']['engine']['storage-driver'] = 'zfs'
82 if storage_driver == 'overlay2'
83 if !docker_ver.empty? && gem_ver(docker_ver) < gem_ver('1.12')
84 Chef::Application.fatal!('Docker version must be 1.12 or later for overlay2 storage driver.') # and exit.
87 load_kernel_module('overlay') if storage_driver =~ /overlay2?/
89 if !userns_remap.nil? && !userns_remap.empty?
90 if !docker_ver.empty? && gem_ver(docker_ver) < gem_ver('1.10')
91 Chef::Application.fatal!('Docker version must be 1.10 or later for userns-remap.') # and exit.
94 include_recipe 'platform_utils::kernel_user_namespace'
96 remap_user = userns_remap == 'default' ? 'dockremap' : userns_remap
99 'resource' => 'service[docker]',
102 ::Chef::Recipe.send(:include, PlatformUtils::Helper)
103 append_subusers([remap_user], notifies_conf)
106 file '/etc/systemd/system/docker.service.d/override.conf' do
110 bash 'clean_up_docker0_bridge' do
112 if brctl show | grep docker0; then
113 ip link set docker0 down
116 # https://github.com/docker/docker/issues/23630
117 if [ -d /var/lib/docker/network ]; then
118 rm -rf /var/lib/docker/network
126 ex_enablerepo = node['docker-grid']['dockerproject']['yum_new_repo_extra_enablerepo']
128 if storage_driver == 'devicemapper'
131 'device-mapper-persistent-data',
134 resources(package: pkg) rescue package pkg do
140 if install_flavor == 'dockerproject'
141 # https://dcos.io/docs/1.8/administration/installing/custom/system-requirements/install-docker-centos/
142 old_repo_action = nil
143 new_repo_action = nil
146 old_repo_action = :delete
147 new_repo_action = :create
152 old_repo_action = :create
153 new_repo_action = :delete
155 'docker-engine-selinux',
164 template "/etc/yum.repos.d/#{repo_file}" do
165 source "etc/yum.repos.d/#{repo_file}"
169 action repo_file == 'docker.repo' ? old_repo_action : new_repo_action
176 #'container-selinux',
179 resources(package: pkg) rescue package pkg do
180 action platform_family == 'debian' ? :purge : :remove
181 notifies :delete, 'file[/etc/systemd/system/docker.service.d/override.conf]', :immediately
182 notifies :run, 'bash[clean_up_docker0_bridge]', :immediately
186 # each repo of the new 'docker-ce-stable' and the old 'dockerrepo' is disabled by default to prevent automatic update.
187 enablerepos = enable_new_repo ? ['docker-ce-stable'] : ['dockerrepo']
188 enablerepos.push(ex_enablerepo) if enable_new_repo && !ex_enablerepo.nil? && !ex_enablerepo.empty?
190 resources(yum_package: pkg) rescue yum_package pkg do
193 version docker_ver unless docker_ver.empty?
194 options "--enablerepo=#{enablerepos.join(',')}"
195 notifies :run, 'bash[clean_up_docker0_bridge]', :before if pkg == 'docker-engine' || pkg == 'docker-ce'
201 'docker-engine-selinux',
204 resources(package: pkg) rescue package pkg do
205 action platform_family == 'debian' ? :purge : :remove
206 notifies :delete, 'file[/etc/systemd/system/docker.service.d/override.conf]', :immediately
207 notifies :run, 'bash[clean_up_docker0_bridge]', :immediately
214 resources(yum_package: pkg) rescue yum_package pkg do
217 version docker_ver unless docker_ver.empty?
218 notifies :run, 'bash[clean_up_docker0_bridge]', :before
222 template '/etc/sysconfig/docker' do
223 source 'etc/sysconfig/docker'
227 notifies :restart, 'service[docker]'
231 # https://docs.docker.com/engine/installation/linux/debian/
232 # https://docs.docker.com/engine/installation/linux/ubuntulinux/
234 'apt-transport-https',
238 'software-properties-common',
241 if storage_driver == 'aufs' \
242 && !container_guest_node?
243 if platform == 'debian'
247 elsif platform == 'ubuntu'
249 "linux-image-extra-#{node['os_version']}",
250 'linux-image-extra-virtual',
256 resources(package: pkg) rescue package pkg do
261 apt_get_update = 'apt-get_update'
262 resources(execute: apt_get_update) rescue execute apt_get_update do
263 command 'apt-get update'
267 if install_flavor == 'dockerproject'
268 pkg_name_removed = 'docker.io'
269 pkg_name = node['docker-grid']['dockerproject']['package_name']
271 apt_repo_config = node['docker-grid']['apt_repo']
272 apt_key_add_cmd = nil
275 apt_key_add_cmd = "curl -fsSL https://download.docker.com/linux/#{platform}/gpg | apt-key add -"
276 apt_key_name = 'Docker Release (CE deb)'
278 apt_key_add_cmd = "apt-key adv --keyserver #{apt_repo_config['keyserver']} --recv-keys #{apt_repo_config['recv-keys']}"
279 apt_key_name = 'Docker Release Tool (releasedocker)'
281 bash 'apt-key_adv_docker_tools_key' do
286 not_if "apt-key list 2>&1 | grep '#{apt_key_name}'"
289 template '/etc/apt/sources.list.d/docker.list' do
290 source 'etc/apt/sources.list.d/docker.list'
294 helpers Chef::Mixin::ShellOut
295 notifies :run, 'bash[apt-key_adv_docker_tools_key]', :before
296 notifies :run, "execute[#{apt_get_update}]", :immediately
300 pkg_name_removed = node['docker-grid']['dockerproject']['package_name']
301 pkg_name = 'docker.io'
304 # Pinning Docker version
305 template '/etc/apt/preferences.d/docker.pref' do
306 source 'etc/apt/preferences.d/docker.pref'
310 action :delete if docker_ver.empty?
316 resources(package: pkg_name_removed) rescue package pkg_name_removed do
317 action platform_family == 'debian' ? :purge : :remove
318 notifies :delete, 'file[/etc/systemd/system/docker.service.d/override.conf]', :immediately
319 notifies :run, 'bash[clean_up_docker0_bridge]', :immediately
322 resources(package: pkg_name) rescue package pkg_name do
324 options "-o Dpkg::Options::='--force-confnew'" if platform_family == 'debian'
325 options '--allow-downgrades' if platform == 'debian' || platform_version >= '16.04' # LTS (xenial)
326 options '--force-yes' if platform_version == '14.04' # LTS (trusty)
327 version docker_ver unless docker_ver.empty?
328 notifies :run, 'bash[clean_up_docker0_bridge]', :before
329 notifies :run, 'bash[apt-key_adv_docker_tools_key]', :before
330 notifies :run, "execute[#{apt_get_update}]", :before
335 docker_opts.push("--storage-driver=#{storage_driver}") if !storage_driver.nil? && !storage_driver.empty?
336 docker_opts.push("--userns-remap=#{userns_remap}") if !userns_remap.nil? && !userns_remap.empty?
338 extra_options = engine_conf['daemon_extra_options']
339 # for docker-engine package on RHEL: remove '-H fd://'
340 # https://github.com/docker/docker/issues/22847
341 if platform_family == 'rhel' || platform == 'debian' || (platform == 'ubuntu' && platform_version == '14.04')
342 # Note: docker_ver.empty? -> the latest version
343 if docker_ver.empty? \
344 || gem_ver(docker_ver) >= gem_ver('1.12')
345 extra_options = extra_options.gsub(%r{-H\sfd://}, '') # for frozen string.
348 if docker_ver.empty? \
349 || gem_ver(docker_ver) > gem_ver('18.06.99')
350 extra_options = extra_options.gsub(%r{-H\sfd://}, '-H unix://') # for frozen string.
353 docker_opts.push(extra_options) if !extra_options.nil? && !extra_options.empty?
355 init_package = node['init_package']
356 if init_package == 'systemd'
357 directory '/etc/systemd/system/docker.service.d' do
364 template '/etc/systemd/system/docker.service.d/override.conf' do
365 source 'etc/systemd/system/docker.service.d/override.conf'
370 docker_opts: docker_opts
372 not_if { install_flavor == 'os-repository' && platform_family == 'rhel' }
373 notifies :run, "bash[#{systemctl_daemon_reload}]", :immediately
374 notifies :restart, 'service[docker]'
376 elsif init_package == 'init' # for Ubuntu 14.04,...
377 template '/etc/default/docker' do
378 source 'etc/default/docker'
383 docker_opts: docker_opts
385 notifies :restart, 'service[docker]'
390 provider Chef::Provider::Service::Upstart if platform == 'ubuntu' && platform_version < '15.04'
391 action [:start, :enable]
392 subscribes :restart, 'execute[update-ca-certificates]', :delayed
395 users = engine_conf['users_allow']
397 members users unless users.empty?
404 'docker_containers_cleanup',
405 'docker_images_cleanup',
406 'docker_volumes_cleanup',
408 template "/usr/local/bin/#{script}" do
409 source "usr/local/bin/#{script}"
419 template "/etc/cron.d/#{srv}-local" do
420 source "etc/cron.d/#{srv}-local"
424 action :delete unless engine_conf['autopilot']['enabled']