From: Tom Hughes Date: Thu, 14 Jan 2016 19:28:34 +0000 (+0000) Subject: Add a systemd cookbook to manage unit files X-Git-Url: https://git.openstreetmap.org./chef.git/commitdiff_plain/e2d9c76e0d9a32f82f45b7a3d98199eb18405351 Add a systemd cookbook to manage unit files --- diff --git a/cookbooks/chef/metadata.rb b/cookbooks/chef/metadata.rb index 8db6f1bb9..6d2eb7940 100644 --- a/cookbooks/chef/metadata.rb +++ b/cookbooks/chef/metadata.rb @@ -10,3 +10,4 @@ depends "apt" depends "git" depends "ohai" depends "munin" +depends "systemd" diff --git a/cookbooks/chef/recipes/default.rb b/cookbooks/chef/recipes/default.rb index 2ab55ac60..2d9dc4fe0 100644 --- a/cookbooks/chef/recipes/default.rb +++ b/cookbooks/chef/recipes/default.rb @@ -111,17 +111,11 @@ directory "/var/log/chef" do end if node[:lsb][:release].to_f >= 15.10 - execute "systemctl-daemon-reload" do - action :nothing - command "systemctl daemon-reload" - end - - template "/etc/systemd/system/chef-client.service" do - source "chef-client.service.erb" - owner "root" - group "root" - mode 0644 - notifies :run, "execute[systemctl-daemon-reload]" + systemd_service "chef-client" do + description "Chef client" + after "network.target" + exec_start "/usr/bin/chef-client -i 1800 -s 20" + restart "on-failure" end service "chef-client" do @@ -129,7 +123,7 @@ if node[:lsb][:release].to_f >= 15.10 action [:enable, :start] supports :status => true, :restart => true, :reload => true subscribes :restart, "dpkg_package[chef]" - subscribes :restart, "template[/etc/systemd/system/chef-client.service]" + subscribes :restart, "systemd_service[chef-client]" subscribes :restart, "template[/etc/chef/client.rb]" subscribes :restart, "template[/etc/chef/report.rb]" end diff --git a/cookbooks/chef/templates/default/chef-client.service.erb b/cookbooks/chef/templates/default/chef-client.service.erb deleted file mode 100644 index 0b16414ff..000000000 --- a/cookbooks/chef/templates/default/chef-client.service.erb +++ /dev/null @@ -1,10 +0,0 @@ -[Unit] -Description=Chef Client -After=network.target - -[Service] -ExecStart=/usr/bin/chef-client -i 1800 -s 20 -Restart=on-failure - -[Install] -WantedBy=multi-user.target diff --git a/cookbooks/squid/metadata.rb b/cookbooks/squid/metadata.rb index 87a766776..9e9397017 100644 --- a/cookbooks/squid/metadata.rb +++ b/cookbooks/squid/metadata.rb @@ -5,3 +5,4 @@ license "Apache 2.0" description "Installs and configures squid" long_description IO.read(File.join(File.dirname(__FILE__), "README.md")) version "1.0.0" +depends "systemd" diff --git a/cookbooks/squid/recipes/default.rb b/cookbooks/squid/recipes/default.rb index 89377089f..c774bb5fb 100644 --- a/cookbooks/squid/recipes/default.rb +++ b/cookbooks/squid/recipes/default.rb @@ -41,24 +41,25 @@ directory "/etc/squid/squid.conf.d" do end if node[:lsb][:release].to_f >= 15.10 - execute "systemctl-daemon-reload" do - action :nothing - command "systemctl daemon-reload" - end - - template "/etc/systemd/system/squid.service" do - source "squid.service.erb" - owner "root" - group "root" - mode 0644 - notifies :run, "execute[systemctl-daemon-reload]" + systemd_service "squid" do + description "Squid caching proxy" + after ["network.target", "nss-lookup.target"] + limit_nofile 65536 + environment "SQUID_ARGS" => "-D" + environment_file "/etc/default/squid" + exec_start_pre "/usr/sbin/squid $SQUID_ARGS -z" + exec_start "/usr/sbin/squid -N $SQUID_ARGS" + exec_reload "/usr/sbin/squid -k reconfigure" + exec_stop "/usr/sbin/squid -k shutdown" + restart "on-failure" + timeout_sec 0 end service "squid" do provider Chef::Provider::Service::Systemd action [:enable, :start] supports :status => true, :restart => true, :reload => true - subscribes :restart, "template[/etc/systemd/system/squid.service]" + subscribes :restart, "systemd_service[squid]" subscribes :reload, "template[/etc/squid/squid.conf]" subscribes :restart, "template[/etc/default/squid]" subscribes :reload, "template[/etc/resolv.conf]" diff --git a/cookbooks/squid/templates/default/squid.service.erb b/cookbooks/squid/templates/default/squid.service.erb deleted file mode 100644 index 07b9bb276..000000000 --- a/cookbooks/squid/templates/default/squid.service.erb +++ /dev/null @@ -1,17 +0,0 @@ -[Unit] -Description=Squid caching proxy -After=network.target nss-lookup.target - -[Service] -LimitNOFILE=65536 -Environment=SQUID_ARGS=-D -EnvironmentFile=/etc/default/squid -ExecStartPre=/usr/sbin/squid $SQUID_ARGS -z -ExecStart=/usr/sbin/squid -N $SQUID_ARGS -ExecReload=/usr/sbin/squid -k reconfigure -ExecStop=/usr/sbin/squid -k shutdown -Restart=on-failure -TimeoutSec=0 - -[Install] -WantedBy=multi-user.target diff --git a/cookbooks/systemd/README.md b/cookbooks/systemd/README.md new file mode 100644 index 000000000..53a6d4463 --- /dev/null +++ b/cookbooks/systemd/README.md @@ -0,0 +1,3 @@ +# Systemd Cookbook + +This cookbook provides lightweight resources to manage systemd units. diff --git a/cookbooks/systemd/metadata.rb b/cookbooks/systemd/metadata.rb new file mode 100644 index 000000000..7902949d1 --- /dev/null +++ b/cookbooks/systemd/metadata.rb @@ -0,0 +1,7 @@ +name "systemd" +maintainer "OpenStreetMap Administrators" +maintainer_email "admins@openstreetmap.org" +license "Apache 2.0" +description "Installs and configures systemd units" +long_description IO.read(File.join(File.dirname(__FILE__), "README.md")) +version "1.0.0" diff --git a/cookbooks/systemd/resources/service.rb b/cookbooks/systemd/resources/service.rb new file mode 100644 index 000000000..e6fba41b5 --- /dev/null +++ b/cookbooks/systemd/resources/service.rb @@ -0,0 +1,71 @@ +# +# Cookbook Name:: systemd +# Resource:: systemd_service +# +# Copyright 2016, OpenStreetMap Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +default_action :create + +property :name, String +property :description, String, :required => true +property :after, [String, Array] +property :type, String, + :default => "simple", + :is => %w(simple forking oneshot dbus notify idle) +property :limit_nofile, Fixnum +property :environment, Hash, :default => {} +property :environment_file, String +property :exec_start_pre, String +property :exec_start, String, :required => true +property :exec_start_post, String +property :exec_stop, String +property :exec_reload, String +property :restart, String, + :is => %w(on-success on-failure on-abnormal on-watchdog on-abort always) +property :timeout_sec, Fixnum + +action :create do + template "/etc/systemd/system/#{name}.service" do + cookbook "systemd" + source "service.erb" + owner "root" + group "root" + mode 0644 + variables new_resource.to_hash + end + + execute "systemctl-reload-#{name}.service" do + action :nothing + command "systemctl daemon-reload" + user "root" + group "root" + subscribes :run, "template[/etc/systemd/system/#{name}.service]" + end +end + +action :delete do + file "/etc/systemd/system/#{name}.service" do + action :delete + end + + execute "systemctl-reload-#{name}.service" do + action :nothing + command "systemctl daemon-reload" + user "root" + group "root" + subscribes :run, "file[/etc/systemd/system/#{name}.service]" + end +end diff --git a/cookbooks/systemd/templates/default/service.erb b/cookbooks/systemd/templates/default/service.erb new file mode 100644 index 000000000..13983b02b --- /dev/null +++ b/cookbooks/systemd/templates/default/service.erb @@ -0,0 +1,39 @@ +[Unit] +Description=<%= @description %> +<% if @after -%> +After=<%= Array(@after).join(" ") %> +<% end -%> + +[Service] +Type=<%= @type %> +<% if @limit_nofile -%> +LimitNOFILE=<%= @limit_nofile %> +<% end -%> +<% @environment.each do |name,value| -%> +Environment="<%= name %>=<%= value %>" +<% end -%> +<% if @environment_file -%> +EnvironmentFile=<%= @environment_file %> +<% end -%> +<% if @exec_start_pre -%> +ExecStartPre=<%= @exec_start_pre %> +<% end -%> +ExecStart=<%= @exec_start %> +<% if @exec_start_post -%> +ExecStartPost=<%= @exec_start_post %> +<% end -%> +<% if @exec_stop -%> +ExecStop=<%= @exec_stop %> +<% end -%> +<% if @exec_reload -%> +ExecReload=<%= @exec_reload %> +<% end -%> +<% if @restart -%> +Restart=<%= @restart %> +<% end -%> +<% if @timeout_sec -%> +TimeoutSec=<%= @timeout_sec %> +<% end -%> + +[Install] +WantedBy=multi-user.target