serverspec-runner 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +59 -0
- data/Rakefile +232 -0
- data/bin/serverspec-runner +56 -0
- data/lib/serverspec-runner.rb +2 -0
- data/lib/serverspec-runner/detection.rb +45 -0
- data/lib/serverspec-runner/util.rb +48 -0
- data/lib/serverspec-runner/util/hash.rb +5 -0
- data/lib/serverspec-runner/version.rb +3 -0
- data/scenario.yml +20 -0
- data/serverspec-runner.gemspec +27 -0
- data/spec/example/default.rb +10 -0
- data/spec/spec_helper.rb +93 -0
- data/ssh_options.yml +5 -0
- metadata +148 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c63aa61a94f4d60995a22640710b627185ab8c1e
|
4
|
+
data.tar.gz: 10add729a057ad6c55199e24005a430c0be48173
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1b5fba50a5079a3d079823ebc6006dbad20e16d2a726b7b3ba8f32a466f816a80ae6ca811bf3ab7178d2196ea76b883f6164138e340c6bcc8d940ea897e489eb
|
7
|
+
data.tar.gz: e4b1c753dfbd6918426d3fa5ca708b28b1482b4f626ba8766f32c98be7b99d8851b252c67ea305538f12e70b3b181342ed632c73e38622c59f75b66a8b3c122d
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 hiracy
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
serverspec-runner
|
2
|
+
======================
|
3
|
+
|
4
|
+
Simple execution framework for [serverspec](http://serverspec.org/).
|
5
|
+
|
6
|
+
----
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
$ gem install serverspec-runner
|
11
|
+
|
12
|
+
----
|
13
|
+
|
14
|
+
## Usage
|
15
|
+
|
16
|
+
initialize spec direcotries and create skeleton-specfiles.
|
17
|
+
|
18
|
+
$ serverspec-runner -r /path/to/your_serverspec_root
|
19
|
+
|
20
|
+
Edit your [spec-files](http://serverspec.org/resource_types.html).
|
21
|
+
|
22
|
+
$ vim /path/to/your_serverspec_root/test_name/test_detail_name/your_serverspec_test.rb
|
23
|
+
|
24
|
+
Edit your infrastructure or middleware tests scenario to "scenario.yml".
|
25
|
+
|
26
|
+
```
|
27
|
+
test_name: # test directory name(required)
|
28
|
+
test_detail_name: # test detail directory name(not required)
|
29
|
+
- servername # ssh-accessible ip address or fqdn. or alias
|
30
|
+
- :
|
31
|
+
- :
|
32
|
+
:
|
33
|
+
---
|
34
|
+
servername: # alias name(not required)
|
35
|
+
host: 192.168.0.11 # ssh-accessible ip address or fqdn(required if alias exist)
|
36
|
+
any_attribute: "aaa" # host attributes. left example available to get "property[:servername][:any_attribute]"
|
37
|
+
:
|
38
|
+
:
|
39
|
+
```
|
40
|
+
|
41
|
+
do tests.
|
42
|
+
|
43
|
+
$ serverspec-runner -r /path/to/your_serverspec_root -s /path/to/your_scenario.yml
|
44
|
+
|
45
|
+
You can also specify [ssh_options.yml](http://net-ssh.github.io/ssh/v2/api/classes/Net/SSH/Config.html)(Net::SSH options) file by "-o" option.
|
46
|
+
|
47
|
+
$ serverspec-runner -s /path/to/your_scenario.yml -o /path/to/your_ssh_options.yml
|
48
|
+
|
49
|
+
<!-- See details to [here](http://serverspec.org/) -->
|
50
|
+
|
51
|
+
----
|
52
|
+
|
53
|
+
## Contributing
|
54
|
+
|
55
|
+
1. Fork it
|
56
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
57
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
58
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
59
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,232 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require 'rake'
|
3
|
+
require 'rspec/core/rake_task'
|
4
|
+
require 'yaml'
|
5
|
+
require 'csv'
|
6
|
+
require 'fileutils'
|
7
|
+
require 'net/ssh'
|
8
|
+
require 'open-uri'
|
9
|
+
require 'serverspec-runner'
|
10
|
+
|
11
|
+
desc "Run serverspec to all scenario"
|
12
|
+
task :spec => 'spec:all'
|
13
|
+
|
14
|
+
namespace :spec do
|
15
|
+
|
16
|
+
ENV['EXEC_PATH'] = '/usr/local/bin:/usr/sbin:/sbin:/usr/bin:/bin'
|
17
|
+
|
18
|
+
ENV['specroot'] = ENV['specroot'] || "."
|
19
|
+
ENV['specpath'] = "#{ENV['specroot']}/spec"
|
20
|
+
|
21
|
+
ENV['ssh_options'] = ENV['ssh_options'] || "#{ENV['specroot']}/ssh_options.yml" || "#{File.dirname(__FILE__)}/ssh_options.yml"
|
22
|
+
ENV['ssh_options'] = "#{File.dirname(__FILE__)}/ssh_options.yml" unless File.exists?(ENV['ssh_options'])
|
23
|
+
ssh_options = YAML.load_file(ENV['ssh_options'])
|
24
|
+
ENV['result_csv'] = ENV['result_csv'] || './_serverspec_result.csv'
|
25
|
+
csv_file = ENV['result_csv']
|
26
|
+
CSV.open(csv_file, 'w') { |w| w << ['description', 'result'] }
|
27
|
+
ENV['explain'] = ENV['explain'] || "long"
|
28
|
+
ENV['tableformat'] = ENV['tableformat'] || "aa"
|
29
|
+
ENV['scenario'] = ENV['scenario'] || "./scenario.yml"
|
30
|
+
|
31
|
+
def init_specpath(path)
|
32
|
+
|
33
|
+
begin
|
34
|
+
print "want to create spec-tree to #{ENV['specpath']}? (y/n): "
|
35
|
+
ans = STDIN.gets.strip
|
36
|
+
exit 0 unless (ans == 'y' || ans == 'yes')
|
37
|
+
rescue Exception
|
38
|
+
exit 0
|
39
|
+
end
|
40
|
+
|
41
|
+
FileUtils.mkdir_p(path)
|
42
|
+
FileUtils.cp("#{File.dirname(__FILE__)}/scenario.yml", ENV['specroot'])
|
43
|
+
FileUtils.cp_r("#{File.dirname(__FILE__)}/spec/.", path)
|
44
|
+
|
45
|
+
puts("Please edit \"#{ENV['specroot']}/scenario.yml\" and change directory to \"#{ENV['specroot']}\" and exec \"serverspec-runner\" command !!")
|
46
|
+
end
|
47
|
+
|
48
|
+
def gen_exec_plan(parent, node, path, ssh_options, tasks, platform)
|
49
|
+
|
50
|
+
if parent == nil
|
51
|
+
abs_node = node
|
52
|
+
else
|
53
|
+
abs_node = parent[node]
|
54
|
+
end
|
55
|
+
|
56
|
+
if abs_node.kind_of?(Hash)
|
57
|
+
abs_node.keys.each do |n|
|
58
|
+
path << '::' unless path.empty?
|
59
|
+
path << n.to_s
|
60
|
+
path = gen_exec_plan(abs_node, n, path, ssh_options, tasks, platform)
|
61
|
+
end
|
62
|
+
elsif abs_node.kind_of?(Array)
|
63
|
+
abs_node.each do |host_alias|
|
64
|
+
if platform.include?(host_alias.to_sym)
|
65
|
+
ssh_host = platform[host_alias.to_sym][:host]
|
66
|
+
else
|
67
|
+
platform[host_alias.to_sym] = {}
|
68
|
+
ssh_host = host_alias
|
69
|
+
end
|
70
|
+
|
71
|
+
tasks << "#{path}::#{host_alias}"
|
72
|
+
platform[host_alias.to_sym] = ServerspecRunner::Detection.platform(ssh_host, ssh_options, platform[host_alias.to_sym])
|
73
|
+
return ''
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
return path
|
78
|
+
end
|
79
|
+
|
80
|
+
def exec_tasks(parent, node, real_path, platform)
|
81
|
+
|
82
|
+
if parent == nil
|
83
|
+
abs_node = node
|
84
|
+
else
|
85
|
+
abs_node = parent[node]
|
86
|
+
end
|
87
|
+
|
88
|
+
if abs_node.kind_of?(Hash)
|
89
|
+
abs_node.keys.each do |n|
|
90
|
+
real_path << n
|
91
|
+
real_path = exec_tasks(abs_node, n, real_path, platform)
|
92
|
+
end
|
93
|
+
elsif abs_node.kind_of?(Array)
|
94
|
+
task_path = ''
|
95
|
+
real_path.map.each do |p|
|
96
|
+
task_path << '::' unless task_path.empty?
|
97
|
+
task_path << p
|
98
|
+
end
|
99
|
+
|
100
|
+
abs_node.each do |host_alias|
|
101
|
+
desc "Run serverspec to #{task_path}@#{host_alias}"
|
102
|
+
RSpec::Core::RakeTask.new("#{task_path}::#{host_alias}".to_sym) do |t|
|
103
|
+
|
104
|
+
fpath = task_path.gsub(/::/, '/')
|
105
|
+
t.pattern = %W[
|
106
|
+
#{ENV['specpath']}/#{fpath}/{default.rb}
|
107
|
+
#{ENV['specpath']}/#{fpath}/{#{platform[host_alias.to_sym][:platform_name]}.rb}
|
108
|
+
#{ENV['specpath']}/#{fpath}/{#{platform[host_alias.to_sym][:platform_detail_name]}.rb}
|
109
|
+
]
|
110
|
+
|
111
|
+
raise "\e[31mspec file not found!![#{t.pattern.to_s}]\e[m" if Dir.glob(t.pattern).empty?
|
112
|
+
t.fail_on_error = false
|
113
|
+
ENV['TARGET_HOST'] = host_alias
|
114
|
+
ENV['TARGET_SSH_HOST'] = platform[host_alias.to_sym][:host] || host_alias
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
return []
|
119
|
+
end
|
120
|
+
|
121
|
+
return real_path
|
122
|
+
end
|
123
|
+
|
124
|
+
if !Dir.exists?(ENV['specpath'])
|
125
|
+
init_specpath(ENV['specpath'])
|
126
|
+
exit 0
|
127
|
+
end
|
128
|
+
|
129
|
+
if !ENV['tmpdir']
|
130
|
+
ENV['platforms_tmp'] = "./_platforms.yml"
|
131
|
+
ENV['scenario_tmp'] = "./_scenario.yml"
|
132
|
+
else
|
133
|
+
ENV['platforms_tmp'] = "#{ENV['tmpdir']}/_platforms.yml"
|
134
|
+
ENV['scenario_tmp'] = "#{ENV['tmpdir']}/_scenario.yml"
|
135
|
+
end
|
136
|
+
|
137
|
+
scenarios = nil
|
138
|
+
platform = {}
|
139
|
+
|
140
|
+
if ENV['scenario'] =~ /^(http|https):\/\//
|
141
|
+
open(ENV['scenario_tmp'], 'w') do |f|
|
142
|
+
open(ENV['scenario']) do |data|
|
143
|
+
f.write(data.read)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
ENV['scenario'] = ENV['scenario_tmp']
|
148
|
+
end
|
149
|
+
|
150
|
+
File.open(ENV['scenario'] || "#{ENV['specroot']}/scenario.yml") do |f|
|
151
|
+
YAML.load_documents(f).each_with_index do |data, idx|
|
152
|
+
if idx == 0
|
153
|
+
scenarios = data
|
154
|
+
else
|
155
|
+
data.each do |k, v|
|
156
|
+
platform[k.to_sym] = {}
|
157
|
+
v.each do |kk, vv|
|
158
|
+
platform[k.to_sym][kk.to_sym] = vv
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
if !scenarios
|
166
|
+
print "\e[31m"
|
167
|
+
puts "scenario.yml is empty."
|
168
|
+
print "\e[m"
|
169
|
+
exit 1
|
170
|
+
end
|
171
|
+
|
172
|
+
tasks = []
|
173
|
+
gen_exec_plan(nil, scenarios, '', ssh_options, tasks, platform)
|
174
|
+
|
175
|
+
task :stdout do
|
176
|
+
|
177
|
+
if ENV['tableformat'] == 'bool'
|
178
|
+
|
179
|
+
ret = 'ok'
|
180
|
+
CSV.foreach(csv_file) do |r|
|
181
|
+
ret = 'ng' if r[1] == 'NG'
|
182
|
+
end
|
183
|
+
|
184
|
+
puts ret
|
185
|
+
elsif ENV['tableformat'] == 'csv'
|
186
|
+
CSV.foreach(csv_file) do |r|
|
187
|
+
puts "#{r[0].strip}#{r[1].empty? ? '': ','}#{r[1]}"
|
188
|
+
end
|
189
|
+
else
|
190
|
+
maxlen = 0
|
191
|
+
CSV.foreach(csv_file) do |r|
|
192
|
+
n = r[0].each_char.map{|c| c.bytesize == 1 ? 1 : 2}.reduce(0, &:+)
|
193
|
+
maxlen = n if n > maxlen
|
194
|
+
end
|
195
|
+
|
196
|
+
pad_spaces = 4
|
197
|
+
|
198
|
+
spacer = nil
|
199
|
+
if ENV['tableformat'] == 'mkd'
|
200
|
+
spacer = "|:" + ("-" * maxlen) + "|:" + ("-" * "result".length) + ":|"
|
201
|
+
elsif ENV['tableformat'] == 'aa'
|
202
|
+
spacer = "+" + ("-" * (maxlen + "result".length + pad_spaces)) + "+"
|
203
|
+
end
|
204
|
+
|
205
|
+
puts spacer unless ENV['tableformat'] == 'mkd'
|
206
|
+
is_header = true
|
207
|
+
CSV.foreach(csv_file) do |r|
|
208
|
+
n = r[0].each_char.map{|c| c.bytesize == 1 ? 1 : 2}.reduce(0, &:+)
|
209
|
+
pad_mid = (" " * (maxlen - n)) + " | "
|
210
|
+
pad_tail = (" " * ("result".length - r[1].length)) + " |"
|
211
|
+
puts "|#{r[0]}#{pad_mid}#{r[1]}#{pad_tail}"
|
212
|
+
|
213
|
+
if is_header
|
214
|
+
puts spacer
|
215
|
+
is_header = false
|
216
|
+
end
|
217
|
+
end
|
218
|
+
puts spacer unless ENV['tableformat'] == 'mkd'
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
tasks << :stdout
|
223
|
+
task :all => tasks
|
224
|
+
|
225
|
+
# tempファイルに書き出し
|
226
|
+
open(ENV['platforms_tmp'] ,"w") do |y|
|
227
|
+
YAML.dump(platform, y)
|
228
|
+
end
|
229
|
+
|
230
|
+
path = {}
|
231
|
+
exec_tasks(nil, scenarios, [], platform)
|
232
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
|
4
|
+
|
5
|
+
require 'rubygems'
|
6
|
+
require 'rake'
|
7
|
+
require 'bundler/setup'
|
8
|
+
require 'getoptlong'
|
9
|
+
|
10
|
+
raketask = 'spec'
|
11
|
+
|
12
|
+
opts = GetoptLong.new(
|
13
|
+
["--scenario", "-s", GetoptLong::REQUIRED_ARGUMENT],
|
14
|
+
["--specroot", "-r", GetoptLong::REQUIRED_ARGUMENT],
|
15
|
+
["--ssh_options", "-o", GetoptLong::REQUIRED_ARGUMENT],
|
16
|
+
["--result_csv", "-c", GetoptLong::REQUIRED_ARGUMENT],
|
17
|
+
["--explain", "-e", GetoptLong::REQUIRED_ARGUMENT],
|
18
|
+
["--tableformat", "-t", GetoptLong::REQUIRED_ARGUMENT],
|
19
|
+
["--help", "-h", GetoptLong::NO_ARGUMENT],
|
20
|
+
["--tmpdir", "-1", GetoptLong::REQUIRED_ARGUMENT],
|
21
|
+
["--task", "-2", GetoptLong::REQUIRED_ARGUMENT]
|
22
|
+
)
|
23
|
+
|
24
|
+
opts.each do |opt, arg|
|
25
|
+
case opt
|
26
|
+
when '--scenario'
|
27
|
+
ENV['scenario'] = arg
|
28
|
+
when '--specroot'
|
29
|
+
ENV['specroot'] = arg
|
30
|
+
when '--ssh_options'
|
31
|
+
ENV['ssh_options'] = arg
|
32
|
+
when '--result_csv'
|
33
|
+
ENV['result_csv'] = arg
|
34
|
+
when '--explain'
|
35
|
+
ENV['explain'] = arg
|
36
|
+
when '--tableformat'
|
37
|
+
ENV['tableformat'] = arg
|
38
|
+
when '--tmpdir'
|
39
|
+
ENV['tmpdir'] = arg
|
40
|
+
when '--task'
|
41
|
+
raketask += "::" + arg
|
42
|
+
else
|
43
|
+
puts "Usage: serverspec-runner (options)"
|
44
|
+
puts "-s, --scenario SCENARIO_FILE path to scenario yml file"
|
45
|
+
puts "-r, --specroot SPEC_ROOT path to spec tests root dir"
|
46
|
+
puts "-o, --ssh_options SSH_OPTIONS_FILE path to ssh options yml file"
|
47
|
+
puts "-c, --result_csv RESULT_CSV_FILE path to result csv file"
|
48
|
+
puts "-e, --explain (short|long) specify result explain length(default: long)"
|
49
|
+
puts "-t, --tableformat (aa|mkd|csv|bool) specify result table type(default: aa)"
|
50
|
+
puts "-h, --help show help"
|
51
|
+
exit 0
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
load "#{File.dirname(__FILE__)}/../Rakefile"
|
56
|
+
Rake::Task[raketask.to_sym].invoke
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'socket'
|
2
|
+
require 'serverspec-runner/util'
|
3
|
+
|
4
|
+
module ServerspecRunner
|
5
|
+
module Detection
|
6
|
+
|
7
|
+
def self.platform(host, ssh_options={}, properties)
|
8
|
+
|
9
|
+
detected = {}
|
10
|
+
|
11
|
+
sudo = properties[:nosudo] ? '' : 'sudo'
|
12
|
+
properties[:sudo_prefix] = sudo + ' '
|
13
|
+
|
14
|
+
commands = {
|
15
|
+
:detect_platform_redhat => 'cat /etc/redhat-release 2> /dev/null',
|
16
|
+
:detect_platform_debian => 'cat /etc/debian_version 2> /dev/null',
|
17
|
+
:detect_hostname => 'hostname 2> /dev/null',
|
18
|
+
:detect_hostname_short => 'hostname -s 2> /dev/null',
|
19
|
+
}
|
20
|
+
|
21
|
+
executed = ServerspecRunner::Util.alternative_exec!(host, commands, ssh_options)
|
22
|
+
|
23
|
+
# platform
|
24
|
+
if executed[:detect_platform_redhat][:exit_succeeded]
|
25
|
+
detected[:platform_name] = 'centos'
|
26
|
+
|
27
|
+
if executed[:detect_platform_redhat][:stdout] =~ /^(CentOS release )(\d+).(\d+).*$/
|
28
|
+
detected[:platform_detail_name] = "centos#{$2}"
|
29
|
+
end
|
30
|
+
elsif executed[:detect_platform_debian][:exit_succeeded]
|
31
|
+
detected[:platform_name] = 'debian'
|
32
|
+
|
33
|
+
if executed[:detect_platform_debian][:stdout] =~ /^(\d+).(\d+).(\d*)$/
|
34
|
+
detected[:platform_detail_name] = "debian#{$1}"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# hostname
|
39
|
+
detected[:hostname] = executed[:detect_hostname][:stdout]
|
40
|
+
detected[:hostname_short] = executed[:detect_hostname_short][:stdout]
|
41
|
+
|
42
|
+
detected.merge(properties)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'net/ssh'
|
2
|
+
require 'net/ssh/shell'
|
3
|
+
|
4
|
+
module ServerspecRunner
|
5
|
+
module Util
|
6
|
+
|
7
|
+
def self.alternative_exec!(host, commands={}, options={})
|
8
|
+
|
9
|
+
result = {}
|
10
|
+
|
11
|
+
if host == 'localhost' || host == '127.0.0.1' || host == nil
|
12
|
+
commands.each do |key,cmd|
|
13
|
+
result[key] = { :stdout => `#{cmd}`.strip, :exit_status => $?, :exit_succeeded => ($? == 0) }
|
14
|
+
end
|
15
|
+
|
16
|
+
return result
|
17
|
+
end
|
18
|
+
|
19
|
+
options = Net::SSH::Config.for(host).merge(options)
|
20
|
+
user = options[:user] || Etc.getlogin
|
21
|
+
|
22
|
+
puts "connecting #{host}..."
|
23
|
+
|
24
|
+
Net::SSH.start(host, user, options) do |ssh|
|
25
|
+
|
26
|
+
ssh.shell do |sh|
|
27
|
+
commands.each do |key,cmd|
|
28
|
+
|
29
|
+
stdout = ''
|
30
|
+
stderr = ''
|
31
|
+
exit_status = nil
|
32
|
+
exit_succeeded = nil
|
33
|
+
|
34
|
+
pr = sh.execute! cmd do |shell_process|
|
35
|
+
shell_process.on_output do |pr, data|
|
36
|
+
stdout = data
|
37
|
+
end
|
38
|
+
end
|
39
|
+
result[key] = { :stdout => stdout.strip, :exit_status => pr.exit_status, :exit_succeeded => (pr.exit_status == 0) }
|
40
|
+
end
|
41
|
+
sh.execute! "exit"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
result
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/scenario.yml
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
#
|
2
|
+
# role_1:
|
3
|
+
# role_detail_1:
|
4
|
+
# - ssh-access-host-or-symbol
|
5
|
+
# - :
|
6
|
+
# role_detail_2:
|
7
|
+
# - :
|
8
|
+
# :
|
9
|
+
# role_2:
|
10
|
+
# :
|
11
|
+
# ---
|
12
|
+
# ssh-access-host-or-symbol:
|
13
|
+
# host: xxx.xxx.xxx.xxx
|
14
|
+
#
|
15
|
+
|
16
|
+
example:
|
17
|
+
- anyhost-01
|
18
|
+
---
|
19
|
+
anyhost-01:
|
20
|
+
host: 127.0.0.1
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'serverspec-runner/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "serverspec-runner"
|
8
|
+
spec.version = ServerspecRunner::VERSION
|
9
|
+
spec.authors = ["hiracy"]
|
10
|
+
spec.email = ["leizhen@mbr.nifty.com"]
|
11
|
+
spec.description = %q{simple execution framework for serverspec}
|
12
|
+
spec.summary = %q{simple execution framework for serverspec}
|
13
|
+
spec.homepage = "http://serverspec.org/"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency "serverspec"
|
22
|
+
spec.add_runtime_dependency "net-ssh"
|
23
|
+
spec.add_runtime_dependency "net-ssh-shell"
|
24
|
+
spec.add_runtime_dependency "rake"
|
25
|
+
spec.add_runtime_dependency "rspec-core", "~> 2.99"
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
27
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
require 'serverspec'
|
2
|
+
require 'pathname'
|
3
|
+
require 'net/ssh'
|
4
|
+
require 'yaml'
|
5
|
+
require 'csv'
|
6
|
+
require 'serverspec-runner/util/hash'
|
7
|
+
|
8
|
+
include Serverspec::Helper::DetectOS
|
9
|
+
|
10
|
+
ssh_opts_default = YAML.load_file(ENV['ssh_options'])
|
11
|
+
csv_path = ENV['result_csv']
|
12
|
+
explains = []
|
13
|
+
results = []
|
14
|
+
|
15
|
+
RSpec.configure do |c|
|
16
|
+
|
17
|
+
c.expose_current_running_example_as :example
|
18
|
+
c.path = ENV['EXEC_PATH']
|
19
|
+
|
20
|
+
run_path = c.files_to_run[0].split('/')
|
21
|
+
|
22
|
+
speck_i = 0
|
23
|
+
run_path.reverse.each_with_index do |r,i|
|
24
|
+
if r == 'spec'
|
25
|
+
speck_i = ((run_path.size - 1) - i)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
sliced = run_path.slice((speck_i + 1)..(run_path.size - 2))
|
29
|
+
role_name = sliced.join('-')
|
30
|
+
|
31
|
+
if ENV['ASK_SUDO_PASSWORD']
|
32
|
+
require 'highline/import'
|
33
|
+
c.sudo_password = ask("Enter sudo password: ") { |q| q.echo = false }
|
34
|
+
else
|
35
|
+
c.sudo_password = ENV['SUDO_PASSWORD']
|
36
|
+
end
|
37
|
+
|
38
|
+
set_property (YAML.load_file(ENV['platforms_tmp']))[ENV['TARGET_HOST'].to_sym]
|
39
|
+
|
40
|
+
if ENV['TARGET_SSH_HOST'] =~ /localhost|127\.0\.0\.1/
|
41
|
+
include Serverspec::Helper::Exec
|
42
|
+
c.os = backend(Serverspec::Commands::Base).check_os
|
43
|
+
else
|
44
|
+
include SpecInfra::Helper::Ssh
|
45
|
+
|
46
|
+
c.host = ENV['TARGET_SSH_HOST']
|
47
|
+
options = Net::SSH::Config.for(c.host, files=["~/.ssh/config"])
|
48
|
+
|
49
|
+
ssh_opts = YAML.load_file(property[:ssh_opts]) if File.exists?(property[:ssh_opts])
|
50
|
+
ssh_opts ||= ssh_opts_default
|
51
|
+
user = options[:user] || ssh_opts[:user] || Etc.getlogin
|
52
|
+
c.ssh = Net::SSH.start(c.host, user, options.merge(ssh_opts))
|
53
|
+
c.os = backend.check_os
|
54
|
+
end
|
55
|
+
|
56
|
+
c.before(:suite) do
|
57
|
+
entity_host = (((ENV['TARGET_HOST'] != ENV['TARGET_SSH_HOST']) && (ENV['TARGET_SSH_HOST'] != nil)) ? "(#{ENV['TARGET_SSH_HOST']})" : "")
|
58
|
+
puts "\e[33m"
|
59
|
+
puts "### start [#{role_name}@#{ENV['TARGET_HOST']}] #{entity_host} serverspec... ###"
|
60
|
+
print "\e[m"
|
61
|
+
|
62
|
+
explains << "#{role_name}@#{ENV['TARGET_HOST']}#{entity_host}"
|
63
|
+
results << ""
|
64
|
+
end
|
65
|
+
|
66
|
+
c.after(:each) do
|
67
|
+
if ENV['explain'] == 'long'
|
68
|
+
explains << " " + example.metadata[:full_description] + (RSpec::Matchers.generated_description || '')
|
69
|
+
else
|
70
|
+
|
71
|
+
second_depth = self.example.metadata.depth - 3
|
72
|
+
h = self.example.metadata
|
73
|
+
|
74
|
+
second_depth.times do |i|
|
75
|
+
h = h[:example_group]
|
76
|
+
end
|
77
|
+
|
78
|
+
second_desc = h[:description]
|
79
|
+
first_desc = h[:example_group][:description]
|
80
|
+
|
81
|
+
explains << " " + first_desc + " " + second_desc
|
82
|
+
end
|
83
|
+
results << (self.example.exception ? 'NG' : 'OK')
|
84
|
+
end
|
85
|
+
|
86
|
+
c.after(:suite) do
|
87
|
+
CSV.open(csv_path, 'a') do |writer|
|
88
|
+
explains.each_with_index do |v, i|
|
89
|
+
writer << [v, results[i]]
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
data/ssh_options.yml
ADDED
metadata
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: serverspec-runner
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- hiracy
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: serverspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: net-ssh
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: net-ssh-shell
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec-core
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '2.99'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '2.99'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: bundler
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '1.3'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1.3'
|
97
|
+
description: simple execution framework for serverspec
|
98
|
+
email:
|
99
|
+
- leizhen@mbr.nifty.com
|
100
|
+
executables:
|
101
|
+
- serverspec-runner
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".gitignore"
|
106
|
+
- ".rspec"
|
107
|
+
- Gemfile
|
108
|
+
- LICENSE
|
109
|
+
- README.md
|
110
|
+
- Rakefile
|
111
|
+
- bin/serverspec-runner
|
112
|
+
- lib/serverspec-runner.rb
|
113
|
+
- lib/serverspec-runner/detection.rb
|
114
|
+
- lib/serverspec-runner/util.rb
|
115
|
+
- lib/serverspec-runner/util/hash.rb
|
116
|
+
- lib/serverspec-runner/version.rb
|
117
|
+
- scenario.yml
|
118
|
+
- serverspec-runner.gemspec
|
119
|
+
- spec/example/default.rb
|
120
|
+
- spec/spec_helper.rb
|
121
|
+
- ssh_options.yml
|
122
|
+
homepage: http://serverspec.org/
|
123
|
+
licenses:
|
124
|
+
- MIT
|
125
|
+
metadata: {}
|
126
|
+
post_install_message:
|
127
|
+
rdoc_options: []
|
128
|
+
require_paths:
|
129
|
+
- lib
|
130
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - ">="
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
requirements: []
|
141
|
+
rubyforge_project:
|
142
|
+
rubygems_version: 2.2.2
|
143
|
+
signing_key:
|
144
|
+
specification_version: 4
|
145
|
+
summary: simple execution framework for serverspec
|
146
|
+
test_files:
|
147
|
+
- spec/example/default.rb
|
148
|
+
- spec/spec_helper.rb
|