kanrisuru 0.15.0 → 0.16.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +23 -0
  3. data/kanrisuru.gemspec +7 -3
  4. data/lib/kanrisuru/command.rb +6 -3
  5. data/lib/kanrisuru/core/disk/commands/lsblk.rb +6 -11
  6. data/lib/kanrisuru/core/disk/constants.rb +9 -0
  7. data/lib/kanrisuru/core/disk/parser.rb +1 -0
  8. data/lib/kanrisuru/core/disk/parsers/lsblk_version.rb +21 -0
  9. data/lib/kanrisuru/core/disk.rb +1 -0
  10. data/lib/kanrisuru/core/dmi/commands/dmi.rb +1 -1
  11. data/lib/kanrisuru/core/file/commands/chmod.rb +1 -1
  12. data/lib/kanrisuru/core/file/commands/copy.rb +1 -3
  13. data/lib/kanrisuru/core/file/commands/mkdir.rb +11 -6
  14. data/lib/kanrisuru/core/file/commands/rm.rb +4 -1
  15. data/lib/kanrisuru/core/file/commands/touch.rb +2 -1
  16. data/lib/kanrisuru/core/system/commands/kill.rb +1 -1
  17. data/lib/kanrisuru/core/user/commands/create_user.rb +9 -17
  18. data/lib/kanrisuru/core/user/commands/delete_user.rb +1 -1
  19. data/lib/kanrisuru/core/user/commands/update_user.rb +14 -23
  20. data/lib/kanrisuru/core/zypper/commands/add_repo.rb +1 -8
  21. data/lib/kanrisuru/core/zypper/commands/add_service.rb +4 -2
  22. data/lib/kanrisuru/core/zypper/commands/info.rb +1 -2
  23. data/lib/kanrisuru/core/zypper/commands/install.rb +2 -3
  24. data/lib/kanrisuru/core/zypper/commands/modify_repo.rb +1 -7
  25. data/lib/kanrisuru/core/zypper/commands/modify_service.rb +3 -1
  26. data/lib/kanrisuru/core/zypper/commands/remove.rb +1 -2
  27. data/lib/kanrisuru/core/zypper/commands/remove_repo.rb +3 -3
  28. data/lib/kanrisuru/core/zypper/commands/remove_service.rb +6 -1
  29. data/lib/kanrisuru/core/zypper/commands/search.rb +1 -3
  30. data/lib/kanrisuru/core/zypper/commands/source_install.rb +2 -0
  31. data/lib/kanrisuru/core/zypper/commands.rb +10 -1
  32. data/lib/kanrisuru/mode/permission.rb +103 -0
  33. data/lib/kanrisuru/mode.rb +2 -98
  34. data/lib/kanrisuru/os_package.rb +2 -0
  35. data/lib/kanrisuru/remote/host.rb +1 -3
  36. data/lib/kanrisuru/result.rb +15 -0
  37. data/lib/kanrisuru/version.rb +1 -1
  38. data/spec/functional/core/archive_spec.rb +1 -1
  39. data/spec/functional/core/disk_spec.rb +77 -0
  40. data/spec/functional/core/dmi_spec.rb +78 -0
  41. data/spec/functional/core/file_spec.rb +284 -0
  42. data/spec/functional/core/group_spec.rb +62 -0
  43. data/spec/functional/core/{ip/ip_address_label_spec.rb → ip_address_label_spec.rb} +0 -0
  44. data/spec/functional/core/{ip/ip_address_spec.rb → ip_address_spec.rb} +0 -0
  45. data/spec/functional/core/{ip/ip_link_spec.rb → ip_link_spec.rb} +0 -0
  46. data/spec/functional/core/{ip/ip_maddress_spec.rb → ip_maddress_spec.rb} +0 -0
  47. data/spec/functional/core/{ip/ip_neighbour_spec.rb → ip_neighbour_spec.rb} +0 -0
  48. data/spec/functional/core/{ip/ip_route_spec.rb → ip_route_spec.rb} +0 -0
  49. data/spec/functional/core/{ip/ip_rule_spec.rb → ip_rule_spec.rb} +0 -0
  50. data/spec/functional/core/{ip/ip_spec.rb → ip_spec.rb} +0 -0
  51. data/spec/functional/core/system_spec.rb +135 -0
  52. data/spec/functional/core/user_spec.rb +97 -0
  53. data/spec/functional/core/zypper_spec.rb +708 -0
  54. data/spec/functional/result_spec.rb +91 -44
  55. data/spec/helper/stub_network.rb +7 -3
  56. data/spec/unit/command_spec.rb +2 -0
  57. data/spec/unit/core/ip_spec.rb +12 -0
  58. metadata +23 -12
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kanrisuru
4
+ class Mode
5
+ class Permission
6
+ attr_reader :symbolic
7
+
8
+ def initialize(numeric, symbolic)
9
+ @numeric = numeric
10
+ @symbolic = symbolic
11
+
12
+ update_symbolic_rwx
13
+ end
14
+
15
+ def to_i
16
+ numeric.to_i
17
+ end
18
+
19
+ def to_s
20
+ symbolic
21
+ end
22
+
23
+ def numeric
24
+ @numeric.to_s
25
+ end
26
+
27
+ def all?
28
+ read? && write? && execute?
29
+ end
30
+
31
+ def symbolic=(symbolic)
32
+ @symbolic = symbolic
33
+
34
+ update_symbolic_rwx
35
+ update_numeric
36
+ end
37
+
38
+ def numeric=(numeric)
39
+ @numeric = numeric
40
+
41
+ update_numeric_rwx
42
+ update_symbolic
43
+ end
44
+
45
+ def read=(boolean)
46
+ @readable = boolean
47
+
48
+ update_numeric
49
+ update_symbolic
50
+ end
51
+
52
+ def write=(boolean)
53
+ @writeable = boolean
54
+
55
+ update_numeric
56
+ update_symbolic
57
+ end
58
+
59
+ def execute=(boolean)
60
+ @executable = boolean
61
+
62
+ update_numeric
63
+ update_symbolic
64
+ end
65
+
66
+ def read?
67
+ @readable
68
+ end
69
+
70
+ def write?
71
+ @writeable
72
+ end
73
+
74
+ def execute?
75
+ @executable
76
+ end
77
+
78
+ private
79
+
80
+ def update_symbolic_rwx
81
+ @readable = @symbolic.include?('r')
82
+ @writeable = @symbolic.include?('w')
83
+ @executable = @symbolic.include?('x')
84
+ end
85
+
86
+ def update_numeric_rwx
87
+ mode = @numeric.to_i(8)
88
+
89
+ @readable = ((mode >> 2) & 0b001) == 1
90
+ @writeable = ((mode >> 1) & 0b001) == 1
91
+ @executable = ((mode >> 0) & 0b001) == 1
92
+ end
93
+
94
+ def update_numeric
95
+ @numeric = (((read? ? 1 : 0) << 2) + ((write? ? 1 : 0) << 1) + (execute? ? 1 : 0)).to_s
96
+ end
97
+
98
+ def update_symbolic
99
+ @symbolic = "#{read? ? 'r' : '-'}#{write? ? 'w' : '-'}#{execute? ? 'x' : '-'}"
100
+ end
101
+ end
102
+ end
103
+ end
@@ -1,105 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'mode/permission'
4
+
3
5
  module Kanrisuru
