blob: 081b7e097d7e470ebc23af52534502dc7d8bd2ff [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
16
17try:
18 import docker
19 HAS_DOCKER = True
20except ImportError:
21 HAS_DOCKER = False
22
23__opts__ = {}
24__virtualname__ = 'dockerng_service'
25
26
27def __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
36def 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
Pavel Svimbersky60e636b2017-12-19 12:57:34 +010044def status_retcode(container, service):
45 cmd = "systemctl show " + service + " -p ActiveState,SubState,UnitFileState"
46 data = __salt__['dockerng.run'](container, cmd)
47 data = data.splitlines()
48 result = dict(s.split('=') for s in data)
49 if result['ActiveState'] == "active" and result['SubState'] == "running":
50 return True
51 return False
52
53
Pavel Svimbersky27816ab2017-12-18 15:13:49 +010054def restart(container, service):
55 cmd = "systemctl restart " + service
56 data = __salt__['dockerng.run'](container, cmd)
57 if len(data) > 0:
58 return False
59 return True
60
61
62def stop(container, service):
63 cmd = "systemctl stop " + service
64 data = __salt__['dockerng.run'](container, cmd)
65 if len(data) > 0:
66 return False
67 return True
68
69
70def start(container, service):
71 cmd = "systemctl start " + service
72 data = __salt__['dockerng.run'](container, cmd)
73 if len(data) > 0:
74 return False
75 return True
76
77
78def enable(container, service):
79 cmd = "systemctl enable " + service
80 data = __salt__['dockerng.run'](container, cmd)
81 if len(data) > 0:
82 return False
83 return True
84
85
86def reload(container, service):
87 cmd = "systemctl reload " + service
88 data = __salt__['dockerng.run'](container, cmd)
89 if len(data) > 0:
90 return False
91 return True
92
93
94def disable(container, service):
95 cmd = "systemctl disable " + service
96 data = __salt__['dockerng.run'](container, cmd)
97 if len(data) > 0:
98 return False
99 return True