serverspec_launcher 0.2.4 → 0.3.0
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 +4 -4
- data/.gitignore +0 -0
- data/.rspec +0 -0
- data/.rubocop.yml +0 -0
- data/.travis.yml +0 -0
- data/CODE_OF_CONDUCT.md +0 -0
- data/Gemfile +0 -0
- data/LICENSE.txt +0 -0
- data/README.md +117 -10
- data/Rakefile +7 -0
- data/lib/serverspec_launcher/generators/properties_generator.rb +0 -0
- data/lib/serverspec_launcher/generators/rakefile_generator.rb +0 -0
- data/lib/serverspec_launcher/generators/role_spec_generator.rb +0 -0
- data/lib/serverspec_launcher/generators/spec_helper_generator.rb +0 -0
- data/lib/serverspec_launcher/helpers/example_helper.rb +0 -0
- data/lib/serverspec_launcher/helpers/symbolize_helper.rb +0 -0
- data/lib/serverspec_launcher/rake_tasks.rb +190 -16
- data/lib/serverspec_launcher/spec_helper.rb +1 -0
- data/lib/serverspec_launcher/version.rb +1 -1
- data/lib/serverspec_launcher.rb +0 -0
- data/serverspec_launcher.gemspec +1 -1
- data/templates/Rakefile.erb +0 -0
- data/templates/properties.yaml.erb +0 -0
- data/templates/role_spec.rb.erb +0 -0
- data/templates/spec_helper.rb.erb +0 -0
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 51df9c0b623e4364ddc2e8230a7c320bbfaf84b8a57a407516a76165ca21ef21
|
4
|
+
data.tar.gz: cac54f2f95343d60e248834d2706cfd3ec0741d24a4d6aad6221c4c70c1d8f3e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe95afd76a4b1004eb62ac4434326e5d3c49b0d8500dbc21ebc08c0714e23d60cd99719615979e87ae1c264beeb90d19d10f973c203121c393c92d277b30bc1d
|
7
|
+
data.tar.gz: e5e50f3cd690942325ce579a0953104289522ab488315c8de61a4dc4402750cb2f0fda9d6c00f0824e9d6fb95d61c7aef4cd00f87f8142a8674dda7ac7184321
|
data/.gitignore
CHANGED
File without changes
|
data/.rspec
CHANGED
File without changes
|
data/.rubocop.yml
CHANGED
File without changes
|
data/.travis.yml
CHANGED
File without changes
|
data/CODE_OF_CONDUCT.md
CHANGED
File without changes
|
data/Gemfile
CHANGED
File without changes
|
data/LICENSE.txt
CHANGED
File without changes
|
data/README.md
CHANGED
@@ -5,6 +5,20 @@ and containers using a YAML based configuration files.
|
|
5
5
|
|
6
6
|
It allows for spec files (or lists of shared behaviours) to be ran across groups of servers.
|
7
7
|
|
8
|
+
It will generate rake tasks
|
9
|
+
|
10
|
+
Serverspec launcher also has limited support for running inspec based checks, running via the [chef/inspec](https://hub.docker.com/r/chef/inspec/) docker image
|
11
|
+
|
12
|
+
## Requirements
|
13
|
+
|
14
|
+
* Ruby >= 2.3
|
15
|
+
* bundler
|
16
|
+
* docker (if using inspec tests)
|
17
|
+
|
18
|
+
|
19
|
+
Currently this only runs on linux, possibly OS X (I don't have a mac to test it with)
|
20
|
+
|
21
|
+
|
8
22
|
## Installation
|
9
23
|
|
10
24
|
Add this line to your application's Gemfile:
|
@@ -46,8 +60,10 @@ the target section, and a task for any hosts specifed within the targets config.
|
|
46
60
|
|
47
61
|
Each target consists of:
|
48
62
|
|
63
|
+
###### Serverspec
|
64
|
+
|
49
65
|
targetname (hash key): (required) The name of the target
|
50
|
-
* backend: (optional) Which backend to run against, supported backend are ssh, exec, docker, and
|
66
|
+
* backend: (optional) Which backend to run against, supported backend are ssh, exec, docker, vagrant, and inspec. Windows based backend will be availible in future versions
|
51
67
|
* user: (optional) what user to run the tests as, Defaults to current user
|
52
68
|
* hosts: (optional) list or single value specifying the hostname(s) to run this against. defaults to target name
|
53
69
|
* spec_type: (optional) which spec file from the spec directory to execute against the target (do not include the _spec.rb). Defaults to role
|
@@ -56,6 +72,7 @@ targetname (hash key): (required) The name of the target
|
|
56
72
|
Any values specified here will overwrite environment level(not to be confused with environment variables), and global level variables
|
57
73
|
* environment: (optional) hash of environment variables which will be set on the target, environment level(not to be confused with environment variables), and global level environment varaibles(see below) that have previously defined values will be overwritten
|
58
74
|
* fail_on_err: Stop running the tests if the target fails its checks
|
75
|
+
* formatters: Use specific formatters for this target ([see formatters in option section](#options))
|
59
76
|
|
60
77
|
Example :
|
61
78
|
|
@@ -71,6 +88,69 @@ targets:
|
|
71
88
|
spec_type: webserver
|
72
89
|
```
|
73
90
|
|
91
|
+
###### Inspec
|
92
|
+
|
93
|
+
targetname (hash key): (required) The name of the target
|
94
|
+
* backend: (optional) Which backend to run against, this should be set to inspec
|
95
|
+
* user: (optional) what user to run the tests as, Defaults to current user
|
96
|
+
* hosts: (optional) list or single value specifying the hostname(s) to run this against. defaults to target name
|
97
|
+
* spec_type: (optional) which spec file from the spec directory to execute against the target (do not include the _spec.rb). If control or profile are specifed this field is ignored
|
98
|
+
* control: (optional) path to inspec control to execute against the target (specify full path to spec file from project root). If profile is specified this field is ignored
|
99
|
+
* profile: (optional) path to inspec profile to execute against the target (specify full path to profile from project root, github or chef supermarket). If profile is specified this field is ignored
|
100
|
+
* fail_on_err: (optional) Stop running the tests if the target fails its checks
|
101
|
+
* auth_method: How to authenticate the target user
|
102
|
+
* ssh-key: use an ssh key for authentication
|
103
|
+
* agent: (recommened) Use an existing ssh agent as the authentication method
|
104
|
+
* keyfile: Space separated list ssh-key(s) to use for authentication if using ssh-key as the authentication method
|
105
|
+
* bastion_host: (optional) Specify a bastion host
|
106
|
+
* bastion_user: (optional) Specify the user for a bastion host
|
107
|
+
* bastion_port: (optional) Specify the port for a bastion host
|
108
|
+
* formatters: Use specific reporters for this target ([see formatters in option section](#options))
|
109
|
+
|
110
|
+
|
111
|
+
Examples :
|
112
|
+
|
113
|
+
This would create the rake tasks serverspec:security, serverspec:security:webserver1 and serverspec:security:webserver2
|
114
|
+
which run test contained in spec/webserver_spec.rb
|
115
|
+
```yaml
|
116
|
+
targets:
|
117
|
+
security: # the name of the target
|
118
|
+
backend: inspec # use the 'inspec' backend.
|
119
|
+
user: ec2-user
|
120
|
+
auth_method: agent
|
121
|
+
hosts:
|
122
|
+
- webserver1
|
123
|
+
- webserver2
|
124
|
+
spec_type: webserver
|
125
|
+
```
|
126
|
+
This would create the rake tasks serverspec:security, serverspec:security:env01 and serverspec:security:env02
|
127
|
+
which run tests contained in spec/ssh_access.rb
|
128
|
+
```yaml
|
129
|
+
targets:
|
130
|
+
security: # the name of the target
|
131
|
+
backend: inspec # use the 'inspec' backend.
|
132
|
+
user: ec2-user
|
133
|
+
auth_method: agent
|
134
|
+
hosts:
|
135
|
+
- env01
|
136
|
+
- env02
|
137
|
+
control: spec/ssh_access.rb
|
138
|
+
```
|
139
|
+
This would create the rake tasks serverspec:security, serverspec:security:env01 and serverspec:security:env02
|
140
|
+
which run the CIS Distribution Independent Linux Benchmark profile against the target
|
141
|
+
```yaml
|
142
|
+
targets:
|
143
|
+
security: # the name of the target
|
144
|
+
backend: inspec # use the 'inspec' backend.
|
145
|
+
user: ec2-user
|
146
|
+
auth_method: agent
|
147
|
+
hosts:
|
148
|
+
- env01
|
149
|
+
- env02
|
150
|
+
control: https://github.com/dev-sec/cis-dil-benchmark
|
151
|
+
```
|
152
|
+
|
153
|
+
|
74
154
|
##### environments
|
75
155
|
serverspec_launcher supports the concept of environments. Environments are groups of targets organised as a named entity, i.e. test or qa.
|
76
156
|
|
@@ -108,6 +188,7 @@ environments:
|
|
108
188
|
- web3.perf.domain
|
109
189
|
- web4.perf.domain
|
110
190
|
```
|
191
|
+
<a name="options"></a>
|
111
192
|
##### options
|
112
193
|
A hash of options to pass to serverspec_launcher
|
113
194
|
|
@@ -115,14 +196,15 @@ A hash of options to pass to serverspec_launcher
|
|
115
196
|
* color: (optional) colorize the output, defaults to true
|
116
197
|
* formatters: (optional) list of RSpec formatter to process the results with. Supported formatters are:
|
117
198
|
|
118
|
-
- docs RSpec Documentation Formatter writing to file reports
|
119
|
-
|
120
|
-
-
|
121
|
-
-
|
122
|
-
-
|
123
|
-
-
|
124
|
-
-
|
125
|
-
-
|
199
|
+
- docs RSpec Documentation Formatter writing to file reports/[\<environment\>]/\<target>/\<host>.docs.
|
200
|
+
If using inspec checks this will use the 'Documentation' reporter.
|
201
|
+
- docs_screen - RSpec Documentation Formatter writing to screen. If using inspec checks this will use the 'Documentation' reporter.
|
202
|
+
- tick - Tick/Cross output to screen. If using inspec checks this will use the 'cli' reporter.
|
203
|
+
- tick_file - Tick/Cross output to reports/[\<environment\>]/\<target>/\<host>.tick. If using inspec checks this will use the 'cli' reporter.
|
204
|
+
- html - HTML Reports to reports/[\<environment\>]/\<target>/\<host>.html. If using inspec checks this will use the 'html' reporter.
|
205
|
+
- junit - Unit Reports (useful for jenkins jobs) to
|
206
|
+
- html_pretty - Pretty HTML Reports to reports/[\<environment\>]/\<target>/\<host>.html. If using inspec checks this will use the 'html' reporter.
|
207
|
+
- json - JSON Output to reports/[\<environment\>]/\<target>/\<host>.html. If using inspec checks this will use the 'html' reporter.
|
126
208
|
- progress - RSpec .F* progress output
|
127
209
|
|
128
210
|
Example:
|
@@ -136,7 +218,7 @@ options:
|
|
136
218
|
|
137
219
|
```
|
138
220
|
##### variables
|
139
|
-
A hash containing key value pairs. Each entry will be available as property[:variables][
|
221
|
+
A hash containing key value pairs. Each entry will be available as property[:variables][:\<key>]
|
140
222
|
|
141
223
|
Example:
|
142
224
|
```yaml
|
@@ -278,6 +360,31 @@ targets:
|
|
278
360
|
# Override a globally set environment var
|
279
361
|
SOMEVAR: some other value
|
280
362
|
|
363
|
+
inspec-profile-example: # the name of the target
|
364
|
+
backend: inspec
|
365
|
+
user: ec2-user
|
366
|
+
auth_method: agent
|
367
|
+
hosts:
|
368
|
+
- env01
|
369
|
+
- env02
|
370
|
+
profile: https://github.com/dev-sec/cis-dil-benchmark
|
371
|
+
|
372
|
+
inspec-control-example: # the name of the target
|
373
|
+
backend: inspec
|
374
|
+
user: ec2-user
|
375
|
+
auth_method: agent
|
376
|
+
hosts:
|
377
|
+
- env01
|
378
|
+
- env02
|
379
|
+
control: spec/ssh_access.rb
|
380
|
+
|
381
|
+
inspec-spec-example: # the name of the target
|
382
|
+
backend: inspec
|
383
|
+
user: ec2-user
|
384
|
+
auth_method: agent
|
385
|
+
hosts: env01
|
386
|
+
spec_type: webserver
|
387
|
+
|
281
388
|
environments:
|
282
389
|
qa:
|
283
390
|
variables:
|
data/Rakefile
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
require 'bundler/gem_tasks'
|
3
3
|
require 'rspec/core/rake_task'
|
4
4
|
require 'conventional_changelog'
|
5
|
+
require 'docker-api'
|
5
6
|
|
6
7
|
RSpec::Core::RakeTask.new(:spec)
|
7
8
|
|
@@ -10,4 +11,10 @@ task default: :spec
|
|
10
11
|
|
11
12
|
task :changelog do
|
12
13
|
ConventionalChangelog::Generator.new.generate!
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
task :docker do
|
18
|
+
|
19
|
+
|
13
20
|
end
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -1,8 +1,11 @@
|
|
1
|
-
# frozen_string_literal:
|
1
|
+
# frozen_string_literal: false
|
2
2
|
require 'bundler/gem_tasks'
|
3
|
+
require 'bundler'
|
3
4
|
require 'rake'
|
4
5
|
require 'rspec/core/rake_task'
|
5
6
|
require 'yaml'
|
7
|
+
require 'docker-api'
|
8
|
+
require 'etc'
|
6
9
|
|
7
10
|
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
8
11
|
require 'serverspec_launcher/helpers/example_helper'
|
@@ -18,7 +21,7 @@ class ServerspecLauncherRakeTasks
|
|
18
21
|
@properties = properties ? properties.deep_symbolize_keys : YAML.load_file('properties.yml').deep_symbolize_keys
|
19
22
|
options = @properties[:options] || {}
|
20
23
|
@fail_on_err = options[:fail_on_err]
|
21
|
-
@formatters = options[:formatters] || ['
|
24
|
+
@formatters = options[:formatters] || ['tick']
|
22
25
|
@colorize = options[:color].nil? ? true : options[:color]
|
23
26
|
end
|
24
27
|
|
@@ -48,8 +51,8 @@ class ServerspecLauncherRakeTasks
|
|
48
51
|
end
|
49
52
|
end
|
50
53
|
|
51
|
-
def
|
52
|
-
env = options[:source] == 'environment' ? ":"+ options[:environment]: ""
|
54
|
+
def serverspec_task_array(key, spec_type, target, options)
|
55
|
+
env = options[:source] == 'environment' ? ":"+ options[:environment] : ""
|
53
56
|
desc "Run serverspec to #{key}"
|
54
57
|
task key.to_sym => "serverspec#{env}:#{key}:all"
|
55
58
|
namespace key.to_sym do
|
@@ -61,12 +64,12 @@ class ServerspecLauncherRakeTasks
|
|
61
64
|
end
|
62
65
|
target[:hosts].each do |host|
|
63
66
|
task_name = "#{host || target[:name]}"
|
64
|
-
|
67
|
+
serverspec_rake_task(host, key, task_name, spec_type, options)
|
65
68
|
end
|
66
69
|
end
|
67
70
|
end
|
68
71
|
|
69
|
-
def
|
72
|
+
def serverspec_rake_task(host, key, task_name, spec_type, options = {}, target = {})
|
70
73
|
desc "Run serverspec to #{key}"
|
71
74
|
RSpec::Core::RakeTask.new(task_name.to_s.to_sym) do |t|
|
72
75
|
ENV['TARGET_HOST'] = host.to_s
|
@@ -76,17 +79,18 @@ class ServerspecLauncherRakeTasks
|
|
76
79
|
ENV['TASK_ENV'] = options[:environment]
|
77
80
|
t.pattern = "spec/#{spec_type}_spec.rb"
|
78
81
|
t.fail_on_error = options[:fail_on_err]
|
79
|
-
|
82
|
+
report_name = options[:environment] ? "reports/#{options[:environment]}/#{key.to_s}/#{host.to_s}" : "reports/#{key.to_s}/#{host.to_s}"
|
83
|
+
set_formatters(report_name, options, t)
|
80
84
|
end
|
81
85
|
end
|
82
86
|
|
83
|
-
def set_formatters(
|
87
|
+
def set_formatters(report_path, options, t)
|
84
88
|
opts = t.rspec_opts
|
85
89
|
if options[:formatters].include?('junit') || options[:formatters].include?('xml')
|
86
|
-
opts = "#{opts} --format RspecJunitFormatter --out
|
90
|
+
opts = "#{opts} --format RspecJunitFormatter --out #{report_path}.xml"
|
87
91
|
end
|
88
92
|
if options[:formatters].include?('docs') || options[:formatters].include?('documentation') || options[:formatters].include?('docs_file')
|
89
|
-
opts = "#{opts} --format documentation --out
|
93
|
+
opts = "#{opts} --format documentation --out #{report_path}.docs"
|
90
94
|
end
|
91
95
|
if options[:formatters].include?('docs_screen')
|
92
96
|
opts = "#{opts} --format documentation"
|
@@ -95,19 +99,19 @@ class ServerspecLauncherRakeTasks
|
|
95
99
|
opts = "#{opts} --format RspecTickFormatter"
|
96
100
|
end
|
97
101
|
if options[:formatters].include?('tick_file')
|
98
|
-
opts = "#{opts} --format RspecTickFormatter --out
|
102
|
+
opts = "#{opts} --format RspecTickFormatter --out #{report_path}.tick"
|
99
103
|
end
|
100
104
|
if options[:formatters].include?('progress')
|
101
105
|
opts = "#{opts} --format progress"
|
102
106
|
end
|
103
107
|
if options[:formatters].include?('html')
|
104
|
-
opts = "#{opts} --format html --out
|
108
|
+
opts = "#{opts} --format html --out #{report_path}.html"
|
105
109
|
end
|
106
110
|
if options[:formatters].include?('html_report') || options[:formatters].include?('html_pretty')
|
107
111
|
opts = "#{opts} --format RspecHtmlReporter"
|
108
112
|
end
|
109
113
|
if options[:formatters].include?('json')
|
110
|
-
opts = "#{opts} --format j --out
|
114
|
+
opts = "#{opts} --format j --out #{report_path}.json"
|
111
115
|
end
|
112
116
|
unless options[:color]
|
113
117
|
opts = "#{opts} --no-color"
|
@@ -146,16 +150,186 @@ class ServerspecLauncherRakeTasks
|
|
146
150
|
source: task_source,
|
147
151
|
environment: environment
|
148
152
|
}
|
153
|
+
if target[:backend] == 'inspec'
|
154
|
+
inspec_target(key, options, target)
|
155
|
+
else
|
156
|
+
serverspec_target(key, options, target)
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
def inspec_target(key, options, target)
|
161
|
+
spec_type, options = get_inspec_type(target, options)
|
162
|
+
if target[:hosts].is_a?(Array)
|
163
|
+
inspec_task_array(key, spec_type, target, options)
|
164
|
+
elsif target[:hosts]
|
165
|
+
host = target[:hosts]
|
166
|
+
task_name = (key || target[:name]).to_s
|
167
|
+
inspec_task(host, "#{key}/#{host}", task_name, spec_type, options, target)
|
168
|
+
else
|
169
|
+
host = (target[:hosts] || 'local' )
|
170
|
+
task_name = (key || target[:name]).to_s
|
171
|
+
inspec_task(host, "#{key}/#{host}", task_name, spec_type, options, target)
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
def inspec_task_array(key, spec_type, target, options)
|
176
|
+
env = options[:source] == 'environment' ? ":"+ options[:environment] : ""
|
177
|
+
desc "Run serverspec to #{key}"
|
178
|
+
task key.to_sym => "serverspec#{env}:#{key}:all"
|
179
|
+
namespace key.to_sym do
|
180
|
+
desc "Run #{key} against all hosts"
|
181
|
+
task :all do
|
182
|
+
target[:hosts].each do |host|
|
183
|
+
Rake::Task["serverspec#{env}:#{key}:#{host.split(':')[0].to_sym}"].execute
|
184
|
+
end
|
185
|
+
end
|
186
|
+
target[:hosts].each do |host|
|
187
|
+
task_name = "#{host || target[:name]}"
|
188
|
+
inspec_task(host, "#{key}/#{host}", task_name, spec_type, options, target)
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
def get_inspec_type(target, options)
|
194
|
+
spec_type = 'role'
|
195
|
+
if (target[:control] && target[:profile]) || (target[:control] && target[:spec_type]) || (target[:spec_type] && target[:profile])
|
196
|
+
puts 'WARNING: Multiple options specified: they will be evalated in the follow precidence profile > control > spec_type'
|
197
|
+
end
|
198
|
+
if target[:spec_type]
|
199
|
+
spec_type = target[:spec_type]
|
200
|
+
options[:spec_type] = 'spec'
|
201
|
+
end
|
202
|
+
if target[:control]
|
203
|
+
spec_type = target[:control]
|
204
|
+
options[:spec_type] = 'control'
|
205
|
+
end
|
206
|
+
if target[:profile]
|
207
|
+
spec_type = target[:profile]
|
208
|
+
options[:spec_type] = 'control'
|
209
|
+
end
|
210
|
+
return spec_type, options
|
211
|
+
end
|
212
|
+
|
213
|
+
def inspec_task(host, key, task_name, spec_type, options = {}, target = {})
|
214
|
+
protocol = host == 'local' ? 'local' : 'ssh'
|
215
|
+
command = inspec_commandline(target,key,host, spec_type, protocol, options)
|
216
|
+
Rake::Task.define_task(task_name.to_s.to_sym) do
|
217
|
+
|
218
|
+
unless Docker::Image.exist? 'chef/inspec:latest'
|
219
|
+
Docker::Image.create('fromImage' => 'chef/inspec:latest')
|
220
|
+
end
|
221
|
+
|
222
|
+
container = Docker::Container.create(
|
223
|
+
'Image' => 'chef/inspec:latest',
|
224
|
+
'Mounts' => inspec_mounts(target, protocol),
|
225
|
+
'Cmd' => command,
|
226
|
+
'Tty' => STDIN.tty?,
|
227
|
+
'Env' => inspec_environment(target, protocol)
|
228
|
+
)
|
229
|
+
begin
|
230
|
+
puts "inspec #{command.join(' ')}"
|
231
|
+
container.start
|
232
|
+
container.wait
|
233
|
+
rescue Docker::Error::TimeoutError => ex
|
234
|
+
container.stop
|
235
|
+
puts "A Docker::Error::TimeoutError occurred, most likey because you are using password protected ssh key, whihc is not supported"
|
236
|
+
puts "Either agent your ssh key and use 'agent' as the auth_method in your target settings or use an unprotected key (not recommended)"
|
237
|
+
puts 'Container Logs: '
|
238
|
+
puts container.logs(stderr: true)
|
239
|
+
ensure
|
240
|
+
puts container.logs(stdout: true)
|
241
|
+
container.delete
|
242
|
+
#Report have wrong permission hookie fix
|
243
|
+
chown_files(protocol, target)
|
244
|
+
end
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
def chown_files(protocol, target)
|
249
|
+
unless Docker::Image.exist? 'alpine:latest'
|
250
|
+
Docker::Image.create('fromImage' => 'alpine:latest')
|
251
|
+
end
|
252
|
+
command = %W[chown -R #{Etc.getpwnam(ENV['USER']).uid}:#{Etc.getpwnam(ENV['USER']).gid} /share/reports/]
|
253
|
+
container = Docker::Container.create(
|
254
|
+
'Image' => 'alpine:latest',
|
255
|
+
'Mounts' => inspec_mounts(target, protocol),
|
256
|
+
'Cmd' => command
|
257
|
+
)
|
258
|
+
container.start
|
259
|
+
container.wait
|
260
|
+
container.delete
|
261
|
+
end
|
262
|
+
|
263
|
+
def inspec_environment(target_info, protocol)
|
264
|
+
environment = []
|
265
|
+
environment << "SSH_AUTH_SOCK=#{ENV['SSH_AUTH_SOCK']}" if target_info[:auth_method] == 'agent'
|
266
|
+
environment
|
267
|
+
end
|
268
|
+
|
269
|
+
def inspec_mounts(target_info, protocol)
|
270
|
+
mounts = [{
|
271
|
+
'Type' => 'bind',
|
272
|
+
'Source' => "#{Dir.pwd}",
|
273
|
+
'Target' => "/share"
|
274
|
+
}, {
|
275
|
+
'Type' => 'bind',
|
276
|
+
'Source' => "/etc/hosts",
|
277
|
+
'Target' => "/etc/hosts"
|
278
|
+
}]
|
279
|
+
mount_ssh = target_info[:mount_ssh_dir] ? target_info[:mount_ssh_dir] : true
|
280
|
+
mounts << { 'Type' => 'bind', 'Source' => "#{File.expand_path('~')}/.ssh", 'Target' => "#{File.expand_path('~')}/.ssh" } if mount_ssh && protocol == 'ssh'
|
281
|
+
mounts << { 'Type' => 'bind', 'Source' => "#{ENV['SSH_AUTH_SOCK']}", 'Target' => "#{ENV['SSH_AUTH_SOCK']}" } if target_info[:auth_method] == 'agent'
|
282
|
+
mounts
|
283
|
+
end
|
284
|
+
|
285
|
+
def inspec_commandline(target_info, key, host, spec_type, protocol, options = {})
|
286
|
+
spec = if options[:spec_type] == 'spec'
|
287
|
+
"spec/#{spec_type}_spec.rb"
|
288
|
+
else
|
289
|
+
spec_type
|
290
|
+
end
|
291
|
+
target = "#{protocol}://#{protocol == 'local' ? '' : host}"
|
292
|
+
command = %W[exec #{spec} -t #{target}]
|
293
|
+
authmethod = target_info[:auth_method] ? target_info[:auth_method] : 'ssh-keys'
|
294
|
+
keyfile = target_info[:keyfile] ? target_info[:keyfile] : "#{File.expand_path('~')}/.ssh/id_rsa"
|
295
|
+
if protocol == 'ssh' && authmethod == 'ssh-keys'
|
296
|
+
command << '-i'
|
297
|
+
command << keyfile
|
298
|
+
end
|
299
|
+
command << "--user=#{target_info[:user]}" if target_info[:user]
|
300
|
+
command << "--bastion-host=#{target_info[:bastion_host]}" if target_info[:bastion_host]
|
301
|
+
command << "--bastion-port=#{target_info[:bastion_port]}" if target_info[:bastion_port]
|
302
|
+
command << "--bastion-user=#{target_info[:bastion_user]}" if target_info[:bastion_user]
|
303
|
+
command << set_inspec_reporters(key, host, options)
|
304
|
+
command
|
305
|
+
end
|
306
|
+
|
307
|
+
def set_inspec_reporters(key, host, options)
|
308
|
+
reporters = []
|
309
|
+
report_path = options[:environment] ? "/share/reports/#{options[:environment]}/#{key}" : "/share/#{report_path}"
|
310
|
+
reporters << "junit:#{report_path}.xml" if options[:formatters].include?('junit') || options[:formatters].include?('xml')
|
311
|
+
reporters << "documentation:#{report_path}.docs" if options[:formatters].include?('docs') || options[:formatters].include?('documentation') || options[:formatters].include?('docs_file')
|
312
|
+
reporters << 'documentation' if options[:formatters].include?('docs_screen')
|
313
|
+
reporters << 'cli' if options[:formatters].include?('tick')
|
314
|
+
reporters << "cli:#{report_path}.tick" if options[:formatters].include?('tick_file')
|
315
|
+
reporters << 'progress' if options[:formatters].include?('progress')
|
316
|
+
reporters << "html:#{report_path}.html" if options[:formatters].include?('html')
|
317
|
+
reporters << "html#{report_path}.html" if (options[:formatters].include?('html_report') || options[:formatters].include?('html_pretty')) && !options[:formatters].include?('html')
|
318
|
+
reporters << "json-rspec:#{report_path}.json" if options[:formatters].include?('json')
|
319
|
+
"--reporter=#{reporters.join(' ')}"
|
320
|
+
end
|
321
|
+
|
322
|
+
def serverspec_target(key, options, target)
|
149
323
|
spec_type = target[:spec_type] || 'role'
|
150
324
|
if target[:hosts].is_a?(Array)
|
151
|
-
|
325
|
+
serverspec_task_array(key, spec_type, target, options)
|
152
326
|
elsif target[:hosts]
|
153
327
|
host = target[:hosts]
|
154
328
|
task_name = (key || target[:name]).to_s
|
155
|
-
|
329
|
+
serverspec_rake_task(host, key, task_name, spec_type, options)
|
156
330
|
else
|
157
331
|
task_name = (key || target[:name]).to_s
|
158
|
-
|
332
|
+
serverspec_rake_task(key, key, task_name, spec_type, options)
|
159
333
|
end
|
160
334
|
end
|
161
335
|
end
|
data/lib/serverspec_launcher.rb
CHANGED
File without changes
|
data/serverspec_launcher.gemspec
CHANGED
@@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
|
|
23
23
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
24
|
spec.require_paths = ['lib']
|
25
25
|
|
26
|
-
spec.add_development_dependency 'bundler', '~>
|
26
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
27
27
|
spec.add_development_dependency 'rake', '~> 12.0'
|
28
28
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
29
29
|
spec.add_development_dependency 'rubocop'
|
data/templates/Rakefile.erb
CHANGED
File without changes
|
File without changes
|
data/templates/role_spec.rb.erb
CHANGED
File without changes
|
File without changes
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: serverspec_launcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Wardrobe
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-01-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '2.0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '2.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|