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
|
#
# Author:: Mike Dodge (<mikedodge04@gmail.com>)
# Copyright:: Copyright (c) 2015 Facebook, Inc.
# License:: Apache License, Version 2.0
#
# 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/provider'
require 'chef/resource'
require 'chef/resource/file'
require 'chef/resource/cookbook_file'
require 'chef/resource/macosx_service'
class Chef
class Provider
class Launchd < Chef::Provider
def load_current_resource
@current_resource = Chef::Resource::Launchd.new(@new_resource.name)
@name = @new_resource.name
@label = @new_resource.label ? @new_resource.label : @name
@backup = @new_resource.backup
@cookbook = @new_resource.cookbook
@group = @new_resource.group
@mode = @new_resource.mode
@owner = @new_resource.owner
@path = @new_resource.path
@source = @new_resource.source
@content = content?
end
def action_create
manage_plist(:create)
end
def action_create_if_missing
manage_plist(:create_if_missing)
end
def action_delete
# If you delete a service you want to make sure its not loaded or
# the service will be in memory and you wont be able to stop it.
if ::File.exists?(@path)
manage_service(:disable)
end
manage_plist(:delete)
end
def action_enable
if manage_plist(:create)
manage_service(:restart)
else
manage_service(:enable)
end
end
def action_disable
manage_service(:disable)
end
def manage_plist(action)
if @source
res = cookbook_file_resource
else
res = file_resource
end
res.run_action(action)
new_resource.updated_by_last_action(true) if res.updated?
res.updated
end
def manage_service(action)
res = service_resource
res.run_action(action)
new_resource.updated_by_last_action(true) if res.updated?
end
def service_resource
res = Chef::Resource::MacosxService.new(@label, run_context)
res.name(@label)
res.service_name(@label)
res.plist(@path)
res.session_type(@session_type) if @session_type
res
end
def file_resource
res = Chef::Resource::File.new(@path, run_context)
res.name(@path)
res.backup(@backup) if @backup
res.content(@content) if @content
res.group(@group) if @group
res.mode(@mode) if @mode
res.owner(@owner) if @owner
res
end
def cookbook_file_resource
res = Chef::Resource::CookbookFile.new(@path, run_context)
res.cookbook_name = @cookbook if @cookbook
res.name(@path)
res.backup(@backup) if @backup
res.group(@group) if @group
res.mode(@mode) if @mode
res.owner(@owner) if @owner
res.source(@source) if @source
res
end
def define_resource_requirements
requirements.assert(
:create, :create_if_missing, :delete, :enable, :disable
) do |a|
type = @new_resource.type.to_s
a.assertion { ['daemon', 'agent'].include?(type) }
error_msg = 'type must be daemon or agent'
a.failure_message Chef::Exceptions::ValidationFailed, error_msg
end
end
def content?
if @new_resource.hash
@new_resource.hash.to_plist
elsif @new_resource.program || @new_resource.program_arguments
gen_plist
end
end
def gen_plist
plist_hash = {}
{
'label' => 'Label',
'program' => 'Program',
'program_arguments' => 'ProgramArguments',
'abandon_process_group' => 'AbandonProcessGroup',
'debug' => 'Debug',
'disabled' => 'Disabled',
'enable_globbing' => 'EnableGlobbing',
'enable_transactions' => 'EnableTransactions',
'environment_variables' => 'EnvironmentVariables',
'exit_timeout' => 'ExitTimeout',
'ld_group' => 'GroupName',
'hard_resource_limits' => 'HardreSourceLimits',
'inetd_compatibility' => 'inetdCompatibility',
'init_groups' => 'InitGroups',
'keep_alive' => 'KeepAlive',
'launch_only_once' => 'LaunchOnlyOnce',
'limit_load_from_hosts' => 'LimitLoadFromHosts',
'limit_load_to_hosts' => 'LimitLoadToHosts',
'limit_load_to_session_type' => 'LimitLoadToSessionType',
'low_priority_io' => 'LowPriorityIO',
'mach_services' => 'MachServices',
'nice' => 'Nice',
'on_demand' => 'OnDemand',
'process_type' => 'ProcessType',
'queue_directories' => 'QueueDirectories',
'root_directory' => 'RootDirectory',
'run_at_load' => 'RunAtLoad',
'sockets' => 'Sockets',
'soft_resource_limits' => 'SoftResourceLimits',
'standard_error_path' => 'StandardErrorPath',
'standard_in_path' => 'StandardInPath',
'standard_out_path' => 'StandardOutPath',
'start_calendar_interval' => 'StartCalendarInterval',
'start_interval' => 'StartInterval',
'start_on_mount' => 'StartOnMount',
'throttle_interval' => 'ThrottleInterval',
'time_out' => 'TimeOut',
'umask' => 'Umask',
'username' => 'UserName',
'wait_for_debugger' => 'WaitForDebugger',
'watch_paths' => 'WatchPaths',
'working_directory' => 'WorkingDirectory',
}.each do |key, val|
if @new_resource.send(key)
plist_hash[val] = @new_resource.send(key)
end
end
plist_hash.to_plist
end
end
end
end
|