kanrisuru 0.14.0 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,109 +6,146 @@ module Kanrisuru
6
6
  def ip_route(action, opts)
7
7
  case action
8
8
  when 'show', 'list'
9
- version = ip_version
10
-
11
- command = Kanrisuru::Command.new('ip')
12
- command.append_flag('-json') if version >= IPROUTE2_JSON_VERSION
13
- command.append_arg('-family', opts[:family])
14
- command << 'route show'
15
-
16
- command.append_arg('to', opts[:to])
17
- command.append_arg('dev', opts[:dev])
18
- command.append_arg('proto', opts[:proto])
19
- command.append_arg('type', opts[:type])
20
- command.append_arg('via', opts[:via])
21
- command.append_arg('src', opts[:src])
22
- command.append_arg('realms', opts[:realms])
9
+ version = ip_version.to_i
10
+ command = ip_route_show(opts, version)
23
11
  when 'flush'
24
- command = Kanrisuru::Command.new('ip')
25
- command.append_arg('-family', opts[:family])
26
- command << 'route flush'
27
-
28
- command.append_arg('to', opts[:to])
29
- command.append_arg('dev', opts[:dev])
30
- command.append_arg('proto', opts[:proto])
31
- command.append_arg('type', opts[:type])
32
- command.append_arg('via', opts[:via])
33
- command.append_arg('src', opts[:src])
34
- command.append_arg('realm', opts[:realm])
35
- command.append_arg('realms', opts[:realm])
36
- when 'add', 'change', 'append', 'del', 'delete'
37
- command = Kanrisuru::Command.new('ip route')
38
- command << action
39
-
40
- command.append_arg('to', opts[:to])
41
- command.append_arg('tos', opts[:tos])
42
- command.append_arg('dsfield', opts[:dsfield])
43
- command.append_arg('metric', opts[:metric])
44
- command.append_arg('preference', opts[:preference])
45
- command.append_arg('table', opts[:table])
46
- command.append_arg('vrf', opts[:vrf])
47
- command.append_arg('dev', opts[:dev])
48
- command.append_arg('via', opts[:via])
49
- command.append_arg('src', opts[:src])
50
- command.append_arg('realm', opts[:realm])
51
-
52
- command.append_arg('mtu', opts[:mtu])
53
- command.append_arg('window', opts[:window])
54
- command.append_arg('rtt', opts[:rtt])
55
- command.append_arg('rttvar', opts[:rttvar])
56
- command.append_arg('rto_min', opts[:rto_min])
57
- command.append_arg('ssthresh', opts[:ssthresh])
58
- command.append_arg('cwnd', opts[:cwnd])
59
- command.append_arg('initcwnd', opts[:initcwnd])
60
- command.append_arg('initrwnd', opts[:initrwnd])
61
- command.append_arg('features', opts[:features])
62
- command.append_arg('quickack', opts[:quickack])
63
- command.append_arg('fastopen_no_cookie', opts[:fastopen_no_cookie])
64
-
65
- if Kanrisuru::Util.present?(opts[:congctl])
66
- if Kanrisuru::Util.present?(opts[:congctl_lock])
67
- command.append_arg('congctl', opts[:congctl])
68
- else
69
- command.append_arg('congctl lock', opts[:congctl])
70
- end
71
- end
12
+ command = ip_route_flush(opts)
13
+ when 'add', 'change', 'append', 'del', 'delete', 'replace'
14
+ command = ip_route_modify(action, opts)
15
+ when 'get'
16
+ command = ip_route_get(opts)
17
+ end
18
+
19
+ execute_shell(command)
20
+
21
+ Kanrisuru::Result.new(command) do |cmd|
22
+ Parser::Route.parse(cmd, action, version)
23
+ end
24
+ end
25
+
26
+ def ip_route_show(opts, version)
27
+ command = Kanrisuru::Command.new('ip')
28
+ command.append_flag('-json') if version >= IPROUTE2_JSON_VERSION
29
+ command.append_arg('-family', opts[:family])
30
+ command << 'route show'
31
+
32
+ ip_route_common_opts(command, opts)
33
+
34
+ command
35
+ end
72
36
 
73
- command.append_arg('advmss', opts[:advmss])
74
- command.append_arg('reordering', opts[:reordering])
37
+ def ip_route_modify(action, opts)
38
+ command = Kanrisuru::Command.new('ip route')
39
+ command << action
75
40
 
