2024-02-12 06:04:01 +00:00
|
|
|
import debug from 'debug';
|
2024-02-14 05:33:22 +00:00
|
|
|
import assert from 'assert';
|
2024-02-12 06:04:01 +00:00
|
|
|
import { inc as semverInc } from 'semver';
|
2024-02-14 05:33:22 +00:00
|
|
|
import { DateTime } from 'luxon';
|
2024-02-12 06:04:01 +00:00
|
|
|
|
|
|
|
import { Registry as LaconicRegistry } from '@cerc-io/laconic-sdk';
|
|
|
|
|
|
|
|
import { RegistryConfig } from './config';
|
|
|
|
import { ApplicationDeploymentRequest } from './entity/Project';
|
2024-02-19 08:13:29 +00:00
|
|
|
import { ApplicationRecord, Deployment } from './entity/Deployment';
|
|
|
|
import { AppDeploymentRecord, PackageJSON } from './types';
|
2024-02-12 06:04:01 +00:00
|
|
|
|
|
|
|
const log = debug('snowball:registry');
|
|
|
|
|
|
|
|
const APP_RECORD_TYPE = 'ApplicationRecord';
|
2024-02-19 08:13:29 +00:00
|
|
|
const APP_DEPLOYMENT_REQUEST_TYPE = 'ApplicationDeploymentRequest';
|
|
|
|
const APP_DEPLOYMENT_RECORD_TYPE = 'ApplicationDeploymentRecord';
|
2024-02-12 06:04:01 +00:00
|
|
|
|
2024-02-12 09:48:00 +00:00
|
|
|
// TODO: Move registry code to laconic-sdk/watcher-ts
|
2024-02-12 06:04:01 +00:00
|
|
|
export class Registry {
|
|
|
|
private registry: LaconicRegistry;
|
|
|
|
private registryConfig: RegistryConfig;
|
|
|
|
|
|
|
|
constructor (registryConfig : RegistryConfig) {
|
|
|
|
this.registryConfig = registryConfig;
|
|
|
|
this.registry = new LaconicRegistry(registryConfig.gqlEndpoint, registryConfig.restEndpoint, registryConfig.chainId);
|
|
|
|
}
|
|
|
|
|
2024-02-14 05:33:22 +00:00
|
|
|
async createApplicationRecord ({
|
|
|
|
packageJSON,
|
|
|
|
commitHash,
|
2024-02-15 11:54:57 +00:00
|
|
|
appType,
|
|
|
|
repoUrl
|
2024-02-14 05:33:22 +00:00
|
|
|
}: {
|
|
|
|
packageJSON: PackageJSON
|
2024-02-15 11:54:57 +00:00
|
|
|
commitHash: string,
|
2024-02-14 05:33:22 +00:00
|
|
|
appType: string,
|
2024-02-15 11:54:57 +00:00
|
|
|
repoUrl: string
|
2024-02-19 08:13:29 +00:00
|
|
|
}): Promise<{applicationRecordId: string, applicationRecordData: ApplicationRecord}> {
|
2024-02-16 09:35:52 +00:00
|
|
|
assert(packageJSON.name, "name field doesn't exist in package.json");
|
2024-02-12 06:04:01 +00:00
|
|
|
// Use laconic-sdk to publish record
|
|
|
|
// Reference: https://git.vdb.to/cerc-io/test-progressive-web-app/src/branch/main/scripts/publish-app-record.sh
|
|
|
|
// Fetch previous records
|
|
|
|
const records = await this.registry.queryRecords({
|
|
|
|
type: APP_RECORD_TYPE,
|
2024-02-14 05:33:22 +00:00
|
|
|
name: packageJSON.name
|
2024-02-12 06:04:01 +00:00
|
|
|
}, true);
|
|
|
|
|
|
|
|
// Get next version of record
|
|
|
|
const bondRecords = records.filter((record: any) => record.bondId === this.registryConfig.bondId);
|
|
|
|
const [latestBondRecord] = bondRecords.sort((a: any, b: any) => new Date(b.createTime).getTime() - new Date(a.createTime).getTime());
|
|
|
|
const nextVersion = semverInc(latestBondRecord?.attributes.version ?? '0.0.0', 'patch');
|
|
|
|
|
2024-02-14 05:33:22 +00:00
|
|
|
assert(nextVersion, 'Application record version not valid');
|
|
|
|
|
2024-02-12 06:04:01 +00:00
|
|
|
// Create record of type ApplicationRecord and publish
|
|
|
|
const applicationRecord = {
|
|
|
|
type: APP_RECORD_TYPE,
|
2024-02-14 05:33:22 +00:00
|
|
|
version: nextVersion,
|
|
|
|
repository_ref: commitHash,
|
2024-02-15 11:54:57 +00:00
|
|
|
repository: [repoUrl],
|
2024-02-14 05:33:22 +00:00
|
|
|
app_type: appType,
|
2024-02-16 09:35:52 +00:00
|
|
|
name: packageJSON.name,
|
2024-02-14 05:33:22 +00:00
|
|
|
...(packageJSON.description && { description: packageJSON.description }),
|
|
|
|
...(packageJSON.homepage && { homepage: packageJSON.homepage }),
|
|
|
|
...(packageJSON.license && { license: packageJSON.license }),
|
|
|
|
...(packageJSON.author && { author: typeof packageJSON.author === 'object' ? JSON.stringify(packageJSON.author) : packageJSON.author }),
|
|
|
|
...(packageJSON.version && { app_version: packageJSON.version })
|
2024-02-12 06:04:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const result = await this.registry.setRecord(
|
|
|
|
{
|
|
|
|
privateKey: this.registryConfig.privateKey,
|
|
|
|
record: applicationRecord,
|
|
|
|
bondId: this.registryConfig.bondId
|
|
|
|
},
|
|
|
|
'',
|
|
|
|
this.registryConfig.fee
|
|
|
|
);
|
|
|
|
|
|
|
|
log('Application record data:', applicationRecord);
|
|
|
|
|
2024-02-14 05:33:22 +00:00
|
|
|
// TODO: Discuss computation of CRN
|
2024-02-16 09:35:52 +00:00
|
|
|
const crn = this.getCrn(packageJSON.name);
|
2024-02-15 11:54:57 +00:00
|
|
|
log(`Setting name: ${crn} for record ID: ${result.data.id}`);
|
2024-02-12 06:04:01 +00:00
|
|
|
|
|
|
|
await this.registry.setName({ cid: result.data.id, crn }, this.registryConfig.privateKey, this.registryConfig.fee);
|
|
|
|
await this.registry.setName({ cid: result.data.id, crn: `${crn}@${applicationRecord.app_version}` }, this.registryConfig.privateKey, this.registryConfig.fee);
|
|
|
|
await this.registry.setName({ cid: result.data.id, crn: `${crn}@${applicationRecord.repository_ref}` }, this.registryConfig.privateKey, this.registryConfig.fee);
|
|
|
|
|
2024-02-19 08:13:29 +00:00
|
|
|
return { applicationRecordId: result.data.id, applicationRecordData: applicationRecord };
|
2024-02-12 06:04:01 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 05:33:22 +00:00
|
|
|
async createApplicationDeploymentRequest (data: {
|
|
|
|
appName: string,
|
|
|
|
commitHash: string,
|
2024-02-15 12:32:37 +00:00
|
|
|
repository: string,
|
|
|
|
environmentVariables: { [key: string]: string }
|
2024-02-14 05:33:22 +00:00
|
|
|
}): Promise<{
|
2024-02-19 08:13:29 +00:00
|
|
|
applicationDeploymentRequestId: string,
|
|
|
|
applicationDeploymentRequestData: ApplicationDeploymentRequest
|
2024-02-14 05:33:22 +00:00
|
|
|
}> {
|
2024-02-12 06:04:01 +00:00
|
|
|
const crn = this.getCrn(data.appName);
|
|
|
|
const records = await this.registry.resolveNames([crn]);
|
|
|
|
const applicationRecord = records[0];
|
|
|
|
|
|
|
|
if (!applicationRecord) {
|
|
|
|
throw new Error(`No record found for ${crn}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create record of type ApplicationDeploymentRequest and publish
|
|
|
|
const applicationDeploymentRequest = {
|
2024-02-19 08:13:29 +00:00
|
|
|
type: APP_DEPLOYMENT_REQUEST_TYPE,
|
2024-02-12 06:04:01 +00:00
|
|
|
version: '1.0.0',
|
|
|
|
name: `${applicationRecord.attributes.name}@${applicationRecord.attributes.app_version}`,
|
|
|
|
application: `${crn}@${applicationRecord.attributes.app_version}`,
|
|
|
|
|
|
|
|
// TODO: Not set in test-progressive-web-app CI
|
|
|
|
// dns: '$CERC_REGISTRY_DEPLOYMENT_SHORT_HOSTNAME',
|
|
|
|
// deployment: '$CERC_REGISTRY_DEPLOYMENT_CRN',
|
|
|
|
|
2024-02-14 05:33:22 +00:00
|
|
|
// https://git.vdb.to/cerc-io/laconic-registry-cli/commit/129019105dfb93bebcea02fde0ed64d0f8e5983b
|
|
|
|
config: JSON.stringify({
|
2024-02-15 12:32:37 +00:00
|
|
|
env: data.environmentVariables
|
2024-02-14 05:33:22 +00:00
|
|
|
}),
|
|
|
|
meta: JSON.stringify({
|
|
|
|
note: `Added by Snowball @ ${DateTime.utc().toFormat('EEE LLL dd HH:mm:ss \'UTC\' yyyy')}`,
|
|
|
|
repository: data.repository,
|
|
|
|
repository_ref: data.commitHash
|
|
|
|
})
|
2024-02-12 06:04:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const result = await this.registry.setRecord(
|
|
|
|
{
|
|
|
|
privateKey: this.registryConfig.privateKey,
|
|
|
|
record: applicationDeploymentRequest,
|
|
|
|
bondId: this.registryConfig.bondId
|
|
|
|
},
|
|
|
|
'',
|
|
|
|
this.registryConfig.fee
|
|
|
|
);
|
|
|
|
log(`Application deployment request record published: ${result.data.id}`);
|
|
|
|
log('Application deployment request data:', applicationDeploymentRequest);
|
|
|
|
|
2024-02-19 08:13:29 +00:00
|
|
|
return { applicationDeploymentRequestId: result.data.id, applicationDeploymentRequestData: applicationDeploymentRequest };
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch ApplicationDeploymentRecords for deployments
|
|
|
|
*/
|
|
|
|
async getDeploymentRecords (deployments: Deployment[]): Promise<AppDeploymentRecord[]> {
|
|
|
|
// Fetch ApplicationDeploymentRecords for corresponding ApplicationRecord set in deployments
|
|
|
|
// TODO: Implement Laconicd GQL query to filter records by multiple values for an attribute
|
|
|
|
const records = await this.registry.queryRecords({
|
|
|
|
type: APP_DEPLOYMENT_RECORD_TYPE
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
// Filter records with ApplicationRecord ids
|
|
|
|
return records.filter((record: AppDeploymentRecord) => deployments.some(deployment => deployment.applicationRecordId === record.attributes.application));
|
2024-02-12 06:04:01 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 05:33:22 +00:00
|
|
|
getCrn (packageJsonName: string): string {
|
|
|
|
const [arg1, arg2] = packageJsonName.split('/');
|
|
|
|
|
|
|
|
if (arg2) {
|
|
|
|
const authority = arg1.replace('@', '');
|
|
|
|
return `crn://${authority}/applications/${arg2}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return `crn://${arg1}/applications/${arg1}`;
|
2024-02-12 06:04:01 +00:00
|
|
|
}
|
|
|
|
}
|