functions_framework 0.1.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1458a2e7cef4ad3841ae328a79cd95ee8a5eea0091fb9dd6e618d443725a304a
4
- data.tar.gz: 6294b3dcf3ec251ba3c026588b5eb28559353a9e09316ff9965b25a81511d725
3
+ metadata.gz: cdf75f322fe9e9e78b5e65bfcc9682097606deffb46137d85b6b1cfde68d3ab8
4
+ data.tar.gz: 54f8e668875607738141963c70b1960da6c06df69083a7ef4357f66889e1a328
5
5
  SHA512:
6
- metadata.gz: ec252cf13280072a0882292326b5b9d881efef8db349268f2d380996fc94180fbb203707d47f92d3c0dee31758f8f733b26a24219191df523cd8420f3562ba3e
7
- data.tar.gz: 44eb7e2c387710b77e0cf2676f00aaf259e67f84a1bad1b1b531c130b1cf37140681833bf5004d224ba82f0912f5dc6e0ebb2686e50be2cf0350c9ff7df9d821
6
+ metadata.gz: 3d6fd59bc3730333e91a049d03089169720390f5f9300420449480924800d97da118a87cdae33224644c3550fe78dfee4a07b18e32e534d9bb7645340fbdf4c9
7
+ data.tar.gz: 897cef8cc366475123250d4c9a7cea0ccd07989c9ebdc0d88d358d996c5fa727aa9bcc5eb46be286d38e72a9d2512adfbaed743975d0300502516db9e3a5fde2
data/.yardopts CHANGED
@@ -2,9 +2,13 @@
2
2
  --title=Functions Framework
3
3
  --markup markdown
4
4
  --markup-provider redcarpet
5
- --main=README.md
5
+ --main=docs/overview.md
6
6
  ./lib/functions_framework/**/*.rb
7
7
  ./lib/functions_framework.rb
8
8
  -
9
- README.md
9
+ docs/overview.md
10
+ docs/writing-functions.md
11
+ docs/testing-functions.md
12
+ docs/running-a-functions-server.md
13
+ docs/deploying-functions.md
10
14
  CHANGELOG.md
@@ -1,5 +1,49 @@
1
1
  # Changelog
2
2
 
3
+ ### v0.4.0 / 2020-06-29
4
+
5
+ * Dropped the legacy and largely unsupported `:event` function type. All event functions should be of type `:cloud_event`.
6
+ * Define the object context for function execution, and include an extensible context helper.
7
+ * Support for CloudEvents with specversion 0.3.
8
+ * CloudEvents now correct percent-encodes/decodes binary headers.
9
+ * CloudEvents now includes more robust RFC 2045 parsing of the Content-Type header.
10
+ * The CloudEventsError class now properly subclasses StandardError instead of RuntimeError.
11
+ * Removed redundant `_string` accessors from event classes since raw forms are already available via `[]`.
12
+ * A variety of corrections to event-related class documentation.
13
+
14
+ ### v0.3.1 / 2020-06-27
15
+
16
+ * Fixed crash when using "return" directly in a function block.
17
+ * Added a more flexible request generation helper in the testing module.
18
+ * Fixed several typos in the documentation.
19
+
20
+ ### v0.3.0 / 2020-06-26
21
+
22
+ * Updated the CloudEvent data format for converted pubsub events to conform to Cloud Run's conversion.
23
+
24
+ ### v0.2.1 / 2020-06-25
25
+
26
+ * The `--signature-type` check recognizes the legacy `event` type for `:cloud_event` functions.
27
+
28
+ ### v0.2.0 / 2020-06-24
29
+
30
+ Significant changes:
31
+
32
+ * Converts legacy GCF events and passes them to functions as CloudEvents.
33
+ * The executable is now named `functions-framework-ruby` to avoid collisions with functions frameworks for other languages.
34
+ * Deprecated the `event` function type. Use `cloud_event`.
35
+ * The CloudEvents implementation is now fully-featured and can encode as well as decode events.
36
+ * Wrote an expanded set of getting-started documentation.
37
+
38
+ Minor changes:
39
+
40
+ * `Testing.load_temporary` now caches loaded functions so they don't have to be reloaded for subsequent tests.
41
+ * The executable recognizes the `--signature-type` flag, and verifies that the type is correct.
42
+ * Error reporting is expanded and improved.
43
+ * Fixed a crash when a batch CloudEvent was received. (These are still not supported, and now result in a 400.)
44
+ * Renamed a few undocumented environment variables, and added support for a logging level environment variable. All CLI flags now have associated environment variables.
45
+ * Several fixes to the example code, and added a new Sinatra example.
46
+
3
47
  ### v0.1.1 / 2020-02-27
