howto

Wrapping a executable in a function

Easy function wrapping

tags
openfaas
cli

Lets look at how to wrap a command line program into a function. We'll assume that you already have an OpenFaaS service running somewhere.

First create the template

1
faas new --lang dockerfile myfunction

Calling the function

I'm going to write a command in ruby so I'll include things from the ruby:3.0.1 base image.

The main thing here is that I'm copying the script into /usr/local/bin and I'm changing the fprocess ENV variable to be xargs gitinfo.rb which will pass in the standard input as command line arguments.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  FROM ghcr.io/openfaas/classic-watchdog:0.1.5 as watchdog

  FROM ruby:3.0.1

  RUN apt-get update && apt-get install -y cloc libsqlite3-dev

  COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
  RUN chmod +x /usr/bin/fwatchdog

  WORKDIR /home/app

  # Add non root user
  RUN useradd app
  RUN chown app /home/app

  COPY Gemfile* ./
  RUN bundle install
  COPY *rb /usr/local/bin/
  RUN chmod +x /usr/local/bin/*rb

  USER app

  # Populate example here - i.e. "cat", "sha512sum" or "node index.js"
  ENV fprocess="xargs gitinfo.rb"
  # Set to true to see request in function logs
  ENV write_debug="false"

  EXPOSE 8080

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

  CMD ["fwatchdog"]

The script looks like this:

1
2
3
4
5
6
7
  #!/usr/bin/env ruby

  puts "This is the info file"

  ARGV.each do |v,i|
    puts "#{i}: #{v}"
  end

Running without an argument

1
curl https://api.gitgratitude.com/function/gitinfo
This is the info file

Passing in arguments

Here we can pass in data to the function:

1
echo Hello world | curl -X POST --data @- https://api.gitgratitude.com/function/gitinfo
This is the info file
: Hello
: world

Async test

I'm going to use pipedream.com as a way to collect a response. Go there and sign up for it if you haven't, and then set the CALLBACK_URL to be resulting address.

In order to have a function run asyncronously, you need the change the url to start with /async-function/ and pass in an X-Callback-URL header where the result is posted.

1
2
3
4
5
  CALLBACK_URL=https://eno35z9ue0i7ffz.m.pipedream.net
  echo Hello world | curl -X POST \
                          -H "X-Callback-Url: ${CALLBACK_URL}" \
                          --data @- \
                          https://api.gitgratitude.com/async-function/gitinfo

Conclustion

Very simple, very little was changed from the below posts below.

Previously

howto

Uploading to S3 on the command line

throwing data into a bucket

tags
bash
ruby
s3
spaces
curl

Next

howto

Getting emacs working on OSX Monterey

security and permissions

tags
emacs
osx
homebrew