Skip to content

Commit

Permalink
feat: logConfig obtained on-line
Browse files Browse the repository at this point in the history
  • Loading branch information
wss-git authored and SquatsTonight committed Jun 4, 2021
1 parent 44ac2c6 commit 9b7ee93
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 47 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ dist/
test/
**/*.min.js
**/*-min.js
**/*.bundle.js
**/*.bundle.js
examples/
8 changes: 4 additions & 4 deletions examples/fc-build/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
if you open the initializer feature, please implement the initializer function, as below:
module.exports.initializer = function(context, callback) {
console.log('initializing');
callback(null, '');
callback(null, '');
};
*/

module.exports.handler = function(event, context, callback) {
module.exports.handler = function (event, context, callback) {
console.log('hello world');
callback(null, 'hello world');
};
callback(null, 'hello world');
};
1 change: 0 additions & 1 deletion examples/remote-invoke/http-code/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const getRawBody = require('raw-body');

module.exports.handler = function (request, response, context) {

// get request body
getRawBody(request, (err, data) => {
const respBody = {
Expand Down
58 changes: 28 additions & 30 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as core from '@serverless-devs/core';
import * as _ from 'lodash';
import { COMPONENT_HELP_INFO } from './lib/static';
import { isAutoConfig, genServiceStateID } from './lib/utils';
import tarnsformNas from './lib/tarnsform-nas';
import { ICredentials } from './lib/interface/profile';
import { IInputs, IProperties } from './lib/interface/interface';
Expand Down Expand Up @@ -122,7 +121,7 @@ export default class FcBaseComponent {
property = {
region: props?.region,
serviceName: props?.service?.name,
}
};

if (!_.isNil(props?.function?.name)) {
property.functionName = props?.function?.name;
Expand Down Expand Up @@ -164,47 +163,46 @@ export default class FcBaseComponent {
const invokePayload: FcSyncProps = {
region: props?.region,
serviceName: props?.service?.name,
functionName: props?.function?.name
functionName: props?.function?.name,
};

await this.componentMethodCaller(inputs, 'devsapp/fc-remote-invoke', 'invoke', invokePayload, args);
}

async logs(inputs: IInputs): Promise<any> {
const { props, args, project } = this.handlerComponentInputs(inputs);
const { props, args } = this.handlerComponentInputs(inputs);

const comParse: any = core.commandParse({ args });
if (comParse?.data?.help || comParse?.data?.h) {
await this.componentMethodCaller(inputs, 'devsapp/logs', 'logs', props, args)
await this.componentMethodCaller(inputs, 'devsapp/logs', 'logs', props, args);
return;
}

const region = props?.region;
const serviceName = props?.service?.name;
let logConfig = props?.service?.logConfig;
if (!logConfig) {
throw new Error('Not fount logConfig.');
} else if (isAutoConfig(logConfig)) {
const credential: ICredentials = await core.getCredential(project?.access);
const stateId = genServiceStateID(credential.AccountID, props?.region, serviceName);
logConfig = (await core.getState(stateId))?.resolvedConfig?.logConfig;

if (!logConfig) {
throw new Error('It is detected that logConfig is auto, but the configuration is not obtained, please execute the [deploy] first.');

let logsPayload: LogsProps;
try {
const { logConfig } = (await this.info(inputs)).service || {};

if (!isLogConfig(logConfig)) {
throw new Error('The service logConfig is not found online, please confirm whether logConfig is configured first, and then execute [s exec - deploy].');
}
}

if (!isLogConfig(logConfig)) {
throw new Error(`logConfig does not meet expectations, the value obtained is ${JSON.stringify(logConfig)}, and the expected value exists in project,logstore.`);
}
const logsPayload: LogsProps = {
logConfig,
region,
topic: serviceName,
query: props?.function?.name,
logsPayload = {
logConfig,
region,
topic: serviceName,
query: props?.function?.name,
};
} catch (ex) {
if (ex.code?.endsWith('NotFound')) {
throw new Error(`Online search failed, error message: ${ex.message}. Please execute [s exec -- deploy]`);
}
throw ex;
}

await this.componentMethodCaller(inputs, 'devsapp/logs', 'logs', logsPayload, args)
await this.componentMethodCaller(inputs, 'devsapp/logs', 'logs', logsPayload, args);
}

async metrics(inputs: IInputs): Promise<any> {
Expand All @@ -215,7 +213,7 @@ export default class FcBaseComponent {
serviceName: props?.service?.name,
functionName: props?.function?.name,
};

await this.componentMethodCaller(inputs, 'devsapp/fc-metrics', 'metrics', payload, args);
}

Expand All @@ -225,11 +223,11 @@ export default class FcBaseComponent {

async nas(inputs: IInputs) {
const { props, args, project } = this.handlerComponentInputs(inputs);
const SUPPORTED_METHOD = ['ls', 'cp', 'rm', 'download', 'upload', 'command'];
const SUPPORTED_METHOD = ['remove', 'deploy', 'ls', 'cp', 'rm', 'download', 'upload', 'command'];

const apts = {
boolean: ['all', 'long', 'help', 'recursive', 'no-clobber', 'force'],
alias: { force: 'f', 'no-clobber': 'n', recursive: 'r', help: 'h', all: 'a', long: 'l' }
alias: { force: 'f', 'no-clobber': 'n', recursive: 'r', help: 'h', all: 'a', long: 'l' },
};
const comParse: any = core.commandParse({ args }, apts);

Expand All @@ -247,7 +245,7 @@ export default class FcBaseComponent {
return;
}

let tarnsformArgs = args.replace(commandName, '').replace(/(^\s*)|(\s*$)/g, '');
const tarnsformArgs = args.replace(commandName, '').replace(/(^\s*)|(\s*$)/g, '');

if (nonOptionsArgs.length === 1 && comParse?.data?.help) {
await this.componentMethodCaller(inputs, 'devsapp/nas', commandName, inputs.props, tarnsformArgs);
Expand Down
22 changes: 11 additions & 11 deletions src/lib/tarnsform-nas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { isEmpty, isString } from 'lodash';
import { isAutoConfig, genServiceStateID } from './utils';
import { ICredentials } from './interface/profile';

export default async function toNas (props, nonOptionsArgs, args, access) {
export default async function toNas(props, nonOptionsArgs, args, access) {
const {
vpcConfig,
nasConfig,
Expand All @@ -18,7 +18,7 @@ export default async function toNas (props, nonOptionsArgs, args, access) {
if (!vpcConfig) {
throw new Error('Not fount vpcConfig.');
}

const { vpcId, vswitchIds, securityGroupId } = vpcConfig;

if (!vpcId) {
Expand All @@ -33,7 +33,7 @@ export default async function toNas (props, nonOptionsArgs, args, access) {
const fcDirInput = getFcDirPath(nonOptionsArgs);
const { serverAddr, nasDir, tarnsforInputDir } = getMount(fcDirInput, mountPoints);
if (!serverAddr) {
core.Logger.warn('FC', 'Not fount serverAddr/serverAddr');
core.Logger.warn('FC', 'Not fount serverAddr/nasDir');
}

return {
Expand All @@ -51,7 +51,7 @@ export default async function toNas (props, nonOptionsArgs, args, access) {
mountPointDomain: serverAddr,
nasDir,
// excludes,
}
},
};
}

Expand All @@ -77,18 +77,18 @@ function getFcDirPath(inputPaths: string[]) {
}
}

async function getServiceConfig (props, access) {
let { name, vpcConfig, nasConfig, role } = props?.service || {};
async function getServiceConfig(props, access) {
const { name, vpcConfig, nasConfig, role } = props?.service || {};

if (isAutoConfig(nasConfig) || isAutoConfig(vpcConfig) || !isString(role)) {
const credential: ICredentials = await core.getCredential(access);
const stateId = genServiceStateID(credential.AccountID, props?.region, name);
const data = (await core.getState(stateId))?.resolvedConfig || {};

if (isEmpty(data)) {
throw new Error('Configuration is not obtained, please execute the [deploy] first.');
throw new Error('Configuration is not obtained, please execute the [s exec -- deploy] first.');
}

return data;
}

Expand All @@ -97,5 +97,5 @@ async function getServiceConfig (props, access) {
nasConfig,
name,
role: props?.service?.role,
}
}
};
}

0 comments on commit 9b7ee93

Please sign in to comment.