faastruby 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/CHANGELOG.md +11 -0
  4. data/Gemfile +6 -0
  5. data/Gemfile.lock +67 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +39 -0
  8. data/Rakefile +2 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +8 -0
  11. data/example-blank/Gemfile +5 -0
  12. data/example-blank/handler.rb +4 -0
  13. data/example-blank/spec/handler_spec.rb +11 -0
  14. data/example-blank/spec/helpers/faastruby.rb +23 -0
  15. data/example-blank/spec/spec_helper.rb +2 -0
  16. data/example/Gemfile +5 -0
  17. data/example/handler.rb +17 -0
  18. data/example/spec/handler_spec.rb +20 -0
  19. data/example/spec/helpers/faastruby.rb +23 -0
  20. data/example/spec/spec_helper.rb +2 -0
  21. data/exe/faastruby +5 -0
  22. data/faastruby.gemspec +41 -0
  23. data/lib/faastruby.rb +6 -0
  24. data/lib/faastruby/api.rb +94 -0
  25. data/lib/faastruby/base.rb +53 -0
  26. data/lib/faastruby/cli.rb +42 -0
  27. data/lib/faastruby/cli/commands.rb +61 -0
  28. data/lib/faastruby/cli/commands/function.rb +31 -0
  29. data/lib/faastruby/cli/commands/function/build.rb +65 -0
  30. data/lib/faastruby/cli/commands/function/deploy.rb +62 -0
  31. data/lib/faastruby/cli/commands/function/new.rb +99 -0
  32. data/lib/faastruby/cli/commands/function/remove_from.rb +71 -0
  33. data/lib/faastruby/cli/commands/function/run.rb +135 -0
  34. data/lib/faastruby/cli/commands/function/test.rb +42 -0
  35. data/lib/faastruby/cli/commands/function/update_context.rb +67 -0
  36. data/lib/faastruby/cli/commands/function/upgrade.rb +69 -0
  37. data/lib/faastruby/cli/commands/help.rb +31 -0
  38. data/lib/faastruby/cli/commands/version.rb +13 -0
  39. data/lib/faastruby/cli/commands/workspace.rb +12 -0
  40. data/lib/faastruby/cli/commands/workspace/create.rb +70 -0
  41. data/lib/faastruby/cli/commands/workspace/destroy.rb +66 -0
  42. data/lib/faastruby/cli/commands/workspace/list.rb +54 -0
  43. data/lib/faastruby/cli/credentials.rb +49 -0
  44. data/lib/faastruby/cli/package.rb +46 -0
  45. data/lib/faastruby/function.rb +27 -0
  46. data/lib/faastruby/version.rb +3 -0
  47. data/lib/faastruby/workspace.rb +49 -0
  48. metadata +203 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1a2b492f1a720606438bf41373fc04ef892932224b8272cc1ef9ee48a793d31e
