• Stars
    star
    334
  • Rank 126,264 (Top 3 %)
  • Language
    Ruby
  • License
    Apache License 2.0
  • Created about 11 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Vagrant provider for GCE.

Vagrant Google Compute Engine (GCE) Provider

Gem Version

This is a Vagrant plugin that adds an Google Compute Engine (GCE) provider to Vagrant, allowing Vagrant to control and provision instances in GCE.

NOTE: The plugin is currently looking for maintainers, please contact @temikus.

Features

  • Boot Google Compute Engine instances.
  • SSH into the instances.
  • Provision the instances with any built-in Vagrant provisioner.
  • Synced folder support via Vagrant's rsync action.
  • Define zone-specific configurations so Vagrant can manage machines in multiple zones.

Requirements

  • Google Cloud Platform (GCP) account,
  • a GCP project with:
    • Google Compute Engine API enabled
    • Your public SSH key added as GCE metadata.
  • Vagrant 2.0.3+

Google Cloud Platform Setup

Do the following:

  1. Log in with your Google Account and go to Google Cloud Platform and click on the Try it free button.
  2. Create a new project and remember to record the Project ID
  3. Enable the Google Compute Engine API for your project in the API console. If prompted, review and agree to the terms of service.
  4. Install the Cloud SDK
  5. Run `gcloud auth appplication-default login to create your credentials. (Alternatively, you may use a service account, see Using a Service Account section).
  6. Add the public SSH key you're going to use to GCE Metadata in Compute -> Compute Engine -> Metadata section of the console, SSH Keys tab. (Read the SSH Support readme section for more information.)

Using a Service Account

The `appplication-default login method is intended to be used for developing code on a local environment - this is typically Vagrant's use-case as well. However, if this is not your use-case, you will want to use a credential not tied to your local environment: a service account.

To use a service account:

  1. While still in the API & Services, go to Credentials subsection, and click Create credentials -> Service account.
  2. Create a Service Account with any name (f.e. vagrant) and grant it a Compute Admin role.
  3. Open the new service account page and click on the Keys tab. Click Add key -> Create new key, choose JSON. Download the JSON private key and save this file in a secure and reliable location.

Then include the private key in your Vagrantfile's provider block as a google_json_key_location attribute:

Vagrant.configure("2") do |config|
  # ... other stuff

  config.vm.provider :google do |google|
    google.google_project_id = "YOUR_GOOGLE_CLOUD_PROJECT_ID"
    google.google_json_key_location = "/path/to/your/private-key.json"
  end
end

Vagrant Setup

Install as a Vagrant plugin:

vagrant plugin install vagrant-google

Usage

Make a Vagrantfile that looks like the following, filling in your information where necessary:

Vagrant.configure("2") do |config|
  config.vm.box = "google/gce"

  config.vm.provider :google do |google, override|
    google.google_project_id = "YOUR_GOOGLE_CLOUD_PROJECT_ID"
    google.image_family = 'ubuntu-2004-lts'

    override.ssh.username = "USERNAME"
    override.ssh.private_key_path = "~/.ssh/id_rsa"
  end

end

Run:

vagrant up --provider=google

This will start the latest version of Ubuntu 20.04 LTS instance in the us-central1-f zone, with an n1-standard-1 machine, and the "default" network within your project. And assuming your SSH information (see below) was filled in properly within your Vagrantfile, SSH and provisioning will work as well.

Note that normally a lot of this boilerplate is encoded within the box file, but the box file used for the quick start, the "google" box, has no preconfigured defaults.

SSH Support

In order for SSH to work properly to the GCE VM, you will first need to add your public key to the GCE metadata service for the desired VM user account. When a VM first boots, a Google-provided daemon is responsible for talking to the internal GCE metadata service and creates local user accounts and their respective ~/.ssh/authorized_keys entries. Most new GCE users will use the Cloud SDK gcloud compute utility when first getting started with GCE. This utility has built in support for creating SSH key pairs, and uploading the public key to the GCE metadata service. By default, gcloud compute creates a key pair named ~/.ssh/google_compute_engine[.pub].

Note that you can use the more standard ~/.ssh/id_rsa[.pub] files, but you will need to manually add your public key to the GCE metadata service so your VMs will pick up the key. Note that the public key is typically prefixed with the username, so that the daemon on the VM adds the public key to the correct user account.

Additionally, you will probably need to add the key and username to override settings in your Vagrantfile like so:

config.vm.provider :google do |google, override|

    #...google provider settings are skipped...

    override.ssh.username = "testuser"
    override.ssh.private_key_path = "~/.ssh/id_rsa"

    #...google provider settings are skipped...

end

