Skip to content
This repository has been archived by the owner on Jun 11, 2020. It is now read-only.

Initial support for Vagrant local development #14

Merged
merged 1 commit into from
Aug 29, 2017
Merged
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
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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).
Expand All @@ -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
Expand Down
22 changes: 22 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions provision.sh
Original file line number Diff line number Diff line change
@@ -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