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 | |
| 17 | try: |
| 18 | import docker |
| 19 | HAS_DOCKER = True |
| 20 | except ImportError: |
| 21 | HAS_DOCKER = False |
| 22 | |
| 23 | __opts__ = {} |
| 24 | __virtualname__ = 'dockerng_service' |
Martin Polreich | 9c79a77 | 2018-06-28 15:56:59 +0200 | [diff] [blame] | 25 | salt_version = __salt__['grains.get']('saltversioninfo', default=[2017,7,6]) |
| 26 | docker_module = 'docker' |
| 27 | if salt_version < [2017,7]: |
| 28 | docker_module = 'dockerng' |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 29 | |
| 30 | |
| 31 | def __virtual__(): |
| 32 | ''' |
| 33 | Only load this module if docker library is installed. |
| 34 | ''' |
| 35 | if HAS_DOCKER: |
| 36 | return __virtualname__ |
| 37 | return (False, 'dockerio execution module not loaded: docker python library not available.') |
| 38 | |
| 39 | |
| 40 | def status(container, service): |
| 41 | cmd = "systemctl show " + service + " -p ActiveState,SubState,UnitFileState" |
Martin Polreich | 9c79a77 | 2018-06-28 15:56:59 +0200 | [diff] [blame] | 42 | data = __salt__[docker_module + '.run'](container, cmd) |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 43 | data = data.splitlines() |
| 44 | result = dict(s.split('=') for s in data) |
| 45 | return result |
| 46 | |
| 47 | |
Pavel Svimbersky | 60e636b | 2017-12-19 12:57:34 +0100 | [diff] [blame] | 48 | def status_retcode(container, service): |
| 49 | cmd = "systemctl show " + service + " -p ActiveState,SubState,UnitFileState" |
Martin Polreich | 9c79a77 | 2018-06-28 15:56:59 +0200 | [diff] [blame] | 50 | data = __salt__[docker_module + '.run'](container, cmd) |
Pavel Svimbersky | 60e636b | 2017-12-19 12:57:34 +0100 | [diff] [blame] | 51 | data = data.splitlines() |
| 52 | result = dict(s.split('=') for s in data) |
| 53 | if result['ActiveState'] == "active" and result['SubState'] == "running": |
| 54 | return True |
| 55 | return False |
| 56 | |
| 57 | |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 58 | def restart(container, service): |
| 59 | cmd = "systemctl restart " + service |
Martin Polreich | 9c79a77 | 2018-06-28 15:56:59 +0200 | [diff] [blame] | 60 | data = __salt__[docker_module + '.run'](container, cmd) |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 61 | if len(data) > 0: |
| 62 | return False |
| 63 | return True |
| 64 | |
| 65 | |
| 66 | def stop(container, service): |
| 67 | cmd = "systemctl stop " + service |
Martin Polreich | 9c79a77 | 2018-06-28 15:56:59 +0200 | [diff] [blame] | 68 | data = __salt__[docker_module + '.run'](container, cmd) |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 69 | if len(data) > 0: |
| 70 | return False |
| 71 | return True |
| 72 | |
| 73 | |
| 74 | def start(container, service): |
| 75 | cmd = "systemctl start " + service |
Martin Polreich | 9c79a77 | 2018-06-28 15:56:59 +0200 | [diff] [blame] | 76 | data = __salt__[docker_module + '.run'](container, cmd) |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 77 | if len(data) > 0: |
| 78 | return False |
| 79 | return True |
| 80 | |
| 81 | |
| 82 | def enable(container, service): |
| 83 | cmd = "systemctl enable " + service |
Martin Polreich | 9c79a77 | 2018-06-28 15:56:59 +0200 | [diff] [blame] | 84 | data = __salt__[docker_module + '.run'](container, cmd) |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 85 | if len(data) > 0: |
| 86 | return False |
| 87 | return True |
| 88 | |
| 89 | |
| 90 | def reload(container, service): |
| 91 | cmd = "systemctl reload " + service |
Martin Polreich | 9c79a77 | 2018-06-28 15:56:59 +0200 | [diff] [blame] | 92 | data = __salt__[docker_module + '.run'](container, cmd) |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 93 | if len(data) > 0: |
| 94 | return False |
| 95 | return True |
| 96 | |
| 97 | |
| 98 | def disable(container, service): |
| 99 | cmd = "systemctl disable " + service |
Martin Polreich | 9c79a77 | 2018-06-28 15:56:59 +0200 | [diff] [blame] | 100 | data = __salt__[docker_module + '.run'](container, cmd) |
Pavel Svimbersky | 27816ab | 2017-12-18 15:13:49 +0100 | [diff] [blame] | 101 | if len(data) > 0: |
| 102 | return False |
| 103 | return True |