From 9810b46f7fccfbde68a1fa956140594b64fab336 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Tue, 8 Oct 2024 12:51:35 +0530 Subject: [PATCH] Fix gpg key usage when uploading config to multiple deployers --- .../webapp/request_webapp_deployment.py | 197 +++++++++--------- 1 file changed, 99 insertions(+), 98 deletions(-) diff --git a/stack_orchestrator/deploy/webapp/request_webapp_deployment.py b/stack_orchestrator/deploy/webapp/request_webapp_deployment.py index 097dcc0e..6de17e4a 100644 --- a/stack_orchestrator/deploy/webapp/request_webapp_deployment.py +++ b/stack_orchestrator/deploy/webapp/request_webapp_deployment.py @@ -97,69 +97,70 @@ def command( # noqa: C901 if env_file and config_ref: fatal("Cannot use --env-file and --config-ref at the same time.") - tempdir = tempfile.mkdtemp() - try: - laconic = LaconicRegistryClient(laconic_config) + laconic = LaconicRegistryClient(laconic_config) - app_record = laconic.get_record(app) - if not app_record: - fatal(f"Unable to locate app: {app}") + app_record = laconic.get_record(app) + if not app_record: + fatal(f"Unable to locate app: {app}") - # Deployers to send requests to - deployer_records = [] + # Deployers to send requests to + deployer_records = [] - auction = None - auction_winners = None - if auction_id: - # Fetch auction details - auction = laconic.get_auction(auction_id) - if not auction: - fatal(f"Unable to locate auction: {auction_id}") + auction = None + auction_winners = None + if auction_id: + # Fetch auction details + auction = laconic.get_auction(auction_id) + if not auction: + fatal(f"Unable to locate auction: {auction_id}") - # Check auction owner - if auction.ownerAddress != laconic.whoami().address: - fatal(f"Auction {auction_id} owner mismatch") + # Check auction owner + if auction.ownerAddress != laconic.whoami().address: + fatal(f"Auction {auction_id} owner mismatch") - # Check auction kind - if auction.kind != AUCTION_KIND_PROVIDER: - fatal(f"Auction kind needs to be ${AUCTION_KIND_PROVIDER}, got {auction.kind}") + # Check auction kind + if auction.kind != AUCTION_KIND_PROVIDER: + fatal(f"Auction kind needs to be ${AUCTION_KIND_PROVIDER}, got {auction.kind}") - # Check auction status - if auction.status != AuctionStatus.COMPLETED: - fatal(f"Auction {auction_id} not completed yet, status {auction.status}") + # Check auction status + if auction.status != AuctionStatus.COMPLETED: + fatal(f"Auction {auction_id} not completed yet, status {auction.status}") - # Check that winner list is not empty - if len(auction.winnerAddresses) == 0: - fatal(f"Auction {auction_id} has no winners") + # Check that winner list is not empty + if len(auction.winnerAddresses) == 0: + fatal(f"Auction {auction_id} has no winners") - auction_winners = auction.winnerAddresses + auction_winners = auction.winnerAddresses - # Get deployer record for all the auction winners - for auction_winner in auction_winners: - # TODO: Match auction winner address with provider address? - deployer_records_by_owner = laconic.webapp_deployers({"paymentAddress": auction_winner}) - if len(deployer_records_by_owner) == 0: - print(f"WARNING: Unable to locate deployer for auction winner {auction_winner}") + # Get deployer record for all the auction winners + for auction_winner in auction_winners: + # TODO: Match auction winner address with provider address? + deployer_records_by_owner = laconic.webapp_deployers({"paymentAddress": auction_winner}) + if len(deployer_records_by_owner) == 0: + print(f"WARNING: Unable to locate deployer for auction winner {auction_winner}") - # Take first record with name set - target_deployer_record = deployer_records_by_owner[0] - for r in deployer_records_by_owner: - if len(r.names) > 0: - target_deployer_record = r - break - deployer_records.append(target_deployer_record) - else: - deployer_record = laconic.get_record(deployer) - if not deployer_record: - fatal(f"Unable to locate deployer: {deployer}") + # Take first record with name set + target_deployer_record = deployer_records_by_owner[0] + for r in deployer_records_by_owner: + if len(r.names) > 0: + target_deployer_record = r + break + print("target_deployer_record", target_deployer_record) + deployer_records.append(target_deployer_record) + else: + deployer_record = laconic.get_record(deployer) + if not deployer_record: + fatal(f"Unable to locate deployer: {deployer}") - deployer_records.append(deployer_record) + deployer_records.append(deployer_record) - # Create and send request to each deployer - deployment_requests = [] - for deployer_record in deployer_records: - # If env_file - if env_file: + # Create and send request to each deployer + deployment_requests = [] + for deployer_record in deployer_records: + # Upload config to deployers if env_file is passed + if env_file: + tempdir = tempfile.mkdtemp() + try: gpg = gnupg.GPG(gnupghome=tempdir) # Import the deployer's public key @@ -194,59 +195,59 @@ def command( # noqa: C901 response.raise_for_status() config_ref = response.json()["id"] + finally: + shutil.rmtree(tempdir, ignore_errors=True) - target_deployer = deployer - if (not deployer) and len(deployer_record.names): - target_deployer = deployer_record.names[0] + target_deployer = deployer + if (not deployer) and len(deployer_record.names): + target_deployer = deployer_record.names[0] - deployment_request = { - "record": { - "type": "ApplicationDeploymentRequest", - "application": app, - "version": "1.0.0", - "name": f"{app_record.attributes.name}@{app_record.attributes.version}", - "deployer": target_deployer, - "meta": {"when": str(datetime.utcnow())}, - } + deployment_request = { + "record": { + "type": "ApplicationDeploymentRequest", + "application": app, + "version": "1.0.0", + "name": f"{app_record.attributes.name}@{app_record.attributes.version}", + "deployer": target_deployer, + "meta": {"when": str(datetime.utcnow())}, } + } - if auction_id: - deployment_request["record"]["auction"] = auction_id + if auction_id: + deployment_request["record"]["auction"] = auction_id - if config_ref: - deployment_request["record"]["config"] = {"ref": config_ref} + if config_ref: + deployment_request["record"]["config"] = {"ref": config_ref} - if dns: - deployment_request["record"]["dns"] = dns.lower() + if dns: + deployment_request["record"]["dns"] = dns.lower() - if make_payment: - amount = 0 - if dry_run: - deployment_request["record"]["payment"] = "DRY_RUN" - elif "auto" == make_payment: - if "minimumPayment" in deployer_record.attributes: - amount = int( - deployer_record.attributes.minimumPayment.replace("alnt", "") - ) - else: - amount = make_payment - if amount: - receipt = laconic.send_tokens( - deployer_record.attributes.paymentAddress, amount - ) - deployment_request["record"]["payment"] = receipt.tx.hash - print("Payment TX:", receipt.tx.hash) - elif use_payment: - deployment_request["record"]["payment"] = use_payment - - deployment_requests.append(deployment_request) - - # Send all requests - for deployment_request in deployment_requests: + if make_payment: + amount = 0 if dry_run: - print(yaml.dump(deployment_request)) - continue + deployment_request["record"]["payment"] = "DRY_RUN" + elif "auto" == make_payment: + if "minimumPayment" in deployer_record.attributes: + amount = int( + deployer_record.attributes.minimumPayment.replace("alnt", "") + ) + else: + amount = make_payment + if amount: + receipt = laconic.send_tokens( + deployer_record.attributes.paymentAddress, amount + ) + deployment_request["record"]["payment"] = receipt.tx.hash + print("Payment TX:", receipt.tx.hash) + elif use_payment: + deployment_request["record"]["payment"] = use_payment - laconic.publish(deployment_request) - finally: - shutil.rmtree(tempdir, ignore_errors=True) + deployment_requests.append(deployment_request) + + # Send all requests + for deployment_request in deployment_requests: + if dry_run: + print(yaml.dump(deployment_request)) + continue + + laconic.publish(deployment_request)