clairlune 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f9a36a15714b36210021c692f6e0cbdf79e5baa7
4
+ data.tar.gz: e9996c945eee2c45bc04e2de3fba7659b5c77a1e
5
+ SHA512:
6
+ metadata.gz: 2da9e47dfa94a7445b7174bb54750ae4b2aff2eb593397509297c8b5621398e91cd1df19a220bc9d63c466a800f12783d2b25dc940144b60190eb30c78d6142e
7
+ data.tar.gz: 7bd56459bd4d20f89e22036c4f2b693d7b9fd1b58f533e649c24fa1603462a463afaab246284aae6ed68db07719e472c0741d7266ae56d61272f43184717706c
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /lambda/clairlune/node_modules/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in clairlune.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Moza USANE
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,66 @@
1
+ # Clairlune
2
+
3
+ Clairlune is a tool to package AWS Lambda function with npm modules for deployment.
4
+
5
+ ## Overview
6
+
7
+ AWS Lambda is very useful but we have to build native npm modules in Amazon Linux environment when we want to use these modules.
8
+
9
+ Clairlune consits of a Lambda function (Node.js) and Ruby code.
10
+ The function is invoked to build npm modules, and upload it to S3 bucket.
11
+
12
+ Ruby code provides the interface to use the Lambda function.
13
+ So the building process can be integrated to Ruby program, Rake task, Rails application etc.
14
+
15
+ ## Requirements
16
+
17
+ - AWS credential
18
+ - Ruby 2.3+
19
+
20
+ ## Installation
21
+
22
+ 1. Execute `$ npm install` in ./lambda/clairlune directory.
23
+ 2. Zip ./lambda/clairlune directory and upload it to Lambda.
24
+
25
+ TODO: Create installer
26
+
27
+ ## Usage
28
+
29
+ ### In Ruby code
30
+
31
+ ```ruby
32
+ require 'clairlune'
33
+
34
+ builder = Clairlune::Builder.new(
35
+ bucket: 'my-awesome-bucket',
36
+ key: 'npm_modules.zip',
37
+ package_json: '/path/to/package.json',
38
+ function_name: 'clairlune',
39
+ dest: '/path/to/node_modules.zip',
40
+ )
41
+ builder.performe
42
+ ```
43
+
44
+ ### As CLI tool
45
+
46
+ TODO: Not yet implemented CLI interface.
47
+
48
+ ## Progress report
49
+
50
+ - [x] Lambda function to build npm modules
51
+ - [x] Core toolkit with Ruby
52
+ - [ ] Installer
53
+ - [ ] CLI interface
54
+ - [ ] Documentation
55
+
56
+ ## License
57
+
58
+ MIT
59
+
60
+ ## Contributing
61
+
62
+ 1. Fork it ( https://github.com/mozamimy/clairlune/fork )
63
+ 2. Create your feature branch (`git checkout -b awesome-feature`)
64
+ 3. Commit your changes (`git commit -am 'Add an awesome feature'`)
65
+ 4. Push to the branch (`git push origin awesome-feature`)
66
+ 5. Create a new Pull Request
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "clairlune"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'clairlune/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "clairlune"
8
+ spec.version = Clairlune::VERSION
9
+ spec.authors = ["Moza USANE"]
10
+ spec.email = ["mozamimy@quellencode.org"]
11
+
12
+ spec.summary = %q{Clairlune is a tool to package AWS Lambda function with npm modules for deployment.}
13
+ spec.description = %q{Clairlune is a tool to package AWS Lambda function with npm modules for deployment.}
14
+ spec.homepage = "https://github.com/mozamimy/clairlune"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.12"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+
26
+ spec.add_dependency "aws-sdk", "~> 2.6.0"
27
+ end
@@ -0,0 +1,84 @@
1
+ const exec = require('child_process').exec;
2
+ const fs = require('fs');
3
+ const uuid = require('node-uuid');
4
+ const archiver = require('archiver');
5
+ const aws = require('aws-sdk');
6
+ const s3 = new aws.S3({ apiVersion: '2006-03-01' });
7
+
8
+ const TMP_DIR_PATH = `/tmp/${uuid.v4()}`;
9
+ const PACKAGE_JSON_PATH = `${TMP_DIR_PATH}/package.json`;
10
+ const MODULES_PATH = `${TMP_DIR_PATH}/node_modules`;
11
+ const DUMMY_HOME_PATH = `/tmp/${uuid.v4()}`;
12
+ const ZIP_FILE_PATH = `/tmp/${uuid.v4()}`;
13
+
14
+ function handler(event, context, callback) {
15
+ new Promise((resolve, reject) => {
16
+ if (!fs.existsSync(TMP_DIR_PATH)) {
17
+ fs.mkdirSync(TMP_DIR_PATH);
18
+ }
19
+ if (!fs.existsSync(DUMMY_HOME_PATH)) {
20
+ fs.mkdirSync(DUMMY_HOME_PATH);
21
+ }
22
+ fs.writeFileSync(PACKAGE_JSON_PATH, JSON.stringify(event, null, 2));
23
+
24
+ exec(`HOME=${DUMMY_HOME_PATH} npm install`, { cwd: TMP_DIR_PATH }, (err, stdout, stderr) => {
25
+ if (err) {
26
+ reject(err);
27
+ } else {
28
+ resolve({
29
+ bucket: event['clairlune']['bucket'],
30
+ key: event['clairlune']['key'],
31
+ });
32
+ }
33
+ });
34
+ }).then(data => {
35
+ return new Promise((resolve, reject) => {
36
+ const output = fs.createWriteStream(ZIP_FILE_PATH);
37
+ const archive = archiver('zip');
38
+
39
+ archive.pipe(output);
40
+ archive.bulk([
41
+ { expand: true, cwd: MODULES_PATH, src: ['**/*'], dest: '/', dot: true }
42
+ ]);
43
+
44
+ output.on('close', () => {
45
+ resolve(data);
46
+ });
47
+ archive.on('error', (err) => {
48
+ reject(err);
49
+ });
50
+
51
+ archive.finalize();
52
+ });
53
+ }).then((data) => {
54
+ const fileStream = fs.createReadStream(ZIP_FILE_PATH);
55
+
56
+ fileStream.on('error', (err) => {
57
+ context.fail(err);
58
+ });
59
+
60
+ fileStream.on('open', () => {
61
+ const s3Params = {
62
+ Bucket: data['bucket'],
63
+ Key: data['key'],
64
+ Body: fileStream,
65
+ };
66
+
67
+ s3.putObject(s3Params, (err, _) => {
68
+ if (err) {
69
+ context.fail(err);
70
+ } else {
71
+ context.succeed({
72
+ status: 'success',
73
+ bucket: data['bucket'],
74
+ key: data['key'],
75
+ });
76
+ }
77
+ });
78
+ });
79
+ }).catch(err => {
80
+ context.fail(err);
81
+ });
82
+ }
83
+
84
+ exports.handler = handler;
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "clairlune",
3
+ "version": "1.0.0",
4
+ "main": "index.js",
5
+ "dependencies": {
6
+ "aws-sdk": "^2.6.0",
7
+ "node-uuid": "^1.4.7",
8
+ "archiver": "^1.1.0"
9
+ },
10
+ "engines": {
11
+ "node": "4.3.2"
12
+ },
13
+ "scripts": {
14
+ "test": "echo \"Error: no test specified\" && exit 1"
15
+ },
16
+ "author": "mozamimy (Moza USANE) <mozamimy@quellencode.org>"
17
+ }
@@ -0,0 +1,65 @@
1
+ require 'json'
2
+ require 'logger'
3
+ require 'aws-sdk'
4
+
5
+ require 'clairlune/version'
6
+
7
+ module Clairlune
8
+ class Builder
9
+ def initialize(key:, bucket:, package_json:, function_name:, dest:, loglevel: 'info')
10
+ @key = key
11
+ @bucket = bucket
12
+ @function_name = function_name
13
+ @dest = dest
14
+ @package_json = File.read(package_json)
15
+ @logger = Logger.new(STDOUT)
16
+ @logger.level = loglevel
17
+ end
18
+
19
+ def performe(loglevel: 'info')
20
+ lambda_client = Aws::Lambda::Client.new
21
+
22
+ @logger.info("Invoke build function: #{@function_name}")
23
+
24
+ lambda_response = lambda_client.invoke(
25
+ function_name: @function_name,
26
+ payload: @package_json,
27
+ invocation_type: 'RequestResponse',
28
+ )
29
+
30
+ if lambda_response.data.status_code != 200
31
+ @logger.fatal('Returned status code is not 200 (Lambda).')
32
+
33
+ return {
34
+ success: false,
35
+ description: 'Returned status code is not 200 (Lambda).',
36
+ }
37
+ end
38
+
39
+ if JSON.load(lambda_response.data.payload)['status'] != 'success'
40
+ @logger.fatal('Build is failed.')
41
+ @logger.fatal(lambda_response.payload.read)
42
+
43
+ return {
44
+ success: false,
45
+ description: lambda_response.payload.read,
46
+ }
47
+ end
48
+
49
+ @logger.info("Download zipped npm module from: s3://#{@bucket}/#{@key}")
50
+
51
+ s3_client = Aws::S3::Client.new
52
+ File.open(@dest, 'w') do |file|
53
+ s3_client.get_object(bucket: @bucket, key: @key) do |chunk|
54
+ file.write(chunk)
55
+ end
56
+ end
57
+
58
+ {
59
+ success: true,
60
+ file_path: @dest,
61
+ description: 'success',
62
+ }
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,3 @@
1
+ module Clairlune
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: clairlune
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Moza USANE
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: aws-sdk
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.6.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 2.6.0
69
+ description: Clairlune is a tool to package AWS Lambda function with npm modules for
70
+ deployment.
71
+ email:
72
+ - mozamimy@quellencode.org
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - clairlune.gemspec
87
+ - lambda/clairlune/index.js
88
+ - lambda/clairlune/package.json
89
+ - lib/clairlune.rb
90
+ - lib/clairlune/version.rb
91
+ homepage: https://github.com/mozamimy/clairlune
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.5.1
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Clairlune is a tool to package AWS Lambda function with npm modules for deployment.
115
+ test_files: []