Skip to content
Snippets Groups Projects
Commit ed2e6294 authored by Nicolas KAROLAK's avatar Nicolas KAROLAK
Browse files

switch to pydbus

parent f794af3c
No related branches found
No related tags found
No related merge requests found
...@@ -7,6 +7,7 @@ PACKAGES = [ ...@@ -7,6 +7,7 @@ PACKAGES = [
"python3-defusedxml", "python3-defusedxml",
"python3-openssl", "python3-openssl",
"python3-psutil", "python3-psutil",
"python3-pydbus",
"python3-requests", "python3-requests",
"python3-spf", "python3-spf",
] ]
...@@ -14,13 +15,16 @@ PACKAGES = [ ...@@ -14,13 +15,16 @@ PACKAGES = [
def main(): def main():
for pkg in PACKAGES: for pkg in PACKAGES:
if run( if (
["/usr/bin/dpkg", "-s", pkg], run(
shell=False, ["/usr/bin/dpkg", "-s", pkg],
stdout=DEVNULL, shell=False,
stderr=DEVNULL, stdout=DEVNULL,
stdin=PIPE, stderr=DEVNULL,
).returncode != 0: stdin=PIPE,
).returncode
!= 0
):
result = run( result = run(
["/usr/bin/apt", "install", "-y", pkg], ["/usr/bin/apt", "install", "-y", pkg],
shell=False, shell=False,
......
...@@ -5,8 +5,8 @@ Criticality: Normal ...@@ -5,8 +5,8 @@ Criticality: Normal
Checks that DNS records are provided by the customer servers are correctly set Checks that DNS records are provided by the customer servers are correctly set
""" """
import dbus
from pathlib import Path from pathlib import Path
import pydbus
import re import re
import subprocess import subprocess
import sys import sys
...@@ -23,20 +23,19 @@ def get_dns_servers() -> set: ...@@ -23,20 +23,19 @@ def get_dns_servers() -> set:
# dbus method # dbus method
try: try:
bus = dbus.SystemBus() bus = pydbus.SystemBus()
proxy = bus.get_object( bus_client = bus.get("org.freedesktop.resolve1", "/org/freedesktop/resolve1")
bus_name="org.freedesktop.resolve1", object_path="/org/freedesktop/resolve1" servers.extend(
) [".".join(map(str, dns[2])) for dns in bus_client.DNS if dns[1] == 2]
iface = dbus.Interface( ) # IPv4
object=proxy, dbus_interface="org.freedesktop.DBus.Properties" servers.extend(
) [":".join(map(str, dns[2])) for dns in bus_client.DNS if dns[1] == 10]
data = iface.Get("org.freedesktop.resolve1.Manager", "DNS") ) # IPv6
for dns in iface.Get("org.freedesktop.resolve1.Manager", "DNS"):
delimiter = ":" if len(dns[2]) == 16 else "." # if IPv6 else IPv4
servers.append(delimiter.join([str(int(o)) for o in dns[2]]))
except Exception as dbus_err: except Exception as dbus_err:
u.info("DBus method failed: {}".format(dbus_err)) u.info("DBus method failed: {}".format(dbus_err))
# TODO: remove fallback methods below when the dbus one is confirmed to work everwhere
# network-manager method # network-manager method
if not len(servers) and subprocess.getstatusoutput("command -v nmcli")[0] == 0: if not len(servers) and subprocess.getstatusoutput("command -v nmcli")[0] == 0:
_, output = subprocess.getstatusoutput( _, output = subprocess.getstatusoutput(
...@@ -122,6 +121,9 @@ def check_resolver(conf: dict, resolvers: set, ip: str) -> tuple: ...@@ -122,6 +121,9 @@ def check_resolver(conf: dict, resolvers: set, ip: str) -> tuple:
def main(): def main():
print("Check DNS settings:") print("Check DNS settings:")
if not u.supported_platform():
u.info("Platform not supported")
exit(2)
warnings = 0 warnings = 0
errors = 0 errors = 0
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
"""EnvSetup utilities.""" """EnvSetup utilities."""
from collections import OrderedDict from collections import OrderedDict
from configparser import ConfigParser
from pathlib import Path from pathlib import Path
import re import re
import socket import socket
...@@ -22,6 +23,32 @@ CONF_PATH = "conf.sh" ...@@ -22,6 +23,32 @@ CONF_PATH = "conf.sh"
_conf_cache = None _conf_cache = None
SUPPORTED_PLATFORMS = (("debian", "10"), ("ubuntu", "18.04"))
def dist() -> tuple:
"""Return distribution name and version).
:return: Distribution name and version
:rtype: tuple
"""
parser = ConfigParser()
with open("/etc/os-release") as os_release:
parser.read_string("[os]\n{}".format(os_release.read()))
return (parser["os"]["ID"], parser["os"]["VERSION_ID"].strip('"'))
def supported_platform() -> bool:
"""Let you know if the current platform is supported.
:return: Wether the platform is supported or not
:rtype: bool
"""
return dist() in SUPPORTED_PLATFORMS
def log(text: str, error: bool = False): def log(text: str, error: bool = False):
"""Output log message to stout or stderr. """Output log message to stout or stderr.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment