faastruby 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ca9d4cec0bdbdaefb83ad63a578a211a5fb186f118f2e9c7f4bb8f81f4b2ac08
4
- data.tar.gz: 5857b9257bafe182fb38f89a6a1290d3a7fc148e75bc405e48a9cd51e6acbb80
3
+ metadata.gz: 2d752da5ce5767b5c7b97f6e53e884ab2ba0123f36f713f6a70ba82fc8055acf
4
+ data.tar.gz: d9c3038e42ca26f6b3de8953c06c7724a046549ae49942040ab8d9c3344536d2
5
5
  SHA512:
6
- metadata.gz: f6fdbc0c311dc686ef5f1aa2f8310d4cef9fdf7c77d304bb42edc8069da4603b30d033df29d2b4743e5891f1ef90a34460ffb49cebcf927bbbad02c6860b8eff
7
- data.tar.gz: 8aeb14ace46c50277eff7a9a9f29d7dda88f302674a96a5d5ece6930072698a3eff8a3816984f91944d39f359594b643caf0c1605e8ec8e2f1c6c02e515d4062
6
+ metadata.gz: 60085ed613203a88e39cbcc17aba0de3ed3884d6794255385e887c042097a871af5cb00069f422bcf96d855a969ec0e9253c98baf37c2819cefaf32737b8c6a0
7
+ data.tar.gz: 2e50627ab43fd3fa0a6eabd702211d9df7759d810fa1691ef57177d390f4fa74730412b2c0c2c40a4429f05b83977520213a9fcac579766bf4c44bb459c61562
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.3.2 - Dec 15 2018
4
+ - Better output for tests
5
+ - `bundle check && bundle install` runs before building a package to make sure Gemfile.lock is updated.
6
+ - New command: `faastruby deploy`. This command is meant to be run in a FaaStRuby Project folder, and will deploy all workspaces and their functions.
7
+ - Updated spec templates with new helper that stubs calls to FaaStRuby server when using `faastruby-rpc`.
8
+ - `render` is now the preferred method to set the return value of functions.
9
+ - Upgraded dependency `faastruby-rpc` to version 0.1.3.
10
+ - Removed region SFO2 (sorry, but the usage was minimal.)
11
+
3
12
  ## 0.3.1 - Dec 9 2018
4
13
  - Add faastruby-rpc to Gemfile template and runtime dependencies
5
14
 
data/Gemfile.lock CHANGED
@@ -1,9 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- faastruby (0.3.0)
4
+ faastruby (0.3.2)
5
5
  colorize (~> 0.8)
6
- faastruby-rpc (~> 0.1)
6
+ faastruby-rpc (~> 0.1.3)
7
7
  oj (~> 3.6)
8
8
  puma (~> 3.12)
9
9
  rest-client (~> 2.0)
@@ -32,12 +32,12 @@ GEM
32
32
  domain_name (0.5.20180417)
33
33
  unf (>= 0.0.5, < 1.0.0)
34
34
  equatable (0.5.0)
35
- faastruby-rpc (0.1.1)
35
+ faastruby-rpc (0.1.3)
36
36
  oj (~> 3.6)
37
37
  hashdiff (0.3.7)
38
38
  http-cookie (1.0.3)
39
39
  domain_name (~> 0.5)
40
- i18n (1.1.1)
40
+ i18n (1.2.0)
41
41
  concurrent-ruby (~> 1.0)
42
42
  mime-types (3.2.2)
43
43
  mime-types-data (~> 3.2015)
@@ -1,4 +1,18 @@
1
1
  def handler event
2
- # Do the magic here
3
- # Use respond_with RESPONSE, where RESPONSE is a Hash, String or Array.
2
+ # FUNCTION RESPONSE
3
+ #
4
+ # You can render text, json, yaml, html or js. Example:
5
+ # render html: '<p>Hello World!</p>'
6
+ # render yaml: {hello: 'world!'}
7
+ #
8
+ # Status:
9
+ # The default status is 200. You can set a custom status like this:
10
+ # render json: {error: 'Could not perform the action'}, status: 422
11
+ #
12
+ # Headers:
13
+ # The 'Content-Type' header is automatically set when you use 'render'.
14
+ # You can set custom headers using a hash with string keys. Example:
15
+ # render text: 'It Works!', headers: {'TransactionId' => 23928}
16
+
17
+ # Write code here
4
18
  end
