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 | |
| 35 | |
| 36 | def status(container, service): |
| 37 | cmd = "systemctl show " + service + " -p ActiveState,SubState,UnitFileState" |
| 38 | data = __salt__['dockerng.run'](container, cmd) |
| 39 | data = data.splitlines() |
| 40 | result = dict(s.split('=') for s in data) |
| 41 | return result |
| 42 | |
| 43 | |
| 44 | def restart(container, service): |
| 45 | cmd = "systemctl restart " + service |
| 46 | data = __salt__['dockerng.run'](container, cmd) |
| 47 | if len(data) > 0: |
| 48 | return False |
| 49 | return True |
| 50 | |
| 51 | |
| 52 | def stop(container, service): |
| 53 | cmd = "systemctl stop " + service |
| 54 | data = __salt__['dockerng.run'](container, cmd) |
| 55 | if len(data) > 0: |
| 56 | return False |
| 57 | return True |
| 58 | |
| 59 | |
| 60 | def start(container, service): |
| 61 | cmd = "systemctl start " + service |
| 62 | data = __salt__['dockerng.run'](container, cmd) |
| 63 | if len(data) > 0: |
| 64 | return False |
| 65 | return True |
| 66 | |
| 67 | |
| 68 | def enable(container, service): |
| 69 | cmd = "systemctl enable " + service |
| 70 | data = __salt__['dockerng.run'](container, cmd) |
| 71 | if len(data) > 0: |
| 72 | return False |
| 73 | return True |
| 74 | |
| 75 | |
| 76 | def reload(container, service): |
| 77 | cmd = "systemctl reload " + service |
| 78 | data = __salt__['dockerng.run'](container, cmd) |
| 79 | if len(data) > 0: |
| 80 | return False |
| 81 | return True |
| 82 | |
| 83 | |
| 84 | def disable(container, service): |
| 85 | cmd = "systemctl disable " + service |
| 86 | data = __salt__['dockerng.run'](container, cmd) |
| 87 | if len(data) > 0: |
| 88 | return False |
| 89 | return True |
| 90 | |