-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathresource_runit_service.rb
116 lines (106 loc) · 5.3 KB
/
resource_runit_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
#
# Cookbook:: runit
# resource:: runit_service
#
# Author:: Joshua Timberman <[email protected]>
# Copyright:: 2011-2019, Chef Software Inc.
#
# 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/resource'
require 'chef/resource/service'
class Chef
class Resource
# Missing top-level class documentation comment
class RunitService < Chef::Resource::Service
resource_name :runit_service
default_action :enable
# For legacy reasons we allow setting these via attribute
property :sv_bin, String, default: lazy { node['runit']['sv_bin'] || (platform_family?('debian') ? '/usr/bin/sv' : '/sbin/sv') }
property :sv_dir, [String, FalseClass], default: lazy { node['runit']['sv_dir'] || '/etc/sv' }
property :service_dir, String, default: lazy { node['runit']['service_dir'] || '/etc/service' }
property :lsb_init_dir, String, default: lazy { node['runit']['lsb_init_dir'] || '/etc/init.d' }
property :control, Array, default: []
property :options, Hash, default: lazy { default_options }, coerce: proc { |r| default_options.merge(r) if r.respond_to?(:merge) }
property :env, Hash, default: {}
property :log, [true, false], default: true
property :cookbook, String
property :check, [true, false], default: false
property :start_down, [true, false], default: false
property :delete_downfile, [true, false], default: false
property :finish, [true, false], default: false
property :supervisor_owner, String, regex: [Chef::Config[:user_valid_regex]]
property :supervisor_group, String, regex: [Chef::Config[:group_valid_regex]]
property :owner, String, regex: [Chef::Config[:user_valid_regex]]
property :group, String, regex: [Chef::Config[:group_valid_regex]]
property :enabled, [true, false], default: false
property :running, [true, false], default: false
property :default_logger, [true, false], default: false
property :restart_on_update, [true, false], default: true
property :run_template_name, String, default: lazy { service_name }
property :log_template_name, String, default: lazy { service_name }
property :check_script_template_name, String, default: lazy { service_name }
property :finish_script_template_name, String, default: lazy { service_name }
property :control_template_names, Hash, default: lazy { set_control_template_names }
property :service_start_command, String, default: 'start'
property :service_stop_command, String, default: 'stop'
property :service_restart_command, String, default: 'restart'
property :service_status_command, String, default: 'status'
property :sv_templates, [true, false], default: true
property :sv_timeout, Integer
property :sv_verbose, [true, false], default: false
property :log_dir, String, default: lazy { ::File.join('/var/log/', service_name) }
property :log_flags, String, default: '-tt'
property :log_size, Integer
property :log_num, Integer
property :log_min, Integer
property :log_timeout, Integer
property :log_processor, String
property :log_socket, [String, Hash]
property :log_prefix, String
property :log_config_append, String
# Use a link to sv instead of a full blown init script calling runit.
# This was added for omnibus projects and probably shouldn't be used elsewhere
property :use_init_script_sv_link, [true, false], default: false
alias template_name run_template_name
def set_control_template_names
template_names = {}
control.each do |signal|
template_names[signal] ||= service_name
end
template_names
end
# the default legacy options kept for compatibility with the definition
#
# @return [Hash] if env is the default empty hash then return env_dir value. Otherwise return an empty hash
def default_options
env.empty? ? { env_dir: ::File.join(sv_dir, service_name, 'env') } : {}
end
def after_created
unless run_context.nil?
new_resource = self
service_dir_name = ::File.join(service_dir, service_name)
find_resource(:service, new_resource.name) do # creates if it does not exist
provider Chef::Provider::Service::Simple
supports new_resource.supports
start_command "#{new_resource.sv_bin} #{new_resource.service_start_command} #{service_dir_name}"
stop_command "#{new_resource.sv_bin} #{new_resource.service_stop_command} #{service_dir_name}"
restart_command "#{new_resource.sv_bin} #{new_resource.service_restart_command} #{service_dir_name}"
status_command "#{new_resource.sv_bin} #{new_resource.service_status_command} #{service_dir_name}"
action :nothing
end
end
end
end
end
end