Skip to content

Commit

Permalink
Merge pull request #16 from webcoyote/consul-runit
Browse files Browse the repository at this point in the history
Add support for runit
  • Loading branch information
John Bellone committed Jun 11, 2014
2 parents 74c0499 + 4e521cd commit 1718eb3
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 22 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,24 @@ Installs and configures [Consul][1].
<td>address that we advertise to other nodes in the cluster</td>
<td>Value of <i>bind_addr</i></td>
</tr>
<tr>
<td><tt>['consul']['init_style']</tt></td>
<td>String</td>
<td>Service init mode for running consul as: init or runit</td>
<td><tt>init</tt></td>
</tr>
<tr>
<td><tt>['consul']['service_user']</tt></td>
<td>String</td>
<td>For runit service: run consul as this user (init uses 'root')</td>
<td><tt>consul</tt></td>
</tr>
<tr>
<td><tt>['consul']['servers']</tt></td>
<td>String</td>
<td>For runit service: run consul as this group (init uses 'root')</td>
<td><tt>consul</tt></td>
</tr>
</table>

### Consul UI Attributes
Expand Down
3 changes: 3 additions & 0 deletions attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
Expand All @@ -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 }
98 changes: 78 additions & 20 deletions recipes/service.rb
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -23,36 +65,52 @@
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]
service_config[key] = node[:consul][key]
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
5 changes: 5 additions & 0 deletions templates/default/sv-consul-log-run.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh
exec chpst -u '<%= node[:consul][:service_user] %>':'<%= node[:consul][:service_group] %>' \
svlogd \
/var/log/consul \
# #
6 changes: 6 additions & 0 deletions templates/default/sv-consul-run.erb
Original file line number Diff line number Diff line change
@@ -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] %>' \
# #

0 comments on commit 1718eb3

Please sign in to comment.