Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 1 | #!/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 | ''' |
| 16 | Management of Contrail resources |
| 17 | ================================ |
| 18 | |
| 19 | :depends: - vnc_api Python module |
| 20 | |
| 21 | |
Pavel Svimbersky | 60e636b | 2017-12-19 12:57:34 +0100 | [diff] [blame] | 22 | Enforce the service in container is running |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 23 | ------------------------------------------- |
| 24 | |
| 25 | .. code-block:: yaml |
| 26 | |
Pavel Svimbersky | 60e636b | 2017-12-19 12:57:34 +0100 | [diff] [blame] | 27 | contrail_control_running: |
| 28 | dockerng_service.running: |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 29 | - container: f020d0d3efa8 |
| 30 | - service: contrail-control |
| 31 | |
| 32 | or |
| 33 | |
| 34 | .. code-block:: yaml |
| 35 | |
Pavel Svimbersky | 60e636b | 2017-12-19 12:57:34 +0100 | [diff] [blame] | 36 | contrail_control_running: |
| 37 | dockerng_service.running: |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 38 | - container: contrail_controller |
| 39 | - service: contrail-control |
| 40 | |
| 41 | |
Pavel Svimbersky | 60e636b | 2017-12-19 12:57:34 +0100 | [diff] [blame] | 42 | Enforce the service in container is dead |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 43 | ------------------------------------------ |
| 44 | |
| 45 | .. code-block:: yaml |
| 46 | |
Pavel Svimbersky | 60e636b | 2017-12-19 12:57:34 +0100 | [diff] [blame] | 47 | contrail_control_dead: |
| 48 | dockerng_service.dead: |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 49 | - container: f020d0d3efa8 |
| 50 | - service: contrail-control |
| 51 | |
| 52 | Enforce the service in container will be restarted |
| 53 | -------------------------------------------------- |
| 54 | |
| 55 | .. code-block:: yaml |
| 56 | |
Pavel Svimbersky | 60e636b | 2017-12-19 12:57:34 +0100 | [diff] [blame] | 57 | contrail_control_restarted: |
| 58 | dockerng_service.restarted: |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 59 | - container: f020d0d3efa8 |
| 60 | - service: contrail-control |
| 61 | |
| 62 | Enforce the service in container is enabled |
| 63 | ------------------------------------------- |
| 64 | |
| 65 | .. code-block:: yaml |
| 66 | |
Pavel Svimbersky | 60e636b | 2017-12-19 12:57:34 +0100 | [diff] [blame] | 67 | contrail_control_enabled: |
| 68 | dockerng_service.enabled: |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 69 | - container: f020d0d3efa8 |
| 70 | - service: contrail-control |
| 71 | |
| 72 | Enforce the service in container is disabled |
| 73 | -------------------------------------------- |
| 74 | |
| 75 | .. code-block:: yaml |
| 76 | |
Pavel Svimbersky | 60e636b | 2017-12-19 12:57:34 +0100 | [diff] [blame] | 77 | contrail_control_disabled: |
| 78 | dockerng_service.disabled: |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 79 | - container: f020d0d3efa8 |
| 80 | - service: contrail-control |
| 81 | |
| 82 | ''' |
| 83 | |
| 84 | |
| 85 | def __virtual__(): |
| 86 | ''' |
| 87 | Load Contrail module |
| 88 | ''' |
| 89 | return 'dockerng_service' |
| 90 | |
| 91 | |
Pavel Svimbersky | 60e636b | 2017-12-19 12:57:34 +0100 | [diff] [blame] | 92 | def running(container, service=None, services=None, **kwargs): |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 93 | ''' |
Pavel Svimbersky | 60e636b | 2017-12-19 12:57:34 +0100 | [diff] [blame] | 94 | Ensures that the service in the container is running |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 95 | |
| 96 | :param container: ID or name of the container |
Pavel Svimbersky | 60e636b | 2017-12-19 12:57:34 +0100 | [diff] [blame] | 97 | :param services: List of services |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 98 | :param service: Service name |
| 99 | ''' |
Pavel Svimbersky | 60e636b | 2017-12-19 12:57:34 +0100 | [diff] [blame] | 100 | ret = {'name': kwargs.get('name', 'dockerng_service.running'), |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 101 | 'changes': {}, |
| 102 | 'result': True, |
Pavel Svimbersky | 60e636b | 2017-12-19 12:57:34 +0100 | [diff] [blame] | 103 | 'comment': {} |
| 104 | } |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 105 | |
Pavel Svimbersky | 60e636b | 2017-12-19 12:57:34 +0100 | [diff] [blame] | 106 | if service and not services: |
| 107 | services = [service, ] |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 108 | |
Pavel Svimbersky | 60e636b | 2017-12-19 12:57:34 +0100 | [diff] [blame] | 109 | for service in services: |
| 110 | status = __salt__['dockerng_service.status'](container, service) |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 111 | |
Pavel Svimbersky | 60e636b | 2017-12-19 12:57:34 +0100 | [diff] [blame] | 112 | 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 Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 120 | |
| 121 | return ret |
| 122 | |
| 123 | |
Pavel Svimbersky | 60e636b | 2017-12-19 12:57:34 +0100 | [diff] [blame] | 124 | def dead(container, service, **kwargs): |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 125 | ''' |
Pavel Svimbersky | 60e636b | 2017-12-19 12:57:34 +0100 | [diff] [blame] | 126 | Ensures that the service in the container is dead |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 127 | |
| 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 Svimbersky | 60e636b | 2017-12-19 12:57:34 +0100 | [diff] [blame] | 144 | __salt__['dockerng_service.stop'](container, service) |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 145 | 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 Svimbersky | 60e636b | 2017-12-19 12:57:34 +0100 | [diff] [blame] | 152 | def restarted(container, service, **kwargs): |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 153 | ''' |
| 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 Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 164 | 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 Svimbersky | 60e636b | 2017-12-19 12:57:34 +0100 | [diff] [blame] | 170 | ret['comment'] = service + " in " + container + " has been restarted" |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 171 | ret['changes'] = {"status": "restarted"} |
| 172 | return ret |
| 173 | |
| 174 | |
Pavel Svimbersky | 60e636b | 2017-12-19 12:57:34 +0100 | [diff] [blame] | 175 | def enabled(container, service, **kwargs): |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 176 | ''' |
| 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 Svimbersky | 60e636b | 2017-12-19 12:57:34 +0100 | [diff] [blame] | 195 | __salt__['dockerng_service.enable'](container, service) |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 196 | 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 Svimbersky | 60e636b | 2017-12-19 12:57:34 +0100 | [diff] [blame] | 203 | def disabled(container, service, **kwargs): |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 204 | ''' |
| 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 Svimbersky | 60e636b | 2017-12-19 12:57:34 +0100 | [diff] [blame] | 223 | __salt__['dockerng_service.disable'](container, service) |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 224 | ret['comment'] = service + " in " + container + " has been disabled" |
| 225 | ret['changes'] = {"old": "enabled", "new": "disabled"} |
| 226 | return ret |
| 227 | |
| 228 | return ret |
Pavel Svimbersky | 60e636b | 2017-12-19 12:57:34 +0100 | [diff] [blame] | 229 | |
| 230 | |
| 231 | def 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 |