blob: 2a6b20e517885f122326e3c4cc31b2b9447b1689 [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'
Martin Polreich9c79a772018-06-28 15:56:59 +020025salt_version = __salt__['grains.get']('saltversioninfo', default=[2017,7,6])
26docker_module = 'docker'
27if salt_version < [2017,7]:
28 docker_module = 'dockerng'
Pavel Svimbersky27816ab2017-12-18 15:13:49 +010029
30
31def __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
40def status(container, service):
41 cmd = "systemctl show " + service + " -p ActiveState,SubState,UnitFileState"
Martin Polreich9c79a772018-06-28 15:56:59 +020042 data = __salt__[docker_module + '.run'](container, cmd)
Pavel Svimbersky27816ab2017-12-18 15:13:49 +010043 data = data.splitlines()
44 result = dict(s.split('=') for s in data)
45 return result
46
47
Pavel Svimbersky60e636b2017-12-19 12:57:34 +010048def status_retcode(container, service):
49 cmd = "systemctl show " + service + " -p ActiveState,SubState,UnitFileState"
Martin Polreich9c79a772018-06-28 15:56:59 +020050 data = __salt__[docker_module + '.run'](container, cmd)
Pavel Svimbersky60e636b2017-12-19 12:57:34 +010051 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 Svimbersky27816ab2017-12-18 15:13:49 +010058def restart(container, service):
59 cmd = "systemctl restart " + service
Martin Polreich9c79a772018-06-28 15:56:59 +020060 data = __salt__[docker_module + '.run'](container, cmd)
Pavel Svimbersky27816ab2017-12-18 15:13:49 +010061 if len(data) > 0:
62 return False
63 return True
64
65
66def stop(container, service):
67 cmd = "systemctl stop " + service
Martin Polreich9c79a772018-06-28 15:56:59 +020068 data = __salt__[docker_module + '.run'](container, cmd)
Pavel Svimbersky27816ab2017-12-18 15:13:49 +010069 if len(data) > 0:
70 return False
71 return True
72
73
74def start(container, service):
75 cmd = "systemctl start " + service
Martin Polreich9c79a772018-06-28 15:56:59 +020076 data = __salt__[docker_module + '.run'](container, cmd)
Pavel Svimbersky27816ab2017-12-18 15:13:49 +010077 if len(data) > 0:
78 return False
79 return True
80
81
82def enable(container, service):
83 cmd = "systemctl enable " + service
Martin Polreich9c79a772018-06-28 15:56:59 +020084 data = __salt__[docker_module + '.run'](container, cmd)
Pavel Svimbersky27816ab2017-12-18 15:13:49 +010085 if len(data) > 0:
86 return False
87 return True
88
89
90def reload(container, service):
91 cmd = "systemctl reload " + service
Martin Polreich9c79a772018-06-28 15:56:59 +020092 data = __salt__[docker_module + '.run'](container, cmd)
Pavel Svimbersky27816ab2017-12-18 15:13:49 +010093 if len(data) > 0:
94 return False
95 return True
96
97
98def disable(container, service):
99 cmd = "systemctl disable " + service
Martin Polreich9c79a772018-06-28 15:56:59 +0200100 data = __salt__[docker_module + '.run'](container, cmd)
Pavel Svimbersky27816ab2017-12-18 15:13:49 +0100101 if len(data) > 0:
102 return False
103 return True