prerequisites 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +8 -0
  3. data/Gemfile.lock +36 -0
  4. data/LICENSE +7 -0
  5. data/README.md +49 -0
  6. data/lib/prerequisites.rb +66 -0
  7. metadata +64 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e8ff637be0e81f14cf0f706d1fa4c9ec7fa3ae9959fa96212b20153365049863
4
+ data.tar.gz: a3b75123ad34c9faf976a2f762fc2eb8b4b0f4d298d31b40d6e585aee355d932
5
+ SHA512:
6
+ metadata.gz: 869c46534e715fc19e9369c5a8fcb438f3a716b1574285b1990e87136f7f8130e9485d76098229a769bdbbd44808da27f57c387e65fdb8fc392aad64fd2d4550
7
+ data.tar.gz: 2fb73fb4bf3306abe07e9712821158b8ba92e0b46bbfb25ab2fac8b69372bc6e828fa7d2d75a21f5e49f585b058199fa40a90bf1b41ccee390daeed5d3e73851
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ gem "rspec"
8
+ gem "pry-byebug"
data/Gemfile.lock ADDED
@@ -0,0 +1,36 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ byebug (11.1.1)
5
+ coderay (1.1.2)
6
+ diff-lcs (1.3)
7
+ method_source (1.0.0)
8
+ pry (0.13.0)
9
+ coderay (~> 1.1)
10
+ method_source (~> 1.0)
11
+ pry-byebug (3.9.0)
12
+ byebug (~> 11.0)
13
+ pry (~> 0.13.0)
14
+ rspec (3.9.0)
15
+ rspec-core (~> 3.9.0)
16
+ rspec-expectations (~> 3.9.0)
17
+ rspec-mocks (~> 3.9.0)
18
+ rspec-core (3.9.1)
19
+ rspec-support (~> 3.9.1)
20
+ rspec-expectations (3.9.1)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.9.0)
23
+ rspec-mocks (3.9.1)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.9.0)
26
+ rspec-support (3.9.2)
27
+
28
+ PLATFORMS
29
+ ruby
30
+
31
+ DEPENDENCIES
32
+ pry-byebug
33
+ rspec
34
+
35
+ BUNDLED WITH
36
+ 2.0.1
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2020 David Salgado
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # Prerequisites Gem
2
+
3
+ A ruby gem to make it easy to test that a set of prerequisite conditions are in place.
4
+
5
+ This is particularly useful when using ruby as 'glue' code for sysadmin or other tasks.
6
+
7
+ ## Installation
8
+
9
+ ```
10
+ gem install prerequisites
11
+ ```
12
+
13
+ Or add to your `Gemfile`:
14
+
15
+ ```
16
+ gem "prerequisites"
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ This example checks that:
22
+
23
+ * Environment variables "FOO" and "BAR" are set
24
+ * Environment variable "BAZ" is set to the value "whatever"
25
+ * Executables "grep", "git", and "kubectl" are in your $PATH
26
+
27
+ ```
28
+ config = {
29
+ environment_variables: [
30
+ "FOO",
31
+ "BAR",
32
+ { "BAZ" => "whatever" }
33
+ ],
34
+ executables_in_path: [
35
+ "grep",
36
+ "git",
37
+ "kubectl"
38
+ ]
39
+ }
40
+
41
+ Prerequisites.new(config).check
42
+ ```
43
+
44
+ If any of these conditions are not met, the code will raise an error of class:
45
+
46
+ * Prerequisites::EnvironmentVariableError
47
+ * Prerequisites::ExecutableError
48
+
49
+ ...depending on the problem.
@@ -0,0 +1,66 @@
1
+ require "open3"
2
+
3
+ class Prerequisites
4
+ attr_reader :config
5
+
6
+ def initialize(config)
7
+ @config = config
8
+ end
9
+
10
+ def check
11
+ environment_variables
12
+ executables_in_path
13
+ end
14
+
15
+ private
16
+
17
+ def environment_variables
18
+ config.dig(:environment_variables).to_a.each do |env_var|
19
+ if env_var.is_a?(Hash)
20
+ var, value = env_var.first
21
+ check_env_var_has_value(var, value)
22
+ else
23
+ check_env_var_is_set(env_var)
24
+ end
25
+ end
26
+
27
+ true
28
+ end
29
+
30
+ def executables_in_path
31
+ config.dig(:executables_in_path).to_a.each do |executable|
32
+ check_executable(executable)
33
+ end
34
+
35
+ true
36
+ end
37
+
38
+ def check_env_var_is_set(env_var)
39
+ unless ENV.key?(env_var)
40
+ raise Prerequisites::EnvironmentVariableError.new(
41
+ "Required environment variable #{env_var} is not set"
42
+ )
43
+ end
44
+ end
45
+
46
+ def check_env_var_has_value(var, value)
47
+ unless ENV[var] == value
48
+ raise Prerequisites::EnvironmentVariableError.new(
49
+ "Expected environment variable #{var} to be #{value}"
50
+ )
51
+ end
52
+ end
53
+
54
+ def check_executable(executable)
55
+ _, _, status = Open3.capture3("which #{executable}")
56
+
57
+ unless status.success?
58
+ raise Prerequisites::ExecutableError.new(
59
+ "Required executable #{executable} not found in path"
60
+ )
61
+ end
62
+ end
63
+ end
64
+
65
+ class Prerequisites::EnvironmentVariableError < RuntimeError; end
66
+ class Prerequisites::ExecutableError < RuntimeError; end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prerequisites
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - David Salgado
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-03-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.9'
27
+ description:
28
+ email: david@digitalronin.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files:
32
+ - README.md
33
+ files:
34
+ - Gemfile
35
+ - Gemfile.lock
36
+ - LICENSE
37
+ - README.md
38
+ - lib/prerequisites.rb
39
+ homepage: https://github.com/digitalronin/prerequisites-gem
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options:
45
+ - "--main"
46
+ - README.md
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubygems_version: 3.0.3
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Simplify checking pre-requisites (env. vars. & executables) for ruby scripts
64
+ test_files: []