Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into elzar_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Horner and Lake Denman committed May 3, 2012
2 parents a7b4d3d + fe571ca commit 8f1b6c2
Show file tree
Hide file tree
Showing 13 changed files with 201 additions and 82 deletions.
7 changes: 7 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
guard 'rspec', :version => 2 do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }

watch('spec/spec_helper.rb') { "spec" }
watch(%r{^spec/support/.+\.rb$}) { "spec" }
end
51 changes: 40 additions & 11 deletions lib/generators/provision_config/provision_config_generator.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'relevance_rails'
require 'tempfile'
require 'rails/generators'

class ProvisionConfigGenerator < Rails::Generators::Base
Expand Down Expand Up @@ -27,7 +28,14 @@ def check_git_status

def check_authorized_keys
if (@authorized_keys = fetch_keys).empty?
abort "No ssh keys were found! Check ssh-add -L and your keys_git_url config."
abort <<-EOF
No SSH public keys were found!
To ensure you have remote access to your servers, an SSH public key must be available from at least one of these sources:
- local file (~/.ssh/id_rsa.pub, ~/.ssh/id_dsa.pub, or ~/.ssh/id_ecdsa.pub)
- ssh-agent (by running `ssh-add -L`)
- public keys git repo (URL to repo specified in ~/.relevance_rails/key_git_url)
EOF
end
end

Expand All @@ -48,16 +56,19 @@ def fetch_elzar
end

def create_authorized_key_data_bag
@authorized_keys.map! {|key| "\"#{key}\""}
template('authorized_keys.json.erb', 'provision/data_bags/deploy/authorized_keys.json', {:force => true})
content = {
"id" => "authorized_keys",
"keys" => @authorized_keys,
}
create_file('provision/data_bags/deploy/authorized_keys.json', JSON.generate(content), {:force => true})
end

def create_dna_json
path = File.expand_path('provision/dna.json', destination_root)
json = JSON.parse File.binread(path)
json['rails_app']['name'] = name
RelevanceRails::ChefDNA.gene_splice(json,database)
create_file('provision/dna.json', JSON.generate(json), {:force => true})
content = JSON.parse(File.binread(path))
content['rails_app']['name'] = name
RelevanceRails::ChefDNA.gene_splice(content, database)
create_file('provision/dna.json', JSON.generate(content), {:force => true})
end

def create_rvmrc
Expand Down Expand Up @@ -104,10 +115,28 @@ def backup_manip_file(source, destination, operation)
git :add => destination
end


def fetch_keys
local_keys = `ssh-add -L`.split("\n")
local_keys = [] unless $?.success?
(local_keys + RelevanceRails::PublicKeyFetcher.public_keys).uniq
keys = local_keys
keys = ssh_agent_keys if keys.empty?
keys += RelevanceRails::PublicKeyFetcher.public_keys
keys.uniq
end

def local_keys
key_files = [
File.expand_path("~/.ssh/id_dsa.pub"),
File.expand_path("~/.ssh/id_ecdsa.pub"),
File.expand_path("~/.ssh/id_rsa.pub"),
]
key_files.select { |p| File.exist?(p) }.take(1).map { |p| split_keys(File.read(p)) }.flatten(1)
end

def ssh_agent_keys
keys = split_keys(`ssh-add -L`)
$?.success? ? keys : []
end

def split_keys(s)
s.split("\n").reject { |k| k.blank? }
end
end

This file was deleted.

2 changes: 1 addition & 1 deletion lib/relevance_rails.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "relevance_rails/version"
require "relevance_rails/public_key_fetcher"
require "relevance_rails/chef_dna"
require 'relevance_rails/railtie' if defined? Rails
require 'relevance_rails/railtie' if defined?(Rails)
require 'relevance_rails/generator_overrides'

module RelevanceRails
Expand Down
38 changes: 19 additions & 19 deletions lib/relevance_rails/chef_dna.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
module RelevanceRails::ChefDNA
def self.gene_splice(json, database)
set_ruby(json)
set_database(json, database)
def self.gene_splice(content, database)
set_ruby(content)
set_database(content, database)
end