76
- if Kanrisuru::Util.present?(opts[:next_hop])
77
- next_hop = opts[:next_hop]
41
+ command.append_arg('to', opts[:to])
42
+ command.append_arg('tos', opts[:tos])
43
+ command.append_arg('dsfield', opts[:dsfield])
44
+ command.append_arg('metric', opts[:metric])
45
+ command.append_arg('preference', opts[:preference])
46
+ command.append_arg('table', opts[:table])
47
+ command.append_arg('vrf', opts[:vrf])
48
+ command.append_arg('dev', opts[:dev])
49
+ command.append_arg('via', opts[:via])
50
+ command.append_arg('src', opts[:src])
51
+ command.append_arg('realm', opts[:realm])
78
52
 
79
- command << 'next_hop'
80
- command.append_arg('via', next_hop[:via])
81
- command.append_arg('dev', next_hop[:dev])
82
- command.append_arg('weight', next_hop[:weight])
53
+ if Kanrisuru::Util.present?(opts[:mtu])
54
+ if Kanrisuru::Util.present?(opts[:mtu_lock])
55
+ command.append_arg('mtu lock', opts[:mtu])
56
+ else
57
+ command.append_arg('mtu', opts[:mtu])
83
58
  end
59
+ end
84
60
 
85
- command.append_arg('scope', opts[:scope])
86
- command.append_arg('protocol', opts[:protocol])
87
- command.append_flag('onlink', opts[:onlink])
88
- command.append_arg('pref', opts[:pref])
89
- when 'get'
90
- command = Kanrisuru::Command.new('ip route get')
91
-
92
- command.append_arg('to', opts[:to])
93
- command.append_arg('from', opts[:from])
94
- command.append_arg('tos', opts[:tos])
95
- command.append_arg('dsfield', opts[:dsfield])
96
- command.append_arg('iif', opts[:iif])
97
- command.append_arg('oif', opts[:oif])
98
- command.append_arg('mark', opts[:mark])
99
- command.append_arg('vrf', opts[:vrf])
100
- command.append_arg('ipproto', opts[:ipproto])
101
- command.append_arg('sport', opts[:sport])
102
- command.append_arg('dport', opts[:dport])
103
-
104
- command.append_flag('connected', opts[:connected])
61
+ command.append_arg('window', opts[:window])
62
+ command.append_arg('rtt', opts[:rtt])
63
+ command.append_arg('rttvar', opts[:rttvar])
64
+ command.append_arg('rto_min', opts[:rto_min])
65
+ command.append_arg('ssthresh', opts[:ssthresh])
66
+ command.append_arg('cwnd', opts[:cwnd])
67
+ command.append_arg('initcwnd', opts[:initcwnd])
68
+ command.append_arg('initrwnd', opts[:initrwnd])
69
+ command.append_arg('features', opts[:features])
70
+ command.append_arg('quickack', opts[:quickack])
71
+ command.append_arg('fastopen_no_cookie', opts[:fastopen_no_cookie])
72
+
73
+ if Kanrisuru::Util.present?(opts[:congctl])
74
+ if Kanrisuru::Util.present?(opts[:congctl_lock])
75
+ command.append_arg('congctl lock', opts[:congctl])
76
+ else
77
+ command.append_arg('congctl', opts[:congctl])
78
+ end
105
79
  end
106
80
 
107
- execute_shell(command)
81
+ command.append_arg('advmss', opts[:advmss])
82
+ command.append_arg('reordering', opts[:reordering])
108
83
 
109
- Kanrisuru::Result.new(command) do |cmd|
110
- Parser::Route.parse(cmd, action, version)
84
+ if Kanrisuru::Util.present?(opts[:next_hop])
85
+ next_hop = opts[:next_hop]
86
+
87
+ command << 'next_hop'
88
+ command.append_arg('via', next_hop[:via])
89
+ command.append_arg('dev', next_hop[:dev])
90
+ command.append_arg('weight', next_hop[:weight])
111
91
  end
