lam 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +8 -16
  3. data/Gemfile +3 -3
  4. data/README.md +17 -19
  5. data/Rakefile +0 -4
  6. data/bin/console +14 -0
  7. data/bin/setup +8 -0
  8. data/lam.gemspec +20 -18
  9. data/lib/lam.rb +1 -10
  10. data/lib/lam/version.rb +1 -1
  11. metadata +21 -189
  12. data/.rspec +0 -2
  13. data/.ruby-version +0 -1
  14. data/Gemfile.lock +0 -107
  15. data/Guardfile +0 -22
  16. data/LICENSE.txt +0 -22
  17. data/bin/lam +0 -14
  18. data/lib/lam/base_controller.rb +0 -54
  19. data/lib/lam/build.rb +0 -43
  20. data/lib/lam/build/handler_generator.rb +0 -34
  21. data/lib/lam/build/lambda_deducer.rb +0 -47
  22. data/lib/lam/build/templates/handler.js +0 -156
  23. data/lib/lam/build/traveling_ruby.rb +0 -108
  24. data/lib/lam/cli.rb +0 -23
  25. data/lib/lam/cli/help.rb +0 -19
  26. data/lib/lam/command.rb +0 -25
  27. data/lib/lam/process.rb +0 -18
  28. data/lib/lam/process/base_processor.rb +0 -23
  29. data/lib/lam/process/controller_processor.rb +0 -36
  30. data/lib/lam/process/help.rb +0 -11
  31. data/lib/lam/process/processor_deducer.rb +0 -52
  32. data/lib/lam/util.rb +0 -13
  33. data/notes/design.md +0 -43
  34. data/notes/traveling-ruby-packaging-lam.md +0 -26
  35. data/notes/traveling-ruby-packaging.md +0 -103
  36. data/notes/traveling-ruby.md +0 -82
  37. data/spec/fixtures/project/.gitignore +0 -3
  38. data/spec/fixtures/project/.ruby-version +0 -1
  39. data/spec/fixtures/project/Gemfile +0 -4
  40. data/spec/fixtures/project/Gemfile.lock +0 -35
  41. data/spec/fixtures/project/app/controllers/application_controller.rb +0 -2
  42. data/spec/fixtures/project/app/controllers/posts_controller.rb +0 -12
  43. data/spec/fixtures/project/bin/lam +0 -22
  44. data/spec/fixtures/project/handlers/controllers/posts.js +0 -156
  45. data/spec/lib/cli_spec.rb +0 -20
  46. data/spec/lib/lam/base_controller_spec.rb +0 -18
  47. data/spec/lib/lam/build/lambda_deducer_spec.rb +0 -20
  48. data/spec/lib/lam/build_spec.rb +0 -29
  49. data/spec/lib/lam/process/controller_processor_spec.rb +0 -22
  50. data/spec/lib/lam/process/infer_spec.rb +0 -24
  51. data/spec/lib/lam/process_spec.rb +0 -18
  52. data/spec/spec_helper.rb +0 -25