4
6
  class Mode
5
- class Permission
6
- attr_reader :symbolic
7
-
8
- def initialize(numeric, symbolic)
9
- @numeric = numeric
10
- @symbolic = symbolic
11
-
12
- update_symbolic_rwx
13
- end
14
-
15
- def to_i
16
- numeric.to_i
17
- end
18
-
19
- def to_s
20
- symbolic
21
- end
22
-
23
- def numeric
24
- @numeric.to_s
25
- end
26
-
27
- def all?
28
- read? && write? && execute?
29
- end
30
-
31
- def symbolic=(symbolic)
32
- @symbolic = symbolic
33
-
34
- update_symbolic_rwx
35
- update_numeric
36
- end
37
-
38
- def numeric=(numeric)
39
- @numeric = numeric
40
-
41
- update_numeric_rwx
42
- update_symbolic
43
- end
44
-
45
- def read=(boolean)
46
- @readable = boolean
47
-
48
- update_numeric
49
- update_symbolic
50
- end
51
-
52
- def write=(boolean)
53
- @writeable = boolean
54
-
55
- update_numeric
56
- update_symbolic
57
- end
58
-
59
- def execute=(boolean)
60
- @executable = boolean
61
-
62
- update_numeric
63
- update_symbolic
64
- end
65
-
66
- def read?
67
- @readable
68
- end
69
-
70
- def write?
71
- @writeable
72
- end
73
-
74
- def execute?
75
- @executable
76
- end
77
-
78
- private
79
-
80
- def update_symbolic_rwx
81
- @readable = @symbolic.include?('r')
82
- @writeable = @symbolic.include?('w')
83
- @executable = @symbolic.include?('x')
84
- end
85
-
86
- def update_numeric_rwx
87
- mode = @numeric.to_i(8)
88
-
89
- @readable = ((mode >> 2) & 0b001) == 1
90
- @writeable = ((mode >> 1) & 0b001) == 1
91
- @executable = ((mode >> 0) & 0b001) == 1
92
- end
93
-
94
- def update_numeric
95
- @numeric = (((read? ? 1 : 0) << 2) + ((write? ? 1 : 0) << 1) + (execute? ? 1 : 0)).to_s
96
- end
97
-
98
- def update_symbolic
99
- @symbolic = "#{read? ? 'r' : '-'}#{write? ? 'w' : '-'}#{execute? ? 'x' : '-'}"
100
- end
101
- end
102
-
103
7
  attr_reader :owner, :group, :other
