Serverspecã®Rakefileãã«ã¹ã¿ãã¤ãºãã¦ã¿ã話
ããã£ã¦ãã®6æãªã®ã§æ¢ã«ã ãã¶å¿ãã¦ããã®ã§ããã
ããããã£ããã¨ã¨ãã¦ã¯ã
â attributes.ymlæ¹å¼ã§ãfqdnã§ã¯ãªãIPãã¼ã¹ã§éä¿¡å
ãæå®ãããã
â¡è¤æ°hostå®è¡ã§ããã¹ã¯ã¼ãå
¥å1åã«ãããã
â¢æå®ããhostã«å¯¾ãã¦å®è¡ã§ããããã«ããã
ããããå®ç¾ããããã«Rubyåå¿è
ãªãã1æ¥ãããããã¦Rakefileãã«ã¹ã¿ãã¤ãºãã¦ã¿ã¾ããã
以ä¸ããããã説æãã¾ãã
â attributes.ymlæ¹å¼ã§ãfqdnã§ã¯ãªãIPãã¼ã¹ã§éä¿¡å ãæå®ãããã
Serverspecã®ããã©ã«ãã¯å¯¾è±¡hostæ¯ã«ãã£ã¬ã¯ããªãä½ã£ã¦å®è¡ããæ¹å¼ã§ããã
èªåã®å ´åã¯AdvancedTipsï¼http://serverspec.org/advanced_tips.htmlï¼ã®ä¸ã§ç´¹ä»ããã¦ãã
roleãattributes.ymlã§æå®ããæ¹å¼ã§ä½¿ç¨ãã¦ãã¾ãã
ãµã¤ãã®ä¾ã®å ´åãåhostã®fqdnï¼host.domainï¼ããrakeã¿ã¹ã¯ã®symbolãçæãã¦ããã®ã§ããã
symbolã«ã¯"."ï¼ãããï¼ã使ããªããããªã®ã§ããããã§åå²ãã1çªç®ã®è¦ç´ ãrakeã¿ã¹ã¯åã¨ãã¦ãã¾ãã
ããã¯fqdnã®å ´åã¯ã ããããã¾ãããã¾ããã
èªåã®å ´åãã¼ã«ã«ã§ã¯IPã¢ãã¬ã¹ãã¼ã¹ã§éä¿¡ãã¦ããã
IPã¢ãã¬ã¹ã®1çªç®ã使ãã¨ã»ã¨ãã©ã®hostã§rakeã¿ã¹ã¯åãéè¤ãã¦ãã¾ãã¾ãã
ããã§attributes.ymlã®keyã«ãããé¨åã«ã¯hostã®ç¥ç§°ãæ¸ãããã«ãã¦ã
ããããã®ã¾ã¾rakeã¿ã¹ã¯åã«ä½¿ãããã«ãã¾ããã
ã¾ããå®éã«éä¿¡ããIPã¢ãã¬ã¹ã¯hostnameã¨ããattributeã§å¥ã«å®ç¾©ãã¾ãã
â¡è¤æ°ãã¼ãå®è¡ã§ããã¹ã¯ã¼ãå ¥å1åã«ãããã
Serverspecã«ã¯sudoæã®ãã¹ã¯ã¼ããå
¥åããæ¹æ³ãç¨æããã¦ããã®ã§ããã
SUDO_PASSWORD=xxxxxxxxãã¯ç»é¢ã«å¹³æã§æ¸ããã¨ã«ãªãã®ã§ãªãã¨ãªãå´ä¸ã
ASK_SUDO_PASSWORD=1ãã ã¨sudoãè¡ãããæ¯ã«ãã¹ã¯ã¼ãèããã¦ãã¾ãã¾ãã
ï¼sudoeræå®ããã£ã¦è©±ããããã¾ãããï¼
ããããã®ã¯æåã«ä¸åå
¥åãæ±ãããã¦å¾ã¯ãã®å¤ã使ã£ã¦æ¬²ããã
ã¨ãããã¨ã§LDAP=1ãæå®ãããå
¥å1åã ãããã¦å¾ã¯èããªãããã«ãã¾ããã
ååã®ã¨ããLDAPãå©ç¨ãã¦ãç°å¢ã§ä½¿ããã¨ãæ³å®ãã¦ãã¦ã
è¤æ°ç°å¢ï¼LDAPå©ç¨ããhostã¨LDAPå©ç¨ããªãhostï¼æ··ãã¦ä½¿ããã¨ã¯æ³å®ãã¦ãã¾ããã
attributeã§ãã©ã°ä»ããã°åå¥æä½ã§ããã¨ã¯æãã¾ãããã
ç¨éçã«æ··ãã¦ä½¿ããã¨ã¯ç¡ãã ãããªã¨ã
â¢æå®ãããã¼ãã«å¯¾ãã¦å®è¡ã§ããããã«ããã
attribute.ymlã«10種é¡æ¸ããããã¨ãã£ã¦æ¯åå
¨é¨ã«å®è¡ããã¨ã¯éããªãã
ä¾ãã°AdvancedTipsã®ããã®ã¾ã¾å©ç¨ããå ´åã
rake serverspec:hostname
ã¿ããã«ããã°1å1åæå®ãããã¨ã¯åºæ¥ã¾ãã
ãã ãä»åã®å ´åâ¡ã¨çµã¿åãããå¿
è¦ããã£ãã®ã¨å½ç¶è¤æ°æå®ããããã£ãã®ã§ã
rake spec HOST=host1,host2
ã¿ããã«æå®ã§ããããã«ãã¾ããã
â¡ã¨â¢ã«é¢ãã¦ã¯rakeã¿ã¹ã¯ã®ä»çµã¿ãããããããè¦å´ãã¾ããã
ãããªãããªã§Rubyç´ äººãã«ã¹ã¿ãã¤ãºããRakefileã¯ä»¥ä¸ã
require 'rake' require 'rspec/core/rake_task' require 'yaml' attributes = YAML.load_file('spec/attributes.yml') desc "Run serverspec" task :spec => 'serverspec:prepare' namespace :serverspec do attributes.keys.each do |key| desc "Run serverspec to #{key}" RSpec::Core::RakeTask.new(key.split('.')[0].to_sym) do |t| ENV['TARGET_HOST'] = key t.pattern = 'spec/{' + attributes[key][:roles].join(',') + '}/*_spec.rb' end end desc "Run serverspec to all hosts" task :all => attributes.keys.map {|key| 'serverspec:' + key.split('.')[0] } desc "Run bofore task for serverspec" task :prepare do if ENV['LDAP'] require 'highline/import' ENV['LDAP_PASSWORD'] = ask("Enter ldap password: ") { |q| q.echo = false } ENV['ASK_LDAP'] = '1' end if ENV['HOST'] ENV['HOST'].split(",").map {|host| Rake::Task['serverspec:' + host.split('.')[0]].invoke } else Rake::Task['serverspec:all'].invoke end end end
spec_helper.rb
require 'serverspec' require 'pathname' require 'net/ssh' require 'yaml' include Serverspec::Helper::Ssh include Serverspec::Helper::DetectOS include Serverspec::Helper::Attributes attributes = YAML.load_file('spec/attributes.yml') RSpec.configure do |c| c.color_enabled = true c.path = "/sbin:/usr/sbin" key = ENV['TARGET_HOST'] if ENV['ASK_SUDO_PASSWORD'] then require 'highline/import' c.sudo_password = ask("Enter sudo password: ") { |q| q.echo = false } elsif ENV['ASK_LDAP'] then c.sudo_password = ENV['LDAP_PASSWORD'] else c.sudo_password = ENV['SUDO_PASSWORD'] end c.host = attributes[key][:hostname] attr_set attributes[key] options = Net::SSH::Config.for(c.host) user = options[:user] || Etc.getlogin if ENV['ASK_LDAP'] options[:password] = ENV['LDAP_PASSWORD'] end c.ssh = Net::SSH.start(c.host, user, options) c.os = backend.check_os end
attributes.yml
host1: :roles: - web - db :hostname: 192.168.100.10 host2: :roles: - web :hostname: 192.168.100.20
Serverspecã®ç¥è¦ã¯æ¢ã«ä¸æ¯ããããèªå以å¤ä½¿ã£ã¦ãªãç´ äººä»äºãªã®ã§ã©ããã¨æãã¾ãããã
ãããä½ãã®åå¼·ã«ãªã£ã足跡ã¨ãã¦æ®ãã¦ããã¾ãã
èªåãªãã«rakeã¿ã¹ã¯ãç解ã§ããç¬éããã£ãã¤ãããªãã ãã©ã
ééã£ã¦ããæ¥ããããã®ã§è©³ããã¯æ¸ããªãã§ããã¾ãã
å¤æ´ç¹ãè²è¡¨ç¤ºã§ããããããè¯ãã£ãããªã¨ãããã¤ã¤çµäºã