Store DNS deployment records data in Deployment entity

This commit is contained in:
IshaVenikar 2025-01-31 12:50:15 +05:30
parent 55500b5914
commit 13892015a5
3 changed files with 51 additions and 37 deletions

View File

@ -105,15 +105,24 @@ export class Deployment {
@Column('varchar', { nullable: true }) @Column('varchar', { nullable: true })
applicationDeploymentRequestId!: string | null; applicationDeploymentRequestId!: string | null;
@Column('varchar', { nullable: true })
dnsDeploymentRequestId!: string | null;
@Column('simple-json', { nullable: true }) @Column('simple-json', { nullable: true })
applicationDeploymentRequestData!: ApplicationDeploymentRequest | null; applicationDeploymentRequestData!: ApplicationDeploymentRequest | null;
@Column('varchar', { nullable: true }) @Column('varchar', { nullable: true })
applicationDeploymentRecordId!: string | null; applicationDeploymentRecordId!: string | null;
@Column('varchar', { nullable: true })
dnsDeploymentRecordId!: string | null;
@Column('simple-json', { nullable: true }) @Column('simple-json', { nullable: true })
applicationDeploymentRecordData!: AppDeploymentRecordAttributes | null; applicationDeploymentRecordData!: AppDeploymentRecordAttributes | null;
@Column('simple-json', { nullable: true })
dnsDeploymentRecordData!: AppDeploymentRecordAttributes | null;
@Column('varchar', { nullable: true }) @Column('varchar', { nullable: true })
applicationDeploymentRemovalRequestId!: string | null; applicationDeploymentRemovalRequestId!: string | null;

View File

@ -387,8 +387,8 @@ export class Registry {
return records.filter((record: AppDeploymentRecord) => return records.filter((record: AppDeploymentRecord) =>
deployments.some( deployments.some(
(deployment) => (deployment) =>
deployment.applicationDeploymentRequestId === record.attributes.request && deployment.applicationDeploymentRequestId === record.attributes.request ||
record.attributes.url.includes(deployment.id) deployment.dnsDeploymentRequestId === record.attributes.request
) )
); );
} }
@ -444,9 +444,9 @@ export class Registry {
} }
/** /**
* Fetch deployment DNS Records by Id * Fetch deployment DNS record by Id
*/ */
async getDNSRecordsById(id: string): Promise<DNSRecord[]> { async getDNSRecordById(id: string): Promise<DNSRecord[]> {
return this.registry.getRecordsByIds([id]); return this.registry.getRecordsByIds([id]);
} }

View File

@ -185,23 +185,28 @@ export class Service {
}, },
}); });
const recordToDeploymentsMap = deployments.reduce( const requestIdToRecordsMap = records.reduce(
(acc: { [key: string]: Deployment }, deployment) => { (acc: { [key: string]: AppDeploymentRecord }, record) => {
acc[deployment.applicationDeploymentRequestId!] = deployment; acc[record.attributes.request] = record;
return acc; return acc;
}, },
{}, {},
); );
// Update deployment data for ApplicationDeploymentRecords // Update deployment data for ApplicationDeploymentRecords
const deploymentUpdatePromises = records.map(async (record) => { const deploymentUpdatePromises = deployments.map(async (deployment) => {
const deployment = recordToDeploymentsMap[record.attributes.request]; const applicationDeploymentRecord = requestIdToRecordsMap[deployment.applicationDeploymentRequestId!]
const dnsDeploymentRecord = requestIdToRecordsMap[deployment.dnsDeploymentRequestId!]
if (!applicationDeploymentRecord || !dnsDeploymentRecord) {
return;
}
if (!deployment.project) { if (!deployment.project) {
log(`Project ${deployment.projectId} not found`); log(`Project ${deployment.projectId} not found`);
return; return;
} else { } else {
const dnsRecords = await this.laconicRegistry.getDNSRecordsById(record.attributes.dns); const dnsRecords = await this.laconicRegistry.getDNSRecordById(applicationDeploymentRecord.attributes.dns);
const dnsRecordData: DNSRecordAttributes = { const dnsRecordData: DNSRecordAttributes = {
name: dnsRecords[0].attributes.name, name: dnsRecords[0].attributes.name,
@ -211,9 +216,11 @@ export class Service {
version: dnsRecords[0].attributes.version, version: dnsRecords[0].attributes.version,
} }
deployment.applicationDeploymentRecordId = record.id; deployment.applicationDeploymentRecordId = applicationDeploymentRecord.id;
deployment.applicationDeploymentRecordData = record.attributes; deployment.dnsDeploymentRecordId = dnsDeploymentRecord.id;
deployment.url = record.attributes.url; deployment.applicationDeploymentRecordData = applicationDeploymentRecord.attributes;
deployment.dnsDeploymentRecordData = dnsDeploymentRecord.attributes;
deployment.url = applicationDeploymentRecord.attributes.url;
deployment.status = DeploymentStatus.Ready; deployment.status = DeploymentStatus.Ready;
deployment.isCurrent = deployment.environment === Environment.Production; deployment.isCurrent = deployment.environment === Environment.Production;
deployment.dnsRecordData = dnsRecordData; deployment.dnsRecordData = dnsRecordData;
@ -233,18 +240,12 @@ export class Service {
} }
log( log(
`Updated deployment ${deployment.id} with URL ${record.attributes.url}`, `Updated deployment ${deployment.id} with URL ${applicationDeploymentRecord.attributes.url}`,
); );
} }
});
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 // Set the isCurrent state to false for the old deployments
for (const deployment of prodDeployments) { if (deployment.isCurrent) {
const projectDeployments = await this.db.getDeploymentsByProjectId(deployment.projectId); const projectDeployments = await this.db.getDeploymentsByProjectId(deployment.projectId);
const oldDeployments = projectDeployments const oldDeployments = projectDeployments
.filter(projectDeployment => projectDeployment.deployer.deployerLrn === deployment.deployer.deployerLrn && projectDeployment.id !== deployment.id); .filter(projectDeployment => projectDeployment.deployer.deployerLrn === deployment.deployer.deployerLrn && projectDeployment.id !== deployment.id);
@ -255,6 +256,7 @@ export class Service {
); );
} }
} }
});
await Promise.all(deploymentUpdatePromises); await Promise.all(deploymentUpdatePromises);
} }
@ -666,10 +668,11 @@ export class Service {
const domain = await this.db.getOldestDomainByProjectId(data.project!.id!); const domain = await this.db.getOldestDomainByProjectId(data.project!.id!);
// To set project DNS // To set project DNS
let dnsDeploymentRequestId = null;
if (data.environment === Environment.Production) { if (data.environment === Environment.Production) {
// On deleting deployment later, project DNS deployment is also deleted // 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 // 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, deployment: newDeployment,
appName: repo, appName: repo,
repository: repoUrl, repository: repoUrl,
@ -682,6 +685,7 @@ export class Service {
requesterAddress: address, requesterAddress: address,
publicKey: deployer!.publicKey! publicKey: deployer!.publicKey!
}); });
dnsDeploymentRequestId = dnsRequest.applicationDeploymentRequestId;
} }
const { applicationDeploymentRequestId, applicationDeploymentRequestData } = const { applicationDeploymentRequestId, applicationDeploymentRequestData } =
@ -702,6 +706,7 @@ export class Service {
await this.db.updateDeploymentById(newDeployment.id, { await this.db.updateDeploymentById(newDeployment.id, {
applicationDeploymentRequestId, applicationDeploymentRequestId,
applicationDeploymentRequestData, applicationDeploymentRequestData,
dnsDeploymentRequestId
}); });
return newDeployment; return newDeployment;