renv 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e26c29836717d549fdf77aeb14b7532b0dc36f8c
4
+ data.tar.gz: 0e2ec7d01003e5582aec6d90d2c1cce9ff508e19
5
+ SHA512:
6
+ metadata.gz: 7b68c0ecca614b4c6e088a78e17d9f3e3343ad00d7b52711cfa83143b97c15eb53161cb2dc6ab16d2fb67215d77f55daf42fe5844acfc5b90079ef134bdf6c7e
7
+ data.tar.gz: 9aa845d58d33b6700b4b1180e9d1e8e1b139a31a7b308d4afc1e186d1e989eb3bb6b0cf3c7514b2ea596c9edbb6c20be7160f3e34e58f324fd70b6eb163f4f92
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in renv.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 HouseTrip
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,88 @@
1
+ # Renv
2
+
3
+ The name stands for "Remote ENVironment" (thanks, captain obvious).
4
+
5
+ `renv` manages your `.env` (envrionment variables) files so they are never stored on your local machine.
6
+
7
+ It can be used to provide a replacement to Heroku's superb `config:get` /
8
+ `config:set` in Capistrano-land.
9
+
10
+
11
+ ## Installation
12
+
13
+ You know the dance:
14
+
15
+ $ gem install renv
16
+
17
+ You may need in your application's Gemfile, of course.
18
+
19
+ ## Configuration
20
+
21
+ You will need to set at least two variables in your environment:
22
+
23
+ RENV_AWS_KEY_myapp=...
24
+ RENV_AWS_SECRET_myapp=...
25
+
26
+ Plus a few optional ones (can be overriden on the command-line):
27
+
28
+ RENV_APP=myapp
29
+ RENV_BUCKET_myapp=...
30
+
31
+ ## Usage
32
+
33
+ Commands:
34
+ renv del KEY... # deletes KEY and its value
35
+ renv dump # dumps all key-value pairs in .env format
36
+ renv get KEY # returns the value of KEY
37
+ renv help [COMMAND] # Describe available commands or one specific command
38
+ renv load # set keys from standard input in .env format
39
+ renv set KEY=VALUE... # sets the value of KEY to VALUE
40
+
41
+ Options:
42
+ -a, [--app=APP] # Application name, defaults to RENV_APP
43
+ -b, [--bucket=BUCKET] # S3 bucket storing environment(s), defaults to RENV_BUCKET_<app>
44
+ -n, [--name=NAME] # Environment name, e.g. for staging apps, defaults to app name
45
+
46
+ ## How it works
47
+
48
+ Simply enough:
49
+
50
+ - `get` and `dump` will read a `.env`-formatted file from S3 (and parse it, in
51
+ the case of `get`)
52
+ - `set`, `load`, and `del` will write them back.
53
+
54
+ Every time a write to S3 occurs, two files get written: one named `latest` (all
55
+ read operations look at this file), and one named with the current ISO timestamp
56
+ (for backup purposes... in case you delete a needed key!)
57
+
58
+ **Caveat:** No effort is made to be safeguard against multiple users writing at
59
+ the same time.
60
+
61
+
62
+ ## Hooking to Capistrano
63
+
64
+ Here's an example, assuming you use the multistage extensions:
65
+
66
+ ```ruby
67
+ after 'deploy:finalize_update' do
68
+ environment = %x(bundle exec renv dump -a my_app -b my_bucket -n #{stage})
69
+ unless $?.success?
70
+ warn "Failed to obtain environment variables"
71
+ exit 1
72
+ end
73
+ put environment, "#{release_path}/.env"
74
+ end
75
+ ```
76
+
77
+ The environment variables will briefly live in Capistrano's memory, but will not
78
+ be saved to your disk.
79
+
80
+ ## Contributing
81
+
82
+ Yes, please!
83
+
84
+ 1. Fork it ( https://github.com/HouseTrip/renv/fork )
85
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
86
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
87
+ 4. Push to the branch (`git push origin my-new-feature`)
88
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'renv'
5
+ require 'renv/cli'
6
+
7
+ Renv::CLI.start(ARGV)
@@ -0,0 +1,3 @@
1
+ require "renv/version"
2
+
3
+ module Renv ; end
@@ -0,0 +1,59 @@
1
+ require 'renv'
2
+ require 'thor'
3
+ require 'renv/engine'
4
+
5
+ module Renv
6
+ class CLI < Thor
7
+ class_option :app, aliases: '-a', desc: 'Application name, defaults to RENV_APP'
8
+ class_option :bucket, aliases: '-b', desc: 'S3 bucket storing environment(s), defaults to RENV_BUCKET_<app>'
9
+ class_option :name, aliases: '-n', desc: 'Environment name, e.g. for staging apps, defaults to app name'
10
+
11
+ desc 'get KEY', 'returns the value of KEY'
12
+ def get(key)
13
+ puts _engine.get(key)
14
+ end
15
+
16
+ desc 'set KEY=VALUE...', 'sets the value of KEY to VALUE'
17
+ def set(*pairs)
18
+ hash = _parse_pairs(pairs)
19
+ _engine.set(hash)
20
+ end
21
+
22
+ desc 'del KEY...', 'deletes KEY and its value'
23
+ def del(*keys)
24
+ _engine.del(keys)
25
+ end
26
+
27
+ desc 'dump', 'dumps all key-value pairs in .env format'
28
+ def dump
29
+ puts _engine.dump
30
+ end
31
+
32
+ desc 'load', 'set keys from standard input in .env format'
33
+ def load
34
+ _engine.load(STDIN.read)
35
+ end
36
+
37
+ private
38
+
39
+ def _engine
40
+ Engine.new(
41
+ app: options[:app],
42
+ name: options[:name],
43
+ bucket: options[:bucket])
44
+ end
45
+
46
+ def _parse_pairs(pairs)
47
+ Hash.new.tap do |h|
48
+ pairs.each do |p|
49
+ if p !~ /^([^=]+)=(.*)$/
50
+ $stderr.puts "Not a valid key-value: '#{p}'"
51
+ exit 1
52
+ end
53
+ h[$1] = $2
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+
@@ -0,0 +1,46 @@
1
+ require 'renv'
2
+ require 'delegate'
3
+
4
+ module Renv
5
+ class Data
6
+ extend Forwardable
7
+
8
+ def initialize(payload)
9
+ @data = _parse(payload)
10
+ end
11
+
12
+ delegate [:[], :delete] => :@data
13
+
14
+ def []=(key, value)
15
+ _assert(key =~ /^[A-Z0-9_]+$/, 'Key must be uppercase letters, digits, and underscores')
16
+ @data[key] = value
17
+ end
18
+
19
+ def dump
20
+ result = []
21
+ @data.map { |k,v| "#{k}=#{v}\n" }.join
22
+ end
23
+
24
+ def load(payload)
25
+ @data = _parse(payload).merge(@data)
26
+ end
27
+
28
+ private
29
+
30
+ def _assert(condition, message)
31
+ return if condition
32
+ $stderr.puts message
33
+ exit 1
34
+ end
35
+
36
+ def _parse(payload)
37
+ Hash.new.tap do |result|
38
+ payload.strip.split(/[\n\r]+/).each do |line|
39
+ next if line.strip.empty? || line =~ /^#/
40
+ _assert(line.strip =~ /^([^=]+)=(.*)$/, "Cannot parse '#{line}'")
41
+ result[$1] = $2
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,93 @@
1
+ require 'renv'
2
+ require 'fog'
3
+ require 'renv/data'
4
+
5
+ module Renv
6
+ class Engine
7
+ def initialize(app: nil, name: nil, bucket: nil)
8
+ @_app = app
9
+ @_name = name
10
+ @_bucket = bucket
11
+ end
12
+
13
+ def get(key)
14
+ _data[key]
15
+ end
16
+
17
+ def set(hash)
18
+ hash.each_pair do |key, value|
19
+ _data[key] = value
20
+ end
21
+ _save
22
+ end
23
+
24
+ def del(keys)
25
+ keys.each { |key| _data.delete(key) }
26
+ _save
27
+ end
28
+
29
+ def dump
30
+ _data.dump
31
+ end
32
+
33
+ def load(payload)
34
+ _data.load(payload)
35
+ _save
36
+ end
37
+
38
+ private
39
+
40
+ def _data
41
+ @_data ||= begin
42
+ s3file = _directory.files.get(_path_current)
43
+ payload = s3file ? s3file.body : ''
44
+ Data.new(payload)
45
+ end
46
+ end
47
+
48
+ def _save
49
+ [_path_new, _path_current].each do |path|
50
+ _directory.files.create(key: path, body: _data.dump, public: false)
51
+ end
52
+ end
53
+
54
+ def _path_current
55
+ @_path_current ||= "#{_name}/current"
56
+ end
57
+
58
+ def _path_new
59
+ @_path_new ||= "#{_name}/#{Time.now.strftime('%FT%T')}"
60
+ end
61
+
62
+ def _directory
63
+ @_directory ||= _connection.directories.get(_bucket).tap do |dir|
64
+ if dir.nil?
65
+ $stderr.puts "Bucket '#{_bucket}' does not seem to exist"
66
+ exit 1
67
+ end
68
+ end
69
+ end
70
+
71
+ def _connection
72
+ @_connection ||= Fog::Storage.new(
73
+ provider: 'AWS',
74
+ aws_access_key_id: ENV.fetch("RENV_AWS_KEY_#{_app}"),
75
+ aws_secret_access_key: ENV.fetch("RENV_AWS_SECRET_#{_app}"),
76
+ region: ENV.fetch("RENV_AWS_REGION_#{_app}", 'eu-west-1')
77
+ )
78
+ end
79
+
80
+ def _bucket
81
+ @_bucket ||= ENV.fetch("RENV_BUCKET_#{_app}")
82
+ end
83
+
84
+ def _name
85
+ @_name ||= _app
86
+ end
87
+
88
+ def _app
89
+ @_app ||= ENV.fetch('RENV_APP')
90
+ end
91
+
92
+ end
93
+ end
@@ -0,0 +1,3 @@
1
+ module Renv
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'renv/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'renv'
8
+ spec.version = Renv::VERSION
9
+ spec.authors = ['Julien Letessier']
10
+ spec.email = ['julien.letessier@gmail.com']
11
+ spec.summary = %q{Manages out-of-repository .env file}
12
+ spec.homepage = ''
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'bundler', '~> 1.6'
21
+ spec.add_development_dependency 'rake'
22
+ spec.add_development_dependency 'pry'
23
+
24
+ spec.add_runtime_dependency 'fog'
25
+ spec.add_runtime_dependency 'thor'
26
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: renv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Julien Letessier
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-24 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
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
+ - !ruby/object:Gem::Dependency
56
+ name: fog
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: thor
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - julien.letessier@gmail.com
86
+ executables:
87
+ - renv
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - bin/renv
97
+ - lib/renv.rb
98
+ - lib/renv/cli.rb
99
+ - lib/renv/data.rb
100
+ - lib/renv/engine.rb
101
+ - lib/renv/version.rb
102
+ - renv.gemspec
103
+ homepage: ''
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.2.2
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Manages out-of-repository .env file
127
+ test_files: []