kanrisuru 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +17 -0
  3. data/.rubocop.yml +47 -0
  4. data/.rubocop_todo.yml +0 -0
  5. data/CHANGELOG.md +0 -0
  6. data/Gemfile +2 -5
  7. data/LICENSE.txt +1 -1
  8. data/README.md +143 -7
  9. data/Rakefile +5 -3
  10. data/bin/console +4 -3
  11. data/kanrisuru.gemspec +21 -12
  12. data/lib/kanrisuru.rb +41 -2
  13. data/lib/kanrisuru/command.rb +99 -0
  14. data/lib/kanrisuru/core.rb +53 -0
  15. data/lib/kanrisuru/core/archive.rb +154 -0
  16. data/lib/kanrisuru/core/disk.rb +302 -0
  17. data/lib/kanrisuru/core/file.rb +332 -0
  18. data/lib/kanrisuru/core/find.rb +108 -0
  19. data/lib/kanrisuru/core/group.rb +97 -0
  20. data/lib/kanrisuru/core/ip.rb +1032 -0
  21. data/lib/kanrisuru/core/mount.rb +138 -0
  22. data/lib/kanrisuru/core/path.rb +140 -0
  23. data/lib/kanrisuru/core/socket.rb +168 -0
  24. data/lib/kanrisuru/core/stat.rb +104 -0
  25. data/lib/kanrisuru/core/stream.rb +121 -0
  26. data/lib/kanrisuru/core/system.rb +348 -0
  27. data/lib/kanrisuru/core/transfer.rb +203 -0
  28. data/lib/kanrisuru/core/user.rb +198 -0
  29. data/lib/kanrisuru/logger.rb +8 -0
  30. data/lib/kanrisuru/mode.rb +277 -0
  31. data/lib/kanrisuru/os_package.rb +235 -0
  32. data/lib/kanrisuru/remote.rb +10 -0
  33. data/lib/kanrisuru/remote/cluster.rb +95 -0
  34. data/lib/kanrisuru/remote/cpu.rb +68 -0
  35. data/lib/kanrisuru/remote/env.rb +33 -0
  36. data/lib/kanrisuru/remote/file.rb +354 -0
  37. data/lib/kanrisuru/remote/fstab.rb +412 -0
  38. data/lib/kanrisuru/remote/host.rb +191 -0
  39. data/lib/kanrisuru/remote/memory.rb +19 -0
  40. data/lib/kanrisuru/remote/os.rb +87 -0
  41. data/lib/kanrisuru/result.rb +78 -0
  42. data/lib/kanrisuru/template.rb +32 -0
  43. data/lib/kanrisuru/util.rb +40 -0
  44. data/lib/kanrisuru/util/bits.rb +203 -0
  45. data/lib/kanrisuru/util/fs_mount_opts.rb +655 -0
  46. data/lib/kanrisuru/util/os_family.rb +213 -0
  47. data/lib/kanrisuru/util/signal.rb +161 -0
  48. data/lib/kanrisuru/version.rb +3 -1
  49. data/spec/functional/core/archive_spec.rb +228 -0
  50. data/spec/functional/core/disk_spec.rb +80 -0
  51. data/spec/functional/core/file_spec.rb +341 -0
  52. data/spec/functional/core/find_spec.rb +52 -0
  53. data/spec/functional/core/group_spec.rb +65 -0
  54. data/spec/functional/core/ip_spec.rb +71 -0
  55. data/spec/functional/core/path_spec.rb +93 -0
  56. data/spec/functional/core/socket_spec.rb +31 -0
  57. data/spec/functional/core/stat_spec.rb +98 -0
  58. data/spec/functional/core/stream_spec.rb +99 -0
  59. data/spec/functional/core/system_spec.rb +96 -0
  60. data/spec/functional/core/transfer_spec.rb +108 -0
  61. data/spec/functional/core/user_spec.rb +76 -0
  62. data/spec/functional/os_package_spec.rb +75 -0
  63. data/spec/functional/remote/cluster_spec.rb +45 -0
  64. data/spec/functional/remote/cpu_spec.rb +41 -0
  65. data/spec/functional/remote/env_spec.rb +36 -0
  66. data/spec/functional/remote/fstab_spec.rb +76 -0
  67. data/spec/functional/remote/host_spec.rb +68 -0
  68. data/spec/functional/remote/memory_spec.rb +29 -0
  69. data/spec/functional/remote/os_spec.rb +63 -0
  70. data/spec/functional/remote/remote_file_spec.rb +180 -0
  71. data/spec/helper/test_hosts.rb +68 -0
  72. data/spec/hosts.json +92 -0
  73. data/spec/spec_helper.rb +11 -3
  74. data/spec/unit/fstab_spec.rb +22 -0
  75. data/spec/unit/kanrisuru_spec.rb +9 -0
  76. data/spec/unit/mode_spec.rb +183 -0
  77. data/spec/unit/template_spec.rb +13 -0
  78. data/spec/unit/util_spec.rb +177 -0
  79. data/spec/zz_reboot_spec.rb +46 -0
  80. metadata +136 -13
  81. data/spec/kanrisuru_spec.rb +0 -9
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe Kanrisuru::Core::Group do
6
+ TestHosts.each_os do |os_name|
7
+ context "with #{os_name}" do
8
+ let(:host_json) { TestHosts.host(os_name) }
9
+ let(:host) do
10
+ Kanrisuru::Remote::Host.new(
11
+ host: host_json['hostname'],
12
+ username: host_json['username'],
13
+ keys: [host_json['ssh_key']]
14
+ )
15
+ end
16
+
17
+ after do
18
+ host.disconnect
19
+ end
20
+
21
+ it 'gets gid for group' do
22
+ case os_name
23
+ when 'opensuse', 'sles'
24
+ expect(host.get_gid('users').to_i).to eq(100)
25
+ else
26
+ expect(host.get_gid(host_json['username']).to_i).to eq(1000)
27
+ end
28
+
29
+ expect(host.get_gid('asdf').to_i).to eq(nil)
30
+ end
31
+
32
+ it 'gets group details' do
33
+ case os_name
34
+ when 'opensuse', 'sles'
35
+ result = host.get_group('users')
36
+ expect(result.success?).to eq(true)
37
+ expect(result.gid).to eq(100)
38
+ expect(result.name).to eq('users')
39
+ else
40
+ result = host.get_group(host_json['username'])
41
+ expect(result.success?).to eq(true)
42
+ expect(result.gid).to eq(1000)
43
+ expect(result.name).to eq(host_json['username'])
44
+ end
45
+
46
+ expect(result).to respond_to(:users)
47
+ end
48
+
49
+ it 'manages a group' do
50
+ ## Need priviledge escalation to manage group
51
+ host.su('root')
52
+
53
+ if host.group?('rspec')
54
+ expect(host.delete_group('rspec').success?).to eq(true)
55
+ elsif host.group?('minitest')
56
+ expect(host.delete_group('minitest').success?).to eq(true)
57
+ end
58
+
59
+ expect(host.create_group('rspec').gid).to be >= 1000
60
+ expect(host.update_group('rspec', gid: 1005, new_name: 'minitest').gid).to eq(1005)
61
+ expect(host.delete_group('minitest').success?).to eq(true)
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe Kanrisuru::Core::IP do
6
+ TestHosts.each_os do |os_name|
7
+ context "with #{os_name}" do
8
+ let(:host_json) { TestHosts.host(os_name) }
9
+ let(:host) do
10
+ Kanrisuru::Remote::Host.new(
11
+ host: host_json['hostname'],
12
+ username: host_json['username'],
13
+ keys: [host_json['ssh_key']]
14
+ )
15
+ end
16
+
17
+ after do
18
+ host.disconnect
19
+ end
20
+
21
+ describe 'ip address' do
22
+ it 'show' do
23
+ result = host.ip('address', 'show', stats: true)
24
+ expect(result).to be_success
25
+ end
26
+ end
27
+
28
+ describe 'ip link' do
29
+ it 'show' do
30
+ result = host.ip('link', 'show', stats: true)
31
+ expect(result).to be_success
32
+ end
33
+ end
34
+
35
+ describe 'ip addrlabel' do
36
+ it 'list' do
37
+ result = host.ip('addrlabel', 'list')
38
+ expect(result).to be_success
39
+ end
40
+ end
41
+
42
+ describe 'ip route' do
43
+ it 'show' do
44
+ result = host.ip('route', 'show')
45
+ expect(result).to be_success
46
+ end
47
+ end
48
+
49
+ describe 'ip rule' do
50
+ it 'show' do
51
+ result = host.ip('rule', 'show')
52
+ expect(result).to be_success
53
+ end
54
+ end
55
+
56
+ describe 'ip neighbour' do
57
+ it 'show' do
58
+ result = host.ip('neighbour', 'show', stats: true)
59
+ expect(result).to be_success
60
+ end
61
+ end
62
+
63
+ describe 'ip maddress' do
64
+ it 'show' do
65
+ result = host.ip('maddress', 'show')
66
+ expect(result).to be_success
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe Kanrisuru::Core::Path do
6
+ TestHosts.each_os do |os_name|
7
+ context "with #{os_name}" do
8
+ let(:host_json) { TestHosts.host(os_name) }
9
+ let(:host) do
10
+ Kanrisuru::Remote::Host.new(
11
+ host: host_json['hostname'],
12
+ username: host_json['username'],
13
+ keys: [host_json['ssh_key']]
14
+ )
15
+ end
16
+
17
+ after do
18
+ host.disconnect
19
+ end
20
+
21
+ it 'lists files and directories' do
22
+ result = host.ls(all: true)
23
+ expect(result.data).to be_instance_of(Array)
24
+
25
+ dir = result.find { |file| file.path == '.' }
26
+ expect(dir.path).to eq('.')
27
+
28
+ result = host.ls(path: host_json['home'], id: true, all: true)
29
+ expect(result.data).to be_instance_of(Array)
30
+
31
+ file = result.find { |f| f.path == '.bashrc' }
32
+ expect(file.uid).to eq(1000)
33
+
34
+ case os_name
35
+ when 'opensuse', 'sles'
36
+ expect(file.gid).to eq(100)
37
+ else
38
+ expect(file.gid).to eq(1000)
39
+ end
40
+ end
41
+
42
+ it 'gets whoami' do
43
+ expect(host.whoami.user).to eq(host_json['username'])
44
+ end
45
+
46
+ it 'gets pwd' do
47
+ expect(host.pwd.path).to eq(host_json['home'])
48
+ end
49
+
50
+ it 'uses which to get path for bash' do
51
+ result = host.which('bash', all: true)
52
+ paths = result.map(&:path)
53
+
54
+ case os_name
55
+ when 'ubuntu'
56
+ if host.os.version <= 18.04
57
+ expect(paths).to include('/bin/bash')
58
+ else
59
+ expect(paths).to include('/usr/bin/bash', '/bin/bash')
60
+ end
61
+ when 'opensuse'
62
+ ## Ignore for local testing
63
+ when 'sles'
64
+ expect(paths).to include('/bin/bash')
65
+ else
66
+ expect(paths).to include('/usr/bin/bash', '/bin/bash')
67
+ end
68
+ end
69
+
70
+ it 'gets realpath for dir' do
71
+ case os_name
72
+ when 'sles'
73
+ expect(host.realpath('/var/run').path).to eq('/run')
74
+ when 'rhel'
75
+ expect(host.realpath('/bin').path).to eq('/usr/bin')
76
+ else
77
+ expect(host.realpath('/etc/os-release').path).to eq('/usr/lib/os-release')
78
+ end
79
+ end
80
+
81
+ it 'gets full path for dir with readlink' do
82
+ case os_name
83
+ when 'sles'
84
+ expect(host.readlink('/var/run', canonicalize: true).path).to eq('/run')
85
+ when 'rhel'
86
+ expect(host.readlink('/bin', canonicalize: true).path).to eq('/usr/bin')
87
+ else
88
+ expect(host.readlink('/etc/os-release', canonicalize: true).path).to eq('/usr/lib/os-release')
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe Kanrisuru::Core::Socket do
6
+ TestHosts.each_os do |os_name|
7
+ context "with #{os_name}" do
8
+ let(:host_json) { TestHosts.host(os_name) }
9
+
10
+ let(:host) do
11
+ Kanrisuru::Remote::Host.new(
12
+ host: host_json['hostname'],
13
+ username: host_json['username'],
14
+ keys: [host_json['ssh_key']]
15
+ )
16
+ end
17
+
18
+ after do
19
+ host.disconnect
20
+ end
21
+
22
+ it 'gets socket details' do
23
+ result = host.ss
24
+ expect(result).to be_success
25
+
26
+ result = host.ss(state: 'listening')
27
+ expect(result).to be_success
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe Kanrisuru::Core::Stat do
6
+ TestHosts.each_os do |os_name|
7
+ context "with #{os_name}" do
8
+ let(:host_json) { TestHosts.host(os_name) }
9
+ let(:host) do
10
+ Kanrisuru::Remote::Host.new(
11
+ host: host_json['hostname'],
12
+ username: host_json['username'],
13
+ keys: [host_json['ssh_key']]
14
+ )
15
+ end
16
+
17
+ after do
18
+ host.disconnect
19
+ end
20
+
21
+ it 'checks file type correctly' do
22
+ expect(host.dir?('/')).to eq(true)
23
+ expect(host.file?('/etc/hosts')).to eq(true)
24
+
25
+ case os_name
26
+ when 'sles'
27
+ expect(host.block_device?('/dev/xvda')).to eq(true)
28
+ else
29
+ expect(host.block_device?('/dev/vda')).to eq(true)
30
+ end
31
+
32
+ expect(host.char_device?('/dev/tty')).to eq(true)
33
+ expect(host.symlink?('/etc/mtab')).to eq(true)
34
+ expect(host.inode?('/proc/uptime')).to eq(true)
35
+ end
36
+
37
+ it 'gets file stat' do
38
+ result = host.stat(host_json['home'])
39
+
40
+ expect(result.mode).to be_instance_of(Kanrisuru::Mode)
41
+ expect(result.mode.directory?).to eq(true)
42
+
43
+ case os_name
44
+ when 'centos', 'rhel', 'fedora'
45
+ expect(result.mode.numeric).to eq('700')
46
+ expect(result.mode.to_i).to eq(0o700)
47
+
48
+ expect(result.mode.group.read?).to eq(false)
49
+ expect(result.mode.group.write?).to eq(false)
50
+ expect(result.mode.group.execute?).to eq(false)
51
+
52
+ expect(result.mode.other.read?).to eq(false)
53
+ expect(result.mode.other.write?).to eq(false)
54
+ expect(result.mode.other.execute?).to eq(false)
55
+
56
+ expect(result.mode.owner.read?).to eq(true)
57
+ expect(result.mode.owner.write?).to eq(true)
58
+ expect(result.mode.owner.execute?).to eq(true)
59
+
60
+ expect(result.mode.symbolic).to eq('drwx------')
61
+ else
62
+ expect(result.mode.numeric).to eq('755')
63
+ expect(result.mode.to_i).to eq(0o755)
64
+
65
+ expect(result.mode.group.read?).to eq(true)
66
+ expect(result.mode.group.write?).to eq(false)
67
+ expect(result.mode.group.execute?).to eq(true)
68
+
69
+ expect(result.mode.other.read?).to eq(true)
70
+ expect(result.mode.other.write?).to eq(false)
71
+ expect(result.mode.other.execute?).to eq(true)
72
+
73
+ expect(result.mode.owner.read?).to eq(true)
74
+ expect(result.mode.owner.write?).to eq(true)
75
+ expect(result.mode.owner.execute?).to eq(true)
76
+
77
+ expect(result.mode.symbolic).to eq('drwxr-xr-x')
78
+ end
79
+
80
+ expect(result.file_type).to eq('directory')
81
+
82
+ case os_name
83
+ when 'opensuse', 'sles'
84
+ expect(result.gid).to eq(100)
85
+ expect(result.group).to eq('users')
86
+ else
87
+ expect(result.gid).to eq(1000)
88
+ expect(result.group).to eq(host_json['username'])
89
+ end
90
+
91
+ expect(result.uid).to eq(1000)
92
+ expect(result.user).to eq(host_json['username'])
93
+
94
+ expect(result.fsize).to be >= 0
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Kanrisuru::Core::Stream do
4
+ TestHosts.each_os do |os_name|
5
+ context "with #{os_name}" do
6
+ before(:all) do
7
+ host_json = TestHosts.host(os_name)
8
+ host = Kanrisuru::Remote::Host.new(
9
+ host: host_json['hostname'],
10
+ username: host_json['username'],
11
+ keys: [host_json['ssh_key']]
12
+ )
13
+
14
+ host.mkdir("#{host_json['home']}/.kanrisuru_spec_files", silent: true)
15
+ host.disconnect
16
+ end
17
+
18
+ let(:host_json) { TestHosts.host(os_name) }
19
+ let(:host) do
20
+ Kanrisuru::Remote::Host.new(
21
+ host: host_json['hostname'],
22
+ username: host_json['username'],
23
+ keys: [host_json['ssh_key']]
24
+ )
25
+ end
26
+
27
+ let(:spec_dir) { "#{host_json['home']}/.kanrisuru_spec_files" }
28
+
29
+ after do
30
+ host.disconnect
31
+ end
32
+
33
+ after(:all) do
34
+ host_json = TestHosts.host(os_name)
35
+ host = Kanrisuru::Remote::Host.new(
36
+ host: host_json['hostname'],
37
+ username: host_json['username'],
38
+ keys: [host_json['ssh_key']]
39
+ )
40
+
41
+ host.rmdir("#{host_json['home']}/.kanrisuru_spec_files")
42
+ host.disconnect
43
+ end
44
+
45
+ it 'outputs beginning of a file' do
46
+ file = host.file("#{spec_dir}/test-file.txt")
47
+ file.touch
48
+ file.append do |f|
49
+ f << 'This'
50
+ f << 'is'
51
+ f << 'a'
52
+ f << 'file!'
53
+ end
54
+
55
+ result = host.head("#{spec_dir}/test-file.txt", lines: 2)
56
+ expect(result).to be_success
57
+ expect(result.data.length).to eq(2)
58
+ expect(result.data).to eq(%w[This is])
59
+ end
60
+
61
+ it 'outputs end of a file' do
62
+ file = host.file("#{spec_dir}/test-file.txt")
63
+ file.touch
64
+ file.append do |f|
65
+ f << 'This'
66
+ f << 'is'
67
+ f << 'a'
68
+ f << 'file!'
69
+ end
70
+
71
+ result = host.tail("#{spec_dir}/test-file.txt", lines: 2)
72
+ expect(result).to be_success
73
+ expect(result.data.length).to eq(2)
74
+ expect(result.data).to eq(['a', 'file!'])
75
+ end
76
+
77
+ it 'cats a file' do
78
+ result = host.cat('/etc/group')
79
+ expect(result.success?).to eq(true)
80
+ expect(result.data.include?('root:x:0:')).to eq(true)
81
+ end
82
+
83
+ it 'echoes to stdout' do
84
+ result = host.echo('Hello world')
85
+ expect(result.data).to eq('Hello world')
86
+ end
87
+
88
+ it 'seds file to stdout' do
89
+ path = "#{spec_dir}/test-file.txt"
90
+ result = host.echo("Hello world, this is a Cat test file.\nCat\nCat\nDog", new_file: path, mode: 'write')
91
+ expect(result).to be_success
92
+
93
+ result = host.sed(path, 'Cat', 'Dog')
94
+ expect(result).to be_success
95
+ expect(result.data).to eq("Hello world, this is a Dog test file.\nDog\nDog\nDog")
96
+ end
97
+ end
98
+ end
99
+ end