Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
serverspec for my environment
#Webapps
web01:
:roles:
- base
- webapp
:hostname: web01
:ip: xxx.xxx.xxx.xxx
# Database
db01:
:roles:
- base
- db
:hostname: db01
:ip: xxx.xxx.xxx.xxx
:mha_vip: xxx.xxx.xxx.xxx
:dsr_vip: xxx.xxx.xxx.xxx
# memcached
mem01:
:roles:
- base
- memcached
:hostname: mem01
:ip: xxx.xxx.xxx.xxx
require 'rake'
require 'rspec/core/rake_task'
require 'yaml'
require 'highline/import'
properties = YAML.load_file('properties.yaml')
ENV['SSH_USER'] = ask("Enter ssh user: ") { |q| q.echo = true }
ENV['SSH_PASSWORD'] = ask("Enter ssh password: ") { |q| q.echo = false }
desc "Run serverspec to all hosts"
task :serverspec => 'serverspec:all'
namespace :serverspec do
task :all => properties.keys.map {|key| 'serverspec:' + key }
properties.keys.each do |key|
desc "Run serverspec to #{key}"
RSpec::Core::RakeTask.new(key.to_sym) do |t|
ENV['KEY'] = "#{key}"
ENV['TARGET_HOST'] = properties[key][:ip]
t.pattern = 'spec/{' + properties[key][:roles].join(',') + '}/*_spec.rb'
end
end
end
require 'serverspec'
require 'pathname'
require 'net/ssh'
require 'yaml'
include Serverspec::Helper::Ssh
include Serverspec::Helper::DetectOS
properties = YAML.load_file('properties.yaml')
RSpec.configure do |c|
ssh_user = ENV['SSH_USER']
ssh_password = ENV['SSH_PASSWORD']
c.sudo_password = ssh_password
ja_prompt = "パスワード".force_encoding('ascii-8bit')
c.pass_prompt = /#{ja_prompt}|^Password|^\[sudo\] password for/
c.path = '/sbin:/usr/sbin'
c.color_enabled = true
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
c.host = ENV['TARGET_HOST']
set_property properties[ENV['KEY']]
options = Net::SSH::Config.for(c.host)
options[:password] = ssh_password
c.ssh = Net::SSH.start(c.host, ssh_user, options)
c.os = backend.check_os
end
end
def check_centos_version
ver = send_command('head -1 /etc/issue | tr -d "\n"')
case ver
when 'CentOS release 5.4 (Final)'
return '5.4'
when 'CentOS release 6.2 (Final)'
return '6.2'
else
raise "Error: I don't know this version."
end
end
def is_master_db
repl_con_num = send_command('mysql -uroot -e "show processlist" | grep "Binlog Dump" | wc -l')
result = repl_con_num.to_i > 0 ? true : false
return result
end
def vlanid
vlanid = send_command('ls /proc/net/vlan/bond0.* | awk -F. \'{print $2}\' | tr -d "\n"')
return vlanid
end
def expected_server_id
ip_arry = ENV['TARGET_HOST'].split('.')
server_id = "#{ip_arry[1]}#{ip_arry[2]}#{ip_arry[3]}"
return server_id
end
def send_command(cmd)
Net::SSH.start(ENV['TARGET_HOST'], ENV['SSH_USER'], :password => ENV['SSH_PASSWORD']) do |ssh|
return ssh.exec! cmd
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.