1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-11-22 04:43:00 +00:00

remove legacy

This commit is contained in:
Jörn-Michael Miehe 2021-12-03 15:04:03 +01:00
parent 1590041ab8
commit 7641f1846a
13 changed files with 0 additions and 515 deletions

View file

@ -1,38 +0,0 @@
# local
from ._hidden import ConfCopyCommand, NetUpCommand
from .build import BuildCommand
from .cmd import CmdCommand
from .disable import DisableCommand
from .down import DownCommand
from .enable import EnableCommand
from .logs import LogsCommand
from .new import NewCommand
from .pull import PullCommand
from .push import PushCommand
from .restart import RestartCommand
from .shell import ShellCommand
from .show import ShowCommand
from .up import UpCommand
from .update import UpdateCommand
__all__ = [
'ConfCopyCommand',
'NetUpCommand',
'BuildCommand',
'CmdCommand',
'DisableCommand',
'DownCommand',
'EnableCommand',
'InitCommand',
'LogsCommand',
'NewCommand',
'PullCommand',
'PushCommand',
'RestartCommand',
'ShellCommand',
'ShowCommand',
'UpCommand',
'UpdateCommand',
]

View file

@ -1,18 +0,0 @@
# local
from ..subcommand import ServiceCommand
class BuildCommand(ServiceCommand):
"""kiwi build"""
def __init__(self):
super().__init__(
'build', num_projects='?', num_services='*',
action="Building images for",
description="Build images for the whole instance, a project or service(s) inside a project"
)
def _run_services(self, runner, args, project, services):
project.compose_run(['build', '--pull', *services])
return True

View file

@ -1,40 +0,0 @@
# system
import logging
# local
from ..subcommand import ProjectCommand
class CmdCommand(ProjectCommand):
"""kiwi cmd"""
def __init__(self):
super().__init__(
'cmd', num_projects=1,
action="Running docker-compose in",
description="Run raw docker-compose command in a project"
)
# command for docker-compose
self._sub_parser.add_argument(
'compose_cmd', metavar='cmd', type=str,
help="command for 'docker-compose'"
)
# arguments for docker-compose command
self._sub_parser.add_argument(
'compose_args', metavar='arg', nargs='*', type=str,
help="arguments for 'docker-compose' commands"
)
def _run_project(self, runner, args, project):
if args.unknowns:
args.compose_args = [*args.compose_args, *args.unknowns]
args.unknowns = []
logging.debug(f"Updated args: {args}")
# run with split compose_cmd argument
project.compose_run([args.compose_cmd, *args.compose_args])
return True

View file

@ -1,16 +0,0 @@
# local
from ..subcommand import ProjectCommand
class DisableCommand(ProjectCommand):
"""kiwi disable"""
def __init__(self):
super().__init__(
'disable', num_projects='+',
action="Disabling",
description="Disable project(s) in this instance"
)
def _run_project(self, runner, args, project):
return project.disable()

View file

@ -1,56 +0,0 @@
# system
import logging
import subprocess
# local
from ._hidden import _find_net
from ..config import LoadedConfig
from ..executable import Executable
from ..misc import are_you_sure
from ..subcommand import ServiceCommand
class DownCommand(ServiceCommand):
"""kiwi down"""
def __init__(self):
super().__init__(
'down', num_projects='?', num_services='*',
action="Bringing down",
description="Bring down the whole instance, a project or service(s) inside a project"
)
def _run_instance(self, runner, args):
net_name = LoadedConfig.get()['network:name']
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",
]):
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
return False
def _run_project(self, runner, args, project):
project.compose_run(['down'])
return True
def _run_services(self, runner, args, project, services):
project.compose_run(['stop', *services])
project.compose_run(['rm', '-f', *services])
return True

View file