92
+
93
+ command.append_arg('scope', opts[:scope])
94
+ command.append_arg('protocol', opts[:protocol])
95
+ command.append_flag('onlink', opts[:onlink])
96
+ command.append_arg('pref', opts[:pref])
97
+ command.append_arg('nhid', opts[:nhid])
98
+ command
99
+ end
100
+
101
+ def ip_route_flush(opts)
102
+ command = Kanrisuru::Command.new('ip')
103
+ command.append_arg('-family', opts[:family])
104
+ command << 'route flush'
105
+
106
+ ip_route_common_opts(command, opts)
107
+
108
+ command
109
+ end
110
+
111
+ def ip_route_get(opts)
112
+ command = Kanrisuru::Command.new('ip route get')
113
+
114
+ command.append_flag('fibmatch', opts[:fibmatch])
115
+ command.append_arg('to', opts[:to])
116
+ command.append_arg('from', opts[:from])
117
+ command.append_arg('tos', opts[:tos])
118
+ command.append_arg('dsfield', opts[:dsfield])
119
+ command.append_arg('iif', opts[:iif])
120
+ command.append_arg('oif', opts[:oif])
121
+ command.append_arg('mark', opts[:mark])
122
+ command.append_arg('vrf', opts[:vrf])
123
+ command.append_arg('ipproto', opts[:ipproto])
124
+ command.append_arg('sport', opts[:sport])
125
+ command.append_arg('dport', opts[:dport])
126
+
127
+ command.append_flag('connected', opts[:connected])
128
+ command
129
+ end
130
+
131
+ def ip_route_common_opts(command, opts)
132
+ command.append_arg('to', opts[:to])
133
+ command.append_arg('dev', opts[:dev])
134
+ command.append_arg('protocol', opts[:protocol])
135
+ command.append_arg('type', opts[:type])
136
+ command.append_arg('table', opts[:table])
137
+ command.append_arg('tos', opts[:tos])
138
+ command.append_arg('dsfield', opts[:dsfield])
139
+ command.append_arg('via', opts[:via])
140
+ command.append_arg('vrf', opts[:vrf])
141
+ command.append_arg('src', opts[:src])
142
+
143
+ command.append_arg('realm', opts[:realm])
144
+ command.append_arg('realms', opts[:realms])
145
+ command.append_arg('scope', opts[:scope])
146
+
147
+ command.append_flag('cloned', opts[:cloned])
148
+ command.append_flag('cached', opts[:cached])
112
149
  end
113
150
  end
114
151
  end
@@ -6,29 +6,12 @@ module Kanrisuru
6
6
  def ip_rule(action, opts)
7
7
  case action
8
8
  when 'show', 'list'
9
- version = ip_version
10
-
11
- command = Kanrisuru::Command.new('ip')
12
- command.append_flag('-json') if version >= IPROUTE2_JSON_VERSION
13
-
14
- command << 'rule show'
9
+ version = ip_version.to_i
10
+ command = ip_rule_show(opts, version)
15
11
  when 'flush'
16
- command = Kanrisuru::Command.new('ip rule flush')
17
- when 'add', 'delete'
18
- command = Kanrisuru::Command.new('ip rule')
19
- command << action
20
-
21
- command.append_arg('type', opts[:type])
22
- command.append_arg('from', opts[:from])
23
- command.append_arg('to', opts[:to])
24
- command.append_arg('iif', opts[:iif])
25
- command.append_arg('tos', opts[:tos])
26
- command.append_arg('dsfield', opts[:dsfield])
27
- command.append_arg('fwmark', opts[:fwmark])
28
- command.append_arg('priority', opts[:priority])
29
- command.append_arg('table', opts[:table])
30
- command.append_arg('realms', opts[:realms])
31
- command.append_arg('nat', opts[:nat])
12
+ command = ip_rule_flush(opts)
13
+ when 'add', 'delete', 'del'
14
+ command = ip_rule_modify(action, opts)
32
15
  end
33
16
 
34
17
  execute_shell(command)
@@ -37,6 +20,38 @@ module Kanrisuru
37
20
  Parser::Rule.parse(cmd, action, version)
38
21
  end
39
22
  end
23
+
24
+ def ip_rule_modify(action, opts)
25
+ command = Kanrisuru::Command.new('ip rule')
26
+ command << action
27
+
28
+ command.append_arg('type', opts[:type])
29
+ command.append_arg('from', opts[:from])
30
+ command.append_arg('to', opts[:to])
31
+ command.append_arg('iif', opts[:iif])
32
+ command.append_arg('tos', opts[:tos])
33
+ command.append_arg('dsfield', opts[:dsfield])
34
+ command.append_arg('fwmark', opts[:fwmark])
35
+ command.append_arg('priority', opts[:priority])
36
+ command.append_arg('table', opts[:table])
37
+ command.append_arg('realms', opts[:realms])
38
+ command.append_arg('nat', opts[:nat])
39
+ command
40
+ end
41
+
42
+ def ip_rule_flush(opts)
43
+ command = Kanrisuru::Command.new('ip rule flush')
44
+ command.append_arg('protocol', opts[:protocol])
45
+ command
46
+ end
47
+
48
+ def ip_rule_show(_opts, version)
49
+ command = Kanrisuru::Command.new('ip')
50
+ command.append_flag('-json') if version >= IPROUTE2_JSON_VERSION
51
+
52
+ command << 'rule show'
53
+ command
54
+ end
40
55
  end
