Skip to content

Commit

Permalink
fix: deploy本地缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
Joycepy committed Aug 22, 2022
1 parent a9c68d9 commit cf54f23
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 165 deletions.
294 changes: 147 additions & 147 deletions dist/index.js

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions src/common/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,42 @@ interface IPayload {
appId?: string;
bucketName?: string;
}
/**
* 缓存上一次部署的细节
* @param param0
* @param param1
*/
export async function getDeployCache(accountID: string, region: string, appName: string, configPath: string) {
try{
const stateId = `${accountID}-${region}-${appName}_deploy.json`;
const cachePath = path.join(configPath ? path.dirname(configPath) : process.cwd(), '.s');
const cacheData = (await getState(stateId, cachePath)) || {};
return cacheData;
}catch (ex) {
/* 不影响主进程 */
logger.debug(ex);
}
}

/**
* 缓存上一次部署的细节
* @param param0
* @param param1
*/
export async function writeDeployCache(
{ accountID, region, appName, configPath }: IStatePayload,
props: any) {
try{
const stateId = `${accountID}-${region}-${appName}_deploy.json`;
const cachePath = path.join(configPath ? path.dirname(configPath) : process.cwd(), '.s');
const cacheData = (await getState(stateId, cachePath)) || {};
cacheData.props = props;
await setState(stateId, cacheData, cachePath);
}catch (ex) {
/* 不影响主进程 */
logger.debug(ex);
}
}
/**
* 写创建资源的缓存
*/
Expand Down
40 changes: 23 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import * as core from '@serverless-devs/core';
import { InputProps } from './interface/entity';
// @ts-ignore
import { spinner, inquirer } from "@serverless-devs/core";
import { spinner } from "@serverless-devs/core";

import Client from './lib/client';
import * as utils from './lib/utils';
import * as inputHandler from './lib/input-handler';
import * as outputHandler from './lib/output-handler';
import * as HELP from './lib/help';
import logger from './common/logger';
import { getInquire } from './lib/help/constant';
import Oss from './lib/oss.service';
import { writeCreatCache } from './common/cache';
import { writeCreatCache, writeDeployCache } from './common/cache';
import WriteFile from './lib/write-file';

const { lodash } = core;
Expand Down Expand Up @@ -235,7 +234,8 @@ export default class SaeComponent {
await inputHandler.checkInputs(inputs);
let appId: any;
const configPath = core.lodash.get(inputs, 'path.configPath');
let { args, props: { application, slb } } = inputs;
const { args, props } = inputs;
let { application, slb } = props;
const { appName, region } = application;
const credentials = await core.getCredential(inputs.project.access);
await Client.setSaeClient(region, credentials);
Expand Down Expand Up @@ -263,21 +263,15 @@ export default class SaeComponent {
return await utils.infoRes(app);
} else {
if (remoteData['Data']['Applications'].length > 0) {
change = await utils.getDiff(application, slb, remoteData['Data']['Applications'][0]);
const configInquire = getInquire(appName);
const ans: { option: string } = await inquirer.prompt(configInquire);
switch (ans.option) {
case 'use local':
updateRemote = true;
break;
case 'use remote':
const app = remoteData['Data']['Applications'][0];
return await utils.infoRes(app);
default:
break;
change = await utils.getDiff(application, slb, remoteData['Data']['Applications'][0], credentials, configPath);
updateRemote = change['updateRemote'];
if (!updateRemote) {
const app = remoteData['Data']['Applications'][0];
return await utils.infoRes(app);
}
}
}
const lastProps = lodash.cloneDeep(props);
// 查询发布单
if (remoteData['Data']['Applications'].length > 0) {
const app = remoteData['Data']['Applications'][0];
Expand All @@ -303,7 +297,7 @@ export default class SaeComponent {
if (updateRemote) {
appId = remoteAppId;
try {
if(change['needDeploy']){
if (change['needDeploy']) {
let res = await Client.saeClient.updateApplication(applicationObject);
changeOrderId = res['Data']['ChangeOrderId'];
// 检查应用部署状态
Expand Down Expand Up @@ -377,6 +371,18 @@ export default class SaeComponent {

logger.success(`部署成功,请通过以下地址访问您的应用:http://${result.accessLink}`);

/**
* 缓存记录上一次部署细节
*/
await writeDeployCache(
{
region,
appName,
configPath,
accountID: credentials.AccountID,
},
lastProps,
);
/**
* 删除oss文件
*/
Expand Down
26 changes: 25 additions & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import Oss from './oss.service';
import Client, { vpcAvailable } from './client';
import { OutputProps } from '../interface/entity';
import { isNumber, isString } from 'lodash';
import { getInquire } from './help/constant';
import { getDeployCache } from '../common/cache';
import { inquirer } from "@serverless-devs/core";

const { fse, lodash } = core;
const getFilename = (region, namespaceId, appName) => `${region}_${namespaceId}_${appName}`;
Expand Down Expand Up @@ -352,7 +355,7 @@ export async function handleCode(application: any, credentials: any, configPath?
return applicationObject;
}

export async function getDiff(application: any, slb: any, remoteData: any) {
export async function getDiff(application: any, slb: any, remoteData: any, credentials: any, configPath: string) {
const remoteResult = await infoRes(remoteData);
let localApp = lodash.cloneDeep(application);
const port = localApp.port;
Expand All @@ -365,6 +368,7 @@ export async function getDiff(application: any, slb: any, remoteData: any) {
let diffList = [];

let change = {
updateRemote:false,
needRescale: false,
needUpdateSecurityGroup: false,
needRescaleVertically: false,
Expand Down Expand Up @@ -465,6 +469,26 @@ export async function getDiff(application: any, slb: any, remoteData: any) {
for (let data of diffList) {
console.log(`${data.name}: ${data.remote} => ${data.local}`);
}

// 用户选择
// lastDeploy == localApp && remoteApp != localApp ----> 用户选择local或remote
// lastDeploy != localApp ----> 自动选择local
const lastDeploy = await getDeployCache(credentials.AccountID, localApp.region, localApp.appName, configPath);
if(!lodash.isEqual(lastDeploy.props.application, application) || !lodash.isEqual(lastDeploy.props.slb, slb)){
change.updateRemote = true;
}else{
const configInquire = getInquire(application.appName);
const ans: { option: string } = await inquirer.prompt(configInquire);
switch (ans.option) {
case 'use local':
change.updateRemote = true;
break;
case 'use remote':
break;
default:
break;
}
}
return change;
}

Expand Down

0 comments on commit cf54f23

Please sign in to comment.