diff --git a/README.md b/README.md index 62adb93..c61bfb8 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,6 @@ _Disclaimer: This is not an official Google product. It is not and will not be maintained by Google, and is not part of Google Cloud Functions project. There is no guarantee of any kind, including that it will work or continue to work, or that it will supported in any way._ ## Background - At the time of writing, Google Cloud Functions only supports a Node.js based runtime. Running non-JavaScript code is only officially supported in the form of native Node modules and running subprocesses. Node will always be handling the requests. Wrapping Go code in a native Node module will require writing complicated foreign function interfaces (FFIs). FFIs are notoriously difficult to get right and are an easy way to introduce memory leaks as well as security and stability issues. Using a subprocess is much easier to get right, but it introduces the overhead of interprocess communication (IPC) and context switches. In both cases, the requests are proxied between Node and Go which typically involves extra copies and translations. ## 100% Pure Go Request Path @@ -13,11 +12,20 @@ After an initial bootstrap step, the only thing running is a pure Go binary. Thi A native module calls execve(2) without the normal preceding fork(2) call when the user module is imported. Open socket FDs are preserved and handled by the new Go process to ensure a smooth transition. The new Go process then pretends to be Node. ## Requirements +There are two supported environments: +### Native +For advanced users who don't like VirtualBox. * Linux or macOS development environment (The native Node module depends on Linux/macOS specific behavior.) * Go 1.5 or above * Node.js v0.10 or above and node-gyp * Make and GCC * If you are using macOS then as of right now you need the FULL xcode installed. Command line tools alone will not work. +### Vagrant +Compatible with Windows, macOS and Linux and easier than native development. +* [Vagrant](https://www.vagrantup.com/downloads.html) +* [VirtualBox](https://www.virtualbox.org/wiki/Downloads) or a compatable provider + * Optional: [vagrant-vbguest](https://github.com/dotless-de/vagrant-vbguest) +Support for other Vagrant providers is in the works. ## Hello, world! A demo hello world example is included. To try it out, simply skip to the [Deployment](#deployment) section. @@ -31,6 +39,9 @@ Run ```make test``` to compile your code and start the test server. Open ```http ## Deployment Run ```make``` to compile and package your code. Upload the generated ```function.zip``` file to Google Cloud Functions as an HTTP trigger function. +### Vagrant +Run ```vagrant up``` to start the envirement. Run ```vagrant ssh``` to connect to the envirement. Run ```cd /vagrant``` to access the respority files. The instructions in [Local Testing](#local-testing) and [Deployment](#deployment) should now work. + ## Limitations * This has only been tested for HTTP trigger functions. Non-HTTP trigger functions will use a different endpoint (not ```/execute```). * Logging is not supported (yet). @@ -39,7 +50,6 @@ Run ```make``` to compile and package your code. Upload the generated ```functio Some versions of Node.js (especially those packaged for Ubuntu) name their main binary ```nodejs``` instead of ```node```. The symptom of this problem is an error about the ```node``` binary not being found in the path even though Node.js is installed. This can be fixed with ```sudo ln -s $(which nodejs) /usr/local/bin/node```. ## License - Copyright 2017, Google, Inc. Licensed under the Apache License, Version 2.0 diff --git a/Vagrantfile b/Vagrantfile new file mode 100644 index 0000000..99ffe17 --- /dev/null +++ b/Vagrantfile @@ -0,0 +1,22 @@ +# Copyright 2017 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +Vagrant.configure("2") do |config| + config.vm.provider :virtualbox do |_, override| + override.vm.box = "debian/stretch64" + override.vm.network "forwarded_port", guest: 8080, host: 8080, auto_correct: true + override.vm.synced_folder ".", "/vagrant", type: "virtualbox" + end + config.vm.provision :shell, path: "provision.sh" +end diff --git a/provision.sh b/provision.sh new file mode 100644 index 0000000..7133204 --- /dev/null +++ b/provision.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash +# Copyright 2017 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -euo pipefail + +apt-get update +apt-get dist-upgrade -y +apt-get install -y zip curl build-essential + +curl -sL https://deb.nodesource.com/setup_6.x | bash - +apt-get install -y nodejs node-gyp npm + +mkdir /tmp/go +cd /tmp/go +wget https://godeb.s3.amazonaws.com/godeb-amd64.tar.gz +tar -xvzf godeb-amd64.tar.gz +./godeb install +cd +rm -rf /tmp/go