specinfra-backend-salt 1.0.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: b84186c9096ae3b7226205cef3e4a83f6add4956c78081fd8d9f8b1488717c3c
4
+ data.tar.gz: 384b1c47508bde15b4d6ea4a56e4658fe8e6b6a64768e556158eef357c04200f
5
+ SHA512:
6
+ metadata.gz: 57945e96d70f85fd894d237bbdb6f8772a20c4f3990d21b1b8bcfce3a46a08877e30c558d2ccfcb40529b5fefa3845e5274935d767d2c65620cc596c3af28501
7
+ data.tar.gz: ffcdccc719c226ca3460f40bef2d8f6cd665aed477a102e97bdc00891010154771e9bf8f910a208db40c662ec8230cae97918da2b5cd01016c6041d3818d37c6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at 127820811+nyuyuyu@users.noreply.github.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 nyuyuyu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # Specinfra::Backend::Salt
2
+
3
+ This backend execute command on salt-minion from salt-master using `salt cmd.run` command.
4
+
5
+ So, this backend work on ***only salt-master*** .
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'specinfra-backend-salt'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install specinfra-backend-salt
22
+
23
+ ## Usage
24
+
25
+ An example for using [Serverspec](https://serverspec.org/).
26
+
27
+ ```ruby:spec_helper.rb
28
+ require 'serverspec'
29
+ require 'specinfra/backend/salt'
30
+
31
+ set :backend, :salt
32
+
33
+ if ENV['ASK_SALT_SUDO_PASSWORD']
34
+ begin
35
+ require 'highline/import'
36
+ rescue LoadError
37
+ fail "highline is not available. Try installing it."
38
+ end
39
+ set :salt_sudo_password, ask("Enter sudo password: ") { |q| q.echo = false }
40
+ else
41
+ set :salt_sudo_password, ENV['SALT_SUDO_PASSWORD']
42
+ end
43
+
44
+ # :host should be a minion ID.
45
+ set :host, ENV['TARGET_HOST']
46
+ ```
47
+
48
+ ## Optional options
49
+
50
+ - `:salt_user` Specify the username to execute command on salt-minion. (default: `root`)
51
+ - `:salt_become_method` Specify the privilege escalation method to execute `salt run.cmd` on salt-master. (default: `:sudo`, `:su` or `:none`)
52
+ - `:salt_sudo_user` Specify the username to execute `salt run.cmd` with `sudo` on salt-master. (default: `root`)
53
+ - `:salt_sudo_password` Specify the password of `:salt_sudo_user` user.
54
+ - `:salt_sudo_path` Specify the path of the directory where the `sudo` is placed on salt-master.
55
+ - `:salt_su_user` Specify the username to execute `salt run.cmd` with `su` on salt-master. (default: `root`)
56
+ - `:salt_su_password` Specify the password of `:salt_su_user` user.
57
+ - `:salt_su_path` Specify the path of the directory where the `su` is placed on salt-master.
58
+
59
+ ## Contributing
60
+
61
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nyuyuyu/specinfra-backend-salt. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
62
+
63
+ ## License
64
+
65
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
66
+
67
+ ## Code of Conduct
68
+
69
+ Everyone interacting in the Specinfra::Backend::Salt project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/nyuyuyu/specinfra-backend-salt/blob/main/CODE_OF_CONDUCT.md).
@@ -0,0 +1,172 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ require 'specinfra/backend/base'
5
+ require 'specinfra/backend/exec'
6
+ require 'pty'
7
+ require 'expect'
8
+ require 'open3'
9
+ require 'json'
10
+
11
+ module Specinfra
12
+ module Backend
13
+ class Salt < Exec # rubocop:disable Metrics/ClassLength
14
+ def run_command(cmd, _opts = {})
15
+ raise 'DO NOT INCLUDE COMMA IN HOSTNAME!' if get_config(:host) =~ /,/
16
+
17
+ cmd = build_command(cmd)
18
+ ret = salt_exec!(cmd)
19
+
20
+ if @example
21
+ @example.metadata[:command] = cmd
22
+ @example.metadata[:stdout] = ret[:stdout]
23
+ end
24
+
25
+ CommandResult.new ret
26
+ end
27
+
28
+ def send_file(_from, _to)
29
+ raise 'not implemented'
30
+ end
31
+
32
+ def send_directory(_from, _to)
33
+ raise 'not implemented'
34
+ end
35
+
36
+ def build_command(cmd)
37
+ cmd = build_salt_command(cmd)
38
+
39
+ case get_config(:salt_become_method)
40
+ when :none
41
+ cmd
42
+ when :su
43
+ cmd = "/bin/sh -c #{cmd.shellescape} 2> /dev/null"
44
+ "#{su} #{cmd.shellescape}"
45
+ else
46
+ "#{sudo} /bin/sh -c #{cmd.shellescape}"
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ def build_salt_command(cmd)
53
+ options = "env='#{load_env}'"
54
+
55
+ user = get_config(:salt_user)
56
+ options = "#{options} runas='#{user}'" if user
57
+ shell = get_config(:shell) || '/bin/sh'
58
+ options = "#{options} shell='#{shell}'"
59
+
60
+ # avoid failing commands by removing trailing whitespace by salt command.
61
+ cmd += ';'
62
+
63
+ pre_cmd = get_config(:pre_command)
64
+ cmd = "#{pre_cmd} && #{cmd}" if pre_cmd
65
+
66
+ "salt -L #{get_config(:host)} --out=json cmd.run #{options} #{cmd.shellescape}"
67
+ end
68
+
69
+ def load_env
70
+ env = get_config(:env) || {}
71
+ env[:LANG] ||= 'C'
72
+ env.to_json
73
+ end
74
+
75
+ def salt_error_message_regexp
76
+ /^Minion\ did\ not\ return\.\ \[.*\]$/
77
+ end
78
+
79
+ def sudo_fail_message_regexp
80
+ /(Sorry,\ try\ again\.|sudo:\ [0-9]+\ incorrect\ password\ attempts)\R/
81
+ end
82
+
83
+ def su_prompt_regexp
84
+ /^Password:\s*/
85
+ end
86
+
87
+ def su_fail_message_regexp
88
+ /^su:\ (Authentication\ failure|Sorry)\R/
89
+ end
90
+
91
+ def parse_salt_response(response)
92
+ stdout_data = JSON.parse(response[:stdout])[get_config(:host)]
93
+ raise "salt command failed. #{response}" if stdout_data =~ salt_error_message_regexp
94
+
95
+ stderr_data = response[:stderr]
96
+ exit_status = response[:exit_status]
97
+
98
+ { stdout: stdout_data, stderr: stderr_data, exit_status: exit_status }
99
+ rescue JSON::ParserError
100
+ raise "salt command failed. #{response}"
101
+ end
102
+
103
+ def salt_exec!(cmd)
104
+ r = if get_config(:salt_become_method) == :su
105
+ exec_with_pty!(cmd)
106
+ else
107
+ exec_with_open3!(cmd)
108
+ end
109
+ parse_salt_response(r)
110
+ end
111
+
112
+ def exec_with_open3!(cmd)
113
+ sudo_password = nil
114
+ if get_config(:salt_become_method) != :none && get_config(:salt_sudo_password)
115
+ sudo_password = "#{get_config(:salt_sudo_password)}\n"
116
+ end
117
+
118
+ stdout_data, stderr_data, status = Open3.capture3(cmd, stdin_data: sudo_password)
119
+ raise 'Wrong sudo password! Please confirm your password.' if stderr_data =~ sudo_fail_message_regexp
120
+
121
+ { stdout: stdout_data, stderr: stderr_data, exit_status: status.exitstatus }
122
+ end
123
+
124
+ def exec_with_pty!(cmd) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
125
+ stdout_data = ''
126
+ stderr_data = ''
127
+ exit_status = nil
128
+
129
+ PTY.spawn({ 'LANG' => 'C' }, cmd) do |r, w, pid|
130
+ w.sync = true
131
+
132
+ unless Process.uid.zero?
133
+ r.expect(su_prompt_regexp) do
134
+ w.puts get_config(:salt_su_password)
135
+ end
136
+ end
137
+
138
+ begin
139
+ r.each do |line|
140
+ raise 'Wrong su password! Please confirm your password.' if line =~ su_fail_message_regexp
141
+
142
+ stdout_data << line
143
+ end
144
+ rescue Errno::EIO
145
+ # NOP
146
+ ensure
147
+ p = Process.wait2 pid
148
+ exit_status = p[1].exitstatus
149
+ end
150
+ end
151
+
152
+ { stdout: stdout_data, stderr: stderr_data, exit_status: exit_status }
153
+ end
154
+
155
+ def sudo
156
+ sudo_path = 'sudo'
157
+ sudo_path = "#{get_config(:salt_sudo_path)}/#{sudo_path}" if get_config(:salt_sudo_path)
158
+ sudo_user = get_config(:salt_sudo_user) || 'root'
159
+
160
+ "#{sudo_path} -S -u #{sudo_user}"
161
+ end
162
+
163
+ def su
164
+ su_path = 'su'
165
+ su_path = "#{get_config(:salt_su_path)}/#{su_path}" if get_config(:salt_su_path)
166
+ su_user = get_config(:salt_su_user) || 'root'
167
+
168
+ "#{su_path} #{su_user} -c"
169
+ end
170
+ end
171
+ end
172
+ end
@@ -0,0 +1,8 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Specinfra
5
+ module SaltBackend
6
+ VERSION = '1.0.0'
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: specinfra-backend-salt
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - nyuyuyu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-04-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: specinfra
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '1.13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '1.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '12.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Specinfra backend for SaltStack
84
+ email:
85
+ - 127820811+nyuyuyu@users.noreply.github.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - CODE_OF_CONDUCT.md
91
+ - LICENSE
92
+ - README.md
93
+ - lib/specinfra/backend/salt.rb
94
+ - lib/specinfra/salt_backend/version.rb
95
+ homepage: https://github.com/nyuyuyu/specinfra-backend-salt
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubygems_version: 3.4.10
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Specinfra backend for SaltStack
118
+ test_files: []