|
|
|
@ -16,28 +16,38 @@ from urllib3.util import Retry
|
|
|
|
|
from debian_parser import PackagesParser
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def from_env(name, default=None):
|
|
|
|
|
""" Retrieve a parameter from environment """
|
|
|
|
|
for var in (f'PLUGIN_{name}', f'APTLY_{name}'):
|
|
|
|
|
if var in os.environ:
|
|
|
|
|
return os.environ[var]
|
|
|
|
|
return default
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Handle parameters from environment
|
|
|
|
|
API_URL = os.environ.get('PLUGIN_API_URL', None)
|
|
|
|
|
API_URL = from_env('API_URL', None)
|
|
|
|
|
if not API_URL:
|
|
|
|
|
print('API URL not provided')
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
API_USERNAME = os.environ.get('PLUGIN_API_USERNAME', None)
|
|
|
|
|
API_USERNAME = from_env('API_USERNAME', None)
|
|
|
|
|
if not API_USERNAME:
|
|
|
|
|
print('API username not provided')
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
API_PASSWORD = os.environ.get('PLUGIN_API_PASSWORD', None)
|
|
|
|
|
API_PASSWORD = from_env('API_PASSWORD', None)
|
|
|
|
|
if not API_PASSWORD:
|
|
|
|
|
print('API password not provided')
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
MAX_RETRY = os.environ.get('PLUGIN_MAX_RETRIES', None)
|
|
|
|
|
MAX_RETRY = from_env('MAX_RETRIES', None)
|
|
|
|
|
|
|
|
|
|
REPO_NAME = os.environ.get('PLUGIN_REPO_NAME', 'stable')
|
|
|
|
|
REPO_COMPONENT = os.environ.get('PLUGIN_REPO_COMPONENT', 'main')
|
|
|
|
|
DIST = os.environ.get('PLUGIN_PATH', 'dist')
|
|
|
|
|
SOURCE_NAME = os.environ.get('PLUGIN_SOURCE_PACKAGE_NAME', None)
|
|
|
|
|
REPO_NAME = from_env('REPO_NAME', 'stable')
|
|
|
|
|
PREFIX = from_env('PREFIX', '.')
|
|
|
|
|
REPO_COMPONENT = from_env('REPO_COMPONENT', 'main')
|
|
|
|
|
INPUT_PATH = from_env('PATH', 'dist')
|
|
|
|
|
SOURCE_NAME = from_env('SOURCE_PACKAGE_NAME', None)
|
|
|
|
|
DISTRIBUTIONS = []
|
|
|
|
|
|
|
|
|
|
# List changes files
|
|
|
|
|
changes_files_regex = (
|
|
|
|
@ -48,21 +58,21 @@ changes_files_regex = (
|
|
|
|
|
)
|
|
|
|
|
changes_files = []
|
|
|
|
|
try:
|
|
|
|
|
for filename in os.listdir(DIST):
|
|
|
|
|
filepath = os.path.join(DIST, filename)
|
|
|
|
|
for filename in os.listdir(INPUT_PATH):
|
|
|
|
|
filepath = os.path.join(INPUT_PATH, filename)
|
|
|
|
|
if not os.path.isfile(filepath):
|
|
|
|
|
continue
|
|
|
|
|
if changes_files_regex.match(filename):
|
|
|
|
|
changes_files.append(filepath)
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
print(f'Specified directory path "{DIST}" not found')
|
|
|
|
|
print(f'Specified directory path "{INPUT_PATH}" not found')
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
except NotADirectoryError:
|
|
|
|
|
print(f'Specified path "{DIST}" is not a directory')
|
|
|
|
|
print(f'Specified path "{INPUT_PATH}" is not a directory')
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
if not changes_files:
|
|
|
|
|
print(f'No changes file found in {DIST}')
|
|
|
|
|
print(f'No changes file found in {INPUT_PATH}')
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -88,13 +98,14 @@ def list_files_in_changes_file(filepath):
|
|
|
|
|
files = []
|
|
|
|
|
for infos in parser.parse():
|
|
|
|
|
for info in infos:
|
|
|
|
|
if info['tag'].lower() != 'files':
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
for line in info['value'].split(' '):
|
|
|
|
|
if not line:
|
|
|
|
|
continue
|
|
|
|
|
files.append(os.path.join(dirpath, line.split()[-1]))
|
|
|
|
|
if info['tag'].lower() == 'files':
|
|
|
|
|
for line in info['value'].split(' '):
|
|
|
|
|
if not line:
|
|
|
|
|
continue
|
|
|
|
|
files.append(os.path.join(dirpath, line.split()[-1]))
|
|
|
|
|
if info['tag'].lower() == 'distribution':
|
|
|
|
|
if info['value'] not in DISTRIBUTIONS:
|
|
|
|
|
DISTRIBUTIONS.append(info['value'])
|
|
|
|
|
return files
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -137,7 +148,9 @@ def include_file(package_name, changes_file):
|
|
|
|
|
result.status_code == 200 and
|
|
|
|
|
data.get('Report', {}).get('Added')
|
|
|
|
|
):
|
|
|
|
|
print(f'Unknown error occurred including {changes_file}')
|
|
|
|
|
print(
|
|
|
|
|
f'Unknown error occurred including {changes_file}'
|
|
|
|
|
'See APTLY API logs for details.')
|
|
|
|
|
return False
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
@ -150,9 +163,8 @@ for changes_file in changes_files:
|
|
|
|
|
for filepath in filepaths:
|
|
|
|
|
if not upload_file(package_name, filepath):
|
|
|
|
|
print(
|
|
|
|
|
f' - {filepath}: fail to upload file, pass this changes '
|
|
|
|
|
'file'
|
|
|
|
|
)
|
|
|
|
|
f' - {filepath}: fail to upload file. See APTLY API logs '
|
|
|
|
|
'for details.')
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
else:
|
|
|
|
|
print(f' - {filepath}')
|
|
|
|
@ -182,30 +194,35 @@ error = (
|
|
|
|
|
not data.get('CreatedAt')
|
|
|
|
|
)
|
|
|
|
|
if error:
|
|
|
|
|
print(f'Fail to create snapshot "{snap_name}" of repository "{REPO_NAME}"')
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Update published snapshot of repository
|
|
|
|
|
print(
|
|
|
|
|
f'Update published snapshot of repository "{REPO_NAME}" to "{snap_name}"')
|
|
|
|
|
url = f'{API_URL}/publish/:./{REPO_NAME}'
|
|
|
|
|
payload = {
|
|
|
|
|
'Snapshots': [
|
|
|
|
|
{
|
|
|
|
|
'Component': REPO_COMPONENT,
|
|
|
|
|
'Name': snap_name
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
result = session.put(url, json=payload)
|
|
|
|
|
if (
|
|
|
|
|
result.status_code < 200 or
|
|
|
|
|
result.status_code > 299
|
|
|
|
|
):
|
|
|
|
|
print(
|
|
|
|
|
f'Fail to update published snapshot of repository "{REPO_NAME}" to '
|
|
|
|
|
f'"{snap_name}"')
|
|
|
|
|
f'Fail to create snapshot "{snap_name}" of repository "{REPO_NAME}".'
|
|
|
|
|
'See APTLY API logs for details.')
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Update published snapshot of repository for each distributions
|
|
|
|
|
for distribution in DISTRIBUTIONS:
|
|
|
|
|
print(
|
|
|
|
|
f'Update published snapshot of distribution "{distribution}" to '
|
|
|
|
|
f'"{snap_name}" (prefix: {PREFIX})')
|
|
|
|
|
url = f'{API_URL}/publish/:{PREFIX}/{distribution}'
|
|
|
|
|
payload = {
|
|
|
|
|
'Snapshots': [
|
|
|
|
|
{
|
|
|
|
|
'Component': REPO_COMPONENT,
|
|
|
|
|
'Name': snap_name
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
result = session.put(url, json=payload)
|
|
|
|
|
if (
|
|
|
|
|
result.status_code < 200 or
|
|
|
|
|
result.status_code > 299
|
|
|
|
|
):
|
|
|
|
|
print(
|
|
|
|
|
f'Fail to update published snapshot of distribution "{distribution}" '
|
|
|
|
|
f'to "{snap_name}" (prefix: {PREFIX}). See APTLY API logs for '
|
|
|
|
|
'details.')
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
print("Done.")
|