From 4e9e5a263d0bb399ed270538e9160e247a6e3c4e Mon Sep 17 00:00:00 2001 From: Patrick Wyatt Date: Fri, 6 Jun 2014 14:13:57 -0700 Subject: [PATCH 1/3] added support for runit init_style --- README.md | 18 +++++ attributes/default.rb | 3 + metadata.rb | 2 +- recipes/service.rb | 100 +++++++++++++++++++----- templates/default/sv-consul-log-run.erb | 5 ++ templates/default/sv-consul-run.erb | 6 ++ 6 files changed, 113 insertions(+), 21 deletions(-) create mode 100644 templates/default/sv-consul-log-run.erb create mode 100644 templates/default/sv-consul-run.erb 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..d92728ed 100644 --- a/metadata.rb +++ b/metadata.rb @@ -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..927c19bc 100644 --- a/recipes/service.rb +++ b/recipes/service.rb @@ -1,5 +1,49 @@ 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 +unless consul_user == 'root' + user "consul service user: #{consul_user}" do + username consul_user + home "/dev/null" + shell "/bin/false" + comment "consul service user" + end +end + +# Create service group +unless consul_group == 'root' + group "consul service group: #{consul_group}" do + group_name consul_group + members consul_user + append true + end +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 +67,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 +75,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] %>' \ +# # From 6c39fb1e782be52def799a81eac7ce799eca34b0 Mon Sep 17 00:00:00 2001 From: Patrick Wyatt Date: Fri, 6 Jun 2014 13:18:35 -0700 Subject: [PATCH 2/3] version 0.2.3 --- metadata.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metadata.rb b/metadata.rb index d92728ed..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.' From 4e521cd24511c46a3852469d735c15d4bb243b4a Mon Sep 17 00:00:00 2001 From: Patrick Wyatt Date: Wed, 11 Jun 2014 10:11:17 -0700 Subject: [PATCH 3/3] refactor user/group creation --- recipes/service.rb | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/recipes/service.rb b/recipes/service.rb index 927c19bc..ee220719 100644 --- a/recipes/service.rb +++ b/recipes/service.rb @@ -17,22 +17,20 @@ end # Create service user -unless consul_user == 'root' - user "consul service user: #{consul_user}" do - username consul_user - home "/dev/null" - shell "/bin/false" - comment "consul service user" - end +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 -unless consul_group == 'root' - group "consul service group: #{consul_group}" do - group_name consul_group - members consul_user - append true - end +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