]> git.openstreetmap.org Git - chef.git/commitdiff
Add a systemd cookbook to manage unit files
authorTom Hughes <tom@compton.nu>
Thu, 14 Jan 2016 19:28:34 +0000 (19:28 +0000)
committerTom Hughes <tom@compton.nu>
Thu, 14 Jan 2016 20:00:18 +0000 (20:00 +0000)
cookbooks/chef/metadata.rb
cookbooks/chef/recipes/default.rb
cookbooks/chef/templates/default/chef-client.service.erb [deleted file]
cookbooks/squid/metadata.rb
cookbooks/squid/recipes/default.rb
cookbooks/squid/templates/default/squid.service.erb [deleted file]
cookbooks/systemd/README.md [new file with mode: 0644]
cookbooks/systemd/metadata.rb [new file with mode: 0644]
cookbooks/systemd/resources/service.rb [new file with mode: 0644]
cookbooks/systemd/templates/default/service.erb [new file with mode: 0644]

index 8db6f1bb96f4ced45cbea075b8ee095cb386fe6c..6d2eb7940a4d60780ed5f9329fc315c9c9817f5e 100644 (file)
@@ -10,3 +10,4 @@ depends           "apt"
 depends           "git"
 depends           "ohai"
 depends           "munin"
+depends           "systemd"
index 2ab55ac606f41a8fd3d7eb9288864e0d36533f67..2d9dc4fe0f2c1f1561848416b2643e4d512dccf1 100644 (file)
@@ -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 (file)
index 0b16414..0000000
+++ /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
index 87a76677660beaccad3d70c3c446ee207654e060..9e9397017ca18aaf50c8a6afad0b090c8959858c 100644 (file)
@@ -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"
index 89377089f072bf912448f81180600dab10fc00f7..c774bb5fb983da99ba6836dc6265f5756c251b55 100644 (file)
@@ -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 (file)
index 07b9bb2..0000000
+++ /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 (file)
index 0000000..53a6d44
--- /dev/null
@@ -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 (file)
index 0000000..7902949
--- /dev/null
@@ -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 (file)
index 0000000..e6fba41
--- /dev/null
@@ -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 (file)
index 0000000..13983b0
--- /dev/null
@@ -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