lib/serverspec/setup.rb in serverspec-0.0.17 vs lib/serverspec/setup.rb in serverspec-0.0.18

- old
+ new

@@ -1,46 +1,68 @@ require 'fileutils' module Serverspec class Setup def self.run - %w( spec spec/www.example.jp ).each { |dir| safe_mkdir(dir) } + print "Input target host name: " + @hostname = gets.chomp + + prompt = <<-EOF + +Select OS Type of target host: + + 1) Red Hat + 2) Debian + 3) Gentoo + 4) Solaris + 5) None + +Select container number: +EOF + + print prompt.chop + num = gets.to_i - 1 + puts + + @os_type = [ 'RedHat', 'Debian', 'Gentoo', 'Solaris', nil ][num] + [ 'spec', "spec/#{@hostname}" ].each { |dir| safe_mkdir(dir) } safe_create_spec safe_create_spec_helper safe_create_rakefile end def self.safe_create_spec - content = <<-EOF + + content = <<-EOF require 'spec_helper' -describe 'httpd', :os => :redhat do +describe 'httpd' do it { should be_installed } it { should be_enabled } it { should be_running } end -describe 'port 80', :os => :redhat do +describe 'port 80' do it { should be_listening } end -describe '/etc/httpd/conf/httpd.conf', :os => :redhat do +describe '/etc/httpd/conf/httpd.conf' do it { should be_file } - it { should contain "ServerName www.example.jp" } + it { should contain "ServerName #{@hostname}" } end EOF - if File.exists? 'spec/www.example.jp/httpd_spec.rb' - old_content = File.read('spec/www.example.jp/httpd_spec.rb') + if File.exists? "spec/#{@hostname}/httpd_spec.rb" + old_content = File.read("spec/#{@hostname}/httpd_spec.rb") if old_content != content - $stderr.puts "!! spec/www.example.jp/httpd_spec.rb already exists and differs from template" + $stderr.puts "!! spec/#{@hostname}/httpd_spec.rb already exists and differs from template" end else - File.open('spec/www.example.jp/httpd_spec.rb', 'w') do |f| + File.open("spec/#{@hostname}/httpd_spec.rb", 'w') do |f| f.puts content end - puts ' + spec/www.example.jp/httpd_spec.rb' + puts " + spec/#{@hostname}/httpd_spec.rb" end end def self.safe_mkdir(dir) if File.exists? dir @@ -58,10 +80,11 @@ require 'serverspec' require 'pathname' require 'net/ssh' RSpec.configure do |c| + ### include os helper ### c.before do host = File.basename(Pathname.new(example.metadata[:location]).dirname) if c.host != host c.ssh.close if c.ssh c.host = host @@ -70,9 +93,14 @@ c.ssh = Net::SSH.start(c.host, user, options) end end end EOF + + if not @os_type.nil? + content.gsub!(/### include os helper ###/, "c.include(Serverspec::#{@os_type}Helper)") + end + if File.exists? 'spec/spec_helper.rb' old_content = File.read('spec/spec_helper.rb') if old_content != content $stderr.puts "!! spec/spec_helper.rb already exists and differs from template" end