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
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
80 additions
and
85 deletions
+80
-85
library/setup
+80
-85
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
:
macaddress
=
words
[
1
]
return
data
.
strip
()
elif
words
[
0
]
==
'inet'
:
address
,
netmask_length
=
words
[
1
]
.
split
(
'/'
)
def
get_interface_facts
(
self
):
address_bin
=
struct
.
unpack
(
'!L'
,
socket
.
inet_aton
(
address
))[
0
]
for
iface
in
self
.
facts
[
'interfaces'
]:
if
iface
not
in
self
.
facts
:
netmask_bin
=
(
1
<<
32
)
-
(
1
<<
32
>>
int
(
netmask_length
))
self
.
facts
[
iface
]
=
{}
netmask
=
socket
.
inet_ntoa
(
struct
.
pack
(
'!L'
,
netmask_bin
))
self
.
facts
[
iface
]
=
{
'macaddress'
:
self
.
get_iface_hwaddr
(
iface
)
}
if
os
.
path
.
exists
(
'/sys/class/net/
%
s/mtu'
%
iface
):
network
=
socket
.
inet_ntoa
(
struct
.
pack
(
'!L'
,
address_bin
&
netmask_bin
))
mtu
=
get_file_content
(
'/sys/class/net/
%
s/mtu'
%
iface
)
self
.
facts
[
iface
][
'mtu'
]
=
mtu
.
strip
()
iface
=
words
[
-
1
]
# If an interface has multiple IPv4 addresses, make up an
def
get_ipv4_facts
(
self
):
# interface name for each address
for
iface
in
self
.
facts
[
'interfaces'
]:
if
iface
in
interfaces
:
# This is lame, but there doesn't appear to be a good way
i
=
0
# to get all addresses for both IPv4 and IPv6.
while
'{0}:{1}'
.
format
(
iface
,
i
)
in
interfaces
:
cmd
=
subprocess
.
Popen
(
"env LANG=
\"\"
/sbin/ifconfig
%
s"
%
iface
,
shell
=
True
,
i
+=
1
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
)
iface
=
'{0}:{1}'
.
format
(
iface
,
i
)
out
,
err
=
cmd
.
communicate
()
for
line
in
out
.
split
(
'
\n
'
):
interfaces
[
iface
]
=
{}
is_ipv4
=
False
interfaces
[
iface
][
'macaddress'
]
=
macaddress
data
=
line
.
split
()
interfaces
[
iface
][
'mtu'
]
=
mtu
if
'inet addr'
in
line
:
interfaces
[
iface
][
'device'
]
=
device
if
'ipv4'
not
in
self
.
facts
[
iface
]:
interfaces
[
iface
][
'ipv4'
]
=
{
'address'
:
address
,
self
.
facts
[
iface
][
'ipv4'
]
=
{}
'netmask'
:
netmask
,
is_ipv4
=
True
'network'
:
network
}
self
.
facts
[
iface
][
'ipv4'
]
=
{
'address'
:
data
[
1
]
.
split
(
':'
)[
1
],
'netmask'
:
data
[
-
1
]
.
split
(
':'
)[
1
]
}
ips
[
'all_ipv4'
]
.
append
(
address
)
# Slightly different output in net-tools-1.60-134.20120127git
if
not
ips
[
'ipv4'
]
or
ips
[
'ipv4'
]
.
startswith
(
'127'
):
# Looks like
ips
[
'ipv4'
]
=
address
# inet 192.168.1.2 netmask 255.255.255.0 broadcast 192.168.1.255
elif
'inet '
in
line
:
elif
words
[
0
]
==
'inet6'
:
is_ipv4
=
True
address
,
prefix
=
words
[
1
]
.
split
(
'/'
)
if
'ipv4'
not
in
self
.
facts
[
iface
]:
scope
=
words
[
3
]
self
.
facts
[
iface
][
'ipv4'
]
=
{}
self
.
facts
[
iface
][
'ipv4'
]
=
{
'address'
:
data
[
1
],
iface
=
device
'netmask'
:
data
[
3
]
}
if
iface
not
in
interfaces
:
if
is_ipv4
:
interfaces
[
iface
]
=
{}
ip
=
struct
.
unpack
(
"!L"
,
socket
.
inet_aton
(
self
.
facts
[
iface
][
'ipv4'
][
'address'
]))[
0
]
interfaces
[
iface
][
'macaddress'
]
=
macaddress
mask
=
struct
.
unpack
(
"!L"
,
socket
.
inet_aton
(
self
.
facts
[
iface
][
'ipv4'
][
'netmask'
]))[
0
]
interfaces
[
iface
][
'mtu'
]
=
mtu
self
.
facts
[
iface
][
'ipv4'
][
'network'
]
=
socket
.
inet_ntoa
(
struct
.
pack
(
"!L"
,
ip
&
mask
))
interfaces
[
iface
][
'device'
]
=
device
if
'ipv6'
not
in
interfaces
[
iface
]:
def
get_ipv6_facts
(
self
):
interfaces
[
iface
][
'ipv6'
]
=
[]
if
not
socket
.
has_ipv6
:
interfaces
[
iface
][
'ipv6'
]
.
append
(
{
return
'address'
:
address
,
data
=
get_file_content
(
'/proc/net/if_inet6'
)
'prefix'
:
prefix
,
if
data
is
None
:
'scope'
:
scope
}
)
return
for
line
in
data
.
split
(
'
\n
'
):
ips
[
'all_ipv6'
]
.
append
(
address
)
l
=
line
.
split
()
if
not
ips
[
'ipv6'
]
or
ips
[
'ipv6'
]
==
'::1'
:
iface
=
l
[
5
]
ips
[
'ipv6'
]
=
address
if
'ipv6'
not
in
self
.
facts
[
iface
]:
self
.
facts
[
iface
][
'ipv6'
]
=
[]
return
interfaces
,
ips
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
,
'scope'
:
scope
}
)
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