serverspec-ruby19 2.24.3
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 +26 -0
- data/.gitmodules +3 -0
- data/.travis.yml +19 -0
- data/Gemfile +8 -0
- data/Guardfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +26 -0
- data/Rakefile +30 -0
- data/WINDOWS_SUPPORT.md +118 -0
- data/appveyor.yml +56 -0
- data/bin/serverspec-init +7 -0
- data/lib/serverspec.rb +63 -0
- data/lib/serverspec/commands/base.rb +7 -0
- data/lib/serverspec/helper.rb +8 -0
- data/lib/serverspec/helper/type.rb +26 -0
- data/lib/serverspec/matcher.rb +39 -0
- data/lib/serverspec/matcher/be_enabled.rb +13 -0
- data/lib/serverspec/matcher/be_enforcing.rb +10 -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_permissive.rb +10 -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 +28 -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/power_assert.rb +14 -0
- data/lib/serverspec/setup.rb +338 -0
- data/lib/serverspec/subject.rb +12 -0
- data/lib/serverspec/type/base.rb +30 -0
- data/lib/serverspec/type/bond.rb +11 -0
- data/lib/serverspec/type/bridge.rb +11 -0
- data/lib/serverspec/type/cgroup.rb +17 -0
- data/lib/serverspec/type/command.rb +20 -0
- data/lib/serverspec/type/cron.rb +15 -0
- data/lib/serverspec/type/default_gateway.rb +15 -0
- data/lib/serverspec/type/docker_base.rb +29 -0
- data/lib/serverspec/type/docker_container.rb +27 -0
- data/lib/serverspec/type/docker_image.rb +4 -0
- data/lib/serverspec/type/file.rb +133 -0
- data/lib/serverspec/type/fstab.rb +11 -0
- data/lib/serverspec/type/group.rb +11 -0
- data/lib/serverspec/type/host.rb +21 -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 +27 -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_audit_system.rb +45 -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/mysql_config.rb +10 -0
- data/lib/serverspec/type/package.rb +66 -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 +52 -0
- data/lib/serverspec/type/user.rb +43 -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/x509_certificate.rb +76 -0
- data/lib/serverspec/type/x509_private_key.rb +21 -0
- data/lib/serverspec/type/yumrepo.rb +11 -0
- data/lib/serverspec/type/zfs.rb +29 -0
- data/lib/serverspec/version.rb +3 -0
- data/serverspec.gemspec +28 -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 +67 -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 +356 -0
- data/spec/type/base/group_spec.rb +11 -0
- data/spec/type/base/host_spec.rb +47 -0
- data/spec/type/base/mail_alias_spec.rb +7 -0
- data/spec/type/base/mysql_config_spec.rb +13 -0
- data/spec/type/base/package_spec.rb +51 -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 +63 -0
- data/spec/type/base/service_spec.rb +49 -0
- data/spec/type/base/user_spec.rb +44 -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/bond_spec.rb +12 -0
- data/spec/type/linux/bridge_spec.rb +12 -0
- data/spec/type/linux/cgroup_spec.rb +13 -0
- data/spec/type/linux/docker_container_pre_1_8_spec.rb +124 -0
- data/spec/type/linux/docker_container_spec.rb +126 -0
- data/spec/type/linux/docker_image_spec.rb +94 -0
- data/spec/type/linux/file_spec.rb +27 -0
- data/spec/type/linux/fstab_spec.rb +31 -0
- data/spec/type/linux/interface_spec.rb +30 -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_audit_system_spec.rb +139 -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 +23 -0
- data/spec/type/linux/x509_certificate_spec.rb +62 -0
- data/spec/type/linux/x509_private_key_spec.rb +31 -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 +120 -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
- data/wercker.yml +1 -0
- metadata +450 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# file
|
|
2
|
+
require 'serverspec/matcher/be_mounted'
|
|
3
|
+
require 'serverspec/matcher/contain'
|
|
4
|
+
require 'serverspec/matcher/be_readable'
|
|
5
|
+
require 'serverspec/matcher/be_writable'
|
|
6
|
+
require 'serverspec/matcher/be_executable'
|
|
7
|
+
|
|
8
|
+
# port
|
|
9
|
+
require 'serverspec/matcher/be_listening'
|
|
10
|
+
|
|
11
|
+
# host
|
|
12
|
+
require 'serverspec/matcher/be_resolvable'
|
|
13
|
+
require 'serverspec/matcher/be_reachable'
|
|
14
|
+
|
|
15
|
+
# package
|
|
16
|
+
require 'serverspec/matcher/be_installed'
|
|
17
|
+
|
|
18
|
+
# selinux
|
|
19
|
+
require 'serverspec/matcher/be_enforcing'
|
|
20
|
+
require 'serverspec/matcher/be_permissive'
|
|
21
|
+
|
|
22
|
+
# service
|
|
23
|
+
require 'serverspec/matcher/be_enabled'
|
|
24
|
+
require 'serverspec/matcher/be_running'
|
|
25
|
+
|
|
26
|
+
# user
|
|
27
|
+
require 'serverspec/matcher/belong_to_group'
|
|
28
|
+
require 'serverspec/matcher/belong_to_primary_group'
|
|
29
|
+
|
|
30
|
+
# ipfiter, ipnat, iptables, ip6tables
|
|
31
|
+
require 'serverspec/matcher/have_rule'
|
|
32
|
+
|
|
33
|
+
# cron, fstab, routing_table
|
|
34
|
+
require 'serverspec/matcher/have_entry'
|
|
35
|
+
|
|
36
|
+
# iis_website
|
|
37
|
+
require 'serverspec/matcher/have_site_application'
|
|
38
|
+
require 'serverspec/matcher/have_site_bindings'
|
|
39
|
+
require 'serverspec/matcher/have_virtual_dir'
|
|
@@ -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,28 @@
|
|
|
1
|
+
RSpec::Matchers.define :contain do |pattern|
|
|
2
|
+
match do |resource|
|
|
3
|
+
if resource.is_a?(String)
|
|
4
|
+
resource.match(Regexp.new([@from, pattern, @to].compact.join.gsub('/', '.*'), Regexp::MULTILINE))
|
|
5
|
+
else
|
|
6
|
+
resource.contain(pattern, @from, @to)
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
# for contain(pattern).from(/A/).to(/B/)
|
|
11
|
+
chain :from do |from|
|
|
12
|
+
@from = Regexp.new(from).inspect
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
chain :to do |to|
|
|
16
|
+
@to = Regexp.new(to).inspect
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# for contain(pattern).after(/A/)
|
|
20
|
+
chain :after do |after|
|
|
21
|
+
@from = Regexp.new(after).inspect
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# for contain(pattern).before(/B/)
|
|
25
|
+
chain :before do |before|
|
|
26
|
+
@to = Regexp.new(before).inspect
|
|
27
|
+
end
|
|
28
|
+
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,14 @@
|
|
|
1
|
+
require 'test/unit'
|
|
2
|
+
require 'specinfra'
|
|
3
|
+
|
|
4
|
+
require 'serverspec/helper/type'
|
|
5
|
+
|
|
6
|
+
PowerAssert.configure do |c|
|
|
7
|
+
c.lazy_inspection = true
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
class Serverspec::TestCase < Test::Unit::TestCase
|
|
11
|
+
extend Serverspec::Helper::Type
|
|
12
|
+
include Serverspec::Helper::Type
|
|
13
|
+
end
|
|
14
|
+
|
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
require 'pathname'
|
|
2
|
+
require 'fileutils'
|
|
3
|
+
require 'erb'
|
|
4
|
+
|
|
5
|
+
module Serverspec
|
|
6
|
+
class Setup
|
|
7
|
+
def self.run
|
|
8
|
+
|
|
9
|
+
ask_os_type
|
|
10
|
+
|
|
11
|
+
if @os_type == 'UN*X'
|
|
12
|
+
ask_unix_backend
|
|
13
|
+
else
|
|
14
|
+
ask_windows_backend
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
if @backend_type == 'ssh'
|
|
18
|
+
print 'Vagrant instance y/n: '
|
|
19
|
+
@vagrant = $stdin.gets.chomp
|
|
20
|
+
if @vagrant =~ (/(true|t|yes|y|1)$/i)
|
|
21
|
+
@vagrant = true
|
|
22
|
+
print 'Auto-configure Vagrant from Vagrantfile? y/n: '
|
|
23
|
+
auto_config = $stdin.gets.chomp
|
|
24
|
+
if auto_config =~ (/(true|t|yes|y|1)$/i)
|
|
25
|
+
auto_vagrant_configuration
|
|
26
|
+
else
|
|
27
|
+
print('Input vagrant instance name: ')
|
|
28
|
+
@hostname = $stdin.gets.chomp
|
|
29
|
+
end
|
|
30
|
+
else
|
|
31
|
+
@vagrant = false
|
|
32
|
+
print('Input target host name: ')
|
|
33
|
+
@hostname = $stdin.gets.chomp
|
|
34
|
+
end
|
|
35
|
+
elsif @backend_type == 'winrm'
|
|
36
|
+
print('Input target host name: ')
|
|
37
|
+
@hostname = $stdin.gets.chomp
|
|
38
|
+
else
|
|
39
|
+
@hostname = 'localhost'
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
['spec', "spec/#{@hostname}"].each { |dir| safe_mkdir(dir) }
|
|
43
|
+
safe_create_spec
|
|
44
|
+
safe_create_spec_helper
|
|
45
|
+
safe_create_rakefile
|
|
46
|
+
safe_create_dotrspec
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def self.ask_os_type
|
|
50
|
+
prompt = <<-EOF
|
|
51
|
+
Select OS type:
|
|
52
|
+
|
|
53
|
+
1) UN*X
|
|
54
|
+
2) Windows
|
|
55
|
+
|
|
56
|
+
Select number:
|
|
57
|
+
EOF
|
|
58
|
+
|
|
59
|
+
print prompt.chop
|
|
60
|
+
num = $stdin.gets.to_i - 1
|
|
61
|
+
puts
|
|
62
|
+
|
|
63
|
+
@os_type = ['UN*X', 'Windows'][num] || 'UN*X'
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def self.ask_unix_backend
|
|
67
|
+
prompt = <<-EOF
|
|
68
|
+
Select a backend type:
|
|
69
|
+
|
|
70
|
+
1) SSH
|
|
71
|
+
2) Exec (local)
|
|
72
|
+
|
|
73
|
+
Select number:
|
|
74
|
+
EOF
|
|
75
|
+
print prompt.chop
|
|
76
|
+
num = $stdin.gets.to_i - 1
|
|
77
|
+
puts
|
|
78
|
+
|
|
79
|
+
@backend_type = ['ssh', 'exec'][num] || 'exec'
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def self.ask_windows_backend
|
|
83
|
+
prompt = <<-EOF
|
|
84
|
+
Select a backend type:
|
|
85
|
+
|
|
86
|
+
1) WinRM
|
|
87
|
+
2) Cmd (local)
|
|
88
|
+
|
|
89
|
+
Select number:
|
|
90
|
+
EOF
|
|
91
|
+
print prompt.chop
|
|
92
|
+
num = $stdin.gets.to_i - 1
|
|
93
|
+
puts
|
|
94
|
+
|
|
95
|
+
@backend_type = ['winrm', 'cmd'][num] || 'exec'
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def self.safe_create_spec
|
|
99
|
+
content = <<-EOF
|
|
100
|
+
require 'spec_helper'
|
|
101
|
+
|
|
102
|
+
describe package('httpd'), :if => os[:family] == 'redhat' do
|
|
103
|
+
it { should be_installed }
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
describe package('apache2'), :if => os[:family] == 'ubuntu' do
|
|
107
|
+
it { should be_installed }
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
describe service('httpd'), :if => os[:family] == 'redhat' do
|
|
111
|
+
it { should be_enabled }
|
|
112
|
+
it { should be_running }
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
describe service('apache2'), :if => os[:family] == 'ubuntu' do
|
|
116
|
+
it { should be_enabled }
|
|
117
|
+
it { should be_running }
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
describe service('org.apache.httpd'), :if => os[:family] == 'darwin' do
|
|
121
|
+
it { should be_enabled }
|
|
122
|
+
it { should be_running }
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
describe port(80) do
|
|
126
|
+
it { should be_listening }
|
|
127
|
+
end
|
|
128
|
+
EOF
|
|
129
|
+
|
|
130
|
+
if File.exists? "spec/#{@hostname}/sample_spec.rb"
|
|
131
|
+
old_content = File.read("spec/#{@hostname}/sample_spec.rb")
|
|
132
|
+
if old_content != content
|
|
133
|
+
$stderr.puts "!! spec/#{@hostname}/sample_spec.rb already exists and differs from template"
|
|
134
|
+
end
|
|
135
|
+
else
|
|
136
|
+
File.open("spec/#{@hostname}/sample_spec.rb", 'w') do |f|
|
|
137
|
+
f.puts content
|
|
138
|
+
end
|
|
139
|
+
puts " + spec/#{@hostname}/sample_spec.rb"
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def self.safe_mkdir(dir)
|
|
144
|
+
if File.exists? dir
|
|
145
|
+
unless File.directory? dir
|
|
146
|
+
$stderr.puts "!! #{dir} already exists and is not a directory"
|
|
147
|
+
end
|
|
148
|
+
else
|
|
149
|
+
FileUtils.mkdir dir
|
|
150
|
+
puts " + #{dir}/"
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def self.safe_create_spec_helper
|
|
155
|
+
content = ERB.new(spec_helper_template, nil, '-').result(binding)
|
|
156
|
+
if File.exists? 'spec/spec_helper.rb'
|
|
157
|
+
old_content = File.read('spec/spec_helper.rb')
|
|
158
|
+
if old_content != content
|
|
159
|
+
$stderr.puts "!! spec/spec_helper.rb already exists and differs from template"
|
|
160
|
+
end
|
|
161
|
+
else
|
|
162
|
+
File.open('spec/spec_helper.rb', 'w') do |f|
|
|
163
|
+
f.puts content
|
|
164
|
+
end
|
|
165
|
+
puts ' + spec/spec_helper.rb'
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def self.safe_create_rakefile
|
|
170
|
+
content = <<-'EOF'
|
|
171
|
+
require 'rake'
|
|
172
|
+
require 'rspec/core/rake_task'
|
|
173
|
+
|
|
174
|
+
task :spec => 'spec:all'
|
|
175
|
+
task :default => :spec
|
|
176
|
+
|
|
177
|
+
namespace :spec do
|
|
178
|
+
targets = []
|
|
179
|
+
Dir.glob('./spec/*').each do |dir|
|
|
180
|
+
next unless File.directory?(dir)
|
|
181
|
+
target = File.basename(dir)
|
|
182
|
+
target = "_#{target}" if target == "default"
|
|
183
|
+
targets << target
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
task :all => targets
|
|
187
|
+
task :default => :all
|
|
188
|
+
|
|
189
|
+
targets.each do |target|
|
|
190
|
+
original_target = target == "_default" ? target[1..-1] : target
|
|
191
|
+
desc "Run serverspec tests to #{original_target}"
|
|
192
|
+
RSpec::Core::RakeTask.new(target.to_sym) do |t|
|
|
193
|
+
ENV['TARGET_HOST'] = original_target
|
|
194
|
+
t.pattern = "spec/#{original_target}/*_spec.rb"
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
EOF
|
|
199
|
+
if File.exists? 'Rakefile'
|
|
200
|
+
old_content = File.read('Rakefile')
|
|
201
|
+
if old_content != content
|
|
202
|
+
$stderr.puts '!! Rakefile already exists and differs from template'
|
|
203
|
+
end
|
|
204
|
+
else
|
|
205
|
+
File.open('Rakefile', 'w') do |f|
|
|
206
|
+
f.puts content
|
|
207
|
+
end
|
|
208
|
+
puts ' + Rakefile'
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
def self.find_vagrantfile
|
|
213
|
+
Pathname.new(Dir.pwd).ascend do |dir|
|
|
214
|
+
path = File.expand_path('Vagrantfile', dir)
|
|
215
|
+
return path if File.exists?(path)
|
|
216
|
+
end
|
|
217
|
+
nil
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
def self.auto_vagrant_configuration
|
|
221
|
+
if find_vagrantfile
|
|
222
|
+
vagrant_list = `vagrant status`
|
|
223
|
+
list_of_vms = []
|
|
224
|
+
if vagrant_list != ''
|
|
225
|
+
vagrant_list.each_line do |line|
|
|
226
|
+
if match = /([\w-]+[\s]+)(created|aborted|not created|poweroff|running|saved)[\s](\(virtualbox\)|\(vmware\)|\(vmware_fusion\)|\(libvirt\))/.match(line)
|
|
227
|
+
list_of_vms << match[1].strip!
|
|
228
|
+
end
|
|
229
|
+
end
|
|
230
|
+
if list_of_vms.length == 1
|
|
231
|
+
@hostname = list_of_vms[0]
|
|
232
|
+
else
|
|
233
|
+
list_of_vms.each_with_index { |vm, index | puts "#{index}) #{vm}\n" }
|
|
234
|
+
print 'Choose a VM from the Vagrantfile: '
|
|
235
|
+
chosen_vm = $stdin.gets.chomp
|
|
236
|
+
@hostname = list_of_vms[chosen_vm.to_i]
|
|
237
|
+
end
|
|
238
|
+
else
|
|
239
|
+
$stderr.puts 'Vagrant status error - Check your Vagrantfile or .vagrant'
|
|
240
|
+
exit 1
|
|
241
|
+
end
|
|
242
|
+
else
|
|
243
|
+
$stderr.puts 'Vagrantfile not found in directory!'
|
|
244
|
+
exit 1
|
|
245
|
+
end
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
def self.spec_helper_template
|
|
249
|
+
template = <<-'EOF'
|
|
250
|
+
require 'serverspec'
|
|
251
|
+
<% if @backend_type == 'ssh' -%>
|
|
252
|
+
require 'net/ssh'
|
|
253
|
+
<% end -%>
|
|
254
|
+
<%- if @vagrant -%>
|
|
255
|
+
require 'tempfile'
|
|
256
|
+
<% end -%>
|
|
257
|
+
<% if @backend_type == 'winrm' -%>
|
|
258
|
+
require 'winrm'
|
|
259
|
+
<% end -%>
|
|
260
|
+
|
|
261
|
+
set :backend, :<%= @backend_type %>
|
|
262
|
+
|
|
263
|
+
<% if @os_type == 'UN*X' && @backend_type == 'ssh' -%>
|
|
264
|
+
if ENV['ASK_SUDO_PASSWORD']
|
|
265
|
+
begin
|
|
266
|
+
require 'highline/import'
|
|
267
|
+
rescue LoadError
|
|
268
|
+
fail "highline is not available. Try installing it."
|
|
269
|
+
end
|
|
270
|
+
set :sudo_password, ask("Enter sudo password: ") { |q| q.echo = false }
|
|
271
|
+
else
|
|
272
|
+
set :sudo_password, ENV['SUDO_PASSWORD']
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
<%- if @backend_type == 'ssh' -%>
|
|
276
|
+
host = ENV['TARGET_HOST']
|
|
277
|
+
|
|
278
|
+
<%- if @vagrant -%>
|
|
279
|
+
`vagrant up #{host}`
|
|
280
|
+
|
|
281
|
+
config = Tempfile.new('', Dir.tmpdir)
|
|
282
|
+
config.write(`vagrant ssh-config #{host}`)
|
|
283
|
+
config.close
|
|
284
|
+
|
|
285
|
+
options = Net::SSH::Config.for(host, [config.path])
|
|
286
|
+
<%- else -%>
|
|
287
|
+
options = Net::SSH::Config.for(host)
|
|
288
|
+
<%- end -%>
|
|
289
|
+
|
|
290
|
+
options[:user] ||= Etc.getlogin
|
|
291
|
+
|
|
292
|
+
set :host, options[:host_name] || host
|
|
293
|
+
set :ssh_options, options
|
|
294
|
+
|
|
295
|
+
# Disable sudo
|
|
296
|
+
# set :disable_sudo, true
|
|
297
|
+
<%- end -%>
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
# Set environment variables
|
|
301
|
+
# set :env, :LANG => 'C', :LC_MESSAGES => 'C'
|
|
302
|
+
|
|
303
|
+
# Set PATH
|
|
304
|
+
# set :path, '/sbin:/usr/local/sbin:$PATH'
|
|
305
|
+
<%- end -%>
|
|
306
|
+
<% if @backend_type == 'winrm'-%>
|
|
307
|
+
user = <username>
|
|
308
|
+
pass = <password>
|
|
309
|
+
endpoint = "http://#{ENV['TARGET_HOST']}:5985/wsman"
|
|
310
|
+
|
|
311
|
+
winrm = ::WinRM::WinRMWebService.new(endpoint, :ssl, :user => user, :pass => pass, :basic_auth_only => true)
|
|
312
|
+
winrm.set_timeout 300 # 5 minutes max timeout for any operation
|
|
313
|
+
Specinfra.configuration.winrm = winrm
|
|
314
|
+
<% end -%>
|
|
315
|
+
EOF
|
|
316
|
+
template
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
def self.safe_create_dotrspec
|
|
320
|
+
content = <<-'EOF'
|
|
321
|
+
--color
|
|
322
|
+
--format documentation
|
|
323
|
+
EOF
|
|
324
|
+
if File.exists? '.rspec'
|
|
325
|
+
old_content = File.read('.rspec')
|
|
326
|
+
if old_content != content
|
|
327
|
+
$stderr.puts '!! .rspec already exists and differs from template'
|
|
328
|
+
end
|
|
329
|
+
else
|
|
330
|
+
File.open('.rspec', 'w') do |f|
|
|
331
|
+
f.puts content
|
|
332
|
+
end
|
|
333
|
+
puts ' + .rspec'
|
|
334
|
+
end
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
end
|
|
338
|
+
end
|