See the links below for more help with SSH and GCE VMs.

Box Format

Every provider in Vagrant must introduce a custom box format. This provider introduces google boxes. You can view an example box in example_boxes/. That directory also contains instructions on how to build a box.

The box format is basically just the required metadata.json file along with a Vagrantfile that does default settings for the provider-specific configuration for this provider.

Configuration

This provider exposes quite a few provider-specific configuration options:

  • google_json_key_location - The location of the JSON private key file matching your Service Account. (Can also be configured with GOOGLE_JSON_KEY_LOCATION environment variable.)
  • google_project_id - The Project ID for your Google Cloud Platform account. (Can also be configured with GOOGLE_PROJECT_ID environment variable.)
  • image - The image name to use when booting your instance.
  • image_family - Specify an "image family" to pull the latest image from. For example: centos-7 will pull the most recent CentOS 7 image. For more info, refer to Google Image documentation.
  • image_project_id - The ID of the GCP project to search for the image or image_family. For example: centos-cloud for Centos 7/8/Stream image families.
  • instance_group - Unmanaged instance group to add the machine to. If one doesn't exist it will be created.
  • instance_ready_timeout - The number of seconds to wait for the instance to become "ready" in GCE. Defaults to 20 seconds.
  • machine_type - The machine type to use. The default is "n1-standard-1".
  • disk_size - The disk size in GB. The default is 10.
  • disk_name - The disk name to use. If the disk exists, it will be reused, otherwise created.
  • disk_type - Whether to use Standard disk or SSD disk. Use either pd-ssd or pd-standard.
  • autodelete_disk - Boolean whether to delete the disk when the instance is deleted or not. Default is true.
  • metadata - Custom key/value pairs of metadata to add to the instance.
  • name - The name of your instance. The default is "i-yyyymmddhh-randomsd", e.g. 10/08/2015 13:15:15 is "i-2015081013-15637fda".
  • network - The name of the network to use for the instance. Default is "default".
  • network_project_id - The ID of the GCP project for the network and subnetwork to use for the instance. Default is google_project_id.
  • subnetwork - The name of the subnetwork to use for the instance.
  • tags - An array of tags to apply to this instance.
  • labels - Custom key/value pairs of labels to add to the instance.
  • zone - The zone name where the instance will be created.
  • can_ip_forward - Boolean whether to enable IP Forwarding.
  • external_ip - The external IP address to use (supports names). Set to false to not assign an external address.
  • network_ip - The internal IP address to use. Default is to use next available address.
  • use_private_ip - Boolean whether to use private IP for SSH/provisioning. Default is false.
  • preemptible - Boolean whether to enable preemptibility. Default is false.
  • auto_restart - Boolean whether to enable auto_restart. Default is true.
  • on_host_maintenance - What to do on host maintenance. Can be set to MIGRATE or TERMINATE Default is MIGRATE.
  • scopes or service_accounts - An array of OAuth2 account scopes for services that the instance will have access to. Those can be both full API scopes, just endpoint aliases (the part after ...auth/), and gcloud utility aliases, for example: ['storage-full', 'bigquery', 'https://www.googleapis.com/auth/compute'].
  • service_account - The IAM service account email to use for the instance.
  • additional_disks - An array of additional disk configurations. disk_size is default to 10GB; disk_name is default to name + "-additional-disk-#{index}"; disk_type is default to pd-standard; autodelete_disk is default to true. Here is an example of configuration.
      [{
       :image_family => "google-image-family",
       :image => nil,
       :image_project_id => "google-project-id",
       :disk_size => 20,
       :disk_name => "google-additional-disk-0",
       :disk_type => "pd-standard",
       :autodelete_disk => true
      }]
  • accelerators - An array of accelerator configurations. type is the accelerator type (e.g. nvidia-tesla-k80); count is the number of accelerators and defaults to 1. Note that only TERMINATE is supported for on_host_maintenance; this should be set explicitly, since the default is MIGRATE.
    google.accelerators = [{
      :type => "nvidia-tesla-k80",
      :count => 2
    }]
    
    google.on_host_maintenance = "TERMINATE"
  • enable_secure_boot - For Shielded VM, whether to enable Secure Boot.
  • enable_vtpm - For Shielded VM, whether to enable vTPM.
  • enable_integrity_monitoring - For Shielded VM, whether to enable Integrity monitoring.
  • resource_policies - Adds Resource Policies to given instance. These can be set like typical provider-specific configuration:
Vagrant.configure("2") do |config|
  # ... other stuff

  config.vm.provider :google do |google|
    google.google_project_id = "YOUR_GOOGLE_CLOUD_PROJECT_ID"
    google.google_json_key_location = "/path/to/your/private-key.json"
  end