@@ -5,7 +5,7 @@ describe 'handler(event)' do
5
5
  let(:event) {SpecHelper::Event.new}
6
6
 
7
7
  it 'should return Hash, String or Array' do
8
- body = handler(event).body
9
- expect([String, Hash, Array].include? body.class).to be true
8
+ body = handler(event).call.body
9
+ expect([String, Hash, Array, ].include? body.class).to be true
10
10
  end
11
11
  end
@@ -1,8 +1,12 @@
1
+ require 'yaml'
2
+ require 'oj'
3
+
1
4
  module FaaStRuby
2
5
  def self.included(base)
3
6
  base.extend(SpecHelper)
4
7
  $LOAD_PATH << Dir.pwd
5
8
  end
9
+ class DoubleRenderError < StandardError; end
6
10
  module SpecHelper
7
11
  class Event
8
12
  @@event = Struct.new(:body, :query_params, :headers, :context)
@@ -11,13 +15,52 @@ module FaaStRuby
11
15
  end
12
16
  end
13
17
  class Response
14
- @@response = Struct.new(:body, :status, :headers)
15
- def self.new(body, status, headers)
16
- @@response.new(body, status, headers)
18
+ # @@response = Struct.new(:body, :status, :headers)
19
+ attr_accessor :body, :status, :headers
20
+ @@rendered = false
21
+ def initialize(body, status, headers)
22
+ if @@rendered
23
+ raise FaaStRuby::DoubleRenderError.new("You called 'render' or 'respond_with' multiple times in your handler method.")
24
+ end
25
+ @@rendered = true
26
+ @body = body
27
+ @status = status
28
+ @headers = headers
29
+ end
30
+
31
+ def call
32
+ @@rendered = false
33
+ self
17
34
  end
18
35
  end
19
36
  end
37
+
20
38
  def respond_with(body, status: 200, headers: {})
21
39
  SpecHelper::Response.new(body, status, headers)
22
40
  end
41
+
42
+ def render(js: nil, body: nil, inline: nil, html: nil, json: nil, yaml: nil, text: nil, status: 200, headers: {}, content_type: nil)
43
+ headers["Content-Type"] = content_type if content_type
44
+ case
45
+ when json
46
+ headers["Content-Type"] ||= "application/json"
47
+ resp_body = json.is_a?(String) ? json : Oj.dump(json)
48
+ when html, inline
49
+ headers["Content-Type"] ||= "text/html"
50
+ resp_body = html
51
+ when text
52
+ headers["Content-Type"] ||= "text/plain"
53
+ resp_body = text
54
+ when yaml
55
+ headers["Content-Type"] ||= "application/yaml"
56
+ resp_body = yaml.to_yaml
57
+ when body
58
+ headers["Content-Type"] ||= "application/octet-stream"
59
+ resp_body = raw
60
+ when js
61
+ headers["Content-Type"] ||= "text/javascript"
62
+ resp_body = js
63
+ end
64
+ respond_with(resp_body, status: status, headers: headers)
65
+ end
23
66
  end
@@ -1,3 +1,3 @@
1
- require 'faastruby-rpc'
1
+ require 'faastruby-rpc/test_helper'
2
2
  require 'helpers/faastruby'
3
3
  include FaaStRuby
data/example/handler.rb CHANGED
@@ -4,14 +4,22 @@ require 'json'
4
4
  # 1) If you don't have a workspace, create one by running:
5
5
  # faastruby create-workspace WORKSPACE_NAME
6
6
  # 2) To deploy this function, cd into its folder and run:
7
- # faastruby deploy WORKSPACE_NAME
7
+ # faastruby deploy-to WORKSPACE_NAME
8
8
  def handler event
