Skip to content

Commit

Permalink
Merge pull request #1038 from wss-git/wss/add-policy-to-user
Browse files Browse the repository at this point in the history
feat(deploy/ros-codeuri): deploy support ros codeyuri
  • Loading branch information
rsonghuster authored Dec 4, 2020
2 parents bae5304 + fdc652c commit 4c40112
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/lib/deploy/deploy-by-tpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ async function deploy(tplPath, context) {
console.log(yellow(`missing --stack-name parameter, using default stackName '${dirName}'`));
}
const stackName = context.stackName || DEFAULT_STACK_NAME;
await deployByRos(baseDir, stackName, tpl, context.assumeYes, context.parameterOverride);
await deployByRos(baseDir, stackName, tpl, context.assumeYes, context.parameterOverride, tplPath);
} else {

await deployByApi(baseDir, tpl, tplPath, context);
Expand Down
9 changes: 7 additions & 2 deletions src/lib/deploy/deploy-support-ros.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const promiseRetry = require('../retry');
const { getProfile } = require('../profile');
const { green, red, yellow } = require('colors');
const { promptForConfirmContinue } = require('../init/prompt');
const { transformRosYmlCodeUri } = require('../package/template');
const { outputTemplateFile } = require('../import/utils');

const log = require('single-line-log').stdout;
const Table = require('cli-table3');
Expand Down Expand Up @@ -661,13 +663,16 @@ function transformAsyncConfiguration (resources = {}, region, accountId) {
});
}