@ -1,16 +0,0 @@
# local
from ..subcommand import ProjectCommand
class EnableCommand(ProjectCommand):
"""kiwi enable"""
def __init__(self):
super().__init__(
'enable', num_projects='+',
action="Enabling",
description="Enable project(s) in this instance"
)
def _run_project(self, runner, args, project):
return project.enable()

View file

@ -1,40 +0,0 @@
# local
from ..subcommand import ServiceCommand
class LogsCommand(ServiceCommand):
"""kiwi logs"""
def __init__(self):
super().__init__(
'logs', num_projects=1, num_services='*',
action="Showing logs of",
description="Show logs of a project or service(s) inside a project"
)
# -f switch: Follow logs
self._sub_parser.add_argument(
'-f', '--follow', action='store_true',
help="output appended data as log grows"
)
def _run_services(self, runner, args, project, services):
# include timestamps
compose_cmd = ['logs', '-t']
# handle following the log output
if args.follow:
compose_cmd = [*compose_cmd, '-f', '--tail=10']
# append if one or more services are given
if services:
compose_cmd = [*compose_cmd, *args.services]
if args.follow:
project.compose_run(compose_cmd)
else:
# use 'less' viewer if output is static
project.compose_run_less(compose_cmd)
return True

View file

@ -1,18 +0,0 @@
# local
from ..subcommand import ServiceCommand
class PullCommand(ServiceCommand):
"""kiwi pull"""
def __init__(self):
super().__init__(
'pull', num_projects='?', num_services='*',
action="Pulling images for",
description="Pull images for the whole instance, a project or service(s) inside a project"
)
def _run_services(self, runner, args, project, services):
project.compose_run(['pull', '--ignore-pull-failures', *services])
return True

View file

@ -1,18 +0,0 @@
# local
from ..subcommand import ServiceCommand
class PushCommand(ServiceCommand):
"""kiwi push"""
def __init__(self):
super().__init__(
'push', num_projects='?', num_services='*',
action="Pushing images for",
description="Push images for the whole instance, a project or service(s) inside a project"
)
def _run_services(self, runner, args, project, services):
project.compose_run(['push', *services])
return True

View file

@ -1,26 +0,0 @@
# local
from ..misc import are_you_sure
from ..subcommand import ServiceCommand
class RestartCommand(ServiceCommand):
"""kiwi restart"""
def __init__(self):
super().__init__(
'restart', num_projects='?', num_services='*',
action="Restarting",
description="Restart the whole instance, a project or service(s) inside a project"
)
def _run_instance(self, runner, args):
if are_you_sure([
"This will restart the entire instance."
]):
return super()._run_instance(runner, args)
return False
def _run_services(self, runner, args, project, services):
project.compose_run(['restart', *services])
return True

View file

@ -1,104 +0,0 @@
# system
import logging
import subprocess
from ..config import LoadedConfig
# local
from ..subcommand import ServiceCommand
def _service_has_executable(project, service, exe_name):
"""
Test if service in project has an executable exe_name in its PATH.
Requires /bin/sh.
"""
try:
# test if desired shell exists
project.compose_run(
['exec', service, '/bin/sh', '-c', f"command -v {exe_name}"],
check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
)
return True
except subprocess.CalledProcessError as e:
# fallback
return False
def _find_shell(args, project, service):
"""find first working shell (provided by config and args) in service in project"""
# builtin shells: as a last resort, fallback to '/bin/sh' and 'sh'
shells = ['/bin/sh', 'sh']
# load favorite shells from config
config = LoadedConfig.get()
if config['runtime:shells']:
shells = [*config['runtime:shells'], *shells]
# consider shell from args
if args.shell:
shells = [args.shell, *shells]
logging.debug(f"Shells priority: {shells}")
# actually try shells
for i, shell in enumerate(shells):
if _service_has_executable(project, service, shell):
# found working shell
logging.debug(f"Using shell '{shell}'")
return shell
elif i + 1 < len(shells):
# try next in list
logging.info(f"Shell '{shell}' not found in container, trying '{shells[i + 1]}'")
elif args.shell:
# not found, user suggestion provided
logging.warning(f"Could not find any working shell in this container. "
f"Launching provided '{args.shell}' nevertheless. "
f"Don't get mad if this fails!")
return args.shell
else:
# not found, search exhausted
logging.error(f"Could not find any working shell among '{shells}' in this container. "
f"Please suggest a shell using the '-s SHELL' command line option!")
return None
class ShellCommand(ServiceCommand):
"""kiwi shell"""
def __init__(self):
super().__init__(
'shell', num_projects=1, num_services=1,
action="Spawning shell in",
description="Spawn shell inside a project's service"
)
# -s argument: Select shell
self._sub_parser.add_argument(
'-s', '--shell', type=str,
help="shell to spawn"
)
# -u argument: Run as user
self._sub_parser.add_argument(
'-u', '--user', type=str,
help="container user to run shell"
)
def _run_services(self, runner, args, project, services):
service = services[0]
shell = _find_shell(args, project, service)
user_args = ['-u', args.user] if args.user else []
if shell is not None:
# spawn shell
project.compose_run(['exec', *user_args, service, shell])
return True
return False

