Skip to content

Commit

Permalink
Merge pull request #1010 from wss-git/wss/fix-customContainer-codeuri
Browse files Browse the repository at this point in the history
Wss/fix custom container codeuri
  • Loading branch information
anycodes authored Sep 28, 2020
2 parents 0637391 + 4529f1f commit 2d5e94e
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 49 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@alicloud/fun",
"version": "3.6.16",
"version": "3.6.17",
"description": "(have)Fun with Serverless",
"engines": {
"node": ">=8.6.0"
Expand Down
1 change: 1 addition & 0 deletions src/bin/fun-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ program
.option('-b, --oss-bucket <bucket>', 'The name of the oss bucket where Fun uploads local artifacts')
.option('-o, --output-template-file <filename>', 'The output path of the packaged template file')
.option('--use-nas', 'Automatically upload local resources to NAS.')
.option('--push-registry <pushRegistry>', 'Modify the image upload path')
.parse(process.argv);

if (program.args.length > 1) {
Expand Down
13 changes: 10 additions & 3 deletions src/lib/build/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,21 @@ function updateTemplateResources(originTplContent, buildFuncs, skippedBuildFuncs
const absCodeDir = path.resolve(baseDir, functionRes.Properties.CodeUri);
const relativeCodeUri = path.relative(absRootArtifactsDir, absCodeDir);

functionRes.Properties.CodeUri = relativeCodeUri;

if (functionRes.Properties.Runtime === 'custom-container') {
delete functionRes.Properties.CodeUri;
} else {
functionRes.Properties.CodeUri = relativeCodeUri;
}
} else { // refer to artifact dir
const funcArtifactDir = path.join(rootArtifactsDir, serviceName, functionName);
const absFuncArtifactDir = path.resolve(baseDir, funcArtifactDir);
const relativeCodeUri = path.relative(absRootArtifactsDir, absFuncArtifactDir);

functionRes.Properties.CodeUri = relativeCodeUri;
if (functionRes.Properties.Runtime === 'custom-container') {
delete functionRes.Properties.CodeUri;
} else {
functionRes.Properties.CodeUri = relativeCodeUri;
}
}
});

