serverspec-clc 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +26 -0
- data/.gitmodules +3 -0
- data/.travis.yml +14 -0
- data/Gemfile +8 -0
- data/Guardfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +15 -0
- data/Rakefile +30 -0
- data/WINDOWS_SUPPORT.md +116 -0
- data/bin/serverspec-init +7 -0
- data/lib/serverspec.rb +33 -0
- data/lib/serverspec/commands/base.rb +7 -0
- data/lib/serverspec/helper.rb +8 -0
- data/lib/serverspec/helper/type.rb +23 -0
- data/lib/serverspec/matcher.rb +35 -0
- data/lib/serverspec/matcher/be_enabled.rb +13 -0
- data/lib/serverspec/matcher/be_executable.rb +13 -0
- data/lib/serverspec/matcher/be_installed.rb +13 -0
- data/lib/serverspec/matcher/be_listening.rb +13 -0
- data/lib/serverspec/matcher/be_mounted.rb +15 -0
- data/lib/serverspec/matcher/be_reachable.rb +17 -0
- data/lib/serverspec/matcher/be_readable.rb +13 -0
- data/lib/serverspec/matcher/be_resolvable.rb +9 -0
- data/lib/serverspec/matcher/be_running.rb +13 -0
- data/lib/serverspec/matcher/be_writable.rb +13 -0
- data/lib/serverspec/matcher/belong_to_group.rb +5 -0
- data/lib/serverspec/matcher/belong_to_primary_group.rb +5 -0
- data/lib/serverspec/matcher/contain.rb +24 -0
- data/lib/serverspec/matcher/have_entry.rb +14 -0
- data/lib/serverspec/matcher/have_rule.rb +17 -0
- data/lib/serverspec/matcher/have_site_application.rb +18 -0
- data/lib/serverspec/matcher/have_site_bindings.rb +22 -0
- data/lib/serverspec/matcher/have_virtual_dir.rb +14 -0
- data/lib/serverspec/setup.rb +333 -0
- data/lib/serverspec/subject.rb +12 -0
- data/lib/serverspec/type/base.rb +21 -0
- data/lib/serverspec/type/cgroup.rb +17 -0
- data/lib/serverspec/type/command.rb +20 -0
- data/lib/serverspec/type/cron.rb +11 -0
- data/lib/serverspec/type/default_gateway.rb +15 -0
- data/lib/serverspec/type/file.rb +113 -0
- data/lib/serverspec/type/group.rb +11 -0
- data/lib/serverspec/type/host.rb +15 -0
- data/lib/serverspec/type/iis_app_pool.rb +43 -0
- data/lib/serverspec/type/iis_website.rb +40 -0
- data/lib/serverspec/type/interface.rb +18 -0
- data/lib/serverspec/type/ip6tables.rb +13 -0
- data/lib/serverspec/type/ipfilter.rb +11 -0
- data/lib/serverspec/type/ipnat.rb +11 -0
- data/lib/serverspec/type/iptables.rb +11 -0
- data/lib/serverspec/type/kernel_module.rb +7 -0
- data/lib/serverspec/type/linux_kernel_parameter.rb +10 -0
- data/lib/serverspec/type/lxc.rb +15 -0
- data/lib/serverspec/type/mail_alias.rb +7 -0
- data/lib/serverspec/type/package.rb +65 -0
- data/lib/serverspec/type/php_config.rb +10 -0
- data/lib/serverspec/type/port.rb +40 -0
- data/lib/serverspec/type/ppa.rb +11 -0
- data/lib/serverspec/type/process.rb +27 -0
- data/lib/serverspec/type/routing_table.rb +11 -0
- data/lib/serverspec/type/selinux.rb +19 -0
- data/lib/serverspec/type/selinux_module.rb +11 -0
- data/lib/serverspec/type/service.rb +33 -0
- data/lib/serverspec/type/user.rb +31 -0
- data/lib/serverspec/type/windows_feature.rb +7 -0
- data/lib/serverspec/type/windows_hot_fix.rb +7 -0
- data/lib/serverspec/type/windows_registry_key.rb +19 -0
- data/lib/serverspec/type/windows_scheduled_task.rb +7 -0
- data/lib/serverspec/type/yumrepo.rb +11 -0
- data/lib/serverspec/type/zfs.rb +15 -0
- data/lib/serverspec/version.rb +3 -0
- data/serverspec.gemspec +26 -0
- data/spec/helper/type_spec.rb +6 -0
- data/spec/spec_helper.rb +28 -0
- data/spec/type/aix/file_spec.rb +31 -0
- data/spec/type/aix/group_spec.rb +7 -0
- data/spec/type/aix/package_spec.rb +7 -0
- data/spec/type/aix/port_spec.rb +20 -0
- data/spec/type/aix/service_spec.rb +12 -0
- data/spec/type/aix/user_spec.rb +16 -0
- data/spec/type/arch/file_spec.rb +15 -0
- data/spec/type/arch/package_spec.rb +19 -0
- data/spec/type/arch/service_spec.rb +9 -0
- data/spec/type/base/command_spec.rb +62 -0
- data/spec/type/base/cron_spec.rb +11 -0
- data/spec/type/base/default_gateway_spec.rb +11 -0
- data/spec/type/base/file_spec.rb +336 -0
- data/spec/type/base/group_spec.rb +11 -0
- data/spec/type/base/host_spec.rb +36 -0
- data/spec/type/base/mail_alias_spec.rb +7 -0
- data/spec/type/base/package_spec.rb +43 -0
- data/spec/type/base/php_config_spec.rb +33 -0
- data/spec/type/base/port_spec.rb +34 -0
- data/spec/type/base/process_spec.rb +35 -0
- data/spec/type/base/routing_table_spec.rb +118 -0
- data/spec/type/base/service_spec.rb +45 -0
- data/spec/type/base/user_spec.rb +31 -0
- data/spec/type/darwin/file_spec.rb +41 -0
- data/spec/type/darwin/package_spec.rb +19 -0
- data/spec/type/darwin/port_spec.rb +27 -0
- data/spec/type/darwin/service_spec.rb +16 -0
- data/spec/type/debian/package_spec.rb +23 -0
- data/spec/type/debian/service_spec.rb +11 -0
- data/spec/type/fedora/service_spec.rb +15 -0
- data/spec/type/fedora15/service_spec.rb +15 -0
- data/spec/type/fedora20/service_spec.rb +15 -0
- data/spec/type/freebsd/file_spec.rb +29 -0
- data/spec/type/freebsd/package_spec.rb +19 -0
- data/spec/type/freebsd/port_spec.rb +27 -0
- data/spec/type/freebsd/service_spec.rb +8 -0
- data/spec/type/freebsd10/package_spec.rb +19 -0
- data/spec/type/gentoo/package_spec.rb +7 -0
- data/spec/type/gentoo/service_spec.rb +12 -0
- data/spec/type/linux/cgroup_spec.rb +13 -0
- data/spec/type/linux/file_spec.rb +21 -0
- data/spec/type/linux/interface_spec.rb +25 -0
- data/spec/type/linux/ip6tables_spec.rb +19 -0
- data/spec/type/linux/iptables_spec.rb +11 -0
- data/spec/type/linux/kernel_module_spec.rb +7 -0
- data/spec/type/linux/linux_kernel_parameter_spec.rb +33 -0
- data/spec/type/linux/lxc_container_spec.rb +12 -0
- data/spec/type/linux/selinux_module_spec.rb +11 -0
- data/spec/type/linux/selinux_spec.rb +15 -0
- data/spec/type/linux/zfs_spec.rb +15 -0
- data/spec/type/nixos/package_spec.rb +15 -0
- data/spec/type/nixos/service_spec.rb +9 -0
- data/spec/type/openbsd/file_spec.rb +134 -0
- data/spec/type/openbsd/interface_spec.rb +21 -0
- data/spec/type/openbsd/mail_alias_spec.rb +7 -0
- data/spec/type/openbsd/package_spec.rb +11 -0
- data/spec/type/openbsd/port_spec.rb +7 -0
- data/spec/type/openbsd/service_spec.rb +13 -0
- data/spec/type/openbsd/user_spec.rb +12 -0
- data/spec/type/opensuse/service_spec.rb +16 -0
- data/spec/type/plamo/package_spec.rb +8 -0
- data/spec/type/plamo/service_spec.rb +8 -0
- data/spec/type/redhat/file_spec.rb +19 -0
- data/spec/type/redhat/package_spec.rb +23 -0
- data/spec/type/redhat/service_spec.rb +11 -0
- data/spec/type/redhat/yumrepo_spec.rb +11 -0
- data/spec/type/redhat5/iptables_spec.rb +11 -0
- data/spec/type/redhat7/service_spec.rb +9 -0
- data/spec/type/smartos/package_spec.rb +19 -0
- data/spec/type/smartos/service_spec.rb +16 -0
- data/spec/type/solaris/cron_spec.rb +11 -0
- data/spec/type/solaris/file_spec.rb +15 -0
- data/spec/type/solaris/group_spec.rb +7 -0
- data/spec/type/solaris/host_spec.rb +19 -0
- data/spec/type/solaris/ipfilter_spec.rb +7 -0
- data/spec/type/solaris/ipnat_spec.rb +7 -0
- data/spec/type/solaris/package_spec.rb +7 -0
- data/spec/type/solaris/port_spec.rb +34 -0
- data/spec/type/solaris/service_spec.rb +16 -0
- data/spec/type/solaris/user_spec.rb +16 -0
- data/spec/type/solaris/zfs_spec.rb +15 -0
- data/spec/type/solaris10/file_spec.rb +313 -0
- data/spec/type/solaris10/group_spec.rb +8 -0
- data/spec/type/solaris10/host_spec.rb +16 -0
- data/spec/type/solaris10/package_spec.rb +8 -0
- data/spec/type/solaris10/user_spec.rb +8 -0
- data/spec/type/suse/package_spec.rb +23 -0
- data/spec/type/suse/service_spec.rb +11 -0
- data/spec/type/ubuntu/ppa_spec.rb +11 -0
- data/spec/type/ubuntu/service_spec.rb +7 -0
- data/spec/type/windows/command_spec.rb +64 -0
- data/spec/type/windows/feature_spec.rb +17 -0
- data/spec/type/windows/file_spec.rb +121 -0
- data/spec/type/windows/group_spec.rb +23 -0
- data/spec/type/windows/host_spec.rb +32 -0
- data/spec/type/windows/hot_fix_spec.rb +22 -0
- data/spec/type/windows/iis_app_pool_spec.rb +17 -0
- data/spec/type/windows/iis_webisite_spec.rb +16 -0
- data/spec/type/windows/package_spec.rb +10 -0
- data/spec/type/windows/port_spec.rb +25 -0
- data/spec/type/windows/registry_key_spec.rb +58 -0
- data/spec/type/windows/scheduled_task_spec.rb +9 -0
- data/spec/type/windows/service_spec.rb +30 -0
- data/spec/type/windows/user_spec.rb +33 -0
- metadata +400 -0
@@ -0,0 +1,17 @@
|
|
1
|
+
RSpec::Matchers.define :be_reachable do
|
2
|
+
match do |host|
|
3
|
+
proto = 'tcp'
|
4
|
+
timeout = 5
|
5
|
+
if @attr
|
6
|
+
port = @attr[:port]
|
7
|
+
proto = @attr[:proto] if @attr[:proto]
|
8
|
+
timeout = @attr[:timeout] if @attr[:timeout]
|
9
|
+
end
|
10
|
+
|
11
|
+
host.reachable?(port, proto, timeout)
|
12
|
+
end
|
13
|
+
|
14
|
+
chain :with do |attr|
|
15
|
+
@attr = attr
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
RSpec::Matchers.define :contain do |pattern|
|
2
|
+
match do |file|
|
3
|
+
file.contain(pattern, @from, @to)
|
4
|
+
end
|
5
|
+
|
6
|
+
# for contain(pattern).from(/A/).to(/B/)
|
7
|
+
chain :from do |from|
|
8
|
+
@from = Regexp.new(from).inspect
|
9
|
+
end
|
10
|
+
|
11
|
+
chain :to do |to|
|
12
|
+
@to = Regexp.new(to).inspect
|
13
|
+
end
|
14
|
+
|
15
|
+
# for contain(pattern).after(/A/)
|
16
|
+
chain :after do |after|
|
17
|
+
@from = Regexp.new(after).inspect
|
18
|
+
end
|
19
|
+
|
20
|
+
# for contain(pattern).before(/B/)
|
21
|
+
chain :before do |before|
|
22
|
+
@to = Regexp.new(before).inspect
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
RSpec::Matchers.define :have_entry do |entry|
|
2
|
+
match do |subject|
|
3
|
+
if subject.class.name == 'Serverspec::Type::Cron'
|
4
|
+
subject.has_entry?(@user, entry)
|
5
|
+
elsif subject.respond_to?(:has_entry?)
|
6
|
+
subject.has_entry?(entry)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
# For cron type
|
11
|
+
chain :with_user do |user|
|
12
|
+
@user = user
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
RSpec::Matchers.define :have_rule do |rule|
|
2
|
+
match do |subject|
|
3
|
+
if subject.class.name == 'Serverspec::Type::Iptables' || subject.class.name == 'Serverspec::Type::Ip6tables'
|
4
|
+
subject.has_rule?(rule, @table, @chain)
|
5
|
+
else
|
6
|
+
subject.has_rule?(rule)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
chain :with_table do |table|
|
11
|
+
@table = table
|
12
|
+
end
|
13
|
+
|
14
|
+
chain :with_chain do |chain|
|
15
|
+
@chain = chain
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
RSpec::Matchers.define :have_site_application do |app|
|
2
|
+
match do |subject|
|
3
|
+
if subject.class.name == 'Serverspec::Type::IisWebsite'
|
4
|
+
subject.has_site_application?(app, @pool, @physical_path)
|
5
|
+
else
|
6
|
+
className = subject.class.name
|
7
|
+
raise "not supported class #{className}"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
chain :with_pool do |pool|
|
12
|
+
@pool = pool
|
13
|
+
end
|
14
|
+
|
15
|
+
chain :with_physical_path do |physical_path|
|
16
|
+
@physical_path = physical_path
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
RSpec::Matchers.define :have_site_bindings do |port|
|
2
|
+
match do |subject|
|
3
|
+
if subject.class.name == 'Serverspec::Type::IisWebsite'
|
4
|
+
subject.has_site_bindings?(port, @protocol, @ipaddress, @host_header)
|
5
|
+
else
|
6
|
+
className = subject.class.name
|
7
|
+
raise "not supported class #{className}"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
chain :with_protocol do |protocol|
|
12
|
+
@protocol = protocol
|
13
|
+
end
|
14
|
+
|
15
|
+
chain :with_ipaddress do |ipaddress|
|
16
|
+
@ipaddress = ipaddress
|
17
|
+
end
|
18
|
+
|
19
|
+
chain :with_host_header do |host_header|
|
20
|
+
@host_header = host_header
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
RSpec::Matchers.define :have_virtual_dir do |vdir|
|
2
|
+
match do |subject|
|
3
|
+
if subject.class.name == 'Serverspec::Type::IisWebsite'
|
4
|
+
subject.has_virtual_dir?(vdir, @path)
|
5
|
+
else
|
6
|
+
className = subject.class.name
|
7
|
+
raise "not supported class #{className}"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
chain :with_path do |path|
|
12
|
+
@path = path
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,333 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'erb'
|
3
|
+
|
4
|
+
module Serverspec
|
5
|
+
class Setup
|
6
|
+
def self.run
|
7
|
+
|
8
|
+
ask_os_type
|
9
|
+
|
10
|
+
if @os_type == 'UN*X'
|
11
|
+
ask_unix_backend
|
12
|
+
else
|
13
|
+
ask_windows_backend
|
14
|
+
end
|
15
|
+
|
16
|
+
if @backend_type == 'ssh'
|
17
|
+
print 'Vagrant instance y/n: '
|
18
|
+
@vagrant = $stdin.gets.chomp
|
19
|
+
if @vagrant =~ (/(true|t|yes|y|1)$/i)
|
20
|
+
@vagrant = true
|
21
|
+
print 'Auto-configure Vagrant from Vagrantfile? y/n: '
|
22
|
+
auto_config = $stdin.gets.chomp
|
23
|
+
if auto_config =~ (/(true|t|yes|y|1)$/i)
|
24
|
+
auto_vagrant_configuration
|
25
|
+
else
|
26
|
+
print('Input vagrant instance name: ')
|
27
|
+
@hostname = $stdin.gets.chomp
|
28
|
+
end
|
29
|
+
else
|
30
|
+
@vagrant = false
|
31
|
+
print('Input target host name: ')
|
32
|
+
@hostname = $stdin.gets.chomp
|
33
|
+
end
|
34
|
+
elsif @backend_type == 'winrm'
|
35
|
+
print('Input target host name: ')
|
36
|
+
@hostname = $stdin.gets.chomp
|
37
|
+
else
|
38
|
+
@hostname = 'localhost'
|
39
|
+
end
|
40
|
+
|
41
|
+
['spec', "spec/#{@hostname}"].each { |dir| safe_mkdir(dir) }
|
42
|
+
safe_create_spec
|
43
|
+
safe_create_spec_helper
|
44
|
+
safe_create_rakefile
|
45
|
+
safe_create_dotrspec
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.ask_os_type
|
49
|
+
prompt = <<-EOF
|
50
|
+
Select OS type:
|
51
|
+
|
52
|
+
1) UN*X
|
53
|
+
2) Windows
|
54
|
+
|
55
|
+
Select number:
|
56
|
+
EOF
|
57
|
+
|
58
|
+
print prompt.chop
|
59
|
+
num = $stdin.gets.to_i - 1
|
60
|
+
puts
|
61
|
+
|
62
|
+
@os_type = ['UN*X', 'Windows'][num] || 'UN*X'
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.ask_unix_backend
|
66
|
+
prompt = <<-EOF
|
67
|
+
Select a backend type:
|
68
|
+
|
69
|
+
1) SSH
|
70
|
+
2) Exec (local)
|
71
|
+
|
72
|
+
Select number:
|
73
|
+
EOF
|
74
|
+
print prompt.chop
|
75
|
+
num = $stdin.gets.to_i - 1
|
76
|
+
puts
|
77
|
+
|
78
|
+
@backend_type = ['ssh', 'exec'][num] || 'exec'
|
79
|
+
end
|
80
|
+
|
81
|
+
def self.ask_windows_backend
|
82
|
+
prompt = <<-EOF
|
83
|
+
Select a backend type:
|
84
|
+
|
85
|
+
1) WinRM
|
86
|
+
2) Cmd (local)
|
87
|
+
|
88
|
+
Select number:
|
89
|
+
EOF
|
90
|
+
print prompt.chop
|
91
|
+
num = $stdin.gets.to_i - 1
|
92
|
+
puts
|
93
|
+
|
94
|
+
@backend_type = ['winrm', 'cmd'][num] || 'exec'
|
95
|
+
end
|
96
|
+
|
97
|
+
def self.safe_create_spec
|
98
|
+
content = <<-EOF
|
99
|
+
require 'spec_helper'
|
100
|
+
|
101
|
+
describe package('httpd'), :if => os[:family] == 'redhat' do
|
102
|
+
it { should be_installed }
|
103
|
+
end
|
104
|
+
|
105
|
+
describe package('apache2'), :if => os[:family] == 'ubuntu' do
|
106
|
+
it { should be_installed }
|
107
|
+
end
|
108
|
+
|
109
|
+
describe service('httpd'), :if => os[:family] == 'redhat' do
|
110
|
+
it { should be_enabled }
|
111
|
+
it { should be_running }
|
112
|
+
end
|
113
|
+
|
114
|
+
describe service('apache2'), :if => os[:family] == 'ubuntu' do
|
115
|
+
it { should be_enabled }
|
116
|
+
it { should be_running }
|
117
|
+
end
|
118
|
+
|
119
|
+
describe service('org.apache.httpd'), :if => os[:family] == 'darwin' do
|
120
|
+
it { should be_enabled }
|
121
|
+
it { should be_running }
|
122
|
+
end
|
123
|
+
|
124
|
+
describe port(80) do
|
125
|
+
it { should be_listening }
|
126
|
+
end
|
127
|
+
EOF
|
128
|
+
|
129
|
+
if File.exists? "spec/#{@hostname}/sample_spec.rb"
|
130
|
+
old_content = File.read("spec/#{@hostname}/sample_spec.rb")
|
131
|
+
if old_content != content
|
132
|
+
$stderr.puts "!! spec/#{@hostname}/sample_spec.rb already exists and differs from template"
|
133
|
+
end
|
134
|
+
else
|
135
|
+
File.open("spec/#{@hostname}/sample_spec.rb", 'w') do |f|
|
136
|
+
f.puts content
|
137
|
+
end
|
138
|
+
puts " + spec/#{@hostname}/sample_spec.rb"
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
def self.safe_mkdir(dir)
|
143
|
+
if File.exists? dir
|
144
|
+
unless File.directory? dir
|
145
|
+
$stderr.puts "!! #{dir} already exists and is not a directory"
|
146
|
+
end
|
147
|
+
else
|
148
|
+
FileUtils.mkdir dir
|
149
|
+
puts " + #{dir}/"
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def self.safe_create_spec_helper
|
154
|
+
content = ERB.new(spec_helper_template, nil, '-').result(binding)
|
155
|
+
if File.exists? 'spec/spec_helper.rb'
|
156
|
+
old_content = File.read('spec/spec_helper.rb')
|
157
|
+
if old_content != content
|
158
|
+
$stderr.puts "!! spec/spec_helper.rb already exists and differs from template"
|
159
|
+
end
|
160
|
+
else
|
161
|
+
File.open('spec/spec_helper.rb', 'w') do |f|
|
162
|
+
f.puts content
|
163
|
+
end
|
164
|
+
puts ' + spec/spec_helper.rb'
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
def self.safe_create_rakefile
|
169
|
+
content = <<-'EOF'
|
170
|
+
require 'rake'
|
171
|
+
require 'rspec/core/rake_task'
|
172
|
+
|
173
|
+
task :spec => 'spec:all'
|
174
|
+
task :default => :spec
|
175
|
+
|
176
|
+
namespace :spec do
|
177
|
+
targets = []
|
178
|
+
Dir.glob('./spec/*').each do |dir|
|
179
|
+
next unless File.directory?(dir)
|
180
|
+
targets << File.basename(dir)
|
181
|
+
end
|
182
|
+
|
183
|
+
task :all => targets
|
184
|
+
task :default => :all
|
185
|
+
|
186
|
+
targets.each do |target|
|
187
|
+
desc "Run serverspec tests to #{target}"
|
188
|
+
RSpec::Core::RakeTask.new(target.to_sym) do |t|
|
189
|
+
ENV['TARGET_HOST'] = target
|
190
|
+
t.pattern = "spec/#{target}/*_spec.rb"
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
194
|
+
EOF
|
195
|
+
if File.exists? 'Rakefile'
|
196
|
+
old_content = File.read('Rakefile')
|
197
|
+
if old_content != content
|
198
|
+
$stderr.puts '!! Rakefile already exists and differs from template'
|
199
|
+
end
|
200
|
+
else
|
201
|
+
File.open('Rakefile', 'w') do |f|
|
202
|
+
f.puts content
|
203
|
+
end
|
204
|
+
puts ' + Rakefile'
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
def self.find_vagrantfile
|
209
|
+
Pathname.new(Dir.pwd).ascend do |dir|
|
210
|
+
path = File.expand_path('Vagrantfile', dir)
|
211
|
+
return path if File.exists?(path)
|
212
|
+
end
|
213
|
+
nil
|
214
|
+
end
|
215
|
+
|
216
|
+
def self.auto_vagrant_configuration
|
217
|
+
if find_vagrantfile
|
218
|
+
vagrant_list = `vagrant status`
|
219
|
+
list_of_vms = []
|
220
|
+
if vagrant_list != ''
|
221
|
+
vagrant_list.each_line do |line|
|
222
|
+
if match = /([\w-]+[\s]+)(created|aborted|not created|poweroff|running|saved)[\s](\(virtualbox\)|\(vmware\)|\(vmware_fusion\))/.match(line)
|
223
|
+
list_of_vms << match[1].strip!
|
224
|
+
end
|
225
|
+
end
|
226
|
+
if list_of_vms.length == 1
|
227
|
+
@hostname = list_of_vms[0]
|
228
|
+
else
|
229
|
+
list_of_vms.each_with_index { |vm, index | puts "#{index}) #{vm}\n" }
|
230
|
+
print 'Choose a VM from the Vagrantfile: '
|
231
|
+
chosen_vm = $stdin.gets.chomp
|
232
|
+
@hostname = list_of_vms[chosen_vm.to_i]
|
233
|
+
end
|
234
|
+
else
|
235
|
+
$stderr.puts 'Vagrant status error - Check your Vagrantfile or .vagrant'
|
236
|
+
exit 1
|
237
|
+
end
|
238
|
+
else
|
239
|
+
$stderr.puts 'Vagrantfile not found in directory!'
|
240
|
+
exit 1
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
def self.spec_helper_template
|
245
|
+
template = <<-'EOF'
|
246
|
+
require 'serverspec'
|
247
|
+
<% if @backend_type == 'ssh' -%>
|
248
|
+
require 'net/ssh'
|
249
|
+
<% end -%>
|
250
|
+
<%- if @vagrant -%>
|
251
|
+
require 'tempfile'
|
252
|
+
<% end -%>
|
253
|
+
<% if @backend_type == 'winrm' -%>
|
254
|
+
require 'winrm'
|
255
|
+
<% end -%>
|
256
|
+
|
257
|
+
set :backend, :<%= @backend_type %>
|
258
|
+
|
259
|
+
<% if @os_type == 'UN*X' && @backend_type == 'ssh' -%>
|
260
|
+
if ENV['ASK_SUDO_PASSWORD']
|
261
|
+
begin
|
262
|
+
require 'highline/import'
|
263
|
+
rescue LoadError
|
264
|
+
fail "highline is not available. Try installing it."
|
265
|
+
end
|
266
|
+
set :sudo_password, ask("Enter sudo password: ") { |q| q.echo = false }
|
267
|
+
else
|
268
|
+
set :sudo_password, ENV['SUDO_PASSWORD']
|
269
|
+
end
|
270
|
+
|
271
|
+
<%- if @backend_type == 'ssh' -%>
|
272
|
+
host = ENV['TARGET_HOST']
|
273
|
+
|
274
|
+
<%- if @vagrant -%>
|
275
|
+
`vagrant up #{host}`
|
276
|
+
|
277
|
+
config = Tempfile.new('', Dir.tmpdir)
|
278
|
+
`vagrant ssh-config #{host} > #{config.path}`
|
279
|
+
|
280
|
+
options = Net::SSH::Config.for(host, [config.path])
|
281
|
+
<%- else -%>
|
282
|
+
options = Net::SSH::Config.for(host)
|
283
|
+
<%- end -%>
|
284
|
+
|
285
|
+
options[:user] ||= Etc.getlogin
|
286
|
+
|
287
|
+
set :host, options[:host_name] || host
|
288
|
+
set :ssh_options, options
|
289
|
+
|
290
|
+
# Disable sudo
|
291
|
+
# set :disable_sudo, true
|
292
|
+
<%- end -%>
|
293
|
+
|
294
|
+
|
295
|
+
# Set environment variables
|
296
|
+
# set :env, :LANG => 'C', :LC_MESSAGES => 'C'
|
297
|
+
|
298
|
+
# Set PATH
|
299
|
+
# set :path, '/sbin:/usr/local/sbin:$PATH'
|
300
|
+
<%- end -%>
|
301
|
+
<% if @backend_type == 'winrm'-%>
|
302
|
+
user = <username>
|
303
|
+
pass = <password>
|
304
|
+
endpoint = "http://#{ENV['TARGET_HOST']}:5985/wsman"
|
305
|
+
|
306
|
+
winrm = ::WinRM::WinRMWebService.new(endpoint, :ssl, :user => user, :pass => pass, :basic_auth_only => true)
|
307
|
+
winrm.set_timeout 300 # 5 minutes max timeout for any operation
|
308
|
+
Specinfra.configuration.winrm = winrm
|
309
|
+
<% end -%>
|
310
|
+
EOF
|
311
|
+
template
|
312
|
+
end
|
313
|
+
|
314
|
+
def self.safe_create_dotrspec
|
315
|
+
content = <<-'EOF'
|
316
|
+
--color
|
317
|
+
--format documentation
|
318
|
+
EOF
|
319
|
+
if File.exists? '.rspec'
|
320
|
+
old_content = File.read('.rspec')
|
321
|
+
if old_content != content
|
322
|
+
$stderr.puts '!! .rspec already exists and differs from template'
|
323
|
+
end
|
324
|
+
else
|
325
|
+
File.open('.rspec', 'w') do |f|
|
326
|
+
f.puts content
|
327
|
+
end
|
328
|
+
puts ' + .rspec'
|
329
|
+
end
|
330
|
+
end
|
331
|
+
|
332
|
+
end
|
333
|
+
end
|