forked from sous-chefs/consul
-
Notifications
You must be signed in to change notification settings - Fork 1
/
_service.rb
189 lines (166 loc) · 5.44 KB
/
_service.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#
# Copyright 2014 John Bellone <jbellone@bloomberg.net>
# Copyright 2014 Bloomberg Finance L.P.
#
# 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.
#
require 'json'
# Configure directories
consul_directories = []
consul_directories << node['consul']['data_dir']
consul_directories << node['consul']['config_dir']
consul_directories << '/var/lib/consul'
# Select service user & group
case node['consul']['init_style']
when 'runit'
include_recipe 'runit::default'
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 = JSON.parse(node['consul']['extra_params'].to_json)
service_config['data_dir'] = node['consul']['data_dir']
num_cluster = node['consul']['bootstrap_expect'].to_i
case node['consul']['service_mode']
when 'bootstrap'
service_config['server'] = true
service_config['bootstrap'] = true
when 'cluster'
service_config['server'] = true
if num_cluster > 1
service_config['bootstrap_expect'] = num_cluster
service_config['start_join'] = node['consul']['servers']
else
service_config['bootstrap'] = true
end
when 'server'
service_config['server'] = true
service_config['start_join'] = node['consul']['servers']
when 'client'
service_config['start_join'] = node['consul']['servers']
else
Chef::Application.fatal! %Q(node['consul']['service_mode'] must be "bootstrap", "cluster", "server", or "client")
end
iface_addr_map = {
:bind_interface => :bind_addr,
:advertise_interface => :advertise_addr,
:client_interface => :client_addr
}
iface_addr_map.each_pair do |interface,addr|
next unless node['consul'][interface]
if node["network"]["interfaces"][node['consul'][interface]]
ip = node["network"]["interfaces"][node['consul'][interface]]["addresses"].detect{|k,v| v[:family] == "inet"}.first
node.default['consul'][addr] = ip
else
Chef::Application.fatal!("Interface specified in node['consul'][#{interface}] does not exist!")
end
end
if node['consul']['serve_ui']
service_config['ui_dir'] = node['consul']['ui_dir']
service_config['client_addr'] = node['consul']['client_addr']
end
additional_options = ['recursor', 'statsd_addr', 'leave_on_terminate', 'disable_remote_exec', 'acl_datacenter', 'acl_token', 'acl_default_policy', 'acl_down_policy', 'acl_master_token']
additional_options.each do |option|
if node['consul'][option]
service_config[option] = node['consul'][option]
end
end
copy_params = [
:bind_addr, :datacenter, :domain, :log_level, :node_name, :advertise_addr, :ports, :enable_syslog, :encrypt
]
copy_params.each do |key|
if node['consul'][key]
if key == :ports
Chef::Application.fatal! 'node[:consul][:ports] must be a Hash' unless node[:consul][key].kind_of?(Hash)
end
service_config[key] = node['consul'][key]
end
end
consul_config_filename = File.join(node['consul']['config_dir'], 'default.json')
file consul_config_filename do
user consul_user
group consul_group
mode 0600
action :create
content JSON.pretty_generate(service_config, quirks_mode: true)
# https://github.com/johnbellone/consul-cookbook/issues/72
notifies :restart, "service[consul]"
end
case node['consul']['init_style']
when 'init'
if platform?("ubuntu")
init_file = '/etc/init/consul.conf'
init_tmpl = 'consul.conf.erb'
else
init_file = '/etc/init.d/consul'
init_tmpl = 'consul-init.erb'
end
template node['consul']['etc_config_dir'] do
source 'consul-sysconfig.erb'
mode 0755
notifies :create, "template[#{init_file}]", :immediately
end
template init_file do
source init_tmpl
mode 0755
variables(
consul_binary: "#{node['consul']['install_dir']}/consul",
config_dir: node['consul']['config_dir'],
)
notifies :restart, 'service[consul]', :immediately
end
service 'consul' do
provider Chef::Provider::Service::Upstart if platform?("ubuntu")
supports status: true, restart: true, reload: true
action [:enable, :start]
subscribes :restart, "file[#{consul_config_filename}", :delayed
end
when 'runit'
runit_service 'consul' do
supports status: true, restart: true, reload: true
action [:enable, :start]
subscribes :restart, "file[#{consul_config_filename}]", :delayed
log true
options(
consul_binary: "#{node['consul']['install_dir']}/consul",
config_dir: node['consul']['config_dir'],
)
end
end