41
56
  end
42
57
  end
@@ -2,6 +2,8 @@
2
2
 
3
3
  require_relative 'commands/address'
4
4
  require_relative 'commands/address_label'
5
+ require_relative 'commands/link_type_opts'
6
+ require_relative 'commands/link_set_opts'
5
7
  require_relative 'commands/link'
6
8
  require_relative 'commands/maddress'
7
9
  require_relative 'commands/neighbour'
@@ -15,9 +17,9 @@ module Kanrisuru
15
17
  command = Kanrisuru::Command.new('ip -V')
16
18
  execute_shell(command)
17
19
 
18
- raise 'ip command not found' if command.failure?
19
-
20
- command.to_s.split('ip utility, iproute2-ss')[1].to_i
20
+ Kanrisuru::Result.new(command) do |cmd|
21
+ Parser::Version.parse(cmd)
22
+ end
21
23
  end
22
24
  end
23
25
  end
@@ -4,10 +4,22 @@ module Kanrisuru
4
4
  module Core
5
5
  module IP
6
6
  IPROUTE2_JSON_VERSION = 180_129
7
+
7
8
  IP_ROUTE_TYPES = %w[
8
9
  unicast unreachable blackhole prohibit local
9
10
  broadcast throw nat via anycast multicast
10
11
  ].freeze
12
+
13
+ IP_FAMILIES = %w[inet inet6 link].freeze
14
+ IP_SCOPES = %w[global site link host].freeze
15
+ IP_LINK_TYPES = %w[
16
+ vlan veth vcan vxcan dummy ifb macvlan macvtap
17
+ bridge bond team ipoib ip6tnl ipip sit vxlan
18
+ gre gretap erspan ip6gre ip6gretap ip6erspan
19
+ vti nlmon team_slave bond_slave bridge_slave
20
+ ipvlan ipvtap geneve vrf macsec netdevsim rmnet
21
+ xfrm bareudp hsr
22
+ ].freeze
11
23
  end
12
24
  end
13
25
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'parsers/base'
4
+ require_relative 'parsers/version'
4
5
  require_relative 'parsers/address'
5
6
  require_relative 'parsers/address_label'
6
7
  require_relative 'parsers/link'
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kanrisuru
4
+ module Core
5
+ module IP
6
+ module Parser
7
+ class Version < Base
8
+ def self.parse(command)
9
+ command.to_s.split('ip utility, iproute2-ss')[1].to_i
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -16,25 +16,28 @@ module Kanrisuru
16
16
 
17
17
  def ip(object, action = nil, opts = {})
18
18
  if action.instance_of?(Hash)
19
- opts = action
19
+ opts = action || {}
20
20
  action = 'show'
21
+ elsif action.nil?
22
+ action = 'show'
23
+ opts = {}
21
24
  end
22
25
 
23
26
  case object
24
- when 'link', 'l'
25
- ip_link(action, opts)
26
27
  when 'address', 'addr', 'a'
27
28
  ip_address(action, opts)
28
29
  when 'addrlabel', 'addrl'
29
30
  ip_address_label(action, opts)
31
+ when 'link', 'l'
32
+ ip_link(action, opts)
33
+ when 'maddress', 'maddr', 'm'
34
+ ip_maddress(action, opts)
35
+ when 'neighbour', 'neigh', 'n'
36
+ ip_neighbour(action, opts)
30
37
  when 'route', 'r', 'ro', 'rou', 'rout'
31
38
  ip_route(action, opts)
32
39
  when 'rule', 'ru'
33
40
  ip_rule(action, opts)
34
- when 'neighbour', 'neigh', 'n'
35
- ip_neighbour(action, opts)
36
- when 'maddress', 'maddr', 'm'
37
- ip_maddress(action, opts)
38
41
  end
39
42
  end
40
43
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kanrisuru
4
- VERSION = '0.14.0'
4
+ VERSION = '0.15.0'
5
5
  end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe Kanrisuru::Core::IP 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