104
8
 
105
9
  def initialize(mode)
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'set'
4
+
3
5
  require_relative 'os_package/collection'
4
6
  require_relative 'os_package/define'
5
7
  require_relative 'os_package/include'
@@ -135,6 +135,7 @@ module Kanrisuru
135
135
  end
136
136
 
137
137
  ch.on_data do |_, data|
138
+ Kanrisuru.logger.debug { data.strip }
138
139
  command.handle_data(data)
139
140
  end
140
141
 
@@ -145,9 +146,6 @@ module Kanrisuru
145
146
  end
146
147
 
147
148
  channel.wait
148
-
149
- Kanrisuru.logger.debug { command.to_a }
150
-
151
149
  command
152
150
  rescue Net::SSH::ConnectionTimeout, Net::SSH::Timeout => e
153
151
  if retry_attempts > 1
@@ -39,6 +39,21 @@ module Kanrisuru
39
39
  @data.instance_of?(Array) ? @data : [@data]
40
40
  end
41
41
 
42
+ def to_f
43
+ case @data
44
+ when Numeric
45
+ @data
46
+ when String
47
+ @data.to_f
48
+ when Array
49
+ @data.map(&:to_f)
50
+ when NilClass
51
+ nil
52
+ else
53
+ raise NoMethodError, "(undefined method `to_f' for Kanrisuru::Result)"
54
+ end
55
+ end
56
+
42
57
  def to_i
43
58
  case @data
44
59
  when Integer
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kanrisuru
4
- VERSION = '0.15.0'
4
+ VERSION = '0.16.3'
5
5
  end
@@ -5,7 +5,7 @@ require 'spec_helper'
5
5
  RSpec.describe Kanrisuru::Core::Archive do
6
6
  before(:all) do
7
7
  StubNetwork.stub!
8
- StubNetwork.stub_command!(:realpath) do |_args|
8
+ StubNetwork.stub_command!(:realpath) do
9
9
  Kanrisuru::Core::Path::FilePath.new(directory)
10
10
  end
11
11
  end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe Kanrisuru::Core::Disk do
