Skip to content
This repository has been archived by the owner on Apr 26, 2020. It is now read-only.

Commit

Permalink
Switch to Admin API
Browse files Browse the repository at this point in the history
  • Loading branch information
kalkin committed May 22, 2017
2 parents 88ca6cb + acb9443 commit c3d0a5c
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 42 deletions.
2 changes: 1 addition & 1 deletion dbus-1/services/org.qubes.DomainManager1.service
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[D-BUS Service]
Name=org.qubes.DomainManager1
Exec=/usr/bin/python /usr/lib/python2.7/site-packages/qubesdbus/domain_manager.py
Exec=/usr/bin/python3 -m qubesdbus.domain_manager
2 changes: 1 addition & 1 deletion dbus-1/services/org.qubes.Labels1.service
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[D-BUS Service]
Name=org.qubes.Labels1
Exec=/usr/bin/python /usr/lib/python2.7/site-packages/qubesdbus/labels.py
Exec=/usr/bin/python3 -m qubesdbus.labels
1 change: 0 additions & 1 deletion qubes

This file was deleted.

4 changes: 2 additions & 2 deletions qubesdbus/device_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import pprint
import sys

import qubes
import qubesadmin
import systemd.journal
from gi.repository import GLib

Expand Down Expand Up @@ -60,7 +60,7 @@ def __init__(self, bus, bus_name, bus_path, data):
def main(args=None): # pylint: disable=unused-argument
''' Main function starting the DomainManager1 service. '''
loop = GLib.MainLoop()
app = qubes.Qubes()
app = qubesadmin.Qubes()
app.domains[0].devices[
'pci'].available() # HACK this populates dom0 devices
devices = qubesdbus.serialize.devices_data(app)
Expand Down
6 changes: 3 additions & 3 deletions qubesdbus/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
''' D-Bus Domain object '''

import dbus.service
import qubes
import qubesadmin

import qubesdbus.service

Expand Down Expand Up @@ -62,7 +62,7 @@ def StartingSignal(self, name):

@dbus.service.method("org.qubes.Domain", out_signature="b")
def Shutdown(self):
app = qubes.Qubes()
app = qubesadmin.Qubes()
name = str(self.name)
vm = app.domains[name]
vm.shutdown(wait=True)
Expand All @@ -71,7 +71,7 @@ def Shutdown(self):

@dbus.service.method("org.qubes.Domain", out_signature="b")
def Start(self):
app = qubes.Qubes()
app = qubesadmin.Qubes()
name = str(self.name)
vm = app.domains[name]
vm.start()
Expand Down
4 changes: 2 additions & 2 deletions qubesdbus/domain_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import sys

import dbus.service
import qubes
import qubesadmin
from gi.repository import GLib
from systemd.journal import JournalHandler

Expand Down Expand Up @@ -119,7 +119,7 @@ def _proxify_domain(self, vm):
def main(args=None): # pylint: disable=unused-argument
''' Main function starting the DomainManager1 service. '''
loop = GLib.MainLoop()
app = qubes.Qubes()
app = qubesadmin.Qubes()
data = qubesdbus.serialize.qubes_data(app)
domains = [qubesdbus.serialize.domain_data(vm) for vm in app.domains]
_ = DomainManager(data, domains)
Expand Down
6 changes: 3 additions & 3 deletions qubesdbus/labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import logging
import sys

import qubes
import qubesadmin
from gi.repository import GLib
from systemd.journal import JournalHandler

Expand Down Expand Up @@ -65,10 +65,10 @@ def __init__(self, bus, bus_name, bus_path, data):
def main(args=None):
''' Main function ''' # pylint: disable=unused-argument
loop = GLib.MainLoop()
app = qubes.Qubes()
app = qubesadmin.Qubes()

labels_data = [qubesdbus.serialize.label_data(label)
for label in app.labels.values()]
for label in app.labels]
_ = Labels(labels_data)
print("Service running...")
loop.run()
Expand Down
37 changes: 30 additions & 7 deletions qubesdbus/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@

from __future__ import absolute_import

import asyncio
import logging

import dbus
from qubes import Qubes
from qubes.ext import Extension, handler
from qubes.vm.qubesvm import QubesVM
from qubesadmin import Qubes
from qubesadmin.events import EventsDispatcher
from qubesadmin.vm import QubesVM
from systemd.journal import JournalHandler

import qubesdbus.serialize
Expand Down Expand Up @@ -62,15 +63,24 @@ def is_garbage(event):
return False


class QubesDbusProxy(Extension):
class QubesDbusProxy(object):
# pylint: disable=too-few-public-methods,no-self-use
def __init__(self):
super(QubesDbusProxy, self).__init__()
self.domains = {} # type: Dict[int, bool]
self.new_vm = [] # type: List[QubesVM]
self.new_vm = [] # type: List[QubesVM]
self.app = Qubes()
self._events_dispatcher = EventsDispatcher(self.app)
self._events_dispatcher.add_handler('*', self.forward_vm_event)
self._events_dispatcher.add_handler('*', self.forward_app_event)

@asyncio.coroutine
def run(self):
yield from self._events_dispatcher.listen_for_events()