9
- headers = {
10
- 'Content-Type' => 'text/plain',
11
- 'My-Custom-Header' => 'Value'
12
- }
13
9
  data = event.body ? JSON.parse(event.body) : {}
14
- # The response must be a Hash, Array or String.
15
- response = "Hello, #{data['name'] || 'World'}!"
16
- respond_with response, status: 200, headers: headers
10
+ # FUNCTION RESPONSE
11
+ #
12
+ # You can render text, json, yaml, html or js. Example:
13
+ # render html: '<p>Hello World!</p>'
14
+ # render yaml: {hello: 'world!'}
15
+ #
16
+ # Status:
17
+ # The default status is 200. You can set a custom status like this:
18
+ # render json: {error: 'Could not perform the action'}, status: 422
19
+ #
20
+ # Headers:
21
+ # The 'Content-Type' header is automatically set when you use 'render'.
22
+ # You can set custom headers using a hash with string keys. Example:
23
+ # render text: 'It Works!', headers: {'TransactionId' => 23928}
24
+ render text: "Hello, #{data['name'] || 'World'}!"
17
25
  end
@@ -5,16 +5,16 @@ describe 'handler(event)' do
5
5
  let(:event) {SpecHelper::Event.new(body: '{"name": "Ruby"}')}
6
6
 
7
7
  it 'should return Hash, String or Array' do
8
- body = handler(event).body
8
+ body = handler(event).call.body
9
9
  expect([String, Hash, Array].include? body.class).to be true
10
10
  end
11
11
  it 'should add the name to the response string' do
12
- body = handler(event).body
12
+ body = handler(event).call.body
13
13
  expect(body).to be == 'Hello, Ruby!'
14
14
  end
15
15
  it 'should say Hello, World! when name is not present' do
16
16
  event = SpecHelper::Event.new(body: nil)
17
- body = handler(event).body
17
+ body = handler(event).call.body
18
18
  expect(body).to be == 'Hello, World!'
19
19
  end
20
20
  end
@@ -1,8 +1,12 @@
1
+ require 'yaml'
2
+ require 'oj'
3
+
1
4
  module FaaStRuby
2
5
  def self.included(base)
3
6
  base.extend(SpecHelper)
4
7
  $LOAD_PATH << Dir.pwd
5
8
  end
9
+ class DoubleRenderError < StandardError; end
6
10
  module SpecHelper
7
11
  class Event
8
12
  @@event = Struct.new(:body, :query_params, :headers, :context)
@@ -11,13 +15,52 @@ module FaaStRuby
11
15
  end
12
16
  end
13
17
  class Response
14
- @@response = Struct.new(:body, :status, :headers)
15
- def self.new(body, status, headers)
16
- @@response.new(body, status, headers)
18
+ # @@response = Struct.new(:body, :status, :headers)
19
+ attr_accessor :body, :status, :headers
20
+ @@rendered = false
21
+ def initialize(body, status, headers)
22
+ if @@rendered
23
+ raise FaaStRuby::DoubleRenderError.new("You called 'render' or 'respond_with' multiple times in your handler method.")
24
+ end
25
+ @@rendered = true
26
+ @body = body
27
+ @status = status
28
+ @headers = headers
29
+ end
30
+
31
+ def call
32
+ @@rendered = false
33
+ self
17
34
  end
18
35
  end
19
36
  end
37
+
20
38
  def respond_with(body, status: 200, headers: {})
21
39
  SpecHelper::Response.new(body, status, headers)
22
40
  end
41
+
42
+ def render(js: nil, body: nil, inline: nil, html: nil, json: nil, yaml: nil, text: nil, status: 200, headers: {}, content_type: nil)
43
+ headers["Content-Type"] = content_type if content_type
44
+ case
45
+ when json
46
+ headers["Content-Type"] ||= "application/json"
47
+ resp_body = json.is_a?(String) ? json : Oj.dump(json)
48
+ when html, inline
49
+ headers["Content-Type"] ||= "text/html"
50
+ resp_body = html
51
+ when text
52
+ headers["Content-Type"] ||= "text/plain"
53
+ resp_body = text
54
+ when yaml
55
+ headers["Content-Type"] ||= "application/yaml"
56
+ resp_body = yaml.to_yaml
57
+ when body
58
+ headers["Content-Type"] ||= "application/octet-stream"
59
+ resp_body = raw
60
+ when js
61
+ headers["Content-Type"] ||= "text/javascript"
62
+ resp_body = js
63
+ end
64
+ respond_with(resp_body, status: status, headers: headers)
65
+ end
23
66
  end
