kanrisuru 0.16.17 → 0.19.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e31b687d1a4fbd77c62a502ffe4e3ec6e7c314c9e8335fc1964e8fcaca75f554
4
- data.tar.gz: e36de3bb37966123d08c25aa6023be77f932705e49d005590f63a45d6a8c2d1b
3
+ metadata.gz: a2f4308d14c64ac5f78a9f459623671d0d91901b679570ab72466c5176c22035
4
+ data.tar.gz: 734a54c6d1d147bf4e2ae67be7b3569331849cf9cb2df458d15dbe3effcc9292
5
5
  SHA512:
6
- metadata.gz: fb5549794cf62990bd0026c315b5c9828884b814f28ea82a7e04f944d12d2d52a77131d72b64e1d2c3d9d9c525f3bd468d020d69f0539304818f78b8b070b5ef
7
- data.tar.gz: dd58f2d707f62615d4a7cc997dea5f27ca0a1678e6d60478f5548c4953685b469f61f8928c879b4965a56c51de4ec8ddb722980bd14cb9f1793355b001f60678
6
+ metadata.gz: bb982b8fa4cb1c313ec4111770498ee9ff2bd75676bc2a5c57da71b3ef5f3dc01a9c016848939ef92bb0b84ed990f64684d7825bc93febd4a7d5e8def44ec5a8
7
+ data.tar.gz: b97a3b67ef2b3be719d967adf2e3bf54ac583da368d96a95ea18d415961f46a5368768f022f160a4de3f7de6f9d2787f5c68b66e547e2c4c7ee1b931b2f3d9ec
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ## Kanrisuru 0.19.1 (February 02, 2022) ##
2
+ * Fix `load_env` command with parsing output, use `to_a` for cleaner newline parsing.
3
+
4
+ ## Kanrisuru 0.19.0 (January 25, 2022) ##
5
+ * Add readonly version of `sysctl` command with test cases.
6
+
7
+ ## Kanrisuru 0.18.0 (January 23, 2022) ##
8
+ * Add `history` command with test cases.
9
+
10
+ ## Kanrisuru 0.17.0 (January 20, 2022) ##
11
+ * Add `nproc` command with test cases.
12
+
1
13
  ## Kanrisuru 0.16.17 (January 10, 2022) ##
2
14
  * Add additional options to `wget` command.
3
15
  * Rename some options to better reflect options from wget program.
@@ -16,7 +16,7 @@ module Kanrisuru
16
16
 
17
17
  command.append_flag('-l', opts[:lazy])
18
18
  command.append_flag('-f', opts[:force])
19
-
19
+
20
20
  command.append_flag('--all-targets', opts[:all_targets])
21
21
  command.append_flag('--recursive', opts[:recursive])
22
22
 
@@ -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,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kanrisuru
4
+ module Core
5
+ module System
6
+ def nproc(opts = {})
7
+ command = Kanrisuru::Command.new('nproc')
8
+ command.append_flag('--all', opts[:all])
9
+ execute_shell(command)
10
+
11
+ Kanrisuru::Result.new(command, &:to_i)
12
+ end
13
+ end
14
+ end
15
+ 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'
@@ -9,9 +10,11 @@ require_relative 'commands/load_average'
9
10
  require_relative 'commands/load_env'
10
11
  require_relative 'commands/lscpu'
11
12
  require_relative 'commands/lsof'
13
+ require_relative 'commands/nproc'
12
14
  require_relative 'commands/poweroff'
13
15
  require_relative 'commands/ps'
14
16
  require_relative 'commands/reboot'
17
+ require_relative 'commands/sysctl'
15
18
  require_relative 'commands/uptime'
16
19
  require_relative 'commands/w'
17
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
- string = command.to_s
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('=', 2)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kanrisuru
4
- VERSION = '0.16.17'
4
+ VERSION = '0.19.1'
5
5
  end
@@ -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')
@@ -124,6 +143,11 @@ RSpec.describe Kanrisuru::Core::System do
124
143
  end.to raise_error(ArgumentError)
125
144
  end
126
145
 
146
+ it 'prepares nproc command' do
147
+ expect_command(host.nproc, 'nproc')
148
+ expect_command(host.nproc(all: true), 'nproc --all')
149
+ end
150
+
127
151
  it 'prepares uptime command' do
128
152
  expect_command(host.uptime, 'cat /proc/uptime')
129
153
  end
@@ -127,7 +127,7 @@ RSpec.describe Kanrisuru::Core::Stat do
127
127
  "wget --certificate ~/cert.pem --certificate-type PEM --private-key ~/key.pem --private-key-type PEM --ca-certificate ~/ca.pem --random-file ~/random --https-only #{url}")
128
128
 
129
129
  ## FTP
