From 327c8ca465ab6cbd9a5f91460373a5af56c7479f Mon Sep 17 00:00:00 2001 From: C Cirello Date: Wed, 14 Dec 2016 02:41:08 +0100 Subject: [PATCH] examples: update guppy example with latest fn functionalities (#432) * examples: update guppy example * examples: fix typo --- examples/slackbot/guppy/README.md | 8 +++---- fn/langs/ruby.go | 40 +++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/examples/slackbot/guppy/README.md b/examples/slackbot/guppy/README.md index 0032cd028..ba086da36 100644 --- a/examples/slackbot/guppy/README.md +++ b/examples/slackbot/guppy/README.md @@ -5,9 +5,7 @@ This example will show you how to test and deploy a SlackBot command to IronFunc ```sh # create your func.yaml file fn init /guppy -# install dependencies, we need the json gem to run this -docker run --rm -v "$PWD":/worker -w /worker iron/ruby:dev bundle install --standalone --clean -# build the function +# build the function - install dependencies from json gem fn build # test it cat slack.payload | fn run @@ -16,7 +14,9 @@ fn push # Create a route to this function on IronFunctions fn routes create slackbot /guppy # Change the route response header content-type to application/json -curl -X PUT http://127.0.0.1:8080/v1/apps/slackbot/routes/guppy -d '{ "route": { "headers": { "Content-type": ["application/json"] } } }' +fn routes headers set slackbot /guppy Content-Type application/json +# test it remotely +cat slack.payload | fn call slackbot /guppy ``` ## Create a Slash Command integration in Slack diff --git a/fn/langs/ruby.go b/fn/langs/ruby.go index ca48dce24..75d9f143f 100644 --- a/fn/langs/ruby.go +++ b/fn/langs/ruby.go @@ -1,5 +1,13 @@ package langs +import ( + "fmt" + "os" + "os/exec" + "path/filepath" + "strings" +) + type RubyLangHelper struct { } @@ -8,14 +16,42 @@ func (lh *RubyLangHelper) Entrypoint() string { } func (lh *RubyLangHelper) HasPreBuild() bool { - return false + return true } -// PreBuild for Go builds the binary so the final image can be as small as possible func (lh *RubyLangHelper) PreBuild() error { + wd, err := os.Getwd() + if err != nil { + return err + } + + if !exists(filepath.Join(wd, "Gemfile")) { + return nil + } + + pbcmd := fmt.Sprintf("docker run --rm -v %s:/worker -w /worker iron/ruby:dev bundle install --standalone --clean", wd) + fmt.Println("Running prebuild command:", pbcmd) + parts := strings.Fields(pbcmd) + head := parts[0] + parts = parts[1:len(parts)] + cmd := exec.Command(head, parts...) + cmd.Stderr = os.Stderr + cmd.Stdout = os.Stdout + if err := cmd.Run(); err != nil { + return fmt.Errorf("error running docker build: %v", err) + } return nil } func (lh *RubyLangHelper) AfterBuild() error { return nil } + +func exists(name string) bool { + if _, err := os.Stat(name); err != nil { + if os.IsNotExist(err) { + return false + } + } + return true +}