Skip to content

Commit 6cfd50d

Browse files
committed
init
0 parents  commit 6cfd50d

14 files changed

+658
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
attributes.yml
2+
hosts.yml

Gemfile

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source 'https://rubygems.org'
2+
gem 'serverspec'
3+
gem 'pry'

Gemfile.lock

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
coderay (1.1.0)
5+
diff-lcs (1.2.5)
6+
highline (1.6.21)
7+
method_source (0.8.2)
8+
net-ssh (2.9.1)
9+
pry (0.9.12.6)
10+
coderay (~> 1.0)
11+
method_source (~> 0.8)
12+
slop (~> 3.4)
13+
rspec (2.14.1)
14+
rspec-core (~> 2.14.0)
15+
rspec-expectations (~> 2.14.0)
16+
rspec-mocks (~> 2.14.0)
17+
rspec-core (2.14.8)
18+
rspec-expectations (2.14.5)
19+
diff-lcs (>= 1.1.3, < 2.0)
20+
rspec-mocks (2.14.6)
21+
serverspec (1.7.0)
22+
highline
23+
net-ssh
24+
rspec (~> 2.13)
25+
specinfra (~> 1.13)
26+
slop (3.5.0)
27+
specinfra (1.14.0)
28+
29+
PLATFORMS
30+
ruby
31+
32+
DEPENDENCIES
33+
pry
34+
serverspec

README.md

