From 14473088bcf9ba68f986ad5c855f6577afa96a27 Mon Sep 17 00:00:00 2001 From: thomas-tacquet Date: Sun, 26 Jun 2022 17:47:22 +0200 Subject: [PATCH 1/6] serverless info returns yaml --- deploy/lib/createFunctions.js | 2 +- info/lib/display.js | 10 ++++++++-- package-lock.json | 16 +++++++++++++++- package.json | 3 ++- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/deploy/lib/createFunctions.js b/deploy/lib/createFunctions.js index cb97f838..da4025db 100644 --- a/deploy/lib/createFunctions.js +++ b/deploy/lib/createFunctions.js @@ -155,7 +155,7 @@ module.exports = { params.runtime = this.validateRuntime(func, availableRuntimes, this.serverless.cli); // checking if there is custom_domains set on function creation. - if (func.custom_domains.length > 0) { + if (func.custom_domains && func.custom_domains.length > 0) { this.serverless.cli.log("WARNING: custom_domains are available on function update only. "+ "Redeploy your function to apply custom domains. Doc : https://www.scaleway.com/en/docs/compute/functions/how-to/add-a-custom-domain-name-to-a-function/") } diff --git a/info/lib/display.js b/info/lib/display.js index faf53b6f..31fdc909 100644 --- a/info/lib/display.js +++ b/info/lib/display.js @@ -1,5 +1,7 @@ "use strict"; +const yaml = require('yaml'); + module.exports = { displayInfo() { const configInput = this.serverless.configurationInput; @@ -17,13 +19,17 @@ module.exports = { ) { this.listContainers(namespace.id).then((containers) => { containers.forEach((container) => { - this.serverless.cli.log(JSON.stringify(container, null, "\t")); + const doc = new yaml.Document(); + doc.contents = container; + this.serverless.cli.log(doc.toString()); }); }); } else { this.listFunctions(namespace.id).then((functions) => { functions.forEach((func) => { - this.serverless.cli.log(JSON.stringify(func, null, "\t")); + const doc = new yaml.Document(); + doc.contents = func; + this.serverless.cli.log(doc.toString()); }); }); } diff --git a/package-lock.json b/package-lock.json index 9df7b454..0396223a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,7 +14,8 @@ "axios": "^0.27.2", "bluebird": "^3.7.2", "dockerode": "^3.3.2", - "tar-fs": "^2.1.1" + "tar-fs": "^2.1.1", + "yaml": "^2.1.1" }, "devDependencies": { "@jest/globals": "^28.1.1", @@ -7177,6 +7178,14 @@ "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" }, + "node_modules/yaml": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.1.1.tgz", + "integrity": "sha512-o96x3OPo8GjWeSLF+wOAbrPfhFOGY0W00GNaxCDv+9hkcDJEnev1yh8S7pgHF0ik6zc8sQLuL8hjHjJULZp8bw==", + "engines": { + "node": ">= 14" + } + }, "node_modules/yargs": { "version": "17.5.1", "resolved": "https://registry.npmjs.org/yargs/-/yargs-17.5.1.tgz", @@ -12665,6 +12674,11 @@ "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" }, + "yaml": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.1.1.tgz", + "integrity": "sha512-o96x3OPo8GjWeSLF+wOAbrPfhFOGY0W00GNaxCDv+9hkcDJEnev1yh8S7pgHF0ik6zc8sQLuL8hjHjJULZp8bw==" + }, "yargs": { "version": "17.5.1", "resolved": "https://registry.npmjs.org/yargs/-/yargs-17.5.1.tgz", diff --git a/package.json b/package.json index 3ff51004..07b0599d 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,8 @@ "axios": "^0.27.2", "bluebird": "^3.7.2", "dockerode": "^3.3.2", - "tar-fs": "^2.1.1" + "tar-fs": "^2.1.1", + "yaml": "^2.1.1" }, "devDependencies": { "@jest/globals": "^28.1.1", From 67cf8f2c6d7166a881773dd1e477abb71ca853da Mon Sep 17 00:00:00 2001 From: thomas-tacquet Date: Mon, 27 Jun 2022 17:26:42 +0200 Subject: [PATCH 2/6] add special case to avoid logs in serverless info --- provider/scalewayProvider.js | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/provider/scalewayProvider.js b/provider/scalewayProvider.js index 6978eb36..3b2fe887 100644 --- a/provider/scalewayProvider.js +++ b/provider/scalewayProvider.js @@ -38,22 +38,36 @@ class ScalewayProvider { } setCredentials(options) { + // On serverless info command we do not want log pollution from authentication. + // This is necessary to use it in automated environment. + let hideLog = false; + if (this.serverless.configurationInput.service && this.serverless.configurationInput.service === 'serverlessInfo') { + hideLog = true; + } if (options['scw-token'] && options['scw-project']) { - this.serverless.cli.log('Using credentials from command line parameters'); + if (!hideLog) { + this.serverless.cli.log('Using credentials from command line parameters'); + } this.scwToken = options['scw-token']; this.scwProject = options['scw-project']; } else if (process.env.SCW_SECRET_KEY && process.env.SCW_DEFAULT_PROJECT_ID) { - this.serverless.cli.log('Using credentials from system environment'); + if (!hideLog) { + this.serverless.cli.log('Using credentials from system environment'); + } this.scwToken = process.env.SCW_SECRET_KEY; this.scwProject = process.env.SCW_DEFAULT_PROJECT_ID; } else if (process.env.SCW_TOKEN && process.env.SCW_PROJECT) { - this.serverless.cli.log('Using credentials from system environment'); - this.serverless.cli.log('NOTICE: you are using deprecated environment variable notation,'); - this.serverless.cli.log('please update to SCW_SECRET_KEY and SCW_DEFAULT_PROJECT_ID'); + if (!hideLog) { + this.serverless.cli.log('Using credentials from system environment'); + this.serverless.cli.log('NOTICE: you are using deprecated environment variable notation,'); + this.serverless.cli.log('please update to SCW_SECRET_KEY and SCW_DEFAULT_PROJECT_ID'); + } this.scwToken = process.env.SCW_TOKEN; this.scwProject = process.env.SCW_PROJECT; } else { - this.serverless.cli.log('Using credentials from yml'); + if (!hideLog) { + this.serverless.cli.log('Using credentials from yml'); + } this.scwToken = this.serverless.service.provider.scwToken || ''; this.scwProject = this.serverless.service.provider.scwProject || ''; } From 29e793358fdb389e9917ab3042530ecaaf28a394 Mon Sep 17 00:00:00 2001 From: thomas-tacquet Date: Mon, 27 Jun 2022 17:26:59 +0200 Subject: [PATCH 3/6] add tests for serverless info yaml --- tests/functions/functions.test.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/functions/functions.test.js b/tests/functions/functions.test.js index cb19ae33..746f243d 100644 --- a/tests/functions/functions.test.js +++ b/tests/functions/functions.test.js @@ -1,8 +1,10 @@ 'use strict'; const path = require('path'); +const yaml = require('yaml'); const fs = require('fs'); const axios = require('axios'); +const util = require('util') const { expect } = require('chai'); const { expect: jestExpect } = require('@jest/globals'); const { execSync } = require('../utils/child-process'); @@ -57,6 +59,13 @@ describe('Service Lifecyle Integration Test', () => { namespace.functions = await api.listFunctions(namespace.id); }); + it('should get function info and parse it', async () => { + const infoResult = execSync(`${serverlessExec} info`); + console.log(util.inspect(infoResult, false, null, true)); + const yamlo = yaml.parse(infoResult.toString('utf8')); + // WIP on YAML scan to match serverless config file (that should be the image of the API) + }); + it('should invoke function from scaleway', async () => { await sleep(30000); const deployedFunction = namespace.functions[0]; From 1252e45681358ba095299d8ea0bfd0d2183d083a Mon Sep 17 00:00:00 2001 From: thomas-tacquet Date: Tue, 28 Jun 2022 11:03:03 +0200 Subject: [PATCH 4/6] test in other context --- tests/functions/functions.test.js | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/tests/functions/functions.test.js b/tests/functions/functions.test.js index 746f243d..732968f8 100644 --- a/tests/functions/functions.test.js +++ b/tests/functions/functions.test.js @@ -1,10 +1,8 @@ 'use strict'; const path = require('path'); -const yaml = require('yaml'); const fs = require('fs'); const axios = require('axios'); -const util = require('util') const { expect } = require('chai'); const { expect: jestExpect } = require('@jest/globals'); const { execSync } = require('../utils/child-process'); @@ -13,6 +11,7 @@ const { getServiceName, sleep } = require('../utils/misc'); const { FunctionApi, RegistryApi } = require('../../shared/api'); const { FUNCTIONS_API_URL, REGISTRY_API_URL } = require('../../shared/constants'); const { validateRuntime } = require('../../deploy/lib/createFunctions'); +const { exec } = require('child_process'); const serverlessExec = path.join('serverless'); @@ -59,13 +58,6 @@ describe('Service Lifecyle Integration Test', () => { namespace.functions = await api.listFunctions(namespace.id); }); - it('should get function info and parse it', async () => { - const infoResult = execSync(`${serverlessExec} info`); - console.log(util.inspect(infoResult, false, null, true)); - const yamlo = yaml.parse(infoResult.toString('utf8')); - // WIP on YAML scan to match serverless config file (that should be the image of the API) - }); - it('should invoke function from scaleway', async () => { await sleep(30000); const deployedFunction = namespace.functions[0]; From 978e0822f5a82c139bdd10fb2bafe2fc2e6f6cef Mon Sep 17 00:00:00 2001 From: thomas-tacquet Date: Tue, 28 Jun 2022 11:07:39 +0200 Subject: [PATCH 5/6] clean --- tests/functions/functions.test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/functions/functions.test.js b/tests/functions/functions.test.js index 732968f8..cb19ae33 100644 --- a/tests/functions/functions.test.js +++ b/tests/functions/functions.test.js @@ -11,7 +11,6 @@ const { getServiceName, sleep } = require('../utils/misc'); const { FunctionApi, RegistryApi } = require('../../shared/api'); const { FUNCTIONS_API_URL, REGISTRY_API_URL } = require('../../shared/constants'); const { validateRuntime } = require('../../deploy/lib/createFunctions'); -const { exec } = require('child_process'); const serverlessExec = path.join('serverless'); From 750c7f0a75e854dfe781936e19f06d9297d89ffb Mon Sep 17 00:00:00 2001 From: thomas-tacquet Date: Tue, 28 Jun 2022 16:29:43 +0200 Subject: [PATCH 6/6] use existing yaml lib --- info/lib/display.js | 10 +++------- package-lock.json | 16 +--------------- package.json | 3 +-- 3 files changed, 5 insertions(+), 24 deletions(-) diff --git a/info/lib/display.js b/info/lib/display.js index 31fdc909..e4a48641 100644 --- a/info/lib/display.js +++ b/info/lib/display.js @@ -1,6 +1,6 @@ "use strict"; -const yaml = require('yaml'); +const yaml = require('js-yaml'); module.exports = { displayInfo() { @@ -19,17 +19,13 @@ module.exports = { ) { this.listContainers(namespace.id).then((containers) => { containers.forEach((container) => { - const doc = new yaml.Document(); - doc.contents = container; - this.serverless.cli.log(doc.toString()); + this.serverless.cli.log(yaml.dump(container)); }); }); } else { this.listFunctions(namespace.id).then((functions) => { functions.forEach((func) => { - const doc = new yaml.Document(); - doc.contents = func; - this.serverless.cli.log(doc.toString()); + this.serverless.cli.log(yaml.dump(func)); }); }); } diff --git a/package-lock.json b/package-lock.json index 0396223a..9df7b454 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,8 +14,7 @@ "axios": "^0.27.2", "bluebird": "^3.7.2", "dockerode": "^3.3.2", - "tar-fs": "^2.1.1", - "yaml": "^2.1.1" + "tar-fs": "^2.1.1" }, "devDependencies": { "@jest/globals": "^28.1.1", @@ -7178,14 +7177,6 @@ "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" }, - "node_modules/yaml": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.1.1.tgz", - "integrity": "sha512-o96x3OPo8GjWeSLF+wOAbrPfhFOGY0W00GNaxCDv+9hkcDJEnev1yh8S7pgHF0ik6zc8sQLuL8hjHjJULZp8bw==", - "engines": { - "node": ">= 14" - } - }, "node_modules/yargs": { "version": "17.5.1", "resolved": "https://registry.npmjs.org/yargs/-/yargs-17.5.1.tgz", @@ -12674,11 +12665,6 @@ "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" }, - "yaml": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.1.1.tgz", - "integrity": "sha512-o96x3OPo8GjWeSLF+wOAbrPfhFOGY0W00GNaxCDv+9hkcDJEnev1yh8S7pgHF0ik6zc8sQLuL8hjHjJULZp8bw==" - }, "yargs": { "version": "17.5.1", "resolved": "https://registry.npmjs.org/yargs/-/yargs-17.5.1.tgz", diff --git a/package.json b/package.json index 07b0599d..3ff51004 100644 --- a/package.json +++ b/package.json @@ -49,8 +49,7 @@ "axios": "^0.27.2", "bluebird": "^3.7.2", "dockerode": "^3.3.2", - "tar-fs": "^2.1.1", - "yaml": "^2.1.1" + "tar-fs": "^2.1.1" }, "devDependencies": { "@jest/globals": "^28.1.1",