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 | Management of Contrail resources |
| 17 | ================================ |
| 18 | |
| 19 | :depends: - vnc_api Python module |
| 20 | |
| 21 | |
| 22 | Enforce the service in container is started |
| 23 | ------------------------------------------- |
| 24 | |
| 25 | .. code-block:: yaml |
| 26 | |
| 27 | contrail_control_started: |
| 28 | dockerng_service.start: |
| 29 | - container: f020d0d3efa8 |
| 30 | - service: contrail-control |
| 31 | |
| 32 | or |
| 33 | |
| 34 | .. code-block:: yaml |
| 35 | |
| 36 | contrail_control_started: |
| 37 | dockerng_service.start: |
| 38 | - container: contrail_controller |
| 39 | - service: contrail-control |
| 40 | |
| 41 | |
| 42 | Enforce the service in container is stoped |
| 43 | ------------------------------------------ |
| 44 | |
| 45 | .. code-block:: yaml |
| 46 | |
| 47 | contrail_control_stoped: |
| 48 | dockerng_service.stop: |
| 49 | - container: f020d0d3efa8 |
| 50 | - service: contrail-control |
| 51 | |
| 52 | Enforce the service in container will be restarted |
| 53 | -------------------------------------------------- |
| 54 | |
| 55 | .. code-block:: yaml |
| 56 | |
| 57 | contrail_control_restart: |
| 58 | dockerng_service.restart: |
| 59 | - container: f020d0d3efa8 |
| 60 | - service: contrail-control |
| 61 | |
| 62 | Enforce the service in container is enabled |
| 63 | ------------------------------------------- |
| 64 | |
| 65 | .. code-block:: yaml |
| 66 | |
| 67 | contrail_control_enable: |
| 68 | dockerng_service.enable: |
| 69 | - container: f020d0d3efa8 |
| 70 | - service: contrail-control |
| 71 | |
| 72 | Enforce the service in container is disabled |
| 73 | -------------------------------------------- |
| 74 | |
| 75 | .. code-block:: yaml |
| 76 | |
| 77 | contrail_control_disable: |
| 78 | dockerng_service.disable: |
| 79 | - container: f020d0d3efa8 |
| 80 | - service: contrail-control |
| 81 | |
| 82 | ''' |
| 83 | |
| 84 | |
| 85 | def __virtual__(): |
| 86 | ''' |
| 87 | Load Contrail module |
| 88 | ''' |
| 89 | return 'dockerng_service' |
| 90 | |
| 91 | |
| 92 | def start(container, service, **kwargs): |
| 93 | ''' |
| 94 | Ensures that the service in the container is started. |
| 95 | |
| 96 | :param container: ID or name of the container |
| 97 | :param service: Service name |
| 98 | ''' |
| 99 | ret = {'name': service + " in " + container, |
| 100 | 'changes': {}, |
| 101 | 'result': True, |
| 102 | 'comment': ''} |
| 103 | |
| 104 | status = __salt__['dockerng_service.status'](container, service) |
| 105 | |
| 106 | if status['ActiveState'] == "inactive" and status['SubState'] == "dead": |
| 107 | if __opts__['test']: |
| 108 | ret['result'] = None |
| 109 | ret['comment'] = service + " in " + container + " will be started" |
| 110 | return ret |
| 111 | |
| 112 | res = __salt__['dockerng_service.start'](container, service) |
| 113 | ret['comment'] = service + " in " + container + " has been started" |
| 114 | ret['changes'] = {"old": "stoped", "new": "started"} |
| 115 | return ret |
| 116 | |
| 117 | return ret |
| 118 | |
| 119 | |
| 120 | def stop(container, service, **kwargs): |
| 121 | ''' |
| 122 | Ensures that the service in the container is stoped |
| 123 | |
| 124 | :param container: ID or name of the container |
| 125 | :param service: Service name |
| 126 | ''' |
| 127 | ret = {'name': service + " in " + container, |
| 128 | 'changes': {}, |
| 129 | 'result': True, |
| 130 | 'comment': ''} |
| 131 | |
| 132 | status = __salt__['dockerng_service.status'](container, service) |
| 133 | |
| 134 | if status['ActiveState'] != "inactive" and status['SubState'] != "dead": |
| 135 | if __opts__['test']: |
| 136 | ret['result'] = None |
| 137 | ret['comment'] = service + " in " + container + " will be stoped" |
| 138 | return ret |
| 139 | |
| 140 | res = __salt__['dockerng_service.stop'](container, service) |
| 141 | ret['comment'] = service + " in " + container + " has been stoped" |
| 142 | ret['changes'] = {"new": "stoped", "old": "started"} |
| 143 | return ret |
| 144 | |
| 145 | return ret |
| 146 | |
| 147 | |
| 148 | def restart(container, service, **kwargs): |
| 149 | ''' |
| 150 | Service in the container will be restarted |
| 151 | |
| 152 | :param container: ID or name of the container |
| 153 | :param service: Service name |
| 154 | ''' |
| 155 | ret = {'name': service + " in " + container, |
| 156 | 'changes': {}, |
| 157 | 'result': True, |
| 158 | 'comment': ''} |
| 159 | |
| 160 | |
| 161 | if __opts__['test']: |
| 162 | ret['result'] = None |
| 163 | ret['comment'] = service + " in " + container + " will be restarted" |
| 164 | return ret |
| 165 | |
| 166 | res = __salt__['dockerng_service.restart'](container, service) |
| 167 | ret['comment'] = service + " in " + container + " has been stoped" |
| 168 | ret['changes'] = {"status": "restarted"} |
| 169 | return ret |
| 170 | |
| 171 | |
| 172 | def enable(container, service, **kwargs): |
| 173 | ''' |
| 174 | Ensures that the service in the container is enabled |
| 175 | |
| 176 | :param container: ID or name of the container |
| 177 | :param service: Service name |
| 178 | ''' |
| 179 | ret = {'name': service + " in " + container, |
| 180 | 'changes': {}, |
| 181 | 'result': True, |
| 182 | 'comment': ''} |
| 183 | |
| 184 | status = __salt__['dockerng_service.status'](container, service) |
| 185 | |
| 186 | if status['UnitFileState'] != "enabled": |
| 187 | if __opts__['test']: |
| 188 | ret['result'] = None |
| 189 | ret['comment'] = service + " in " + container + " will be enabled" |
| 190 | return ret |
| 191 | |
| 192 | res = __salt__['dockerng_service.enable'](container, service) |
| 193 | ret['comment'] = service + " in " + container + " has been enabled" |
| 194 | ret['changes'] = {"new": "enabled", "old": "disabled"} |
| 195 | return ret |
| 196 | |
| 197 | return ret |
| 198 | |
| 199 | |
| 200 | def disable(container, service, **kwargs): |
| 201 | ''' |
| 202 | Ensures that the service in the container is disabled |
| 203 | |
| 204 | :param container: ID or name of the container |
| 205 | :param service: Service name |
| 206 | ''' |
| 207 | ret = {'name': service + " in " + container, |
| 208 | 'changes': {}, |
| 209 | 'result': True, |
| 210 | 'comment': ''} |
| 211 | |
| 212 | status = __salt__['dockerng_service.status'](container, service) |
| 213 | |
| 214 | if status['UnitFileState'] != "disabled": |
| 215 | if __opts__['test']: |
| 216 | ret['result'] = None |
| 217 | ret['comment'] = service + " in " + container + " will be disabled" |
| 218 | return ret |
| 219 | |
| 220 | res = __salt__['dockerng_service.disable'](container, service) |
| 221 | ret['comment'] = service + " in " + container + " has been disabled" |
| 222 | ret['changes'] = {"old": "enabled", "new": "disabled"} |
| 223 | return ret |
| 224 | |
| 225 | return ret |