+180
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
# これはなに?
2+
3+
* 解決する問題
4+
* [Serverspec](http://http://serverspec.org/)はデフォルトでは単一のホストしかチェックできなかった件
5+
6+
* 解決した内容
7+
* 複数のホストとテストするattributeを定義すると、ホストの状態をチェック出来るServerspecのRakefileを作りました
8+
* hosts.ymlにhostsとroleの組み合わせを書くとテストしてくれます
9+
10+
* なお、UN*X用です。そしてsshでテストを実行します
11+
12+
13+
# インストール
14+
15+
```
16+
$ git clone [email protected]:dwango/serverspecd.git
17+
$ bundle
18+
```
19+
20+
## ディレクトリ構成
21+
22+
サンプルとしてmysqlのプロセスや設定をチェックしてくれる ``mysql_server`` 、ネットワーク周りを見てくれる ``network`` 、osのインストールされているパッケージなどをチェックしてくれる ``os`` などを同梱しています。参考にどうぞ。
23+
24+
```
25+
.
26+
|-- Gemfile
27+
|-- Gemfile.lock
28+
|-- README.md
29+
|-- Rakefile
30+
|-- attributes.yml.template
31+
|-- hosts.yml.template
32+
`-- spec
33+
|-- app
34+
| `-- app_spec.rb
35+
|-- mysql_server
36+
| `-- mysql_server_spec.rb
37+
|-- network
38+
| `-- network_spec.rb
39+
|-- os
40+
| `-- os_spec.rb
41+
|-- php
42+
| `-- php_spec.rb
43+
|-- spec_helper.rb
44+
`-- zabbix-agent
45+
`-- zabbix-agent_spec.rb
46+
```
47+
48+
## 使う
49+
50+
* attribute.yml.templeteをattribute.ymlに、hosts.yml.templeteをhosts.ymlにそれぞれリネーム
51+
* hosts.ymlにホスト名とspecのディレクトリ名を書きます
52+
* attributes.ymlにspecのディレクトリ名に設定するattributeを書きます
53+
54+
55+
## 設定
56+
57+
### hosts.yml
58+
59+
* hosts.ymlにhostsとroleの組み合わせを書きます
60+
* hosts.ymlの例
61+
62+
```
63+
nico:
64+
:roles:
65+
- os
66+
maki:
67+
:roles:
68+
- os
69+
- network
70+
```
71+
72+
* ノンパスで ``ssh nico`` , ``ssh maki`` できるようにしてください
73+
74+
### attributes.yml
75+
76+
* spec名のディレクトリのspecに対応する設定を書きます
77+
* :で始まっている理由は、参考にしたコードに引っ張られました
78+
79+
```
80+
mysql_server:
81+
:listen_port: 3306
82+
:version: 5.5.37
83+
:user: mysql
84+
:innodb_buffer_pool_size: 24G
85+
:innodb_log_file_size: 600M
86+
:innodb_log_files_in_group: 3
87+
php:
88+
:version: 5.5.12
89+
:max_execution_time: 0
90+
:memory_limit: 128M
91+
:post_max_size: 512M
92+
:upload_max_filesize: 2M
93+
:max_input_time: -1
94+
:timezone: Asia/Tokyo
95+
:libmcrypt_version: 2.5.8
96+
network:
97+
:gw_addr: 192.168.0.254
98+
:gw_addr_device: bond0
99+
```
100+
101+
### specファイルを作ってroleに追加する方法
102+
103+
hogeというroleを作ってみましょう。まずはspecファイルを作ります。
104+
105+
```
106+
mkdir spec/hoge
107+
touch spec/hoge/hoge_spec.rb
108+
```
109+
110+
hosts.ymlのrolesに追加します。
111+
112+
```
113+
nico:
114+
:roles:
115+
- os
116+
- hoge # add 'hoge' role
117+
maki:
118+
:roles:
119+
- os
120+
- network
121+
```
122+
123+
### specファイルを作ろう
124+
125+
* attributes.ymlのspecに対応した値を読み出すためにproperties = property['spec']が必要です
126+
127+
```
128+
$ cat spec/network/network_spec.rb
129+
require 'spec_helper'
130+
properties = property['network']
131+
132+
describe service('network') do
133+
it { should be_enabled }
134+
it { should be_running }
135+
end
136+
```
137+
138+
139+
## 実行
140+
141+
* 実行時の一覧
142+
143+
```
144+
$ rake -T
145+
rake serverspec # Run serverspec to all hosts
146+
rake serverspec:hostname1 # Run serverspec to hostname1
147+
rake serverspec:hostname2 # Run serverspec to hostname2
148+
```
149+
150+
* 特定のホストに対してテスト
151+
152+
```
153+
$ rake serverspec:hostname1
154+
```
155+
156+
* デバッグ情報も表示
157+
158+
```
159+
$ rake serverspec:hostname1 --trace SPEC_OPTS="-fd"
160+
```
161+
162+
163+
# その他
164+
165+
## TODO
166+
167+
* specファイルに propaties = propaties['spec']と書かないといけないのを何とかしたい
168+
* rake serverspecして全てのサーバをチェックしたとき、一つ目のサーバのチェックが失敗すると、それ以降のサーバのチェックが走りません。何とかしたい
169+
170+
## 参考
171+
172+
* http://serverspec.org/ Serverspec
173+
* https://github.com/serverspec/serverspec Serverspec(github.com)
174+
* http://mizzy.org/blog/2013/05/12/1/ serverspec のテストをホスト間で共有する方法
175+
* http://mizzy.org/blog/2013/05/12/2/ serverspec でホスト固有の属性値を扱う方法
176+
* https://github.com/hayato1980/serverspec-example NTPのチェック部分をコピーしました
177+
178+
## see also
179+
180+
* http://qiita.com/norobust/items/478866c355e35947835c serverspec で複数ホストの指定を簡単にしてみた

Rakefile

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'rake'
2+
require 'rspec/core/rake_task'
3+
require 'yaml'
4+
require 'pry'
5+
6+
hosts = YAML.load_file("hosts.yml").to_a
7+
8+
hosts = hosts.map do |key, value|
9+
{
10+
:name => key,
11+
:short_name => key,
12+
:roles => value[:roles],
13+
}
14+
end
15+
16+
desc "Run serverspec to all hosts"
17+
task :serverspec => 'serverspec:all'
18+
19+
namespace :serverspec do
20+
task :all => hosts.map {|h| 'serverspec:' + h[:short_name] }
21+
hosts.each do |host|
22+
desc "Run serverspec to #{host[:name]}"
23+
RSpec::Core::RakeTask.new(host[:short_name].to_sym) do |t|
24+
ENV['TARGET_HOST'] = host[:name]
25+
t.pattern = 'spec/{' + host[:roles].join(',') + '}/*_spec.rb'
26+
end
27+
end
28+
end

attributes.yml.template

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
mysql_server:
2+
# see spec/mysql_server/mysql_server_spec.rb
3+
:listen_port: 3306
4+
:version: 5.5.37
5+
:user: mysql
6+
:innodb_buffer_pool_size: 24G
7+
:innodb_log_file_size: 600M
8+
:innodb_log_files_in_group: 3
9+
php:
10+
# see spec/php/php_spec.rb
11+
:version: 5.5.12
12+
:max_execution_time: 0
13+
:memory_limit: 128M
14+
:post_max_size: 512M
15+
:upload_max_filesize: 2M
16+
:max_input_time: -1
17+
:timezone: Asia/Tokyo
18+
:libmcrypt_version: 2.5.8
19+
os:
20+
# see spec/os/os_spec.rb
21+
## /home/src directory username
22+
:homesrc_user: user
23+
## should install packages
24+
:packages:
25+
- lm_sensors
26+
- net-snmp-libs
27+
- net-snmp-devel
28+
- net-snmp
29+
- net-snmp-utils
30+
- libevent-devel
31+
- ntp
32+
- bind-utils
33+
- sysstat
34+
- lsof
35+
- gcc
36+
- gcc-c++
37+
- make
38+
- zip
39+
- unzip
40+
- lzop
41+
- vim-enhanced
42+
- curl
43+
- wget
44+
- git
45+
- libxml2-devel
46+
- libjpeg-turbo-devel
47+
- libpng-devel
48+
- libcurl-devel
49+
- gd-devel
50+
- readline-devel
51+
# - etc ...
52+
## disable service names
53+
:disable_services:
54+
- acpid
55+
- avahi-daemon
56+
- bluetooth
57+
- cups
58+
- gpm
59+
- hiddmcstrans
60+
- mdmonitor
61+
- messagebus
62+
- netfs
63+
- nfslock
64+
- pcscd
65+
- restorecond
66+
- rpcbind
67+
- rpcgssd
68+
- rpcidmapd
69+
- xfs
70+
- xinetd
71+
- yum-updatesd
72+
- ip6tables
73+
- iptables
74+
# - etc ...
75+
:resolv:
76+
- 192.168.0.1
77+
- 192.168.0.2
78+
network:
79+
# gateway ip addr
80+
:gw_addr: 192.168.0.254
81+
# gateway ip interface name
82+
:gw_addr_device: bond0
83+
# reachable tcp check, hostname and port number.
84+
:reachable:
85+
- host: localhost
86+
port: 80
87+
- host: www.google.co.jp
88+
port: 80
89+
# - host: hostname
90+
# port: 22
91+
zabbix-agent:
92+
:version: 2.2.3
93+
:logfile:
94+
- filename: /var/log/zabbix/zabbix_agentd.log
95+
user: zabbix
96+
# application specific settings
97+
app:
98+
:users:
99+
- hoge
100+
- taro_yamada
101+
:directories:
102+
- user: hoge
103+
directory: /home/hoge
104+
mode: 755
105+
- user: simainte
106+
directory: /home/simainte
107+
mode: 700

hosts.yml.template

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
hostname1:
2+
:roles:
3+
- os
4+
hostname2:
5+
:roles:
6+
- os
7+
- network

spec/app/app_spec.rb

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require 'spec_helper'
2+
properties = property['app']
3+
4+
properties[:users].map do |u|
5+
describe user(u) do
6+
it { should exist }
7+
end
8+
end
9+
10+
properties[:directories].map do |d|
11+
describe file(d['directory']) do
12+
it { should be_directory }
13+
it { should be_owned_by d['user'] }
14+
it { should be_mode d['mode'] }
15+
end
16+
end

0 commit comments

Comments
 (0)