Create deployments after auction creation
This commit is contained in:
parent
42d35cae84
commit
b58d9e6c21
@ -126,14 +126,20 @@ export class Registry {
|
|||||||
|
|
||||||
await sleep(SLEEP_DURATION);
|
await sleep(SLEEP_DURATION);
|
||||||
await this.registry.setName(
|
await this.registry.setName(
|
||||||
{ cid: result.id, lrn },
|
{
|
||||||
|
cid: result.id,
|
||||||
|
lrn
|
||||||
|
},
|
||||||
this.registryConfig.privateKey,
|
this.registryConfig.privateKey,
|
||||||
fee
|
fee
|
||||||
);
|
);
|
||||||
|
|
||||||
await sleep(SLEEP_DURATION);
|
await sleep(SLEEP_DURATION);
|
||||||
await this.registry.setName(
|
await this.registry.setName(
|
||||||
{ cid: result.id, lrn: `${lrn}@${applicationRecord.app_version}` },
|
{
|
||||||
|
cid: result.id,
|
||||||
|
lrn: `${lrn}@${applicationRecord.app_version}`
|
||||||
|
},
|
||||||
this.registryConfig.privateKey,
|
this.registryConfig.privateKey,
|
||||||
fee
|
fee
|
||||||
);
|
);
|
||||||
@ -162,7 +168,6 @@ export class Registry {
|
|||||||
): Promise<{
|
): Promise<{
|
||||||
applicationDeploymentAuctionId: string;
|
applicationDeploymentAuctionId: string;
|
||||||
}> {
|
}> {
|
||||||
// TODO: If data.domain is present then call createDeployment (don't need auction)
|
|
||||||
assert(data.project?.repository, 'Project repository not found');
|
assert(data.project?.repository, 'Project repository not found');
|
||||||
const [owner, repo] = data.project.repository.split('/');
|
const [owner, repo] = data.project.repository.split('/');
|
||||||
|
|
||||||
@ -190,7 +195,7 @@ export class Registry {
|
|||||||
).data.html_url;
|
).data.html_url;
|
||||||
|
|
||||||
// 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 } =
|
||||||
await this.createApplicationRecord({
|
await this.createApplicationRecord({
|
||||||
appName: repo,
|
appName: repo,
|
||||||
packageJSON,
|
packageJSON,
|
||||||
@ -235,7 +240,7 @@ export class Registry {
|
|||||||
|
|
||||||
// Create record of type applicationDeploymentAuction and publish
|
// Create record of type applicationDeploymentAuction and publish
|
||||||
const applicationDeploymentAuction = {
|
const applicationDeploymentAuction = {
|
||||||
application: `${lrn}@${applicationRecord.attributes.app_version}`,
|
application: lrn,
|
||||||
auction: auctionResult.auction.id,
|
auction: auctionResult.auction.id,
|
||||||
type: APP_DEPLOYMENT_AUCTION_RECORD_TYPE,
|
type: APP_DEPLOYMENT_AUCTION_RECORD_TYPE,
|
||||||
};
|
};
|
||||||
@ -454,6 +459,11 @@ export class Registry {
|
|||||||
return completedAuctions;
|
return completedAuctions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getRecordsByName(name: string): Promise<any> {
|
||||||
|
|
||||||
|
return this.registry.resolveNames([name]);
|
||||||
|
}
|
||||||
|
|
||||||
getLrn(appName: string): string {
|
getLrn(appName: string): string {
|
||||||
assert(this.registryConfig.authority, "Authority doesn't exist");
|
assert(this.registryConfig.authority, "Authority doesn't exist");
|
||||||
return `lrn://${this.registryConfig.authority}/applications/${appName}`;
|
return `lrn://${this.registryConfig.authority}/applications/${appName}`;
|
||||||
|
@ -270,35 +270,28 @@ export class Service {
|
|||||||
async checkAuctionStatus(
|
async checkAuctionStatus(
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
// Deployment should be in building state and should not have domain.
|
// Deployment should be in building state and should not have domain.
|
||||||
const deployments = await this.db.getDeployments({
|
const projects = await this.db.getProjects({
|
||||||
where: {
|
where: {
|
||||||
status: DeploymentStatus.Building,
|
deployerLrn: IsNull()
|
||||||
domain: IsNull()
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// Check the auctionId for those deployments with auctions
|
const auctionIds = projects
|
||||||
const auctionIds = deployments
|
.map(project => project.auctionId) as string[];
|
||||||
.filter(deployment => deployment.project && deployment.project.auctionId)
|
|
||||||
.map(deployment => deployment.project!.auctionId) as string[];
|
|
||||||
// Get all the auctions for those ids with auction status completed
|
// Get all the auctions for those ids with auction status completed
|
||||||
const completedAuctionIds = await this.registry.getCompletedAuctionIds(auctionIds);
|
const completedAuctionIds = await this.registry.getCompletedAuctionIds(auctionIds);
|
||||||
// If the deplyer lrn array is empty then call createDeploymentFromAuction
|
// If the deplyer lrn array is empty then call createDeploymentFromAuction
|
||||||
const auctionDeployments = deployments.filter(deployment =>
|
const auctionProjects = projects.filter(project =>
|
||||||
completedAuctionIds.includes(deployment.project!.auctionId!) &&
|
completedAuctionIds.includes(project.auctionId!)
|
||||||
deployment.project!.deployerLrn?.length === 0
|
|
||||||
);
|
);
|
||||||
|
|
||||||
for (const auctionDeployment of auctionDeployments) {
|
for (const project of auctionProjects) {
|
||||||
await this.createDeploymentFromAuction(
|
await this.createDeploymentFromAuction(project);
|
||||||
'user id',
|
|
||||||
auctionDeployment.project!.auctionId!,
|
|
||||||
auctionDeployment
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.deployRecordCheckTimeout = setTimeout(() => {
|
this.deployRecordCheckTimeout = setTimeout(() => {
|
||||||
this.checkAuctionStatus();
|
this.checkAuctionStatus();
|
||||||
}, this.config.registryConfig.fetchDeploymentRecordDelay);
|
}, this.config.registryConfig.checkAuctionStatusDelay);
|
||||||
}
|
}
|
||||||
|
|
||||||
async getUser(userId: string): Promise<User | null> {
|
async getUser(userId: string): Promise<User | null> {
|
||||||
@ -691,32 +684,71 @@ export class Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async createDeploymentFromAuction(
|
async createDeploymentFromAuction(
|
||||||
userId: string,
|
project: DeepPartial<Project>,
|
||||||
auctionId: string,
|
|
||||||
// take project data
|
|
||||||
// project: DeepPartial<Project>,
|
|
||||||
data: DeepPartial<Deployment>,
|
|
||||||
) {
|
) {
|
||||||
// TODO: If data.domain is present then call createDeployment (don't need auction)
|
const deployerLrns = await this.registry.getAuctionWinners(project!.auctionId!);
|
||||||
|
|
||||||
|
// Update project with deployer LRNs
|
||||||
|
await this.db.updateProjectById(project.id!, {
|
||||||
|
deployerLrn: deployerLrns
|
||||||
|
})
|
||||||
|
|
||||||
|
const octokit = await this.getOctokit(project.owner!.id!);
|
||||||
|
const [owner, repo] = project.repository!.split('/');
|
||||||
|
|
||||||
|
const repoUrl = (
|
||||||
|
await octokit.rest.repos.get({
|
||||||
|
owner,
|
||||||
|
repo,
|
||||||
|
})
|
||||||
|
).data.html_url;
|
||||||
|
|
||||||
|
const {
|
||||||
|
data: [latestCommit],
|
||||||
|
} = await octokit.rest.repos.listCommits({
|
||||||
|
owner,
|
||||||
|
repo,
|
||||||
|
sha: project.prodBranch,
|
||||||
|
per_page: 1,
|
||||||
|
});
|
||||||
|
|
||||||
|
const lrn = this.registry.getLrn(repo);
|
||||||
|
const [record] = await this.registry.getRecordsByName(lrn);
|
||||||
|
const applicationRecordId = record.id;
|
||||||
|
const applicationRecordData = record.attributes;
|
||||||
|
|
||||||
|
for (const deployer in deployerLrns) {
|
||||||
|
// Create deployment with prod branch and latest commit
|
||||||
|
const deploymentData = {
|
||||||
|
project,
|
||||||
|
branch: project.prodBranch,
|
||||||
|
environment: Environment.Production,
|
||||||
|
domain: null,
|
||||||
|
commitHash: latestCommit.sha,
|
||||||
|
commitMessage: latestCommit.commit.message,
|
||||||
|
};
|
||||||
|
|
||||||
const newDeployment = await this.db.addDeployment({
|
const newDeployment = await this.db.addDeployment({
|
||||||
project: data.project,
|
project: project,
|
||||||
branch: data.branch,
|
branch: deploymentData.branch,
|
||||||
commitHash: data.commitHash,
|
commitHash: deploymentData.commitHash,
|
||||||
commitMessage: data.commitMessage,
|
commitMessage: deploymentData.commitMessage,
|
||||||
environment: data.environment,
|
environment: deploymentData.environment,
|
||||||
status: DeploymentStatus.Building,
|
status: DeploymentStatus.Building,
|
||||||
domain: data.domain,
|
applicationRecordId,
|
||||||
|
applicationRecordData,
|
||||||
|
domain: deploymentData.domain,
|
||||||
createdBy: Object.assign(new User(), {
|
createdBy: Object.assign(new User(), {
|
||||||
id: userId,
|
id: project.owner!.id!,
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
log(
|
log(
|
||||||
`Created deployment ${newDeployment.id}`,
|
`Created deployment ${newDeployment.id} and published application record ${applicationRecordId}`,
|
||||||
);
|
);
|
||||||
|
|
||||||
const environmentVariables =
|
const environmentVariables =
|
||||||
await this.db.getEnvironmentVariablesByProjectId(data.project!.id!, {
|
await this.db.getEnvironmentVariablesByProjectId(project!.id!, {
|
||||||
environment: Environment.Production,
|
environment: Environment.Production,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -730,28 +762,25 @@ export class Service {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// To set project DNS
|
// To set project DNS
|
||||||
if (data.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.registry.createApplicationDeploymentRequest({
|
||||||
deployment: newDeployment,
|
deployment: newDeployment,
|
||||||
appName: data.project!.name!,
|
appName: project.name!,
|
||||||
repository: data.url!,
|
repository: repoUrl,
|
||||||
environmentVariables: environmentVariablesObj,
|
environmentVariables: environmentVariablesObj,
|
||||||
dns: `${newDeployment.project.name}`,
|
dns: `${newDeployment.project.name}`,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const deployerLrns = await this.registry.getAuctionWinners(auctionId);
|
|
||||||
|
|
||||||
for (const deployer in deployerLrns) {
|
|
||||||
const { applicationDeploymentRequestId, applicationDeploymentRequestData } =
|
const { applicationDeploymentRequestId, applicationDeploymentRequestData } =
|
||||||
// Create requests for all the deployers
|
// Create requests for all the deployers
|
||||||
await this.registry.createApplicationDeploymentRequest({
|
await this.registry.createApplicationDeploymentRequest({
|
||||||
deployment: newDeployment,
|
deployment: newDeployment,
|
||||||
appName: data.project!.name!,
|
appName: project.name!,
|
||||||
repository: data.url!,
|
repository: repoUrl,
|
||||||
auctionId,
|
auctionId: project.auctionId!,
|
||||||
lrn: deployer,
|
lrn: deployer,
|
||||||
environmentVariables: environmentVariablesObj,
|
environmentVariables: environmentVariablesObj,
|
||||||
dns: `${newDeployment.project.name}-${newDeployment.id}`,
|
dns: `${newDeployment.project.name}-${newDeployment.id}`,
|
||||||
@ -762,12 +791,6 @@ export class Service {
|
|||||||
applicationDeploymentRequestData,
|
applicationDeploymentRequestData,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// update project with deployerlrns
|
|
||||||
|
|
||||||
await this.db.updateProjectById(data.project?.id!, {
|
|
||||||
deployerLrn: deployerLrns
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async addProjectFromTemplate(
|
async addProjectFromTemplate(
|
||||||
@ -859,9 +882,10 @@ export class Service {
|
|||||||
commitMessage: latestCommit.commit.message,
|
commitMessage: latestCommit.commit.message,
|
||||||
};
|
};
|
||||||
|
|
||||||
const deployment = auctionData
|
await (auctionData
|
||||||
? await this.registry.createApplicationDeploymentAuction(repo, octokit, auctionData!, deploymentData)
|
? this.registry.createApplicationDeploymentAuction(repo, octokit, auctionData!, deploymentData)
|
||||||
: await this.createDeployment(user.id, octokit, deploymentData, lrn);
|
: this.createDeployment(user.id, octokit, deploymentData, lrn)
|
||||||
|
);
|
||||||
|
|
||||||
await this.createRepoHook(octokit, project);
|
await this.createRepoHook(octokit, project);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user