end

In addition to the above top-level configs, you can use the zone_config method to specify zone-specific overrides within your Vagrantfile. Note that the top-level zone config must always be specified to choose which zone you want to actually use, however. This looks like this:

Vagrant.configure("2") do |config|

  config.vm.box = "google/gce"

  config.vm.provider :google do |google|
    google.google_project_id = "YOUR_GOOGLE_CLOUD_PROJECT_ID"
    google.google_json_key_location = "/path/to/your/private-key.json"

    # Make sure to set this to trigger the zone_config
    google.zone = "us-central1-f"

    google.zone_config "us-central1-f" do |zone1f|
        zone1f.name = "testing-vagrant"
        zone1f.image = "debian-9-stretch-v20211105"
        zone1f.machine_type = "n1-standard-4"
        zone1f.zone = "us-central1-f"
        zone1f.metadata = {'custom' => 'metadata', 'testing' => 'foobarbaz'}
        zone1f.scopes = ['bigquery', 'monitoring', 'https://www.googleapis.com/auth/compute']
        zone1f.tags = ['web', 'app1']
    end
  end
end

The zone-specific configurations will override the top-level configurations when that zone is used. They otherwise inherit the top-level configurations, as you would expect.

There are a few example Vagrantfiles located in the vagrantfile_examples/ directory.

Networks

Networking features in the form of config.vm.network are not supported with vagrant-google, currently. If any of these are specified, Vagrant will emit a warning, but will otherwise boot the GCE machine.

Synced Folders

Since plugin version 2.0, this is implemented via built-in SyncedFolders action. See Vagrant's rsync action documentation for more info.

Automatic shutdown

To save money you may want to ensure you don't forget to shut down your instances when you stop using them.

A very basic solution for this is to use Vagrant's provisioning feature to plan automatic shutdown of the vm after given time after each vagrant up:

# Plan automatic shutdown of machine to prevent unwanted costs
config.vm.provision "auto-shutdown", type: "shell", run: "always",
  inline: "shutdown -P +480" # = 60 minutes * 8 hours

Print external IP

You may want to know your machine's external IP f.e. to put it in your Ansible inventory or open the app you deploy in it in your browser.

To automate printing it IP you can also use the Vagrant's provisioning feature:

# Print the external IP
config.vm.provision "print-ip", type: "shell", run: "always",
  inline: "echo External IP: $(curl -s icanhazip.com)"

Development

To work on the vagrant-google plugin, clone this repository, and use Bundler to get the dependencies:

$ bundle

Once you have the dependencies, verify the unit tests pass with rake:

$ bundle exec rake

If those pass, you're ready to start developing the plugin. You can test the plugin without installing it into your Vagrant environment by just creating a Vagrantfile in the top level of this directory (it is ignored by git), and use bundler to execute Vagrant:

$ bundle exec vagrant up --provider=google

Acceptance testing

Work-in-progress: Acceptance tests are based on vagrant-spec library which is currently under active development so they may occasionally break.

Before you start acceptance tests, you'll need to set the authentication shell variables accordingly:

export GOOGLE_PROJECT_ID="your-google-cloud-project-id"
export GOOGLE_JSON_KEY_LOCATION="/full/path/to/your/private-key.json"

export GOOGLE_SSH_USER="testuser"
export GOOGLE_SSH_KEY_LOCATION="/home/testuser/.ssh/id_rsa"

After, you can run acceptance tests by running the full task in acceptance namespace:

$ bundle exec rake acceptance:full

IMPORTANT NOTES:

  • Since acceptance tests spin up instances on GCE, the whole suite may take 20+ minutes to run.
  • Since those are live instances, you will be billed for running them.

Changelog

See CHANGELOG.md

License

Apache 2.0; see LICENSE for details.

More Repositories

1

mapstructure

Go library for decoding generic map values into native Go structures and vice versa.
Go
7,685
star
2

gox

A dead simple, no frills Go cross compile tool
Go
4,594
star
3

vagrant-aws

Use Vagrant to manage your EC2 and VPC instances.
Ruby
2,609
star
4

nixos-config

My NixOS configurations.
Nix
1,791
star
5

cli

A Go library for implementing command-line interfaces.
Go
1,724
star
6

libxev

libxev is a cross-platform, high-performance event loop that provides abstractions for non-blocking IO, timers, events, and more and works on Linux (io_uring or epoll), macOS (kqueue), and Wasm + WASI. Available as both a Zig and C API.
Zig
1,589
star
7

gon

Sign, notarize, and package macOS CLI tools and applications written in any language. Available as both a CLI and a Go library.
Go
1,461
star
8

