vagrant-envbash 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2e8ab55723262f531ef6679be94de55ad9a714eb
4
+ data.tar.gz: 6a4c2a45c3cd3afff207174183ac28e820500246
5
+ SHA512:
6
+ metadata.gz: 94fdba0131b7376d06f83b8a3fc72d92135028b43012c7479277bc8d0d628e084faf799dd56c717ebdd3e51043756630fe100bf52f87886cff1208e159f68a38
7
+ data.tar.gz: 9c589a1202f519b40307f7b0886dd33dff175e70a4b5f5caed5ed5cf95249c5d8f274622c524120a8f4ea35aed8bea2c73fe89e81af35c6d84eb187b80aef1c7
@@ -0,0 +1,14 @@
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
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ # We depend on Vagrant for development, but we don't add it as a
7
+ # gem dependency because we expect to be installed within the
8
+ # Vagrant environment itself using `vagrant plugin`.
9
+ gem "vagrant", git: "https://github.com/mitchellh/vagrant.git"
10
+ end
11
+
12
+ group :plugins do
13
+ gem "vagrant-envbash", path: "."
14
+ end
15
+
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2016 Aron Griffis
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.
@@ -0,0 +1,152 @@
1
+ # Vagrant envbash plugin
2
+
3
+ This is a [Vagrant](http://www.vagrantup.com) plugin to load environment
4
+ variables from `env.bash`. Putting settings in `env.bash` provides a single
5
+ source of development configuration for Vagrant (e.g. AWS secrets that shouldn't
6
+ be committed to source control) and the application under development. This is
7
+ especially important for web apps that adhere to the
8
+ [twelve-factor methodology](http://12factor.net/).
9
+
10
+ This plugin was inspired by [vagrant-env](https://github.com/gosuri/vagrant-env)
11
+ with the primary difference that `env.bash` is a proper Bash script that will be
12
+ executed by `/bin/bash`, so it can contain conditionals, multi-line strings,
13
+ etc. This also makes it easier to source into the shell in the Vagrant guest as
14
+ application configuration.
15
+
16
+ This plugin also adds a command `vagrant env` to inspect how `env.bash` modifies
17
+ the environment.
18
+
19
+ ## Installation
20
+
21
+ Install using Vagrant's plugin system:
22
+
23
+ ```
24
+ vagrant plugin install vagrant-envbash
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ With the plugin installed, Vagrant will look for `env.bash` in the same
30
+ directory as `Vagrantfile`. If found, Vagrant will execute the file as a Bash
31
+ script. Any environment variables that are exported in the file will be updated
32
+ in `ENV`. Additionally any variables that are unset will be removed from `ENV`.
33
+
34
+ The plugin runs very early in the Vagrant execution, so `ENV` is fully updated
35
+ before executing configuration in `Vagrantfile`. This means that `env.bash` is
36
+ the ideal place to put development configuration such as AWS secrets for
37
+ [vagrant-aws](https://github.com/mitchellh/vagrant-aws) that shouldn't be
38
+ committed to source control in Vagrantfile.
39
+
40
+ ## Examples
41
+
42
+ ### Example of AWS secrets in `env.bash`
43
+
44
+ With this `Vagrantfile`:
45
+
46
+ ```
47
+ Vagrant.configure("2") do |config|
48
+ config.vm.provider :aws do |aws, override|
49
+ override.vm.box = "dummy"
50
+ aws.access_key_id = ENV['AWS_ACCESS_KEY_ID']
51
+ aws.secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
52
+ aws.keypair_name = ENV['AWS_KEYPAIR_NAME']
53
+ aws.ami = ENV.fetch('AWS_AMI', "ami-7747d01e")
54
+ end
55
+ end
56
+ ```
57
+
58
+ then the secrets can be put into `env.bash`:
59
+
60
+ ```
61
+ export AWS_ACCESS_KEY_ID=xxxxxxxxxxxx
62
+ export AWS_SECRET_ACCESS_KEY=yyyyyyyyyyyyyyyyyy
63
+ export AWS_KEYPAIR_NAME=email@example.com
64
+ ```
65
+
66
+ Additionally if the `Vagrantfile` has multiple provider configuration stanzas to
67
+ enable a large developer organization, then the developer can choose their
68
+ preferred provider on a per-project basis by setting `VAGRANT_DEFAULT_PROVIDER`
69
+ in `env.bash`:
70
+
71
+ ```
72
+ export VAGRANT_DEFAULT_PROVIDER=aws
73
+ ```
74
+
75
+ ### Trivial example of `vagrant env`
76
+
77
+ With this initial environment configuration:
78
+
79
+ ```
80
+ $ export W=unmodified
81
+ $ export X=unmodified
82
+ $ # Y is not set
83
+ $ export Z=unmodified
84
+ ```
85
+
86
+ And this `env.bash`:
87
+
88
+ ```
89
+ export W=unmodified
90
+ export X=modified
91
+ export Y=added
92
+ unset Z # remove from ENV
93
+ ```
94
+
95
+ Then we can run `vagrant env` to see what happens:
96
+
97
+ ```
98
+ $ vagrant env
99
+ CHANGED the following variables:
100
+ X="modified" (was: "unmodified")
101
+ ADDED the following variables:
102
+ Y="added"
103
+ REMOVED the following variables:
104
+ Z="unmodified"
105
+ ```
106
+
107
+ ## FAQ
108
+
109
+ ### Is it necessary to explicitly `export` variables in `env.bash`?
110
+
111
+ Yes. If you have a lot of settings and want to avoid repeating `export`, you can
112
+ put `set -a` at the top of your `env.bash` to automatically export all
113
+ variables. In that case, you should also `set +a` at the bottom to avoid
114
+ confusion if you source `env.bash` into your guest shell configuration.
115
+
116
+ ### How do I put a multi-line string into `env.bash`?
117
+
118
+ You can put newlines directly into a multi-line string in Bash, so for example
119
+ this works:
120
+
121
+ ```
122
+ export PRIVATE_KEY="
123
+ -----BEGIN RSA PRIVATE KEY-----
124
+ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
125
+ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
126
+ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
127
+ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
128
+ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
129
+ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
130
+ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
131
+ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
132
+ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
133
+ -----END RSA PRIVATE KEY-----"
134
+ ```
135
+
136
+ ### Can I remove settings from the environment?
137
+
138
+ Yes, anything you `unset` will be removed from `ENV`. See the example of
139
+ `vagrant env` above.
140
+
141
+ ### How do I source `env.bash` into my guest shell environment?
142
+
143
+ Assuming that your source directory is available on the default `/vagrant` mount
144
+ point in the guest, you can simply add a line at the bottom of
145
+ `/home/vagrant/.bash_profile`:
146
+
147
+ ```
148
+ source /vagrant/env.bash
149
+ ```
150
+
151
+ Note that this means that settings are loaded on `vagrant ssh` so you need to
152
+ exit the shell and rerun `vagrant ssh` to refresh if you change settings.
@@ -0,0 +1,4 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+ Bundler::GemHelper.install_tasks
4
+
File without changes
@@ -0,0 +1,10 @@
1
+ require "pathname"
2
+
3
+ require "vagrant-envbash/plugin"
4
+
5
+ module VagrantPlugins
6
+ module EnvBash
7
+ lib_path = Pathname.new(File.expand_path("../vagrant-envbash", __FILE__))
8
+ autoload :Action, lib_path.join("action")
9
+ end
10
+ end
@@ -0,0 +1,81 @@
1
+ module VagrantPlugins
2
+ module EnvBash
3
+ module Action
4
+ class LoadEnvBash
5
+ def initialize(app, env)
6
+ # This method is defined to avoid a stack trace if it doesn't exist.
7
+ # We have no real need to save @app
8
+ @app = app
9
+ end
10
+
11
+ def call(env)
12
+ # Note that this loads early enough that there's no env[:ui] yet for
13
+ # outputting informational messages. Use the "vagrant env" command
14
+ # instead to investigate.
15
+
16
+ # The passed `env` is a wrapper around the env we want.
17
+ e = env[:env]
18
+
19
+ # This plugin will can be called multiple times, especially when
20
+ # operating on ids, for example "vagrant status 0fb925d". Don't try to
21
+ # load env.bash until we have a root path, and don't load twice.
22
+ return unless e.root_path
23
+ begin
24
+ return if e.envbash_ran
25
+ rescue
26
+ class << e
27
+ attr_accessor :envbash_ran, :envbash_file, :envbash_loaded,
28
+ :envbash_before, :envbash_after
29
+ end
30
+
31
+ # Avoid running twice.
32
+ e.envbash_ran = true
33
+
34
+ # We haven't loaded yet.
35
+ e.envbash_file = nil
36
+ e.envbash_loaded = false
37
+
38
+ # Save the original ENV for comparison in "vagrant env"
39
+ e.envbash_before = ENV.to_h
40
+ end
41
+
42
+ # Try to find env.bash, since it will be adjacent to Vagrantfile.
43
+ keep_vagrant_envbash_file = !! ENV['VAGRANT_ENVBASH_FILE']
44
+ if ! keep_vagrant_envbash_file
45
+ ENV['VAGRANT_ENVBASH_FILE'] = (e.root_path + 'env.bash').to_s
46
+ end
47
+ e.envbash_file = ENV['VAGRANT_ENVBASH_FILE']
48
+
49
+ # Load env.bash. This runs bash inside %x because Ruby uses /bin/sh
50
+ # for backticks.
51
+ new_env = eval %x{bash -c '
52
+ if [[ -s $VAGRANT_ENVBASH_FILE ]]; then
53
+ source "$VAGRANT_ENVBASH_FILE"
54
+ fi
55
+ ruby -e "p ENV"
56
+ '}
57
+
58
+ # Ignore modification to SHLVL which is just a shell artifact.
59
+ if e.envbash_before['SHLVL']
60
+ new_env['SHLVL'] = e.envbash_before['SHLVL']
61
+ else
62
+ new_env.delete('SHLVL')
63
+ end
64
+
65
+ # Replace the entire ENV (rather than update) so that env.bash can
66
+ # both set and unset vars.
67
+ ENV.replace(new_env)
68
+
69
+ # We are loaded!
70
+ e.envbash_loaded = ENV['VAGRANT_ENVBASH_FILE']
71
+
72
+ # Remove VAGRANT_ENVBASH_FILE from ENV if we set it.
73
+ ENV.delete('VAGRANT_ENVBASH_FILE') unless keep_vagrant_envbash_file
74
+
75
+ # Save the new ENV for comparison in "vagrant env"
76
+ e.envbash_after = ENV.to_h
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,49 @@
1
+ class EnvCommand < Vagrant.plugin(2, :command)
2
+ def execute
3
+ begin
4
+ @env.envbash_ran
5
+ rescue
6
+ @env.ui.error "vagrant-envbash plugin did not load! Do you have a Vagrantfile?"
7
+ return 2
8
+ end
9
+
10
+ if ! @env.envbash_loaded
11
+ @env.ui.warn "vagrant-envbash couldn't load #{@env.envbash_file.inspect}"
12
+ return 1
13
+ end
14
+
15
+ #@env.ui.info "vagrant-envbash loaded #{@env.envbash_loaded.inspect}"
16
+
17
+ before, after = @env.envbash_before, @env.envbash_after
18
+ added = after.to_a.select {|k, v| ! before.has_key? k}
19
+ removed = before.to_a.select {|k, v| ! after.has_key? k}
20
+ changed = after.to_a.select {|k, v| before.has_key? k and before[k] != v}
21
+
22
+ if ! changed.empty?
23
+ @env.ui.info "CHANGED the following variables:"
24
+ changed.each do |k, v|
25
+ @env.ui.info " #{k}=#{v.inspect} (was: #{before[k].inspect})"
26
+ end
27
+ end
28
+
29
+ if ! added.empty?
30
+ @env.ui.info "ADDED the following variables:"
31
+ added.each do |k, v|
32
+ @env.ui.info " #{k}=#{v.inspect}"
33
+ end
34
+ end
35
+
36
+ if ! removed.empty?
37
+ @env.ui.info "REMOVED the following variables:"
38
+ removed.each do |k, v|
39
+ @env.ui.info " #{k}=#{v.inspect}"
40
+ end
41
+ end
42
+
43
+ if added.empty? && removed.empty? && changed.empty?
44
+ @env.ui.info "NOTHING added, removed or changed."
45
+ end
46
+
47
+ 0
48
+ end
49
+ end
@@ -0,0 +1,30 @@
1
+ begin
2
+ require "vagrant"
3
+ rescue LoadError
4
+ raise "The Vagrant EnvBash plugin must be run within Vagrant."
5
+ end
6
+
7
+ module VagrantPlugins
8
+ module EnvBash
9
+ class Plugin < Vagrant.plugin("2")
10
+ name "EnvBash"
11
+ description <<-DESC
12
+ This plugin loads environment from env.bash.
13
+ DESC
14
+
15
+ # Hook as early as possible to load env.bash.
16
+ # This is the earliest hook that's publicly documented (and might be the
17
+ # earliest hook anyway).
18
+ action_hook(:load_env_bash, :environment_plugins_loaded) do |hook|
19
+ hook.prepend(VagrantPlugins::EnvBash::Action::LoadEnvBash)
20
+ end
21
+
22
+ # Register the "vagrant env" command which can list the full or partial
23
+ # environment.
24
+ command "env" do
25
+ require_relative "command"
26
+ EnvCommand
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,5 @@
1
+ module VagrantPlugins
2
+ module EnvBash
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'vagrant-envbash/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "vagrant-envbash"
8
+ spec.version = VagrantPlugins::EnvBash::VERSION
9
+ spec.authors = ["Aron Griffis"]
10
+ spec.email = ["aron@arongriffis.com"]
11
+ spec.summary = %q{Vagrant plugin to load environment variables from env.bash}
12
+ spec.description = %q{Vagrant plugin to load environment variables from env.bash}
13
+ spec.homepage = "https://github.com/agriffis/vagrant-envbash"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+
24
+ # It seems like this shouldn't be required, but I need this in order for
25
+ # "bundle exec vagrant" to run, even when cloning the upstream vagrant-aws
26
+ # plugin. https://botbot.me/freenode/vagrant/2016-05-15/?msg=66099642&page=3
27
+ spec.add_development_dependency "json"
28
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vagrant-envbash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Aron Griffis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Vagrant plugin to load environment variables from env.bash
56
+ email:
57
+ - aron@arongriffis.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - Vagrantfile
68
+ - lib/vagrant-envbash.rb
69
+ - lib/vagrant-envbash/action.rb
70
+ - lib/vagrant-envbash/command.rb
71
+ - lib/vagrant-envbash/plugin.rb
72
+ - lib/vagrant-envbash/version.rb
73
+ - vagrant-envbash.gemspec
74
+ homepage: https://github.com/agriffis/vagrant-envbash
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.4.8
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Vagrant plugin to load environment variables from env.bash
98
+ test_files: []