4
+ data.tar.gz: c9489fbc690bd3ab685067852c7dda8115c0a7c6f075b145ebeb92558f83c8ab
5
+ SHA512:
6
+ metadata.gz: 2ddf53f3d75310e788d2671a6939d2706f7658bb946b49dfc5828c52816d8a091c06cf815e2a93e31294c09ae93d475ea82525c825f6bd44f3466d36e7387b5a
7
+ data.tar.gz: 325b6fc49cb1168ac0522abdfaebad1bd06466d511e5271bcb9c0bdb03e5dda0dcac93a8abb990035b768997db52e1a864495234d61e98afa4fac84e050bec13
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /*.gem
@@ -0,0 +1,11 @@
1
+ # Changelog
2
+
3
+ ## 0.2.0 - Oct 12 2018
4
+ - Full rewrite of the gem. All command line parameters have changed. Please see the documentation.
5
+ - No more git dependence.
6
+ - Functions are packaged and deployed individually, and also removed individually.
7
+ - Functions have a YAML configuration file.
8
+ - Functions can have tests and they can be configured to run before each deploy, aborting it if a failure happens.
9
+ - Added option to print or save the workspace credentials into a different file upon creation, instead of saving it to ~/.faastruby
10
+ - Read a different credentials file via FAASTRUBY_CREDENTIALS environment variable.
11
+ - Read credentials via environment variables: FAASTRUBY_API_KEY and FAASTRUBY_API_SECRET.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in faastruby.gemspec
6
+ gemspec
@@ -0,0 +1,67 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ faastruby (0.2.0)
5
+ colorize
6
+ oj
7
+ rest-client
8
+ rubyzip
9
+ tty-spinner
10
+ tty-table
11
+
12
+ GEM
13
+ remote: https://rubygems.org/
14
+ specs:
15
+ colorize (0.8.1)
16
+ domain_name (0.5.20180417)
17
+ unf (>= 0.0.5, < 1.0.0)
18
+ equatable (0.5.0)
19
+ http-cookie (1.0.3)
20
+ domain_name (~> 0.5)
21
+ mime-types (3.2.2)
22
+ mime-types-data (~> 3.2015)
23
+ mime-types-data (3.2018.0812)
24
+ necromancer (0.4.0)
25
+ netrc (0.11.0)
26
+ oj (3.6.11)
27
+ pastel (0.7.2)
28
+ equatable (~> 0.5.0)
29
+ tty-color (~> 0.4.0)
30
+ rake (10.5.0)
31
+ rest-client (2.0.2)
32
+ http-cookie (>= 1.0.2, < 2.0)
33
+ mime-types (>= 1.16, < 4.0)
34
+ netrc (~> 0.8)
35
+ rubyzip (1.2.2)
36
+ strings (0.1.4)
37
+ strings-ansi (~> 0.1.0)
38
+ unicode-display_width (~> 1.4.0)
39
+ unicode_utils (~> 1.4.0)
40
+ strings-ansi (0.1.0)
41
+ tty-color (0.4.3)
42
+ tty-cursor (0.6.0)
43
+ tty-screen (0.6.5)
44
+ tty-spinner (0.8.0)
45
+ tty-cursor (>= 0.5.0)
46
+ tty-table (0.10.0)
47
+ equatable (~> 0.5.0)
48
+ necromancer (~> 0.4.0)
49
+ pastel (~> 0.7.2)
50
+ strings (~> 0.1.0)
51
+ tty-screen (~> 0.6.4)
52
+ unf (0.1.4)
53
+ unf_ext
54
+ unf_ext (0.0.7.5)
55
+ unicode-display_width (1.4.0)
56
+ unicode_utils (1.4.0)
57
+
58
+ PLATFORMS
59
+ ruby
60
+
61
+ DEPENDENCIES
62
+ bundler (~> 1.16)
63
+ faastruby!
64
+ rake (~> 10.0)
65
+
66
+ BUNDLED WITH
67
+ 1.16.5
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Paulo Arruda
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,39 @@
1
+ # FaaSt Ruby
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rubyfaas`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'faastruby'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install faastruby
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/faastruby.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rubyfaas"
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(__FILE__)
@@ -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,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :test do
4
+ gem 'rspec'
5
+ end
@@ -0,0 +1,4 @@
1
+ def handler event
2
+ # Do the magic here
3
+ # Use respond_with RESPONSE, where RESPONSE is a Hash, String or Array.
4
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+ require 'handler'
3
+
4
+ describe 'handler(event)' do
5
+ let(:event) {SpecHelper::Event.new}
6
+
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
10
+ end
11
+ end
@@ -0,0 +1,23 @@
1
+ module FaaStRuby
2
+ def self.included(base)
3
+ base.extend(SpecHelper)
4
+ $LOAD_PATH << Dir.pwd
5
+ end
6
+ module SpecHelper
7
+ class Event
8
+ @@event = Struct.new(:body, :query_params, :headers, :context)
9
+ def self.new(body: 'example body', query_params: {'foo' => 'bar'}, headers: {'Foo' => 'Bar'}, context: '{"foo": "bar"}')
10
+ @@event.new(body, query_params, headers, context)
11
+ end
12
+ end
13
+ class Response
14
+ @@response = Struct.new(:body, :status, :headers)
15
+ def self.new(body, status, headers)
16
+ @@response.new(body, status, headers)
17
+ end
18
+ end
19
+ end
20
+ def respond_with(body, status: 200, headers: {})
21
+ SpecHelper::Response.new(body, status, headers)
22
+ end
23
+ end
@@ -0,0 +1,2 @@
1
+ require 'helpers/faastruby'
2
+ include FaaStRuby
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :test do
4
+ gem 'rspec'
5
+ end
@@ -0,0 +1,17 @@
1
+ # require 'cool-gem'
2
+ require 'json'
3
+
4
+ # 1) If you don't have a workspace, create one by running:
5
+ # faastruby create-workspace WORKSPACE_NAME
6
+ # 2) To deploy this function, cd into its folder and run:
7
+ # faastruby deploy WORKSPACE_NAME
8
+ def handler event
9
+ headers = {
10
+ 'Content-Type' => 'text/plain',
11
+ 'My-Custom-Header' => 'Value'
12
+ }
13
+ 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
17
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+ require 'handler'
3
+
4
+ describe 'handler(event)' do
5
+ let(:event) {SpecHelper::Event.new(body: '{"name": "Ruby"}')}
6
+
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
10
+ end
11
+ it 'should add the name to the response string' do
12
+ body = handler(event).body
13
+ expect(body).to be == 'Hello, Ruby!'
14
+ end
15
+ it 'should say Hello, World! when name is not present' do
16
+ event = SpecHelper::Event.new(body: nil)
17
+ body = handler(event).body
18
+ expect(body).to be == 'Hello, World!'
19
+ end
20
+ end
@@ -0,0 +1,23 @@
1
+ module FaaStRuby
2
+ def self.included(base)
3
+ base.extend(SpecHelper)
4
+ $LOAD_PATH << Dir.pwd
5
+ end
6
+ module SpecHelper
7
+ class Event
8
+ @@event = Struct.new(:body, :query_params, :headers, :context)
9
+ def self.new(body: 'example body', query_params: {'foo' => 'bar'}, headers: {'Foo' => 'Bar'}, context: '{"foo": "bar"}')
10
+ @@event.new(body, query_params, headers, context)
11
+ end
12
+ end
13
+ class Response
14
+ @@response = Struct.new(:body, :status, :headers)
15
+ def self.new(body, status, headers)
16
+ @@response.new(body, status, headers)
17
+ end
18
+ end
19
+ end
20
+ def respond_with(body, status: 200, headers: {})
21
+ SpecHelper::Response.new(body, status, headers)
22
+ end
23
+ end
@@ -0,0 +1,2 @@
1
+ require 'helpers/faastruby'
2
+ include FaaStRuby
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ require 'faastruby'
3
+ require 'faastruby/cli'
4
+ command = ARGV.shift
5
+ FaaStRuby::CLI.run(command, ARGV)
@@ -0,0 +1,41 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "faastruby/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "faastruby"
7
+ spec.version = FaaStRuby::VERSION
8
+ spec.authors = ["Paulo Arruda"]
9
+ spec.email = ["parrudaj@gmail.com"]
10
+ spec.required_ruby_version = '>= 2.4.0'
11
+ spec.summary = %q{FaaStRuby CLI - Manage workspaces and functions hosted at faastruby.io.}
12
+ spec.homepage = "https://faastruby.io"
13
+ spec.license = "MIT"
14
+ spec.add_runtime_dependency 'rest-client', '~> 2.0'
15
+ spec.add_runtime_dependency 'oj', '~> 3.6'
16
+ spec.add_runtime_dependency 'tty-spinner', '~> 0.8'
17
+ spec.add_runtime_dependency 'tty-table', '~> 0.10'
18
+ spec.add_runtime_dependency 'rubyzip', '~> 1.2'
19
+ spec.add_runtime_dependency 'colorize', '~> 0.8'
20
+
21
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
22
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
23
+ # if spec.respond_to?(:metadata)
24
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
25
+ # else
26
+ # raise "RubyGems 2.0 or newer is required to protect against " \
27
+ # "public gem pushes."
28
+ # end
29
+
30
+ # Specify which files should be added to the gem when it is released.
31
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
32
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
33
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
34
+ end
35
+ spec.bindir = "exe"
36
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
+ spec.require_paths = ["lib"]
38
+
39
+ spec.add_development_dependency "bundler", "~> 1.16"
40
+ spec.add_development_dependency "rake", "~> 10.0"
41
+ end
@@ -0,0 +1,6 @@
1
+ require 'oj'
2
+ require 'faastruby/version'
3
+ require 'faastruby/base'
4
+ require 'faastruby/api'
5
+ require 'faastruby/function'
6
+ require 'faastruby/workspace'
@@ -0,0 +1,94 @@
1
+ require 'rest-client'
2
+
3
+ module FaaStRuby
4
+ class API
5
+ @@api_version = 'v2'
6
+ def initialize
7
+ @api_url = "#{HOST}/#{@@api_version}"
8
+ @credentials = {'API-KEY' => FaaStRuby.api_key, 'API-SECRET' => FaaStRuby.api_secret}
9
+ @headers = {content_type: :json, accept: :json}.merge(@credentials)
10
+ end
11
+
12
+ def create_workspace(workspace_name:, email: nil)
13
+ url = "#{@api_url}/workspaces"
14
+ payload = {'name' => workspace_name}
15
+ payload['email'] = email if email
16
+ parse RestClient.post(url, Oj.dump(payload), @headers){|response, request, result| response }
17
+ end
18
+
19
+ def destroy_workspace(workspace_name)
20
+ url = "#{@api_url}/workspaces/#{workspace_name}"
21
+ parse RestClient.delete(url, @headers){|response, request, result| response }
22
+ end
23
+
24
+ # def update_workspace(workspace_name, payload)
25
+ # url = "#{@api_url}/workspaces/#{workspace_name}"
26
+ # parse RestClient.patch(url, Oj.dump(payload), @headers)
27
+ # end
28
+
29
+ def get_workspace_info(workspace_name)
30
+ url = "#{@api_url}/workspaces/#{workspace_name}"
31
+ parse RestClient.get(url, @headers){|response, request, result| response }
32
+ end
33
+
34
+ def deploy(workspace_name:, package:)
35
+ url = "#{@api_url}/workspaces/#{workspace_name}/deploy"
36
+ payload = {package: File.new(package, 'rb')}
37
+ parse RestClient.post(url, payload, @credentials){|response, request, result| response }
38
+ end
39
+
40
+ def delete_from_workspace(function:, workspace:)
41
+ url = "#{@api_url}/workspaces/#{workspace.name}/functions/#{function.name}"
42
+ parse RestClient.delete(url, @headers){|response, request, result| response }
43
+ end
44
+
45
+ def list_workspace_functions(workspace_name)
46
+ url = "#{@api_url}/workspaces/#{workspace_name}/functions"
47
+ parse RestClient.get(url, @headers){|response, request, result| response }
48
+ end
49
+
50
+ def run(function_name:, workspace_name:, payload:, method:, headers: {}, time: false, query: nil)
51
+ url = "#{HOST}/#{workspace_name}/#{function_name}#{query}"
52
+ headers['Benchmark'] = true if time
53
+ if method == 'get'
54
+ RestClient.public_send(method, url, headers){|response, request, result| response }
55
+ else
56
+ RestClient.public_send(method, url, payload, headers){|response, request, result| response }
57
+ end
58
+ end
59
+
60
+ def update_function_context(function:, workspace:, payload:)
61
+ # payload is a string
62
+ url = "#{@api_url}/workspaces/#{workspace.name}/functions/#{function.name}"
63
+ parse RestClient.patch(url, Oj.dump(payload), @headers){|response, request, result| response }
64
+ end
65
+
66
+ def parse(response)
67
+ struct = Struct.new(:response, :body, :errors, :code)
68
+ case response.code
69
+ when 401
70
+ body = Oj.load(response.body)
71
+ return struct.new(nil, nil, ["(401) Unauthorized - #{body['error']}"], 401)
72
+ when 404
73
+ body = Oj.load(response.body)
74
+ return struct.new(nil, nil, ["(404) Not Found - #{body['error']}"], 404)
75
+ when 409
76
+ body = Oj.load(response.body)
77
+ return struct.new(nil, nil, ["(409) Conflict - #{body['error']}"], 409)
78
+ when 500
79
+ return struct.new(nil, nil, ["(500) Error"], 500)
80
+ when 408
81
+ return struct.new(nil, nil, ["(408) Request Timeout"], 408)
82
+ when 422
83
+ body = Oj.load(response.body)
84
+ return struct.new(nil, nil, ["(422) Unprocessable Entity - #{body['error']}"], 422)
85
+ when 402 # Limit excedeed
86
+ body = Oj.load(response.body)
87
+ return struct.new(nil, nil, ["(402) Limit Exceeded - #{body['error']}"], 402)
88
+ else
89
+ body = Oj.load(response.body)
90
+ return struct.new(response, body, (body['errors'] || []), response.code)
91
+ end
92
+ end
93
+ end
94
+ end