4
48
 
5
49
  * Server returns 404 when receiving a /favicon.ico or /robots.txt request.
data/README.md CHANGED
@@ -4,7 +4,7 @@ An open source framework for writing lightweight, portable Ruby functions that
4
4
  run in a serverless environment. Functions written to this Framework will run
5
5
  in many different environments, including:
6
6
 
7
- * [Google Cloud Functions](https://cloud.google.com/functions) *(coming soon)*
7
+ * [Google Cloud Functions](https://cloud.google.com/functions) *(in preview)*
8
8
  * [Cloud Run or Cloud Run for Anthos](https://cloud.google.com/run)
9
9
  * Any other [Knative](https://github.com/knative)-based environment
10
10
  * Your local development machine
@@ -12,7 +12,7 @@ in many different environments, including:
12
12
  The framework allows you to go from:
13
13
 
14
14
  ```ruby
15
- FunctionsFramework.http do |request|
15
+ FunctionsFramework.http("hello") do |request|
16
16
  "Hello, world!\n"
17
17
  end
18
18
  ```
@@ -24,11 +24,8 @@ curl http://my-url
24
24
  # Output: Hello, world!
25
25
  ```
26
26
 
27
- All without needing to worry about writing an HTTP server or complicated
28
- request handling logic.
29
-
30
- For more information about the Functions Framework, see
31
- https://github.com/GoogleCloudPlatform/functions-framework
27
+ Running on a fully-managed or self-managed serverless environment, without
28
+ requiring an HTTP server or complicated request handling logic.
32
29
 
33
30
  ## Features
34
31
 
@@ -36,26 +33,14 @@ https://github.com/GoogleCloudPlatform/functions-framework
36
33
  * Invoke functions in response to requests.
37
34
  * Automatically unmarshal events conforming to the
38
35
  [CloudEvents](https://cloudevents.io) spec.
36
+ * Automatically convert most legacy events from Google Cloud services such
37
+ as Cloud Pub/Sub and Cloud Storage, to CloudEvents.
39
38
  * Spin up a local development server for quick testing.
40
39
  * Integrate with standard Ruby libraries such as Rack and Minitest.
41
40
  * Portable between serverless platforms.
41
+ * Supports all non-end-of-life versions of Ruby.
42
42
 
43
- ## Installation
44
-
45
- Install the Functions Framework via Rubygems:
46
-
47
- ```sh
48
- gem install functions_framework
49
- ```
50
-
51
- Or add it to your Gemfile for installation using Bundler:
52
-
53
- ```ruby
54
- # Gemfile
55
- gem "functions_framework", "~> 0.1"
56
- ```
57
-
58
- ### Supported Ruby versions
43
+ ## Supported Ruby versions
59
44
 
60
45
  This library is supported on Ruby 2.4+.
61
46
 
@@ -68,14 +53,14 @@ about the Ruby support schedule.
68
53
 
69
54
  ## Quickstart
70
55
 
71
- ### Running Hello World on your local machine
56
+ Here is how to run a Hello World function on your local machine.
72
57
 
73
58
  Create a `Gemfile` listing the Functions Framework as a dependency:
74
59
 
75
60
  ```ruby
76
61
  # Gemfile
77
62
  source "https://rubygems.org"
78
- gem "functions_framework", "~> 0.1"
63
+ gem "functions_framework", "~> 0.4"
79
64
  ```
80
65
 
81
66
  Create a file called `app.rb` and include the following code. This defines a
@@ -96,123 +81,58 @@ running your "hello" function:
96
81
  ```sh
97
82
  bundle install
98
83
  # ...installs the functions_framework gem and other dependencies
99
- bundle exec functions-framework --target hello
84
+ bundle exec functions-framework-ruby --target hello
100
85
  # ...starts the web server in the foreground
101
86
  ```
102
87
 
103
88
  In a separate shell, you can send requests to this function using curl:
104
89
 
105
90
  ```sh
106
- curl localhost:8080
91
+ curl http://localhost:8080
107
92
  # Output: Hello, world!
108
93
  ```
109
94
 
110
- Stop the web server with `CTRL+C`.
111
-
112
- ### Deploying a function to Google Cloud Functions
113
-
114
- (Google Cloud Functions does not yet support the Ruby Function Framework)
115
-
116
- ### Deploying a function to Cloud Run
117
-
118
- Create a `Dockerfile` for your function:
119
-
120
- ```dockerfile
121
- # Dockerfile
122
- FROM ruby:2.6
123
-
124
- WORKDIR /app
125
- COPY . .
126
- RUN gem install --no-document bundler \
127
- && bundle config --local frozen true \
128
- && bundle install
129
-
130
- ENTRYPOINT ["bundle", "exec", "functions-framework"]
131
- CMD ["--target", "hello"]
132
- ```
133
-
134
- Build your function into a Docker image:
135
-
136
- ```sh
137
- gcloud builds submit --tag=gcr.io/[YOUR-PROJECT]/hello:build-1
138
- ```
139
-
140
- Deploy to Cloud Run:
141
-
142
- ```sh
143
- gcloud run deploy hello --image=gcr.io/[YOUR-PROJECT]/hello:build-1 \
144
- --platform=managed --allow-unauthenticated --region=us-central1
145
- ```
146
-
147
- You can use a similar approach to deploy to any other Knative-based serverless
148
- environment.
149
-
150
- ### Responding to CloudEvents
151
-
152
- You can also define a function that response to
153
- [CloudEvents](https://cloudevents.io). The Functions Framework will handle
154
- unmarshalling of the event data.
155
-
156
- Change `app.rb` to read:
157
-
158
- ```ruby
159
- # app.rb
160
- require "functions_framework"
161
-
162
- FunctionsFramework.event("my-handler") do |data, context|
163
- FunctionsFramework.logger.info "I received #{data.inspect}"
164
- end
165
- ```
166
-
167
- Start up the framework with this new function:
168
-
169
- ```sh
170
- bundle install
171
- bundle exec functions-framework --target my-handler
172
- ```
173
-
174
- In a separate shell, you can send a CloudEvent to this function using curl:
175
-
176
- ```sh
177
- curl --header "Content-Type: text/plain; charset=utf-8" \
178
- --header "CE-ID: 12345" \
179
- --header "CE-Source: curl" \
180
- --header "CE-Type: com.example.test" \
181
- --header "CE-Specversion: 1.0" \
182
- --data "Hello, world!" \
183
- http://localhost:8080
184
- ```
185
-
186
- CloudEvents functions do not return meaningful results, but you will see the
187
- log message from the web server.
188
-
189
- ### Configuring the Functions Framework
190
-
191
- The Ruby Functions Framework recognizes the standard command line arguments to
192
- the `functions-framework` executable. Each argument also corresponds to an
193
- environment variable. If you specify both, the environment variable will be
194
- ignored.
195
-
196
- Command-line flag | Environment variable | Description
197
- ----------------- | -------------------- | -----------
198
- `--port` | `PORT` | The port on which the Functions Framework listens for requests. Default: `8080`
199
- `--target` | `FUNCTION_TARGET` | The name of the exported function to be invoked in response to requests. Default: `function`
200
- `--source` | `FUNCTION_SOURCE` | The path to the file containing your function. Default: `app.rb` (in the current working directory)
201
-
202
- Note: the flag `--signature-type` and corresponding environment variable
203
- `FUNCTION_SIGNATURE_TYPE` are not used by the Ruby Function Framework, because
204
- you specify the signature type when defining the function in the source.
205
-
206
- The Ruby `functions-framework` executable also recognizes several additional
207
- flags that can be used to control logging verbosity, binding, and other
208
- parameters. For details, see the online help:
209
-
210
- ```sh
211
- functions-framework --help
212
- ```
213
-
214
- ## For more information
215
-
216
- * See the `examples` directory for additional examples
217
- * Consult https://rubydoc.info/gems/functions_framework for full reference
218
- documentation.
95
+ Stop the server with `CTRL+C`.
96
+
97
+ ## Documentation
98
+
99
+ These guides provide additional getting-started information.
100
+
101
+ * **[Writing Functions](https://rubydoc.info/gems/functions_framework/file/docs/writing-functions.md)**
102
+ : How to write functions that respond to HTTP requests, industry-standard
103
+ [CloudEvents](https://cloudevents.io), as well as events sent from Google
104
+ Cloud services such as [Pub/Sub](https://cloud.google.com/pubsub) and
105
+ [Storage](https://cloud.google.com/storage).
106
+ * **[Testing Functions](https://rubydoc.info/gems/functions_framework/file/docs/testing-functions.md)**
107
+ : How to use the testing features of the Functions Framework to write local
108
+ unit tests for your functions using standard Ruby testing frameworks such
109
+ as [Minitest](https://github.com/seattlerb/minitest) and
110
+ [RSpec](https://rspec.info/).
111
+ * **[Running a Functions Server](https://rubydoc.info/gems/functions_framework/file/docs/running-a-functions-server.md)**
112
+ : How to use the `functions-framework-ruby` executable to run a local
113
+ functions server.
114
+ * **[Deploying Functions](https://rubydoc.info/gems/functions_framework/file/docs/deploying-functions.md)**
115
+ : How to deploy functions to
116
+ [Google Cloud Functions](https://cloud.google.com/functions) or
117
+ [Google Cloud Run](https://cloud.google.com/run).
118
+
119
+ The library reference documentation can be found at:
120
+ https://rubydoc.info/gems/functions_framework
121
+
122
+ Additional examples are available in the `examples` directory:
123
+ https://github.com/GoogleCloudPlatform/functions-framework-ruby/blob/master/examples/
124
+
125
+ ## Development
126
+
127
+ The source for the Ruby Functions Framework is available on GitHub at
128
+ https://github.com/GoogleCloudPlatform/functions-framework-ruby. For more
129
+ information on the Functions Framework contract implemented by this framework,
130
+ as well as links to Functions Frameworks for other languages, see
131
+ https://github.com/GoogleCloudPlatform/functions-framework.
132
+
133
+ The Functions Framework is open source under the Apache 2.0 license.
134
+ Contributions are welcome. Please see the contributing guide at
135
+ https://github.com/GoogleCloudPlatform/functions-framework-ruby/blob/master/.github/CONTRIBUTING.md.
136
+
137
+ Report issues at
138
+ https://github.com/GoogleCloudPlatform/functions-framework-ruby/issues.
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Copyright 2020 Google LLC
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # https://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require "functions_framework/cli"
18
+
19
+ ::FunctionsFramework::CLI.new.parse_args(::ARGV).run
@@ -0,0 +1,182 @@
1
+ <!--
2
+ # @title Deploying Functions
3
+ -->
4
+
5
+ # Deploying Functions
6
+
7
+ This guide covers how to deploy your Ruby functions written with the Functions
8
+ Framework. Functions can be deployed to
9
+ [Google Cloud Functions](https://cloud.google.com/functions), Google's
10
+ Functions-as-a-service (FaaS) product, to
11
+ [Google Cloud Run](https://cloud.google.com/run). Google's container-based
12
+ serverless environment, or to any KNative-based environment.
13
+ For more information about the Framework as a whole, see the
14
+ {file:docs/overview.md Overview Guide}.
15
+
16
+ ## Before you begin
17
+
18
+ To deploy to Google Cloud, whether to Cloud Functions or Cloud Run, you'll need
19
+ a Google Cloud project with billing enabled. Go to the
20
+ [Google Cloud console](https://console.cloud.google.com/), create a project (or
21
+ select an existing project), and ensure billing is enabled.
22
+
23
+ Additionally, install the [Google Cloud SDK](https://cloud.google.com/sdk) if
24
+ you haven't done so previously.
25
+
26
+ ## Deploying to Cloud Functions
27
+
28
+ Google Cloud Functions is Google's scalable pay-as-you-go Functions-as-a-Service
29
+ (FaaS) environment that can run your function with zero server management. The
30
+ Functions Framework is designed especially for functions that can be hosted on
31
+ Cloud Functions.
32
+
33
+ You can run Ruby functions on Google Cloud Functions by selecting the `ruby26`
34
+ runtime. This runtime uses a recent release of Ruby 2.6. Support for other
35
+ versions of Ruby may be added in the future.
36
+
37
+ > **Note:** Ruby support on Cloud Functions is currently in limited preview.
38
+ > It is not yet suitable for production workloads, and support is best-effort
39
+ > only. Access is currently limited to selected early-access users.
40
+
41
+ ### Deploying and updating your function
42
+
43
+ Before you can deploy to Cloud Functions, make sure your bundle, and in
44
+ particular your `Gemfile.lock` file, is up to date. The easiest way to do this
45
+ is to `bundle install` or `bundle update` and run your local tests prior to
46
+ deploying. Cloud Functions will not accept your function unless an up-to-date
47
+ `Gemfile.lock` is present.
48
+
49
+ Choose a name for your function. This function name is how it will appear in the
50
+ cloud console, and will also be part of the function's URL. (It's different from
51
+ the name you provide when writing your function; Cloud Functions calls that name
52
+ the "function target".)
53
+
54
+ Then, issue the gcloud command to deploy:
55
+
56
+ ```sh
57
+ gcloud functions deploy $YOUR_FUNCTION_NAME --project=$YOUR_PROJECT_ID \
58
+ --runtime=ruby26 --trigger-http --source=$YOUR_FUNCTION_SOURCE \
59
+ --entry-point=$YOUR_FUNCTION_TARGET
60
+ ```
61
+
62
+ The source file defaults to `./app.rb` and the function target defaults to
63
+ `function`, so those flags can be omitted if you're using the defaults. The
64
+ project flag can also be omitted if you've set it as the default with
65
+ `gcloud config set project`.
66
+
67
+ If your function handles events rather than HTTP requests, you'll need to
68
+ replace `--trigger-http` with a different trigger. For details, see the
69
+ [reference documentation](https://cloud.google.com/sdk/gcloud/reference/functions/deploy)
70
+ for `gcloud functions deploy`.
71
+
72
+ To update your deployment, just redeploy using the same function name.
73
+
74
+ ### Configuring Cloud Functions deployments
75
+
76
+ The Functions Framework provides various configuration parameters, described in
77
+ {file:docs/running-a-functions-server.md Running a Functions Server}.
78
+ If you want to set any of these parameters beyond the source file and target,
79
+ you must set environment variables. For example, to limit logging to WARN level
80
+ and above, set `FUNCTION_LOGGING_LEVEL` to `WARN` when deploying:
81
+
82
+ ```sh
83
+ gcloud functions deploy $YOUR_FUNCTION_NAME --project=$YOUR_PROJECT_ID \
84
+ --runtime=ruby26 --trigger-http --source=$YOUR_FUNCTION_SOURCE \
85
+ --entry-point=$YOUR_FUNCTION_TARGET \
86
+ --set-env-vars=FUNCTION_LOGGING_LEVEL=WARN
87
+ ```
88
+
89
+ Consult the table in
90
+ {file:docs/running-a-functions-server.md Running a Functions Server}
91
+ for a list of the environment variables that can be set.
92
+
93
+ ## Deploying to Cloud Run
94
+
95
+ Google Cloud Run is Google's managed compute platform for deploying and scaling
96
+ containerized applications quickly and securely. It can run any container-based
97
+ workload, including a containerized function.
98
+
99
+ Cloud Run has a hosted fully-managed option that runs on Google's infrastructure
100
+ and monitors and scales your application automatically, and a self-managed or
101
+ on-prem option called Cloud Run for Anthos that runs atop Kubernetes. Both
102
+ flavors use the same general interface and can run functions in the same way.
103
+ This tutorial is written for the managed option, but it should not be difficult
104
+ to adapt it if you have an Anthos installation.
105
+
106
+ ### Building an image for your function
107
+
108
+ First, build a Docker image containing your function. Following is a simple
109
+ Dockerfile that you can use as a starting point. Feel free to adjust it to the
110
+ needs of your project:
111
+
112
+ ```
113
+ FROM ruby:2.6
114
+ WORKDIR /app
115
+ COPY . .
116
+ RUN gem install --no-document bundler \
117
+ && bundle config --local frozen true \
118
+ && bundle config --local without "development test" \
119
+ && bundle install
120
+ ENTRYPOINT ["bundle", "exec", "functions-framework-ruby"]
121
+ ```
122
+
123
+ You can test your image locally using the steps described under
124
+ {file:docs/running-a-functions-server.md Running a Functions Server}.
125
+
126
+ When your Dockerfile is ready, you can use
127
+ [Cloud Build](https://cloud.google.com/cloud-build) to build it and store the
128
+ image in your project's container registry.
129
+
130
+ ```sh
131
+ gcloud builds submit --tag=gcr.io/$YOUR_PROJECT_ID/$YOUR_APP_NAME:$YOUR_BUILD_ID .
132
+ ```
133
+
134
+ You must use your project ID, but you can choose an app name and build ID. The
135
+ command may ask you for permission to enable the Cloud Build API for the project
136
+ if it isn't already enabled.
137
+
138
+ Because you provide your own Docker image when deploying to Cloud Run, you can
139
+ use any version of Ruby supported by the Functions Framework, from 2.4 through
140
+ 2.7.
141
+
142
+ ### Deploying an image to Cloud Run
143
+
144
+ To deploy to Cloud Run, specify the same image URL that you built above. For
145
+ example:
146
+
147
+ ```sh
148
+ gcloud run deploy $YOUR_APP_NAME --project=$YOUR_PROJECT_ID \
149
+ --image=gcr.io/$YOUR_PROJECT_ID/$YOUR_APP_NAME:$YOUR_BUILD_ID \
150
+ --platform=managed --allow-unauthenticated --region=us-central1 \
151
+ --set-env-vars=FUNCTION_SOURCE=$YOUR_SOURCE,FUNCTION_TARGET=$YOUR_TARGET
152
+ ```
153
+
154
+ You can omit the `--project` flag if you've already set it as the default with
155
+ `gcloud config set project`.
156
+
157
+ The command may ask you for permission to enable the Cloud Run API for the
158
+ project, if it isn't already enabled.
159
+
160
+ At the end of the deployment process, the command will display the hostname for
161
+ the Cloud Run service. You can use that hostname to send test requests to your
162
+ deployed function.
163
+
164
+ ### Configuring Cloud Run deployments
165
+
166
+ Note that our Dockerfile's entrypoint did not pass any source file or target
167
+ name to the Functions Framework. If these are not specified, the Framework will
168
+ use the source `.app.rb` and the target `function` by default. To use different
169
+ values, you need to set the appropriate environment variables when deploying, as
170
+ illustrated above with the `FUNCTION_SOURCE` and `FUNCTION_TARGET` variables.
171
+
172
+ Source and target are not the only configuration parameters available. The
173
+ various parameters, along with their environment variables, are described in
174
+ {file:docs/running-a-functions-server.md Running a Functions Server}.
175
+ Any of these can be specified in the `--set-env-vars` flag when you deploy to
176
+ Google Cloud Run.
177
+
178
+ It is also possible to "hard-code" configuration into the Dockerfile, by setting
179
+ environment variables in the Dockerfile, or adding flags to the entrypoint.
180
+ However, it is often better practice to keep your Dockerfile "generic", and set
181
+ configuration environment variables during deployment, so that you do not need
182
+ to rebuild your Docker image every time you want to change configuration.