6
+ before(:all) do
7
+ StubNetwork.stub!
8
+ end
9
+
10
+ after(:all) do
11
+ StubNetwork.unstub!
12
+ end
13
+
14
+ let(:host) do
15
+ Kanrisuru::Remote::Host.new(
16
+ host: 'localhost',
17
+ username: 'ubuntu',
18
+ keys: ['id_rsa']
19
+ )
20
+ end
21
+
22
+ it 'prepares blkid command' do
23
+ expect_command(host.blkid, 'blkid -o export')
24
+ expect_command(host.blkid(device: '/dev/vda1'), 'blkid -pio export /dev/vda1')
25
+ expect_command(host.blkid(label: 'UEFI'), 'blkid -L UEFI')
26
+ expect_command(host.blkid(uuid: '26F2-56F9'), 'blkid -U 26F2-56F9')
27
+ expect_command(host.blkid(label: 'UEFI', uuid: '26F2-56F9'), 'blkid -L UEFI -U 26F2-56F9')
28
+ end
29
+
30
+ it 'prepares df command' do
31
+ expect_command(host.df, "df -PT | awk '{print $1, $2, $3, $5, $6, $7}'")
32
+ expect_command(host.df(inodes: true, path: '/dev/vda1'),
33
+ "df -PT -i /dev/vda1 | awk '{print $1, $2, $3, $5, $6, $7}'")
34
+ end
35
+
36
+ it 'prepares du command' do
37
+ expect_command(host.du, "du | awk '{print \\$1, \\$2}'")
38
+ expect_command(host.du(summarize: true, path: '/etc'), "du -s /etc | awk '{print \\$1, \\$2}'")
39
+ end
40
+
41
+ context 'with json support' do
42
+ before(:all) do
43
+ StubNetwork.stub_command!(:lsblk_version) do
44
+ Kanrisuru::Core::Disk::LSBK_VERSION
45
+ end
46
+ end
47
+
48
+ after(:all) do
49
+ StubNetwork.unstub_command!(:lsblk_version)
50
+ end
51
+
52
+ it 'prepares lsblk command' do
53
+ expect_command(host.lsblk, 'lsblk --json -o NAME,FSTYPE,MAJ:MIN,MOUNTPOINT,SIZE,UUID,RO,RM,OWNER,GROUP,MODE,TYPE')
54
+ expect_command(host.lsblk(all: true, paths: true, nodeps: true),
55
+ 'lsblk --json -a -p -d -o NAME,FSTYPE,MAJ:MIN,MOUNTPOINT,SIZE,UUID,RO,RM,OWNER,GROUP,MODE,TYPE')
56
+ end
57
+ end
58
+
59
+ context 'without json support' do
60
+ before(:all) do
61
+ StubNetwork.stub_command!(:lsblk_version) do
62
+ Kanrisuru::Core::Disk::LSBK_VERSION - 0.1
63
+ end
64
+ end
65
+
66
+ after(:all) do
67
+ StubNetwork.unstub_command!(:lsblk_version)
68
+ end
69
+
70
+ it 'prepares lsblk command' do
71
+ expect_command(host.lsblk,
72
+ 'lsblk -i -P --noheadings -o NAME,FSTYPE,MAJ:MIN,MOUNTPOINT,SIZE,UUID,RO,RM,OWNER,GROUP,MODE,TYPE')
73
+ expect_command(host.lsblk(all: true, paths: true, nodeps: true),
74
+ 'lsblk -i -P --noheadings -a -p -d -o NAME,FSTYPE,MAJ:MIN,MOUNTPOINT,SIZE,UUID,RO,RM,OWNER,GROUP,MODE,TYPE')
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe Kanrisuru::Core::Dmi do
6
+ before(:all) do
7
+ StubNetwork.stub!
8
+ end
9
+
10
+ after(:all) do
11
+ StubNetwork.unstub!
12
+ end
13
+
14
+ let(:host) do
15
+ Kanrisuru::Remote::Host.new(
16
+ host: 'localhost',
17
+ username: 'ubuntu',
18
+ keys: ['id_rsa']
19
+ )
20
+ end
21
+
22
+ it 'prepares dmi command' do
23
+ expect_command(host.dmi, 'dmidecode')
24
+ expect_command(host.dmi(types: 'BIOS'), 'dmidecode --type 0')
25
+ expect_command(host.dmi(types: 1), 'dmidecode --type 1')
26
+
27
+ expect do
28
+ host.dmi(types: 'hello')
29
+ end.to raise_error(ArgumentError)
30
+
31
+ expect_command(host.dmi(types: [
32
+ 'BIOS',
33
+ 'System',
34
+ 'Baseboard',
35
+ 'Chassis',
36
+ 'Processor',
37
+ 'Memory Controller',
38
+ 'Memory Module',
39
+ 'Cache',
40
+ 'Port Connector',
41
+ 'System Slots',
42
+ 'On Board Devices',
43
+ 'OEM Strings',
44
+ 'System Configuration Options',
45
+ 'BIOS Language',
46
+ 'Group Associations',
47
+ 'System Event Log',
48
+ 'Physical Memory Array',
49
+ 'Memory Device',
50
+ '32-bit Memory Error',
51
+ 'Memory Array Mapped Address',
52
+ 'Memory Device Mapped Address',
53
+ 'Built-in Pointing Device',
54
+ 'Portable Battery',
55
+ 'System Reset',
56
+ 'Hardware Security',
57
+ 'System Power Controls',
58
+ 'Voltage Probe',
59
+ 'Cooling Device',
60
+ 'Temperature Probe',
61
+ 'Electrical Current Probe',
62
+ 'Out-of-band Remote Access',
63
+ 'Boot Integrity Services',
64
+ 'System Boot',
65
+ '64-bit Memory Error',
66
+ 'Management Device',
67
+ 'Management Device Component',
68
+ 'Management Device Threshold Data',
69
+ 'Memory Channel',
70
+ 'IPMI Device',
71
+ 'System Power Supply',
72
+ 'Additional Information',
73
+ 'Onboard Devices Extended Information',
74
+ 'Management Controller Host Interface',
75
+ 'TPM Device'
76
+ ]), 'dmidecode --type 0 --type 1 --type 2 --type 3 --type 4 --type 5 --type 6 --type 7 --type 8 --type 9 --type 10 --type 11 --type 12 --type 13 --type 14 --type 15 --type 16 --type 17 --type 18 --type 19 --type 20 --type 21 --type 22 --type 23 --type 24 --type 25 --type 26 --type 27 --type 28 --type 29 --type 30 --type 31 --type 32 --type 33 --type 34 --type 35 --type 36 --type 37 --type 38 --type 39 --type 40 --type 41 --type 42 --type 43')
77
+ end
78
+ end