Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: cli and cloud pkg #9060

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions packages/cubejs-backend-cloud/src/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import crypto from 'crypto';
import path from 'path';
import fs from 'fs-extra';
import { DotenvParseOutput } from '@cubejs-backend/dotenv';
import { CubeCloudClient } from './cloud';

type DeployDirectoryOptions = {
Expand Down Expand Up @@ -64,15 +65,16 @@ export class DeployDirectory {
}

type DeployHooks = {
onStart?: Function,
onUpdate?: Function,
onUpload?: Function,
onFinally?: Function
onStart?: (deploymentName: string, files: string[]) => void,
onUpdate?: (i: number, { file }: { file: string}) => void,
onUpload?: (files: string[], file: string) => void,
onFinally?: () => void
};

export class DeployController {
public constructor(
protected readonly cubeCloudClient: CubeCloudClient,
protected envVariables: DotenvParseOutput = {},
protected readonly hooks: DeployHooks = {}
) {
}
Expand All @@ -85,6 +87,10 @@ export class DeployController {
const upstreamHashes = await this.cubeCloudClient.getUpstreamHashes();
const { transaction, deploymentName } = await this.cubeCloudClient.startUpload();

if (Object.keys(this.envVariables).length) {
await this.cubeCloudClient.setEnvVars({ envVariables: this.envVariables });
}

const files = Object.keys(fileHashes);
const fileHashesPosix: Record<string, any> = {};
if (this.hooks.onStart) this.hooks.onStart(deploymentName, files);
Expand Down
1 change: 1 addition & 0 deletions packages/cubejs-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@cubejs-backend/dotenv": "^9.0.2",
"@cubejs-backend/schema-compiler": "1.1.11",
"@cubejs-backend/shared": "1.1.10",
"@cubejs-backend/cloud": "1.1.10",
"chalk": "^2.4.2",
"cli-progress": "^3.10",
"commander": "^2.19.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/cubejs-cli/src/command/auth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { CommanderStatic } from 'commander';
import { Config } from '@cubejs-backend/cloud';

import { displayError, event } from '../utils';
import { Config } from '../config';

const authenticate = async (currentToken: string) => {
const config = new Config();
Expand Down
106 changes: 23 additions & 83 deletions packages/cubejs-cli/src/command/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import fs from 'fs-extra';
import path from 'path';
import cliProgress from 'cli-progress';
import { CommanderStatic } from 'commander';
import { CubeCloudClient, DeployController } from '@cubejs-backend/cloud';
import { ConfigCli } from '../config';

import { DeployDirectory } from '../deploy';
import { logStage, displayError, event } from '../utils';
import { Config } from '../config';

const deploy = async ({ directory, auth, uploadEnv, token }: any) => {
if (!(await fs.pathExists(path.join(process.cwd(), 'node_modules', '@cubejs-backend/server-core')))) {
Expand All @@ -14,102 +14,42 @@ const deploy = async ({ directory, auth, uploadEnv, token }: any) => {
);
}

const config = new ConfigCli();
if (token) {
const config = new Config();
await config.addAuthToken(token);

await event({
event: 'Cube Cloud CLI Authenticate'
});

await event({ event: 'Cube Cloud CLI Authenticate' });
console.log('Token successfully added!');
}

const config = new Config();
const bar = new cliProgress.SingleBar({
format: '- Uploading files | {bar} | {percentage}% || {value} / {total} | {file}',
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
hideCursor: true
});

const deployDir = new DeployDirectory({ directory });
const fileHashes: any = await deployDir.fileHashes();

const upstreamHashes = await config.cloudReq({
url: (deploymentId: string) => `build/deploy/${deploymentId}/files`,
method: 'GET',
auth
});

const { transaction, deploymentName } = await config.cloudReq({
url: (deploymentId: string) => `build/deploy/${deploymentId}/start-upload`,
method: 'POST',
auth
});

if (uploadEnv) {
const envVariables = await config.envFile(`${directory}/.env`);
await config.cloudReq({
url: (deploymentId) => `build/deploy/${deploymentId}/set-env`,
method: 'POST',
body: {
envVariables: JSON.stringify(envVariables),
},
auth
});
}

await logStage(`Deploying ${deploymentName}...`, 'Cube Cloud CLI Deploy');

const files = Object.keys(fileHashes);
const fileHashesPosix = {};

bar.start(files.length, 0, {
file: ''
});

try {
for (let i = 0; i < files.length; i++) {
const file = files[i];
const envVariables = uploadEnv ? await config.envFile(`${directory}/.env`) : {};

const cubeCloudClient = new CubeCloudClient(auth || (await config.deployAuthForCurrentDir()));
const deployController = new DeployController(cubeCloudClient, envVariables, {
onStart: async (deploymentName, files) => {
await logStage(`Deploying ${deploymentName}...`, 'Cube Cloud CLI Deploy');
bar.start(files.length, 0, {
file: ''
});
},
onUpdate: (i, { file }) => {
bar.update(i, { file });

const filePosix = file.split(path.sep).join(path.posix.sep);
fileHashesPosix[filePosix] = fileHashes[file];

if (!upstreamHashes[filePosix] || upstreamHashes[filePosix].hash !== fileHashes[file].hash) {
await config.cloudReq({
url: (deploymentId: string) => `build/deploy/${deploymentId}/upload-file`,
method: 'POST',
formData: {
transaction: JSON.stringify(transaction),
fileName: filePosix,
file: {
value: fs.createReadStream(path.join(directory, file)),
options: {
filename: path.basename(file),
contentType: 'application/octet-stream'
}
}
},
auth
});
}
},
onUpload: (files) => {
bar.update(files.length, { file: 'Post processing...' });
},
onFinally: () => {
bar.stop();
}
bar.update(files.length, { file: 'Post processing...' });
await config.cloudReq({
url: (deploymentId: string) => `build/deploy/${deploymentId}/finish-upload`,
method: 'POST',
body: {
transaction,
files: fileHashesPosix
},
auth
});
} finally {
bar.stop();
}
});

await deployController.deploy(directory);
await logStage('Done 🎉', 'Cube Cloud CLI Deploy Success');
};

Expand Down
Loading
Loading