go-ps

Find, list, and inspect processes from Go (golang).
Go
1,449
star
9

go-homedir

Go library for detecting and expanding the user's home directory without cgo.
Go
1,390
star
10

go-server-timing

Go (golang) library for creating and consuming HTTP Server-Timing headers
Go
861
star
11

hashstructure

Get hash values for arbitrary values in Go (golang).
Go
745
star
12

goamz

Golang Amazon Library
Go
673
star
13

golicense

Scan and analyze OSS dependencies and licenses from compiled Go binaries
Go
666
star
14

ioprogress

Go (golang) package for progress bars around io.Reader/Writers.
Go
502
star
15

go-mruby

Go (golang) bindings to mruby.
Go
468
star
16

panicwrap

panicwrap is a Go library for catching and handling panics in Go applications.
Go
443
star
17

advent-2021-sql

Advent of Code 2021 using SQL (PostgreSQL-flavored)
PLpgSQL
436
star
18

boot2docker-vagrant-box

Packer scripts to build a Vagrant-compatible boot2docker box.
Smarty
425
star
19

copystructure

Go (golang) library for deep copying values in Go.
Go
345
star
20

go-glint

Component-based UI-framework for command-line tools. Easily create highly dynamic CLI interfaces using shared, easily testable components.
Go
311
star
21

go-vnc

VNC client and server library for Go.
Go
283
star
22

colorstring

Go (golang) library for colorizing strings for terminal output.
Go
276
star
23

reflectwalk

reflectwalk is a Go library for "walking" complex structures, similar to walking a filesystem.
Go
274
star
24

virtualbox

[ABANDONED] Create and modify virtual machines in VirtualBox using pure ruby.
Ruby
244
star
25

vagrant-rackspace

Use Vagrant to manage Rackspace Cloud instances.
Ruby
234
star
26

protoc-gen-go-json

Protobuf compiler plugin to generate Go JSON Marshal/Unmarshal implementations for messages using jsonpb.
Go
225
star
27

pointerstructure

Go library for addressing and reading/writing a specific value within any Go structure using a string syntax.
Go
214
star
28

zig-overlay

Nix flake for the Zig compiler.
Nix
195
star
29

zig-js

Access the JS host environment from Zig compiled to WebAssembly.
Zig
178
star
30

dotfiles

My personal dotfiles.
Batchfile
176
star
31

protostructure

Encode and decode Go (golang) struct types via protocol buffers.
Go
172
star
32

consulstructure

Decode Consul data into Go (Golang) structures and watch for updates
Go
172
star
33

packer-ubuntu-12.04-docker

Packer template that builds images that are Docker-ready on Ubuntu 12.04.
Shell
157
star
34

zig-libgc

Zig-friendly library for interfacing with libgc (bdwgc) -- the Boehm-Demers-Weiser conservative garbage collector
Zig
156
star
35

zig-objc

Objective-C runtime bindings for Zig (Zig calling ObjC).
Zig
153
star
36

libflightplan

A library for reading and writing flight plans in various formats. Available as both a C and Zig library.
Zig
153
star
37

terraform-provider-multispace

Terraform Provider for cascading runs across multiple workspaces.
Go
147
star
38

multistep

multistep is a Go library for building up complex actions using discrete steps.
Go
146
star
39

go-z3

Go (golang) bindings to the Z3 SMT Solver
Go
138
star
40

go-sat

SAT solver written in Go (golang).
Go
136
star
41

go-wordwrap

A Go (golang) library for wrapping words in a string.
Go
107
star
42

vim-misc

My Vim configuration files.
Vim Script
99
star
43

middleware

Generalized middleware implementation for Ruby.
Ruby
94
star
44

go-fs

Filesystem library for Go, implementing FAT filesystems so far.
Go
88
star
45

zig-graph

Directed graph data structure for Zig
Zig
86
star
46

go-grpc-net-conn

Turn any gRPC stream into a Go `net.Conn` implementation.
Go
80
star
47

zig-libxml2

libxml2 built using Zig build system
Zig
76
star
48

lightcloud

Library for accessing Plurk's LightCloud distributed key-value store for Ruby
Ruby
75
star
49

tree-sitter-hcl

A tree-sitter grammar for HCL (HashiCorp Configuration Language), used by projects such as Terraform.
C
69
star
50

go-linereader

Golang package that reads lines from an io.Reader and puts them onto a channel.
Go
66
star
51

veewee-to-packer

A tool for converting Veewee templates into Packer templates.
Ruby
65
star
52

tree-sitter-proto

A tree-sitter grammar for protocol buffer files (proto3).
C
63
star
53

