Browse Source

Remove `import dbus` use pydbus instead.

develop
Sam Black 2 years ago
parent
commit
dc39192aab
2 changed files with 36 additions and 35 deletions
  1. +20
    -21
      sponson/container/firewall.py
  2. +16
    -14
      sponson/systemd/__init__.py

+ 20
- 21
sponson/container/firewall.py View File

@@ -16,11 +16,12 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import dbus
import os.path
import socket
import time

from pydbus import SystemBus

import sponson.configfile
from sponson.constants import DNSMASQ_LEASES_FILE
from sponson.constants import ETC_CONTAINER_CONF_DIR
@@ -50,13 +51,13 @@ class Firewall(object):
:param runtime_config: network runtime configuration.
:type runtime_config: dict or None.
"""
self.bus = dbus.SystemBus()
self.proxy = self.bus.get_object("org.fedoraproject.FirewallD1",
"/org/fedoraproject/FirewallD1")
self.firewalld_zone = dbus.Interface(
self.proxy, "org.fedoraproject.FirewallD1.zone")
self.firewalld = dbus.Interface(self.proxy,
"org.fedoraproject.FirewallD1")
self.bus = SystemBus()
self.firewalld_proxy = self.bus.get("org.fedoraproject.FirewallD1",
"/org/fedoraproject/FirewallD1")
self.firewalld_zone = self.firewalld_proxy[
"org.fedoraproject.FirewallD1.zone"]
self.firewalld = self.firewalld_proxy[
"org.fedoraproject.FirewallD1"]

if runtime_config:
self.default_zone = runtime_config.get(
@@ -64,20 +65,18 @@ class Firewall(object):
else:
self.default_zone = self.firewalld.getDefaultZone()

self.config_proxy = self.bus.get_object(
self.firewalld_config = self.bus.get(
"org.fedoraproject.FirewallD1",
"/org/fedoraproject/FirewallD1/config")
self.config_interface = dbus.Interface(
self.config_proxy, "org.fedoraproject.FirewallD1.config")
"/org/fedoraproject/FirewallD1/config")[
"org.fedoraproject.FirewallD1.config"]

default_zone_object_path = self.config_interface.getZoneByName(
default_zone_object_path = self.firewalld_config.getZoneByName(
self.default_zone)

self.default_zone_config_proxy = self.bus.get_object(
"org.fedoraproject.FirewallD1", default_zone_object_path)
self.default_zone_config = dbus.Interface(
self.default_zone_config_proxy,
"org.fedoraproject.FirewallD1.config.zone")
self.firewalld_default_zone_config = self.bus.get(
"org.fedoraproject.FirewallD1",
default_zone_object_path)[
"org.fedoraproject.FirewallD1.config.zone"]

def config_string_to_dict(self, config):
"""
@@ -162,7 +161,7 @@ class Firewall(object):
"""
Create the bridge firewall zone.
"""
self.config_interface.addZone(FIREWALL_ZONE, FIREWALL_ZONE_CONF)
self.firewalld_config.addZone(FIREWALL_ZONE, FIREWALL_ZONE_CONF)

self.firewalld.reload()

@@ -175,8 +174,8 @@ class Firewall(object):
richrule):
self.firewalld_zone.addRichRule(self.default_zone, richrule, 0)

if not self.default_zone_config.queryRichRule(richrule):
self.default_zone_config.addRichRule(richrule)
if not self.firewalld_default_zone_config.queryRichRule(richrule):
self.firewalld_default_zone_config.addRichRule(richrule)

def sanity_checks(self):
"""


+ 16
- 14
sponson/systemd/__init__.py View File

@@ -16,12 +16,14 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import dbus
import os
import os.path
import subprocess
import time

from gi.repository import GLib
from pydbus import SystemBus

from sponson.constants import BASE_DIR
from sponson.constants import DNSMASQ_RESOLV_PATH
from sponson.constants import ETC_DNSMASQ_FILE
@@ -59,11 +61,11 @@ class Systemd(object):
:param runtime_config: runtime configuration
:type runtime_config: dict
"""
self.bus = dbus.SystemBus()
self.proxy = self.bus.get_object("org.freedesktop.systemd1",
"/org/freedesktop/systemd1")
self.manager = dbus.Interface(self.proxy,
"org.freedesktop.systemd1.Manager")
self.bus = SystemBus()
self.manager = self.bus.get(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1")[
"org.freedesktop.systemd1.Manager"]

self.version = self._version()

@@ -132,18 +134,18 @@ class Systemd(object):
# the second is otherwise.
try:
unit_obj_path = self.manager.GetUnit(unit_name)
except dbus.DBusException:
except GLib.Error:
try:
unit_obj_path = self.manager.LoadUnit(unit_name)
except dbus.DBusException:
except GLib.Error:
raise SystemdException(
"Cannot connect to {}".format(unit_name))

dbus_obj = self.bus.get_object("org.freedesktop.systemd1",
unit_obj_path)
obj_props = dbus.Interface(dbus_obj, "org.freedesktop.DBus.Properties")
dbus_obj = self.bus.get(
"org.freedesktop.systemd1",
unit_obj_path)["org.freedesktop.DBus.Properties"]

return obj_props
return dbus_obj

def mount_create_unit(self, what, where, mount_type, options,
automount=True, requires=None, permissions=None):
@@ -268,12 +270,12 @@ class Systemd(object):
for unit in units:
try:
self.manager.StopUnit(unit, "replace")
except dbus.DBusException:
except GLib.Error:
logger.warning("Did not stop unit {}".format(unit))

try:
self.manager.DisableUnitFiles(units, False)
except dbus.DBusException:
except GLib.Error:
logger.exception("Cannot disable given units")

def remove_units(self, units):


Loading…
Cancel
Save