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