diff --git a/src/cmd-rollout b/src/cmd-rollout new file mode 100644 index 0000000000000000000000000000000000000000..7c6e56a88451ac57559f6a0be8c8144fdbe472ea --- /dev/null +++ b/src/cmd-rollout @@ -0,0 +1,72 @@ +#!/usr/bin/python3 + +import argparse +import json +import os +import requests +import tempfile +from datetime import datetime, timezone +import paramiko +from scp import SCPClient + +def download_json(url): + response = requests.get(url) + response.raise_for_status() + return response.json() + +def save_temp_file(data): + with tempfile.NamedTemporaryFile(delete=False, suffix='.json', dir='/tmp') as temp_file: + temp_file.write(json.dumps(data, indent=2).encode('utf-8')) + temp_file_path = temp_file.name + return temp_file_path + +def modify_json(data, version): + now = datetime.now(timezone.utc).isoformat(timespec='seconds').replace('+00:00', 'Z') + data['metadata']['last-modified'] = now + if 'releases' not in data: + data['releases'] = [] + new_release = { + 'version': version, + 'metadata': { + 'rollout': { + 'duration_minutes': 0, + 'start_epoch': int(datetime.now().timestamp()), + 'start_percentage': 0.0, + } + } + } + data['releases'].append(new_release) + return data + +def upload_file(file_path, user, host, remote_path, stream, key_file): + ssh_client = paramiko.SSHClient() + ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + ssh_client.connect(hostname=host, username=user, key_filename=key_file) + + with SCPClient(ssh_client.get_transport()) as scp: + # Combine remote path with 'stable.json' as the filename + remote_file_path = os.path.join(remote_path, f'{stream}.json') + scp.put(file_path, remote_path=remote_file_path) + +def main(): + parser = argparse.ArgumentParser(description='Manage and upload updates.json') + parser.add_argument('--stream', required=True, help='Stream name (e.g. "stable")') + parser.add_argument('--url', required=True, help='Base URL to download JSON from') + parser.add_argument('--version', required=True, help='Version information') + parser.add_argument('--user', required=True, help='Username for SCP') + parser.add_argument('--host', required=True, help='Host for SCP') + parser.add_argument('--path', required=True, help='Path to upload the file via SCP') + parser.add_argument('--key', required=True, help='Path to SSH private key file') + + args = parser.parse_args() + + json_url = f"{args.url}/{args.stream}.json" + json_data = download_json(json_url) + modified_json = modify_json(json_data, args.version) + temp_file_path = save_temp_file(modified_json) + + upload_file(temp_file_path, args.user, args.host, args.path, args.stream, args.key) + os.remove(temp_file_path) + +if __name__ == '__main__': + main()