From cb62b39d81bd05cc274cc5c00f35af3c1da76d19 Mon Sep 17 00:00:00 2001 From: liyu Date: Thu, 11 Nov 2021 15:33:19 +0800 Subject: [PATCH] fix BUILD_ID empty hanging --- setup.py | 5 ++--- sidecarinit/commands.py | 24 +++++++++++++++++------- sidecarinit/config.py | 9 +++++---- sidecarinit/download.py | 5 +++-- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/setup.py b/setup.py index a6c46aa..2937561 100644 --- a/setup.py +++ b/setup.py @@ -1,12 +1,11 @@ from setuptools import setup, find_packages -import sys setup( zip_safe=True, use_2to3=False, name='sidecarinit', version='0.0.1', - long_description='sidecar-init container for download archive and decompress', + long_description='sidecar-init container for download archive and unzip', classifiers=[ "Development Status :: 3 - Alpha", "Intended Audience :: Developers", @@ -26,4 +25,4 @@ 'run-sidecar-init = sidecarinit:run' ], } -) \ No newline at end of file +) diff --git a/sidecarinit/commands.py b/sidecarinit/commands.py index b0aa393..c681fc2 100644 --- a/sidecarinit/commands.py +++ b/sidecarinit/commands.py @@ -1,22 +1,32 @@ import click import os import sys +import logging from pathlib import Path import sidecarinit.config as config import sidecarinit.download as download +logger = logging.getLogger(__name__) + + @click.command() -@click.argument('env_yml', required=False, default='/deployments/config/application.yaml') #help='Target environment, Same as sidecar application.yml' +@click.argument('env_yml', required=False, + default='/deployments/config/application.yaml') +# help='Target environment, Same as sidecar application.yml' def run(env_yml): - + suite = config.read_config(env_yml) - archive_name = '/' + os.environ.get('BUILD_ID') + '.zip' + archive_name = os.environ.get('BUILD_ID') + '.zip' - print('Downloading from :', suite.archive_api, ' to ' ,suite.local_repository) + logger.info('Downloading from : %s to %s .', suite.archive_api, suite.local_repository) Path(suite.local_repository).mkdir(parents=True, exist_ok=True) - Path(suite.local_repository + archive_name ).touch(exist_ok=True) + Path(os.path.join(suite.local_repository, archive_name)).touch(exist_ok=True) - download.download_archive(suite.archive_api + archive_name, suite.local_repository) + if archive_name != '.zip': + download.download_archive(suite.archive_api + archive_name, + suite.local_repository) + else: + logger.info('BUILD_ID does not exist, exit now.') - sys.exit(0); \ No newline at end of file + sys.exit(0) diff --git a/sidecarinit/config.py b/sidecarinit/config.py index 1ac9ed4..9df46f6 100644 --- a/sidecarinit/config.py +++ b/sidecarinit/config.py @@ -11,11 +11,12 @@ class Environment: def __init__(self, env_spec): self.archive_api = env_spec.get('sidecar').get(ENV_ARCHIVE_API) - self.local_repository = env_spec.get('sidecar').get(ENV_LOCAL_REPOSITORY).replace('${user.home}',os.environ.get('HOME')) or DEFAULT_LOCAL_REPOSITORY + self.local_repository = env_spec.get('sidecar').get(ENV_LOCAL_REPOSITORY).replace('${user.home}', os.environ.get('HOME')) or DEFAULT_LOCAL_REPOSITORY + def read_config(env_yml): """ Read the suite configuration that this worker should run, from a config.yml file - (specified on the command line and passed in as a parameter here). + (specified on the command line and passed in as a parameter here). Once we have a suite YAML file (from the suite_yml config), that file will be parsed and passed back with the rest of the config values, in a Config object. If any required configs are missing and don't have default values, error messages will @@ -33,8 +34,8 @@ def read_config(env_yml): yaml = YAML(typ='safe') env_spec = yaml.load(f) else: - errors.append( f"Missing test environment config file") + errors.append(f"Missing test environment config file") env = Environment(env_spec) - return env \ No newline at end of file + return env diff --git a/sidecarinit/download.py b/sidecarinit/download.py index ca3ad40..7e8d4d1 100644 --- a/sidecarinit/download.py +++ b/sidecarinit/download.py @@ -2,10 +2,11 @@ import shutil import os -def download_archive(url,repository): + +def download_archive(url, repository): archive_name = "/" + os.environ.get('BUILD_ID') + '.zip' with urllib.request.urlopen(url) as response, open(repository + archive_name, 'wb') as out_file: shutil.copyfileobj(response, out_file) - shutil.unpack_archive(repository + archive_name, repository) \ No newline at end of file + shutil.unpack_archive(repository + archive_name, repository)