async function deployByRos(baseDir, stackName, tpl, assumeYes, parameterOverride = {}) {
async function deployByRos(baseDir, stackName, tpl, assumeYes, parameterOverride = {}, tplPath) {
const profile = await getProfile();
const region = profile.defaultRegion;
const { accountId } = profile;

transformAsyncConfiguration(tpl.Resources, region, accountId);

if (!tpl.Transform) {
await transformRosYmlCodeUri({ baseDir, tpl, tplPath });
outputTemplateFile(path.join(process.cwd(), 'template.packaged.yml'), tpl);
}
const rosClient = await client.getRosClient();

let { stackId, stackStatus } = await findRosStack(rosClient, region, stackName);
Expand Down
3 changes: 2 additions & 1 deletion src/lib/fc.js
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,7 @@ async function checkAlreadyConfirmedForCustomSpringBoot(runtime, codeUri) {
async function nasAutoConfigurationIfNecessary({ stage, tplPath, runtime, codeUri, nasConfig, vpcConfig, useNas = false, assumeYes,
compressedSize = 0,
nasFunctionName,
isRosCodeUri,
nasServiceName
}) {

Expand All @@ -964,7 +965,7 @@ async function nasAutoConfigurationIfNecessary({ stage, tplPath, runtime, codeUr
const tipOssUploadCodeSize = Math.floor(ossUploadCodeSize / 1024 / 1024);
const maxCodeSize = packageStage ? ossUploadCodeSize : 52428800;

if (!_.includes(SUPPORT_RUNTIMES, runtime) || (!useNas && compressedSize < maxCodeSize)) { return { stop, tplChanged }; }
if (isRosCodeUri || !_.includes(SUPPORT_RUNTIMES, runtime) || (!useNas && compressedSize < maxCodeSize)) { return { stop, tplChanged }; }

if (compressedSize > maxCodeSize) {
if (packageStage) {
Expand Down
6 changes: 4 additions & 2 deletions src/lib/language-service/schema/ros/ALIYUN-FC-Function.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@
},
"Code": {
"type": "object"
},
"CodeUri": {
"type": "string"
}
},
"required": [
"FunctionName",
"Runtime",
"ServiceName",
"Handler",
"Code"
"Handler"
],
"additionalProperties": false,
"document": {
Expand Down
55 changes: 55 additions & 0 deletions src/lib/oss.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const { getProfile } = require('./profile');
const { getOssClient } = require('./client');
const { promptForConfirmContinue, promptForInputContinue } = require('./init/prompt');
const { yellow } = require('colors');

const _ = require('lodash');


async function bucketExist(ossClient, bucketName) {
let bucketExist = false;

try {
await ossClient.getBucketLocation(bucketName);
bucketExist = true;
} catch (ex) {

if (!ex.code || !_.includes(['AccessDenied', 'NoSuchBucket'], ex.code)) {
throw ex;
}
}
return bucketExist;
}


async function generateOssBucket(bucketName) {
const ossClient = await getOssClient();

if (await bucketExist(ossClient, bucketName)) {
console.log(yellow(`using oss-bucket: ${bucketName}`));
return bucketName;
}

console.log(yellow(`using oss-bucket: ${bucketName}`));

if (process.stdin.isTTY && !await promptForConfirmContinue('Auto generate OSS bucket for you?')) {
bucketName = (await promptForInputContinue('Input OSS bucket name:')).input;
}

await ossClient.putBucket(bucketName);

return bucketName;
}

async function processOSSBucket(bucket) {
if (!bucket) {
const profile = await getProfile();
const defalutBucket = `fun-gen-${profile.defaultRegion}-${profile.accountId}`;
return await generateOssBucket(defalutBucket);
}
return await generateOssBucket(bucket);
}

module.exports = {
processOSSBucket
};
77 changes: 24 additions & 53 deletions src/lib/package/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ const _ = require('lodash');
const { getProfile } = require('../profile');
const { isEmptyDir } = require('../nas/cp/file');
const { getOssClient } = require('../client');
const { green, yellow } = require('colors');
const { green } = require('colors');
const { showPackageNextTips } = require('../build/tips');
const { ensureFilesModified } = require('../utils/file');
const { parseMountDirPrefix } = require('../fc');
const { transformRosYmlCodeUri } = require('./template');
const { getTpl, detectNasBaseDir, getNasYmlPath } = require('../tpl');
const { getFunctionImage } = require('./pushImage');
const { promptForConfirmContinue, promptForInputContinue } = require('../init/prompt');
const { processOSSBucket } = require('../oss');
const { validateNasAndVpcConfig, SERVICE_RESOURCE, iterateResources, isNasAutoConfig, isVpcAutoConfig, getUserIdAndGroupId } = require('../definition');

const {
Expand All @@ -40,6 +41,7 @@ const {
generateRosTemplateForDefaultOutputs,
generateRosTemplateForDefaultResources
} = require('./template');
const utils = require('../import/utils');

async function processNasAutoToRosTemplate({ tpl, baseDir, tplPath,
ossClient,
Expand Down Expand Up @@ -227,51 +229,11 @@ async function transformSlsAuto(tpl) {
return cloneTpl;
}

async function bucketExist(ossClient, bucketName) {
let bucketExist = false;

try {
await ossClient.getBucketLocation(bucketName);
bucketExist = true;
} catch (ex) {

if (!ex.code || !_.includes(['AccessDenied', 'NoSuchBucket'], ex.code)) {
throw ex;
}
}
return bucketExist;
}

async function generateOssBucket(bucketName) {
const ossClient = await getOssClient();

if (await bucketExist(ossClient, bucketName)) {
console.log(yellow(`using oss-bucket: ${bucketName}`));
return bucketName;
}

console.log(yellow(`using oss-bucket: ${bucketName}`));

if (process.stdin.isTTY && !await promptForConfirmContinue('Auto generate OSS bucket for you?')) {
bucketName = (await promptForInputContinue('Input OSS bucket name:')).input;
}

await ossClient.putBucket(bucketName);

return bucketName;
}
async function processOSSBucket(bucket) {
if (!bucket) {
const profile = await getProfile();
const defalutBucket = `fun-gen-${profile.defaultRegion}-${profile.accountId}`;
return await generateOssBucket(defalutBucket);
}
return await generateOssBucket(bucket);
}

async function pack(tplPath, bucket, outputTemplateFile, useNas, pushRegistry) {
const tpl = await getTpl(tplPath);
validateNasAndVpcConfig(tpl.Resources);
if (tpl.Transform) {
validateNasAndVpcConfig(tpl.Resources);
}

const baseDir = path.dirname(tplPath);

Expand All @@ -281,6 +243,23 @@ async function pack(tplPath, bucket, outputTemplateFile, useNas, pushRegistry) {

const ossClient = await getOssClient(bucketName);

let packedYmlPath;

if (outputTemplateFile) {
packedYmlPath = path.resolve(process.cwd(), outputTemplateFile);
} else {
packedYmlPath = path.join(process.cwd(), 'template.packaged.yml');
}

if (!tpl.Transform) {
await transformRosYmlCodeUri({ baseDir, tpl, tplPath, bucketName, ossClient });
path.join(process.cwd(), 'template.packaged.yml');
utils.outputTemplateFile(packedYmlPath, tpl);
console.log(green('\nPackage success'));
showPackageNextTips(packedYmlPath);
return;
}

const updatedEnvTpl = await processNasPythonPaths(tpl, tplPath);
if (pushRegistry) {
const profile = await getProfile();
Expand All @@ -291,14 +270,6 @@ async function pack(tplPath, bucket, outputTemplateFile, useNas, pushRegistry) {
const updatedFlowTpl = await transformFlowDefinition(baseDir, transformCustomDomain(updatedSlsTpl));
const updatedTpl = await processNasAutoToRosTemplate({ ossClient, baseDir, tplPath, tpl: updatedFlowTpl, bucketName });

let packedYmlPath;

if (outputTemplateFile) {
packedYmlPath = path.resolve(process.cwd(), outputTemplateFile);
} else {
packedYmlPath = path.join(process.cwd(), 'template.packaged.yml');
}

util.outputTemplateFile(packedYmlPath, updatedTpl);

console.log(green('\nPackage success'));
Expand Down
42 changes: 42 additions & 0 deletions src/lib/package/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const { getTpl } = require('../tpl');
const { green, yellow } = require('colors');
const { generateRandomZipPath } = require('../utils/path');
const { generateNasPythonPaths, mergeEnvs } = require('../../lib/install/env');
const { processOSSBucket } = require('../oss');
const { getOssClient } = require('../client');

const _ = require('lodash');

Expand Down Expand Up @@ -504,6 +506,7 @@ async function zipCodeToOss({ ossClient, codeUri, runtime, ignore, tplPath, nasC
zipName = 'code.zip',
prefix = '',
useNas = false,
isRosCodeUri = false,
zlibOptions = {}
}) {

Expand All @@ -527,6 +530,7 @@ async function zipCodeToOss({ ossClient, codeUri, runtime, ignore, tplPath, nasC

const rs = await fc.nasAutoConfigurationIfNecessary({ stage: 'package', tplPath, compressedSize, useNas,
nasConfig, runtime, codeUri,
isRosCodeUri,
nasServiceName: serviceName,
nasFunctionName: functionName
});
Expand Down Expand Up @@ -727,8 +731,46 @@ async function transformFlowDefinition(baseDir, tpl) {
return updatedTplContent;
}

async function transformRosYmlCodeUri ({ baseDir, tpl, tplPath, bucketName, ossClient }) {
if (!ossClient) {
bucketName = await processOSSBucket();
ossClient = await getOssClient(bucketName);
}

for (const key of Object.keys(tpl.Resources)) {
const { Type, Properties: properties } = tpl.Resources[key];
if (Type === 'ALIYUN::FC::Function' && properties.Runtime !== 'custom-container' && !properties.Code) {
if (!properties.CodeUri) {
throw new Error(`ALIYUN::FC::Function Code is empty.`);
}

const ignore = await fc.generateFunIngore(baseDir, properties.CodeUri);
const oss = await zipCodeToOss({
tplPath,
ignore,
ossClient,
codeUri: properties.CodeUri,
runtime: properties.Runtime,
isRosCodeUri: true
});

if (!oss.objectName) {
throw new Error(`Codeuri ${properties.CodeUri} upload to oss error.`);
}
tpl.Resources[key].Properties.Code = {
OssBucketName: bucketName,
OssObjectName: oss.objectName
};
delete tpl.Resources[key].Properties.CodeUri;
}
}
return tpl;
}

module.exports = {
zipToOss,
zipCodeToOss,
transformRosYmlCodeUri,
uploadNasService,
processNasPythonPaths,
transformFlowDefinition,
Expand Down

0 comments on commit 4c40112

Please sign in to comment.