functions_framework 0.1.1 → 0.2.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: d3591e5ab03b6e29d0a7f9b9c16d4d470138fbf0ddebec875e80f5f1df8a9767
4
+ data.tar.gz: 50ed98f079c42638fcb3328bab3d1388e323613ea310842bcd3d887b1a96fdfb
5
5
  SHA512:
6
- metadata.gz: ec252cf13280072a0882292326b5b9d881efef8db349268f2d380996fc94180fbb203707d47f92d3c0dee31758f8f733b26a24219191df523cd8420f3562ba3e
7
- data.tar.gz: 44eb7e2c387710b77e0cf2676f00aaf259e67f84a1bad1b1b531c130b1cf37140681833bf5004d224ba82f0912f5dc6e0ebb2686e50be2cf0350c9ff7df9d821
6
+ metadata.gz: 87a9631c32f05b5e5831f50e9dd7235351fd6556fb22474d655a916ceacf1d57e1d25a60369c31aa3790a9667cafec02dafad2ccf9c98499a681badf2516d13e
7
+ data.tar.gz: 755d890b0684d2af126306c3b7f7f2e5d3b1392b0fb88e325b45eba6c85d9d6cc28e320e20a8a9d716244e52e63fee839ab02ec56014873c62dd77f3fe4ea37c
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,24 @@
1
1
  # Changelog
2
2
 
3
+ ### v0.2.0 / 2020-06-24
4
+
5
+ Significant changes:
6
+
7
+ * Converts legacy GCF events and passes them to functions as CloudEvents.
8
+ * The executable is now named `functions-framework-ruby` to avoid collisions with functions frameworks for other languages.
9
+ * Deprecated the `event` function type. Use `cloud_event`.
10
+ * The CloudEvents implementation is now fully-featured and can encode as well as decode events.
11
+ * Wrote an expanded set of getting-started documentation.
12
+
13
+ Minor changes:
14
+
15
+ * `Testing.load_temporary` now caches loaded functions so they don't have to be reloaded for subsequent tests.
16
+ * The executable recognizes the `--signature-type` flag, and verifies that the type is correct.
17
+ * Error reporting is expanded and improved.
18
+ * Fixed a crash when a batch CloudEvent was received. (These are still not supported, and now result in a 400.)
19
+ * Renamed a few undocumented environment variables, and added support for a logging level environment variable. All CLI flags now have associated environment variables.
20
+ * Several fixes to the example code, and added a new Sinatra example.
21
+
3
22
  ### v0.1.1 / 2020-02-27
4
23
 
