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"

Class Method Summary collapse

Class Method Details

.mainObject



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

def self.main()
  safe_create_spec_helper
  safe_create_rakefile
end

.safe_create_rakefileObject



60
61
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
# File 'lib/ansible_spec.rb', line 60

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

# param: inventory file of Ansible
# return: Hash {"active_group_name" => ["192.168.0.1","192.168.0.2"]}
def load_host(file)
if File.exist?(file) == false
  puts 'Error: Please create inventory file. name MUST "hosts"'
  exit
end
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

load_file = YAML.load_file('site.yml')

# 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('hosts')
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



16
17
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
# File 'lib/ansible_spec.rb', line 16

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



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/ansible_spec.rb', line 139

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



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/ansible_spec.rb', line 152

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