kanrisuru 0.18.0 → 0.19.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bde6a8003df4dbe874a1b2471eb042b294fabc7e84d4146458fd3a1dbf4882ca
4
- data.tar.gz: 4b2e3f0f35c62ee2286ad9aea4ae32cd7044418cc141c1abc176c5b017821c8d
3
+ metadata.gz: 80ead7b757f8228b63f2908df9163f726e99ec27dcd2b88f7a5ff197e98f8008
4
+ data.tar.gz: 8393af4b3e03f583981b06eaa384b47527882870289df125d4887f98abb293b2
5
5
  SHA512:
6
- metadata.gz: b32475bde12456c03a3028f8ebbc6f792d59ad69b8014737d266d65ea4d4ba3208f864aa1eb8de58929abc8460e41971371fc6d4c3bc6c2d6e28db54c73fc9b5
7
- data.tar.gz: a22f5af50adce7dc76504ba78fdddd000d08766e2115e4d092b12e8d44b5b76aa3e0f68cf6286c3344cde9d82520238ecd96b2e6b0b11384cc4ec859bbcebd2a
6
+ metadata.gz: 2268c08d84aa9cf09232900c7d2ccd489cb9de12aa77c3fdc9666eb8859d41b072a932d0afb0d6ed2818978e641dee1569ddeda30f417466f7bd1a8150206cb9
7
+ data.tar.gz: e4ef880302b200deafb4c69e703cfb79fc4162d20d6b6269a635e0277f790c4c872299513cab53edeb395f20e15660fda09ba7d0c135b8ebf97d67b7464e1859
data/CHANGELOG.md CHANGED
@@ -1,8 +1,11 @@
1
+ ## Kanrisuru 0.19.0 (January 25, 2022) ##
2
+ * Add readonly version of `sysctl` command with test cases.
3
+
1
4
  ## Kanrisuru 0.18.0 (January 23, 2022) ##
2
- * Add `history` command with test cases.
5
+ * Add `history` command with test cases.
3
6
 
4
7
  ## Kanrisuru 0.17.0 (January 20, 2022) ##
5
- * Add `nproc` command with test cases.
8
+ * Add `nproc` command with test cases.
6
9
 
7
10
  ## Kanrisuru 0.16.17 (January 10, 2022) ##
8
11
  * Add additional options to `wget` command.
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kanrisuru
4
+ module Core
5
+ module System
6
+ def sysctl(variable = nil)
7
+ command = Kanrisuru::Command.new('sysctl')
8
+
9
+ ## Some older versions have a bug with 255 exit code being returned
10
+ command.append_valid_exit_code(255)
11
+
12
+ if Kanrisuru::Util.present?(variable)
13
+ command << variable
14
+ else
15
+ command.append_flag('--all')
16
+ end
17
+
18
+ execute_shell(command)
19
+
20
+ Kanrisuru::Result.new(command) do |cmd|
21
+ Parser::Sysctl.parse(cmd)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -14,6 +14,7 @@ require_relative 'commands/nproc'
14
14
  require_relative 'commands/poweroff'
15
15
  require_relative 'commands/ps'
16
16
  require_relative 'commands/reboot'
17
+ require_relative 'commands/sysctl'
17
18
  require_relative 'commands/uptime'
18
19
  require_relative 'commands/w'
19
20
 
@@ -8,5 +8,6 @@ require_relative 'parsers/load_env'
8
8
  require_relative 'parsers/lscpu'
9
9
  require_relative 'parsers/lsof'
10
10
  require_relative 'parsers/ps'
11
+ require_relative 'parsers/sysctl'
11
12
  require_relative 'parsers/uptime'
