sfn-serverspec 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3b8afe3d9e73b49ff634ee667f86b20e64ee3d7b
4
- data.tar.gz: d99d8bc34c182d9e732b62eb190035ca472195e7
3
+ metadata.gz: bf48b1b61f92b092cb89a41cb6b0caa476957476
4
+ data.tar.gz: 81ea86881c7572971cc7e5682afb3f8e3b030560
5
5
  SHA512:
6
- metadata.gz: 9beffad9fb727f1fd25a4429d52ffe04b9859f4cfbd5c0c0c68d2da706f35fe7c7b03873f1642fa8d742670538e792c19b5caeb2e4f391db26899809ed8e48a4
7
- data.tar.gz: 2271aac2052a33acd71c7cf9539fec2d26f9449c25af33636a4efb69d357f575c5242a22ff39096a7453f1b5103e3ae830e11123d1e34d99174855cd2c3a8b0c
6
+ metadata.gz: 263d927fff74bf6d55987810f67fef88676c59d3e4c51ab5f1559499681261aae1528d98720cd4434753ccd7c13fac884323e3f4d01c83ec8364e4c417bb2220
7
+ data.tar.gz: 162047b925f2745185f93a865a9592ced40b6ff33b8569f4af1bc7891b00751d140eb7074fc7e5ff76848e7d916b73862073fb76ae6d9f6e33af6a16718df355
data/CHANGELOG.md CHANGED
@@ -1,2 +1,7 @@
1
+ # v0.1.2
2
+ * Use inherited bogo-ui instance for log messages
3
+ * Fix incorrect scoping of methods on Sfn::Callback instead of Sfn::Callback::ServerspecValidator
4
+ * Add sfn command for running on-demand validation of existing stack
5
+
1
6
  # v0.1.0
2
7
  * Initial release
data/README.md CHANGED
@@ -64,6 +64,51 @@ The following configuration options specified at the resource level will overrid
64
64
  * `ssh_key_paths`
65
65
  * `ssh_key_passphrase`
66
66
 
67
+ ### On-demand validation
68
+
69
+ Provided that you are using Bundler, this callback also adds a `serverspec` command to sfn, enabling on-demand validation of a running stack. You'll want to configure sfn-serverspec thusly:
70
+
71
+ ```ruby
72
+ gem 'sfn-serverspec', :require => 'sfn-serverspec'
73
+ ```
74
+
75
+ The `serverspec` command requires both a stack name and a template to use as the source of Serverspec configuration. The template may be provided via command line flag or interactive file path prompt:
76
+
77
+ ```
78
+ $ bundle exec sfn serverspec example-stack -f sparkleformation/example.rb
79
+ [Sfn]: Callback template serverspec_validator: starting
80
+ [Sfn]: Callback template serverspec_validator: complete
81
+ [Sfn]: Serverspec validating stack example-stack with template sparkleformation/example.rb:
82
+ [Sfn]: Callback after_serverspec serverspec_validator: starting
83
+ [Sfn]: Serverspec validating i-55836892 (10.101.100.15)
84
+
85
+ base
86
+ Port "22"
87
+ should be listening
88
+
89
+ hello world
90
+ displays a custom message on the index page
91
+ Port "80"
92
+ should be listening
93
+
94
+ Finished in 2.92 seconds (files took 10.49 seconds to load)
95
+ 3 examples, 0 failures
96
+
97
+ [Sfn]: Serverspec validating i-52836895 (10.101.100.19)
98
+
99
+ Port "22"
100
+ should be listening
101
+
102
+ hello world
103
+ displays a custom message on the index page
104
+ Port "80"
105
+ should be listening
106
+
107
+ Finished in 0.35415 seconds (files took 13.41 seconds to load)
108
+ 3 examples, 0 failures
109
+
110
+ [Sfn]: Callback after_serverspec serverspec_validator: complete
111
+ ```
67
112
 
68
113
  ## Caveats
69
114
 
@@ -71,7 +116,7 @@ Providing `serverspec` configuration on compute resources works in a manner simi
71
116
 
72
117
  As of this writing, the callback only processes Serverspec configuration on `AWS::AutoScaling::AutoScalingGroup` and `AWS::EC2::Instance` resources.
73
118
 
74
- Non-compute resources with `serverspec` configuration will not be processed by this callback, and will probably cause a validation error when trying to validate or create a stack from the template.
119
+ Non-compute resources with `serverspec` configuration will not be processed by this callback, and will probably yield a validation error when trying to validate or create a stack from the template.
75
120
 
76
121
  ## Info
77
122
 
@@ -1,2 +1,4 @@
1
1
  require 'sfn-serverspec/version'
2
2
  require 'sfn-serverspec/validator'
