Skip to content

Commit

Permalink
Version 1.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
zirkerc committed Jan 5, 2018
1 parent f6ab0c8 commit 01dc683
Show file tree
Hide file tree
Showing 17 changed files with 494 additions and 8 deletions.
129 changes: 129 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
LeoPlatform/cli
===================

Leo CLI

A Nodejs interface to interact with the Leo SDK and AWS

Documentation: https://docs.leoplatform.io

How to install the Leo SDK
===================================

Pre-Requisites
--------------
1. Install the aws-cli toolkit - Instructions for this are found at http://docs.aws.amazon.com/cli/latest/userguide/installing.html
2. Configure the aws-cli tools - Instructions are found at http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html
3. Install node - https://nodejs.org/en/

Install SDK
-----------
1. Install using npm. In your project folder run the following command.

```
npm install leo-cli -g
```

How to use the Leo SDK
===================================

Create a System
---------------

```
leo-cli create system MySystem
```

Create a Microservice
---------------
Inside a system directory

```
leo-cli create microservice MyService
```


Build Bots & Apis
-----------------
Inside a microservice directory

```
leo-cli create load MyLoadBot
leo-cli create enrich MyEnrichBot
leo-cli create offload MyOffloadBot
leo-cli create bot MyBot
leo-cli create cron MyCronBot
```

Testing Bots & Apis
-------------------
Inside the bot or resource directory

```
leo-cli test .
```

Runing Bots
-----------
Inside the bot or resource directory

```
leo-cli run .
```

Deploying Microservices, Bots, & Apis
-------------------------------------

```
leo-cli publish .
```

options
* --region awsRegion Sets the AWS Region. default: us-west-2
* --filter idGlobExp Filters the lambdas deployed by the given glob expression. default: *
* --force botId|all Forces a bot to build event if version are the same (Must be included in --filter expression). default: false
* --run awsStackName Runs the generated cloudformation.json against the AWS Stack 'awsStackName'. If the stack doesn't exist, it will be crated
* --build Builds the cloudformation and lambdas but doesn't publish them to s3
* --public Makes the s3 publish folder public

Version of the build using the microservice/bot/api package.json file. If a bot/api is forced to be built and has the same version number the current timestamp will be appended to he version

Deploy Examples
---------------

Publish a Microservice with all new/updated bots & apis

```
cd /MySystem/MyService
leo-cli publish .
```


Publish a Microservice and force all bots & apis to build

```
cd /MySystem/MyService
leo-cli publish .
```


Publish a single bot in a Microservice

```
cd /MySystem/MyService/Bot1
leo-cli publish .
```


Publish a single bot or resource in a Microservice

```
cd /MySystem/MyService/Bot1
leo-cli publish .
```

```
cd /MySystem/MyService
leo-cli publish . --filter Bot1
```

134 changes: 134 additions & 0 deletions leo-cli-create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#!/usr/bin/env node

var fs = require('fs');
var path = require('path');
var program = require('commander');
var colors = require('colors');

program
.version('0.0.2')
.arguments('<type> <dir>')
.usage('<type> <dir> [options]')
.action(function (type, dir) {
var pkgname = null;
let declaredType = type = type.toLowerCase();


var parentType = findFirstPackageValue(process.cwd(), [], "type");

let roots = {
bot: new RegExp("bots[/\\\\]"),
load: new RegExp("bots[/\\\\]"),
enrich: new RegExp("bots[/\\\\]"),
offload: new RegExp("bots[/\\\\]"),
resource: new RegExp("apis[/\\\\]"),
};

if (['system', 'microservice', 'resource', 'load', 'enrich', 'offload'].indexOf(type) === -1) {
type = "bot";
}
let prefix = "./";

if (roots[type] && !path.resolve(dir).match(roots[type])) {
prefix = roots[type] || "";
}

if (!fs.existsSync(prefix)) {
fs.mkdirSync(prefix);
}
if (!fs.existsSync(prefix + dir)) {
if (type == "microservice") {

if (parentType != "system") {
console.log(`Type ${type} must be within a system package`);
process.exit(1);
}

copyDirectorySync(__dirname + "/templates/microservice", prefix + dir, {
'____DIRNAME____': dir
});
} else if (type == "system") {
copyDirectorySync(__dirname + "/templates/system", prefix + dir, {
'____DIRNAME____': dir
});
} else {
if (parentType != "microservice" && parentType != "system") {
console.log(`Type ${type} must be within a system or microservice package`);
process.exit(1);
}

copyDirectorySync(__dirname + `/templates/${type}`, prefix + dir, {
'____DIRNAME____': dir,
'____BOTNAME____': dir,
'____BOTTYPE____': declaredType
});
}

process.chdir(prefix + dir);
console.log("done");

} else {
console.log("Directory already exists");
}
})
.parse(process.argv);

if (!process.argv.slice(2).length) {
program.outputHelp(colors.red);
}

