kpn_winrm 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0a8fa621a194fcac699a2a168a87d3bbb55f0fac53e8da0daf938e5ede20c7e5
4
+ data.tar.gz: cab7d40f550adb3a2d6336df145213c9efeff7e188b07c43949e666fd5981b82
5
+ SHA512:
6
+ metadata.gz: 58cc3cb31c6a6941f827f90249799b5f02122e9010c75923b5947ee9d53ee5486afc0f5c1923f9fca79195f0a990e3307b0772e0a4d1f054304e320a5ecd4d9a
7
+ data.tar.gz: f12ea25c53b8ae58a045328c4f6c2bdc7899509b6cbfaf6ed6546710030957df3b4ddc5e2853c3617f7a8e4083ef4f2b4abfde900a88d307206f887a19f73e2b
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ Gemfile.lock
13
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ inherit_gem:
3
+ kpn-style:
4
+ - ruby-2.1.yml
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in kpn_winrm.gemspec
6
+ gemspec
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # kpn_winrm
2
+
3
+ This gem is used to execute Puppet module acceptance tests (beaker) for Windows using WinRM instead of SSH.
4
+
5
+
6
+ TODO: rspec test for this gem
7
+
8
+ ## Dependencies
9
+
10
+ This gem has two gem dependencies:
11
+ - [winrm](https://rubygems.org/gems/winrm) (version requirement: >= 2.2.2)
12
+ - [winrm-elevated](https://rubygems.org/gems/winrm-elevated) (version requirement: >= 1.1.0)
13
+
14
+ ## Installation
15
+
16
+ Add this line to your spec_helper_acceptance.rb:
17
+
18
+ ```ruby
19
+ require 'kpn_winrm'
20
+ ```
21
+
22
+ Make sure the gem is installed:
23
+
24
+ $ gem install kpn_winrm
25
+
26
+
27
+ ## Usage
28
+
29
+ This gem contains 2 functions:
30
+ - apply_manifest_on_winrm
31
+ - winrm_command
32
+
33
+ ### apply_manifest_on_winrm
34
+ This function can be used to apply a puppet manifest on the beaker node using winrm.
35
+
36
+ The functions has 3 parameters:
37
+ #### host
38
+ The host definition on which to execute the commands, for one node tests set this to `default`.
39
+
40
+ #### manifest
41
+ The ruby variable containting the manifest to apply, example:
42
+ ```ruby
43
+ pp = <<-PP
44
+ user { 'testuser':
45
+ password => 'Dummy1234qwer!',
46
+ groups => ['Administrators'],
47
+ }
48
+ PP
49
+ ```
50
+
51
+ #### opts
52
+ A hash of options, which can have one of the following:
53
+ - catch_changes
54
+ - catch_failures
55
+ - expect_failures
56
+ - expect_changes
57
+
58
+ Example
59
+ ```ruby
60
+ apply_manifest_on_winrm(default, pp, :catch_failures => true)
61
+ apply_manifest_on_winrm(default, pp, :catch_changes => true)
62
+ ```
63
+
64
+ ### winrm_command
65
+ Run a command on the beaker node via winrm.
66
+
67
+ The functions has 3 parameters:
68
+ #### host
69
+ The host definition on which to execute the commands, for one node tests set this to `default`.
70
+
71
+ #### command
72
+ The command which should be executed via WinRM.
73
+ - acceptable_exit_codes
74
+ - run_as_cmd
75
+
76
+ #### opts
77
+ A hash of options, which can have one of the following:
78
+
79
+ Example
80
+ ```ruby
81
+ winrm_command(default, 'SecEdit /export /cfg c:\cfg.txt')
82
+ ```
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+
2
+ require 'bundler/gem_tasks'
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
data/kpn_winrm.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'kpn_winrm/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'kpn_winrm'
7
+ spec.version = KpnWinrm::VERSION
8
+ spec.authors = ['kpn']
9
+ spec.email = ['noreply@kpn.com']
10
+
11
+ spec.summary = 'Additional functions for acceptance testing, to run commands and puppet apply over WinRM instead of ssh'
12
+ spec.homepage = 'https://github.com/kpn-puppet/gem-kpn-winrm'
13
+ spec.license = 'Apache-2.0'
14
+
15
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
16
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
17
+ if spec.respond_to?(:metadata)
18
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
19
+ else
20
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
21
+ 'public gem pushes.'
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
25
+ f.match(%r{^(test|spec|features)/})
26
+ end
27
+ spec.bindir = 'exe'
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ['lib']
30
+
31
+ spec.add_dependency 'winrm', '~> 2.2', '>= 2.2.2'
32
+ spec.add_dependency 'winrm-elevated', '~> 1.1', '>= 1.1.0'
33
+
34
+ spec.add_development_dependency 'bundler', '~> 1.16'
35
+ spec.add_development_dependency 'rspec', '~> 3.0'
36
+ end
data/lib/kpn_winrm.rb ADDED
@@ -0,0 +1,99 @@
1
+ require 'winrm'
2
+ require 'winrm-elevated'
3
+
4
+ # kpn_winrm
5
+ module KpnWinrm
6
+ def self.winrm_command(host, command, opts = {})
7
+ # Get parameters
8
+ puts host
9
+ user = host[:ssh][:user]
10
+ pass = host[:ssh][:password]
11
+ hostname = host.to_s
12
+ endpoint = 'http://' + hostname + ':5985/wsman'
13
+ acceptable_exit_codes = if opts.key?(:acceptable_exit_codes) && !opts[:acceptable_exit_codes].nil?
14
+ opts[:acceptable_exit_codes]
15
+ else
16
+ opts[:acceptable_exit_codes] = [0]
17
+ end
18
+
19
+ run_as_cmd = false
20
+ if opts.key?(:run_as_cmd) && !opts[:acceptable_exit_codes].nil?
21
+ run_as_cmd = true
22
+ end
23
+
24
+ # Setup winrm
25
+ opts = {
26
+ user: user,
27
+ password: pass,
28
+ endpoint: endpoint,
29
+ operation_timeout: 300,
30
+ }
31
+ host.logger.debug 'Trying to connect to windows host'
32
+ p opts.inspect
33
+ winrm = WinRM::Connection.new opts
34
+
35
+ # Debugging
36
+ host.logger.debug "Running command '#{command}' via winrm"
37
+ # Execute command via winrm
38
+ if run_as_cmd
39
+ winrm.shell(:cmd) do |shell|
40
+ @result = shell.run(command) do |stdout, stderr|
41
+ host.logger.debug stdout
42
+ host.logger.debug stderr
43
+ end
44
+ end
45
+ else
46
+ winrm.shell(:elevated) do |shell|
47
+ @result = shell.run(command) do |stdout, stderr|
48
+ host.logger.debug stdout
49
+ host.logger.debug stderr
50
+ end
51
+ end
52
+ end
53
+
54
+ # Debugging
55
+ host.logger.debug "winrm - stdout :#{stdout}"
56
+ host.logger.debug "winrm - stderr :#{stderr}"
57
+ host.logger.debug "winrm - exitcode:#{@result.exitcode}"
58
+
59
+ unless acceptable_exit_codes.include?(@result.exitcode)
60
+ raise StandardError, "Command '#{command}' failed with unacceptable exit code:#{@result.exitcode} on host '#{hostname}'\n" \
61
+ "Stdout:#{@result.stdout}\n" \
62
+ "Stderr:#{@result.stderr}\n"
63
+ end
64
+
65
+ # Return flat hash with stdout, stderr and the exitcode
66
+ { stdout: @result.stdout,
67
+ stderr: @result.stderr,
68
+ exitcode: @result.exitcode }
69
+ end
70
+
71
+ def self.apply_manifest_on_winrm(host, manifest, opts = {})
72
+ if [opts[:catch_changes], opts[:catch_failures], opts[:expect_failures], opts[:expect_changes]].compact.length > 1
73
+ raise StandardError, 'only one of :catch_changes, :catch_failures, :expect_failures and :expect_changes should be set'
74
+ end
75
+
76
+ acceptedexitcodes = [0]
77
+
78
+ if opts[:catch_changes]
79
+ acceptedexitcodes = [0]
80
+ end
81
+
82
+ if opts[:catch_failures]
83
+ acceptedexitcodes = [0, 2]
84
+ end
85
+
86
+ if opts[:expect_failures]
87
+ acceptedexitcodes = [1, 4, 6]
88
+ end
89
+
90
+ if opts[:expect_changes]
91
+ acceptedexitcodes = [2]
92
+ end
93
+
94
+ file_path = host.tmpfile('apply_manifest.pp')
95
+ create_remote_file(host, file_path, manifest + "\n")
96
+
97
+ winrm_command(host, 'puppet apply --detailed-exitcodes ' + file_path + '; exit $lastexitcode', acceptable_exit_codes: acceptedexitcodes)
98
+ end
99
+ end
@@ -0,0 +1,3 @@
1
+ module KpnWinrm
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kpn_winrm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - kpn
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-04-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: winrm
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.2'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.2.2
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.2'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.2.2
33
+ - !ruby/object:Gem::Dependency
34
+ name: winrm-elevated
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.1'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 1.1.0
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '1.1'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 1.1.0
53
+ - !ruby/object:Gem::Dependency
54
+ name: bundler
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '1.16'
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - "~>"
65
+ - !ruby/object:Gem::Version
66
+ version: '1.16'
67
+ - !ruby/object:Gem::Dependency
68
+ name: rspec
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: '3.0'
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: '3.0'
81
+ description:
82
+ email:
83
+ - noreply@kpn.com
84
+ executables: []
85
+ extensions: []
86
+ extra_rdoc_files: []
87
+ files:
88
+ - ".gitignore"
89
+ - ".rspec"
90
+ - ".rubocop.yml"
91
+ - Gemfile
92
+ - README.md
93
+ - Rakefile
94
+ - kpn_winrm.gemspec
95
+ - lib/kpn_winrm.rb
96
+ - lib/kpn_winrm/version.rb
97
+ homepage: https://github.com/kpn-puppet/gem-kpn-winrm
98
+ licenses:
99
+ - Apache-2.0
100
+ metadata:
101
+ allowed_push_host: https://rubygems.org
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.7.6
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: Additional functions for acceptance testing, to run commands and puppet apply
122
+ over WinRM instead of ssh
123
+ test_files: []