kanrisuru 0.17.0 → 0.19.2
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 +4 -4
- data/CHANGELOG.md +13 -1
- data/lib/kanrisuru/core/system/commands/history.rb +29 -0
- data/lib/kanrisuru/core/system/commands/sysctl.rb +26 -0
- data/lib/kanrisuru/core/system/commands.rb +2 -0
- data/lib/kanrisuru/core/system/parser.rb +2 -0
- data/lib/kanrisuru/core/system/parsers/history.rb +18 -0
- data/lib/kanrisuru/core/system/parsers/load_env.rb +2 -3
- data/lib/kanrisuru/core/system/parsers/sysctl.rb +73 -0
- data/lib/kanrisuru/result.rb +4 -0
- data/lib/kanrisuru/version.rb +1 -1
- data/spec/functional/core/system_spec.rb +19 -0
- data/spec/support/shared_examples/integration/core/system.rb +20 -0
- data/spec/unit/core/system_spec.rb +2 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 750858fb5bf17a34ad853a052cf6f0c13f3226acb7d92a0c26537740b6cf44e2
|
4
|
+
data.tar.gz: f15b5d63e3687b3d605ccc24c3b929d3e16cdaa0ed5e871964716e6f6529b1d1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f8cf821cfe1260da1f87e7001093cbbca2571087076b8bdb478f58b128399dddd05448648a9c4b15c07346c010cc01bf58a63b94d030179a141d6afbe97f8598
|
7
|
+
data.tar.gz: da6b7c1337dc893dd131329670594277f3e4da5fbb5b4502a9a7b5c7434cf8e32666397fadbfb3fb570b6196e10d6775d12c9cd3e8cedab9c8e58020f50f11bc
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,17 @@
|
|
1
|
+
## Kanrisuru 0.19.2 (February 02, 2022) ##
|
2
|
+
* Add `to_h` to result class.
|
3
|
+
|
4
|
+
## Kanrisuru 0.19.1 (February 02, 2022) ##
|
5
|
+
* Fix `load_env` command with parsing output, use `to_a` for cleaner newline parsing.
|
6
|
+
|
7
|
+
## Kanrisuru 0.19.0 (January 25, 2022) ##
|
8
|
+
* Add readonly version of `sysctl` command with test cases.
|
9
|
+
|
10
|
+
## Kanrisuru 0.18.0 (January 23, 2022) ##
|
11
|
+
* Add `history` command with test cases.
|
12
|
+
|
1
13
|
## Kanrisuru 0.17.0 (January 20, 2022) ##
|
2
|
-
*
|
14
|
+
* Add `nproc` command with test cases.
|
3
15
|
|
4
16
|
## Kanrisuru 0.16.17 (January 10, 2022) ##
|
5
17
|
* Add additional options to `wget` command.
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Kanrisuru
|
4
|
+
module Core
|
5
|
+
module System
|
6
|
+
def history(opts = {})
|
7
|
+
histfile = opts[:histfile] || '~/.bash_history'
|
8
|
+
|
9
|
+
command = Kanrisuru::Command.new("HISTFILE=#{histfile}; history -r; history")
|
10
|
+
|
11
|
+
if Kanrisuru::Util.present?(opts[:delete])
|
12
|
+
command.append_arg('-d', Kanrisuru::Util.array_join_string(opts[:delete], ' '))
|
13
|
+
elsif Kanrisuru::Util.present?(opts[:clear])
|
14
|
+
command.append_flag('-c')
|
15
|
+
elsif Kanrisuru::Util.present?(opts[:n])
|
16
|
+
command << opts[:n]
|
17
|
+
end
|
18
|
+
|
19
|
+
execute_shell(command)
|
20
|
+
|
21
|
+
Kanrisuru::Result.new(command) do |cmd|
|
22
|
+
return if Kanrisuru::Util.present?(opts[:delete]) || Kanrisuru::Util.present?(opts[:clear])
|
23
|
+
|
24
|
+
Parser::History.parse(cmd)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -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
|
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require_relative 'commands/cpu_info'
|
4
4
|
require_relative 'commands/free'
|
5
|
+
require_relative 'commands/history'
|
5
6
|
require_relative 'commands/kernel_statistics'
|
6
7
|
require_relative 'commands/kill'
|
7
8
|
require_relative 'commands/last'
|
@@ -13,6 +14,7 @@ require_relative 'commands/nproc'
|
|
13
14
|
require_relative 'commands/poweroff'
|
14
15
|
require_relative 'commands/ps'
|
15
16
|
require_relative 'commands/reboot'
|
17
|
+
require_relative 'commands/sysctl'
|
16
18
|
require_relative 'commands/uptime'
|
17
19
|
require_relative 'commands/w'
|
18
20
|
|
@@ -1,11 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative 'parsers/kernel_statistics'
|
4
|
+
require_relative 'parsers/history'
|
4
5
|
require_relative 'parsers/last'
|
5
6
|
require_relative 'parsers/load_average'
|
6
7
|
require_relative 'parsers/load_env'
|
7
8
|
require_relative 'parsers/lscpu'
|
8
9
|
require_relative 'parsers/lsof'
|
9
10
|
require_relative 'parsers/ps'
|
11
|
+
require_relative 'parsers/sysctl'
|
10
12
|
require_relative 'parsers/uptime'
|
11
13
|
require_relative 'parsers/w'
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Kanrisuru
|
4
|
+
module Core
|
5
|
+
module System
|
6
|
+
module Parser
|
7
|
+
class History
|
8
|
+
def self.parse(command)
|
9
|
+
lines = command.to_a
|
10
|
+
lines.map do |line|
|
11
|
+
line.split(/^\d+\s+/)[1]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -6,12 +6,11 @@ module Kanrisuru
|
|
6
6
|
module Parser
|
7
7
|
class LoadEnv
|
8
8
|
def self.parse(command)
|
9
|
-
|
9
|
+
rows = command.to_a
|
10
10
|
hash = {}
|
11
11
|
|
12
|
-
rows = string.split("\n")
|
13
12
|
rows.each do |row|
|
14
|
-
key, value = row.split('='
|
13
|
+
key, value = row.split('=')
|
15
14
|
hash[key] = value
|
16
15
|
end
|
17
16
|
|
@@ -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
|
data/lib/kanrisuru/result.rb
CHANGED
data/lib/kanrisuru/version.rb
CHANGED
@@ -43,6 +43,20 @@ RSpec.describe Kanrisuru::Core::System do
|
|
43
43
|
end.to raise_error(ArgumentError)
|
44
44
|
end
|
45
45
|
|
46
|
+
it 'prepares history command' do
|
47
|
+
expect_command(host.history, 'HISTFILE=~/.bash_history; history -r; history')
|
48
|
+
expect_command(host.history(histfile: '~/.history', n: 100),
|
49
|
+
'HISTFILE=~/.history; history -r; history 100')
|
50
|
+
expect_command(host.history(clear: true),
|
51
|
+
'HISTFILE=~/.bash_history; history -r; history -c')
|
52
|
+
expect_command(host.history(delete: 5),
|
53
|
+
'HISTFILE=~/.bash_history; history -r; history -d 5')
|
54
|
+
expect_command(host.history(delete: [23, 30]),
|
55
|
+
'HISTFILE=~/.bash_history; history -r; history -d 23 30')
|
56
|
+
expect_command(host.history(delete: '-7'),
|
57
|
+
'HISTFILE=~/.bash_history; history -r; history -d -7')
|
58
|
+
end
|
59
|
+
|
46
60
|
it 'prepares kernel_statistics command' do
|
47
61
|
expect_command(host.kernel_statistics, 'cat /proc/stat')
|
48
62
|
end
|
@@ -79,6 +93,11 @@ RSpec.describe Kanrisuru::Core::System do
|
|
79
93
|
expect_command(host.lsof, 'lsof -F pcuftDsin')
|
80
94
|
end
|
81
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
|
+
|
82
101
|
it 'prepares poweroff command' do
|
83
102
|
expect_command(host.poweroff, 'shutdown')
|
84
103
|
expect_command(host.poweroff(cancel: true), 'shutdown -c')
|
@@ -50,6 +50,12 @@ RSpec.shared_examples 'system' do |os_name, host_json, _spec_dir|
|
|
50
50
|
expect(result.days).to be >= 0
|
51
51
|
end
|
52
52
|
|
53
|
+
it 'gets history' do
|
54
|
+
result = host.history
|
55
|
+
expect(result).to be_success
|
56
|
+
expect(result.data).to be_instance_of(Array)
|
57
|
+
end
|
58
|
+
|
53
59
|
it 'kills pids' do
|
54
60
|
command = 'sleep 100000 > /dev/null 2>&1 &'
|
55
61
|
|
@@ -94,6 +100,20 @@ RSpec.shared_examples 'system' do |os_name, host_json, _spec_dir|
|
|
94
100
|
expect(result.cpus.length).to eq(host.cpu.cores)
|
95
101
|
end
|
96
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
|
+
|
97
117
|
it 'gets login details' do
|
98
118
|
host.su('root')
|
99
119
|
|
@@ -25,6 +25,7 @@ RSpec.describe Kanrisuru::Core::System do
|
|
25
25
|
expect(host).to respond_to(:lscpu)
|
26
26
|
expect(host).to respond_to(:load_average)
|
27
27
|
expect(host).to respond_to(:free)
|
28
|
+
expect(host).to respond_to(:history)
|
28
29
|
expect(host).to respond_to(:ps)
|
29
30
|
expect(host).to respond_to(:kill)
|
30
31
|
expect(host).to respond_to(:kernel_statistics)
|
@@ -32,6 +33,7 @@ RSpec.describe Kanrisuru::Core::System do
|
|
32
33
|
expect(host).to respond_to(:lsof)
|
33
34
|
expect(host).to respond_to(:nproc)
|
34
35
|
expect(host).to respond_to(:last)
|
36
|
+
expect(host).to respond_to(:sysctl)
|
35
37
|
expect(host).to respond_to(:uptime)
|
36
38
|
expect(host).to respond_to(:w)
|
37
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.
|
4
|
+
version: 0.19.2
|
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-
|
11
|
+
date: 2022-02-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: parallel_tests
|
@@ -307,6 +307,7 @@ files:
|
|
307
307
|
- lib/kanrisuru/core/system/commands.rb
|
308
308
|
- lib/kanrisuru/core/system/commands/cpu_info.rb
|
309
309
|
- lib/kanrisuru/core/system/commands/free.rb
|
310
|
+
- lib/kanrisuru/core/system/commands/history.rb
|
310
311
|
- lib/kanrisuru/core/system/commands/kernel_statistics.rb
|
311
312
|
- lib/kanrisuru/core/system/commands/kill.rb
|
312
313
|
- lib/kanrisuru/core/system/commands/last.rb
|
@@ -318,9 +319,11 @@ files:
|
|
318
319
|
- lib/kanrisuru/core/system/commands/poweroff.rb
|
319
320
|
- lib/kanrisuru/core/system/commands/ps.rb
|
320
321
|
- lib/kanrisuru/core/system/commands/reboot.rb
|
322
|
+
- lib/kanrisuru/core/system/commands/sysctl.rb
|
321
323
|
- lib/kanrisuru/core/system/commands/uptime.rb
|
322
324
|
- lib/kanrisuru/core/system/commands/w.rb
|
323
325
|
- lib/kanrisuru/core/system/parser.rb
|
326
|
+
- lib/kanrisuru/core/system/parsers/history.rb
|
324
327
|
- lib/kanrisuru/core/system/parsers/kernel_statistics.rb
|
325
328
|
- lib/kanrisuru/core/system/parsers/last.rb
|
326
329
|
- lib/kanrisuru/core/system/parsers/load_average.rb
|
@@ -328,6 +331,7 @@ files:
|
|
328
331
|
- lib/kanrisuru/core/system/parsers/lscpu.rb
|
329
332
|
- lib/kanrisuru/core/system/parsers/lsof.rb
|
330
333
|
- lib/kanrisuru/core/system/parsers/ps.rb
|
334
|
+
- lib/kanrisuru/core/system/parsers/sysctl.rb
|
331
335
|
- lib/kanrisuru/core/system/parsers/uptime.rb
|
332
336
|
- lib/kanrisuru/core/system/parsers/w.rb
|
333
337
|
- lib/kanrisuru/core/system/types.rb
|