function copyDirectorySync(src, dest, replacements) {
var stats = fs.statSync(src);
if (stats.isDirectory()) {
fs.mkdirSync(dest);
fs.readdirSync(src).forEach(function (entry) {
copyDirectorySync(path.join(src, entry), path.join(dest, entry), replacements);
});
} else {
var fileText = fs.readFileSync(src).toString('utf8');
for (var replaceVar in replacements) {
fileText = fileText.replace(new RegExp(replaceVar, 'g'), replacements[replaceVar]);
}

fs.writeFileSync(dest, fileText);
}
}

function findParentFiles(dir, filename) {
var paths = [];
do {
paths.push(dir);

var lastDir = dir;
dir = path.resolve(dir, "../");
} while (dir != lastDir);

var matches = [];
paths.forEach(function (dir) {
var file = path.resolve(dir, filename);
if (fs.existsSync(file)) {

matches.push(file);
}
});

return matches;
}

function findFirstPackageValue(dir, types, field, reverse) {
if (!Array.isArray(types)) {
types = [types];
}
var paths = findParentFiles(dir, "package.json");
if (reverse) {
paths.reverse();
}
for (var i = 0; i < paths.length; i++) {
var file = paths[i];
var pkg = require(file);
if (pkg && pkg.config && pkg.config.leo && (types.length === 0 || types.indexOf(pkg.config.leo.type) !== -1)) {
return pkg.config.leo[field];
}
}
return null;
}
3 changes: 3 additions & 0 deletions leo-cli-run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

require("./leo-cli-test");
15 changes: 14 additions & 1 deletion leo-cli-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ program
.replace("____FILE____", path.normalize(path.resolve(rootDir, pkg.main || "index.js")).replace(/\\/g, "\\\\"))
.replace("____HANDLER____", config.handler || "handler");


contents = `"use strict";
var configure = require("leo-sdk").configuration;
var AWS = require("aws-sdk");
if (configure.aws.profile && process.env.AWS_DEFAULT_PROFILE != configure.aws.profile) {
console.log("Setting aws profile to", configure.aws.profile);
var credentials = new AWS.SharedIniFileCredentials({
profile: configure.aws.profile
});
AWS.config.credentials = credentials;
process.env.AWS_DEFAULT_PROFILE = configure.aws.profile;
}\n` + contents.replace(`"use strict";`, "");

// Compile the module to run
let r = path.normalize(path.resolve(rootDir, "__leo-cli-test-runner.js")).replace(/\\/g, "\\\\");
let m = new modulejs(r, module);
Expand Down Expand Up @@ -150,7 +163,7 @@ program

handler(runner.event(event), createContext(pkg, config), (err, data) => {
runner.callback(err, data, (err, data) => {
console.log("\n\n\n--------------------------Results--------------------------\n")
data && console.log("\n\n\n--------------------------Results--------------------------\n")
if (err) {
console.log("Error:", err)
} else {
Expand Down
6 changes: 4 additions & 2 deletions leo-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ var configure = require("./package.json");

program
.version(configure.version)
.command('publish [directory] [alias] [region]', "Publish your project to S3")
.command('test [directory] [alias] [region]', "Test your lambda")
.command('publish [directory]', "Publish your project to S3")
.command('test [directory]', "Test your lambda")
.command('run [directory]', "Run your lambda")
.command('create [type] [directory]', "Create a new leo system, bot, resource, or microservice")
.parse(process.argv);
3 changes: 0 additions & 3 deletions lib/test/cron.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ module.exports = {
return a;
},
callback: (err, response, callback) => {
if (!err && response.body && response.statusCode === 200) {
response.body = JSON.parse(response.body);
}
callback(err, response);
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "leo-cli",
"version": "1.0.5",
"version": "1.0.6",
"description": "",
"main": "index.js",
"directories": {
Expand Down Expand Up @@ -66,7 +66,7 @@
"json-loader": "^0.5.4",
"jsonpath": "^0.2.9",
"later": "^1.2.0",
"leo-sdk": "^1.0.32",
"leo-sdk": "^1.0.42",
"less": "^2.7.1",
"less-loader": "^2.2.3",
"lodash.merge": "^4.6.0",
Expand Down
7 changes: 7 additions & 0 deletions templates/bot/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";

var leo = require("leo-sdk");
exports.handler = function (event, context, callback) {
// Do work
callback();
}
28 changes: 28 additions & 0 deletions templates/bot/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "____DIRNAME____",
"version": "1.0.0",
"description": "",
"main": "index.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "leo-cli test . "
},
"config": {
"leo": {
"type": "____BOTTYPE____",
"memory": 128,
"timeout": 10,
"role": "ApiRole",
"env": {},
"cron": {
"settings": {},
"time": "0 */1 * * * * "
}
}
},
"dependencies": {
"leo-sdk": "^1.0.41"
}
}
Loading

0 comments on commit 01dc683

Please sign in to comment.