Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add perl as supported language #84

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This is a CLI for use with [OpenFaaS](https://github.com/alexellis/faas) - a ser

> Before using this tool please setup OpenFaaS by following instructions over on the main repo.

The CLI can be used to build and deploy functions to [OpenFaaS](https://github.com/alexellis/faas). You can build OpenFaaS functions from a set of supported language templates (such as Node.js, Python, CSharp and Ruby). That means you just write a handler file such as (handler.py/handler.js) and the CLI does the rest to create a Docker image.
The CLI can be used to build and deploy functions to [OpenFaaS](https://github.com/alexellis/faas). You can build OpenFaaS functions from a set of supported language templates (such as CSharp, Node.js, Perl, Python and Ruby). That means you just write a handler file such as (handler.py/handler.js) and the CLI does the rest to create a Docker image.

Demo: [ASCII cinema](https://asciinema.org/a/121234)

Expand Down
2 changes: 1 addition & 1 deletion build_samples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

./faas-cli build --yaml ./samples.yml # --squash=true

docker images |head -n 4
docker images |head -n 5
2 changes: 1 addition & 1 deletion builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func BuildImage(image string, handler string, functionName string, language string, nocache bool, squash bool) {

switch language {
case "node", "python", "ruby", "csharp":
case "csharp", "node", "perl", "python", "ruby":
tempPath := createBuildTemplate(functionName, handler, language)

fmt.Printf("Building: %s with %s template. Please wait..\n", image, language)
Expand Down
2 changes: 1 addition & 1 deletion commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var buildCmd = &cobra.Command{
faas-cli build --image IMAGE_NAME
--handler HANDLER_DIR
--name FUNCTION_NAME
[--lang <ruby|python|python-armf|node|node-armf|csharp|Dockerfile>]
[--lang <csharp|Dockerfile|node|node-armf|perl|python|python-armf|ruby>]
[--no-cache] [--squash]`,
Short: "Builds OpenFaaS function containers",
Long: `Builds OpenFaaS function containers either via the supplied YAML config using
Expand Down
4 changes: 4 additions & 0 deletions deploy_samples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ echo
echo "Testing shrink-image"
curl -d "" http://localhost:8080/function/shrink-image --data-binary @gordon.png > small_gordon.png
echo

echo "Testing perl-echo"
curl -sd "Hello World" http://localhost:8080/function/perl-echo
echo
10 changes: 6 additions & 4 deletions proxy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ func DeployFunction(fprocess string, gateway string, functionName string, image
var fprocessTemplate string
if len(fprocess) > 0 {
fprocessTemplate = fprocess
} else if language == "python" {
fprocessTemplate = "python index.py"
} else if language == "csharp" {
fprocessTemplate = "dotnet ./bin/Debug/netcoreapp2.0/root.dll"
} else if language == "node" {
fprocessTemplate = "node index.js"
} else if language == "perl" {
fprocessTemplate = "perl index.pl"
} else if language == "python" {
fprocessTemplate = "python index.py"
} else if language == "ruby" {
fprocessTemplate = "ruby index.rb"
} else if language == "csharp" {
fprocessTemplate = "dotnet ./bin/Debug/netcoreapp2.0/root.dll"
}

gateway = strings.TrimRight(gateway, "/")
Expand Down
9 changes: 9 additions & 0 deletions sample/perl-echo/Handler.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package Handler;

use JSON::XS;

sub handle {
print encode_json( { 'echo' => shift } );
}

1;
8 changes: 8 additions & 0 deletions sample/perl-echo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# perl-echo

This sample Perl function implements an ECHO service returning
the input to the function wrapped in a JSON string i.e.:

```json
{'echo':'<function input>'}
```
1 change: 1 addition & 0 deletions sample/perl-echo/cpanfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requires 'JSON::XS';
23 changes: 14 additions & 9 deletions samples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,29 @@ provider:
network: "func_functions" # this is optional and defaults to func_functions

functions:
url-ping:
lang: python
handler: ./sample/url-ping
image: alexellis/faas-url-ping

nodejs-echo:
lang: node
handler: ./sample/nodejs-echo
image: alexellis/faas-nodejs-echo

perl-echo:
lang: perl
handler: ./perl-echo/
image: faas-perl-echo

ruby-echo:
lang: ruby
handler: ./sample/ruby-echo
image: alexellis/ruby-echo

# curl localhost:8080/function/shrink-image --data-binary @big.png > smaller.png
shrink-image:
lang: Dockerfile
handler: ./sample/imagemagick
image: functions/resizer
fprocess: "convert - -resize 50% fd:1"

ruby-echo:
lang: ruby
handler: ./sample/ruby-echo
image: alexellis/ruby-echo
url-ping:
lang: python
handler: ./sample/url-ping
image: alexellis/faas-url-ping
34 changes: 34 additions & 0 deletions template/perl/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
FROM perl:5.26


RUN apt-get update -qy \
&& apt-get install -qy curl ca-certificates --no-install-recommends \
&& echo "Pulling watchdog binary from Github." \
&& curl -sSL https://github.com/alexellis/faas/releases/download/0.6.4/fwatchdog > /usr/bin/fwatchdog \
&& chmod +x /usr/bin/fwatchdog \
&& apt-get -qy remove curl \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

RUN cpanm Carton

WORKDIR /root/

COPY index.pl .
COPY cpanfile .

RUN carton install

COPY function function

WORKDIR /root/function/
COPY function/cpanfile .
RUN carton install

WORKDIR /root/

ENV fprocess="carton exec perl index.py"

HEALTHCHECK --interval=1s CMD [ -e /tmp/.lock ] || exit 1

CMD ["fwatchdog"]
Empty file added template/perl/cpanfile
Empty file.
8 changes: 8 additions & 0 deletions template/perl/function/Handler.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package Handler;

sub handle {
my $st = shift;
return "Hello world from the Perl template"
}

1;
Empty file added template/perl/function/cpanfile
Empty file.
13 changes: 13 additions & 0 deletions template/perl/index.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use lib qw(function local/lib/perl5 function/local/lib/perl5);
require Handler;

sub get_stdin {
my $buf = "";
while (<STDIN>) {
$buf .= $_;
}
return $buf;
}

my $st = get_stdin();
Handler::handle($st);