Module: AnsibleSpec

Defined in:
lib/ansible_spec.rb,
lib/ansible_spec/version.rb

Overview

Defined Under Namespace

Classes: TermColor

Constant Summary collapse

VERSION =
"0.0.1.3"

Class Method Summary collapse

Class Method Details

.mainObject



11
12
13
14
15
# File 'lib/ansible_spec.rb', line 11

def self.main()
  safe_create_spec_helper
  safe_create_rakefile
  safe_create_ansiblespec
end

.safe_create_ansiblespecObject



160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/ansible_spec.rb', line 160

def self.safe_create_ansiblespec
  content = <<'EOF'
--- 
- 
playbook: site.yml
inventory: hosts
EOF
  safe_touch(".ansiblespec")
  File.open(".ansiblespec", 'w') do |f|
    f.puts content
  end
end

.safe_create_rakefileObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/ansible_spec.rb', line 62

def self.safe_create_rakefile
  content = <<'EOF'
require 'rake'
require 'rspec/core/rake_task'
require 'yaml'

# param: none
# return: @playbook, @inventoryfile
def load_ansiblespec()
f = '.ansiblespec'
if File.exist?(f)
  y = YAML.load_file(f)
  @playbook = y[0]['playbook']
  @inventoryfile = y[0]['inventory']
else
  @playbook = 'site.yml'
  @inventoryfile = 'hosts'
end
if File.exist?(@playbook) == false
  puts 'Error: ' + @playbook + ' is not Found. create site.yml or /.ansiblespec  See https://github.com/volanja/ansible_spec'
  exit 1
elsif File.exist?(@inventoryfile) == false
  puts 'Error: ' + @inventoryfile + ' is not Found. create hosts or /.ansiblespec  See https://github.com/volanja/ansible_spec'
  exit 1
end
end

# param: inventory file of Ansible
# return: Hash {"active_group_name" => ["192.168.0.1","192.168.0.2"]}
def load_host(file)
hosts = File.open(file).read
active_group = Hash.new
active_group_name = ''
hosts.each_line{|line|
  line = line.chomp
  next if line.start_with?('#')
  if line.start_with?('[') && line.end_with?(']')
    active_group_name = line.gsub('[','').gsub(']','')
    active_group["#{active_group_name}"] = Array.new
  elsif active_group_name.empty? == false
    next if line.empty? == true
    active_group["#{active_group_name}"] << line
  end
}
return active_group
end

# main
load_ansiblespec
load_file = YAML.load_file(@playbook)

# e.g. comment-out
if load_file === false
puts 'Error: No data in site.yml'
exit
end

properties = Array.new
load_file.each do |site|
if site.has_key?("include")
  properties.push YAML.load_file(site["include"])[0]
else
  properties.push site
end
end


#load inventry file
hosts = load_host(@inventoryfile)
properties.each do |var|
if hosts.has_key?("#{var["hosts"]}")
  var["hosts"] = hosts["#{var["hosts"]}"]
end
end

namespace :serverspec do
properties.each do |var|
  var["hosts"].each do |host|
    desc "Run serverspec for #{var["name"]}"
    RSpec::Core::RakeTask.new(var["name"].to_sym) do |t|
      puts "Run serverspec for #{var["name"]} to #{host}"
      ENV['TARGET_HOST'] = host
      ENV['TARGET_PRIVATE_KEY'] = '~/.ssh/id_rsa'
      ENV['TARGET_USER'] = var["user"]
      t.pattern = 'roles/{' + var["roles"].join(',') + '}/spec/*_spec.rb'
    end
  end
end
end

EOF
  safe_touch("Rakefile")
  File.open("Rakefile", 'w') do |f|
    f.puts content
  end
  
end

.safe_create_spec_helperObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ansible_spec.rb', line 18

def self.safe_create_spec_helper
  content = <<'EOF'
require 'serverspec'
require 'pathname'
require 'net/ssh'

include SpecInfra::Helper::Ssh
include SpecInfra::Helper::DetectOS

RSpec.configure do |c|
if ENV['ASK_SUDO_PASSWORD']
  require 'highline/import'
  c.sudo_password = ask("Enter sudo password: ") { |q| q.echo = false }
else
  c.sudo_password = ENV['SUDO_PASSWORD']
end
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
  host  = ENV['TARGET_HOST']
  if c.host != host
    c.ssh.close if c.ssh
    c.host  = host
    options = Net::SSH::Config.for(c.host)
    user    = ENV['TARGET_USER']
    options[:keys] = ENV['TARGET_PRIVATE_KEY']
    c.ssh   = Net::SSH.start(host, user, options)
  end
end
end

EOF
  safe_mkdir("spec")
  safe_touch("spec/spec_helper.rb")
  File.open("spec/spec_helper.rb", 'w') do |f|
    f.puts content
  end

end

.safe_mkdir(dir) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/ansible_spec.rb', line 173

def self.safe_mkdir(dir)
  unless FileTest.exist?("#{dir}")
    FileUtils.mkdir_p("#{dir}")
    TermColor.green
    puts "\t\tcreate\t#{dir}"
    TermColor.reset
  else
    TermColor.red
    puts "\t\texists\t#{dir}"
    TermColor.reset
  end
end

.safe_touch(file) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/ansible_spec.rb', line 186

def self.safe_touch(file)
  unless File.exists? "#{file}"
    File.open("#{file}", 'w') do |f|
        #f.puts content
    end
    TermColor.green
    puts "\t\tcreate\t#{file}"
    TermColor.reset
  else 
    TermColor.red
    puts "\t\texists\t#{file}"
    TermColor.reset
  end
end