@@ -1,3 +1,3 @@
1
- require 'faastruby-rpc'
1
+ require 'faastruby-rpc/test_helper'
2
2
  require 'helpers/faastruby'
3
3
  include FaaStRuby
data/faastruby.gemspec CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.add_runtime_dependency 'sinatra', '~> 2.0'
21
21
  spec.add_runtime_dependency 'sinatra-contrib', '~> 2.0'
22
22
  spec.add_runtime_dependency 'puma', '~> 3.12'
23
- spec.add_runtime_dependency 'faastruby-rpc', '~> 0.1'
23
+ spec.add_runtime_dependency 'faastruby-rpc', '~> 0.1.3'
24
24
 
25
25
  # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
26
26
  # to allow pushing to a single host or delete this section to allow pushing to any host.
@@ -2,8 +2,7 @@ module FaaStRuby
2
2
  ROOT_DOMAIN = ENV['FAASTRUBY_ROOT_DOMAIN'] || 'faastruby.io'
3
3
  DEFAULT_REGION = 'tor1'
4
4
  REGIONS = [
5
- 'tor1',
6
- 'sfo2'
5
+ 'tor1'
7
6
  ]
8
7
  class << self
9
8
  attr_accessor :configuration
@@ -43,7 +43,8 @@ module FaaStRuby
43
43
  module Command
