-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathservice.rb
198 lines (166 loc) · 4.95 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
190
191
192
193
194
195
196
197
198
# Provider:: service
#
# Copyright 2013, Holger Amann <holger@fehu.org>
#
# 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 'chef/mixin/shell_out'
require 'chef/mixin/language'
include Chef::Mixin::ShellOut
action :enable do
config_file = ::File.join(user_conf_dir,
"#{new_resource.service_name}.eye")
unless @current_resource.enabled
template_suffix = case node['platform_family']
when 'implement_me' then node['platform_family']
else 'lsb'
end
cache_service_user = service_user
cache_service_group = service_group
template "#{node['eye']['init_dir']}/#{new_resource.init_script_prefix}#{new_resource.service_name}" do
source "eye_init.#{template_suffix}.erb"
cookbook "eye"
owner cache_service_user
group cache_service_group
mode "0755"
variables(
:service_name => new_resource.service_name,
:config_file => config_file,
:user => cache_service_user
)
only_if { ::File.exists?(config_file) }
end
service "#{new_resource.init_script_prefix}#{new_resource.service_name}" do
action [ :enable ]
end
new_resource.updated_by_last_action(true)
end
end
action :load do
unless @current_resource.running
run_command(load_command)
new_resource.updated_by_last_action(true)
end
end
action :reload do
run_command(stop_command) if @current_resource.running
run_command(load_command)
new_resource.updated_by_last_action(true)
end
action :start do
unless @current_resource.running
run_command(start_command)
new_resource.updated_by_last_action(true)
end
end
action :disable do
if @current_resource.enabled
file "#{user_conf_dir}/#{new_resource.service_name}.eye" do
action :delete
end
link "#{node['eye']['init_dir']}/#{new_resource.service_name}" do
action :delete
end
new_resource.updated_by_last_action(true)
end
end
action :stop do
if @current_resource.running
run_command(stop_command)
new_resource.updated_by_last_action(true)
end
end
action :restart do
if @current_resource.running
run_command(restart_command)
new_resource.updated_by_last_action(true)
end
end
def load_current_resource
@current_resource = Chef::Resource::EyeService.new(new_resource.name)
@current_resource.service_name(new_resource.service_name)
determine_current_status!
@current_resource
end
protected
def status_command
"#{node['eye']['bin']} info #{new_resource.service_name}"
end
def load_command
"#{node['eye']['bin']} load #{user_conf_dir}/#{new_resource.service_name}.eye"
end
def load_eye
"#{node['eye']['bin']} load"
end
def start_command
"#{node['eye']['bin']} start #{new_resource.service_name}"
end
def stop_command
"#{node['eye']['bin']} stop #{new_resource.service_name}"
end
def restart_command
"#{node['eye']['bin']} restart #{new_resource.service_name}"
end
def run_command(command, opts = {})
home = user_home(service_user)
env_variables = { 'HOME' => home }
cmd = shell_out(command, :user => service_user, :group => service_group, :env => env_variables)
cmd.error! unless opts[:dont_raise]
cmd
end
def determine_current_status!
service_running?
service_enabled?
end
def service_running?
begin
# get sure eye master process is running
run_command(load_eye)
if run_command(status_command, { :dont_raise => true }).exitstatus > 0
@current_resource.running false
else
@current_resource.running true
end
rescue Mixlib::ShellOut::ShellCommandFailed, SystemCallError
@current_resource.running false
nil
end
end
def service_enabled?
if ::File.exists?("#{user_conf_dir}/#{new_resource.service_name}.eye") &&
::File.exists?("#{node['eye']['init_dir']}/#{new_resource.init_script_prefix}#{new_resource.service_name}")
@current_resource.enabled true
else
@current_resource.enabled false
end
end
def user_home(user)
home = if new_resource.user_srv_home.nil?
node['etc']['passwd'][user]['dir']
else
home = new_resource.user_srv_home
end
home
end
def service_user
new_resource.user_srv ? new_resource.user_srv_uid : node['eye']['user']
end
def service_group
new_resource.user_srv ? new_resource.user_srv_gid : node['eye']['group']
end
def user_conf_dir
::File.join(node['eye']['conf_dir'], service_user)
end
def user_log_dir
::File.join(node['eye']['log_dir'], service_user)
end