3
+ require 'sfn-serverspec/command/serverspec'
4
+ require 'sfn-serverspec/config/serverspec'
@@ -0,0 +1,28 @@
1
+ require 'sparkle_formation'
2
+ require 'sfn'
3
+ require 'sfn-serverspec/validator'
4
+
5
+ module Sfn
6
+ class Command
7
+ # Validate command
8
+ class Serverspec < Command
9
+ include Sfn::CommandModule::Base
10
+ include Sfn::CommandModule::Template
11
+ include Sfn::CommandModule::Stack
12
+
13
+ def execute!
14
+ name_required!
15
+ stack_name = name_args.last
16
+ root_stack = stack(stack_name)
17
+
18
+ print_only_original = config[:print_only]
19
+ config[:print_only] = true
20
+ load_template_file
21
+ config[:print_only] = print_only_original
22
+ api_action!(api_stack: root_stack) do
23
+ ui.info "Serverspec validating stack #{ui.color(root_stack.name, :bold)} with template #{config[:file].sub(Dir.pwd, '').sub(%r{^/}, '')}:" # rubocop:disable LineLength
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,9 @@
1
+ require 'sfn'
2
+
3
+ module Sfn
4
+ class Config
5
+ # Serverspec command configuration
6
+ class Serverspec < Validate
7
+ end
8
+ end
9
+ end
@@ -3,6 +3,7 @@ require 'net/ssh'
3
3
  require 'net/ssh/proxy/command'
4
4
  require 'rspec'
5
5
  require 'rspec/core/formatters/documentation_formatter'
6
+ require 'bogo-config/configuration'
6
7
  require 'serverspec'
7
8
  require 'sfn'
8
9
 
@@ -10,7 +11,7 @@ module Sfn
10
11
  # This is an sfn callback
11
12
  class Callback
12
13
  # Validate stack resources against Serverspec assertions
13
- class ServerspecValidator < Callback
14
+ class ServerspecValidator < Callback # rubocop:disable ClassLength
14
15
  # @return [Smash] cached policies
15
16
  attr_reader :policies
16
17
 
@@ -23,11 +24,6 @@ module Sfn
23
24
  end
24
25
 
25
26
  def after_create(*args)
26
- log = Logger.new(STDOUT)
27
- log.level = Logger.const_get(
28
- config.fetch(:sfn_serverspec, :log_level, :info).to_s.upcase
29
- )
30
-
31
27
  policies.each do |resource, r_config|
32
28
  resource_config = r_config.dump!.to_smash(:snake)
33
29
 
@@ -91,9 +87,9 @@ module Sfn
91
87
 
92
88
  spec_patterns = global_specs + resource_specs
93
89
 
94
- log.debug "spec loading patterns: #{spec_patterns.inspect}"
95
- log.debug "using SSH proxy commmand #{ssh_proxy_command}" unless ssh_proxy_command.nil?
96
- log.info "running specs against #{target_host}"
90
+ ui.debug "spec loading patterns: #{spec_patterns.inspect}"
91
+ ui.debug "using SSH proxy commmand #{ssh_proxy_command}" unless ssh_proxy_command.nil?
92
+ ui.info "Serverspec validating #{instance.id} (#{target_host})"
97
93
 
98
94
  Specinfra.configuration.backend :ssh
99
95
  Specinfra.configuration.request_pty true
@@ -112,12 +108,12 @@ module Sfn
112
108
 
113
109
  unless ssh_proxy_command.nil?
114
110
  connection_options[:proxy] = Net::SSH::Proxy::Command.new(ssh_proxy_command)
115
- log.debug "using ssh proxy command: #{ssh_proxy_command}"
111
+ ui.debug "using ssh proxy command: #{ssh_proxy_command}"
116
112
  end
117
113
 
118
114
  unless ssh_key_paths.empty?
119
115
  connection_options[:keys] = ssh_key_paths
120
- log.debug "using ssh key paths #{connection_options[:keys]} exclusively"
116
+ ui.debug "using ssh key paths #{connection_options[:keys]} exclusively"
121
117
  end
122
118
 
123
119
  unless ssh_key_passphrase.nil?
@@ -129,45 +125,47 @@ module Sfn
129
125
  RSpec::Core::Runner.run(spec_patterns.map { |p| Dir.glob(p) })
130
126
 
131
127
  rescue => e
132
- log.error "Something unexpected happened when running rspec: #{e.inspect}"
128
+ ui.error "Something unexpected happened when running rspec: #{e.inspect}"
133
129
  end
134
130
  end
135
131
  end
136
132
  end
137
- end
138
133
 
139
- COMPUTE_RESOURCE_TYPES = ['AWS::EC2::Instance', 'AWS::AutoScaling::AutoScalingGroup']
134
+ alias_method :after_serverspec, :after_create
140
135
 
