Add Ruby environment (#223)

Adds a Ruby environment and examples.
This commit is contained in:
S. Brent Faulkner
2017-06-17 10:48:05 -07:00
committed by Soam Vasani
parent 0207d13b23
commit a8e7ec534b
8 changed files with 196 additions and 0 deletions
+16
View File
@@ -0,0 +1,16 @@
FROM ruby:2.4.1
RUN apt-get update -qq && apt-get install -y build-essential
RUN mkdir /app
WORKDIR /app
ADD Gemfile /app/Gemfile
ADD Gemfile.lock /app/Gemfile.lock
RUN bundle install
COPY . /app
EXPOSE 8888
ENTRYPOINT ["ruby"]
CMD ["server.rb"]
+4
View File
@@ -0,0 +1,4 @@
# frozen_string_literal: true
source "https://rubygems.org"
gem "rack"
+13
View File
@@ -0,0 +1,13 @@
GEM
remote: https://rubygems.org/
specs:
rack (2.0.3)
PLATFORMS
ruby
DEPENDENCIES
rack
BUNDLED WITH
1.15.1
+41
View File
@@ -0,0 +1,41 @@
# Fission: Ruby Environment
This is the Ruby environment for Fission.
It's a Docker image containing a Ruby 2.4.1 runtime, along with a
dynamic loader. A few common dependencies are included in the
Gemfile.
## Customizing this image
To add package dependencies, edit Gemfile to add what you
need, and rebuild this image (instructions below).
## Rebuilding and pushing the image
You'll need access to a Docker registry to push the image: you can
sign up for Docker hub at hub.docker.com, or use registries from
gcr.io, quay.io, etc. Let's assume you're using a docker hub account
called USER. Build and push the image to the the registry:
```
docker build -t USER/ruby-env . && docker push USER/ruby-env
```
## Using the image in fission
You can add this customized image to fission with "fission env
create":
```
fission env create --name ruby --image USER/ruby-env
```
Or, if you already have an environment, you can update its image:
```
fission env update --name ruby --image USER/ruby-env
```
After this, fission functions that have the env parameter set to the
same environment name as this command will use this environment.
+66
View File
@@ -0,0 +1,66 @@
# frozen_string_literal: true
require 'rack'
CODEPATH = '/userfunc/user'
module Fission
class Request < Rack::Request
def headers
Hash[
*env.select { |k,v| k.start_with?('HTTP_') }
.map { |k,v| [k.sub(/\AHTTP_/, '').split('_').map(&:capitalize).join('-'), v] }
.sort
.flatten
]
end
end
class Context
attr_reader :env
def initialize(env)
@env = env
end
def request
@request ||= Request.new(env)
end
end
module Specializer
def self.call(env)
load CODEPATH
Rack::Response.new([], 201).finish
rescue
Rack::Response.new(['500 Internal Server Error'], 500, {}).finish
end
end
module Handler
def self.call(env)
response = if method(:handler).arity > 0
handler(Context.new(env))
else
handler
end
response.is_a?(Array) ? response : Rack::Response.new([response]).finish
rescue
Rack::Response.new(['500 Internal Server Error'], 500, {}).finish
end
end
end
app = Rack::Builder.new do
use Rack::CommonLogger, $stderr
map "/specialize" do
run Fission::Specializer
end
map "/" do
run Fission::Handler
end
end
Rack::Handler::WEBrick.run app, Host: '0.0.0.0', Port: 8888
+33
View File
@@ -0,0 +1,33 @@
# frozen_string_literal: true
require 'net/http'
require 'uri'
require 'json'
SLACK_BASE_URL = 'https://hooks.slack.com/'
SLACK_WEBHOOK_PATH = 'YOUR RELATIVE PATH HERE' # Something like "/services/XXX/YYY/zZz123"
def send_slack_message(message)
uri = URI.join(SLACK_BASE_URL, SLACK_WEBHOOK_PATH)
data = "{'channel': '#hackdays-serverless', 'username': 'fissionbot', 'text': \"#{message}\", 'icon_emoji': ':fission:'}"
res = Net::HTTP.post_form(uri, payload: data)
res.success?
end
def handler(context)
request = context.request
event_type = request.headers['X-Kubernetes-Event-Type']
object_type = request.headers['X-Kubernetes-Object-Type']
object = JSON.parse(request.body.read)
object_name = object.dig('metadata', 'name')
object_namespace = object.dig('metadata', 'namespace')
message = "#{event_type} #{object_type} #{object_namespace}/#{object_name}"
if send_slack_message(message)
"Slack message sent - #{message}"
else
Rack::Response.new(["Failed to send Slack message - #{message}"], 500).finish
end
end
+4
View File
@@ -0,0 +1,4 @@
# frozen_string_literal: true
def handler
"Hello, world!\n"
end
+19
View File
@@ -0,0 +1,19 @@
# frozen_string_literal: true
def handler(context)
request = context.request
msg = <<~MSG
---ENV---
#{request.env.map { |h| h.join('=') }.join("\n") }
---HEADERS---
#{request.headers.map { |h| h.join(': ') }.join("\n") }
---PARAMS---
#{request.params.map { |h| h.join('=') }.join("\n") }
--BODY--
#{request.body.read}
MSG
Rack::Response.new([msg]).finish
end