blob: ed13798702e74d665b5fec99a381083c8eed85ee [file] [log] [blame]
Pavel Svimbersky27816ab2017-12-18 15:13:49 +01001#!/usr/bin/python
2# Copyright 2017 Mirantis, Inc.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15'''
16Management of Contrail resources
17================================
18
19:depends: - vnc_api Python module
20
21
Pavel Svimbersky60e636b2017-12-19 12:57:34 +010022Enforce the service in container is running
Pavel Svimbersky27816ab2017-12-18 15:13:49 +010023-------------------------------------------
24
25.. code-block:: yaml
26
Pavel Svimbersky60e636b2017-12-19 12:57:34 +010027 contrail_control_running:
28 dockerng_service.running:
Pavel Svimbersky27816ab2017-12-18 15:13:49 +010029 - container: f020d0d3efa8
30 - service: contrail-control
31
32or
33
34.. code-block:: yaml
35
Pavel Svimbersky60e636b2017-12-19 12:57:34 +010036 contrail_control_running:
37 dockerng_service.running:
Pavel Svimbersky27816ab2017-12-18 15:13:49 +010038 - container: contrail_controller
39 - service: contrail-control
40
41
Pavel Svimbersky60e636b2017-12-19 12:57:34 +010042Enforce the service in container is dead
Pavel Svimbersky27816ab2017-12-18 15:13:49 +010043------------------------------------------
44
45.. code-block:: yaml
46
Pavel Svimbersky60e636b2017-12-19 12:57:34 +010047 contrail_control_dead:
48 dockerng_service.dead:
Pavel Svimbersky27816ab2017-12-18 15:13:49 +010049 - container: f020d0d3efa8
50 - service: contrail-control
51
52Enforce the service in container will be restarted
53--------------------------------------------------
54
55.. code-block:: yaml
56
Pavel Svimbersky60e636b2017-12-19 12:57:34 +010057 contrail_control_restarted:
58 dockerng_service.restarted:
Pavel Svimbersky27816ab2017-12-18 15:13:49 +010059 - container: f020d0d3efa8
60 - service: contrail-control
61
62Enforce the service in container is enabled
63-------------------------------------------
64
65.. code-block:: yaml
66
Pavel Svimbersky60e636b2017-12-19 12:57:34 +010067 contrail_control_enabled:
68 dockerng_service.enabled:
Pavel Svimbersky27816ab2017-12-18 15:13:49 +010069 - container: f020d0d3efa8
70 - service: contrail-control
71
72Enforce the service in container is disabled
73--------------------------------------------
74
75.. code-block:: yaml
76
Pavel Svimbersky60e636b2017-12-19 12:57:34 +010077 contrail_control_disabled:
78 dockerng_service.disabled:
Pavel Svimbersky27816ab2017-12-18 15:13:49 +010079 - container: f020d0d3efa8
80 - service: contrail-control
81
82'''
83
84
85def __virtual__():
86 '''
87 Load Contrail module
88 '''
89 return 'dockerng_service'
90
91
Pavel Svimbersky60e636b2017-12-19 12:57:34 +010092def running(container, service=None, services=None, **kwargs):
Pavel Svimbersky27816ab2017-12-18 15:13:49 +010093 '''
Pavel Svimbersky60e636b2017-12-19 12:57:34 +010094 Ensures that the service in the container is running
Pavel Svimbersky27816ab2017-12-18 15:13:49 +010095
96 :param container: ID or name of the container
Pavel Svimbersky60e636b2017-12-19 12:57:34 +010097 :param services: List of services
Pavel Svimbersky27816ab2017-12-18 15:13:49 +010098 :param service: Service name
99 '''
Pavel Svimbersky60e636b2017-12-19 12:57:34 +0100100 ret = {'name': kwargs.get('name', 'dockerng_service.running'),
Pavel Svimbersky27816ab2017-12-18 15:13:49 +0100101 'changes': {},
102 'result': True,
Pavel Svimbersky60e636b2017-12-19 12:57:34 +0100103 'comment': {}
104 }
Pavel Svimbersky27816ab2017-12-18 15:13:49 +0100105
Pavel Svimbersky60e636b2017-12-19 12:57:34 +0100106 if service and not services:
107 services = [service, ]
Pavel Svimbersky27816ab2017-12-18 15:13:49 +0100108
Pavel Svimbersky60e636b2017-12-19 12:57:34 +0100109 for service in services:
110 status = __salt__['dockerng_service.status'](container, service)
Pavel Svimbersky27816ab2017-12-18 15:13:49 +0100111
Pavel Svimbersky60e636b2017-12-19 12:57:34 +0100112 if status['ActiveState'] != "active" and status['SubState'] != "running":
113 if __opts__['test']:
114 ret['result'] = None
115 ret['comment'][service] = " will be started"
116 else:
117 __salt__['dockerng_service.start'](container, service)
118 ret['comment'] = service + " in " + container + " has been started"
119 ret['changes'] = {service: "started"}
Pavel Svimbersky27816ab2017-12-18 15:13:49 +0100120
121 return ret
122
123
Pavel Svimbersky60e636b2017-12-19 12:57:34 +0100124def dead(container, service, **kwargs):
Pavel Svimbersky27816ab2017-12-18 15:13:49 +0100125 '''
Pavel Svimbersky60e636b2017-12-19 12:57:34 +0100126 Ensures that the service in the container is dead
Pavel Svimbersky27816ab2017-12-18 15:13:49 +0100127
128 :param container: ID or name of the container
129 :param service: Service name
130 '''
131 ret = {'name': service + " in " + container,
132 'changes': {},
133 'result': True,
134 'comment': ''}
135
136 status = __salt__['dockerng_service.status'](container, service)
137
138 if status['ActiveState'] != "inactive" and status['SubState'] != "dead":
139 if __opts__['test']:
140 ret['result'] = None
141 ret['comment'] = service + " in " + container + " will be stoped"
142 return ret
143
Pavel Svimbersky60e636b2017-12-19 12:57:34 +0100144 __salt__['dockerng_service.stop'](container, service)
Pavel Svimbersky27816ab2017-12-18 15:13:49 +0100145 ret['comment'] = service + " in " + container + " has been stoped"
146 ret['changes'] = {"new": "stoped", "old": "started"}
147 return ret
148
149 return ret
150
151
Pavel Svimbersky60e636b2017-12-19 12:57:34 +0100152def restarted(container, service, **kwargs):
Pavel Svimbersky27816ab2017-12-18 15:13:49 +0100153 '''
154 Service in the container will be restarted
155
156 :param container: ID or name of the container
157 :param service: Service name
158 '''
159 ret = {'name': service + " in " + container,
160 'changes': {},
161 'result': True,
162 'comment': ''}
163
Pavel Svimbersky27816ab2017-12-18 15:13:49 +0100164 if __opts__['test']:
165 ret['result'] = None
166 ret['comment'] = service + " in " + container + " will be restarted"
167 return ret
168
169 res = __salt__['dockerng_service.restart'](container, service)
Pavel Svimbersky60e636b2017-12-19 12:57:34 +0100170 ret['comment'] = service + " in " + container + " has been restarted"
Pavel Svimbersky27816ab2017-12-18 15:13:49 +0100171 ret['changes'] = {"status": "restarted"}
172 return ret
173
174
Pavel Svimbersky60e636b2017-12-19 12:57:34 +0100175def enabled(container, service, **kwargs):
Pavel Svimbersky27816ab2017-12-18 15:13:49 +0100176 '''
177 Ensures that the service in the container is enabled
178
179 :param container: ID or name of the container
180 :param service: Service name
181 '''
182 ret = {'name': service + " in " + container,
183 'changes': {},
184 'result': True,
185 'comment': ''}
186
187 status = __salt__['dockerng_service.status'](container, service)
188
189 if status['UnitFileState'] != "enabled":
190 if __opts__['test']:
191 ret['result'] = None
192 ret['comment'] = service + " in " + container + " will be enabled"
193 return ret
194
Pavel Svimbersky60e636b2017-12-19 12:57:34 +0100195 __salt__['dockerng_service.enable'](container, service)
Pavel Svimbersky27816ab2017-12-18 15:13:49 +0100196 ret['comment'] = service + " in " + container + " has been enabled"
197 ret['changes'] = {"new": "enabled", "old": "disabled"}
198 return ret
199
200 return ret
201
202
Pavel Svimbersky60e636b2017-12-19 12:57:34 +0100203def disabled(container, service, **kwargs):
Pavel Svimbersky27816ab2017-12-18 15:13:49 +0100204 '''
205 Ensures that the service in the container is disabled
206
207 :param container: ID or name of the container
208 :param service: Service name
209 '''
210 ret = {'name': service + " in " + container,
211 'changes': {},
212 'result': True,
213 'comment': ''}
214
215 status = __salt__['dockerng_service.status'](container, service)
216
217 if status['UnitFileState'] != "disabled":
218 if __opts__['test']:
219 ret['result'] = None
220 ret['comment'] = service + " in " + container + " will be disabled"
221 return ret
222
Pavel Svimbersky60e636b2017-12-19 12:57:34 +0100223 __salt__['dockerng_service.disable'](container, service)
Pavel Svimbersky27816ab2017-12-18 15:13:49 +0100224 ret['comment'] = service + " in " + container + " has been disabled"
225 ret['changes'] = {"old": "enabled", "new": "disabled"}
226 return ret
227
228 return ret
Pavel Svimbersky60e636b2017-12-19 12:57:34 +0100229
230
231def mod_watch(name,
232 contrainer=None,
233 sfun=None,
234 sig=None,
235 reload=False,
236 full_restart=False,
237 init_delay=None,
238 force=False,
239 **kwargs):
240 '''
241 The service watcher, called to invoke the watch command.
242
243 :param name: The name of the init or rc script used to manage the
244 service
245 :param sfun: The original function which triggered the mod_watch
246 call (`service.running`, for example).
247 :param sig: The string to search for when looking for the service
248 process with ps
249 :param reload: Use reload instead of the default restart (exclusive
250 option with full_restart, defaults to reload if both
251 are used)
252 :param full_restart: Use service.full_restart instead of restart
253 (exclusive option with reload)
254 :param force: Use service.force_reload instead of reload
255 (needs reload to be set to True)
256 :param init_delay: Add a sleep command (in seconds) before the service is
257 restarted/reloaded
258 '''
259 ret = {'name': name,
260 'changes': {},
261 'result': True,
262 'comment': {}}
263
264 service = kwargs.get('service')
265 services = kwargs.get('services')
266 if not services and service:
267 services = [service, ]
268 elif not services and not service:
269 ret['result'] = False
270 ret['comment'] = "Service was not defined"
271 return ret
272
273 container = kwargs.get('container', None)
274 if not container:
275 ret['result'] = False
276 ret['comment'] = "Container was not defined"
277 return ret
278
279 ret['comment'] = {}
280 if sfun == 'running':
281
282 for service in services:
283 status = __salt__['dockerng_service.status'](container, service)
284
285
286 if __opts__['test']:
287 ret['result'] = None
288 ret['comment'][service] = "Services will be restarted"
289 ret['changes'][service] = "will be restarted"
290 else:
291 res = __salt__['dockerng_service.restart'](container, service)
292 ret['comment'] = "Services has been restarted"
293 ret['changes'][service] = "restarted"
294 else:
295 ret['comment'] = 'Unable to trigger watch for dockerng_service.{0}'.format(sfun)
296 ret['result'] = False
297 return ret