130
- expect_command(host.wget("ftp.example.com",
130
+ expect_command(host.wget('ftp.example.com',
131
131
  ftp_user: 'admin',
132
132
  ftp_password: '12345678',
133
133
  no_remove_listing: true,
@@ -135,15 +135,15 @@ RSpec.describe Kanrisuru::Core::Stat do
135
135
  no_passive_ftp: true,
136
136
  retr_symlinks: true,
137
137
  preserve_permissions: true),
138
- "wget --ftp-user admin --ftp-password 12345678 --no-remove-listing --no-glob --no-passive-ftp --retr-symlinks --preserve-permissions ftp.example.com")
138
+ 'wget --ftp-user admin --ftp-password 12345678 --no-remove-listing --no-glob --no-passive-ftp --retr-symlinks --preserve-permissions ftp.example.com')
139
139
 
140
- expect_command(host.wget("ftps.example.com",
140
+ expect_command(host.wget('ftps.example.com',
141
141
  ftp_user: 'admin',
142
142
  ftp_password: '12345678',
143
143
  ftps_implicit: true,
144
144
  no_ftps_resume_ssl: true,
145
145
  ftps_fallback_to_ftp: true,
146
- ftps_clear_data_connection: true), "wget --ftp-user admin --ftp-password 12345678 --ftps-implicit --no-ftps-resume-ssl --ftps-clear-data-connection --ftps-fallback-to-ftp ftps.example.com")
146
+ ftps_clear_data_connection: true), 'wget --ftp-user admin --ftp-password 12345678 --ftps-implicit --no-ftps-resume-ssl --ftps-clear-data-connection --ftps-fallback-to-ftp ftps.example.com')
147
147
 
148
148
  ## Recursive Retrieval
149
149
  expect_command(host.wget(url,
@@ -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
 
@@ -108,6 +128,12 @@ RSpec.shared_examples 'system' do |os_name, host_json, _spec_dir|
108
128
  expect(result).to be_success
109
129
  end
110
130
 
131
+ it 'gets nproc count' do
132
+ result = host.nproc
133
+ expect(result).to be_success
134
+ expect(result.to_i).to be > 0
135
+ end
136
+
111
137
  it 'gets process details' do
112
138
  result = host.ps
113
139
 
@@ -25,12 +25,15 @@ 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)
31
32
  expect(host).to respond_to(:kstat)
32
33
  expect(host).to respond_to(:lsof)
34
+ expect(host).to respond_to(:nproc)
33
35
  expect(host).to respond_to(:last)
36
+ expect(host).to respond_to(:sysctl)
34
37
  expect(host).to respond_to(:uptime)
35
38
  expect(host).to respond_to(:w)
36
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.16.17
4
+ version: 0.19.1
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-11 00:00:00.000000000 Z
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
@@ -314,12 +315,15 @@ files:
314
315
  - lib/kanrisuru/core/system/commands/load_env.rb
315
316
  - lib/kanrisuru/core/system/commands/lscpu.rb
316
317
  - lib/kanrisuru/core/system/commands/lsof.rb
318
+ - lib/kanrisuru/core/system/commands/nproc.rb
317
319
  - lib/kanrisuru/core/system/commands/poweroff.rb
318
320
  - lib/kanrisuru/core/system/commands/ps.rb
319
321
  - lib/kanrisuru/core/system/commands/reboot.rb
322
+ - lib/kanrisuru/core/system/commands/sysctl.rb
320
323
  - lib/kanrisuru/core/system/commands/uptime.rb
321
324
  - lib/kanrisuru/core/system/commands/w.rb
322
325
  - lib/kanrisuru/core/system/parser.rb
326
+ - lib/kanrisuru/core/system/parsers/history.rb
323
327
  - lib/kanrisuru/core/system/parsers/kernel_statistics.rb
324
328
  - lib/kanrisuru/core/system/parsers/last.rb
325
329
  - lib/kanrisuru/core/system/parsers/load_average.rb
@@ -327,6 +331,7 @@ files:
327
331
  - lib/kanrisuru/core/system/parsers/lscpu.rb
328
332
  - lib/kanrisuru/core/system/parsers/lsof.rb
329
333
  - lib/kanrisuru/core/system/parsers/ps.rb
334
+ - lib/kanrisuru/core/system/parsers/sysctl.rb
330
335
  - lib/kanrisuru/core/system/parsers/uptime.rb
331
336
  - lib/kanrisuru/core/system/parsers/w.rb
332
337
  - lib/kanrisuru/core/system/types.rb
@@ -706,7 +711,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
706
711
  - !ruby/object:Gem::Version
707
712
  version: '0'
708
713
  requirements: []
709
- rubygems_version: 3.2.32
714
+ rubygems_version: 3.3.5
710
715
  signing_key:
711
716
  specification_version: 4
712
717
  summary: Manage remote servers over ssh with ruby.