@handler('*')
def forward_vm_event(self, vm, event, *args, **kwargs):
if vm is None:
return
if is_garbage(event):
log.debug('Drop %s from %s', event, vm)
return
Expand Down Expand Up @@ -101,8 +111,9 @@ def forward_vm_event(self, vm, event, *args, **kwargs):
else:
log.warn('Unknown %s from %s %s %s', event, vm, args, kwargs)

@handler('*', system=True)
def forward_app_event(self, vm, event, *args, **kwargs):
if vm is not None:
return
proxy = app_proxy()
if is_garbage(event):
log.debug('Drop %s from %s', event, vm)
Expand Down Expand Up @@ -163,3 +174,15 @@ def get_proxy(obj):
else:
log.error("Unknown sender object %s", obj)
return


def main():
proxy = QubesDbusProxy()
loop = asyncio.get_event_loop()
loop.run_until_complete(proxy.run())
loop.stop()
loop.run_forever()
loop.close()

if __name__ == '__main__':
main()
26 changes: 13 additions & 13 deletions qubesdbus/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
import re

import dbus
import qubes
import qubes.vm.qubesvm

from libvirt import libvirtError # pylint: disable=import-error
import qubesadmin
import qubesadmin.devices as devices
import qubesadmin.vm

try:
# Check mypy dependencies. pylint: disable=ungrouped-imports,unused-import
from typing import Any, Callable, Tuple, List, Dict
from qubes.vm.qubesvm import QubesVM
from qubes import Qubes, Label
from qubesadmin.vm import QubesVM
from qubesadmin import Qubes
from qubesadmin.label import Label
except ImportError:
pass

Expand All @@ -46,7 +46,7 @@ def qubes_data(app):
# type: (Qubes) -> Dict[dbus.String, Any]
result = {}
for prop in app.property_list():
name = prop.__name__
name = str(prop)
key = dbus.String(name)
try:
result[key] = serialize_val(getattr(app, name))
Expand All @@ -61,10 +61,10 @@ def domain_data(vm):
# type: (QubesVM) -> Dict[dbus.String, Any]
result = dbus.Dictionary({}, signature='sv')
for prop in vm.property_list():
name = prop.__name__
name = str(prop)
try:
value = serialize_val(getattr(vm, name))
except (AttributeError, libvirtError):
except AttributeError:
value = dbus.String('')
result[name] = value

Expand Down Expand Up @@ -110,13 +110,13 @@ def serialize_val(value):
return dbus.Int32(value)
elif callable(value):
return serialize_val(value())
elif isinstance(value, qubes.Label):
elif isinstance(value, qubesadmin.label.Label):
return label_path(value)
elif isinstance(value, qubes.vm.qubesvm.QubesVM):
elif isinstance(value, qubesadmin.vm.QubesVM):
return domain_path(value)
elif isinstance(value, qubes.devices.DeviceCollection):
elif isinstance(value, devices.DeviceCollection):
return dbus.Array(device_collection_data(value), signature='a{sv}')
elif isinstance(value, qubes.devices.DeviceInfo):
elif isinstance(value, devices.DeviceInfo):
return dbus.Dictionary(device_data(value), signature='sv')
elif isinstance(value, re._pattern_type):
return dbus.String(value.pattern)
Expand Down
3 changes: 1 addition & 2 deletions qubesdbus/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ def __init__(self, bus=None, bus_name=None, bus_path=None):
_name = ''.join([NAME_PREFIX, '.', self.__class__.__name__,
str(VERSION)])
self.bus_name = dbus.service.BusName(_name, self.bus)
# avoid pylint super-on-old-class error
dbus.service.Object.__init__(self, self.bus_name, self.bus_path)
super(DbusServiceObject, self).__init__(self.bus_name, self.bus_path)


class ObjectManager(DbusServiceObject):
Expand Down
7 changes: 4 additions & 3 deletions rpm_spec/qubes-dbus.spec
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ Vendor: Invisible Things Lab
License: GPL2+
URL: http://www.qubes-os.org

Requires: python-dbus
Requires: python3-dbus
Requires: python3-systemd

%define _builddir %(pwd)

Expand All @@ -44,11 +45,11 @@ ln -sf . %{name}-%{version}
foo bar

%build
/usr/bin/python setup.py build
/usr/bin/python3 setup.py build

%install

python setup.py install --single-version-externally-managed -O1 --root=$RPM_BUILD_ROOT --record=INSTALLED_FILES
python3 setup.py install --single-version-externally-managed -O1 --root=$RPM_BUILD_ROOT --record=INSTALLED_FILES
mkdir -p $RPM_BUILD_ROOT/usr/share
cp -r dbus-1 $RPM_BUILD_ROOT/usr/share/dbus-1

Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
url='https://www.qubes-os.org/',
packages=['qubesdbus'],
entry_points={
'qubes.ext': [
'qubesdbus = qubesdbus:QubesDbusProxy'
'console_scripts': [
'qubes-dbus-proxy = qubesdbus.proxy:main'
]
})
4 changes: 2 additions & 2 deletions stubs/qubes/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import (List)
import qubes.vm
import qubesadmin.vm
class Qubes(object):
domains = ... # type: List[qubes.vm.BaseVM]
domains = ... # type: List[qubesadmin.vm.QubesVM]
labels = ... # type: Dict[str,qubes.Label]

class Label(object):
Expand Down

0 comments on commit c3d0a5c

Please sign in to comment.