Update project base domains after deployment is deleted

This commit is contained in:
IshaVenikar 2024-10-11 13:27:50 +05:30 committed by Nabarun
parent 55e238d0b9
commit f374fa69ff
3 changed files with 39 additions and 26 deletions

View File

@ -14,8 +14,9 @@ import {
ApplicationDeploymentRequest, ApplicationDeploymentRequest,
ApplicationDeploymentRemovalRequest ApplicationDeploymentRemovalRequest
} from './entity/Deployment'; } from './entity/Deployment';
import { AppDeploymentRecord, AppDeploymentRemovalRecord, Auction, AuctionData, PackageJSON } from './types'; import { AppDeploymentRecord, AppDeploymentRemovalRecord, AuctionData, PackageJSON } from './types';
import { getConfig, sleep } from './utils'; import { getConfig, sleep } from './utils';
import { Auction } from '@cerc-io/registry-sdk/dist/proto/cerc/auction/v1/auction';
const log = debug('snowball:registry'); const log = debug('snowball:registry');
@ -329,7 +330,7 @@ export class Registry {
}; };
} }
async getAuctionWinners( async getAuctionWinningDeployers(
auctionId: string auctionId: string
): Promise<string[]> { ): Promise<string[]> {
const records = await this.registry.getAuctionsByIds([auctionId]); const records = await this.registry.getAuctionsByIds([auctionId]);
@ -372,11 +373,12 @@ export class Registry {
true true
); );
// Filter records with ApplicationRecord ID and Deployment specific URL // Filter records with ApplicationDeploymentRequestId ID and Deployment specific URL
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)
) )
); );
} }

View File

@ -274,7 +274,7 @@ type Mutation {
): Project! ): Project!
addProject( addProject(
organizationSlug: String! organizationSlug: String!
data: AddProjectInput data: AddProjectInput!
lrn: String lrn: String
auctionData: AuctionData auctionData: AuctionData
): Project! ): Project!

View File

