ansible_spec 0.0.1
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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +95 -0
- data/Rakefile +1 -0
- data/ansible_spec.gemspec +27 -0
- data/bin/ansiblespec-init +7 -0
- data/lib/ansible_spec.rb +183 -0
- data/lib/ansible_spec/version.rb +3 -0
- metadata +121 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 volanja
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
# AnsibleSpec
|
2
|
+
|
3
|
+
This is Severspec template for Run test Multi Role and Multi Host with Ansible
|
4
|
+
Create template (Rakefile and spec/spec_hepler.rb)
|
5
|
+
Serverspec template use Ansible InventoryFile and site.yml
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
install it yourself as:
|
10
|
+
|
11
|
+
$ gem install ansible_spec
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
### Create file Serverspec
|
16
|
+
|
17
|
+
```
|
18
|
+
$ ansiblespec-init
|
19
|
+
create spec
|
20
|
+
create spec/spec_helper.rb
|
21
|
+
create Rakefile
|
22
|
+
```
|
23
|
+
|
24
|
+
### Create Ansible Directory
|
25
|
+
|
26
|
+
sample is [here](https://github.com/volanja/ansible-sample-tdd)
|
27
|
+
|
28
|
+
```
|
29
|
+
.
|
30
|
+
├── README.md
|
31
|
+
├── hosts #use Ansible and Serverspec
|
32
|
+
├── site.yml #use Ansible and Serverspec
|
33
|
+
├── nginx.yml #(comment-out) incluted by site.yml
|
34
|
+
├── roles
|
35
|
+
│ └── nginx
|
36
|
+
│ ├── handlers
|
37
|
+
│ │ └── main.yml
|
38
|
+
│ ├── spec #use Serverspec
|
39
|
+
│ │ └── nginx_spec.rb
|
40
|
+
│ ├── tasks
|
41
|
+
│ │ └── main.yml
|
42
|
+
│ ├── templates
|
43
|
+
│ │ └── nginx.repo
|
44
|
+
│ └── vars
|
45
|
+
│ └── main.yml
|
46
|
+
├── Rakefile #Create file (use Serverspec)
|
47
|
+
└── spec #Create file (use Serverspec)
|
48
|
+
└── spec_helper.rb
|
49
|
+
```
|
50
|
+
|
51
|
+
### Serverspec with Ansible
|
52
|
+
Serverspec use this file. (Rakefile understand Notation of Ansible.)
|
53
|
+
|
54
|
+
* hosts
|
55
|
+
hosts can use [group_name]
|
56
|
+
|
57
|
+
```hosts
|
58
|
+
[server]
|
59
|
+
192.168.0.103
|
60
|
+
192.168.0.104
|
61
|
+
|
62
|
+
```
|
63
|
+
|
64
|
+
* site.yml
|
65
|
+
site.yml can use ```include```
|
66
|
+
|
67
|
+
```site.yml
|
68
|
+
- name: Ansible-Sample-TDD
|
69
|
+
hosts: server
|
70
|
+
user: root
|
71
|
+
roles:
|
72
|
+
- nginx
|
73
|
+
```
|
74
|
+
|
75
|
+
## Run Test
|
76
|
+
```
|
77
|
+
$ rake -T
|
78
|
+
rake serverspec:Ansible-Sample-TDD # Run serverspec for Ansible-Sample-TDD
|
79
|
+
|
80
|
+
$ rake serverspec:Ansible-Sample-TDD
|
81
|
+
Run serverspec for Ansible-Sample-TDD to 192.168.0.103
|
82
|
+
/Users/Adr/.rvm/rubies/ruby-1.9.3-p194/bin/ruby -S rspec roles/mariadb/spec/mariadb_spec.rb roles/nginx/spec/nginx_spec.rb
|
83
|
+
...........
|
84
|
+
|
85
|
+
Finished in 0.34306 seconds
|
86
|
+
11 examples, 0 failures
|
87
|
+
```
|
88
|
+
|
89
|
+
## Contributing
|
90
|
+
|
91
|
+
1. Fork it
|
92
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
93
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
94
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
95
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ansible_spec/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "ansible_spec"
|
8
|
+
gem.version = AnsibleSpec::VERSION
|
9
|
+
gem.authors = ["volanja"]
|
10
|
+
gem.email = ["volaaanja@gmail.com"]
|
11
|
+
gem.description = %q{This is Severspec template for Run test Multi Role and Multi Host with Ansible}
|
12
|
+
gem.summary = %q{This is Severspec template for Run test Multi Role and Multi Host with Ansbile}
|
13
|
+
gem.homepage = "https://github.com/volanja"
|
14
|
+
gem.license = "MIT"
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($/)
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
|
+
gem.require_paths = ["lib"]
|
20
|
+
|
21
|
+
gem.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
gem.add_development_dependency "rake"
|
23
|
+
gem.add_development_dependency "serverspec", "~> 0.13.5"
|
24
|
+
|
25
|
+
gem.add_runtime_dependency "serverspec", "~> 0.13.5"
|
26
|
+
|
27
|
+
end
|
data/lib/ansible_spec.rb
ADDED
@@ -0,0 +1,183 @@
|
|
1
|
+
require "ansible_spec/version"
|
2
|
+
|
3
|
+
# Reference
|
4
|
+
# https://github.com/serverspec/serverspec/blob/master/lib/serverspec/setup.rb
|
5
|
+
# Reference License (MIT)
|
6
|
+
# https://github.com/serverspec/serverspec/blob/master/LICENSE.txt
|
7
|
+
|
8
|
+
module AnsibleSpec
|
9
|
+
|
10
|
+
def self.main()
|
11
|
+
safe_create_spec_helper
|
12
|
+
safe_create_rakefile
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
def self.safe_create_spec_helper
|
17
|
+
content = <<'EOF'
|
18
|
+
require 'serverspec'
|
19
|
+
require 'pathname'
|
20
|
+
require 'net/ssh'
|
21
|
+
|
22
|
+
include SpecInfra::Helper::Ssh
|
23
|
+
include SpecInfra::Helper::DetectOS
|
24
|
+
|
25
|
+
RSpec.configure do |c|
|
26
|
+
if ENV['ASK_SUDO_PASSWORD']
|
27
|
+
require 'highline/import'
|
28
|
+
c.sudo_password = ask("Enter sudo password: ") { |q| q.echo = false }
|
29
|
+
else
|
30
|
+
c.sudo_password = ENV['SUDO_PASSWORD']
|
31
|
+
end
|
32
|
+
c.before :all do
|
33
|
+
block = self.class.metadata[:example_group_block]
|
34
|
+
if RUBY_VERSION.start_with?('1.8')
|
35
|
+
file = block.to_s.match(/.*@(.*):[0-9]+>/)[1]
|
36
|
+
else
|
37
|
+
file = block.source_location.first
|
38
|
+
end
|
39
|
+
host = ENV['TARGET_HOST']
|
40
|
+
if c.host != host
|
41
|
+
c.ssh.close if c.ssh
|
42
|
+
c.host = host
|
43
|
+
options = Net::SSH::Config.for(c.host)
|
44
|
+
user = ENV['TARGET_USER']
|
45
|
+
options[:keys] = ENV['TARGET_PRIVATE_KEY']
|
46
|
+
c.ssh = Net::SSH.start(host, user, options)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
EOF
|
52
|
+
safe_mkdir("spec")
|
53
|
+
safe_touch("spec/spec_helper.rb")
|
54
|
+
File.open("spec/spec_helper.rb", 'w') do |f|
|
55
|
+
f.puts content
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.safe_create_rakefile
|
61
|
+
content = <<'EOF'
|
62
|
+
require 'rake'
|
63
|
+
require 'rspec/core/rake_task'
|
64
|
+
require 'yaml'
|
65
|
+
|
66
|
+
# param: inventory file of Ansible
|
67
|
+
# return: Hash {"active_group_name" => ["192.168.0.1","192.168.0.2"]}
|
68
|
+
def load_host(file)
|
69
|
+
if File.exist?(file) == false
|
70
|
+
puts 'Error: Please create inventory file. name MUST "hosts"'
|
71
|
+
exit
|
72
|
+
end
|
73
|
+
hosts = File.open(file).read
|
74
|
+
active_group = Hash.new
|
75
|
+
active_group_name = ''
|
76
|
+
hosts.each_line{|line|
|
77
|
+
line = line.chomp
|
78
|
+
next if line.start_with?('#')
|
79
|
+
if line.start_with?('[') && line.end_with?(']')
|
80
|
+
active_group_name = line.gsub('[','').gsub(']','')
|
81
|
+
active_group["#{active_group_name}"] = Array.new
|
82
|
+
elsif active_group_name.empty? == false
|
83
|
+
next if line.empty? == true
|
84
|
+
active_group["#{active_group_name}"] << line
|
85
|
+
end
|
86
|
+
}
|
87
|
+
return active_group
|
88
|
+
end
|
89
|
+
|
90
|
+
load_file = YAML.load_file('site.yml')
|
91
|
+
|
92
|
+
# e.g. comment-out
|
93
|
+
if load_file === false
|
94
|
+
puts 'Error: No data in site.yml'
|
95
|
+
exit
|
96
|
+
end
|
97
|
+
|
98
|
+
properties = Array.new
|
99
|
+
load_file.each do |site|
|
100
|
+
if site.has_key?("include")
|
101
|
+
properties.push YAML.load_file(site["include"])[0]
|
102
|
+
else
|
103
|
+
properties.push site
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
|
108
|
+
#load inventry file
|
109
|
+
hosts = load_host('hosts')
|
110
|
+
properties.each do |var|
|
111
|
+
if hosts.has_key?("#{var["hosts"]}")
|
112
|
+
var["hosts"] = hosts["#{var["hosts"]}"]
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
namespace :serverspec do
|
117
|
+
properties.each do |var|
|
118
|
+
var["hosts"].each do |host|
|
119
|
+
desc "Run serverspec for #{var["name"]}"
|
120
|
+
RSpec::Core::RakeTask.new(var["name"].to_sym) do |t|
|
121
|
+
puts "Run serverspec for #{var["name"]} to #{host}"
|
122
|
+
ENV['TARGET_HOST'] = host
|
123
|
+
ENV['TARGET_PRIVATE_KEY'] = '~/.ssh/id_rsa'
|
124
|
+
ENV['TARGET_USER'] = var["user"]
|
125
|
+
t.pattern = 'roles/{' + var["roles"].join(',') + '}/spec/*_spec.rb'
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
EOF
|
132
|
+
safe_touch("Rakefile")
|
133
|
+
File.open("Rakefile", 'w') do |f|
|
134
|
+
f.puts content
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|
138
|
+
|
139
|
+
def self.safe_mkdir(dir)
|
140
|
+
unless FileTest.exist?("#{dir}")
|
141
|
+
FileUtils.mkdir_p("#{dir}")
|
142
|
+
TermColor.green
|
143
|
+
puts "\t\tcreate\t#{dir}"
|
144
|
+
TermColor.reset
|
145
|
+
else
|
146
|
+
TermColor.red
|
147
|
+
puts "\t\texists\t#{dir}"
|
148
|
+
TermColor.reset
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
def self.safe_touch(file)
|
153
|
+
unless File.exists? "#{file}"
|
154
|
+
File.open("#{file}", 'w') do |f|
|
155
|
+
#f.puts content
|
156
|
+
end
|
157
|
+
TermColor.green
|
158
|
+
puts "\t\tcreate\t#{file}"
|
159
|
+
TermColor.reset
|
160
|
+
else
|
161
|
+
TermColor.red
|
162
|
+
puts "\t\texists\t#{file}"
|
163
|
+
TermColor.reset
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
class TermColor
|
168
|
+
class << self
|
169
|
+
# 色を解除
|
170
|
+
def reset ; c 0 ; end
|
171
|
+
|
172
|
+
# 各色
|
173
|
+
def red ; c 31; end
|
174
|
+
def green ; c 32; end
|
175
|
+
|
176
|
+
# カラーシーケンスの出力
|
177
|
+
def c(num)
|
178
|
+
print "\e[#{num.to_s}m"
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
end
|
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ansible_spec
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- volanja
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-01-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: serverspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 0.13.5
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.13.5
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: serverspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 0.13.5
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 0.13.5
|
78
|
+
description: This is Severspec template for Run test Multi Role and Multi Host with
|
79
|
+
Ansible
|
80
|
+
email:
|
81
|
+
- volaaanja@gmail.com
|
82
|
+
executables:
|
83
|
+
- ansiblespec-init
|
84
|
+
extensions: []
|
85
|
+
extra_rdoc_files: []
|
86
|
+
files:
|
87
|
+
- .gitignore
|
88
|
+
- Gemfile
|
89
|
+
- LICENSE.txt
|
90
|
+
- README.md
|
91
|
+
- Rakefile
|
92
|
+
- ansible_spec.gemspec
|
93
|
+
- bin/ansiblespec-init
|
94
|
+
- lib/ansible_spec.rb
|
95
|
+
- lib/ansible_spec/version.rb
|
96
|
+
homepage: https://github.com/volanja
|
97
|
+
licenses:
|
98
|
+
- MIT
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ! '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 1.8.24
|
118
|
+
signing_key:
|
119
|
+
specification_version: 3
|
120
|
+
summary: This is Severspec template for Run test Multi Role and Multi Host with Ansbile
|
121
|
+
test_files: []
|