Skip to content

Commit

Permalink
refactor of docker-wait-ready
Browse files Browse the repository at this point in the history
  • Loading branch information
chasebolt committed Apr 12, 2016
1 parent 5922e6d commit d31b5f0
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 222 deletions.
33 changes: 33 additions & 0 deletions libraries/docker_service_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ class DockerServiceBase < DockerBase
# logging
property :logfile, String, default: '/var/log/docker.log'

# docker-wait-ready timeout
property :service_timeout, Integer, default: 20

allowed_actions :start, :stop, :restart

alias label labels
Expand All @@ -76,5 +79,35 @@ class DockerServiceBase < DockerBase
alias tlskey tls_server_key
alias tlsverify tls_verify
alias run_group group

declare_action_class.class_eval do
def libexec_dir
return '/usr/libexec/docker' if node['platform_family'] == 'rhel'
'/usr/lib/docker'
end

def create_docker_wait_ready
directory libexec_dir do
owner 'root'
group 'root'
mode '0755'
action :create
end

template "#{libexec_dir}/#{docker_name}-wait-ready" do
source 'default/docker-wait-ready.erb'
owner 'root'
group 'root'
mode '0755'
variables(
docker_cmd: docker_cmd,
libexec_dir: libexec_dir,
service_timeout: service_timeout
)
cookbook 'docker'
action :create
end
end
end
end
end
19 changes: 3 additions & 16 deletions libraries/docker_service_manager_execute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,9 @@ class DockerServiceManagerExecute < DockerServiceBase
action :run
end

# loop until docker docker is available
bash "docker-wait-ready #{name}" do
code <<-EOF
timeout=0
while [ $timeout -lt 20 ]; do
#{docker_cmd} ps | head -n 1 | grep ^CONTAINER
if [ $? -eq 0 ]; then
break
fi
((timeout++))
sleep 1
done
[[ $timeout -eq 20 ]] && exit 1
exit 0
EOF
not_if "#{docker_cmd} ps | head -n 1 | grep ^CONTAINER"
create_docker_wait_ready
execute 'docker-wait-ready' do
command "#{libexec_dir}/#{docker_name}-wait-ready"
end
end

Expand Down
35 changes: 3 additions & 32 deletions libraries/docker_service_manager_systemd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,8 @@ class DockerServiceManagerSystemd < DockerServiceBase
node['platform_version'].to_f >= 15.04
end

property :service_timeout, Integer, default: 20

def libexec_dir
return '/usr/libexec/docker' if node['platform_family'] == 'rhel'
'/usr/lib/docker'
end

action :start do
directory libexec_dir do
owner 'root'
group 'root'
mode '0755'
action :create
end

# this script is called by the main systemd unit file, and
# spins around until the service is actually up and running.
template "#{libexec_dir}/#{docker_name}-wait-ready" do
source 'systemd/docker-wait-ready.erb'
owner 'root'
group 'root'
mode '0755'
variables(
docker_cmd: docker_cmd,
libexec_dir: libexec_dir,
service_timeout: service_timeout
)
cookbook 'docker'
action :create
end
create_docker_wait_ready