Expand Down
3 changes: 2 additions & 1 deletion src/lib/commands/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ async function pack(options) {
const bucket = options.ossBucket;
const useNas = options.useNas;
const outputTemplateFile = options.outputTemplateFile;
const pushRegistry = options.pushRegistry;

if (!tplPath) {
tplPath = await detectTplPath();
Expand All @@ -19,7 +20,7 @@ async function pack(options) {

validateTplName(tplPath);

await require('../package/package').pack(tplPath, bucket, outputTemplateFile, useNas);
await require('../package/package').pack(tplPath, bucket, outputTemplateFile, useNas, pushRegistry);
}

module.exports = pack;
42 changes: 2 additions & 40 deletions src/lib/deploy/deploy-by-tpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const debug = require('debug')('fun:deploy');
const definition = require('../definition');
const date = require('date-and-time');

const { execSync } = require('child_process');
const { getFunctionImage } = require('../package/pushImage');
const { deployByRos } = require('./deploy-support-ros');
const { importService } = require('../import/service');
const { getProfile, mark } = require('../profile');
Expand Down Expand Up @@ -939,44 +939,6 @@ async function deployByApi(baseDir, tpl, tplPath, context) {
}
}

async function getpushRegistry(image, pushRegistry, region, configImage) {
const imageArr = image.split('/');
if (pushRegistry === 'acr-internet') {
imageArr[0] = `registry.${region}.aliyuncs.com`;
image = imageArr.join('/');
} else if (pushRegistry === 'acr-vpc') {
imageArr[0] = `registry-vpc.${region}.aliyuncs.com`;
image = imageArr.join('/');
} else if (pushRegistry) {
imageArr[0] = pushRegistry;
image = imageArr.join('/');
}
console.log(`docker tag ${configImage} ${image}`);
execSync(`docker tag ${configImage} ${image}`, {
stdio: 'inherit'
});
console.log(`docker push ${image}`);
execSync(`docker push ${image}`, {
stdio: 'inherit'
});
}

async function getFunctionImage({ tpl, pushRegistry, region }) {
for (const k of _.keys(tpl)) {
const v = tpl[k];
if (_.isObject(v)) {
if (v.Type === 'Aliyun::Serverless::Function') {
const { CustomContainerConfig = {} } = v.Properties || {};
let image = CustomContainerConfig.Image;
if (image) {
await getpushRegistry(image, pushRegistry, region, CustomContainerConfig.Image);
}
} else {
await getFunctionImage({ tpl: v, pushRegistry, region });
}
}
}
}

async function deploy(tplPath, context) {
if (!context.useRos) {
Expand All @@ -990,7 +952,7 @@ async function deploy(tplPath, context) {
const profile = await getProfile();

if (context.pushRegistry) {
getFunctionImage({ tpl, region: profile.defaultRegion, pushRegistry: context.pushRegistry });
await getFunctionImage({ tpl, region: profile.defaultRegion, pushRegistry: context.pushRegistry });
}

console.log(`using region: ${profile.defaultRegion}`);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/deploy/deploy-support-ros.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ async function detectRosHttpTrigger(rosResources) {
continue;
}

await trigger.displayTriggerInfo(serviceName, functionName, triggerName, triggerProp.TriggerType, triggerProperties);
await trigger.displayTriggerInfo(serviceName, functionName, triggerName, triggerProp.TriggerType, triggerProperties, '', rosResources);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/lib/language-service/schema/rosSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ export const rosSchema = {
},
'required': ['Image']
},
"CodeUri": {
"type": "string"
},
"Description": {
"type": "string"
},
Expand Down
7 changes: 6 additions & 1 deletion src/lib/package/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const { showPackageNextTips } = require('../build/tips');
const { ensureFilesModified } = require('../utils/file');
const { parseMountDirPrefix } = require('../fc');
const { getTpl, detectNasBaseDir, getNasYmlPath } = require('../tpl');
const { getFunctionImage } = require('./pushImage');
const { promptForConfirmContinue, promptForInputContinue } = require('../init/prompt');
const { validateNasAndVpcConfig, SERVICE_RESOURCE, iterateResources, isNasAutoConfig, isVpcAutoConfig, getUserIdAndGroupId } = require('../definition');

Expand Down Expand Up @@ -268,7 +269,7 @@ async function processOSSBucket(bucket) {
return await generateOssBucket(bucket);
}

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

Expand All @@ -281,6 +282,10 @@ async function pack(tplPath, bucket, outputTemplateFile, useNas) {
const ossClient = await getOssClient(bucketName);

const updatedEnvTpl = await processNasPythonPaths(tpl, tplPath);
if (pushRegistry) {
const profile = await getProfile();
await getFunctionImage({ tpl, region: profile.defaultRegion, pushRegistry });
}
const updatedCodeTpl = await uploadAndUpdateFunctionCode({ tpl: updatedEnvTpl, tplPath, baseDir, ossClient, useNas });
const updatedSlsTpl = await transformSlsAuto(updatedCodeTpl);
const updatedFlowTpl = await transformFlowDefinition(baseDir, transformCustomDomain(updatedSlsTpl));
Expand Down
45 changes: 45 additions & 0 deletions src/lib/package/pushImage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const _ = require('lodash');
const { execSync } = require('child_process');

async function getpushRegistry(image, pushRegistry, region, configImage) {
const imageArr = image.split('/');
if (pushRegistry === 'acr-internet') {
imageArr[0] = `registry.${region}.aliyuncs.com`;
image = imageArr.join('/');
} else if (pushRegistry === 'acr-vpc') {
imageArr[0] = `registry-vpc.${region}.aliyuncs.com`;
image = imageArr.join('/');
} else if (pushRegistry) {
imageArr[0] = pushRegistry;
image = imageArr.join('/');
}
console.log(`docker tag ${configImage} ${image}`);
execSync(`docker tag ${configImage} ${image}`, {
stdio: 'inherit'
});
console.log(`docker push ${image}`);
execSync(`docker push ${image}`, {
stdio: 'inherit'
});
}

async function getFunctionImage({ tpl, pushRegistry, region }) {
for (const k of _.keys(tpl)) {
const v = tpl[k];
if (_.isObject(v)) {
if (v.Type === 'Aliyun::Serverless::Function') {
const { CustomContainerConfig = {} } = v.Properties || {};
let image = CustomContainerConfig.Image;
if (image) {
await getpushRegistry(image, pushRegistry, region, CustomContainerConfig.Image);
}
} else {
await getFunctionImage({ tpl: v, pushRegistry, region });
}
}
}
}

module.exports = {
getFunctionImage, getpushRegistry
};
7 changes: 6 additions & 1 deletion src/lib/package/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,9 +616,14 @@ async function uploadAndUpdateFunctionCode({ tpl, tplPath, useNas, baseDir, ossC
const codeUriCache = new Map();

for (const {serviceName, serviceRes, functionName, functionRes} of definition.findFunctionsInTpl(updatedTplContent)) {
const runtime = (functionRes.Properties || {}).Runtime;
if (runtime === 'custom-container') {
delete (functionRes.Properties || {}).CodeUri;
continue;
}

if (isOssUrl((functionRes.Properties || {}).CodeUri)) { continue; }

const runtime = (functionRes.Properties || {}).Runtime;
const codeUri = (functionRes.Properties || {}).CodeUri;
const absCodeUri = path.resolve(baseDir, codeUri);

Expand Down
3 changes: 3 additions & 0 deletions src/lib/validate/schema/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ const functionSchema = {
'CAPort': {
'type': 'integer'
},
'CodeUri': {
'type': 'string'
},
'InstanceConcurrency': {
'type': 'integer',
'minimum': 1,
Expand Down

0 comments on commit 2d5e94e

Please sign in to comment.