Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
7950dd01
Commit
7950dd01
authored
Aug 11, 2012
by
Ahmad Khayyat
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Recognize interface aliases in network facts, and add IP facts
parent
05a128c2
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
78 additions
and
83 deletions
+78
-83
library/setup
+78
-83
No files found.
library/setup
View file @
7950dd01
...
@@ -433,93 +433,88 @@ class LinuxNetwork(Network):
...
@@ -433,93 +433,88 @@ class LinuxNetwork(Network):
Network
.
__init__
(
self
)
Network
.
__init__
(
self
)
def
populate
(
self
):
def
populate
(
self
):
self
.
facts
[
'interfaces'
]
=
self
.
get_interfaces
()
interfaces
,
ips
=
self
.
parse_ip_addr
()
self
.
get_interface_facts
()
self
.
get_ipv4_facts
()
self
.
facts
[
'interfaces'
]
=
interfaces
.
keys
()
self
.
get_ipv6_facts
()
for
iface
in
interfaces
:
self
.
facts
[
iface
]
=
interfaces
[
iface
]
self
.
facts
[
'all_ipv4_addresses'
]
=
ips
[
'all_ipv4'
]
self
.
facts
[
'all_ipv6_addresses'
]
=
ips
[
'all_ipv6'
]
self
.
facts
[
'ipv4_address'
]
=
ips
[
'ipv4'
]
self
.
facts
[
'ipv6_address'
]
=
ips
[
'ipv6'
]
return
self
.
facts
return
self
.
facts
# get list of interfaces
def
get_interfaces
(
self
):
def
parse_ip_addr
(
self
):
names
=
[]
interfaces
=
{}
data
=
get_file_content
(
'/proc/net/dev'
)
ips
=
{
'all_ipv4'
:
[],
'all_ipv6'
:
[],
'ipv4'
:
None
,
'ipv6'
:
None
}
# Format of /proc/net/dev is:
# Inter-| Receive ...
output
=
subprocess
.
Popen
([
'ip'
,
'addr'
],
stdout
=
subprocess
.
PIPE
)
.
communicate
()[
0
]
# face |bytes ...
for
line
in
output
.
split
(
'
\n
'
):
# lo: 595059
if
line
:
for
line
in
data
.
split
(
'
\n
'
):
words
=
line
.
split
()
if
':'
in
line
:
if
not
line
.
startswith
(
' '
):
names
.
append
(
line
.
split
(
':'
)[
0
]
.
strip
())
device
=
words
[
1
][
0
:
-
1
]
return
names
mtu
=
words
[
4
]
elif
words
[
0
]
.
startswith
(
'link/'
):
def
get_iface_hwaddr
(
self
,
iface
):
iface_type
=
words
[
0
]
.
split
(
'/'
)[
1
]
data
=
get_file_content
(
'/sys/class/net/
%
s/address'
%
iface
)
if
iface_type
==
'void'
:
if
data
is
None
:
macaddress
=
'unknown'
return
'unknown'
else
:
else
:
return
data
.
strip
()
macaddress
=
words
[
1
]
elif
words
[
0
]
==
'inet'
:
def
get_interface_facts
(
self
):
address
,
netmask_length
=
words
[
1
]
.
split
(
'/'
)
for
iface
in
self
.
facts
[
'interfaces'
]:
address_bin
=
struct
.
unpack
(
'!L'
,
socket
.
inet_aton
(
address
))[
0
]
if
iface
not
in
self
.
facts
:
self
.
facts
[
iface
]
=
{}
netmask_bin
=
(
1
<<
32
)
-
(
1
<<
32
>>
int
(
netmask_length
))
self
.
facts
[
iface
]
=
{
'macaddress'
:
self
.
get_iface_hwaddr
(
iface
)
}
netmask
=
socket
.
inet_ntoa
(
struct
.
pack
(
'!L'
,
netmask_bin
))
if
os
.
path
.
exists
(
'/sys/class/net/
%
s/mtu'
%
iface
):
mtu
=
get_file_content
(
'/sys/class/net/
%
s/mtu'
%
iface
)
network
=
socket
.
inet_ntoa
(
struct
.
pack
(
'!L'
,
address_bin
&
netmask_bin
))
self
.
facts
[
iface
][
'mtu'
]
=
mtu
.
strip
()
iface
=
words
[
-
1
]
def
get_ipv4_facts
(
self
):
# If an interface has multiple IPv4 addresses, make up an
for
iface
in
self
.
facts
[
'interfaces'
]:
# interface name for each address
# This is lame, but there doesn't appear to be a good way
if
iface
in
interfaces
:
# to get all addresses for both IPv4 and IPv6.
i
=
0
cmd
=
subprocess
.
Popen
(
"env LANG=
\"\"
/sbin/ifconfig
%
s"
%
iface
,
shell
=
True
,
while
'{0}:{1}'
.
format
(
iface
,
i
)
in
interfaces
:
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
)
i
+=
1
out
,
err
=
cmd
.
communicate
()
iface
=
'{0}:{1}'
.
format
(
iface
,
i
)
for
line
in
out
.
split
(
'
\n
'
):
is_ipv4
=
False
interfaces
[
iface
]
=
{}
data
=
line
.
split
()
interfaces
[
iface
][
'macaddress'
]
=
macaddress
if
'inet addr'
in
line
:
interfaces
[
iface
][
'mtu'
]
=
mtu
if
'ipv4'
not
in
self
.
facts
[
iface
]:
interfaces
[
iface
][
'device'
]
=
device
self
.
facts
[
iface
][
'ipv4'
]
=
{}
interfaces
[
iface
][
'ipv4'
]
=
{
'address'
:
address
,
is_ipv4
=
True
'netmask'
:
netmask
,
self
.
facts
[
iface
][
'ipv4'
]
=
{
'address'
:
data
[
1
]
.
split
(
':'
)[
1
],
'network'
:
network
}
'netmask'
:
data
[
-
1
]
.
split
(
':'
)[
1
]
}
# Slightly different output in net-tools-1.60-134.20120127git
ips
[
'all_ipv4'
]
.
append
(
address
)
# Looks like
if
not
ips
[
'ipv4'
]
or
ips
[
'ipv4'
]
.
startswith
(
'127'
):
# inet 192.168.1.2 netmask 255.255.255.0 broadcast 192.168.1.255
ips
[
'ipv4'
]
=
address
elif
'inet '
in
line
:
is_ipv4
=
True
elif
words
[
0
]
==
'inet6'
:
if
'ipv4'
not
in
self
.
facts
[
iface
]:
address
,
prefix
=
words
[
1
]
.
split
(
'/'
)
self
.
facts
[
iface
][
'ipv4'
]
=
{}
scope
=
words
[
3
]
self
.
facts
[
iface
][
'ipv4'
]
=
{
'address'
:
data
[
1
],
'netmask'
:
data
[
3
]
}
iface
=
device
if
is_ipv4
:
if
iface
not
in
interfaces
:
ip
=
struct
.
unpack
(
"!L"
,
socket
.
inet_aton
(
self
.
facts
[
iface
][
'ipv4'
][
'address'
]))[
0
]
interfaces
[
iface
]
=
{}
mask
=
struct
.
unpack
(
"!L"
,
socket
.
inet_aton
(
self
.
facts
[
iface
][
'ipv4'
][
'netmask'
]))[
0
]
interfaces
[
iface
][
'macaddress'
]
=
macaddress
self
.
facts
[
iface
][
'ipv4'
][
'network'
]
=
socket
.
inet_ntoa
(
struct
.
pack
(
"!L"
,
ip
&
mask
))
interfaces
[
iface
][
'mtu'
]
=
mtu
interfaces
[
iface
][
'device'
]
=
device
def
get_ipv6_facts
(
self
):
if
'ipv6'
not
in
interfaces
[
iface
]:
if
not
socket
.
has_ipv6
:
interfaces
[
iface
][
'ipv6'
]
=
[]
return
interfaces
[
iface
][
'ipv6'
]
.
append
(
{
data
=
get_file_content
(
'/proc/net/if_inet6'
)
'address'
:
address
,
if
data
is
None
:
return
for
line
in
data
.
split
(
'
\n
'
):
l
=
line
.
split
()
iface
=
l
[
5
]
if
'ipv6'
not
in
self
.
facts
[
iface
]:
self
.
facts
[
iface
][
'ipv6'
]
=
[]
scope
=
l
[
3
]
if
Network
.
IPV6_SCOPE
.
has_key
(
l
[
3
]):
scope
=
Network
.
IPV6_SCOPE
[
l
[
3
]]
prefix
=
int
(
l
[
2
],
16
)
str_addr
=
':'
.
join
(
[
l
[
0
][
i
:
i
+
4
]
for
i
in
range
(
0
,
len
(
l
[
0
]),
4
)
]
)
# Normalize ipv6 address from format in /proc/net/if_inet6
addr
=
socket
.
inet_ntop
(
socket
.
AF_INET6
,
socket
.
inet_pton
(
socket
.
AF_INET6
,
str_addr
))
self
.
facts
[
iface
][
'ipv6'
]
.
append
(
{
'address'
:
addr
,
'prefix'
:
prefix
,
'prefix'
:
prefix
,
'scope'
:
scope
}
)
'scope'
:
scope
}
)
ips
[
'all_ipv6'
]
.
append
(
address
)
if
not
ips
[
'ipv6'
]
or
ips
[
'ipv6'
]
==
'::1'
:
ips
[
'ipv6'
]
=
address
return
interfaces
,
ips
class
Virtual
(
Facts
):
class
Virtual
(
Facts
):
"""
"""
...
...
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