├── example ├── .env └── Vagrantfile ├── Rakefile ├── lib ├── vagrant-env │ ├── version.rb │ ├── plugin.rb │ └── config.rb └── vagrant-env.rb ├── CHANGELOG.md ├── .gitignore ├── Gemfile ├── vagrant-env.gemspec ├── LICENSE.txt └── README.md /example/.env: -------------------------------------------------------------------------------- 1 | BOX_NAME=hashicorp/precise64 2 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'bundler/setup' 3 | Bundler::GemHelper.install_tasks 4 | -------------------------------------------------------------------------------- /lib/vagrant-env/version.rb: -------------------------------------------------------------------------------- 1 | module VagrantPlugins 2 | module Env 3 | VERSION = "0.0.3" 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /lib/vagrant-env.rb: -------------------------------------------------------------------------------- 1 | require "dotenv" 2 | require "vagrant-env/plugin" 3 | 4 | module VagrantPlugins 5 | module Env; end 6 | end 7 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 0.0.3 2 | 3 | * [enhancement] Support for loading .env in a different directory ([#3](https://github.com/gosuri/vagrant-env/pull/3)) 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /Gemfile.lock 4 | /_yardoc/ 5 | /coverage/ 6 | /doc/ 7 | /pkg/ 8 | /spec/reports/ 9 | /tmp/ 10 | *.bundle 11 | *.so 12 | *.o 13 | *.a 14 | mkmf.log 15 | .vagrant 16 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in vagrant-env.gemspec 4 | gemspec 5 | 6 | group :development do 7 | gem "vagrant", git: "https://github.com/mitchellh/vagrant.git" 8 | gem 'rake', '~> 10.0' 9 | end 10 | 11 | group :plugins do 12 | gem "vagrant-env", path: "." 13 | end 14 | -------------------------------------------------------------------------------- /example/Vagrantfile: -------------------------------------------------------------------------------- 1 | # -*- mode: ruby -*- 2 | # vi: set ft=ruby : 3 | 4 | Vagrant.configure(2) do |config| 5 | config.env.enable 6 | config.vm.box = ENV['BOX_NAME'] 7 | config.vm.network "forwarded_port", guest: 80, host: 8080, auto_correct: true 8 | 9 | config.vm.provider "virtualbox" do |v| 10 | v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"] 11 | v.customize ["modifyvm", :id, "--natdnsproxy1", "on"] 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /lib/vagrant-env/plugin.rb: -------------------------------------------------------------------------------- 1 | begin 2 | require "vagrant" 3 | rescue LoadError 4 | raise "The Vagrant ENV plugin must be run within Vagrant." 5 | end 6 | 7 | # This is a sanity check to make sure no one is attempting to install 8 | # this into an early Vagrant version. 9 | if Vagrant::VERSION < "1.2.0" 10 | raise "The Vagrant ENV plugin is only compatible with Vagrant 1.2+" 11 | end 12 | 13 | module VagrantPlugins 14 | module Env 15 | class Plugin < Vagrant.plugin("2") 16 | name "ENV" 17 | description <<-DESC 18 | Vagrant plugin to load environment variables from .env into ENV 19 | DESC 20 | 21 | config "env" do 22 | require_relative "config" 23 | Config 24 | end 25 | end 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /lib/vagrant-env/config.rb: -------------------------------------------------------------------------------- 1 | require 'dotenv' 2 | 3 | module VagrantPlugins 4 | module Env 5 | class Config < Vagrant.plugin("2", :config) 6 | 7 | # Simple interface: 8 | # config.env.enable __FILE__ 9 | def enable(vagrantfile = nil) 10 | if vagrantfile 11 | load File.join File.dirname(vagrantfile), '.env' 12 | else 13 | # The default is .env in the current directory - but that may not be 14 | # the same directory that the Vagrantfile is in 15 | # https://github.com/gosuri/vagrant-env/issues/2 16 | load 17 | end 18 | end 19 | 20 | # Lower-level methods - proxy to Dotenv: 21 | def load(*filenames) 22 | Dotenv::load *filenames 23 | end 24 | 25 | def load!(*filenames) 26 | Dotenv::load! *filenames 27 | end 28 | 29 | def overload(*filenames) 30 | Dotenv::overload *filenames 31 | end 32 | 33 | end 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /vagrant-env.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'vagrant-env/version' 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = "vagrant-env" 8 | spec.version = VagrantPlugins::Env::VERSION 9 | spec.authors = ["Greg Osuri"] 10 | spec.email = ["greg@overclock.io"] 11 | spec.summary = %q{Vagrant plugin to load environment variables from .env into ENV} 12 | spec.description = %q{Vagrant plugin to load environment variables from .env into ENV} 13 | spec.homepage = "http://github.com/gosuri/vagrant-env" 14 | spec.license = "MIT" 15 | spec.files = %w(README.md LICENSE.txt 16 | vagrant-env.gemspec 17 | lib/vagrant-env.rb 18 | lib/vagrant-env/version.rb 19 | lib/vagrant-env/config.rb 20 | lib/vagrant-env/plugin.rb) 21 | spec.require_paths = ["lib"] 22 | spec.add_runtime_dependency "dotenv", "~> 0.9" 23 | spec.add_development_dependency "bundler", "~> 1.6" 24 | end 25 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Greg Osuri 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Vagrant ENV Plugin 2 | 3 | This is a [Vagrant](http://www.vagrantup.com) plugin to load environment variables from .env into ENV. More information on why and how we use this plugin is detailed in this [post](http://www.gregosuri.com/2014/08/31/introducing-vagrant-env-plugin/) 4 | 5 | ## Requirements 6 | 7 | * Vagrant 1.7.4 or higher 8 | 9 | ## Installation 10 | 11 | Install the lastest version using standard vagrant plugin installation method: 12 | 13 | ```sh 14 | $ vagrant plugin install vagrant-env 15 | ``` 16 | 17 | To install an older version of the plugin use `vagrant plugin install vagrant-env --plugin-version VERSION` 18 | 19 | ## Usage 20 | 21 | After installing, add your application configuration to your .env file in the root of your project 22 | 23 | ```sh 24 | $ echo BOX_NAME=hashicorp/precise64 > .env 25 | ``` 26 | 27 | Create a a Vagrantfile that looks like the following, ensure to add `config.env.enable` and fill in your information where necessary. Check out [example](example/) for reference 28 | 29 | ```ruby 30 | Vagrant.configure("2") do |config| 31 | config.env.enable # enable the plugin 32 | config.vm.box = ENV['BOX_NAME'] 33 | end 34 | ``` 35 | 36 | Additionally, You may also add export in front of each line so you can source the file in bash: 37 | 38 | ```sh 39 | export AWS_ACCESS_ID=YOURACCESSIDGOESHERE 40 | export AWS_SECRET_ACCESS_ID=YOURSECRETKEYGOESHERE 41 | ``` 42 | 43 | ### Should I commit my .env file? 44 | 45 | It is recommended that you store development-only settings in your .env file, and commit it to your repository. Make sure that all your credentials for your development environment are different from your other deployments. This makes it easy for other developers to get started on your project, without compromising your credentials for other environments. 46 | 47 | ## Contributing 48 | 49 | 1. Fork it ( https://github.com/gosuri/vagrant-env/fork ) 50 | 2. Create your feature branch (`git checkout -b my-new-feature`) 51 | 3. Commit your changes (`git commit -am 'Add some feature'`) 52 | 4. Push to the branch (`git push origin my-new-feature`) 53 | 5. Create a new Pull Request 54 | --------------------------------------------------------------------------------