configspec 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +75 -2
- data/configspec.gemspec +1 -0
- data/lib/configspec/backend/dockerfile.rb +1 -1
- data/lib/configspec/backend.rb +0 -3
- data/lib/configspec/helper.rb +0 -9
- data/lib/configspec/setup.rb +6 -2
- data/lib/configspec/version.rb +1 -1
- data/lib/configspec.rb +2 -26
- metadata +16 -13
- data/lib/configspec/backend/base.rb +0 -31
- data/lib/configspec/backend/exec.rb +0 -206
- data/lib/configspec/backend/ssh.rb +0 -93
- data/lib/configspec/configuration.rb +0 -12
- data/lib/configspec/helper/configuration.rb +0 -37
- data/lib/configspec/helper/detect_os.rb +0 -19
- data/lib/configspec/helper/exec.rb +0 -14
- data/lib/configspec/helper/properties.rb +0 -14
- data/lib/configspec/helper/redhat.rb +0 -9
- data/lib/configspec/helper/ssh.rb +0 -15
- data/lib/configspec/properties.rb +0 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d13870de5d7dfb1c6b8591b05945391a5867d2e0
|
4
|
+
data.tar.gz: 3244feb1ec286103cd4a6a3b4a3bb266bdfa8f27
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8f33aceb898dbe5383dbbe6da629a6012ee6f16b953504d97ff67c8c8a4c7fd56516fd7a7f6e9a7de5b5a6c9108c532d9aae59b033b9998b9979c517049ac314
|
7
|
+
data.tar.gz: 52374c4bdcab023cb3d0db699cdb8b483ad09689347788cf326a8e8c45c901b1ca0042c707c225dcdbeffb5fb3f1751a8734b513cedce2ad1353337f03a85bf9
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Configspec
|
2
2
|
|
3
|
-
|
3
|
+
A simple configuration management tool powered by RSpec.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -18,7 +18,80 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
```
|
22
|
+
$ configspec-init
|
23
|
+
Select a backend type:
|
24
|
+
|
25
|
+
1) SSH
|
26
|
+
2) Exec (local)
|
27
|
+
|
28
|
+
Select number: 1
|
29
|
+
|
30
|
+
Vagrant instance y/n: y
|
31
|
+
Auto-configure Vagrant from Vagrantfile? y/n: n
|
32
|
+
Input vagrant instance name: www
|
33
|
+
+ spec/
|
34
|
+
+ spec/www/
|
35
|
+
+ spec/www/001_httpd_spec.rb
|
36
|
+
+ spec/spec_helper.rb
|
37
|
+
+ Rakefile
|
38
|
+
|
39
|
+
$ rake spec
|
40
|
+
/opt/boxen/rbenv/versions/2.0.0-p247/bin/ruby -S rspec spec/www/001_httpd_spec.rb
|
41
|
+
|
42
|
+
Package "httpd"
|
43
|
+
should be installed
|
44
|
+
|
45
|
+
Finished in 3.44 seconds
|
46
|
+
1 example, 0 failures
|
47
|
+
```
|
48
|
+
|
49
|
+
The content of ``001_httpd_spec.rb`` is like this.
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
require 'spec_helper'
|
53
|
+
|
54
|
+
describe package('httpd') do
|
55
|
+
it { should be_installed }
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
This spec file runs the command ``yum -y install httpd``.
|
60
|
+
|
61
|
+
Now configspec supports only RedHat based OS and package installing.
|
62
|
+
|
63
|
+
## Other features
|
64
|
+
|
65
|
+
Configspec supports Dockerfile backend.
|
66
|
+
|
67
|
+
```
|
68
|
+
$ configspec-init
|
69
|
+
Select a backend type:
|
70
|
+
|
71
|
+
1) SSH
|
72
|
+
2) Exec (local)
|
73
|
+
3) Dockerfile
|
74
|
+
|
75
|
+
Select number: 3
|
76
|
+
|
77
|
+
+ spec/
|
78
|
+
+ spec/001_httpd_spec.rb
|
79
|
+
+ spec/spec_helper.rb
|
80
|
+
+ Rakefile
|
81
|
+
|
82
|
+
$ rake spec
|
83
|
+
/opt/boxen/rbenv/versions/2.0.0-p247/bin/ruby -S rspec
|
84
|
+
|
85
|
+
Package "httpd"
|
86
|
+
should be installed
|
87
|
+
|
88
|
+
Finished in 0.00229 seconds
|
89
|
+
1 example, 0 failures
|
90
|
+
|
91
|
+
$ cat Dockerfile
|
92
|
+
RUN yum -y install httpd
|
93
|
+
```
|
94
|
+
|
22
95
|
|
23
96
|
## Contributing
|
24
97
|
|
data/configspec.gemspec
CHANGED
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.add_runtime_dependency "net-ssh"
|
22
22
|
spec.add_runtime_dependency "rspec", ">= 2.13.0"
|
23
|
+
spec.add_runtime_dependency "specinfra", ">= 0.0.2"
|
23
24
|
spec.add_development_dependency "bundler", "~> 1.3"
|
24
25
|
spec.add_development_dependency "rake"
|
25
26
|
end
|
data/lib/configspec/backend.rb
CHANGED
data/lib/configspec/helper.rb
CHANGED
@@ -1,14 +1,5 @@
|
|
1
|
-
require 'configspec/helper/exec'
|
2
|
-
require 'configspec/helper/ssh'
|
3
1
|
require 'configspec/helper/dockerfile'
|
4
|
-
require 'configspec/helper/detect_os'
|
5
|
-
require 'configspec/helper/redhat'
|
6
2
|
|
7
3
|
require 'configspec/helper/type'
|
8
4
|
include Configspec::Helper::Type
|
9
5
|
|
10
|
-
require 'configspec/helper/properties'
|
11
|
-
include Configspec::Helper::Properties
|
12
|
-
|
13
|
-
require 'configspec/helper/configuration'
|
14
|
-
|
data/lib/configspec/setup.rb
CHANGED
@@ -212,11 +212,15 @@ require 'net/ssh'
|
|
212
212
|
require 'winrm'
|
213
213
|
<% end -%>
|
214
214
|
|
215
|
+
<% if @backend_type == 'Dockerfile' -%>
|
215
216
|
include Configspec::Helper::<%= @backend_type %>
|
217
|
+
<% else -%>
|
218
|
+
include SpecInfra::Helper::<%= @backend_type %>
|
219
|
+
<% end -%>
|
216
220
|
<% if @os_type == 'UN*X' && @backend_type != 'Dockerfile' -%>
|
217
|
-
include
|
221
|
+
include SpecInfra::Helper::DetectOS
|
218
222
|
<% else -%>
|
219
|
-
include
|
223
|
+
include SpecInfra::Helper::RedHat
|
220
224
|
<% end -%>
|
221
225
|
|
222
226
|
<% if @os_type == 'UN*X' && @backend_type != 'Dockerfile' -%>
|
data/lib/configspec/version.rb
CHANGED
data/lib/configspec.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require "rubygems"
|
2
2
|
require "rspec"
|
3
|
+
require "specinfra"
|
3
4
|
require "configspec/version"
|
4
5
|
require "configspec/setup"
|
5
6
|
require "configspec/helper"
|
@@ -9,29 +10,4 @@ require "configspec/commands/base"
|
|
9
10
|
require "configspec/commands/linux"
|
10
11
|
require "configspec/commands/redhat"
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
include Configspec
|
15
|
-
|
16
|
-
module Configspec
|
17
|
-
class << self
|
18
|
-
def configuration
|
19
|
-
Configspec::Configuration
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
RSpec.configure do |c|
|
25
|
-
c.include(Configspec::Helper::Configuration)
|
26
|
-
|
27
|
-
c.add_setting :ssh, :default => nil
|
28
|
-
c.add_setting :host, :default => nil
|
29
|
-
c.add_setting :os, :default => nil
|
30
|
-
c.add_setting :sudo_password, :default => nil
|
31
|
-
|
32
|
-
Configspec.configuration.defaults.each { |k, v| c.add_setting k, :default => v }
|
33
|
-
|
34
|
-
c.before :each do
|
35
|
-
backend.set_example(example)
|
36
|
-
end
|
37
|
-
end
|
13
|
+
SPEC_TYPE = 'Configspec'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: configspec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gosuke Miyashita
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-11-
|
11
|
+
date: 2013-11-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: net-ssh
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 2.13.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: specinfra
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.0.2
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.0.2
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: bundler
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -83,24 +97,13 @@ files:
|
|
83
97
|
- configspec.gemspec
|
84
98
|
- lib/configspec.rb
|
85
99
|
- lib/configspec/backend.rb
|
86
|
-
- lib/configspec/backend/base.rb
|
87
100
|
- lib/configspec/backend/dockerfile.rb
|
88
|
-
- lib/configspec/backend/exec.rb
|
89
|
-
- lib/configspec/backend/ssh.rb
|
90
101
|
- lib/configspec/commands/base.rb
|
91
102
|
- lib/configspec/commands/linux.rb
|
92
103
|
- lib/configspec/commands/redhat.rb
|
93
|
-
- lib/configspec/configuration.rb
|
94
104
|
- lib/configspec/helper.rb
|
95
|
-
- lib/configspec/helper/configuration.rb
|
96
|
-
- lib/configspec/helper/detect_os.rb
|
97
105
|
- lib/configspec/helper/dockerfile.rb
|
98
|
-
- lib/configspec/helper/exec.rb
|
99
|
-
- lib/configspec/helper/properties.rb
|
100
|
-
- lib/configspec/helper/redhat.rb
|
101
|
-
- lib/configspec/helper/ssh.rb
|
102
106
|
- lib/configspec/helper/type.rb
|
103
|
-
- lib/configspec/properties.rb
|
104
107
|
- lib/configspec/setup.rb
|
105
108
|
- lib/configspec/type/base.rb
|
106
109
|
- lib/configspec/type/package.rb
|
@@ -1,31 +0,0 @@
|
|
1
|
-
require 'singleton'
|
2
|
-
|
3
|
-
module Configspec
|
4
|
-
module Backend
|
5
|
-
class Base
|
6
|
-
include Singleton
|
7
|
-
|
8
|
-
def set_commands(c)
|
9
|
-
@commands = c
|
10
|
-
end
|
11
|
-
|
12
|
-
def set_example(e)
|
13
|
-
@example = e
|
14
|
-
end
|
15
|
-
|
16
|
-
def commands
|
17
|
-
@commands
|
18
|
-
end
|
19
|
-
|
20
|
-
def check_zero(cmd, *args)
|
21
|
-
ret = run_command(commands.send(cmd, *args))
|
22
|
-
ret[:exit_status] == 0
|
23
|
-
end
|
24
|
-
|
25
|
-
# Default action is to call check_zero with args
|
26
|
-
def method_missing(meth, *args, &block)
|
27
|
-
check_zero(meth, *args)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
@@ -1,206 +0,0 @@
|
|
1
|
-
require 'singleton'
|
2
|
-
|
3
|
-
module Configspec
|
4
|
-
module Backend
|
5
|
-
class Exec < Base
|
6
|
-
|
7
|
-
def run_command(cmd, opts={})
|
8
|
-
cmd = build_command(cmd)
|
9
|
-
cmd = add_pre_command(cmd)
|
10
|
-
stdout = `#{build_command(cmd)} 2>&1`
|
11
|
-
# In ruby 1.9, it is possible to use Open3.capture3, but not in 1.8
|
12
|
-
#stdout, stderr, status = Open3.capture3(cmd)
|
13
|
-
|
14
|
-
if @example
|
15
|
-
@example.metadata[:command] = cmd
|
16
|
-
@example.metadata[:stdout] = stdout
|
17
|
-
end
|
18
|
-
|
19
|
-
{ :stdout => stdout, :stderr => nil,
|
20
|
-
:exit_status => $?.exitstatus, :exit_signal => nil }
|
21
|
-
end
|
22
|
-
|
23
|
-
def build_command(cmd)
|
24
|
-
path = Configspec.configuration.path || RSpec.configuration.path
|
25
|
-
if path
|
26
|
-
cmd = "env PATH=#{path}:$PATH #{cmd}"
|
27
|
-
cmd.gsub!(/(\&\&\s*!?\(?\s*)/, "\\1env PATH=#{path}:$PATH ")
|
28
|
-
cmd.gsub!(/(\|\|\s*!?\(?\s*)/, "\\1env PATH=#{path}:$PATH ")
|
29
|
-
end
|
30
|
-
cmd
|
31
|
-
end
|
32
|
-
|
33
|
-
def add_pre_command(cmd)
|
34
|
-
path = Configspec.configuration.path || RSpec.configuration.path
|
35
|
-
if Configspec.configuration.pre_command
|
36
|
-
cmd = "#{Configspec.configuration.pre_command} && #{cmd}"
|
37
|
-
cmd = "env PATH=#{path}:$PATH #{cmd}" if path
|
38
|
-
end
|
39
|
-
cmd
|
40
|
-
end
|
41
|
-
|
42
|
-
def check_running(process)
|
43
|
-
ret = run_command(commands.check_running(process))
|
44
|
-
|
45
|
-
# In Ubuntu, some services are under upstart and "service foo status" returns
|
46
|
-
# exit status 0 even though they are stopped.
|
47
|
-
# So return false if stdout contains "stopped/waiting".
|
48
|
-
return false if ret[:stdout] =~ /stopped\/waiting/
|
49
|
-
|
50
|
-
# If the service is not registered, check by ps command
|
51
|
-
if ret[:exit_status] == 1
|
52
|
-
ret = run_command(commands.check_process(process))
|
53
|
-
end
|
54
|
-
|
55
|
-
ret[:exit_status] == 0
|
56
|
-
end
|
57
|
-
|
58
|
-
def check_monitored_by_monit(process)
|
59
|
-
ret = run_command(commands.check_monitored_by_monit(process))
|
60
|
-
return false unless ret[:stdout] != nil && ret[:exit_status] == 0
|
61
|
-
|
62
|
-
retlines = ret[:stdout].split(/[\r\n]+/).map(&:strip)
|
63
|
-
proc_index = retlines.index("Process '#{process}'")
|
64
|
-
return false unless proc_index
|
65
|
-
|
66
|
-
retlines[proc_index+2].match(/\Amonitoring status\s+monitored\Z/i) != nil
|
67
|
-
end
|
68
|
-
|
69
|
-
def check_readable(file, by_whom)
|
70
|
-
mode = sprintf('%04s',run_command(commands.get_mode(file))[:stdout].strip)
|
71
|
-
mode = mode.split('')
|
72
|
-
mode_octal = mode[0].to_i * 512 + mode[1].to_i * 64 + mode[2].to_i * 8 + mode[3].to_i * 1
|
73
|
-
case by_whom
|
74
|
-
when nil
|
75
|
-
mode_octal & 0444 != 0
|
76
|
-
when 'owner'
|
77
|
-
mode_octal & 0400 != 0
|
78
|
-
when 'group'
|
79
|
-
mode_octal & 0040 != 0
|
80
|
-
when 'others'
|
81
|
-
mode_octal & 0004 != 0
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
def check_writable(file, by_whom)
|
86
|
-
mode = sprintf('%04s',run_command(commands.get_mode(file))[:stdout].strip)
|
87
|
-
mode = mode.split('')
|
88
|
-
mode_octal = mode[0].to_i * 512 + mode[1].to_i * 64 + mode[2].to_i * 8 + mode[3].to_i * 1
|
89
|
-
case by_whom
|
90
|
-
when nil
|
91
|
-
mode_octal & 0222 != 0
|
92
|
-
when 'owner'
|
93
|
-
mode_octal & 0200 != 0
|
94
|
-
when 'group'
|
95
|
-
mode_octal & 0020 != 0
|
96
|
-
when 'others'
|
97
|
-
mode_octal & 0002 != 0
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
def check_executable(file, by_whom)
|
102
|
-
mode = sprintf('%04s',run_command(commands.get_mode(file))[:stdout].strip)
|
103
|
-
mode = mode.split('')
|
104
|
-
mode_octal = mode[0].to_i * 512 + mode[1].to_i * 64 + mode[2].to_i * 8 + mode[3].to_i * 1
|
105
|
-
case by_whom
|
106
|
-
when nil
|
107
|
-
mode_octal & 0111 != 0
|
108
|
-
when 'owner'
|
109
|
-
mode_octal & 0100 != 0
|
110
|
-
when 'group'
|
111
|
-
mode_octal & 0010 != 0
|
112
|
-
when 'others'
|
113
|
-
mode_octal & 0001 != 0
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
def check_mounted(path, expected_attr, only_with)
|
118
|
-
ret = run_command(commands.check_mounted(path))
|
119
|
-
if expected_attr.nil? || ret[:exit_status] != 0
|
120
|
-
return ret[:exit_status] == 0
|
121
|
-
end
|
122
|
-
|
123
|
-
mount = ret[:stdout].scan(/\S+/)
|
124
|
-
actual_attr = { :device => mount[0], :type => mount[4] }
|
125
|
-
mount[5].gsub(/\(|\)/, '').split(',').each do |option|
|
126
|
-
name, val = option.split('=')
|
127
|
-
if val.nil?
|
128
|
-
actual_attr[name.to_sym] = true
|
129
|
-
else
|
130
|
-
val = val.to_i if val.match(/^\d+$/)
|
131
|
-
actual_attr[name.to_sym] = val
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
if ! expected_attr[:options].nil?
|
136
|
-
expected_attr.merge!(expected_attr[:options])
|
137
|
-
expected_attr.delete(:options)
|
138
|
-
end
|
139
|
-
|
140
|
-
if only_with
|
141
|
-
actual_attr == expected_attr
|
142
|
-
else
|
143
|
-
expected_attr.each do |key, val|
|
144
|
-
return false if actual_attr[key] != val
|
145
|
-
end
|
146
|
-
true
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
150
|
-
def check_routing_table(expected_attr)
|
151
|
-
return false if ! expected_attr[:destination]
|
152
|
-
ret = run_command(commands.check_routing_table(expected_attr[:destination]))
|
153
|
-
return false if ret[:exit_status] != 0
|
154
|
-
|
155
|
-
ret[:stdout] =~ /^(\S+)(?: via (\S+))? dev (\S+).+\r\n(?:default via (\S+))?/
|
156
|
-
actual_attr = {
|
157
|
-
:destination => $1,
|
158
|
-
:gateway => $2 ? $2 : $4,
|
159
|
-
:interface => expected_attr[:interface] ? $3 : nil
|
160
|
-
}
|
161
|
-
|
162
|
-
expected_attr.each do |key, val|
|
163
|
-
return false if actual_attr[key] != val
|
164
|
-
end
|
165
|
-
true
|
166
|
-
end
|
167
|
-
|
168
|
-
def check_os
|
169
|
-
return RSpec.configuration.os if RSpec.configuration.os
|
170
|
-
if run_command('ls /etc/redhat-release')[:exit_status] == 0
|
171
|
-
line = run_command('cat /etc/redhat-release')[:stdout]
|
172
|
-
if line =~ /release (\d[\d.]*)/
|
173
|
-
release = $1
|
174
|
-
end
|
175
|
-
{ :family => 'RedHat', :release => release }
|
176
|
-
elsif run_command('ls /etc/system-release')[:exit_status] == 0
|
177
|
-
{ :family => 'RedHat', :release => nil } # Amazon Linux
|
178
|
-
elsif run_command('ls /etc/debian_version')[:exit_status] == 0
|
179
|
-
{ :family => 'Debian', :release => nil }
|
180
|
-
elsif run_command('ls /etc/gentoo-release')[:exit_status] == 0
|
181
|
-
{ :family => 'Gentoo', :release => nil }
|
182
|
-
elsif run_command('ls /usr/lib/setup/Plamo-*')[:exit_status] == 0
|
183
|
-
{ :family => 'Plamo', :release => nil }
|
184
|
-
elsif run_command('uname -s')[:stdout] =~ /AIX/i
|
185
|
-
{ :family => 'AIX', :release => nil }
|
186
|
-
elsif (os = run_command('uname -sr')[:stdout]) && os =~ /SunOS/i
|
187
|
-
if os =~ /5.10/
|
188
|
-
{ :family => 'Solaris10', :release => nil }
|
189
|
-
elsif run_command('grep -q "Oracle Solaris 11" /etc/release')[:exit_status] == 0
|
190
|
-
{ :family => 'Solaris11', :release => nil }
|
191
|
-
elsif run_command('grep -q SmartOS /etc/release')[:exit_status] == 0
|
192
|
-
{ :family => 'SmartOS', :release => nil }
|
193
|
-
else
|
194
|
-
{ :family => 'Solaris', :release => nil }
|
195
|
-
end
|
196
|
-
elsif run_command('uname -s')[:stdout] =~ /Darwin/i
|
197
|
-
{ :family => 'Darwin', :release => nil }
|
198
|
-
elsif run_command('uname -s')[:stdout] =~ /FreeBSD/i
|
199
|
-
{ :family => 'FreeBSD', :release => nil }
|
200
|
-
else
|
201
|
-
{ :family => 'Base', :release => nil }
|
202
|
-
end
|
203
|
-
end
|
204
|
-
end
|
205
|
-
end
|
206
|
-
end
|
@@ -1,93 +0,0 @@
|
|
1
|
-
require 'configspec/backend/exec'
|
2
|
-
|
3
|
-
module Configspec
|
4
|
-
module Backend
|
5
|
-
class Ssh < Exec
|
6
|
-
def run_command(cmd, opt={})
|
7
|
-
cmd = build_command(cmd)
|
8
|
-
cmd = add_pre_command(cmd)
|
9
|
-
ret = ssh_exec!(cmd)
|
10
|
-
|
11
|
-
ret[:stdout].gsub!(/\r\n/, "\n")
|
12
|
-
|
13
|
-
if @example
|
14
|
-
@example.metadata[:command] = cmd
|
15
|
-
@example.metadata[:stdout] = ret[:stdout]
|
16
|
-
end
|
17
|
-
|
18
|
-
ret
|
19
|
-
end
|
20
|
-
|
21
|
-
def build_command(cmd)
|
22
|
-
cmd = super(cmd)
|
23
|
-
if RSpec.configuration.ssh.options[:user] != 'root'
|
24
|
-
cmd = "#{sudo} #{cmd}"
|
25
|
-
cmd.gsub!(/(\&\&\s*!?\(?\s*)/, "\\1#{sudo} ")
|
26
|
-
cmd.gsub!(/(\|\|\s*!?\(?\s*)/, "\\1#{sudo} ")
|
27
|
-
end
|
28
|
-
cmd
|
29
|
-
end
|
30
|
-
|
31
|
-
def add_pre_command(cmd)
|
32
|
-
cmd = super(cmd)
|
33
|
-
user = RSpec.configuration.ssh.options[:user]
|
34
|
-
pre_command = Configspec.configuration.pre_command
|
35
|
-
if pre_command && user != 'root'
|
36
|
-
cmd = "#{sudo} #{cmd}"
|
37
|
-
end
|
38
|
-
cmd
|
39
|
-
end
|
40
|
-
|
41
|
-
private
|
42
|
-
def ssh_exec!(command)
|
43
|
-
stdout_data = ''
|
44
|
-
stderr_data = ''
|
45
|
-
exit_status = nil
|
46
|
-
exit_signal = nil
|
47
|
-
pass_prompt = RSpec.configuration.pass_prompt || /^\[sudo\] password for/
|
48
|
-
|
49
|
-
ssh = RSpec.configuration.ssh
|
50
|
-
ssh.open_channel do |channel|
|
51
|
-
channel.request_pty do |ch, success|
|
52
|
-
abort "Could not obtain pty " if !success
|
53
|
-
end
|
54
|
-
channel.exec("#{command}") do |ch, success|
|
55
|
-
abort "FAILED: couldn't execute command (ssh.channel.exec)" if !success
|
56
|
-
channel.on_data do |ch, data|
|
57
|
-
if data.match pass_prompt
|
58
|
-
abort "Please set sudo password by using SUDO_PASSWORD or ASK_SUDO_PASSWORD environment variable" if RSpec.configuration.sudo_password.nil?
|
59
|
-
channel.send_data "#{RSpec.configuration.sudo_password}\n"
|
60
|
-
else
|
61
|
-
stdout_data += data
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
channel.on_extended_data do |ch, type, data|
|
66
|
-
stderr_data += data
|
67
|
-
end
|
68
|
-
|
69
|
-
channel.on_request("exit-status") do |ch, data|
|
70
|
-
exit_status = data.read_long
|
71
|
-
end
|
72
|
-
|
73
|
-
channel.on_request("exit-signal") do |ch, data|
|
74
|
-
exit_signal = data.read_long
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
78
|
-
ssh.loop
|
79
|
-
{ :stdout => stdout_data, :stderr => stderr_data, :exit_status => exit_status, :exit_signal => exit_signal }
|
80
|
-
end
|
81
|
-
|
82
|
-
def sudo
|
83
|
-
sudo_path = Configspec.configuration.sudo_path || RSpec.configuration.sudo_path
|
84
|
-
if sudo_path
|
85
|
-
"#{sudo_path}/sudo"
|
86
|
-
else
|
87
|
-
'sudo'
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
@@ -1,12 +0,0 @@
|
|
1
|
-
module Configspec
|
2
|
-
module Configuration
|
3
|
-
class << self
|
4
|
-
VALID_OPTIONS_KEYS = [:path, :pre_command, :stdout, :stderr, :sudo_path, :pass_prompt].freeze
|
5
|
-
attr_accessor(*VALID_OPTIONS_KEYS)
|
6
|
-
|
7
|
-
def defaults
|
8
|
-
VALID_OPTIONS_KEYS.inject({}) { |o, k| o.merge!(k => send(k)) }
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
module Configspec
|
2
|
-
module Helper
|
3
|
-
module Configuration
|
4
|
-
def subject
|
5
|
-
example.metadata[:subject] = described_class
|
6
|
-
build_configurations
|
7
|
-
super
|
8
|
-
end
|
9
|
-
|
10
|
-
# You can create a set of configurations provided to all specs in your spec_helper:
|
11
|
-
#
|
12
|
-
# RSpec.configure { |c| c.pre_command = "source ~/.zshrc" }
|
13
|
-
#
|
14
|
-
# Any configurations you provide with `let(:option_name)` in a spec will
|
15
|
-
# automatically be merged on top of the configurations.
|
16
|
-
#
|
17
|
-
# @example
|
18
|
-
#
|
19
|
-
# describe 'Gem' do
|
20
|
-
# let(:pre_command) { "source ~/.zshrc" }
|
21
|
-
#
|
22
|
-
# %w(pry awesome_print bundler).each do |p|
|
23
|
-
# describe package(p) do
|
24
|
-
# it { should be_installed.by('gem') }
|
25
|
-
# end
|
26
|
-
# end
|
27
|
-
# end
|
28
|
-
def build_configurations
|
29
|
-
Configspec::Configuration.defaults.keys.each do |c|
|
30
|
-
value = self.respond_to?(c.to_sym) ?
|
31
|
-
self.send(c) : RSpec.configuration.send(c)
|
32
|
-
Configspec::Configuration.send(:"#{c}=", value)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
module Configspec
|
2
|
-
module Helper
|
3
|
-
module DetectOS
|
4
|
-
def commands
|
5
|
-
property[:os_by_host] = {} if ! property[:os_by_host]
|
6
|
-
host = RSpec.configuration.ssh ? RSpec.configuration.ssh.host : 'localhost'
|
7
|
-
|
8
|
-
if property[:os_by_host][host]
|
9
|
-
os = property[:os_by_host][host]
|
10
|
-
else
|
11
|
-
os = backend(Configspec::Commands::Base).check_os
|
12
|
-
property[:os_by_host][host] = os
|
13
|
-
end
|
14
|
-
|
15
|
-
self.class.const_get('Configspec').const_get('Commands').const_get(os[:family]).new
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
@@ -1,14 +0,0 @@
|
|
1
|
-
module Configspec
|
2
|
-
module Helper
|
3
|
-
module Exec
|
4
|
-
def backend(commands_object=nil)
|
5
|
-
if commands_object.nil? && ! respond_to?(:commands)
|
6
|
-
commands_object = Configspec::Commands::Base.new
|
7
|
-
end
|
8
|
-
instance = Configspec::Backend::Exec.instance
|
9
|
-
instance.set_commands(commands_object || commands)
|
10
|
-
instance
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
@@ -1,14 +0,0 @@
|
|
1
|
-
require 'configspec/properties'
|
2
|
-
|
3
|
-
module Configspec
|
4
|
-
module Helper
|
5
|
-
module Properties
|
6
|
-
def property
|
7
|
-
Configspec::Properties.instance.properties
|
8
|
-
end
|
9
|
-
def set_property(prop)
|
10
|
-
Configspec::Properties.instance.properties(prop)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
module Configspec
|
2
|
-
module Helper
|
3
|
-
module Ssh
|
4
|
-
def backend(commands_object=nil)
|
5
|
-
if ! respond_to?(:commands)
|
6
|
-
commands_object = Configspec::Commands::Base.new
|
7
|
-
end
|
8
|
-
instance = Configspec::Backend::Ssh.instance
|
9
|
-
instance.set_commands(commands_object || commands)
|
10
|
-
instance
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|