Skip to content
This repository was archived by the owner on Oct 12, 2023. It is now read-only.

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
danarnold committed Jun 21, 2016
1 parent e6cf51a commit 5f5ef76
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
config/
.bundle
/Gemfile.lock
/coverage
/gemfiles/*.gemfile.lock
/pkg
/tmp
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.2.3
9 changes: 9 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
source "https://rubygems.org"

gem 'slop', '~> 3.6'
gem 'colorize'

group :development do
gem 'pry'
gem 'awesome_print'
end
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# chorizo

![Chorizo sausage](chorizo.jpg)

Parse and set environment variables on hosting providers Cloud66 and Heroku, in
the spirit of 12factor apps. Essentially, this is a minimal version of
laserlemon/figaro which just functions as an environment variable setter. This
has the added feature of supporting both environments, so you get to specify
config overrides and/or additions for different environments as well as
different deploy targets. Both should be represented as first-level key-values
with the key being the environment name or the deployment target's name.

## Cloud66

For Cloud66, this will output your application's config for the specified
environment and with any deployment specific values you've placed in the
config. To use it, redirect STDOUT to a file and then upload to Cloud66. Any
warnings will go to STDERR.

## Heroku

For Heroku, the command `heroku config:set` is called, which will actually
update the environment variables on your app. As such, you need to specify a
Heroku app name when using this.

## Usage

```ruby
Usage: chorizo [options]
-t, --target target host. one of "cloud66" or "heroku"
-e, --environment environment. e.x. "staging"
-a, --app app. (for heroku only)
-h, --help Display this help message.
```

## Building

`gem build chorizo.gemspec`

## Testing

`pry -Ilib -rchorizo`
36 changes: 36 additions & 0 deletions bin/chorizo
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env ruby

require 'chorizo'
require 'slop'
require 'colorize'

opts = Slop.parse(help: true) do
banner 'Usage: chorizo [options]'

on :t, :target, 'target host. one of "cloud66" or "heroku"', argument: true
on :e, :environment, 'environment. e.x. "staging"', argument: true
on :a, :app, 'app. (for heroku only)', argument: :optional
end

unless opts[:environment]
STDERR.puts "please specify an environment".red
puts opts
exit 1
end

if opts[:target] == 'heroku' && !opts[:app]
STDERR.puts "please specify an app for heroku".red
puts opts
exit 1
end

case opts[:target]
when 'cloud66'
Chorizo.new.cloud66 opts[:environment]
when 'heroku'
Chorizo.new.heroku(opts[:environment], opts[:app])
else
STDERR.puts "please specify a valid target".red
puts opts
exit 1
end
15 changes: 15 additions & 0 deletions chorizo.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Gem::Specification.new do |s|
s.name = 'chorizo'
s.version = '0.1.0'
s.date = '2016-06-20'
s.summary = 'Parse and set environment variables on hosting providers'
s.description = 'Parse and set environment variables on hosting providers'
s.authors = ["Daniel Arnold"]
s.email = '[email protected]'
s.files = Dir['lib/*']
s.add_runtime_dependency 'colorize', '0.7.7'
s.add_runtime_dependency 'slop', '~> 3.6'
s.executables << 'chorizo'
s.homepage = 'http://rubygems.org/gems/chorizo'
s.license = 'MIT'
end
Binary file added chorizo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions examples/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# config/application.yml

database_name: my_app
database_user: dan

development: &development
host: localhost:3000
host_scheme: http

test: *development

production:
database_name: my_app_production
database_user: my_app
database_pass: hunter2

cloud66:
web_concurrency: 5

heroku:
web_concurrency: 3

production:
RAILS_ENV: production
RACK_ENV: production
60 changes: 60 additions & 0 deletions lib/chorizo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require 'yaml'

class Chorizo

def initialize
@host_names = %w(cloud66 heroku)
end

def load_config
yml = YAML.load_file('./config/application.yml')
hashes, base = yml.partition { |k,v| v.is_a?(Hash) }
hashes = hashes.to_h
base = base.to_h
hosts = {}
@host_names.each do |host|
hosts[host] = hashes.delete(host) if hashes[host]
end
{ base: base, hosts: hosts, envs: hashes }
end

def build_output(env, host)
configs = load_config
output = configs[:base]

# load environment config
if configs[:envs][env]
output.merge! configs[:envs][env]
else
STDERR.puts "WARNING: #{env} specific configuration not found".red
end

# load host config
if configs[:hosts][host]
hc = configs[:hosts][host]
# load embedded env config in host config, if present
hc_envs, hc_base = hc.partition { |k,v| v.is_a?(Hash) }
hc_env = hc_envs.to_h[env]
hc_output = hc_base.to_h
hc_output.merge! hc_env if hc_env

output.merge! hc_output
else
STDERR.puts "WARNING: #{host} specific configuration not found".red
end

output
end

def cloud66(env)
output = build_output(env, 'cloud66')
output.each { |k,v| puts "#{k.upcase}=#{v}" }
end

def heroku(env, app)
output = build_output(env, 'heroku')
cmd_output = output.map { |k,v| "#{k}='#{v}'" }.join(' ')
system "heroku config:set #{cmd_output} -a #{app}"
end

end

0 comments on commit 5f5ef76

Please sign in to comment.