5
24
  * 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.2"
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 https://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,169 @@
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
+ ### Deploying and updating your function
34
+
35
+ Before you can deploy to Cloud Functions, make sure your bundle, and in
36
+ particular your `Gemfile.lock` file, is up to date. The easiest way to do this
37
+ is to `bundle install` or `bundle update` and run your local tests prior to
38
+ deploying.
39
+
40
+ Choose a name for your function. This function name is how it will appear in the
41
+ cloud console, and will also be part of the function's URL. (It's different from
42
+ the name you provide when writing your function; Cloud Functions calls that name
43
+ the "function target".)
44
+
45
+ Then, issue the gcloud command to deploy:
46
+
47
+ ```sh
48
+ gcloud functions deploy $YOUR_FUNCTION_NAME --project=$YOUR_PROJECT_ID \
49
+ --runtime=ruby26 --trigger-http --source=$YOUR_FUNCTION_SOURCE \
50
+ --entry-point=$YOUR_FUNCTION_TARGET
51
+ ```
52
+
53
+ The source file defaults to `./app.rb` and the function target defaults to
54
+ `function`, so those flags can be omitted if you're using the defaults. The
55
+ project flag can also be omitted if you've set it as the default with
56
+ `gcloud config set project`.
57
+
58
+ If your function handles events rather than HTTP requests, you'll need to
59
+ replace `--trigger-http` with a different trigger. For details, see the
60
+ [reference documentation](https://cloud.google.com/sdk/gcloud/reference/functions/deploy)
61
+ for `gcloud functions deploy`.
62
+
63
+ To update your deployment, just redeploy using the same function name.
64
+
65
+ ### Configuring Cloud Functions deployments
66
+
67
+ The Functions Framework provides various configuration parameters, described in
68
+ {file:docs/running-a-functions-server.md Running a Functions Server}.
69
+ If you want to set any of these parameters beyond the source file and target,
70
+ you must set environment variables. For example, to limit logging to WARN level
71
+ and above, set `FUNCTION_LOGGING_LEVEL` to `WARN` when deploying:
72
+
73
+ ```sh
74
+ gcloud functions deploy $YOUR_FUNCTION_NAME --project=$YOUR_PROJECT_ID \
75
+ --runtime=ruby26 --trigger-http --source=$YOUR_FUNCTION_SOURCE \
76
+ --entry-point=$YOUR_FUNCTION_TARGET \
77
+ --set-env-vars=FUNCTION_LOGGING_LEVEL=WARN
78
+ ```
79
+
80
+ Consult the table in
81
+ {file:docs/running-a-functions-server.md Running a Functions Server}
82
+ for a list of the environment variables that can be set.
83
+
84
+ ## Deploying to Cloud Run
85
+
86
+ Google Cloud Run is Google's managed compute platform for deploying and scaling
87
+ containerized applications quickly and securely. It can run any container-based
88
+ workload, including a containerized function.
89
+
90
+ Cloud Run has a hosted fully-managed option that runs on Google's infrastructure
91
+ and monitors and scales your application automatically, and a self-managed or
92
+ on-prem option called Cloud Run for Anthos that runs atop Kubernetes. Both
93
+ flavors use the same general interface and can run functions in the same way.
94
+ This tutorial is written for the managed option, but it should not be difficult
95
+ to adapt it if you have an Anthos installation.
96
+
97
+ ### Building an image for your function
98
+
99
+ First, build a Docker image containing your function. Following is a simple
100
+ Dockerfile that you can use as a starting point. Feel free to adjust it to the
101
+ needs of your project:
102
+
103
+ ```
104
+ FROM ruby:2.6
105
+ WORKDIR /app
106
+ COPY . .
107
+ RUN gem install --no-document bundler \
108
+ && bundle config --local frozen true \
109
+ && bundle config --local without "development test" \
110
+ && bundle install
111
+ ENTRYPOINT ["bundle", "exec", "functions-framework-ruby"]
112
+ ```
113
+
114
+ You can test your image locally using the steps described under
115
+ {file:docs/running-a-functions-server.md Running a Functions Server}.
116
+
117
+ When your Dockerfile is ready, you can use
118
+ [Cloud Build](https://cloud.google.com/cloud-build) to build it and store the
119
+ image in your project's container registry.
120
+
121
+ ```sh
122
+ gcloud builds submit --tag=gcr.io/$YOUR_PROJECT_ID/$YOUR_APP_NAME:$YOUR_BUILD_ID .
123
+ ```
124
+
125
+ You must use your project ID, but you can choose an app name and build ID. The
126
+ command may ask you for permission to enable the Cloud Build API for the project
127
+ if it isn't already enabled.
128
+
129
+ ### Deploying an image to Cloud Run
130
+
131
+ To deploy to Cloud Run, specify the same image URL that you built above. For
132
+ example:
133
+
134
+ ```sh
135
+ gcloud run deploy $YOUR_APP_NAME --project=$YOUR_PROJECT_ID \
136
+ --image=gcr.io/$YOUR_PROJECT_ID/$YOUR_APP_NAME:$YOUR_BUILD_ID \
137
+ --platform=managed --allow-unauthenticated --region=us-central1 \
138
+ --set-env-vars=FUNCTION_SOURCE=$YOUR_SOURCE,FUNCTION_TARGET=$YOUR_TARGET
139
+ ```
140
+
141
+ You can omit the `--project` flag if you've already set it as the default with
142
+ `gcloud config set project`.
143
+
144
+ The command may ask you for permission to enable the Cloud Run API for the
145
+ project, if it isn't already enabled.
146
+
147
+ At the end of the deployment process, the command will display the hostname for
148
+ the Cloud Run service. You can use that hostname to send test requests to your
149
+ deployed function.
150
+
151
+ ### Configuring Cloud Run deployments
152
+
153
+ Note that our Dockerfile's entrypoint did not pass any source file or target
154
+ name to the Functions Framework. If these are not specified, the Framework will
155
+ use the source `.app.rb` and the target `function` by default. To use different
156
+ values, you need to set the appropriate environment variables when deploying, as
157
+ illustrated above with the `FUNCTION_SOURCE` and `FUNCTION_TARGET` variables.
158
+
159
+ Source and target are not the only configuration parameters available. The
160
+ various parameters, along with their environment variables, are described in
161
+ {file:docs/running-a-functions-server.md Running a Functions Server}.
162
+ Any of these can be specified in the `--set-env-vars` flag when you deploy to
163
+ Google Cloud Run.
164
+
165
+ It is also possible to "hard-code" configuration into the Dockerfile, by setting
166
+ environment variables in the Dockerfile, or adding flags to the entrypoint.
167
+ However, it is often better practice to keep your Dockerfile "generic", and set
168
+ configuration environment variables during deployment, so that you do not need
169
+ to rebuild your Docker image every time you want to change configuration.