@@ -1,82 +0,0 @@
1
- ## Overview
2
-
3
- There are 2 things to point out:
4
-
5
- 1. What the user's environment contains - cannot control this, can only suggest
6
- 2. What the lambda environment contains - can fully control this
7
-
8
- * User can develop with ruby 2.4 and bundle gems and test and be happy. Hopefully they eventually learn to test with ruby 2.2.0 (when they run into bugs).
9
- * When `lam build` runs it will bundle it in ruby 2.2.0 though.
10
- * When lambda runs it will call `lam` with ruby 2.2.0. The gems will be installed in 2.2.0
11
- * Hope all works
12
-
13
- ## Download ruby commands
14
-
15
- ```sh
16
- # linux 64bit
17
- wget http://d6r77u77i8pq3.cloudfront.net/releases/traveling-ruby-20150715-2.2.2-linux-x86_64.tar.gz .
18
- mkdir ruby-linux
19
- tar -xvf traveling-ruby-20150715-2.2.2-linux-x86_64.tar.gz -C ruby-linux
20
- # mac
21
- wget http://d6r77u77i8pq3.cloudfront.net/releases/traveling-ruby-20150715-2.2.2-osx.tar.gz
22
- mkdir ruby-mac
23
- tar -xvf traveling-ruby-20150715-2.2.2-osx.tar.gz -C ruby-mac
24
-
25
- # another version
26
- mkdir hello-1.0.0-linux-x86_64/lib/ruby && tar -xzf packaging/traveling-ruby-20150715-2.2.2-linux-x86_64.tar.gz -C hello-1.0.0-linux-x86_64/lib/ruby
27
- ```
28
-
29
- ## Launch Instance for Testing
30
-
31
- ```sh
32
- aws ec2 run-instances --image-id ami-8c1be5f6 --count 1 --instance-type t2.micro --key-name default --security-groups demo
33
- ```
34
-
35
- ## Tree Structure
36
-
37
- ```sh
38
- lib/
39
- ├── app
40
- │   └── hello.rb
41
- ├── ruby
42
- │   ├── bin
43
- │   │   ├── gem
44
- │   │   ├── irb
45
- │   │   ├── rake
46
- │   │   ├── ruby
47
- │   │   └── ruby_environment
48
- │   ├── bin.real
49
- │   │   ├── bundle
50
- │   │   ├── bundler
51
- │   │   ├── gem
52
- │   │   ├── irb
53
- │   │   ├── rake
54
- │   │   └── ruby
55
- │   └── lib
56
- │   └── ruby
57
- │   ├── 2.2.0
58
- ...
59
- │   │   └── yaml.rb
60
- │   ├── gems
61
- │   │   └── 2.2.0
62
- │   │   ├── gems
63
- │   │   │   ├── attr_extras-5.2.0
64
- │   │   │   │   ├── attr_extras.gemspec
65
- ...
66
- │   │   │   ├── bundler-1.9.9
67
- ...
68
- │   │   └── rake-12.2.1.gemspec
69
- ...
70
- └── vendor
71
- ├── Gemfile
72
- ├── Gemfile.lock
73
- └── ruby
74
- └── 2.2.0 <= IMPORTANT: bundler copies gems to folder with running ruby version
75
- ├── gems
76
- │   ├── concurrent-ruby-1.0.5
77
- │   ├── faker-1.7.3
78
- │   └── i18n-0.9.0
79
- ...
80
- ```
81
-
82
- * [Full tree](https://gist.github.com/tongueroo/c42f9d35b15b06eb810802243f4e2f6d)
@@ -1,3 +0,0 @@
1
- vendor
2
- bundled
3
- lam.zip
@@ -1 +0,0 @@
1
- 2.2.2
@@ -1,4 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- gem "lam", path: "/Users/tung/src/tongueroo/lam"
4
- gem "print_ruby_info" # test
@@ -1,35 +0,0 @@
1
- PATH
2
- remote: /Users/tung/src/tongueroo/lam
3
- specs:
4
- lam (0.0.1)
5
- activesupport
6
- colorize
7
- hashie
8
- thor
9
-
10
- GEM
11
- remote: https://rubygems.org/
12
- specs:
13
- activesupport (5.1.4)
14
- concurrent-ruby (~> 1.0, >= 1.0.2)
15
- i18n (~> 0.7)
16
- minitest (~> 5.1)
17
- tzinfo (~> 1.1)
18
- colorize (0.8.1)
19
- concurrent-ruby (1.0.5)
20
- hashie (3.5.6)
21
- i18n (0.9.0)
22
- concurrent-ruby (~> 1.0)
23
- minitest (5.10.3)
24
- print_ruby_info (0.0.3)
25
- thor (0.20.0)
26
- thread_safe (0.3.6)
27
- tzinfo (1.2.3)
28
- thread_safe (~> 0.1)
29
-
30
- PLATFORMS
31
- ruby
32
-
33
- DEPENDENCIES
34
- lam!
35
- print_ruby_info
@@ -1,2 +0,0 @@
1
- class ApplicationController < Lam::BaseController
2
- end
@@ -1,12 +0,0 @@
1
- class PostsController < Lam::BaseController
2
- def create
3
- # render text: "test2" # more consistent for web controllers
4
-
5
- # render returns Lamba Proxy struture for web requests
6
- render json: event, status: 200
7
- end
8
-
9
- # def update
10
- # render json: event, status: 200
11
- # end
12
- end
@@ -1,22 +0,0 @@
1
- #!/bin/bash
2
-
3
- set -e
4
-
5
- # Figure out where this script is located.
6
- PROJECTDIR="`dirname \"$0\"`"
7
- PROJECTDIR="`cd \"$PROJECTDIR/..\" && pwd`"
8
-
9
- # Tell Bundler where the Gemfile and gems are.
10
- # IMPORTANT: the Gemfile must be in the same bundled/gems folder
11
- export BUNDLE_GEMFILE="$PROJECTDIR/Gemfile"
12
- unset BUNDLE_IGNORE_CONFIG
13
-
14
- # Run the actual app using the bundled Ruby interpreter, with Bundler activated.
15
- os=$(uname)
16
- if [[ "$os" == 'Darwin' ]]; then
17
- # used macosx ruby for development
18
- exec lam "$@"
19
- else
20
- # used bundled ruby for Lambda
21
- exec "$PROJECTDIR/bundled/ruby/bin/ruby" -rbundler/setup "$PROJECTDIR/bundled/gems/ruby/2.2.0/bin/lam" "$@"
22
- fi
@@ -1,156 +0,0 @@
1
- 'use strict';
2
-
3
- // handler: handlers/controllers/posts.create
4
- const spawn = require('child_process').spawn;
5
-
6
- // Once hooked up to API Gateway can use the curl command to test:
7
- // curl -s -X POST -d @event.json https://endpoint | jq .
8
-
9
- // Filters out lines so only the error lines remain.
10
- // Uses the "RubyError: " marker to find the starting error lines.
11
- //
12
- // Input: String
13
- // random line
14
- // RubyError: RuntimeError: error in submethod
15
- // line1
16
- // line2
17
- // line3
18
- //
19
- // Output: String
20
- // RubyError: RuntimeError: error in submethod
21
- // line1
22
- // line2
23
- // line3
24
- function filterErrorLines(text) {
25
- var lines = text.split("\n")
26
- var markerIndex = lines.findIndex(line => line.startsWith("RubyError: ") )
27
- lines = lines.filter((line, index) => index >= markerIndex )
28
- return lines.join("\n")
29
- }
30
-
31
- // Produces an Error object that displays in the AWS Lambda test console nicely.
32
- // The backtrace are the ruby lines, not the nodejs shim error lines.
33
- // The json payload in the Lambda console looks something like this:
34
- //
35
- // {
36
- // "errorMessage": "RubyError: RuntimeError: error in submethod",
37
- // "errorType": "RubyError",
38
- // "stackTrace": [
39
- // [
40
- // "line1",
41
- // "line2",
42
- // "line3"
43
- // ]
44
- // ]
45
- // }
46
- //
47
- // Input: String
48
- // RubyError: RuntimeError: error in submethod
49
- // line1
50
- // line2
51
- // line3
52
- //
53
- // Output: Error object
54
- // { RubyError: RuntimeError: error in submethod
55
- // line1
56
- // line2
57
- // line3 name: 'RubyError' }
58
- function customError(text) {
59
- text = filterErrorLines(text) // filter for error lines only
60
- var lines = text.split("\n")
61
- var message = lines[0]
62
- var error = new Error(message)
63
- error.name = message.split(':')[0]
64
- error.stack = lines.slice(0, lines.length-1) // drop final empty line
65
- .map(e => e.replace(/^\s+/g,'')) // trim leading whitespaces
66
- .join("\n")
67
- return error
68
- }
69
-
70
- module.exports.create = (event, context, callback) => {
71
- // To test on mac, set these environment variables:
72
- // export RUBY_BIN=$HOME/.rbenv/shims/ruby
73
- // export PROCESSOR_COMMAND="lam process controller"
74
-
75
- // Command: lam process controller [event] [context] [handler]
76
- const processor_command = process.env.PROCESSOR_COMMAND || "lam"
77
- var args = [
78
- "process",
79
- "controller",
80
- JSON.stringify(event),
81
- JSON.stringify(context),
82
- "handlers/controllers/posts.create"
83
- ]
84
- // console.log("processor_command %o", processor_command)
85
- // console.log("args %o", args)
86
-
87
- var ruby = spawn("bin/lam", args);
88
-
89
- // string concatation in javascript is faster than array concatation
90
- // http://bit.ly/2gBMDs6
91
- var stdout_buffer = ""; // stdout buffer
92
- // In the processor_command we do NOT call puts directly and write to stdout
93
- // because it will mess up the eventual response that we want API Gateway to
94
- // process.
95
- // The Lambda prints out function to whatever the return value the ruby method
96
- ruby.stdout.on('data', function(data) {
97
- // Not using console.log because it decorates output with a newline.
98
- //
99
- // Uncomment process.stdout.write to see stdout streamed for debugging.
100
- // process.stdout.write(data)
101
- stdout_buffer += data;
102
- });
103
-
104
- // react to potential errors
105
- var stderr_buffer = "";
106
- ruby.stderr.on('data', function(data) {
107
- // not using console.error because it decorates output with a newline
108
- stderr_buffer += data
109
- process.stderr.write(data)
110
- });
111
-
112
- //finalize when ruby process is done.
113
- ruby.on('close', function(exit_code) {
114
- // http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html#nodejs-prog-model-handler-callback
115
-
116
- // succcess
117
- if (exit_code == 0) {
118
- var result
119
- try {
120
- result = JSON.parse(stdout_buffer)
121
- } catch(e) {
122
- // if json cannot be parse assume simple text output intended
123
- process.stderr.write("WARN: error parsing json, assuming plain text is desired.")
124
- result = stdout_buffer
125
- }
126
- callback(null, result);
127
-
128
- // callback(null, stdout_buffer);
129
- } else {
130
-
131
- // TODO: if this works, allow a way to not decorate the error in case
132
- // it actually errors in javascript land
133
- // Customize error object with ruby error info
134
- var error = customError(stderr_buffer)
135
- callback(error);
136
- // console.log("error!")
137
- }
138
- });
139
- }
140
-
141
- // for local testing
142
- if (process.platform == "darwin") {
143
- // fake event and context
144
- var event = {"hello": "world"}
145
- // var event = {"body": {"hello": "world"}} // API Gateway wrapper structure
146
- var context = {"fake": "context"}
147
- module.exports.create(event, context, (error, message) => {
148
- console.error("\nLOCAL TESTING OUTPUT")
149
- if (error) {
150
- console.error("error message: %o", error)
151
- } else {
152
- console.error("success message %o", message)
153
- // console.log(JSON.stringify(message)) // stringify
154
- }
155
- })
156
- }
@@ -1,20 +0,0 @@
1
- require "spec_helper"
2
-
3
- # to run specs with what"s remembered from vcr
4
- # $ rake
5
- #
6
- # to run specs with new fresh data from aws api calls
7
- # $ rake clean:vcr ; time rake
8
- describe Lam::CLI do
9
- before(:all) do
10
- @args = "--noop"
11
- end
12
-
13
- describe "lam" do
14
- it "build" do
15
- out = execute("bin/lam build #{@args}")
16
- # puts out
17
- expect(out).to include("Building project")
18
- end
19
- end
20
- end
@@ -1,18 +0,0 @@
1
- require "spec_helper"
2
-
3
- # For testing lambda_function_names
4
- class FakeController < Lam::BaseController
5
- def handler1; end
6
- def handler2; end
7
- end
8
-
9
- describe Lam::BaseController do
10
- describe "lambda_functions" do
11
- it "should only list public user defined methods" do
12
- controller = FakeController.new(nil, nil)
13
- expect(controller.lambda_functions).to eq(
14
- [:handler1, :handler2]
15
- )
16
- end
17
- end
18
- end
@@ -1,20 +0,0 @@
1
- require_relative "../../../spec_helper"
2
-
3
- describe Lam::Build::LambdaDeducer do
4
- let(:deducer) do
5
- Lam::Build::LambdaDeducer.new("app/controllers/posts_controller.rb")
6
- end
7
-
8
- describe "LambdaDeducer" do
9
- it "deduces lambda js info" do
10
- deducer.run
11
- expect(deducer.handlers).to eq([
12
- {
13
- handler: "handlers/controllers/posts.create",
14
- js_path: "handlers/controllers/posts.js",
15
- js_method: "create"
16
- }
17
- ])
18
- end
19
- end
20
- end
@@ -1,29 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Lam::Build do
4
- before(:each) do
5
- FileUtils.rm_f("spec/fixtures/project/handlers/controllers/posts.js")
6
- end
7
- let(:build) do
8
- Lam::Build.new(noop: true)
9
- end
10
-
11
- describe "Build" do
12
- it "#handlers" do
13
- expect(build.handlers).to eq([
14
- {
15
- handler: "handlers/controllers/posts.create",
16
- js_path: "handlers/controllers/posts.js",
17
- js_method: "create"
18
- }
19
- ])
20
- end
21
-
22
- it "builds handlers javascript files" do
23
- build.build
24
- file_exist = File.exist?("#{Lam.root}handlers/controllers/posts.js")
25
- expect(file_exist).to be true
26
- end
27
- end
28
- end
29
-
@@ -1,22 +0,0 @@
1
- require_relative "../../../spec_helper"
2
-
3
- describe Lam::Process::ControllerProcessor do
4
- before(:all) do
5
- @event = { "we" => "love", "using" => "Lambda" }
6
- @context = {"test" => "1"}
7
- end
8
- let(:processor) do
9
- Lam::Process::ControllerProcessor.new(
10
- JSON.dump(@event),
11
- JSON.dump(@context),
12
- 'handlers/controllers/posts.create' # handler
13
- )
14
- end
15
-
16
- describe "ControllerProcessor" do
17
- it "find public_instance_methods" do
18
- processor.run
19
- expect(processor.event).to eq(@event)
20
- end
21
- end
22
- end