diff --git a/README.md b/README.md
index 851a369b..0106c49b 100644
--- a/README.md
+++ b/README.md
@@ -103,6 +103,24 @@ Installs and configures [Consul][1].
address that we advertise to other nodes in the cluster |
Value of bind_addr |
+
+ ['consul']['init_style'] |
+ String |
+ Service init mode for running consul as: init or runit |
+ init |
+
+
+ ['consul']['service_user'] |
+ String |
+ For runit service: run consul as this user (init uses 'root') |
+ consul |
+
+
+ ['consul']['servers'] |
+ String |
+ For runit service: run consul as this group (init uses 'root') |
+ consul |
+
### Consul UI Attributes
diff --git a/attributes/default.rb b/attributes/default.rb
index 566ce437..bb25d0ee 100644
--- a/attributes/default.rb
+++ b/attributes/default.rb
@@ -33,6 +33,9 @@
default[:consul][:data_dir] = '/var/lib/consul'
default[:consul][:config_dir] = '/etc/consul.d'
default[:consul][:servers] = []
+default[:consul][:init_style] = 'init' # 'init', 'runit'
+default[:consul][:service_user] = 'consul'
+default[:consul][:service_group] = 'consul'
# UI attributes
default[:consul][:client_addr] = '0.0.0.0'
diff --git a/metadata.rb b/metadata.rb
index 4e259bc1..685356e3 100644
--- a/metadata.rb
+++ b/metadata.rb
@@ -4,7 +4,7 @@
license 'Apache v2.0'
description 'Installs/Configures consul'
long_description 'Installs/Configures consul'
-version '0.2.2'
+version '0.2.3'
recipe 'consul', 'Installs and starts consul service.'
recipe 'consul::binary_install', 'Installs consul service from binary.'
@@ -21,4 +21,4 @@
depends 'ark', '~> 0.8.0'
depends 'golang', '~> 1.3.0'
-%w(yum-repoforge).each { |cb| depends cb }
+%w(yum-repoforge runit).each { |cb| depends cb }
diff --git a/recipes/service.rb b/recipes/service.rb
index a3635774..ee220719 100644
--- a/recipes/service.rb
+++ b/recipes/service.rb
@@ -1,5 +1,47 @@
require 'json'
+# Configure directories
+consul_directories = []
+consul_directories << node[:consul][:config_dir]
+consul_directories << "/var/lib/consul"
+
+# Select service user & group
+case node[:consul][:init_style]
+when 'runit'
+ consul_user = node[:consul][:service_user]
+ consul_group = node[:consul][:service_group]
+ consul_directories << "/var/log/consul"
+else
+ consul_user = 'root'
+ consul_group = 'root'
+end
+
+# Create service user
+user "consul service user: #{consul_user}" do
+ not_if { consul_user == 'root' }
+ username consul_user
+ home "/dev/null"
+ shell "/bin/false"
+ comment "consul service user"
+end
+
+# Create service group
+group "consul service group: #{consul_group}" do
+ not_if { consul_group == 'root' }
+ group_name consul_group
+ members consul_user
+ append true
+end
+
+# Create service directories
+consul_directories.each do |dirname|
+ directory dirname do
+ owner consul_user
+ group consul_group
+ mode 0755
+ end
+end
+
# Determine service params
service_config = {}
service_config['data_dir'] = node[:consul][:data_dir]
@@ -23,7 +65,7 @@
end
copy_params = [
- :bind_addr, :datacenter, :domain, :log_level, :node_name, :advertise_addr
+ :bind_addr, :datacenter, :domain, :log_level, :node_name, :advertise_addr
]
copy_params.each do |key|
if node[:consul][key]
@@ -31,28 +73,44 @@
end
end
-directory node[:consul][:config_dir]
-
-template '/etc/init.d/consul' do
- source 'consul-init.erb'
- mode 0755
- variables(
- consul_binary: "#{node[:consul][:install_dir]}/consul",
- config_dir: node[:consul][:config_dir],
- )
-end
-
file node[:consul][:config_dir] + "/default.json" do
- user "root"
- group "root"
- mode "0600"
+ user consul_user
+ group consul_group
+ mode 0600
action :create
content JSON.pretty_generate(service_config, quirks_mode: true)
end
-service 'consul' do
- supports status: true, restart: true, reload: true
- action [:enable, :start]
- subscribes :reload, "file[#{node[:consul][:config_dir]}/default.json]", :immediately
- subscribes :restart, "template[/etc/init.d/consul]", :immediately
+case node[:consul][:init_style]
+when 'init'
+ template '/etc/init.d/consul' do
+ source 'consul-init.erb'
+ mode 0755
+ variables(
+ consul_binary: "#{node[:consul][:install_dir]}/consul",
+ config_dir: node[:consul][:config_dir],
+ )
+ end
+
+ service 'consul' do
+ supports status: true, restart: true, reload: true
+ action [:enable, :start]
+ subscribes :reload, "file[#{node[:consul][:config_dir]}/default.json]", :immediately
+ subscribes :restart, "template[/etc/init.d/consul]", :immediately
+ end
+
+when 'runit'
+ include_recipe 'runit'
+
+ runit_service 'consul' do
+ supports status: true, restart: true, reload: true
+ action [:enable, :start]
+ subscribes :reload, "file[#{node[:consul][:config_dir]}/default.json]", :immediately
+ log true
+ options(
+ consul_binary: "#{node[:consul][:install_dir]}/consul",
+ config_dir: node[:consul][:config_dir],
+ )
+ end
+
end
diff --git a/templates/default/sv-consul-log-run.erb b/templates/default/sv-consul-log-run.erb
new file mode 100644
index 00000000..42cd1197
--- /dev/null
+++ b/templates/default/sv-consul-log-run.erb
@@ -0,0 +1,5 @@
+#!/bin/sh
+exec chpst -u '<%= node[:consul][:service_user] %>':'<%= node[:consul][:service_group] %>' \
+ svlogd \
+ /var/log/consul \
+# #
diff --git a/templates/default/sv-consul-run.erb b/templates/default/sv-consul-run.erb
new file mode 100644
index 00000000..3cbb8fce
--- /dev/null
+++ b/templates/default/sv-consul-run.erb
@@ -0,0 +1,6 @@
+#!/bin/sh
+exec chpst -u '<%= node[:consul][:service_user] %>':'<%= node[:consul][:service_group] %>' \
+ <%= @options[:consul_binary] %> \
+ agent \
+ -config-dir '<%= @options[:config_dir] %>' \
+# #