Skip to content

Commit

Permalink
Merge pull request #243 from serverspec/refine-serverspec-init
Browse files Browse the repository at this point in the history
Rewrite safe_create_spec_helper() with ERB
  • Loading branch information
mizzy committed Sep 23, 2013
2 parents 72f0f86 + 6f5ada5 commit 27babae
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 74 deletions.
54 changes: 54 additions & 0 deletions bin/serverspec-init
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,57 @@ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
require 'serverspec'

Serverspec::Setup.run

__END__
require 'serverspec'
require 'pathname'
<% if @backend_type == 'Ssh' -%>
require 'net/ssh'
<% end -%>

include Serverspec::Helper::<%= @backend_type %>
include Serverspec::Helper::DetectOS

RSpec.configure do |c|
if ENV['ASK_SUDO_PASSWORD']
require 'highline/import'
c.sudo_password = ask("Enter sudo password: ") { |q| q.echo = false }
else
c.sudo_password = ENV['SUDO_PASSWORD']
end
<% if @backend_type == 'Ssh' -%>
c.before :all do
block = self.class.metadata[:example_group_block]
if RUBY_VERSION.start_with?('1.8')
file = block.to_s.match(/.*@(.*):[0-9]+>/)[1]
else
file = block.source_location.first
end
host = File.basename(Pathname.new(file).dirname)
if c.host != host
c.ssh.close if c.ssh
c.host = host
options = Net::SSH::Config.for(c.host)
user = options[:user] || Etc.getlogin
<% if @vagrant -%>
vagrant_up = `vagrant up #{@hostname}`
config = `vagrant ssh-config #{@hostname}`
if config != ''
config.each_line do |line|
if match = /HostName (.*)/.match(line)
c.host = match[1]
elsif match = /User (.*)/.match(line)
user = match[1]
elsif match = /IdentityFile (.*)/.match(line)
options[:keys] = [match[1].gsub(/\"/,'')]
elsif match = /Port (.*)/.match(line)
options[:port] = match[1]
end
end
end
<% end -%>
c.ssh = Net::SSH.start(c.host, user, options)
end
end
<% end -%>
end
78 changes: 4 additions & 74 deletions lib/serverspec/setup.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'fileutils'
require 'erb'

module Serverspec
class Setup
Expand All @@ -15,7 +16,7 @@ def self.run
num = gets.to_i - 1
puts

@backend_type = [ 'Ssh', 'Exec' ][num]
@backend_type = [ 'Ssh', 'Exec' ][num] || 'Exec'
if @backend_type == 'Ssh'
print "Vagrant instance y/n: "
@vagrant = gets.chomp
Expand Down Expand Up @@ -44,7 +45,6 @@ def self.run
end

def self.safe_create_spec

content = <<-EOF
require 'spec_helper'
Expand Down Expand Up @@ -92,77 +92,8 @@ def self.safe_mkdir(dir)
end

def self.safe_create_spec_helper
content = <<-EOF
require 'serverspec'
require 'pathname'
### include requirements ###
### include backend helper ###
include Serverspec::Helper::DetectOS
RSpec.configure do |c|
if ENV['ASK_SUDO_PASSWORD']
require 'highline/import'
c.sudo_password = ask("Enter sudo password: ") { |q| q.echo = false }
else
c.sudo_password = ENV['SUDO_PASSWORD']
end
### include backend conf ###
end
EOF

if not @backend_type.nil?
content.gsub!(/### include backend helper ###/, "include Serverspec::Helper::#{@backend_type}")
case @backend_type
when 'Ssh'
content.gsub!(/### include requirements ###/, "require 'net/ssh'")
content.gsub!(/### include backend conf ###/, "c.before :all do
block = self.class.metadata[:example_group_block]
if RUBY_VERSION.start_with?('1.8')
file = block.to_s.match(/.*@(.*):[0-9]+>/)[1]
else
file = block.source_location.first
end
host = File.basename(Pathname.new(file).dirname)
if c.host != host
c.ssh.close if c.ssh
c.host = host
options = Net::SSH::Config.for(c.host)
user = options[:user] || Etc.getlogin
### include vagrant conf ###
c.ssh = Net::SSH.start(c.host, user, options)
end
end")
if @vagrant
content.gsub!(/### include vagrant conf ###/,"
vagrant_up = `vagrant up #{@hostname}`
config = `vagrant ssh-config #{@hostname}`
if config != ''
config.each_line do |line|
if match = /HostName (.*)/.match(line)
c.host = match[1]
elsif match = /User (.*)/.match(line)
user = match[1]
elsif match = /IdentityFile (.*)/.match(line)
options[:keys] = [match[1].gsub(/\"/,'')]
elsif match = /Port (.*)/.match(line)
options[:port] = match[1]
end
end
end
")
else
content.gsub!(/### include vagrant conf ###/,'')
end
when 'Exec'
content.gsub!(/### include backend conf ###/, "c.before :all do
end")
when 'Puppet'
content.gsub!(/### include requirements ###/, "require 'puppet'\nrequire 'serverspec/backend/puppet'
")
end
end

requirements = []
content = ERB.new(DATA.read, nil, '-').result(binding)
if File.exists? 'spec/spec_helper.rb'
old_content = File.read('spec/spec_helper.rb')
if old_content != content
Expand Down Expand Up @@ -233,6 +164,5 @@ def self.auto_vagrant_configuration
exit 1
end
end

end
end

0 comments on commit 27babae

Please sign in to comment.