From 13892015a5ae87b9f14fba525f2365417ec07aa9 Mon Sep 17 00:00:00 2001 From: IshaVenikar Date: Fri, 31 Jan 2025 12:50:15 +0530 Subject: [PATCH] Store DNS deployment records data in Deployment entity --- packages/backend/src/entity/Deployment.ts | 9 ++++ packages/backend/src/registry.ts | 16 +++--- packages/backend/src/service.ts | 63 ++++++++++++----------- 3 files changed, 51 insertions(+), 37 deletions(-) diff --git a/packages/backend/src/entity/Deployment.ts b/packages/backend/src/entity/Deployment.ts index 021cfc75..f1477f1a 100644 --- a/packages/backend/src/entity/Deployment.ts +++ b/packages/backend/src/entity/Deployment.ts @@ -105,15 +105,24 @@ export class Deployment { @Column('varchar', { nullable: true }) applicationDeploymentRequestId!: string | null; + @Column('varchar', { nullable: true }) + dnsDeploymentRequestId!: string | null; + @Column('simple-json', { nullable: true }) applicationDeploymentRequestData!: ApplicationDeploymentRequest | null; @Column('varchar', { nullable: true }) applicationDeploymentRecordId!: string | null; + @Column('varchar', { nullable: true }) + dnsDeploymentRecordId!: string | null; + @Column('simple-json', { nullable: true }) applicationDeploymentRecordData!: AppDeploymentRecordAttributes | null; + @Column('simple-json', { nullable: true }) + dnsDeploymentRecordData!: AppDeploymentRecordAttributes | null; + @Column('varchar', { nullable: true }) applicationDeploymentRemovalRequestId!: string | null; diff --git a/packages/backend/src/registry.ts b/packages/backend/src/registry.ts index 664f34d1..66f4840f 100644 --- a/packages/backend/src/registry.ts +++ b/packages/backend/src/registry.ts @@ -387,8 +387,8 @@ export class Registry { return records.filter((record: AppDeploymentRecord) => deployments.some( (deployment) => - deployment.applicationDeploymentRequestId === record.attributes.request && - record.attributes.url.includes(deployment.id) + deployment.applicationDeploymentRequestId === record.attributes.request || + deployment.dnsDeploymentRequestId === record.attributes.request ) ); } @@ -443,12 +443,12 @@ export class Registry { ); } - /** - * Fetch deployment DNS Records by Id - */ - async getDNSRecordsById(id: string): Promise { - return this.registry.getRecordsByIds([id]); - } + /** + * Fetch deployment DNS record by Id + */ + async getDNSRecordById(id: string): Promise { + return this.registry.getRecordsByIds([id]); + } async createApplicationDeploymentRemovalRequest(data: { deploymentId: string; diff --git a/packages/backend/src/service.ts b/packages/backend/src/service.ts index 0df4c829..656781d8 100644 --- a/packages/backend/src/service.ts +++ b/packages/backend/src/service.ts @@ -185,23 +185,28 @@ export class Service { }, }); - const recordToDeploymentsMap = deployments.reduce( - (acc: { [key: string]: Deployment }, deployment) => { - acc[deployment.applicationDeploymentRequestId!] = deployment; + const requestIdToRecordsMap = records.reduce( + (acc: { [key: string]: AppDeploymentRecord }, record) => { + acc[record.attributes.request] = record; return acc; }, {}, ); // Update deployment data for ApplicationDeploymentRecords - const deploymentUpdatePromises = records.map(async (record) => { - const deployment = recordToDeploymentsMap[record.attributes.request]; + const deploymentUpdatePromises = deployments.map(async (deployment) => { + const applicationDeploymentRecord = requestIdToRecordsMap[deployment.applicationDeploymentRequestId!] + const dnsDeploymentRecord = requestIdToRecordsMap[deployment.dnsDeploymentRequestId!] + + if (!applicationDeploymentRecord || !dnsDeploymentRecord) { + return; + } if (!deployment.project) { log(`Project ${deployment.projectId} not found`); return; } else { - const dnsRecords = await this.laconicRegistry.getDNSRecordsById(record.attributes.dns); + const dnsRecords = await this.laconicRegistry.getDNSRecordById(applicationDeploymentRecord.attributes.dns); const dnsRecordData: DNSRecordAttributes = { name: dnsRecords[0].attributes.name, @@ -211,9 +216,11 @@ export class Service { version: dnsRecords[0].attributes.version, } - deployment.applicationDeploymentRecordId = record.id; - deployment.applicationDeploymentRecordData = record.attributes; - deployment.url = record.attributes.url; + deployment.applicationDeploymentRecordId = applicationDeploymentRecord.id; + deployment.dnsDeploymentRecordId = dnsDeploymentRecord.id; + deployment.applicationDeploymentRecordData = applicationDeploymentRecord.attributes; + deployment.dnsDeploymentRecordData = dnsDeploymentRecord.attributes; + deployment.url = applicationDeploymentRecord.attributes.url; deployment.status = DeploymentStatus.Ready; deployment.isCurrent = deployment.environment === Environment.Production; deployment.dnsRecordData = dnsRecordData; @@ -233,30 +240,25 @@ export class Service { } log( - `Updated deployment ${deployment.id} with URL ${record.attributes.url}`, + `Updated deployment ${deployment.id} with URL ${applicationDeploymentRecord.attributes.url}`, ); } + + // Set the isCurrent state to false for the old deployments + if (deployment.isCurrent) { + const projectDeployments = await this.db.getDeploymentsByProjectId(deployment.projectId); + const oldDeployments = projectDeployments + .filter(projectDeployment => projectDeployment.deployer.deployerLrn === deployment.deployer.deployerLrn && projectDeployment.id !== deployment.id); + for (const oldDeployment of oldDeployments) { + await this.db.updateDeployment( + { id: oldDeployment.id }, + { isCurrent: false } + ); + } + } }); await Promise.all(deploymentUpdatePromises); - - // Get deployments that are in production environment - const prodDeployments = Object.values(recordToDeploymentsMap).filter(deployment => deployment.isCurrent); - - // Set the isCurrent state to false for the old deployments - for (const deployment of prodDeployments) { - const projectDeployments = await this.db.getDeploymentsByProjectId(deployment.projectId); - const oldDeployments = projectDeployments - .filter(projectDeployment => projectDeployment.deployer.deployerLrn === deployment.deployer.deployerLrn && projectDeployment.id !== deployment.id); - for (const oldDeployment of oldDeployments) { - await this.db.updateDeployment( - { id: oldDeployment.id }, - { isCurrent: false } - ); - } - } - - await Promise.all(deploymentUpdatePromises); } /** @@ -666,10 +668,11 @@ export class Service { const domain = await this.db.getOldestDomainByProjectId(data.project!.id!); // To set project DNS + let dnsDeploymentRequestId = null; if (data.environment === Environment.Production) { // On deleting deployment later, project DNS deployment is also deleted // So publish project DNS deployment first so that ApplicationDeploymentRecord for the same is available when deleting deployment later - await this.laconicRegistry.createApplicationDeploymentRequest({ + const dnsRequest = await this.laconicRegistry.createApplicationDeploymentRequest({ deployment: newDeployment, appName: repo, repository: repoUrl, @@ -682,6 +685,7 @@ export class Service { requesterAddress: address, publicKey: deployer!.publicKey! }); + dnsDeploymentRequestId = dnsRequest.applicationDeploymentRequestId; } const { applicationDeploymentRequestId, applicationDeploymentRequestData } = @@ -702,6 +706,7 @@ export class Service { await this.db.updateDeploymentById(newDeployment.id, { applicationDeploymentRequestId, applicationDeploymentRequestData, + dnsDeploymentRequestId }); return newDeployment;