# stock systemd unit file
template "/lib/systemd/system/#{docker_name}.service" do
Expand All @@ -55,7 +27,7 @@ def libexec_dir
mode '0644'
variables(
docker_name: docker_name,
docker_socket: connect_socket
docker_socket: connect_socket.sub(%r{unix://|fd://}, '')
)
cookbook 'docker'
action :create
Expand Down Expand Up @@ -83,8 +55,7 @@ def libexec_dir
variables(
config: new_resource,
docker_daemon_cmd: docker_daemon_cmd,
docker_name: docker_name,
libexec_dir: libexec_dir
docker_wait_ready: "#{libexec_dir}/#{docker_name}-wait-ready"
)
cookbook 'docker'
notifies :run, 'execute[systemctl daemon-reload]', :immediately
Expand Down
25 changes: 3 additions & 22 deletions libraries/docker_service_manager_sysvinit_debian.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class DockerServiceManagerSysvinitDebian < DockerServiceBase
end

action :start do
create_docker_wait_ready
create_init
create_service
wait_ready
end

action :stop do
Expand Down Expand Up @@ -56,10 +56,10 @@ def create_init
mode '0755'
variables(
docker_name: docker_name,
docker_daemon_arg: docker_daemon_arg
docker_daemon_arg: docker_daemon_arg,
docker_wait_ready: "#{libexec_dir}/#{docker_name}-wait-ready"
)
cookbook 'docker'
not_if { docker_name == 'docker' && ::File.exist?('/etc/init.d/docker') }
action :create
end

Expand All @@ -82,25 +82,6 @@ def create_service
action [:enable, :start]
end
end

def wait_ready
bash "docker-wait-ready #{name}" do
code <<-EOF
timeout=0
while [ $timeout -lt 20 ]; do
#{docker_cmd} ps | head -n 1 | grep ^CONTAINER
if [ $? -eq 0 ]; then
break
fi
((timeout++))
sleep 1
done
[[ $timeout -eq 20 ]] && exit 1
exit 0
EOF
not_if "#{docker_cmd} ps | head -n 1 | grep ^CONTAINER"
end
end
end
end
end
25 changes: 3 additions & 22 deletions libraries/docker_service_manager_sysvinit_rhel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class DockerServiceManagerSysvinitRhel < DockerServiceBase
end

action :start do
create_docker_wait_ready
create_init
create_service
wait_ready
end

action :stop do
Expand Down Expand Up @@ -52,10 +52,10 @@ def create_init
mode '0755'
variables(
docker_name: docker_name,
docker_daemon_arg: docker_daemon_arg
docker_daemon_arg: docker_daemon_arg,
docker_wait_ready: "#{libexec_dir}/#{docker_name}-wait-ready"
)
cookbook 'docker'
not_if { docker_name == 'docker' && ::File.exist?('/etc/init.d/docker') }
action :create
end

Expand All @@ -77,25 +77,6 @@ def create_service
action [:enable, :start]
end
end

def wait_ready
bash "docker-wait-ready #{name}" do
code <<-EOF
timeout=0
while [ $timeout -lt 20 ]; do
#{docker_cmd} ps | head -n 1 | grep ^CONTAINER
if [ $? -eq 0 ]; then
break
fi
((timeout++))
sleep 1
done
[[ $timeout -eq 20 ]] && exit 1
exit 0
EOF
not_if "#{docker_cmd} ps | head -n 1 | grep ^CONTAINER"
end
end
end
end
end
23 changes: 3 additions & 20 deletions libraries/docker_service_manager_upstart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class DockerServiceManagerUpstart < DockerServiceBase
provides :docker_service_manager, platform: 'linuxmint'

action :start do
create_docker_wait_ready

template "/etc/init/#{docker_name}.conf" do
source 'upstart/docker.conf.erb'
owner 'root'
Expand All @@ -14,10 +16,9 @@ class DockerServiceManagerUpstart < DockerServiceBase
variables(
docker_name: docker_name,
docker_daemon_arg: docker_daemon_arg,
docker_socket: connect_socket
docker_wait_ready: "#{libexec_dir}/#{docker_name}-wait-ready"
)
cookbook 'docker'
not_if { docker_name == 'default' && ::File.exist?('/etc/init/docker.conf') }
action :create
end

Expand All @@ -37,24 +38,6 @@ class DockerServiceManagerUpstart < DockerServiceBase
supports status: true
action :start
end

# FIXME: why do we need this? This should be handled in init
bash "docker-wait-ready #{name}" do
code <<-EOF
timeout=0
while [ $timeout -lt 20 ]; do
#{docker_cmd} ps | head -n 1 | grep ^CONTAINER
if [ $? -eq 0 ]; then
break
fi
((timeout++))
sleep 1
done
[[ $timeout -eq 20 ]] && exit 1
exit 0
EOF
not_if "#{docker_cmd} ps | head -n 1 | grep ^CONTAINER"
end
end

action :stop do
Expand Down
2 changes: 1 addition & 1 deletion libraries/helpers_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def connect_socket
c_b = 3 unless c_b
c_a <=> c_b
end
sorted.first.sub(%r{unix://|fd://}, '')
sorted.first
end

def coerce_host(v)
Expand Down
2 changes: 1 addition & 1 deletion templates/default/systemd/docker.service-override.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ ExecStartPre=/sbin/sysctl -w net.ipv4.ip_forward=1
ExecStartPre=/sbin/sysctl -w net.ipv6.conf.all.forwarding=1
<% end %>
ExecStart=<%= @docker_daemon_cmd %>
ExecStartPost=<%= @libexec_dir %>/<%= @docker_name %>-wait-ready
ExecStartPost=<%= @docker_wait_ready %>
Restart=always
MountFlags=private
LimitNOFILE=1048576
Expand Down
Loading

0 comments on commit d31b5f0

Please sign in to comment.