private

def self.set_database(json, database)
def self.set_database(content, database)
if database == 'postgresql'
db_index = json['run_list'].find_index { |e| e == 'mysql::server' || e == 'role[postgres_database]'}
json['run_list'][db_index] = 'role[postgres_database]'
elsif database.nil? || database == 'mysql'
db_index = json['run_list'].find_index { |e| e == 'mysql::server' || e == 'role[postgres_database]'}
json['run_list'][db_index] = 'mysql::server'
db_index = content['run_list'].find_index { |e| (e == 'mysql::server') || (e == 'role[postgres_database]')}
content['run_list'][db_index] = 'role[postgres_database]'
elsif database.nil? || (database == 'mysql')
db_index = content['run_list'].find_index { |e| (e == 'mysql::server') || (e == 'role[postgres_database]')}
content['run_list'][db_index] = 'mysql::server'
end
end

def self.set_ruby(json)
def self.set_ruby(content)
if RelevanceRails.ruby_version =~ /^ree-(.*)/i
json['ruby_enterprise']['version'] = $1
json['ruby_enterprise']['url'] = "http://rubyenterpriseedition.googlecode.com/files/ruby-enterprise-#{$1}"
json['ruby_enterprise']['gems_version'] = rubygems_version
update_run_list! json['run_list'], 'role[enterprise_appstack]'
content['ruby_enterprise']['version'] = $1
content['ruby_enterprise']['url'] = "http://rubyenterpriseedition.googlecode.com/files/ruby-enterprise-#{$1}"
content['ruby_enterprise']['gems_version'] = rubygems_version
update_run_list! content['run_list'], 'role[enterprise_appstack]'
elsif RelevanceRails.ruby_version =~ /^ruby-(.*)/i
full_version = $1
json['ruby']['version'] = full_version
content['ruby']['version'] = full_version
major_version = full_version[/(\d\.\d).*/, 1]
json['ruby']['url'] = "http://ftp.ruby-lang.org/pub/ruby/#{major_version}/ruby-#{full_version}.tar.gz"
json['ruby']['gems_version'] = rubygems_version
update_run_list! json['run_list'], 'role[ruby_appstack]'
content['ruby']['url'] = "http://ftp.ruby-lang.org/pub/ruby/#{major_version}/ruby-#{full_version}.tar.gz"
content['ruby']['gems_version'] = rubygems_version
update_run_list! content['run_list'], 'role[ruby_appstack]'
else
raise "Your ruby is NOT SUPPORTED. Please use ree or ruby."
end
Expand All @@ -40,7 +40,7 @@ def self.update_run_list!(run_list, val)
end

def self.rubygems_version
require 'rubygems' unless defined? Gem
require 'rubygems' unless defined?(Gem)
Gem::VERSION
end
end
8 changes: 4 additions & 4 deletions lib/relevance_rails/provision.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ def self.create_ec2(name = nil)
end

def self.stop_ec2
return unless ENV["FORCE"] == "true" || Thor::Shell::Basic.new.yes?("Are you sure you want to shut down EC2 instance #{instance_id}?")
return unless (ENV["FORCE"] == "true") || Thor::Shell::Basic.new.yes?("Are you sure you want to shut down EC2 instance #{instance_id}?")
puts "Shutting down EC2 instance #{instance_id}..."
slushy = Slushy::Instance.new fog_connection, instance_id
slushy = Slushy::Instance.new(fog_connection, instance_id)
slushy.stop
puts "Done!"
end