View file

@ -1,99 +0,0 @@
# system
import logging
import os
import yaml
from ..project import Project
from ..projects import Projects
# local
from ..subcommand import ServiceCommand
def _print_list(strings):
if isinstance(strings, str):
print(f" - {strings}")
elif isinstance(strings, Project):
_print_list(strings.get_name())
elif isinstance(strings, list):
for string in strings:
_print_list(string)
else:
_print_list(list(strings))
class ShowCommand(ServiceCommand):
"""kiwi show"""
def __init__(self):
super().__init__(
'show', num_projects='?', num_services='*',
action="Showing",
description="Show projects in this instance, services inside a project or service(s) inside a project"
)
def _run_instance(self, runner, args):
print(f"kiwi-scp instance at '{os.getcwd()}'")
print("#########")
projects = Projects.from_dir()
enabled_projects = projects.filter_enabled()
if enabled_projects:
print(f"Enabled projects:")
_print_list(enabled_projects)
disabled_projects = projects.filter_disabled()
if disabled_projects:
print(f"Disabled projects:")
_print_list(disabled_projects)
return True
def _run_project(self, runner, args, project):
if not project.exists():
logging.warning(f"Project '{project.get_name()}' not found")
return False
print(f"Services in project '{project.get_name()}':")
print("#########")
with open(project.compose_file_name(), 'r') as stream:
try:
docker_compose_yml = yaml.safe_load(stream)
_print_list(docker_compose_yml['services'].keys())
except yaml.YAMLError as exc:
logging.error(exc)
return True
def _run_services(self, runner, args, project, services):
if not project.exists():
logging.error(f"Project '{project.get_name()}' not found")
return False
print(f"Configuration of services {services} in project '{project.get_name()}':")
print("#########")
with open(project.compose_file_name(), 'r') as stream:
try:
docker_compose_yml = yaml.safe_load(stream)
for service_name in services:
try:
print(yaml.dump(
{service_name: docker_compose_yml['services'][service_name]},
default_flow_style=False, sort_keys=False
).strip())
except KeyError:
logging.error(f"Service '{service_name}' not found")
return True
except yaml.YAMLError as exc:
logging.error(exc)
return False

View file

@ -1,26 +0,0 @@
# local
from ..subcommand import ServiceCommand
class UpCommand(ServiceCommand):
"""kiwi up"""
def __init__(self):
super().__init__(
'up', num_projects='?', num_services='*',
action="Bringing up",
description="Bring up the whole instance, a project or service(s) inside a project"
)
def _run_instance(self, runner, args):
if runner.run('conf-copy'):
return super()._run_instance(runner, args)
return False
def _run_services(self, runner, args, project, services):
if runner.run('net-up'):
project.compose_run(['up', '-d', *services])
return True
return False