@ -19,7 +19,6 @@ import {
AddProjectFromTemplateInput, AddProjectFromTemplateInput,
AppDeploymentRecord, AppDeploymentRecord,
AppDeploymentRemovalRecord, AppDeploymentRemovalRecord,
Auction,
AuctionData, AuctionData,
GitPushEventPayload, GitPushEventPayload,
PackageJSON, PackageJSON,
@ -41,7 +40,7 @@ interface Config {
export class Service { export class Service {
private db: Database; private db: Database;
private oauthApp: OAuthApp; private oauthApp: OAuthApp;
private registry: Registry; private laconicRegistry: Registry;
private config: Config; private config: Config;
private deployRecordCheckTimeout?: NodeJS.Timeout; private deployRecordCheckTimeout?: NodeJS.Timeout;
@ -49,7 +48,7 @@ export class Service {
constructor(config: Config, db: Database, app: OAuthApp, registry: Registry) { constructor(config: Config, db: Database, app: OAuthApp, registry: Registry) {
this.db = db; this.db = db;
this.oauthApp = app; this.oauthApp = app;
this.registry = registry; this.laconicRegistry = registry;
this.config = config; this.config = config;
this.init(); this.init();
} }
@ -112,7 +111,7 @@ export class Service {
} }
// Fetch ApplicationDeploymentRecord for deployments // Fetch ApplicationDeploymentRecord for deployments
const records = await this.registry.getDeploymentRecords(deployments); const records = await this.laconicRegistry.getDeploymentRecords(deployments);
log(`Found ${records.length} ApplicationDeploymentRecords`); log(`Found ${records.length} ApplicationDeploymentRecords`);
// Update deployments for which ApplicationDeploymentRecords were returned // Update deployments for which ApplicationDeploymentRecords were returned
@ -145,7 +144,7 @@ export class Service {
// Fetch ApplicationDeploymentRemovalRecords for deployments // Fetch ApplicationDeploymentRemovalRecords for deployments
const records = const records =
await this.registry.getDeploymentRemovalRecords(deployments); await this.laconicRegistry.getDeploymentRemovalRecords(deployments);
log(`Found ${records.length} ApplicationDeploymentRemovalRecords`); log(`Found ${records.length} ApplicationDeploymentRemovalRecords`);
// Update deployments for which ApplicationDeploymentRemovalRecords were returned // Update deployments for which ApplicationDeploymentRemovalRecords were returned
@ -205,6 +204,11 @@ export class Service {
// Update deployment data for ApplicationDeploymentRecords // Update deployment data for ApplicationDeploymentRecords
const deploymentUpdatePromises = records.map(async (record) => { const deploymentUpdatePromises = records.map(async (record) => {
const deployment = recordToDeploymentsMap[record.attributes.request]; const deployment = recordToDeploymentsMap[record.attributes.request];
const project = await this.getProjectById(deployment.projectId)
assert(project)
const parts = record.attributes.url.replace('https://', '').split('.');
const baseDomain = parts.slice(1).join('.');
await this.db.updateDeploymentById(deployment.id, { await this.db.updateDeploymentById(deployment.id, {
applicationDeploymentRecordId: record.id, applicationDeploymentRecordId: record.id,
@ -273,6 +277,13 @@ export class Service {
); );
await this.db.deleteDeploymentById(deployment.id); await this.db.deleteDeploymentById(deployment.id);
const project = await this.db.getProjectById(deployment.projectId);
const updatedBaseDomains = project!.baseDomains!.filter(baseDomain => baseDomain !== deployment.baseDomain);
await this.db.updateProjectById(deployment.projectId, {
baseDomains: updatedBaseDomains
});
}); });
await Promise.all(deploymentUpdatePromises); await Promise.all(deploymentUpdatePromises);
@ -294,7 +305,7 @@ export class Service {
const projects = allProjects.filter(project => project.deployments.length === 0); const projects = allProjects.filter(project => project.deployments.length === 0);
const auctionIds = projects.map((project) => project.auctionId); const auctionIds = projects.map((project) => project.auctionId);
const completedAuctionIds = await this.registry.getCompletedAuctionIds(auctionIds); const completedAuctionIds = await this.laconicRegistry.getCompletedAuctionIds(auctionIds);
if (completedAuctionIds) { if (completedAuctionIds) {
const projectsToBedeployed = projects.filter((project) => const projectsToBedeployed = projects.filter((project) =>
@ -302,12 +313,12 @@ export class Service {
); );
for (const project of projectsToBedeployed) { for (const project of projectsToBedeployed) {
const deployerLrns = await this.registry.getAuctionWinners(project!.auctionId!); const deployerLrns = await this.laconicRegistry.getAuctionWinningDeployers(project!.auctionId!);
// Update project with deployer LRNs // Update project with deployer LRNs
await this.db.updateProjectById(project.id!, { await this.db.updateProjectById(project.id!, {
deployerLrns deployerLrns
}) });
for (const deployer of deployerLrns) { for (const deployer of deployerLrns) {
await this.createDeploymentFromAuction(project, deployer); await this.createDeploymentFromAuction(project, deployer);
@ -619,7 +630,7 @@ export class Service {
// TODO: Set environment variables for each deployment (environment variables can`t be set in application record) // TODO: Set environment variables for each deployment (environment variables can`t be set in application record)
const { applicationRecordId, applicationRecordData } = const { applicationRecordId, applicationRecordData } =
await this.registry.createApplicationRecord({ await this.laconicRegistry.createApplicationRecord({
appName: repo, appName: repo,
packageJSON, packageJSON,
appType: data.project!.template!, appType: data.project!.template!,
@ -678,7 +689,7 @@ export class Service {
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.registry.createApplicationDeploymentRequest({ await this.laconicRegistry.createApplicationDeploymentRequest({
deployment: newDeployment, deployment: newDeployment,
appName: repo, appName: repo,
repository: repoUrl, repository: repoUrl,
@ -689,7 +700,7 @@ export class Service {
} }
const { applicationDeploymentRequestId, applicationDeploymentRequestData } = const { applicationDeploymentRequestId, applicationDeploymentRequestData } =
await this.registry.createApplicationDeploymentRequest({ await this.laconicRegistry.createApplicationDeploymentRequest({
deployment: newDeployment, deployment: newDeployment,
appName: repo, appName: repo,
repository: repoUrl, repository: repoUrl,
@ -734,8 +745,8 @@ export class Service {
per_page: 1, per_page: 1,
}); });
const lrn = this.registry.getLrn(repo); const lrn = this.laconicRegistry.getLrn(repo);
const [record] = await this.registry.getRecordsByName(lrn); const [record] = await this.laconicRegistry.getRecordsByName(lrn);
const applicationRecordId = record.id; const applicationRecordId = record.id;
const applicationRecordData = record.attributes; const applicationRecordData = record.attributes;
@ -787,7 +798,7 @@ export class Service {
if (deploymentData.environment === Environment.Production) { if (deploymentData.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.registry.createApplicationDeploymentRequest({ await this.laconicRegistry.createApplicationDeploymentRequest({
deployment: newDeployment, deployment: newDeployment,
appName: repo, appName: repo,
repository: repoUrl, repository: repoUrl,
@ -800,7 +811,7 @@ export class Service {
const { applicationDeploymentRequestId, applicationDeploymentRequestData } = const { applicationDeploymentRequestId, applicationDeploymentRequestData } =
// Create requests for all the deployers // Create requests for all the deployers
await this.registry.createApplicationDeploymentRequest({ await this.laconicRegistry.createApplicationDeploymentRequest({
deployment: newDeployment, deployment: newDeployment,
appName: repo, appName: repo,
repository: repoUrl, repository: repoUrl,
@ -908,7 +919,7 @@ export class Service {
}; };
if (auctionData) { if (auctionData) {
const { applicationDeploymentAuctionId } = await this.registry.createApplicationDeploymentAuction(repo, octokit, auctionData!, deploymentData); const { applicationDeploymentAuctionId } = await this.laconicRegistry.createApplicationDeploymentAuction(repo, octokit, auctionData!, deploymentData);
await this.updateProject(project.id, { auctionId: applicationDeploymentAuctionId }) await this.updateProject(project.id, { auctionId: applicationDeploymentAuctionId })
} else { } else {
await this.createDeployment(user.id, octokit, deploymentData, lrn!); await this.createDeployment(user.id, octokit, deploymentData, lrn!);
@ -1127,7 +1138,7 @@ export class Service {
const currentDeploymentURL = `https://${(deployment.project.name).toLowerCase()}.${deployment.baseDomain}`; const currentDeploymentURL = `https://${(deployment.project.name).toLowerCase()}.${deployment.baseDomain}`;
const deploymentRecords = const deploymentRecords =
await this.registry.getDeploymentRecordsByFilter({ await this.laconicRegistry.getDeploymentRecordsByFilter({
application: deployment.applicationRecordId, application: deployment.applicationRecordId,
url: currentDeploymentURL, url: currentDeploymentURL,
}); });
@ -1140,14 +1151,14 @@ export class Service {
return false; return false;
} }
await this.registry.createApplicationDeploymentRemovalRequest({ await this.laconicRegistry.createApplicationDeploymentRemovalRequest({
deploymentId: deploymentRecords[0].id, deploymentId: deploymentRecords[0].id,
deployerLrn: deployment.deployerLrn deployerLrn: deployment.deployerLrn
}); });
} }
const result = const result =
await this.registry.createApplicationDeploymentRemovalRequest({ await this.laconicRegistry.createApplicationDeploymentRemovalRequest({
deploymentId: deployment.applicationDeploymentRecordId, deploymentId: deployment.applicationDeploymentRecordId,
deployerLrn: deployment.deployerLrn deployerLrn: deployment.deployerLrn
}); });
@ -1287,8 +1298,8 @@ export class Service {
async getAuctionData( async getAuctionData(
auctionId: string auctionId: string
): Promise<Auction> { ): Promise<any> {
const auctions = await this.registry.getAuctionData(auctionId); const auctions = await this.laconicRegistry.getAuctionData(auctionId);
return auctions[0]; return auctions[0];
} }
} }