Permalink
Browse files

serverspec init

command:

$ serverspec-init
  • Loading branch information...
mknkisk committed May 8, 2016
1 parent f229cf3 commit 6da107bb9825751e36a4460dfb1f3ec9d18d4fc8
Showing with 99 additions and 0 deletions.
  1. +2 −0 .rspec
  2. +27 −0 Rakefile
  3. +1 −0 Vagrantfile
  4. +28 −0 spec/localhost.ubuntu/sample_spec.rb
  5. +41 −0 spec/spec_helper.rb
2 .rspec
@@ -0,0 +1,2 @@
--color
--format documentation
@@ -0,0 +1,27 @@
require 'rake'
require 'rspec/core/rake_task'

task :spec => 'spec:all'
task :default => :spec

namespace :spec do
targets = []
Dir.glob('./spec/*').each do |dir|
next unless File.directory?(dir)
target = File.basename(dir)
target = "_#{target}" if target == "default"
targets << target
end

task :all => targets
task :default => :all

targets.each do |target|
original_target = target == "_default" ? target[1..-1] : target
desc "Run serverspec tests to #{original_target}"
RSpec::Core::RakeTask.new(target.to_sym) do |t|
ENV['TARGET_HOST'] = original_target
t.pattern = "spec/#{original_target}/*_spec.rb"
end
end
end
@@ -13,6 +13,7 @@ Vagrant.configure(2) do |config|
# Every Vagrant development environment requires a box. You can search for
# boxes at https://atlas.hashicorp.com/search.
config.vm.box = "ubuntu/trusty64"
config.vm.hostname = "localhost.ubuntu"

# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
@@ -0,0 +1,28 @@
require 'spec_helper'

describe package('httpd'), :if => os[:family] == 'redhat' do
it { should be_installed }
end

describe package('apache2'), :if => os[:family] == 'ubuntu' do
it { should be_installed }
end

describe service('httpd'), :if => os[:family] == 'redhat' do
it { should be_enabled }
it { should be_running }
end

describe service('apache2'), :if => os[:family] == 'ubuntu' do
it { should be_enabled }
it { should be_running }
end

describe service('org.apache.httpd'), :if => os[:family] == 'darwin' do
it { should be_enabled }
it { should be_running }
end

describe port(80) do
it { should be_listening }
end
@@ -0,0 +1,41 @@
require 'serverspec'
require 'net/ssh'
require 'tempfile'

set :backend, :ssh

if ENV['ASK_SUDO_PASSWORD']
begin
require 'highline/import'
rescue LoadError
fail "highline is not available. Try installing it."
end
set :sudo_password, ask("Enter sudo password: ") { |q| q.echo = false }
else
set :sudo_password, ENV['SUDO_PASSWORD']
end

host = ENV['TARGET_HOST']

`vagrant up #{host}`

config = Tempfile.new('', Dir.tmpdir)
config.write(`vagrant ssh-config #{host}`)
config.close

options = Net::SSH::Config.for(host, [config.path])

options[:user] ||= Etc.getlogin

set :host, options[:host_name] || host
set :ssh_options, options

# Disable sudo
# set :disable_sudo, true


# Set environment variables
# set :env, :LANG => 'C', :LC_MESSAGES => 'C'

# Set PATH
# set :path, '/sbin:/usr/local/sbin:$PATH'

0 comments on commit 6da107b

Please sign in to comment.