12
13
  require_relative 'parsers/w'
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+ require 'ostruct'
3
+
4
+ module Kanrisuru
5
+ module Core
6
+ module System
7
+ module Parser
8
+ class Sysctl
9
+ class << self
10
+ def parse(command)
11
+ result = {}
12
+
13
+ lines = command.to_a
14
+ lines.each do |line|
15
+ next if line.include?("permission denied on key")
16
+
17
+ keys, value = parse_line(line)
18
+ next if Kanrisuru::Util.blank?(value)
19
+
20
+ hash = build_nested_hash(keys, value)
21
+ result = merge_recursively(result, hash)
22
+ end
23
+
24
+ build_struct(result)
25
+ end
26
+
27
+ def build_struct(hash)
28
+ struct = Struct.new(*hash.keys).new
29
+
30
+ hash.keys.each do |key|
31
+ struct[key] = hash[key].is_a?(Hash) ? build_struct(hash[key]) : hash[key]
32
+ end
33
+
34
+ struct
35
+ end
36
+
37
+ def merge_recursively(h1, h2)
38
+ h1.merge(h2) do |k, v1, v2|
39
+ if v1.is_a?(Hash) && v2.is_a?(Hash)
40
+ merge_recursively(v1, v2)
41
+ else
42
+ [v1, v2].flatten
43
+ end
44
+ end
45
+ end
46
+
47
+ def build_nested_hash(array, value)
48
+ array.reverse.inject(value) { |assigned_value, key| { key => assigned_value } }
49
+ end
50
+
51
+ def parse_line(line)
52
+ string, value = line.split(' =')
53
+ return if Kanrisuru::Util.blank?(value)
54
+
55
+ string = string.strip
56
+ value = value.strip
57
+
58
+ if Kanrisuru::Util.numeric?(value)
59
+ value = value.to_i
60
+ elsif /\t+/.match(value)
61
+ ## Split tab delimited strings
62
+ value = value.split(/\t/)
63
+ end
64
+
65
+ [string.split('.').map(&:to_sym), value]
66
+ end
67
+
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kanrisuru
4
- VERSION = '0.18.0'
4
+ VERSION = '0.19.0'
5
5
  end
@@ -93,6 +93,11 @@ RSpec.describe Kanrisuru::Core::System do
93
93
  expect_command(host.lsof, 'lsof -F pcuftDsin')
94
94
  end
95
95
 
96
+ it 'prepares sysctl command' do
97
+ expect_command(host.sysctl, 'sysctl --all')
98
+ expect_command(host.sysctl('net.ipv4.conf.all'), 'sysctl net.ipv4.conf.all')
99
+ end
100
+
96
101
  it 'prepares poweroff command' do
97
102
  expect_command(host.poweroff, 'shutdown')
98
103
  expect_command(host.poweroff(cancel: true), 'shutdown -c')
@@ -100,6 +100,20 @@ RSpec.shared_examples 'system' do |os_name, host_json, _spec_dir|
100
100
  expect(result.cpus.length).to eq(host.cpu.cores)
101
101
  end
102
102
 
103
+ it 'gets kernel sysctl info' do
104
+ result = host.sysctl
105
+ expect(result).to be_success
106
+ expect(result).to respond_to :net
107
+ expect(result).to respond_to :kernel
108
+ expect(result).to respond_to :dev
109
+
110
+ result = host.sysctl('net.ipv4.conf')
111
+ expect(result).to be_success
112
+ expect(result).to respond_to :net
113
+ expect(result.net).to respond_to :ipv4
114
+ expect(result.net.ipv4).to respond_to :conf
115
+ end
116
+
103
117
  it 'gets login details' do
104
118
  host.su('root')
105
119
 
@@ -33,6 +33,7 @@ RSpec.describe Kanrisuru::Core::System do
33
33
  expect(host).to respond_to(:lsof)
34
34
  expect(host).to respond_to(:nproc)
35
35
  expect(host).to respond_to(:last)
36
+ expect(host).to respond_to(:sysctl)
36
37
  expect(host).to respond_to(:uptime)
37
38
  expect(host).to respond_to(:w)
38
39
  expect(host).to respond_to(:who)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kanrisuru
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.18.0
4
+ version: 0.19.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Mammina
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-23 00:00:00.000000000 Z
11
+ date: 2022-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parallel_tests
@@ -319,6 +319,7 @@ files:
319
319
  - lib/kanrisuru/core/system/commands/poweroff.rb
320
320
  - lib/kanrisuru/core/system/commands/ps.rb
321
321
  - lib/kanrisuru/core/system/commands/reboot.rb
322
+ - lib/kanrisuru/core/system/commands/sysctl.rb
322
323
  - lib/kanrisuru/core/system/commands/uptime.rb
323
324
  - lib/kanrisuru/core/system/commands/w.rb
324
325
  - lib/kanrisuru/core/system/parser.rb
@@ -330,6 +331,7 @@ files:
330
331
  - lib/kanrisuru/core/system/parsers/lscpu.rb
331
332
  - lib/kanrisuru/core/system/parsers/lsof.rb
332
333
  - lib/kanrisuru/core/system/parsers/ps.rb
334
+ - lib/kanrisuru/core/system/parsers/sysctl.rb
333
335
  - lib/kanrisuru/core/system/parsers/uptime.rb
334
336
  - lib/kanrisuru/core/system/parsers/w.rb
335
337
  - lib/kanrisuru/core/system/types.rb