vagrant-rake

A Vagrant plugin to execute `rake` commands from the host in the VM
Ruby
62
star
54

libvirt-rb

[ABANDONED] A ruby client library providing an interface to libvirt via FFI.
Ruby
59
star
55

go-testing-interface

Go (golang) library to expose *testing.T as an interface.
Go
58
star
56

patchstructure

Go library for representing and applying patches to modify existing Go structures
Go
55
star
57

squire

Go
55
star
58

go-libucl

Bindings to libucl from Go (golang).
Go
54
star
59

go-finger

Finger protocol library
Go
54
star
60

go-bnet

Go (golang) client for the Battle.net API
Go
52
star
61

libssh2-ruby

libssh2 bindings for Ruby
Ruby
47
star
62

iochan

A Go library for turning `io.Reader` into channels.
Go
43
star
63

prefixedio

Golang library that demultiplexes line-oriented data from an io.Reader into multiple io.Readers based on a prefix.
Go
42
star
64

flask-nix-example

Dockerfile
36
star
65

tlaplus-radix-tree

TLA+ modules, specifications, and models for Radix trees.
TLA
33
star
66

caststructure

A Go library that provides functions for downcasting types, composing values dynamically, and more.
Go
32
star
67

virtuoso

Dead simple virtual machine management over many hypervisors.
Ruby
30
star
68

hash_ring

Consistent hashing in Ruby. Ported from Amir Sailhefendic's hash_ring python library.
Ruby
28
star
69

terraform-aws-dynamic-keys

Terraform module that dynamically generates a public/private keypair.
HCL
26
star
70

go-spdx

Golang library for listing and looking up licenses using SPDX IDs.
Go
23
star
71

ruburple

A ruby interface to libpurple. Copied for git.
C
20
star
72

iorpc

Golang io interfaces across an RPC connection.
Go
18
star
73

fusion-m1-514-repro

Makefile
17
star
74

zig-build-macos-sdk

macOS SDK package for Zig build.
C
15
star
75

omniconfig

Flexible configuration for your Ruby applications and libraries.
Ruby
14
star
76

zig-libuv

Zig bindings for libuv. Also a build script to build libuv from scratch using only Zig (for easy cross-compilation, integration with Zig, etc.).
Zig
13
star
77

terraform-aws-fastai

Terraform module to create Fast.ai course instance.
HCL
12
star
78

radar

Easily report errors in your libraries and applications any way you want!
Ruby
12
star
79

zig-build-libxml2

The libxml2 library built and packaged for the Zig build system. These are not Zig language bindings to the library.
C
11
star
80

tiad-demo

Demo for The Incredible Automation Day in Paris.
Shell
10
star
81

waypoint-helm

WIP
Smarty
9
star
82

boto-route53

Route53 API built on top of Boto
Python
8
star
83

fogli

An efficient, simple, and intuitive Facebook Open Graph library
Ruby
8
star
84

minitest-mark

Proof of concept minitest extension to add test marking.
Ruby
7
star
85

vagrant-downloads

The Vagrant downloads website.
Ruby
7
star
86

minitest-parallel

Proof of concept to run your minitest tests in parallel.
Ruby
6
star
87

goconf

This is a copy of http://code.google.com/p/goconf/
Go
6
star
88

packer-go-bootcamp

Packer templates for the Go Bootcamp images.
Shell
6
star
89

larubyconf-vagrant-examples

Examples of using Vagrant from LARubyConf
Ruby
6
star
90

xidl

Parses XIDL files into Ruby objects.
Ruby
5
star
91

bintray-download-site

Simple Rack app for creating easy downloads for your Bintray packages.
Ruby
5
star
92

terraform-aws-vpc

Temporary, testing something, ignore this.
HCL
5
star
93

minitest-speed

Proof of concept speed tests using minitest.
Ruby
5
star
94

zig-build-xcode-frameworks

Exposing hexops/xcode-frameworks to the Zig package manager to work around some bugs.
Zig
5
star
95

homebrew-gon

Homebrew Tap for Gon (github.com/mitchellh/gon)
Ruby
4
star
96

osext

Copy of https://bitbucket.org/kardianos/osext
Go
4
star
97

minitest-funcarg

Proof of concept showing funcargs (style of DI) in minitest.
Ruby
4
star
98

lifeguard-random

Data source plugin for Lifeguard that generates random numbers.
Shell
4
star
99

go-bootcamp-remotecmds

My solution for the remotecmds problem for the Go Bootcamp I'm helping to instruct.
Go
4
star
100

lifeguard-graphite

Data source plugin for Lifeguard to query data from Graphite.
Shell
4
star