+ %w[addrlabel addrl].each do |object_variant|
23
+ context "with ip #{object_variant}" do
24
+ context 'with json support' do
25
+ before(:all) do
26
+ StubNetwork.stub_command!(:ip_version) do
27
+ Kanrisuru::Core::IP::IPROUTE2_JSON_VERSION
28
+ end
29
+ end
30
+
31
+ after(:all) do
32
+ StubNetwork.unstub_command!(:ip_version)
33
+ end
34
+
35
+ %w[show list].each do |action_variant|
36
+ it "prepares #{action_variant} command" do
37
+ expect_command(host.ip(object_variant, action_variant), 'ip -json addrlabel list')
38
+ end
39
+ end
40
+ end
41
+
42
+ context 'without json support' do
43
+ before(:all) do
44
+ StubNetwork.stub_command!(:ip_version) do
45
+ Kanrisuru::Core::IP::IPROUTE2_JSON_VERSION - 10
46
+ end
47
+ end
48
+
49
+ after(:all) do
50
+ StubNetwork.unstub_command!(:ip_version)
51
+ end
52
+
53
+ %w[show list].each do |action_variant|
54
+ it "prepares #{action_variant} command" do
55
+ expect_command(host.ip(object_variant, action_variant), 'ip addrlabel list')
56
+ end
57
+ end
58
+ end
59
+
60
+ it 'prepares flush command' do
61
+ expect_command(host.ip(object_variant, 'flush'), 'ip addrlabel flush')
62
+ end
63
+
64
+ it 'prepares add command' do
65
+ expect_command(host.ip(object_variant, 'add', {
66
+ prefix: '0.0.0.0/96',
67
+ dev: 'eno2',
68
+ label: 'eno2'
69
+ }), 'ip addrlabel add prefix 0.0.0.0/96 dev eno2 label eno2')
70
+ end
71
+
72
+ it 'prepares del command' do
73
+ expect_command(host.ip(object_variant, 'del', {
74
+ prefix: '0.0.0.0/96',
75
+ dev: 'eno2',
76
+ label: 'eno2'
77
+ }), 'ip addrlabel del prefix 0.0.0.0/96 dev eno2 label eno2')
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe Kanrisuru::Core::IP 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
+ %w[address addr a].each do |object_variant|
23
+ context "with ip #{object_variant}" do
24
+ before(:all) do
25
+ StubNetwork.stub_command!(:ip_version) do
26
+ Kanrisuru::Core::IP::IPROUTE2_JSON_VERSION
27
+ end
28
+ end
29
+
30
+ after(:all) do
31
+ StubNetwork.unstub_command!(:ip_version)
32
+ end
33
+
34
+ %w[show list].each do |action_variant|
35
+ it "prepares #{action_variant} command" do
36
+ expect_command(host.ip(object_variant, action_variant), 'ip -json address show')
37
+ expect_command(host.ip(object_variant, action_variant, {
38
+ stats: true,
39
+ family: 'inet',
40
+ dev: 'lo',
41
+ scope: 'host',
42
+ prefix: '127.0.0.1',
43
+ label: 'lo'
44
+ }), 'ip -json -s -family inet address show dev lo scope host to 127.0.0.1 label lo')
45
+
46
+ expect_command(host.ip(object_variant, action_variant, {
47
+ dynamic: true,
48
+ permanent: true,
49
+ deprecated: true,
50
+ primary: true,
51
+ secondary: true,
52
+ up: true
53
+ }), 'ip -json address show dynamic permanent deprecated primary secondary up')
54
+ end
55
+ end
56
+
57
+ it 'prepares add command' do
58
+ expect_command(host.ip(object_variant, 'add', {
59
+ address: '10.0.0.1',
60
+ dev: 'eno1',
61
+ label: 'eno1',
62
+ scope: 'link'
63
+ }), 'ip address add 10.0.0.1 dev eno1 label eno1 scope link')
64
+ end
65
+
66
+ %w[del delete].each do |action_variant|
67
+ it "prepares #{action_variant} command" do
68
+ expect_command(host.ip(object_variant, action_variant, {
69
+ address: '10.0.0.1',
70
+ dev: 'eno1',
71
+ label: 'eno1',
72
+ scope: 'link'
73
+ }), 'ip address del 10.0.0.1 dev eno1 label eno1 scope link')
74
+ end
75
+ end
76
+
77
+ it 'prepares flush command' do
78
+ expect_command(host.ip(object_variant, 'flush', {
79
+ dev: 'eno1',
80
+ scope: 'link',
81
+ prefix: 'eno1',
82
+ label: 'eno1'
83
+ }), 'ip address flush dev eno1 scope link to eno1 label eno1')
84
+
85
+ expect_command(host.ip(object_variant, 'flush', {
86
+ dynamic: true,
87
+ permanent: true,
88
+ deprecated: true,
89
+ primary: true,
90
+ secondary: true
91
+ }), 'ip address flush dynamic permanent deprecated primary secondary')
92
+ end
93
+ end
94
+ end
95
+ end