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
Labels
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Jobs
Commits
Open sidebar
Rafael Monnerat
slapos.core
Commits
086504fa
Commit
086504fa
authored
Nov 19, 2018
by
Thomas Gambier
🚴🏼
Committed by
Rafael Monnerat
Nov 26, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
CLEANUP: reformat a bit the code (no functional change)
parent
1368d75d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
100 additions
and
98 deletions
+100
-98
slapos/format.py
slapos/format.py
+66
-64
slapos/tests/slapformat.py
slapos/tests/slapformat.py
+34
-34
No files found.
slapos/format.py
View file @
086504fa
...
...
@@ -522,16 +522,6 @@ class Computer(object):
- add groups and users
- construct partitions inside slapgrid
"""
if
alter_network
and
self
.
address
is
not
None
:
self
.
interface
.
addAddr
(
self
.
address
,
self
.
netmask
)
if
use_unique_local_address_block
and
alter_network
:
if
self
.
ipv6_interface
:
network_interface_name
=
self
.
ipv6_interface
else
:
network_interface_name
=
self
.
interface
.
name
self
.
_addUniqueLocalAddressIpv6
(
network_interface_name
)
for
path
in
self
.
instance_root
,
self
.
software_root
:
if
not
os
.
path
.
exists
(
path
):
os
.
makedirs
(
path
,
0o755
)
...
...
@@ -562,23 +552,34 @@ class Computer(object):
if
the_digit
.
isdigit
():
instance_external_list
.
append
(
data_path
)
tap_address_list
=
[]
if
alter_network
and
create_tap
:
if
self
.
tap_gateway_interface
:
gateway_addr_dict
=
getIfaceAddressIPv4
(
self
.
tap_gateway_interface
)
tap_address_list
=
getIPv4SubnetAddressRange
(
gateway_addr_dict
[
'addr'
],
gateway_addr_dict
[
'netmask'
],
len
(
self
.
partition_list
))
assert
(
len
(
self
.
partition_list
)
<=
len
(
tap_address_list
))
else
:
gateway_addr_dict
=
{
'peer'
:
'10.0.0.1'
,
'netmask'
:
'255.255.0.0'
,
'addr'
:
'10.0.0.1'
,
'network'
:
'10.0.0.0'
}
####################
### Network part ###
####################
if
alter_network
:
if
self
.
address
is
not
None
:
self
.
interface
.
addIPv6Address
(
self
.
address
,
self
.
netmask
)
tap_address_list
=
getIPv4SubnetAddressRange
(
gateway_addr_dict
[
'addr'
],
gateway_addr_dict
[
'netmask'
],
len
(
self
.
partition_list
))
if
use_unique_local_address_block
:
if
self
.
ipv6_interface
:
network_interface_name
=
self
.
ipv6_interface
else
:
network_interface_name
=
self
.
interface
.
name
self
.
_addUniqueLocalAddressIpv6
(
network_interface_name
)
if
create_tap
:
if
self
.
tap_gateway_interface
:
gateway_addr_dict
=
getIfaceAddressIPv4
(
self
.
tap_gateway_interface
)
tap_address_list
=
getIPv4SubnetAddressRange
(
gateway_addr_dict
[
'addr'
],
gateway_addr_dict
[
'netmask'
],
len
(
self
.
partition_list
))
assert
(
len
(
self
.
partition_list
)
<=
len
(
tap_address_list
))
else
:
gateway_addr_dict
=
{
'peer'
:
'10.0.0.1'
,
'netmask'
:
'255.255.0.0'
,
'addr'
:
'10.0.0.1'
,
'network'
:
'10.0.0.0'
}
tap_address_list
=
getIPv4SubnetAddressRange
(
gateway_addr_dict
[
'addr'
],
gateway_addr_dict
[
'netmask'
],
len
(
self
.
partition_list
))
if
alter_network
:
self
.
_speedHackAddAllOldIpsToInterface
()
try
:
...
...
@@ -592,37 +593,38 @@ class Computer(object):
if
alter_user
:
partition
.
user
.
create
()
# Reconstructing Tap
if
partition
.
user
and
partition
.
user
.
isAvailable
():
owner
=
partition
.
user
else
:
owner
=
User
(
'root'
)
if
alter_network
and
create_tap
:
partition
.
tap
.
createWithOwner
(
owner
)
# add addresses and create route for this tap
next_ipv4_addr
=
'%s'
%
tap_address_list
.
pop
(
0
)
if
not
partition
.
tap
.
ipv4_addr
:
# define new ipv4 address for this tap
partition
.
tap
.
ipv4_addr
=
next_ipv4_addr
partition
.
tap
.
ipv4_netmask
=
gateway_addr_dict
[
'netmask'
]
partition
.
tap
.
ipv4_gateway
=
gateway_addr_dict
[
'addr'
]
partition
.
tap
.
ipv4_network
=
gateway_addr_dict
[
'network'
]
if
not
partition
.
tap
.
ipv6_addr
:
ipv6_addr
=
self
.
interface
.
addAddr
(
tap
=
partition
.
tap
)
partition
.
tap
.
ipv6_addr
=
""
partition
.
tap
.
ipv6_netmask
=
""
partition
.
tap
.
ipv6_gateway
=
""
partition
.
tap
.
ipv6_network
=
""
partition
.
tap
.
createRoutes
()
if
alter_network
and
partition
.
tun
is
not
None
:
# create TUN interface per partition as well
partition
.
tun
.
createWithOwner
(
owner
)
partition
.
tun
.
createRoutes
()
# Reconstructing Tap and Tun
if
alter_network
:
if
partition
.
user
and
partition
.
user
.
isAvailable
():
owner
=
partition
.
user
else
:
owner
=
User
(
'root'
)
if
create_tap
:
partition
.
tap
.
createWithOwner
(
owner
)
# add addresses and create route for this tap
if
not
partition
.
tap
.
ipv4_addr
:
# define new ipv4 address for this tap
next_ipv4_addr
=
'%s'
%
tap_address_list
.
pop
(
0
)
partition
.
tap
.
ipv4_addr
=
next_ipv4_addr
partition
.
tap
.
ipv4_netmask
=
gateway_addr_dict
[
'netmask'
]
partition
.
tap
.
ipv4_gateway
=
gateway_addr_dict
[
'addr'
]
partition
.
tap
.
ipv4_network
=
gateway_addr_dict
[
'network'
]
if
not
partition
.
tap
.
ipv6_addr
:
ipv6_addr
=
self
.
interface
.
addIPv6Address
(
tap
=
partition
.
tap
)
partition
.
tap
.
ipv6_addr
=
""
partition
.
tap
.
ipv6_netmask
=
""
partition
.
tap
.
ipv6_gateway
=
""
partition
.
tap
.
ipv6_network
=
""
partition
.
tap
.
createRoutes
()
if
partition
.
tun
is
not
None
:
# create TUN interface per partition as well
partition
.
tun
.
createWithOwner
(
owner
)
partition
.
tun
.
createRoutes
()
# Reconstructing partition's directory
partition
.
createPath
(
alter_user
)
...
...
@@ -630,12 +632,12 @@ class Computer(object):
# Reconstructing partition's address
# There should be two addresses on each Computer Partition:
# * global IPv6
# * local IPv4, took from slapformat:ipv4_local_network
# * global IPv6
if
not
partition
.
address_list
:
# regenerate
partition
.
address_list
.
append
(
self
.
interface
.
addIPv4LocalAddress
())
partition
.
address_list
.
append
(
self
.
interface
.
add
Addr
())
partition
.
address_list
.
append
(
self
.
interface
.
add
IPv6Address
())
elif
alter_network
:
# regenerate list of addresses
old_partition_address_list
=
partition
.
address_list
...
...
@@ -653,7 +655,7 @@ class Computer(object):
for
address
in
old_partition_address_list
:
if
netaddr
.
valid_ipv6
(
address
[
'addr'
]):
partition
.
address_list
.
append
(
self
.
interface
.
add
Addr
(
partition
.
address_list
.
append
(
self
.
interface
.
add
IPv6Address
(
address
[
'addr'
],
address
[
'netmask'
]))
elif
netaddr
.
valid_ipv4
(
address
[
'addr'
]):
...
...
@@ -1097,14 +1099,14 @@ class Interface(object):
# confirmed to be configured
return
dict
(
addr
=
addr
,
netmask
=
netmask
)
def
add
Addr
(
self
,
addr
=
None
,
netmask
=
None
,
tap
=
None
):
def
add
IPv6Address
(
self
,
addr
=
None
,
netmask
=
None
,
tap
=
None
):
"""
Adds IP address to interface.
Adds IP
v6
address to interface.
If addr is specified and exists already on interface
does
nothing.
If addr is specified and exists already on interface
, do
nothing.
If addr is specified and does not exist
s on interface, tries
to add given
address. If it is not possible (ex. because network changed)
calculates
new
If addr is specified and does not exist
on interface, try
to add given
address. If it is not possible (ex. because network changed)
, calculate
new
address.
Args:
...
...
slapos/tests/slapformat.py
View file @
086504fa
...
...
@@ -317,7 +317,7 @@ class TestComputer(SlapformatMixin):
instance_root
=
'/instance_root'
,
software_root
=
'/software_root'
,
interface
=
slapos
.
format
.
Interface
(
logger
=
self
.
logger
,
name
=
'
bridge
'
,
ipv4_local_network
=
'127.0.0.1/16'
),
logger
=
self
.
logger
,
name
=
'
lo
'
,
ipv4_local_network
=
'127.0.0.1/16'
),
partition_list
=
[])
computer
.
format
()
self
.
assertEqual
([
...
...
@@ -327,7 +327,7 @@ class TestComputer(SlapformatMixin):
"chmod('/software_root', 493)"
],
self
.
test_result
.
bucket
)
self
.
assertEqual
([
'ip addr list
bridge
'
,
'ip addr list
lo
'
,
'groupadd slapsoft'
,
'useradd -d /software_root -g slapsoft slapsoft -r'
],
...
...
@@ -338,7 +338,7 @@ class TestComputer(SlapformatMixin):
instance_root
=
'/instance_root'
,
software_root
=
'/software_root'
,
interface
=
slapos
.
format
.
Interface
(
logger
=
self
.
logger
,
name
=
'
bridge
'
,
ipv4_local_network
=
'127.0.0.1/16'
),
logger
=
self
.
logger
,
name
=
'
lo
'
,
ipv4_local_network
=
'127.0.0.1/16'
),
partition_list
=
[])
computer
.
format
(
alter_user
=
False
)
self
.
assertEqual
([
...
...
@@ -355,7 +355,7 @@ class TestComputer(SlapformatMixin):
instance_root
=
'/instance_root'
,
software_root
=
'/software_root'
,
interface
=
slapos
.
format
.
Interface
(
logger
=
self
.
logger
,
name
=
'
bridge
'
,
ipv4_local_network
=
'127.0.0.1/16'
),
logger
=
self
.
logger
,
name
=
'
lo
'
,
ipv4_local_network
=
'127.0.0.1/16'
),
partition_list
=
[])
self
.
assertEqual
([
"makedirs('/instance_root', 493)"
,
...
...
@@ -364,7 +364,7 @@ class TestComputer(SlapformatMixin):
"chmod('/software_root', 493)"
],
self
.
test_result
.
bucket
)
self
.
assertEqual
([
'ip addr list
bridge
'
,
'ip addr list
lo
'
,
'groupadd slapsoft'
,
'useradd -d /software_root -g slapsoft slapsoft -r'
],
...
...
@@ -375,7 +375,7 @@ class TestComputer(SlapformatMixin):
instance_root
=
'/instance_root'
,
software_root
=
'/software_root'
,
interface
=
slapos
.
format
.
Interface
(
logger
=
self
.
logger
,
name
=
'
bridge
'
,
ipv4_local_network
=
'127.0.0.1/16'
),
logger
=
self
.
logger
,
name
=
'
lo
'
,
ipv4_local_network
=
'127.0.0.1/16'
),
partition_list
=
[])
computer
.
format
(
alter_network
=
False
,
alter_user
=
False
)
self
.
assertEqual
([
...
...
@@ -392,13 +392,13 @@ class TestComputer(SlapformatMixin):
instance_root
=
'/instance_root'
,
software_root
=
'/software_root'
,
interface
=
slapos
.
format
.
Interface
(
logger
=
self
.
logger
,
name
=
'
bridg
e'
,
ipv4_local_network
=
'127.0.0.1/16'
),
logger
=
self
.
logger
,
name
=
'
myinterfac
e'
,
ipv4_local_network
=
'127.0.0.1/16'
),
partition_list
=
[
slapos
.
format
.
Partition
(
'partition'
,
'/part_path'
,
slapos
.
format
.
User
(
'testuser'
),
[],
tap
=
slapos
.
format
.
Tap
(
'tap'
)),
])
global
INTERFACE_DICT
INTERFACE_DICT
[
'
bridg
e'
]
=
{
INTERFACE_DICT
[
'
myinterfac
e'
]
=
{
socket
.
AF_INET
:
[{
'addr'
:
'192.168.242.77'
,
'broadcast'
:
'127.0.0.1'
,
'netmask'
:
'255.255.255.0'
}],
socket
.
AF_INET6
:
[{
'addr'
:
'2a01:e35:2e27::e59c'
,
'netmask'
:
'ffff:ffff:ffff:ffff::'
}]
...
...
@@ -415,17 +415,17 @@ class TestComputer(SlapformatMixin):
],
self
.
test_result
.
bucket
)
self
.
assertEqual
([
'ip addr list
bridg
e'
,
'ip addr list
myinterfac
e'
,
'groupadd slapsoft'
,
'useradd -d /software_root -g slapsoft slapsoft -r'
,
'groupadd testuser'
,
'useradd -d /instance_root/partition -g testuser -G slapsoft testuser -r'
,
'ip tuntap add dev tap mode tap user testuser'
,
'ip link set tap up'
,
'ip addr add ip/255.255.255.255 dev
bridg
e'
,
'ip addr list
bridg
e'
,
'ip addr add ip/ffff:ffff:ffff:ffff:: dev
bridg
e'
,
'ip addr list
bridg
e'
,
'ip addr add ip/255.255.255.255 dev
myinterfac
e'
,
'ip addr list
myinterfac
e'
,
'ip addr add ip/ffff:ffff:ffff:ffff:: dev
myinterfac
e'
,
'ip addr list
myinterfac
e'
,
],
self
.
fakeCallAndRead
.
external_command_list
)
...
...
@@ -434,7 +434,7 @@ class TestComputer(SlapformatMixin):
instance_root
=
'/instance_root'
,
software_root
=
'/software_root'
,
interface
=
slapos
.
format
.
Interface
(
logger
=
self
.
logger
,
name
=
'
bridg
e'
,
ipv4_local_network
=
'127.0.0.1/16'
),
logger
=
self
.
logger
,
name
=
'
myinterfac
e'
,
ipv4_local_network
=
'127.0.0.1/16'
),
partition_list
=
[
slapos
.
format
.
Partition
(
'partition'
,
'/part_path'
,
slapos
.
format
.
User
(
'testuser'
),
[],
tap
=
slapos
.
format
.
Tap
(
'tap'
)),
...
...
@@ -442,7 +442,7 @@ class TestComputer(SlapformatMixin):
global
USER_LIST
USER_LIST
=
[
'testuser'
]
global
INTERFACE_DICT
INTERFACE_DICT
[
'
bridg
e'
]
=
{
INTERFACE_DICT
[
'
myinterfac
e'
]
=
{
socket
.
AF_INET
:
[{
'addr'
:
'192.168.242.77'
,
'broadcast'
:
'127.0.0.1'
,
'netmask'
:
'255.255.255.0'
}],
socket
.
AF_INET6
:
[{
'addr'
:
'2a01:e35:2e27::e59c'
,
'netmask'
:
'ffff:ffff:ffff:ffff::'
}]
...
...
@@ -468,10 +468,10 @@ class TestComputer(SlapformatMixin):
'ip -6 addr list tap'
,
'ip route show 10.0.0.2'
,
'ip route add 10.0.0.2 dev tap'
,
'ip addr add ip/255.255.255.255 dev
bridg
e'
,
# 'ip addr list
bridg
e',
'ip addr add ip/ffff:ffff:ffff:ffff:: dev
bridg
e'
,
'ip -6 addr list
bridg
e'
,
'ip addr add ip/255.255.255.255 dev
myinterfac
e'
,
# 'ip addr list
myinterfac
e',
'ip addr add ip/ffff:ffff:ffff:ffff:: dev
myinterfac
e'
,
'ip -6 addr list
myinterfac
e'
,
],
self
.
fakeCallAndRead
.
external_command_list
)
...
...
@@ -537,14 +537,14 @@ class TestComputer(SlapformatMixin):
instance_root
=
'/instance_root'
,
software_root
=
'/software_root'
,
interface
=
slapos
.
format
.
Interface
(
logger
=
self
.
logger
,
name
=
'
bridg
e'
,
ipv4_local_network
=
'127.0.0.1/16'
),
logger
=
self
.
logger
,
name
=
'
myinterfac
e'
,
ipv4_local_network
=
'127.0.0.1/16'
),
partition_list
=
[
slapos
.
format
.
Partition
(
'partition'
,
'/part_path'
,
slapos
.
format
.
User
(
'testuser'
),
[],
tap
=
slapos
.
format
.
Tap
(
'tap'
)),
])
global
INTERFACE_DICT
INTERFACE_DICT
[
'
bridg
e'
]
=
{
INTERFACE_DICT
[
'
myinterfac
e'
]
=
{
socket
.
AF_INET
:
[{
'addr'
:
'192.168.242.77'
,
'broadcast'
:
'127.0.0.1'
,
'netmask'
:
'255.255.255.0'
}],
socket
.
AF_INET6
:
[{
'addr'
:
'2a01:e35:2e27::e59c'
,
'netmask'
:
'ffff:ffff:ffff:ffff::'
}]
...
...
@@ -561,15 +561,15 @@ class TestComputer(SlapformatMixin):
],
self
.
test_result
.
bucket
)
self
.
assertEqual
([
# 'ip addr list
bridg
e',
# 'ip addr list
myinterfac
e',
'groupadd slapsoft'
,
'useradd -d /software_root -g slapsoft slapsoft -r'
,
'groupadd testuser'
,
'useradd -d /instance_root/partition -g testuser -G slapsoft testuser -r'
,
# 'ip addr add ip/255.255.255.255 dev
bridg
e',
# 'ip addr list
bridg
e',
# 'ip addr add ip/ffff:ffff:ffff:ffff:: dev
bridg
e',
# 'ip addr list
bridg
e',
# 'ip addr add ip/255.255.255.255 dev
myinterfac
e',
# 'ip addr list
myinterfac
e',
# 'ip addr add ip/ffff:ffff:ffff:ffff:: dev
myinterfac
e',
# 'ip addr list
myinterfac
e',
],
self
.
fakeCallAndRead
.
external_command_list
)
...
...
@@ -578,14 +578,14 @@ class TestComputer(SlapformatMixin):
instance_root
=
'/instance_root'
,
software_root
=
'/software_root'
,
interface
=
slapos
.
format
.
Interface
(
logger
=
self
.
logger
,
name
=
'
bridg
e'
,
ipv4_local_network
=
'127.0.0.1/16'
),
logger
=
self
.
logger
,
name
=
'
myinterfac
e'
,
ipv4_local_network
=
'127.0.0.1/16'
),
partition_list
=
[
slapos
.
format
.
Partition
(
'partition'
,
'/part_path'
,
slapos
.
format
.
User
(
'testuser'
),
[],
tap
=
slapos
.
format
.
Tap
(
'tap'
)),
])
global
INTERFACE_DICT
INTERFACE_DICT
[
'
bridg
e'
]
=
{
INTERFACE_DICT
[
'
myinterfac
e'
]
=
{
socket
.
AF_INET
:
[{
'addr'
:
'192.168.242.77'
,
'broadcast'
:
'127.0.0.1'
,
'netmask'
:
'255.255.255.0'
}],
socket
.
AF_INET6
:
[{
'addr'
:
'2a01:e35:2e27::e59c'
,
'netmask'
:
'ffff:ffff:ffff:ffff::'
}]
...
...
@@ -601,10 +601,10 @@ class TestComputer(SlapformatMixin):
],
self
.
test_result
.
bucket
)
self
.
assertEqual
([
'ip addr add ip/255.255.255.255 dev
bridg
e'
,
# 'ip addr list
bridg
e',
'ip addr add ip/ffff:ffff:ffff:ffff:: dev
bridg
e'
,
'ip -6 addr list
bridg
e'
,
'ip addr add ip/255.255.255.255 dev
myinterfac
e'
,
# 'ip addr list
myinterfac
e',
'ip addr add ip/ffff:ffff:ffff:ffff:: dev
myinterfac
e'
,
'ip -6 addr list
myinterfac
e'
,
],
self
.
fakeCallAndRead
.
external_command_list
)
...
...
@@ -686,7 +686,7 @@ class TestComputerWithCPUSet(SlapformatMixin):
global
USER_LIST
,
INTERFACE_DICT
USER_LIST
=
[
'testuser'
]
INTERFACE_DICT
[
'
bridge
'
]
=
{
INTERFACE_DICT
[
'
lo
'
]
=
{
socket
.
AF_INET
:
[
{
'addr'
:
'127.0.0.1'
,
'broadcast'
:
'127.0.255.255'
,
'netmask'
:
'255.255.0.0'
}],
socket
.
AF_INET6
:
[
...
...
@@ -704,7 +704,7 @@ class TestComputerWithCPUSet(SlapformatMixin):
instance_root
=
'/tmp/slapgrid/instance_root'
,
software_root
=
'/tmp/slapgrid/software_root'
,
interface
=
slapos
.
format
.
Interface
(
logger
=
self
.
logger
,
name
=
'
bridge
'
,
ipv4_local_network
=
'127.0.0.1/16'
),
logger
=
self
.
logger
,
name
=
'
lo
'
,
ipv4_local_network
=
'127.0.0.1/16'
),
partition_list
=
[
slapos
.
format
.
Partition
(
'partition'
,
'/tmp/slapgrid/instance_root/part1'
,
slapos
.
format
.
User
(
'testuser'
),
[],
tap
=
None
),
...
...
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