From 400ca78755d417cdd6f3b9053414da6365ce8c3f Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Thu, 16 Jul 2020 08:47:24 +0200 Subject: [PATCH] #20: Move cooperative to separate repository --- requirements.txt | 1 + src/harbor/__init__.py | 9 +- src/harbor/tasks/cooperative.py | 203 -------------------------------- 3 files changed, 6 insertions(+), 207 deletions(-) delete mode 100644 src/harbor/tasks/cooperative.py diff --git a/requirements.txt b/requirements.txt index aacd84d..6bc6e17 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ rkd==1.1.1.dev31 +rkd-snippet-cooperative ansible>=2.8 diff --git a/src/harbor/__init__.py b/src/harbor/__init__.py index 7c0e0b5..baa9e3c 100644 --- a/src/harbor/__init__.py +++ b/src/harbor/__init__.py @@ -5,6 +5,10 @@ from rkd.standardlib.env import SetEnvTask from rkd.standardlib.jinja import RenderDirectoryTask from rkd.standardlib.jinja import FileRendererTask +from rkd_cooperative.tasks import CooperativeSyncTask +from rkd_cooperative.tasks import CooperativeInstallTask +from rkd_cooperative.tasks import CooperativeSnippetWizardTask +from rkd_cooperative.tasks import CooperativeSnippetInstallTask from .tasks.diagnostic import ListContainersTask from .tasks.running import StartTask from .tasks.running import UpgradeTask @@ -42,10 +46,6 @@ from .tasks.repositories import SetPermissionsForWritableDirectoriesTask from .tasks.repositories import ListRepositoriesTask from .tasks.repositories import FetchAllRepositories -from .tasks.cooperative import CooperativeSyncTask -from .tasks.cooperative import CooperativeInstallTask -from .tasks.cooperative import CooperativeSnippetWizardTask -from .tasks.cooperative import CooperativeSnippetInstallTask from .tasks.structure import CreateHarborStructureTask @@ -114,6 +114,7 @@ def main(): os.environ['RKD_WHITELIST_GROUPS'] = env_or_default('RKD_WHITELIST_GROUPS', ':env,:harbor,') os.environ['RKD_ALIAS_GROUPS'] = env_or_default('RKD_ALIAS_GROUPS', '->:harbor') os.environ['RKD_UI'] = env_or_default('RKD_UI', 'false') + os.environ['REPOSITORIES'] = env_or_default('REPOSITORIES', 'https://github.com/riotkit-org/riotkit-harbor-snippet-cooperative') rkd_main() diff --git a/src/harbor/tasks/cooperative.py b/src/harbor/tasks/cooperative.py deleted file mode 100644 index 749d431..0000000 --- a/src/harbor/tasks/cooperative.py +++ /dev/null @@ -1,203 +0,0 @@ -import os -import subprocess -from json import loads as json_loads -from glob import glob -from argparse import ArgumentParser -from typing import Dict -from urllib.parse import urlparse -from rkd.contract import ExecutionContext -from rkd.exception import MissingInputException -from rkd.inputoutput import Wizard -from .base import HarborBaseTask -from ..formatting import development_formatting - -HARBOR_PATH = os.path.dirname(os.path.realpath(__file__)) + '/..' -DEFAULT_REPOSITORIES = 'https://github.com/riotkit-org/riotkit-harbor-snippet-cooperative' -REPOSITORIES_DIRECTORY = '.rkd/cooperative' - - -class BaseCooperativeTask(HarborBaseTask): - def format_task_name(self, name) -> str: - return development_formatting(name) - - def get_declared_envs(self) -> Dict[str, str]: - envs = super(BaseCooperativeTask, self).get_declared_envs() - envs['REPOSITORIES'] = DEFAULT_REPOSITORIES - - return envs - - def get_group_name(self) -> str: - return ':harbor:cooperative' - - def get_repositories_list(self, ctx: ExecutionContext) -> list: - try: - return ctx.get_arg_or_env('--repositories').split(',') - except MissingInputException: - return DEFAULT_REPOSITORIES.split(',') - - -class CooperativeSyncTask(BaseCooperativeTask): - """Synchronize repositories of the RiotKit's Snippets Cooperative""" - - def get_name(self) -> str: - return ':sync' - - def run(self, ctx: ExecutionContext) -> bool: - end_result = True - - for repository in self.get_repositories_list(ctx): - self.io().info('Syncing repository "%s"' % repository) - - if not self.sync_repository(repository): - self.io().error('Failed to synchronize repository "%s"' % repository) - - end_result = False - continue - - self.io().info('Repository synced.') - - return end_result - - def sync_repository(self, git_url: str): - repository_dir = REPOSITORIES_DIRECTORY + '/' + self.extract_repository_name_from_git_url(git_url) - - try: - if not os.path.isdir(repository_dir + '/'): - self.sh(' '.join(['mkdir', '-p', repository_dir])) - self.sh(' '.join(['git', 'clone', git_url, repository_dir])) - return True - - # pull the existing repository - self.sh('''cd "%s" && git reset --hard HEAD && git checkout master && git pull''' % repository_dir) - - except subprocess.CalledProcessError as e: - self.io().error('Error fetching a git repository: %s' % str(e)) - return False - - return True - - @staticmethod - def extract_repository_name_from_git_url(git_url: str) -> str: - if git_url.startswith('http'): - return urlparse('https://github.com/riotkit-org/riotkit-harbor-snippet-cooperative').path[1:] - - name = git_url[5:].split(':')[1] - - if name.endswith('.git'): - return name[0:-4] - - return name - - -class CooperativeInstallTask(BaseCooperativeTask): - """Installs a snippet from the previously synchronized repository""" - - def get_name(self) -> str: - return ':install' - - def configure_argparse(self, parser: ArgumentParser): - parser.add_argument('name', help='Snippet name') - - def run(self, ctx: ExecutionContext) -> bool: - name = ctx.get_arg('name') - path = self.find_snippet_path(name) - - if not path: - self.io().error('Snippet not found in any synchronized repository. ' + - 'Did you forget to do harbor :harbor:cooperative:sync?') - return False - - self.io().info('Installing snippet from "%s"' % path) - - # mock rkd_path, so the snippet can override the tasks - rkd_path = os.getenv('rkd_path', '') - snippet_rkd_path = os.path.realpath('./' + path + '/.rkd') - - if snippet_rkd_path: - os.putenv('RKD_PATH', (snippet_rkd_path + ':' + rkd_path).strip(':')) - - try: - subprocess.check_call(['rkd', ':snippet:wizard', path]) - subprocess.check_call(['rkd', ':snippet:install', path]) - finally: - if os.path.isfile('.rkd/tmp-wizard.json'): - os.unlink('.rkd/tmp-wizard.json') - - os.putenv('rkd_path', rkd_path) - - return True - - def find_snippet_path(self, name: str): - """Finds a snippet path by name. - - Raises: - Exception: When snippet exists in multiple repositories - """ - - found_path = None - - for snippet_path in self.list_snippets(): - snippet_name = os.path.basename(snippet_path) - - if snippet_name == name: - if found_path is not None: - raise Exception('Ambiguous match, %s exists in %s and in %s' % (name, found_path, snippet_path)) - - found_path = snippet_path - - return found_path - - @staticmethod - def list_snippets(): - dirs = glob('.rkd/cooperative/**/**/files', recursive=True) - - return list(set(map(lambda name: os.path.dirname(name), dirs))) - - -class CooperativeSnippetWizardTask(BaseCooperativeTask): - """Snippet wizard - to be overridden by custom task provided by snippet""" - - def run(self, context: ExecutionContext) -> bool: - pass - - def get_group_name(self) -> str: - return ':snippet' - - def get_name(self) -> str: - return ':wizard' - - def configure_argparse(self, parser: ArgumentParser): - parser.add_argument('path', help='Path to the root directory of the snippet files') - - def execute(self, ctx: ExecutionContext) -> bool: - return True - - -class CooperativeSnippetInstallTask(BaseCooperativeTask): - """Snippet installation - to be overridden by custom task provided by snippet""" - - def run(self, context: ExecutionContext) -> bool: - pass - - def get_group_name(self) -> str: - return ':snippet' - - def get_name(self) -> str: - return ':install' - - def configure_argparse(self, parser: ArgumentParser): - parser.add_argument('path', help='Path to the root directory of the snippet files') - - def execute(self, ctx: ExecutionContext) -> bool: - wizard = Wizard(self) - wizard.load_previously_stored_values() - os.environ.update(wizard.answers) - - self.rkd([ - ':j2:directory-to-directory', - '--source="%s"' % ctx.get_arg('path') + '/files/', - '--target="./"', - '--pattern="(.*)"', - '--copy-not-matching-files' - ]) - return True