[WIP] Add support for multiple custom domains in single deployment #51

Draft
nabarun wants to merge 5 commits from iv-multi-custom-domains into custom-domain-for-auctions
5 changed files with 233 additions and 107 deletions

View File

@ -613,26 +613,27 @@ export class Database {
id: projectId id: projectId
}, },
...filter ...filter
} },
order: {
createdAt: 'DESC',
},
}); });
return domains; return domains;
} }
async getOldestDomainByProjectId( async getOldestDomainByProjectId(projectId: string): Promise<Domain | null> {
projectId: string,
): Promise<Domain | null> {
const domainRepository = this.dataSource.getRepository(Domain); const domainRepository = this.dataSource.getRepository(Domain);
const domain = await domainRepository.findOne({ const domain = await domainRepository.findOne({
where: { where: {
project: { project: {
id: projectId id: projectId,
}, },
}, },
order: { order: {
createdAt: 'ASC' createdAt: 'ASC',
} },
}); });
return domain; return domain;

View File

@ -24,6 +24,9 @@ export class Deployer {
@Column('varchar', { nullable: true }) @Column('varchar', { nullable: true })
paymentAddress!: string | null; paymentAddress!: string | null;
@Column('varchar', { nullable: true })
version!: string | null;
@ManyToMany(() => Project, (project) => project.deployers) @ManyToMany(() => Project, (project) => project.deployers)
projects!: Project[]; projects!: Project[];
} }

View File

@ -272,8 +272,8 @@ type AppDeploymentRecordAttributes {
} }
input AuctionParams { input AuctionParams {
maxPrice: String, maxPrice: String
numProviders: Int, numProviders: Int
} }
type Query { type Query {

View File

@ -7,7 +7,12 @@ import { DateTime } from 'luxon';
import { OAuthApp } from '@octokit/oauth-app'; import { OAuthApp } from '@octokit/oauth-app';
import { Database } from './database'; import { Database } from './database';
import { ApplicationRecord, Deployment, DeploymentStatus, Environment } from './entity/Deployment'; import {
ApplicationRecord,
Deployment,
DeploymentStatus,
Environment,
} from './entity/Deployment';
import { Domain } from './entity/Domain'; import { Domain } from './entity/Domain';
import { EnvironmentVariable } from './entity/EnvironmentVariable'; import { EnvironmentVariable } from './entity/EnvironmentVariable';
import { Organization } from './entity/Organization'; import { Organization } from './entity/Organization';
@ -119,7 +124,8 @@ export class Service {
} }
// Fetch ApplicationDeploymentRecord for deployments // Fetch ApplicationDeploymentRecord for deployments
const records = await this.laconicRegistry.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
@ -204,7 +210,9 @@ export class Service {
return; return;
} }
const registryRecord = await this.laconicRegistry.getRecordById(record.attributes.dns); const registryRecord = await this.laconicRegistry.getRecordById(
record.attributes.dns,
);
if (!registryRecord) { if (!registryRecord) {
log(`DNS record not found for deployment ${deployment.id}`); log(`DNS record not found for deployment ${deployment.id}`);
@ -219,7 +227,7 @@ export class Service {
resourceType: dnsRecord.attributes.resource_type, resourceType: dnsRecord.attributes.resource_type,
value: dnsRecord.attributes.value, value: dnsRecord.attributes.value,
version: dnsRecord.attributes.version, version: dnsRecord.attributes.version,
} };
deployment.applicationDeploymentRecordId = record.id; deployment.applicationDeploymentRecordId = record.id;
deployment.applicationDeploymentRecordData = record.attributes; deployment.applicationDeploymentRecordData = record.attributes;
@ -239,18 +247,38 @@ export class Service {
relations: { relations: {
project: true, project: true,
deployer: true, deployer: true,
} },
}); });
if (previousCanonicalDeployment) { if (previousCanonicalDeployment) {
// If all the DNS in the previous canonical deployment request are different from the new deployment request
// Send removal request for the previous canonical deployment and delete DB entry // Send removal request for the previous canonical deployment and delete DB entry
if (previousCanonicalDeployment.url !== deployment.url) { const previousDnsList =
await this.laconicRegistry.createApplicationDeploymentRemovalRequest({ previousCanonicalDeployment.applicationDeploymentRequestData?.dns ||
deploymentId: previousCanonicalDeployment.applicationDeploymentRecordId!, '';
const newDnsList =
deployment.applicationDeploymentRequestData?.dns || '';
const previousDnsSet = new Set(
previousDnsList.split(',').map((item) => item.trim()),
);
const newDnsSet = new Set(
newDnsList.split(',').map((item) => item.trim()),
);
const isMatch = [...previousDnsSet].some((item) => newDnsSet.has(item));
if (!isMatch) {
await this.laconicRegistry.createApplicationDeploymentRemovalRequest(
{
deploymentId:
previousCanonicalDeployment.applicationDeploymentRecordId!,
deployerLrn: previousCanonicalDeployment.deployer.deployerLrn, deployerLrn: previousCanonicalDeployment.deployer.deployerLrn,
auctionId: previousCanonicalDeployment.project.auctionId, auctionId: previousCanonicalDeployment.project.auctionId,
payment: previousCanonicalDeployment.project.txHash payment: previousCanonicalDeployment.project.txHash,
}); },
);
} }
await this.db.deleteDeploymentById(previousCanonicalDeployment.id); await this.db.deleteDeploymentById(previousCanonicalDeployment.id);
@ -261,7 +289,9 @@ export class Service {
// Release deployer funds on successful deployment // Release deployer funds on successful deployment
if (!deployment.project.fundsReleased) { if (!deployment.project.fundsReleased) {
const fundsReleased = await this.releaseDeployerFundsByProjectId(deployment.projectId); const fundsReleased = await this.releaseDeployerFundsByProjectId(
deployment.projectId,
);
// Return remaining amount to owner // Return remaining amount to owner
await this.returnUserFundsByProjectId(deployment.projectId, true); await this.returnUserFundsByProjectId(deployment.projectId, true);
@ -485,12 +515,17 @@ export class Service {
return dbProjects; return dbProjects;
} }
async getNonCanonicalDeploymentsByProjectId(projectId: string): Promise<Deployment[]> { async getNonCanonicalDeploymentsByProjectId(
const nonCanonicalDeployments = await this.db.getNonCanonicalDeploymentsByProjectId(projectId); projectId: string,
): Promise<Deployment[]> {
const nonCanonicalDeployments =
await this.db.getNonCanonicalDeploymentsByProjectId(projectId);
return nonCanonicalDeployments; return nonCanonicalDeployments;
} }
async getLatestDNSRecordByProjectId(projectId: string): Promise<DNSRecordAttributes | null> { async getLatestDNSRecordByProjectId(
projectId: string,
): Promise<DNSRecordAttributes | null> {
const dnsRecord = await this.db.getLatestDNSRecordByProjectId(projectId); const dnsRecord = await this.db.getLatestDNSRecordByProjectId(projectId);
return dnsRecord; return dnsRecord;
} }
@ -647,7 +682,10 @@ export class Service {
let newDeployment: Deployment; let newDeployment: Deployment;
if (oldDeployment.project.auctionId) { if (oldDeployment.project.auctionId) {
newDeployment = await this.createDeploymentFromAuction(oldDeployment.project, oldDeployment.deployer); newDeployment = await this.createDeploymentFromAuction(
oldDeployment.project,
oldDeployment.deployer,
);
} else { } else {
newDeployment = await this.createDeployment(user.id, octokit, { newDeployment = await this.createDeployment(user.id, octokit, {
project: oldDeployment.project, project: oldDeployment.project,
@ -655,9 +693,10 @@ export class Service {
environment: Environment.Production, environment: Environment.Production,
commitHash: oldDeployment.commitHash, commitHash: oldDeployment.commitHash,
commitMessage: oldDeployment.commitMessage, commitMessage: oldDeployment.commitMessage,
deployer: oldDeployment.deployer deployer: oldDeployment.deployer,
}); });
} }
return newDeployment; return newDeployment;
} }
@ -665,7 +704,7 @@ export class Service {
userId: string, userId: string,
octokit: Octokit, octokit: Octokit,
data: DeepPartial<Deployment>, data: DeepPartial<Deployment>,
deployerLrn?: string deployerLrn?: string,
): Promise<Deployment> { ): Promise<Deployment> {
assert(data.project?.repository, 'Project repository not found'); assert(data.project?.repository, 'Project repository not found');
log( log(
@ -688,33 +727,55 @@ export class Service {
deployer = data.deployer; deployer = data.deployer;
} }
const deployment = await this.createDeploymentFromData(userId, data, deployer!.deployerLrn!, applicationRecordId, applicationRecordData, false); const deployment = await this.createDeploymentFromData(
userId,
data,
deployer!.deployerLrn!,
applicationRecordId,
applicationRecordData,
false,
);
const address = await this.getAddress(); const address = await this.getAddress();
const { repo, repoUrl } = await getRepoDetails(octokit, data.project.repository, data.commitHash); const { repo, repoUrl } = await getRepoDetails(
const environmentVariablesObj = await this.getEnvVariables(data.project!.id!); octokit,
data.project.repository,
data.commitHash,
);
const environmentVariablesObj = await this.getEnvVariables(
data.project!.id!,
);
// To set project DNS // To set project DNS
if (data.environment === Environment.Production) { if (data.environment === Environment.Production) {
const canonicalDeployment = await this.createDeploymentFromData(userId, data, deployer!.deployerLrn!, applicationRecordId, applicationRecordData, true); const canonicalDeployment = await this.createDeploymentFromData(
// If a custom domain is present then use that as the DNS in the deployment request userId,
const customDomain = await this.db.getOldestDomainByProjectId(data.project!.id!); data,
deployer!.deployerLrn!,
applicationRecordId,
applicationRecordData,
true,
);
const dns = await this.getDnsForDeployerByProjectId(data.project.id!, deployer!.version, canonicalDeployment.project.name);
// On deleting deployment later, project canonical deployment is also deleted // On deleting deployment later, project canonical deployment is also deleted
// So publish project canonical deployment first so that ApplicationDeploymentRecord for the same is available when deleting deployment later // So publish project canonical deployment first so that ApplicationDeploymentRecord for the same is available when deleting deployment later
const { applicationDeploymentRequestData, applicationDeploymentRequestId } = const {
await this.laconicRegistry.createApplicationDeploymentRequest({ applicationDeploymentRequestData,
applicationDeploymentRequestId,
} = await this.laconicRegistry.createApplicationDeploymentRequest({
deployment: canonicalDeployment, deployment: canonicalDeployment,
appName: repo, appName: repo,
repository: repoUrl, repository: repoUrl,
environmentVariables: environmentVariablesObj, environmentVariables: environmentVariablesObj,
dns: customDomain?.name ?? `${canonicalDeployment.project.name}`, dns,
lrn: deployer!.deployerLrn!, lrn: deployer!.deployerLrn!,
apiUrl: deployer!.deployerApiUrl!, apiUrl: deployer!.deployerApiUrl!,
payment: data.project.txHash, payment: data.project.txHash,
auctionId: data.project.auctionId, auctionId: data.project.auctionId,
requesterAddress: address, requesterAddress: address,
publicKey: deployer!.publicKey! publicKey: deployer!.publicKey!,
}); });
await this.db.updateDeploymentById(canonicalDeployment.id, { await this.db.updateDeploymentById(canonicalDeployment.id, {
@ -735,7 +796,7 @@ export class Service {
payment: data.project.txHash, payment: data.project.txHash,
auctionId: data.project.auctionId, auctionId: data.project.auctionId,
requesterAddress: address, requesterAddress: address,
publicKey: deployer!.publicKey! publicKey: deployer!.publicKey!,
}); });
await this.db.updateDeploymentById(deployment.id, { await this.db.updateDeploymentById(deployment.id, {
@ -748,7 +809,7 @@ export class Service {
async createDeploymentFromAuction( async createDeploymentFromAuction(
project: DeepPartial<Project>, project: DeepPartial<Project>,
deployer: Deployer deployer: Deployer,
): Promise<Deployment> { ): Promise<Deployment> {
const octokit = await this.getOctokit(project.ownerId!); const octokit = await this.getOctokit(project.ownerId!);
const [owner, repo] = project.repository!.split('/'); const [owner, repo] = project.repository!.split('/');
@ -774,7 +835,7 @@ export class Service {
const applicationRecordId = record.id; const applicationRecordId = record.id;
const applicationRecordData = record.attributes; const applicationRecordData = record.attributes;
const deployerLrn = deployer!.deployerLrn const deployerLrn = deployer!.deployerLrn;
// Create deployment with prod branch and latest commit // Create deployment with prod branch and latest commit
const deploymentData = { const deploymentData = {
@ -786,30 +847,48 @@ export class Service {
commitMessage: latestCommit.commit.message, commitMessage: latestCommit.commit.message,
}; };
const deployment = await this.createDeploymentFromData(project.ownerId!, deploymentData, deployerLrn, applicationRecordId, applicationRecordData, false); const deployment = await this.createDeploymentFromData(
project.ownerId!,
deploymentData,
deployerLrn,
applicationRecordId,
applicationRecordData,
false,
);
const address = await this.getAddress(); const address = await this.getAddress();
const environmentVariablesObj = await this.getEnvVariables(project!.id!); const environmentVariablesObj = await this.getEnvVariables(project!.id!);
// To set project DNS // To set project DNS
if (deploymentData.environment === Environment.Production) { if (deploymentData.environment === Environment.Production) {
const canonicalDeployment = await this.createDeploymentFromData(project.ownerId!, deploymentData, deployerLrn, applicationRecordId, applicationRecordData, true); const canonicalDeployment = await this.createDeploymentFromData(
project.ownerId!,
deploymentData,
deployerLrn,
applicationRecordId,
applicationRecordData,
true,
);
// If a custom domain is present then use that as the DNS in the deployment request // If a custom domain is present then use that as the DNS in the deployment request
const customDomain = await this.db.getOldestDomainByProjectId(project!.id!); const dns = await this.getDnsForDeployerByProjectId(project.id!, deployer.version, project.name!);
// On deleting deployment later, project canonical deployment is also deleted // On deleting deployment later, project canonical deployment is also deleted
// So publish project canonical deployment first so that ApplicationDeploymentRecord for the same is available when deleting deployment later // So publish project canonical deployment first so that ApplicationDeploymentRecord for the same is available when deleting deployment later
const { applicationDeploymentRequestId, applicationDeploymentRequestData } = const {
await this.laconicRegistry.createApplicationDeploymentRequest({ applicationDeploymentRequestId,
applicationDeploymentRequestData,
} = await this.laconicRegistry.createApplicationDeploymentRequest({
deployment: canonicalDeployment, deployment: canonicalDeployment,
appName: repo, appName: repo,
repository: repoUrl, repository: repoUrl,
environmentVariables: environmentVariablesObj, environmentVariables: environmentVariablesObj,
dns: customDomain?.name ?? `${canonicalDeployment.project.name}`, dns,
auctionId: project.auctionId!, auctionId: project.auctionId!,
lrn: deployerLrn, lrn: deployerLrn,
apiUrl: deployer!.deployerApiUrl!, apiUrl: deployer!.deployerApiUrl!,
requesterAddress: address, requesterAddress: address,
publicKey: deployer!.publicKey! publicKey: deployer!.publicKey!,
}); });
await this.db.updateDeploymentById(canonicalDeployment.id, { await this.db.updateDeploymentById(canonicalDeployment.id, {
@ -830,7 +909,7 @@ export class Service {
environmentVariables: environmentVariablesObj, environmentVariables: environmentVariablesObj,
dns: `${deployment.project.name}-${deployment.id}`, dns: `${deployment.project.name}-${deployment.id}`,
requesterAddress: address, requesterAddress: address,
publicKey: deployer!.publicKey! publicKey: deployer!.publicKey!,
}); });
await this.db.updateDeploymentById(deployment.id, { await this.db.updateDeploymentById(deployment.id, {
@ -864,7 +943,7 @@ export class Service {
deployer: Object.assign(new Deployer(), { deployer: Object.assign(new Deployer(), {
deployerLrn, deployerLrn,
}), }),
isCanonical isCanonical,
}); });
log(`Created deployment ${newDeployment.id}`); log(`Created deployment ${newDeployment.id}`);
@ -874,11 +953,11 @@ export class Service {
async updateProjectWithDeployer( async updateProjectWithDeployer(
projectId: string, projectId: string,
deployer: Deployer deployer: Deployer,
): Promise<Deployer> { ): Promise<Deployer> {
const deploymentProject = await this.db.getProjects({ const deploymentProject = await this.db.getProjects({
where: { id: projectId }, where: { id: projectId },
relations: ['deployers'] relations: ['deployers'],
}); });
if (!deploymentProject[0].deployers) { if (!deploymentProject[0].deployers) {
@ -923,15 +1002,22 @@ export class Service {
const prodBranch = createdTemplateRepo.data.default_branch ?? 'main'; const prodBranch = createdTemplateRepo.data.default_branch ?? 'main';
const project = await this.addProject(user, organizationSlug, { const project = await this.addProject(
user,
organizationSlug,
{
name: `${gitRepo.data.owner!.login}-${gitRepo.data.name}`, name: `${gitRepo.data.owner!.login}-${gitRepo.data.name}`,
prodBranch, prodBranch,
repository: gitRepo.data.full_name, repository: gitRepo.data.full_name,
// TODO: Set selected template // TODO: Set selected template
template: 'webapp', template: 'webapp',
paymentAddress: data.paymentAddress, paymentAddress: data.paymentAddress,
txHash: data.txHash txHash: data.txHash,
}, lrn, auctionParams, environmentVariables); },
lrn,
auctionParams,
environmentVariables,
);
if (!project || !project.id) { if (!project || !project.id) {
throw new Error('Failed to create project from template'); throw new Error('Failed to create project from template');
@ -990,7 +1076,9 @@ export class Service {
commitHash: latestCommit.sha, commitHash: latestCommit.sha,
commitMessage: latestCommit.commit.message, commitMessage: latestCommit.commit.message,
}; };
const { applicationDeploymentAuctionId } = await this.laconicRegistry.createApplicationDeploymentAuction(repo, octokit, auctionParams!, deploymentData); const { applicationDeploymentAuctionId } = await this.laconicRegistry.createApplicationDeploymentAuction(repo, octokit, auctionParams!, deploymentData);
await this.updateProject(project.id, { auctionId: applicationDeploymentAuctionId }); await this.updateProject(project.id, { auctionId: applicationDeploymentAuctionId });
} else { } else {
const deployer = await this.db.getDeployerByLRN(lrn!); const deployer = await this.db.getDeployerByLRN(lrn!);
@ -1172,19 +1260,20 @@ export class Service {
let newDeployment: Deployment; let newDeployment: Deployment;
if (oldDeployment.project.auctionId) { if (oldDeployment.project.auctionId) {
newDeployment = await this.createDeploymentFromAuction(oldDeployment.project, oldDeployment.deployer); newDeployment = await this.createDeploymentFromAuction(
oldDeployment.project,
oldDeployment.deployer,
);
} else { } else {
newDeployment = await this.createDeployment(user.id, octokit, newDeployment = await this.createDeployment(user.id, octokit, {
{
project: oldDeployment.project, project: oldDeployment.project,
// TODO: Put isCurrent field in project // TODO: Put isCurrent field in project
branch: oldDeployment.branch, branch: oldDeployment.branch,
environment: Environment.Production, environment: Environment.Production,
commitHash: oldDeployment.commitHash, commitHash: oldDeployment.commitHash,
commitMessage: oldDeployment.commitMessage, commitMessage: oldDeployment.commitMessage,
deployer: oldDeployment.deployer deployer: oldDeployment.deployer,
} });
);
} }
return newDeployment; return newDeployment;
@ -1227,23 +1316,25 @@ export class Service {
{ isCurrent: true }, { isCurrent: true },
); );
if (!newCurrentDeploymentUpdate || !oldCurrentDeploymentUpdate){ if (!newCurrentDeploymentUpdate || !oldCurrentDeploymentUpdate) {
return false; return false;
} }
const newCurrentDeployment = await this.db.getDeployment({ where: { id: deploymentId }, relations: { project: true, deployer: true } }); const newCurrentDeployment = await this.db.getDeployment({
where: { id: deploymentId },
relations: { project: true, deployer: true },
});
if (!newCurrentDeployment) { if (!newCurrentDeployment) {
throw new Error(`Deployment with Id ${deploymentId} not found`); throw new Error(`Deployment with Id ${deploymentId} not found`);
} }
const applicationDeploymentRequestData = newCurrentDeployment.applicationDeploymentRequestData; const applicationDeploymentRequestData =
newCurrentDeployment.applicationDeploymentRequestData;
const customDomain = await this.db.getOldestDomainByProjectId(projectId); const dns = await this.getDnsForDeployerByProjectId(projectId, newCurrentDeployment.deployer!.version, newCurrentDeployment.project.name)
if (customDomain && applicationDeploymentRequestData) { applicationDeploymentRequestData!.dns = dns
applicationDeploymentRequestData.dns = customDomain.name
}
// Create a canonical deployment for the new current deployment // Create a canonical deployment for the new current deployment
const canonicalDeployment = await this.createDeploymentFromData( const canonicalDeployment = await this.createDeploymentFromData(
@ -1258,20 +1349,23 @@ export class Service {
applicationDeploymentRequestData!.meta = JSON.stringify({ applicationDeploymentRequestData!.meta = JSON.stringify({
...JSON.parse(applicationDeploymentRequestData!.meta), ...JSON.parse(applicationDeploymentRequestData!.meta),
note: `Updated by Snowball @ ${DateTime.utc().toFormat( note: `Updated by Snowball @ ${DateTime.utc().toFormat(
"EEE LLL dd HH:mm:ss 'UTC' yyyy" "EEE LLL dd HH:mm:ss 'UTC' yyyy",
)}` )}`,
}); });
const result = await this.laconicRegistry.publishRecord( const result = await this.laconicRegistry.publishRecord(
applicationDeploymentRequestData, applicationDeploymentRequestData,
); );
log(`Application deployment request record published: ${result.id}`) log(`Application deployment request record published: ${result.id}`);
const updateResult = await this.db.updateDeploymentById(canonicalDeployment.id, { const updateResult = await this.db.updateDeploymentById(
canonicalDeployment.id,
{
applicationDeploymentRequestId: result.id, applicationDeploymentRequestId: result.id,
applicationDeploymentRequestData, applicationDeploymentRequestData,
}); },
);
return updateResult; return updateResult;
} }
@ -1598,6 +1692,8 @@ export class Service {
const minimumPayment = record.attributes.minimumPayment; const minimumPayment = record.attributes.minimumPayment;
const paymentAddress = record.attributes.paymentAddress; const paymentAddress = record.attributes.paymentAddress;
const publicKey = record.attributes.publicKey; const publicKey = record.attributes.publicKey;
const version = record.attributes.deployerVersion;
const baseDomain = deployerApiUrl.substring(deployerApiUrl.indexOf('.') + 1); const baseDomain = deployerApiUrl.substring(deployerApiUrl.indexOf('.') + 1);
const deployerData = { const deployerData = {
@ -1607,7 +1703,8 @@ export class Service {
baseDomain, baseDomain,
minimumPayment, minimumPayment,
paymentAddress, paymentAddress,
publicKey publicKey,
version,
}; };
// TODO: Update deployers table in a separate job // TODO: Update deployers table in a separate job
@ -1646,4 +1743,28 @@ export class Service {
return amount === amountSent && sender === senderAddress && recipient === recipientAddress; return amount === amountSent && sender === senderAddress && recipient === recipientAddress;
} }
async getDnsForDeployerByProjectId(projectId: string, deployerVersion: string | undefined | null, projectName: string): Promise<string> {
let dns;
// If a custom domain is present then use that as the DNS in the deployment request
// Only deployers with version > 1 support multiple custom domains
// TODO: Check version number
if (deployerVersion) {
const customDomains = await this.db.getDomainsByProjectId(
projectId,
);
dns =
customDomains.length > 0
? customDomains.map((d) => d.name).join(',')
: `${projectName}`;
} else {
const domain = await this.db.getOldestDomainByProjectId(
projectId
);
dns = domain?.name ?? `${projectName}`;
}
return dns;
}
} }

View File

@ -120,5 +120,6 @@ export interface DeployerRecord {
publicKey: string; publicKey: string;
type: string; type: string;
version: string; version: string;
deployerVersion: string;
}; };
} }