141
- # Generate policy for stack, collate policies in cache
142
- #
143
- # @return [nil]
144
- def template(info)
145
- compiled_stack = info[:sparkle_stack].compile
136
+ COMPUTE_RESOURCE_TYPES = ['AWS::EC2::Instance', 'AWS::AutoScaling::AutoScalingGroup']
146
137
 
147
- compiled_stack.resources.keys!.each do |r|
148
- r_object = compiled_stack.resources[r]
149
- if COMPUTE_RESOURCE_TYPES.include?(r_object['Type']) && r_object['Serverspec']
150
- @policies.set(r, r_object.delete!('Serverspec'))
138
+ # Generate policy for stack, collate policies in cache
139
+ #
140
+ # @return [nil]
141
+ def template(info)
142
+ compiled_stack = info[:sparkle_stack].compile
143
+
144
+ compiled_stack.resources.keys!.each do |r|
145
+ r_object = compiled_stack.resources[r]
146
+ if COMPUTE_RESOURCE_TYPES.include?(r_object['Type']) && r_object['Serverspec']
147
+ @policies.set(r, r_object.delete!('Serverspec'))
148
+ end
151
149
  end
152
150
  end
153
- end
154
151
 
155
- private
152
+ private
156
153
 
157
- # look up stack resource by name, return array of expanded compute instances
158
- #
159
- # @param stack [Miasma::Models::Orchestration::Stack]
160
- # @param name [String]
161
- # @return [Array<Miasma::Models::Compute::Server>]
162
- def expand_compute_resource(stack, name)
163
- compute_resource = stack.resources.all.detect do |resource|
164
- resource.logical_id == name
165
- end
154
+ # look up stack resource by name, return array of expanded compute instances
155
+ #
156
+ # @param stack [Miasma::Models::Orchestration::Stack]
157
+ # @param name [String]
158
+ # @return [Array<Miasma::Models::Compute::Server>]
159
+ def expand_compute_resource(stack, name)
160
+ compute_resource = stack.resources.all.detect do |resource|
161
+ resource.logical_id == name
162
+ end
166
163
 
167
- if compute_resource.within?(:compute, :server)
168
- [compute_resource.instance.expand]
169
- else
170
- compute_resource.expand.servers.map(&:expand)
164
+ if compute_resource.within?(:compute, :servers)
165
+ [compute_resource.expand]
166
+ else
167
+ compute_resource.expand.servers.map(&:expand)
168
+ end
171
169
  end
172
170
  end
173
171
  end
@@ -176,7 +174,7 @@ end
176
174
  # Override the `#set` method provided by Serverspec to ensure any
177
175
  # `spec_helper.rb` files do not clobber our configuration setup
178
176
 
179
- alias :serverspec_set :set
177
+ alias :serverspec_set :set # rubocop:disable Alias
180
178
 
181
179
  def set(*args)
182
180
  serverspec_set(args.first)
@@ -1,5 +1,5 @@
1
1
  # SfnServerspec internal constants
2
2
  module SfnServerspec
3
3
  # Current version
4
- VERSION = Gem::Version.new('0.1.0')
4
+ VERSION = Gem::Version.new('0.1.2')
5
5
  end
@@ -14,5 +14,6 @@ Gem::Specification.new do |s|
14
14
  s.add_runtime_dependency 'serverspec', '~> 2.24'
15
15
  s.add_development_dependency 'rake'
16
16
  s.add_development_dependency 'rubocop', '~> 0.35.0'
17
+ s.add_development_dependency 'pry'
17
18
  s.files = Dir['{lib,bin,docs}/**/*'] + %w(sfn-serverspec.gemspec README.md CHANGELOG.md LICENSE)
18
19
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sfn-serverspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Heavy Water Operations
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-30 00:00:00.000000000 Z
11
+ date: 2016-01-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sfn
@@ -72,6 +72,20 @@ dependencies:
72
72
  - - "~>"
73
73
  - !ruby/object:Gem::Version
74
74
  version: 0.35.0
75
+ - !ruby/object:Gem::Dependency
76
+ name: pry
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
75
89
  description: Executes Serverspec assertions against stack compute resources
76
90
  email: support@heavywater.io
77
91
  executables: []
@@ -82,6 +96,8 @@ files:
82
96
  - LICENSE
83
97
  - README.md
84
98
  - lib/sfn-serverspec.rb
99
+ - lib/sfn-serverspec/command/serverspec.rb
100
+ - lib/sfn-serverspec/config/serverspec.rb
85
101
  - lib/sfn-serverspec/validator.rb
86
102
  - lib/sfn-serverspec/version.rb
87
103
  - sfn-serverspec.gemspec