Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
slapos.core
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Léo-Paul Géneau
slapos.core
Commits
6272d68e
Commit
6272d68e
authored
Mar 08, 2023
by
Xavier Thompson
Browse files
Options
Browse Files
Download
Plain Diff
Slapformat: IPv6 range for partitions and tun
See merge request
nexedi/slapos.core!455
parents
7e2f8eec
c4e86b91
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
356 additions
and
120 deletions
+356
-120
slapos/cli/format.py
slapos/cli/format.py
+1
-1
slapos/format.py
slapos/format.py
+285
-105
slapos/tests/test_slapformat.py
slapos/tests/test_slapformat.py
+13
-14
slapos/util.py
slapos/util.py
+57
-0
No files found.
slapos/cli/format.py
View file @
6272d68e
...
@@ -125,7 +125,7 @@ class FormatCommand(ConfigCommand):
...
@@ -125,7 +125,7 @@ class FormatCommand(ConfigCommand):
try
:
try
:
conf
.
setConfig
()
conf
.
setConfig
()
except
UsageError
as
err
:
except
UsageError
as
err
:
sys
.
stderr
.
write
(
err
.
message
+
'
\
n
'
)
sys
.
stderr
.
write
(
str
(
err
)
+
'
\
n
'
)
sys
.
stderr
.
write
(
"For help use --help
\
n
"
)
sys
.
stderr
.
write
(
"For help use --help
\
n
"
)
sys
.
exit
(
1
)
sys
.
exit
(
1
)
...
...
slapos/format.py
View file @
6272d68e
This diff is collapsed.
Click to expand it.
slapos/tests/test_slapformat.py
View file @
6272d68e
...
@@ -301,7 +301,7 @@ class SlapformatMixin(unittest.TestCase):
...
@@ -301,7 +301,7 @@ class SlapformatMixin(unittest.TestCase):
raise
ValueError
(
"{} already has logger attached"
.
format
(
self
.
__class__
.
__name__
))
raise
ValueError
(
"{} already has logger attached"
.
format
(
self
.
__class__
.
__name__
))
self
.
logger
=
logger
self
.
logger
=
logger
self
.
partition
=
slapos
.
format
.
Partition
(
'partition'
,
'/part_path'
,
self
.
partition
=
slapos
.
format
.
Partition
(
'partition'
,
'/part_path'
,
slapos
.
format
.
User
(
'testuser'
),
[],
None
)
slapos
.
format
.
User
(
'testuser'
),
[],
None
,
None
)
global
USER_LIST
global
USER_LIST
USER_LIST
=
[]
USER_LIST
=
[]
global
GROUP_LIST
global
GROUP_LIST
...
@@ -423,7 +423,8 @@ class TestComputer(SlapformatMixin):
...
@@ -423,7 +423,8 @@ class TestComputer(SlapformatMixin):
logger
=
self
.
logger
,
name
=
'myinterface'
,
ipv4_local_network
=
'127.0.0.1/16'
),
logger
=
self
.
logger
,
name
=
'myinterface'
,
ipv4_local_network
=
'127.0.0.1/16'
),
partition_list
=
[
partition_list
=
[
slapos
.
format
.
Partition
(
slapos
.
format
.
Partition
(
'partition'
,
'/part_path'
,
slapos
.
format
.
User
(
'testuser'
),
[],
tap
=
slapos
.
format
.
Tap
(
'tap'
)),
'partition'
,
'/part_path'
,
slapos
.
format
.
User
(
'testuser'
),
[],
ipv6_range
=
None
,
tap
=
slapos
.
format
.
Tap
(
'tap'
)),
])
])
global
INTERFACE_DICT
global
INTERFACE_DICT
INTERFACE_DICT
[
'myinterface'
]
=
{
INTERFACE_DICT
[
'myinterface'
]
=
{
...
@@ -466,7 +467,8 @@ class TestComputer(SlapformatMixin):
...
@@ -466,7 +467,8 @@ class TestComputer(SlapformatMixin):
logger
=
self
.
logger
,
name
=
'myinterface'
,
ipv4_local_network
=
'127.0.0.1/16'
),
logger
=
self
.
logger
,
name
=
'myinterface'
,
ipv4_local_network
=
'127.0.0.1/16'
),
partition_list
=
[
partition_list
=
[
slapos
.
format
.
Partition
(
slapos
.
format
.
Partition
(
'partition'
,
'/part_path'
,
slapos
.
format
.
User
(
'testuser'
),
[],
tap
=
slapos
.
format
.
Tap
(
'tap'
)),
'partition'
,
'/part_path'
,
slapos
.
format
.
User
(
'testuser'
),
[],
ipv6_range
=
None
,
tap
=
slapos
.
format
.
Tap
(
'tap'
)),
])
])
global
USER_LIST
global
USER_LIST
USER_LIST
=
[
'testuser'
]
USER_LIST
=
[
'testuser'
]
...
@@ -517,7 +519,7 @@ class TestComputer(SlapformatMixin):
...
@@ -517,7 +519,7 @@ class TestComputer(SlapformatMixin):
partition_list
=
[
partition_list
=
[
slapos
.
format
.
Partition
(
slapos
.
format
.
Partition
(
'partition'
,
'/part_path'
,
slapos
.
format
.
User
(
'testuser'
),
[],
'partition'
,
'/part_path'
,
slapos
.
format
.
User
(
'testuser'
),
[],
tap
=
slapos
.
format
.
Tap
(
'tap'
)),
ipv6_range
=
None
,
tap
=
slapos
.
format
.
Tap
(
'tap'
)),
])
])
global
USER_LIST
global
USER_LIST
USER_LIST
=
[
'testuser'
]
USER_LIST
=
[
'testuser'
]
...
@@ -577,7 +579,7 @@ class TestComputer(SlapformatMixin):
...
@@ -577,7 +579,7 @@ class TestComputer(SlapformatMixin):
partition_list
=
[
partition_list
=
[
slapos
.
format
.
Partition
(
slapos
.
format
.
Partition
(
'partition'
,
'/part_path'
,
slapos
.
format
.
User
(
'testuser'
),
[],
'partition'
,
'/part_path'
,
slapos
.
format
.
User
(
'testuser'
),
[],
tap
=
slapos
.
format
.
Tap
(
'tap'
)),
ipv6_range
=
None
,
tap
=
slapos
.
format
.
Tap
(
'tap'
)),
])
])
global
INTERFACE_DICT
global
INTERFACE_DICT
INTERFACE_DICT
[
'myinterface'
]
=
{
INTERFACE_DICT
[
'myinterface'
]
=
{
...
@@ -618,7 +620,7 @@ class TestComputer(SlapformatMixin):
...
@@ -618,7 +620,7 @@ class TestComputer(SlapformatMixin):
partition_list
=
[
partition_list
=
[
slapos
.
format
.
Partition
(
slapos
.
format
.
Partition
(
'partition'
,
'/part_path'
,
slapos
.
format
.
User
(
'testuser'
),
[],
'partition'
,
'/part_path'
,
slapos
.
format
.
User
(
'testuser'
),
[],
tap
=
slapos
.
format
.
Tap
(
'tap'
)),
ipv6_range
=
None
,
tap
=
slapos
.
format
.
Tap
(
'tap'
)),
])
])
global
INTERFACE_DICT
global
INTERFACE_DICT
INTERFACE_DICT
[
'myinterface'
]
=
{
INTERFACE_DICT
[
'myinterface'
]
=
{
...
@@ -636,12 +638,7 @@ class TestComputer(SlapformatMixin):
...
@@ -636,12 +638,7 @@ class TestComputer(SlapformatMixin):
"chmod('/instance_root/partition', 488)"
"chmod('/instance_root/partition', 488)"
],
],
self
.
test_result
.
bucket
)
self
.
test_result
.
bucket
)
self
.
assertEqual
([
self
.
assertEqual
([],
'ip addr add ip/255.255.255.255 dev myinterface'
,
# 'ip addr list myinterface',
'ip addr add ip/ffff:ffff:ffff:ffff:: dev myinterface'
,
'ip -6 addr list myinterface'
,
],
self
.
fakeCallAndRead
.
external_command_list
)
self
.
fakeCallAndRead
.
external_command_list
)
...
@@ -660,7 +657,8 @@ class TestFormatDump(SlapformatMixin):
...
@@ -660,7 +657,8 @@ class TestFormatDump(SlapformatMixin):
logger
=
self
.
logger
,
name
=
'myinterface'
,
ipv4_local_network
=
'127.0.0.1/16'
),
logger
=
self
.
logger
,
name
=
'myinterface'
,
ipv4_local_network
=
'127.0.0.1/16'
),
partition_list
=
[
partition_list
=
[
slapos
.
format
.
Partition
(
slapos
.
format
.
Partition
(
'partition'
,
'part_path'
,
slapos
.
format
.
User
(
'testuser'
),
[],
tap
=
slapos
.
format
.
Tap
(
'tap'
)),
'partition'
,
'part_path'
,
slapos
.
format
.
User
(
'testuser'
),
[],
ipv6_range
=
None
,
tap
=
slapos
.
format
.
Tap
(
'tap'
)),
])
])
global
USER_LIST
global
USER_LIST
USER_LIST
=
[
'testuser'
]
USER_LIST
=
[
'testuser'
]
...
@@ -745,7 +743,8 @@ class TestComputerWithCPUSet(SlapformatMixin):
...
@@ -745,7 +743,8 @@ class TestComputerWithCPUSet(SlapformatMixin):
logger
=
self
.
logger
,
name
=
'lo'
,
ipv4_local_network
=
'127.0.0.1/16'
),
logger
=
self
.
logger
,
name
=
'lo'
,
ipv4_local_network
=
'127.0.0.1/16'
),
partition_list
=
[
partition_list
=
[
slapos
.
format
.
Partition
(
slapos
.
format
.
Partition
(
'partition'
,
'/tmp/slapgrid/instance_root/part1'
,
slapos
.
format
.
User
(
'testuser'
),
[],
tap
=
None
),
'partition'
,
'/tmp/slapgrid/instance_root/part1'
,
slapos
.
format
.
User
(
'testuser'
),
[],
ipv6_range
=
None
,
tap
=
None
),
],
],
config
=
{
config
=
{
"manager_list"
:
"cpuset"
,
"manager_list"
:
"cpuset"
,
...
...
slapos/util.py
View file @
6272d68e
...
@@ -205,6 +205,59 @@ def ipv6FromBin(ip, suffix=''):
...
@@ -205,6 +205,59 @@ def ipv6FromBin(ip, suffix=''):
return
socket
.
inet_ntop
(
socket
.
AF_INET6
,
return
socket
.
inet_ntop
(
socket
.
AF_INET6
,
struct
.
pack
(
'>QQ'
,
int
(
ip
[:
64
],
2
),
int
(
ip
[
64
:],
2
)))
struct
.
pack
(
'>QQ'
,
int
(
ip
[:
64
],
2
),
int
(
ip
[
64
:],
2
)))
def
getPartitionIpv6Addr
(
ipv6_range
,
partition_index
):
"""
from a IPv6 range in the form
{
'addr' : addr,
'prefixlen' : CIDR
}
returns the IPv6 addr
addr::(partition_index+2) (address 1 is is used by re6st)
"""
addr
=
ipv6_range
[
'addr'
]
prefixlen
=
ipv6_range
[
'prefixlen'
]
prefix
=
binFromIpv6
(
addr
)[:
prefixlen
]
return
dict
(
addr
=
ipv6FromBin
(
prefix
+
bin
(
partition_index
+
2
)[
2
:].
zfill
(
128
-
prefixlen
)),
prefixlen
=
prefixlen
)
def
getIpv6RangeFactory
(
k
,
s
):
def
getIpv6Range
(
ipv6_range
,
partition_index
):
"""
from a IPv6 range in the form
{
'addr' : addr,
'prefixlen' : CIDR
}
returns the IPv6 range
{
'addr' : addr:(k*(2^14) + partition_index+1)
'prefixlen' : CIDR+16
}
"""
addr
=
ipv6_range
[
'addr'
]
prefixlen
=
ipv6_range
[
'prefixlen'
]
prefix
=
binFromIpv6
(
addr
)[:
prefixlen
]
# we generate a subnetwork for the partition
# the subnetwork has 16 bits more than our IPv6 range
# make sure we have at least 2 IPs in the subnetwork
prefixlen
+=
16
if
prefixlen
>=
128
:
raise
ValueError
(
'The IPv6 range has prefixlen {} which is too big for generating IPv6 range for partitions.'
.
format
(
prefixlen
))
return
dict
(
addr
=
ipv6FromBin
(
prefix
+
bin
((
k
<<
14
)
+
partition_index
+
1
)[
2
:].
zfill
(
16
)
+
s
*
(
128
-
prefixlen
)),
prefixlen
=
prefixlen
)
return
getIpv6Range
getPartitionIpv6Range
=
getIpv6RangeFactory
(
1
,
'0'
)
getTapIpv6Range
=
getIpv6RangeFactory
(
2
,
'1'
)
getTunIpv6Range
=
getIpv6RangeFactory
(
3
,
'0'
)
def
getIpv6RangeFirstAddr
(
addr
,
prefixlen
):
addr_1
=
"%s1"
%
ipv6FromBin
(
binFromIpv6
(
addr
)[:
prefixlen
])
return
ipv6FromBin
(
binFromIpv6
(
addr_1
))
# correctly format the IPv6
def
lenNetmaskIpv6
(
netmask
):
def
lenNetmaskIpv6
(
netmask
):
"""Convert string represented netmask to its integer prefix"""
"""Convert string represented netmask to its integer prefix"""
# Since version 0.10.7 of netifaces, the netmask is something like "ffff::/16",
# Since version 0.10.7 of netifaces, the netmask is something like "ffff::/16",
...
@@ -215,6 +268,10 @@ def lenNetmaskIpv6(netmask):
...
@@ -215,6 +268,10 @@ def lenNetmaskIpv6(netmask):
except
ValueError
:
except
ValueError
:
return
netaddr
.
IPNetwork
(
netmask
).
prefixlen
return
netaddr
.
IPNetwork
(
netmask
).
prefixlen
def
netmaskFromLenIPv6
(
netmask_len
):
""" opposite of lenNetmaskIpv6"""
return
ipv6FromBin
(
'1'
*
netmask_len
)
# Used for Python 2-3 compatibility
# Used for Python 2-3 compatibility
if
str
is
bytes
:
if
str
is
bytes
:
bytes2str
=
str2bytes
=
lambda
s
:
s
bytes2str
=
str2bytes
=
lambda
s
:
s
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment