blob: 7b1074efc61f0a5eb1e1680c6c091f2e4ed23dee [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
Petr Michalec66415f32018-07-17 15:01:34 +020035def _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 Svimbersky27816ab2017-12-18 15:13:49 +010041
42def status(container, service):
43 cmd = "systemctl show " + service + " -p ActiveState,SubState,UnitFileState"
Petr Michalec66415f32018-07-17 15:01:34 +020044 data = __salt__[_docker_module() + '.run'](container, cmd)
Pavel Svimbersky27816ab2017-12-18 15:13:49 +010045 data = data.splitlines()
46 result = dict(s.split('=') for s in data)
47 return result
48
49
Pavel Svimbersky60e636b2017-12-19 12:57:34 +010050def status_retcode(container, service):
51 cmd = "systemctl show " + service + " -p ActiveState,SubState,UnitFileState"
Petr Michalec66415f32018-07-17 15:01:34 +020052 data = __salt__[_docker_module() + '.run'](container, cmd)
Pavel Svimbersky60e636b2017-12-19 12:57:34 +010053 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 Svimbersky27816ab2017-12-18 15:13:49 +010060def restart(container, service):
61 cmd = "systemctl restart " + service
Petr Michalec66415f32018-07-17 15:01:34 +020062 data = __salt__[_docker_module() + '.run'](container, cmd)
Pavel Svimbersky27816ab2017-12-18 15:13:49 +010063 if len(data) > 0:
64 return False
65 return True
66
67
68def stop(container, service):
69 cmd = "systemctl stop " + service
Petr Michalec66415f32018-07-17 15:01:34 +020070 data = __salt__[_docker_module() + '.run'](container, cmd)
Pavel Svimbersky27816ab2017-12-18 15:13:49 +010071 if len(data) > 0:
72 return False
73 return True
74
75
76def start(container, service):
77 cmd = "systemctl start " + service
Petr Michalec66415f32018-07-17 15:01:34 +020078 data = __salt__[_docker_module() + '.run'](container, cmd)
Pavel Svimbersky27816ab2017-12-18 15:13:49 +010079 if len(data) > 0:
80 return False
81 return True
82
83
84def enable(container, service):
85 cmd = "systemctl enable " + service
Petr Michalec66415f32018-07-17 15:01:34 +020086 data = __salt__[_docker_module() + '.run'](container, cmd)
Pavel Svimbersky27816ab2017-12-18 15:13:49 +010087 if len(data) > 0:
88 return False
89 return True
90
91
92def reload(container, service):
93 cmd = "systemctl reload " + service
Petr Michalec66415f32018-07-17 15:01:34 +020094 data = __salt__[_docker_module() + '.run'](container, cmd)
Pavel Svimbersky27816ab2017-12-18 15:13:49 +010095 if len(data) > 0:
96 return False
97 return True
98
99
100def disable(container, service):
101 cmd = "systemctl disable " + service
Petr Michalec66415f32018-07-17 15:01:34 +0200102 data = __salt__[_docker_module() + '.run'](container, cmd)
Pavel Svimbersky27816ab2017-12-18 15:13:49 +0100103 if len(data) > 0:
104 return False
105 return True