44
44
  COMMANDS = {
45
45
  'new' => FaaStRuby::Command::Function::New,
46
- 'deploy-to' => FaaStRuby::Command::Function::Deploy,
46
+ 'deploy-to' => FaaStRuby::Command::Function::DeployTo,
47
+ 'deploy' => FaaStRuby::Command::Workspace::Deploy,
47
48
  'remove-from' => FaaStRuby::Command::Function::RemoveFrom,
48
49
  'update-context' => FaaStRuby::Command::Function::UpdateContext,
49
50
  'upgrade' => FaaStRuby::Command::Function::Upgrade,
@@ -22,7 +22,7 @@ module FaaStRuby
22
22
  end
23
23
 
24
24
  require 'faastruby/cli/commands/function/build'
25
- require 'faastruby/cli/commands/function/deploy'
25
+ require 'faastruby/cli/commands/function/deploy_to'
26
26
  require 'faastruby/cli/commands/function/new'
27
27
  require 'faastruby/cli/commands/function/remove_from'
28
28
  require 'faastruby/cli/commands/function/test'
@@ -21,6 +21,7 @@ module FaaStRuby
21
21
  end
22
22
 
23
23
  def run
24
+ FaaStRuby::CLI.error('Please fix the problems above and try again') unless bundle_install
24
25
  tests_passed = run_tests
25
26
  FaaStRuby::CLI.error("Build aborted because tests failed and you have 'abort_build_when_tests_fail: true' in 'faastruby.yml'") unless tests_passed || !@abort_when_tests_fail
26
27
  puts "Warning: Ignoring failed tests because you have 'abort_build_when_tests_fail: false' in 'faastruby.yml'".yellow if !tests_passed && !@abort_when_tests_fail
@@ -41,6 +42,12 @@ module FaaStRuby
41
42
  self.class.build(source, output_file)
42
43
  end
43
44
 
45
+ def bundle_install
46
+ puts '[build] Verifying dependencies'
47
+ return true unless File.file?('Gemfile')
48
+ system('bundle check') || system('bundle install')
49
+ end
50
+
44
51
  def run_tests
45
52
  FaaStRuby::Command::Function::Test.new(true).run(do_not_exit: true)
46
53
  end
@@ -1,7 +1,7 @@
1
1
  module FaaStRuby
2
2
  module Command
3
3
  module Function
4
- class Deploy < FunctionBaseCommand
4
+ class DeployTo < FunctionBaseCommand
5
5
  def initialize(args)
6
6
  @args = args
7
7
  @missing_args = []
@@ -14,17 +14,19 @@ module FaaStRuby
14
14
  end
15
15
 
16
16
  def run
17
+ FaaStRuby::CLI.error('Please fix the problems above and try again') unless bundle_install
17
18
  tests_passed = run_tests
18
19
  FaaStRuby::CLI.error("Deploy aborted because tests failed and you have 'abort_deploy_when_tests_fail: true' in 'faastruby.yml'") unless tests_passed || !@abort_when_tests_fail
19
20
  puts "Warning: Ignoring failed tests because you have 'abort_deploy_when_tests_fail: false' in 'faastruby.yml'".yellow if !tests_passed && !@abort_when_tests_fail
20
21
  package_file_name = build_package
21
- spinner = spin("Deploying to workspace '#{@workspace_name}'...")
22
+ spinner = spin("Deploying '#{@workspace_name}/#{@function_name}'")
22
23
  workspace = FaaStRuby::Workspace.new(name: @workspace_name).deploy(package_file_name)
23
24
  if workspace.errors.any?
24
25
  spinner.stop('Failed :(')
25
26
  FaaStRuby::CLI.error(workspace.errors)
26
27
  end
27
28
  spinner.stop('Done!')
29
+ exit 0
28
30
  end
29
31
 
30
32
  def self.help
@@ -37,6 +39,12 @@ module FaaStRuby
37
39
 
38
40
  private
39
41
 
42
+ def bundle_install
43
+ puts '[build] Verifying dependencies'
44
+ return true unless File.file?('Gemfile')
45
+ system('bundle check') || system('bundle install')
46
+ end
47
+
40
48
  def missing_args
41
49
  if @args.empty?
42
50
  @missing_args << "Missing argument: WORKSPACE_NAME".red
@@ -15,18 +15,8 @@ module FaaStRuby
15
15
  puts "[skipped] You have no 'test_command' key/value in 'faastruby.yml'. Please consider using rspec!".yellow
16
16
  return true
17
17
  end
18
- spinner = spin("Running tests...")
19
- output, status = Open3.capture2e(@test_command)
20
- if status == 0
21
- spinner.stop('Passed!')
22
- puts output
23
- return true
24
- else
25
- spinner.stop('Failed :(')
26
- FaaStRuby::CLI.error(output, color: nil) unless do_not_exit
27
- puts output if do_not_exit
28
- return false
29
- end
18
+ puts "[test] Running tests"
19
+ system(@test_command)
30
20
  end
31
21
 
32
22
  def self.help
@@ -9,4 +9,5 @@ end
9
9
 
10
10
  require 'faastruby/cli/commands/workspace/create'
11
11
  require 'faastruby/cli/commands/workspace/destroy'
12
- require 'faastruby/cli/commands/workspace/list'
12
+ require 'faastruby/cli/commands/workspace/list'
13
+ require 'faastruby/cli/commands/workspace/deploy'
@@ -13,7 +13,6 @@ module FaaStRuby
13
13
  end
14
14
 
15
15
  def run
16
- dir_exists? || FileUtils.mkdir_p(@base_dir)
17
16
  spinner = spin("Requesting credentials...")
18
17
  workspace = FaaStRuby::Workspace.create(name: @workspace_name, email: @options['email'])
19
18
  spinner.stop("Done!")
@@ -27,6 +26,7 @@ module FaaStRuby
27
26
  FaaStRuby::Credentials.add(@workspace_name, workspace.credentials, @options['credentials_file'])
28
27
  end
29
28
  puts "Workspace '#{@workspace_name}' created"
29
+ create_dir unless dir_exists?
30
30
  end
31
31
 
32
32
  def self.help
@@ -39,6 +39,11 @@ module FaaStRuby
39
39
 
40
40
  private
41
41
 
42
+ def create_dir
43
+ FileUtils.mkdir_p(@base_dir)
44
+ puts "+ d #{@base_dir}".green
45
+ end
46
+
42
47
  def dir_exists?
43
48
  return false unless File.directory?(@base_dir)
44
49
  puts "Local folder '#{@workspace_name}' already exists."
@@ -0,0 +1,50 @@
1
+ module FaaStRuby
2
+ module Command
3
+ module Workspace
4
+ class Deploy < WorkspaceBaseCommand
5
+ def initialize(args)
6
+ @errors = []
7
+ if args.any?
8
+ @args = args
9
+ else
10
+ @args = Dir.glob('*').select{|f| File.directory?(f)}
11
+ end
12
+ end
13
+
14
+ def run
15
+ result = []
16
+ errors = false
17
+ @args.each do |workspace|
18
+ Dir.chdir workspace
19
+ functions = Dir.glob('*').select{|f| File.directory?(f)}
20
+ functions.each do |function|
21
+ puts "[deploy] Entering folder #{workspace}/#{function}"
22
+ Dir.chdir function
23
+ if system("faastruby deploy-to #{workspace}")
24
+ result << "* #{workspace}/#{function} [Deploy OK]".green
25
+ else
26
+ result << "* #{workspace}/#{function} [Deploy FAILED]".red
27
+ errors = true
28
+ end
29
+ Dir.chdir '..'
30
+ end
31
+ Dir.chdir '..'
32
+ end
33
+ puts "\nResult:"
34
+ FaaStRuby::CLI.error(result, color: nil) if errors
35
+ puts result
36
+ exit 0
37
+ end
38
+
39
+ def self.help
40
+ "deploy".blue + " [WORKSPACE_FOLDER1] [WORKSPACE_FOLDER2]... # Deploy all workspaces in the current directory and their functions"
41
+ end
42
+
43
+ def usage
44
+ "Usage: faastruby #{self.class.help}"
45
+ end
46
+
47
+ end
48
+ end
49
+ end
50
+ end
@@ -1,3 +1,3 @@
1
1
  module FaaStRuby
2
- VERSION = '0.3.1'
2
+ VERSION = '0.3.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faastruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paulo Arruda
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-10 00:00:00.000000000 Z
11
+ date: 2018-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -142,14 +142,14 @@ dependencies:
142
142
  requirements:
143
143
  - - "~>"
144
144
  - !ruby/object:Gem::Version
145
- version: '0.1'
145
+ version: 0.1.3
146
146
  type: :runtime
147
147
  prerelease: false
148
148
  version_requirements: !ruby/object:Gem::Requirement
149
149
  requirements:
150
150
  - - "~>"
151
151
  - !ruby/object:Gem::Version
152
- version: '0.1'
152
+ version: 0.1.3
153
153
  - !ruby/object:Gem::Dependency
154
154
  name: bundler
155
155
  requirement: !ruby/object:Gem::Requirement
@@ -249,7 +249,7 @@ files:
249
249
  - lib/faastruby/cli/commands/credentials/list.rb
250
250
  - lib/faastruby/cli/commands/function.rb
251
251
  - lib/faastruby/cli/commands/function/build.rb
252
- - lib/faastruby/cli/commands/function/deploy.rb
252
+ - lib/faastruby/cli/commands/function/deploy_to.rb
253
253
  - lib/faastruby/cli/commands/function/new.rb
254
254
  - lib/faastruby/cli/commands/function/remove_from.rb
255
255
  - lib/faastruby/cli/commands/function/run.rb
@@ -260,6 +260,7 @@ files:
260
260
  - lib/faastruby/cli/commands/version.rb
261
261
  - lib/faastruby/cli/commands/workspace.rb
262
262
  - lib/faastruby/cli/commands/workspace/create.rb
263
+ - lib/faastruby/cli/commands/workspace/deploy.rb
263
264
  - lib/faastruby/cli/commands/workspace/destroy.rb
264
265
  - lib/faastruby/cli/commands/workspace/list.rb
265
266
  - lib/faastruby/cli/credentials.rb