2020-08-25 14:27:16 +00:00
|
|
|
# system
|
|
|
|
import logging
|
|
|
|
import subprocess
|
|
|
|
|
2020-08-17 12:51:11 +00:00
|
|
|
# local
|
2020-08-25 14:27:16 +00:00
|
|
|
from ._hidden import _find_net
|
2020-08-19 15:21:38 +00:00
|
|
|
from ..subcommand import ServiceCommand
|
2020-08-25 14:27:16 +00:00
|
|
|
from ..config import LoadedConfig
|
|
|
|
from ..executable import Executable
|
2020-08-19 15:15:00 +00:00
|
|
|
from ..misc import are_you_sure
|
2020-08-17 12:51:11 +00:00
|
|
|
|
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
class DownCommand(ServiceCommand):
|
2020-08-17 12:51:11 +00:00
|
|
|
"""kiwi down"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__(
|
2020-08-19 14:10:56 +00:00
|
|
|
'down', num_projects='?', num_services='*',
|
|
|
|
action="Bringing down",
|
2020-08-18 12:18:54 +00:00
|
|
|
description="Bring down the whole instance, a project or service(s) inside a project"
|
2020-08-17 12:51:11 +00:00
|
|
|
)
|
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
def _run_instance(self, runner, args):
|
2020-08-25 14:27:16 +00:00
|
|
|
net_name = LoadedConfig.get()['network:name']
|
|
|
|
|
2020-08-18 13:05:19 +00:00
|
|
|
if are_you_sure([
|
|
|
|
"This will bring down the entire instance.",
|
|
|
|
"",
|
|
|
|
"This may not be what you intended, because:",
|
|
|
|
" - Bringing down the instance stops ALL services in here",
|
|
|
|
]):
|
2020-08-25 14:27:16 +00:00
|
|
|
if super()._run_instance(runner, args):
|
|
|
|
# remove the hub network afterwards
|
|
|
|
if _find_net(net_name):
|
|
|
|
Executable('docker').run([
|
|
|
|
'network', 'rm', net_name
|
|
|
|
], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
|
|
|
|
|
|
logging.info(f"Network '{net_name}' removed")
|
|
|
|
|
|
|
|
else:
|
|
|
|
logging.info(f"Network '{net_name}' does not exist")
|
|
|
|
|
|
|
|
return True
|
2020-08-17 13:00:05 +00:00
|
|
|
|
2020-08-18 11:50:21 +00:00
|
|
|
return False
|
|
|
|
|
2020-08-20 13:09:15 +00:00
|
|
|
def _run_project(self, runner, args, project):
|
|
|
|
project.compose_run(['down'])
|
2020-08-18 11:50:21 +00:00
|
|
|
return True
|
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
def _run_services(self, runner, args, project, services):
|
2020-08-19 15:09:43 +00:00
|
|
|
project.compose_run(['stop', *services])
|
|
|
|
project.compose_run(['rm', '-f', *services])
|
|
|
|
|
2020-08-17 13:00:05 +00:00
|
|
|
return True
|