def self.destroy_ec2
return unless ENV["FORCE"] == "true" || Thor::Shell::Basic.new.yes?("Are you sure you want to destroy EC2 instance #{instance_id}?")
return unless (ENV["FORCE"] == "true") || Thor::Shell::Basic.new.yes?("Are you sure you want to destroy EC2 instance #{instance_id}?")
puts "Destroying EC2 instance #{instance_id}..."
slushy = Slushy::Instance.new fog_connection, instance_id
slushy = Slushy::Instance.new(fog_connection, instance_id)
slushy.terminate
puts "Removing #{CONFIG_FILE}..."
File.delete(CONFIG_FILE)
Expand Down
4 changes: 2 additions & 2 deletions lib/relevance_rails/public_key_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
module RelevanceRails::PublicKeyFetcher
def self.public_keys
pubkeys = []
return pubkeys unless File.exist? user_git_url
git_url = File.read user_git_url
return pubkeys unless File.exist?(user_git_url)
git_url = File.read(user_git_url)
return pubkeys unless git_url =~ /\/(.*)\.git/
repo_name = $1
Dir.mktmpdir('public_keys') { |dir|
Expand Down
8 changes: 4 additions & 4 deletions lib/relevance_rails/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
module RelevanceRails
class Runner
def self.start(argv=ARGV)
if argv.empty? || argv[0] == '--help' || argv[0] == '-h'
if argv.empty? || (argv[0] == '--help') || (argv[0] == '-h')
print_help
elsif argv.delete '--version'
elsif argv.delete('--version')
puts "RelevanceRails #{RelevanceRails::VERSION}"
elsif argv[0] == 'new'
add_default_options! argv
if ENV['rvm_path'].nil? || ENV['NO_RVM']
exec 'rails', *argv
else
app_name = argv[1]
env = setup_rvm app_name
env = setup_rvm(app_name)

new_rvm_string = "#{env.environment_name.split('@')[0]}@#{app_name}"
install_relevance_rails argv, new_rvm_string, env.environment_name
Expand Down Expand Up @@ -64,7 +64,7 @@ def self.install_relevance_rails(argv, new_rvm_string, current_gemset)
child_env = RVM::Environment.new(new_rvm_string)
puts "Installing relevance_rails into the app's gemset..."

result = if argv.delete '--relevance-dev'
result = if argv.delete('--relevance-dev')
rubygem = "#{ENV['rvm_path']}/gems/#{current_gemset}/cache/relevance_rails-#{RelevanceRails::VERSION}.gem"
child_env.run('gem', 'install', rubygem)
else
Expand Down
12 changes: 6 additions & 6 deletions relevance_rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ Gem::Specification.new do |s|

s.executables = ['relevance_rails']

# specify any dependencies here; for example:
s.add_runtime_dependency 'rails', '~> 3.2'

# be aware that we're monkey-patching fog in fog_ext/ssh.rb;
# if we update fog, that monkey-patch might need to be
# revisited
s.add_runtime_dependency 'fog', '1.3.1'
s.add_runtime_dependency 'thor', '~> 0.14.6'
s.add_runtime_dependency 'rails', '~> 3.2'
s.add_runtime_dependency 'slushy', '~> 0.1.1'
s.add_runtime_dependency 'thor', '~> 0.14.6'

s.add_development_dependency 'pry'
s.add_development_dependency 'capybara'
s.add_development_dependency 'rspec'
s.add_development_dependency 'fakefs'
s.add_development_dependency 'guard-rspec'
s.add_development_dependency 'pry'
s.add_development_dependency 'rake', '~> 0.9.2.2'
s.add_development_dependency 'rspec'
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
require 'spec_helper'
require 'fileutils'
require 'fakefs/spec_helpers'
require 'generators/provision_config/provision_config_generator'

describe ProvisionConfigGenerator do
subject { ProvisionConfigGenerator.new(["name"]) }

describe "#local_keys" do
include FakeFS::SpecHelpers

def write_fixture(path, contents)
FileUtils.mkdir_p(File.dirname(path))
File.open(path, "w") { |f| f.write(contents) }
end

it "returns the contents of id_rsa.pub when it exists" do
write_fixture(File.expand_path("~/.ssh/id_rsa.pub"), "RSA-public-key")
subject.send(:local_keys).should == ["RSA-public-key"]
end

it "returns the contents of id_dsa.pub when it exists" do
write_fixture(File.expand_path("~/.ssh/id_dsa.pub"), "DSA-public-key")
subject.send(:local_keys).should == ["DSA-public-key"]
end

it "returns the contents of id_ecdsa.pub when it exists" do
write_fixture(File.expand_path("~/.ssh/id_ecdsa.pub"), "ECDSA-public-key")
subject.send(:local_keys).should == ["ECDSA-public-key"]
end

it "returns the contents of id_dsa.pub when both id_dsa.pub and id_rsa.pub exist" do
write_fixture(File.expand_path("~/.ssh/id_dsa.pub"), "DSA-public-key")
write_fixture(File.expand_path("~/.ssh/id_rsa.pub"), "RSA-public-key")
subject.send(:local_keys).should == ["DSA-public-key"]
end

it "ignores trailing newlines" do
write_fixture(File.expand_path("~/.ssh/id_rsa.pub"), "RSA-public-key\n\n")
subject.send(:local_keys).should == ["RSA-public-key"]
end

it "ignores blank lines" do
write_fixture(File.expand_path("~/.ssh/id_rsa.pub"), "\n\nRSA-public-key\n\n\n")
subject.send(:local_keys).should == ["RSA-public-key"]
end
end

describe "#ssh_agent_keys" do
it "retrieves keys from ssh agent" do
subject.should_receive("`").with("ssh-add -L") do
system("true")
"my-public-key\nanother-public-key"
end
subject.send(:ssh_agent_keys).should == ["my-public-key", "another-public-key"]
end

it "ignores output from ssh-add when execution fails" do
subject.stub("`") do
system("false")
# Actual message that comes back from failed call
"The agent has no entities"
end
subject.send(:ssh_agent_keys).should == []
end
end

describe "#fetch_keys" do
it "returns local keys when they exist" do
subject.stub(:local_keys).and_return(["key-from-local-file"])
subject.stub(:ssh_agent_keys).and_return(["key-from-ssh-agent"])
RelevanceRails::PublicKeyFetcher.stub(:public_keys).and_return([])
subject.send(:fetch_keys).should == ["key-from-local-file"]
end

it "returns keys from ssh agent when local keys do NOT exist" do
subject.stub(:local_keys).and_return([])
subject.stub(:ssh_agent_keys).and_return(["key-from-ssh-agent"])
RelevanceRails::PublicKeyFetcher.stub(:public_keys).and_return([])
subject.send(:fetch_keys).should == ["key-from-ssh-agent"]
end

it "combines local keys with those from PublicKeyFetcher" do
subject.stub(:local_keys).and_return(["key-from-local-file"])
RelevanceRails::PublicKeyFetcher.stub(:public_keys).and_return(["key-from-git-repo"])
subject.send(:fetch_keys).should == ["key-from-local-file", "key-from-git-repo"]
end

it "combines ssh agent keys with those from PublicKeyFetcher" do
subject.stub(:local_keys).and_return([])
subject.stub(:ssh_agent_keys).and_return(["key-from-ssh-agent"])
RelevanceRails::PublicKeyFetcher.stub(:public_keys).and_return(["key-from-git-repo"])
subject.send(:fetch_keys).should == ["key-from-ssh-agent", "key-from-git-repo"]
end

it "excludes duplicate keys" do
subject.stub(:local_keys).and_return(["key-1", "key-2", "key-1"])
RelevanceRails::PublicKeyFetcher.stub(:public_keys).and_return(["key-3", "key-2", "key-3"])
subject.send(:fetch_keys).should == ["key-1", "key-2", "key-3"]
end
end

describe "#check_authorized_keys" do
it "aborts if no ssh-keys are found" do
subject.stub(:fetch_keys).and_return([])
should_abort_with(/^No SSH public keys were found!/) do
subject.check_authorized_keys
end
end

it "doesn't abort if ssh-keys are found" do
subject.should_receive(:fetch_keys).and_return(['ssh-rsa ZZZZ'])
expect { subject.check_authorized_keys }.to_not raise_error
end
end
end
26 changes: 0 additions & 26 deletions spec/lib/provision_config_generator_spec.rb

This file was deleted.

File renamed without changes.
Loading

0 comments on commit 8f1b6c2

Please sign in to comment.