Skip to content

Commit

Permalink
Initial support for systemd/upstart handling for container LWRP
Browse files Browse the repository at this point in the history
  • Loading branch information
bflad committed Nov 25, 2013
1 parent f0b70ad commit 8f1c9c1
Showing 1 changed file with 165 additions and 5 deletions.
170 changes: 165 additions & 5 deletions providers/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def load_current_resource
next unless dps_line.include?(new_resource.image)
next if new_resource.command && !dps_line.include?(new_resource.command)
container_ps = dps_line.split(/\s\s+/)
container_name = container_ps[6] || container_ps[5]
@current_resource.container_name(container_name)
@current_resource.id(container_ps[0])
@current_resource.running(true) if container_ps[4].include?('Up')
break
Expand Down Expand Up @@ -62,6 +64,24 @@ def load_current_resource
end
end

def cidfile
if service?
new_resource.cidfile || "/var/run/#{service_name}.cid"
else
new_resource.cidfile
end
end

def container_name
# TODO: remove when Fedora package supports named containers
return nil if node['platform'] == 'fedora' && node['docker']['install_type'] == 'package'
if service?
new_resource.container_name || new_resource.image.gsub(/^.*\//, '')
else
new_resource.container_name
end
end

def exists?
@current_resource.id
end
Expand All @@ -82,16 +102,21 @@ def remove
'link' => new_resource.link
)
shell_out("docker rm #{rm_args} #{current_resource.id}", :timeout => new_resource.cmd_timeout)
service_remove if service?
end

def restart
shell_out("docker restart #{current_resource.id}", :timeout => new_resource.cmd_timeout)
if service?
service_restart
else
shell_out("docker restart #{current_resource.id}", :timeout => new_resource.cmd_timeout)
end
end

def run
run_args = cli_args(
'c' => new_resource.cpu_shares,
'cidfile' => new_resource.cidfile,
'cidfile' => cidfile,
'd' => new_resource.detach,
'dns' => [*new_resource.dns],
'e' => [*new_resource.env],
Expand All @@ -102,7 +127,7 @@ def run
'link' => new_resource.link,
'lxc-conf' => [*new_resource.lxc_conf],
'm' => new_resource.memory,
'name' => new_resource.container_name,
'name' => container_name,
'p' => [*port],
'P' => new_resource.publish_exposed_ports,
'privileged' => new_resource.privileged,
Expand All @@ -114,26 +139,161 @@ def run
'w' => new_resource.working_directory
)
dr = shell_out("docker run #{run_args} #{new_resource.image} #{new_resource.command}", :timeout => new_resource.cmd_timeout)
dr.error!
new_resource.id(dr.stdout.chomp)
service_create if service?
end

def running?
@current_resource.running
end

def service?
new_resource.init_type
end

def service_action(actions)
service service_name do
case new_resource.init_type
when 'systemd'
provider Chef::Provider::Service::Systemd
when 'upstart'
provider Chef::Provider::Service::Upstart
end
supports :status => true, :restart => true, :reload => true
action actions
end
end

def service_create
case new_resource.init_type
when 'systemd'
service_create_systemd
when 'upstart'
service_create_upstart
end
end

def service_create_systemd
template "/usr/lib/systemd/system/#{service_name}.socket" do
source 'docker-container.socket.erb'
cookbook new_resource.cookbook
mode '0644'
owner 'root'
group 'root'
variables(
:service_name => service_name,
:sockets => sockets
)
not_if port.empty?
end

template "/usr/lib/systemd/system/#{service_name}.service" do
source 'docker-container.service.erb'
cookbook new_resource.cookbook
mode '0644'
owner 'root'
group 'root'
variables(
:cidfile => cidfile,
:cmd_timeout => new_resource.cmd_timeout,
:service_name => service_name
)
end

service_action([:start, :enable])
end

def service_create_upstart
template "/etc/init/#{service_name}.conf" do
source 'docker-container.conf.erb'
cookbook new_resource.cookbook
mode '0600'
owner 'root'
group 'root'
variables(
:cidfile => cidfile,
:cmd_timeout => new_resource.cmd_timeout,
:service_name => service_name
)
end

service_action([:start, :enable])
end

def service_name
# TODO: remove when Fedora package supports named containers
return new_resource.image.gsub(/^.*\//, '') if node['platform'] == 'fedora' && node['docker']['install_type'] == 'package'
container_name
end

def service_remove
case new_resource.init_type
when 'systemd'
service_remove_systemd
when 'upstart'
service_remove_upstart
end
end

def service_remove_systemd
service_action([:stop, :disable])

file "/usr/lib/systemd/system/#{service_name}.socket" do
action :delete
end

file "/usr/lib/systemd/system/#{service_name}.service" do
action :delete
end
end

def service_remove_upstart
service_action([:stop, :disable])

file "/etc/init/#{service_name}" do
action :delete
end
end

def service_restart
service_action([:restart])
end

def service_start
service_action([:start])
end

def service_stop
service_action([:stop])
end

def sockets
return [] if port.empty?
[*port].map { |p| p.gsub!(/.*:/, '') }
end

def start
start_args = cli_args(
'a' => new_resource.attach,
'i' => new_resource.stdin
)
shell_out("docker start #{start_args} #{current_resource.id}", :timeout => new_resource.cmd_timeout)
if service?
service_create

This comment has been minimized.

Copy link
@jochenseeber

jochenseeber Nov 13, 2014

Shouldn't this not only call service_create but also start the service? Otherwise the container will not be running after calling start.

else
shell_out("docker start #{start_args} #{current_resource.id}", :timeout => new_resource.cmd_timeout)
end
end

def stop
stop_args = cli_args(
't' => new_resource.cmd_timeout
)
shell_out("docker stop #{stop_args} #{current_resource.id}", :timeout => (new_resource.cmd_timeout + 1))
if service?
service_stop
else
shell_out("docker stop #{stop_args} #{current_resource.id}", :timeout => (new_resource.cmd_timeout + 15))
end
end

def wait
Expand Down

0 comments on commit 8f1c9c1

Please sign in to comment.