Skip to content

Commit

Permalink
update cli deployment command to v3 api (subquery#2177)
Browse files Browse the repository at this point in the history
* update to cli deployment endpoint

* update changelog

* update error handling
  • Loading branch information
bz888 authored Nov 24, 2023
1 parent 1fae86d commit 97477cf
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 68 deletions.
2 changes: 2 additions & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- Update deployment command with v3 api (#2177)

## [4.2.1] - 2023-11-13
### Changed
Expand Down
31 changes: 19 additions & 12 deletions packages/cli/src/commands/deployment/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import cli from 'cli-ux';
import inquirer from 'inquirer';
import {BASE_PROJECT_URL, DEFAULT_DEPLOYMENT_TYPE, ROOT_API_URL_PROD} from '../../constants';
import {
deployToHostedService,
createDeployment,
dictionaryEndpoints,
imageVersions,
ipfsCID_validate,
networkEndpoints,
processEndpoints,
projectsInfo,
redeploy,
splitEndpoints,
updateDeployment,
} from '../../controller/deploy-controller';
import {addV, checkToken, promptWithDefaultValues, valueOrPrompt} from '../../utils';

Expand Down Expand Up @@ -90,6 +91,7 @@ export default class Deploy extends Command {
maxConnection: flags.queryMaxConnection,
Aggregate: flags.queryAggregate,
};

const indexerAD = {
unsafe: flags.indexerUnsafe,
batchSize: flags.indexerBatchSize,
Expand Down Expand Up @@ -149,37 +151,42 @@ export default class Deploy extends Command {
}
}
const projectInfo = await projectsInfo(authToken, org, projectName, ROOT_API_URL_PROD, flags.type);
const chains = [
{
cid: ipfsCID,
dictEndpoint: dict,
endpoint: splitEndpoints(endpoint),
indexerImageVersion: indexerVersion,
indexerAdvancedSettings: {
indexer: indexerAD,
},
},
];

if (projectInfo !== undefined) {
await redeploy(
await updateDeployment(
org,
projectName,
projectInfo.id,
authToken,
ipfsCID,
endpoint,
dict,
indexerVersion,
queryVersion,
queryAD,
indexerAD,
chains,
ROOT_API_URL_PROD
);
this.log(`Project: ${projectName} has been re-deployed`);
} else {
this.log('Deploying SubQuery project to Hosted Service');
const deploymentOutput = await deployToHostedService(
const deploymentOutput = await createDeployment(
org,
projectName,
authToken,
ipfsCID,
indexerVersion,
queryVersion,
endpoint,
flags.type,
dict,
queryAD,
indexerAD,
chains,
ROOT_API_URL_PROD
).catch((e) => this.error(e));
this.log(`Project: ${deploymentOutput.projectKey}
Expand Down
50 changes: 28 additions & 22 deletions packages/cli/src/controller/deploy-controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// SPDX-License-Identifier: GPL-3.0

import {ROOT_API_URL_DEV} from '../constants';
import {DeploymentDataType, DeploymentSpec, ValidateDataType} from '../types';
import {DeploymentDataType, DeploymentSpec, V3DeploymentIndexerType, ValidateDataType} from '../types';
import {delay} from '../utils';
import {
deployToHostedService,
createDeployment,
promoteDeployment,
deleteDeployment,
deploymentStatus,
Expand All @@ -14,7 +14,7 @@ import {
dictionaryEndpoints,
imageVersions,
processEndpoints,
redeploy,
updateDeployment,
projectsInfo,
} from './deploy-controller';
import {createProject, deleteProject} from './project-controller';
Expand Down Expand Up @@ -52,22 +52,21 @@ async function deployTestProject(
testAuth,
url
);

const endpoint = await networkEndpoints(url);
const dictEndpoint = await dictionaryEndpoints(url);
return deployToHostedService(
org,
project_name,
testAuth,
ipfs,
indexerV[0],
queryV[0],
processEndpoints(endpoint, validator.chainId),
projectSpec.type,
processEndpoints(dictEndpoint, validator.chainId),
{},
{},
url
);

const project = {
cid: ipfs,
dictEndpoint: processEndpoints(dictEndpoint, validator.chainId),
endpoint: processEndpoints(endpoint, validator.chainId),
indexerImageVersion: indexerV[0],
indexerAdvancedSettings: {
indexer: {},
},
};

return createDeployment(org, project_name, testAuth, ipfs, queryV[0], projectSpec.type, {}, [project], url);
}

const describeIf = (condition: boolean, ...args: Parameters<typeof describe>) =>
Expand Down Expand Up @@ -182,18 +181,25 @@ describeIf(!!testAuth, 'CLI deploy, delete, promote', () => {
ROOT_API_URL_DEV
);

await redeploy(
const project = {
cid: ipfs,
dictEndpoint: processEndpoints(dict, validator.chainId),
endpoint: processEndpoints(endpoints, validator.chainId),
indexerImageVersion: indexerV[0],
indexerAdvancedSettings: {
indexer: {},
},
};

await updateDeployment(
org,
projectName,
deployOutput.id,
testAuth,
newIPFS,
processEndpoints(endpoints, validator.chainId),
processEndpoints(dict, validator.chainId),
indexerV[0],
queryV[0],
{},
{},
[project],
ROOT_API_URL_DEV
);
const updatedInfo = await projectsInfo(testAuth, org, projectName, ROOT_API_URL_DEV, type);
Expand Down
57 changes: 24 additions & 33 deletions packages/cli/src/controller/deploy-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@
// SPDX-License-Identifier: GPL-3.0

import axios from 'axios';
import {DeploymentDataType, IndexerAdvancedOpts, ProjectDataType, QueryAdvancedOpts, ValidateDataType} from '../types';
import {
DeploymentDataType,
ProjectDataType,
QueryAdvancedOpts,
V3DeploymentIndexerType,
V3DeploymentInput,
ValidateDataType,
} from '../types';
import {buildProjectKey, errorHandle} from '../utils';

export async function deployToHostedService(
export async function createDeployment(
org: string,
projectName: string,
authToken: string,
ipfsCID: string,
indexerImageVersion: string,
queryImageVersion: string,
endpoint: string,
type: string,
dictEndpoint: string,
query: QueryAdvancedOpts,
indexer: IndexerAdvancedOpts,
chains: V3DeploymentIndexerType[],
url: string
): Promise<DeploymentDataType> {
try {
Expand All @@ -26,20 +30,15 @@ export async function deployToHostedService(
Authorization: `Bearer ${authToken}`,
},
method: 'post',
url: `v2/subqueries/${buildProjectKey(org, projectName)}/deployments`,
url: `v3/subqueries/${buildProjectKey(org, projectName)}/deployments`,
baseURL: url,
data: {
version: ipfsCID,
dictEndpoint: dictEndpoint,
endpoint: splitEndpoints(endpoint),
advancedSettings: {
query: query,
indexer: indexer,
},
indexerImageVersion: indexerImageVersion,
queryImageVersion: queryImageVersion,
cid: ipfsCID,
type: type,
},
queryImageVersion: queryImageVersion,
queryAdvancedSettings: {query},
chains,
} as V3DeploymentInput,
})
).data;
return result.deployment;
Expand Down Expand Up @@ -141,18 +140,15 @@ export async function projectsInfo(
}
}

export async function redeploy(
export async function updateDeployment(
org: string,
projectName: string,
deployID: number,
authToken: string,
ipfsCID: string,
endpoint: string,
dictEndpoint: string,
indexerVersion: string,
queryVersion: string,
query: QueryAdvancedOpts,
indexer: IndexerAdvancedOpts,
chains: V3DeploymentIndexerType[],
url: string
): Promise<void> {
try {
Expand All @@ -161,19 +157,14 @@ export async function redeploy(
Authorization: `Bearer ${authToken}`,
},
method: 'put',
url: `v2/subqueries/${buildProjectKey(org, projectName)}/deployments/${deployID}`,
url: `v3/subqueries/${buildProjectKey(org, projectName)}/deployments/${deployID}`,
baseURL: url,
data: {
version: ipfsCID,
dictEndpoint: dictEndpoint,
endpoint: splitEndpoints(endpoint),
indexerImageVersion: indexerVersion,
cid: ipfsCID,
queryImageVersion: queryVersion,
advancedSettings: {
query: query,
indexer: indexer,
},
},
queryAdvancedSettings: {query},
chains,
} as V3DeploymentInput,
});
} catch (e) {
errorHandle(e, `Failed to redeploy project: ${e.message}`);
Expand Down Expand Up @@ -249,7 +240,7 @@ export async function imageVersions(name: string, version: string, authToken: st
export function splitEndpoints(endpointStr: string): string[] {
return endpointStr.split(',').map((e) => e.trim());
}
interface EndpointType {
export interface EndpointType {
network: string;
endpoint: string;
chainId: string;
Expand Down
20 changes: 20 additions & 0 deletions packages/cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,23 @@ export interface ProjectDataType {
chainId: string;
apiVersion: string;
}

export interface V3DeploymentInput {
cid: string;
type: 'primary' | 'stage';
queryImageVersion: string;
queryAdvancedSettings: {
query: QueryAdvancedOpts;
};
chains: V3DeploymentIndexerType[];
}

export interface V3DeploymentIndexerType {
cid: string;
dictEndpoint: string;
endpoint: string | string[];
indexerImageVersion: string;
indexerAdvancedSettings: {
indexer: IndexerAdvancedOpts;
};
}
2 changes: 1 addition & 1 deletion packages/cli/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export async function checkToken(authToken_ENV: string, token_path: string): Pro

export function errorHandle(e: any, msg: string): Error {
if ((axios.isAxiosError(e) as any) && e?.response?.data) {
throw new Error(`${msg} ${e.response.data.message}`);
throw new Error(`${msg} ${e.response.data.message ?? e.response.data}`);
}

throw new Error(`${msg} ${e.message}`);
Expand Down

0 comments on commit 97477cf

Please sign in to comment.