Skip to content

Commit

Permalink
feat(builder): create option to create symlinks
Browse files Browse the repository at this point in the history
fixes #48
  • Loading branch information
tripodsan committed Jul 23, 2019
1 parent e450a85 commit fbf37e4
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 5 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,17 @@ In order to automatically use the version of the `package.json` use:
> **Note**: the version is internally taken from the `pkgVersion` variable, so it can be overridden with
the `--pkgVersion` argument, in case it should be deployed differently.

#### Automatically create semantic versioning sequence actions

By using the `--vesion-link` (`-l`), the bulider can create action sequences _linking_ to the deployed version,
using the semantic versioning notation: `latest`, `major`, `minor`:

| Action Name | Specifier | Sequence Name |
|-------------|-----------|---------------|
| `[email protected]` | `latest` | `foo@latest` |
| `[email protected]` | `major` | `foo@v2` |
| `[email protected]` | `minor` | `[email protected]` |

### Including static files

Adding static files, i.e. files that are not referenced from the `index.js` and detected by webpack,
Expand Down
51 changes: 47 additions & 4 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"openwhisk": "^3.17.0",
"request": "^2.88.0",
"request-promise-native": "^1.0.7",
"semver": "^6.2.0",
"serverless-http": "^2.0.2",
"webpack": "^4.36.1",
"yargs": "^13.3.0"
Expand Down
85 changes: 85 additions & 0 deletions src/action_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const chalk = require('chalk');
const dotenv = require('dotenv');
const os = require('os');
const ow = require('openwhisk');
const semver = require('semver');
const request = require('request-promise-native');
const { version } = require('../package.json');

Expand Down Expand Up @@ -127,6 +128,7 @@ module.exports = class ActionBuilder {
this._packageShared = false;
this._packageParams = {};
this._timeout = 60000;
this._links = [];
}

get log() {
Expand Down Expand Up @@ -295,6 +297,11 @@ module.exports = class ActionBuilder {
return this;
}

withLinks(value) {
this._links = value || [];
return this;
}

async validate() {
try {
this._pkgJson = await fse.readJson(path.resolve(this._cwd, 'package.json'));
Expand Down Expand Up @@ -587,6 +594,82 @@ module.exports = class ActionBuilder {
}
}

async updateLinks() {
if (this._links.length === 0) {
return;
}
const idx = this._name.lastIndexOf('@');
if (idx < 0) {
this.log.warn(`${chalk.yellow('warn:')} unable to create version sequence. unsupported action name format. should be: "name@version"`);
return;
}
const prefix = this._name.substring(0, idx + 1);
const s = semver.parse(this._version);
if (!s) {
this.log.warn(`${chalk.yellow('warn:')} unable to create version sequences. error while parsing version: ${this._version}`);
return;
}

const fqn = `/${this._wskNamespace}/${this._name}`;
const sfx = [];
this._links.forEach((link) => {
switch (link) {
case 'latest':
sfx.push('latest');
break;
case 'major':
sfx.push(`v${s.major}`);
break;
case 'minor':
sfx.push(`v${s.major}.${s.minor}`);
break;
default:
throw new Error(`Unsupported link type: ${link}`);
}
});

const openwhisk = ow({
apihost: this._wskApiHost,
api_key: this._wskAuth,
namespace: this._wskNamespace,
});

await Promise.all(sfx.map(async (sf) => {
const options = {
name: `${prefix}${sf}`,
action: {
namespace: this._wskNamespace,
name: `${prefix}${sf}`,
exec: {
kind: 'sequence',
components: [fqn],
},
annotations: [{
key: 'exec',
value: 'sequence',
}, {
key: 'web-export',
value: this._webAction,
}, {
key: 'raw-http',
value: this._rawHttp,
}, {
key: 'final',
value: true,
}],
},
};

try {
this.log.debug(`creating sequence: ${options.name} -> ${options.action.exec.components[0]}`);
const result = await openwhisk.actions.update(options);
this.log.info(`${chalk.green('ok:')} created sequence ${chalk.whiteBright(`/${result.namespace}/${result.name}`)} -> ${chalk.whiteBright(fqn)}`);
} catch (e) {
this.log.error(`${chalk.red('error: failed creating sequence:')} ${e.message}`);
}
}));
}

async run() {
this.log.info(chalk`{grey openwhisk-action-builder v${version}}`);
await this.validate();
Expand All @@ -608,5 +691,7 @@ module.exports = class ActionBuilder {
if (typeof this._test === 'string') {
await this.test(this._test);
}

await this.updateLinks();
}
};
10 changes: 9 additions & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ class CLI {
type: 'boolean',
default: true,
})
.group(['build', 'deploy', 'test', 'hints', 'update-package'], 'Operation Options')
.option('version-link', {
alias: 'l',
description: 'Create symlinks (sequences) after deployment',
type: 'string',
array: true,
choices: ['latest', 'major', 'minor'],
})
.group(['build', 'deploy', 'test', 'hints', 'update-package', 'version-link'], 'Operation Options')

.option('name', {
description: 'OpenWhisk action name. Can be prefixed with package.',
Expand Down Expand Up @@ -183,6 +190,7 @@ class CLI {
.withPackageParams(argv.package.params)
.withPackageParamsFile(argv.package['params-file'])
.withTimeout(argv.timeout)
.withLinks(argv.versionLink)
.withPackageShared(argv.package.shared);
}

Expand Down
6 changes: 6 additions & 0 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ describe('CLI Test', () => {
assert.equal(builder._showHints, false);
});

it('sets links', () => {
const builder = new CLI()
.prepare(['--version-link', 'latest', '-l', 'major']);
assert.deepEqual(builder._links, ['latest', 'major']);
});

it('sets disable web-action', () => {
const builder = new CLI()
.prepare(['--no-web-export']);
Expand Down

0 comments on commit fbf37e4

Please sign in to comment.