Merge remote-tracking branch 'origin/pr/108'
* origin/pr/108: app: switch get_local_name method to property tests: qvm_start_gui: make PEP8 happier tests: fix with respect to gui properties app: get_local_name set/from self attribute app: fix missing docstring for get_local_name qvm-start-gui: simplify log info for start_gui function guivm: use getattr instead of try/except and direct property access qvm-start-gui: handle GuiVM Make PEP8 happier
This commit is contained in:
commit
33f04bb49a
@ -164,6 +164,7 @@ class QubesBase(qubesadmin.base.PropertyHolder):
|
||||
#: cache for available storage pool drivers and options to create them
|
||||
self._pool_drivers = None
|
||||
self.log = logging.getLogger('app')
|
||||
self._local_name = None
|
||||
|
||||
def list_vmclass(self):
|
||||
"""Call Qubesd in order to obtain the vm classes list"""
|
||||
@ -226,6 +227,14 @@ class QubesBase(qubesadmin.base.PropertyHolder):
|
||||
""" Remove a storage pool """
|
||||
self.qubesd_call('dom0', 'admin.pool.Remove', name, None)
|
||||
|
||||
@property
|
||||
def local_name(self):
|
||||
""" Get localhost name """
|
||||
if not self._local_name:
|
||||
self._local_name = os.uname()[1]
|
||||
|
||||
return self._local_name
|
||||
|
||||
def get_label(self, label):
|
||||
"""Get label as identified by index or name
|
||||
|
||||
|
@ -1 +0,0 @@
|
||||
/home/user/qubes-builder/qubes-src/core-admin-client/qubesadmin
|
@ -54,7 +54,8 @@ class TC_00_qvm_start_gui(qubesadmin.tests.QubesTestCase):
|
||||
args = self.launcher.kde_guid_args(self.app.domains['test-vm'])
|
||||
self.assertEqual(args, ['-T', '-p',
|
||||
'_KDE_NET_WM_COLOR_SCHEME=s:' +
|
||||
os.path.expanduser('~/.local/share/qubes-kde/red.colors')])
|
||||
os.path.expanduser(
|
||||
'~/.local/share/qubes-kde/red.colors')])
|
||||
|
||||
self.assertAllCalled()
|
||||
|
||||
@ -275,9 +276,11 @@ class TC_00_qvm_start_gui(qubesadmin.tests.QubesTestCase):
|
||||
|
||||
patch_proc = unittest.mock.patch('asyncio.create_subprocess_exec')
|
||||
patch_args = unittest.mock.patch.object(self.launcher,
|
||||
'common_guid_args', lambda vm: [])
|
||||
'common_guid_args',
|
||||
lambda vm: [])
|
||||
patch_pidfile = unittest.mock.patch.object(self.launcher,
|
||||
'guid_pidfile', lambda vm: pidfile.name)
|
||||
'guid_pidfile',
|
||||
lambda vm: pidfile.name)
|
||||
try:
|
||||
mock_proc = patch_proc.start()
|
||||
patch_args.start()
|
||||
@ -315,7 +318,8 @@ class TC_00_qvm_start_gui(qubesadmin.tests.QubesTestCase):
|
||||
b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00'
|
||||
proc_mock = unittest.mock.Mock()
|
||||
with unittest.mock.patch('asyncio.create_subprocess_exec',
|
||||
lambda *args: self.mock_coroutine(proc_mock, *args)):
|
||||
lambda *args: self.mock_coroutine(proc_mock,
|
||||
*args)):
|
||||
with unittest.mock.patch.object(self.launcher,
|
||||
'common_guid_args', lambda vm: []):
|
||||
loop.run_until_complete(self.launcher.start_gui_for_stubdomain(
|
||||
@ -348,7 +352,8 @@ class TC_00_qvm_start_gui(qubesadmin.tests.QubesTestCase):
|
||||
b'0\x001'
|
||||
proc_mock = unittest.mock.Mock()
|
||||
with unittest.mock.patch('asyncio.create_subprocess_exec',
|
||||
lambda *args: self.mock_coroutine(proc_mock, *args)):
|
||||
lambda *args: self.mock_coroutine(proc_mock,
|
||||
*args)):
|
||||
with unittest.mock.patch.object(self.launcher,
|
||||
'common_guid_args', lambda vm: []):
|
||||
loop.run_until_complete(self.launcher.start_gui_for_stubdomain(
|
||||
@ -369,7 +374,8 @@ class TC_00_qvm_start_gui(qubesadmin.tests.QubesTestCase):
|
||||
|
||||
self.app.expected_calls[
|
||||
('dom0', 'admin.vm.List', None, None)] = \
|
||||
b'0\x00test-vm class=AppVM state=Running\n'
|
||||
b'0\x00test-vm class=AppVM state=Running\n' \
|
||||
b'gui-vm class=AppVM state=Running'
|
||||
self.app.expected_calls[
|
||||
('test-vm', 'admin.vm.List', None, None)] = \
|
||||
b'0\x00test-vm class=AppVM state=Running\n'
|
||||
@ -389,7 +395,11 @@ class TC_00_qvm_start_gui(qubesadmin.tests.QubesTestCase):
|
||||
self.app.expected_calls[
|
||||
('test-vm', 'admin.vm.property.Get', 'stubdom_xid', None)] = \
|
||||
b'0\x00default=False type=int 3001'
|
||||
self.app.expected_calls[
|
||||
('test-vm', 'admin.vm.property.Get', 'guivm', None)] = \
|
||||
b'0\x00default=False type=vm gui-vm'
|
||||
|
||||
self.app._local_name = 'gui-vm'
|
||||
vm = self.app.domains['test-vm']
|
||||
mock_start_vm = unittest.mock.Mock()
|
||||
mock_start_stubdomain = unittest.mock.Mock()
|
||||
@ -460,7 +470,8 @@ HDMI1 connected 2560x1920+0+0 (normal left inverted right x axis y axis) 372mm x
|
||||
dpi = 150
|
||||
self.assertEqual(qubesadmin.tools.qvm_start_gui.get_monitor_layout(),
|
||||
['2560 1920 0 0 {} {}\n'.format(
|
||||
int(2560/dpi*254/10), int(1920/dpi*254/10))])
|
||||
int(2560 / dpi * 254 / 10),
|
||||
int(1920 / dpi * 254 / 10))])
|
||||
|
||||
@unittest.mock.patch('subprocess.Popen')
|
||||
def test_052_get_monitor_layout_hidpi2(self, proc_mock):
|
||||
@ -471,7 +482,8 @@ HDMI1 connected 2560x1920+0+0 (normal left inverted right x axis y axis) 310mm x
|
||||
dpi = 200
|
||||
self.assertEqual(qubesadmin.tools.qvm_start_gui.get_monitor_layout(),
|
||||
['2560 1920 0 0 {} {}\n'.format(
|
||||
int(2560/dpi*254/10), int(1920/dpi*254/10))])
|
||||
int(2560 / dpi * 254 / 10),
|
||||
int(1920 / dpi * 254 / 10))])
|
||||
|
||||
@unittest.mock.patch('subprocess.Popen')
|
||||
def test_052_get_monitor_layout_hidpi3(self, proc_mock):
|
||||
@ -482,7 +494,8 @@ HDMI1 connected 2560x1920+0+0 (normal left inverted right x axis y axis) 206mm x
|
||||
dpi = 300
|
||||
self.assertEqual(qubesadmin.tools.qvm_start_gui.get_monitor_layout(),
|
||||
['2560 1920 0 0 {} {}\n'.format(
|
||||
int(2560/dpi*254/10), int(1920/dpi*254/10))])
|
||||
int(2560 / dpi * 254 / 10),
|
||||
int(1920 / dpi * 254 / 10))])
|
||||
|
||||
def test_060_send_monitor_layout(self):
|
||||
loop = asyncio.new_event_loop()
|
||||
@ -625,8 +638,9 @@ HDMI1 connected 2560x1920+0+0 (normal left inverted right x axis y axis) 206mm x
|
||||
('dom0', 'admin.vm.List', None, None)] = \
|
||||
b'0\x00test-vm class=AppVM state=Running\n' \
|
||||
b'test-vm2 class=AppVM state=Running\n' \
|
||||
b'test-vm3 class=AppVM state=Runnig\n' \
|
||||
b'test-vm4 class=AppVM state=Halted\n'
|
||||
b'test-vm3 class=AppVM state=Running\n' \
|
||||
b'test-vm4 class=AppVM state=Halted\n' \
|
||||
b'gui-vm class=AppVM state=Running'
|
||||
self.app.expected_calls[
|
||||
('test-vm', 'admin.vm.List', None, None)] = \
|
||||
b'0\x00test-vm class=AppVM state=Running\n'
|
||||
@ -651,6 +665,23 @@ HDMI1 connected 2560x1920+0+0 (normal left inverted right x axis y axis) 206mm x
|
||||
('test-vm3', 'admin.vm.feature.CheckWithTemplate',
|
||||
'gui', None)] = \
|
||||
b'0\x00'
|
||||
self.app.expected_calls[
|
||||
('gui-vm', 'admin.vm.property.Get', 'guivm', None)] = \
|
||||
b'0\x00default=True type=vm '
|
||||
self.app.expected_calls[
|
||||
('test-vm', 'admin.vm.property.Get', 'guivm', None)] = \
|
||||
b'0\x00default=False type=vm gui-vm'
|
||||
self.app.expected_calls[
|
||||
('test-vm2', 'admin.vm.property.Get', 'guivm', None)] = \
|
||||
b'0\x00default=False type=vm gui-vm'
|
||||
self.app.expected_calls[
|
||||
('test-vm3', 'admin.vm.property.Get', 'guivm', None)] = \
|
||||
b'0\x00default=False type=vm gui-vm'
|
||||
self.app.expected_calls[
|
||||
('test-vm4', 'admin.vm.property.Get', 'guivm', None)] = \
|
||||
b'0\x00default=False type=vm gui-vm'
|
||||
|
||||
self.app._local_name = 'gui-vm'
|
||||
|
||||
vm = self.app.domains['test-vm']
|
||||
vm2 = self.app.domains['test-vm2']
|
||||
|
@ -18,7 +18,7 @@
|
||||
# You should have received a copy of the GNU Lesser General Public License along
|
||||
# with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
''' GUI daemon launcher tool'''
|
||||
""" GUI daemon launcher tool"""
|
||||
|
||||
import os
|
||||
import signal
|
||||
@ -35,10 +35,12 @@ import qubesadmin
|
||||
import qubesadmin.exc
|
||||
import qubesadmin.tools
|
||||
import qubesadmin.vm
|
||||
|
||||
have_events = False
|
||||
try:
|
||||
# pylint: disable=wrong-import-position
|
||||
import qubesadmin.events
|
||||
|
||||
have_events = True
|
||||
except ImportError:
|
||||
pass
|
||||
@ -47,7 +49,7 @@ GUI_DAEMON_PATH = '/usr/bin/qubes-guid'
|
||||
QUBES_ICON_DIR = '/usr/share/icons/hicolor/128x128/devices'
|
||||
|
||||
# "LVDS connected 1024x768+0+0 (normal left inverted right) 304mm x 228mm"
|
||||
REGEX_OUTPUT = re.compile(r'''
|
||||
REGEX_OUTPUT = re.compile(r"""
|
||||
(?x) # ignore whitespace
|
||||
^ # start of string
|
||||
(?P<output>[A-Za-z0-9\-]*)[ ] # LVDS VGA etc
|
||||
@ -67,11 +69,11 @@ REGEX_OUTPUT = re.compile(r'''
|
||||
)?
|
||||
.* # ignore rest of line
|
||||
)? # everything after (dis)connect is optional
|
||||
''')
|
||||
""")
|
||||
|
||||
|
||||
def get_monitor_layout():
|
||||
'''Get list of monitors and their size/position'''
|
||||
"""Get list of monitors and their size/position"""
|
||||
outputs = []
|
||||
|
||||
for line in subprocess.Popen(
|
||||
@ -112,18 +114,19 @@ def get_monitor_layout():
|
||||
|
||||
|
||||
class GUILauncher(object):
|
||||
'''Launch GUI daemon for VMs'''
|
||||
"""Launch GUI daemon for VMs"""
|
||||
|
||||
def __init__(self, app: qubesadmin.app.QubesBase):
|
||||
''' Initialize GUILauncher.
|
||||
""" Initialize GUILauncher.
|
||||
|
||||
:param app: :py:class:`qubesadmin.Qubes` instance
|
||||
'''
|
||||
"""
|
||||
self.app = app
|
||||
self.started_processes = {}
|
||||
|
||||
@staticmethod
|
||||
def kde_guid_args(vm):
|
||||
'''Return KDE-specific arguments for gui-daemon, if applicable'''
|
||||
"""Return KDE-specific arguments for gui-daemon, if applicable"""
|
||||
|
||||
guid_cmd = []
|
||||
# Avoid using environment variables for checking the current session,
|
||||
@ -151,11 +154,12 @@ class GUILauncher(object):
|
||||
guid_cmd += ['-p',
|
||||
'_KDE_NET_WM_COLOR_SCHEME=s:{}'.format(
|
||||
os.path.join(data_dir,
|
||||
'qubes-kde', vm.label.name + '.colors'))]
|
||||
'qubes-kde',
|
||||
vm.label.name + '.colors'))]
|
||||
return guid_cmd
|
||||
|
||||
def common_guid_args(self, vm):
|
||||
'''Common qubes-guid arguments for PV(H), HVM and Stubdomain'''
|
||||
"""Common qubes-guid arguments for PV(H), HVM and Stubdomain"""
|
||||
|
||||
guid_cmd = [GUI_DAEMON_PATH,
|
||||
'-N', vm.name,
|
||||
@ -177,19 +181,19 @@ class GUILauncher(object):
|
||||
|
||||
@staticmethod
|
||||
def guid_pidfile(xid):
|
||||
'''Helper function to construct a pidfile path'''
|
||||
"""Helper function to construct a pidfile path"""
|
||||
return '/var/run/qubes/guid-running.{}'.format(xid)
|
||||
|
||||
@asyncio.coroutine
|
||||
def start_gui_for_vm(self, vm, monitor_layout=None):
|
||||
'''Start GUI daemon (qubes-guid) connected directly to a VM
|
||||
"""Start GUI daemon (qubes-guid) connected directly to a VM
|
||||
|
||||
This function is a coroutine.
|
||||
|
||||
:param vm: VM for which start GUI daemon
|
||||
:param monitor_layout: monitor layout to send; if None, fetch it from
|
||||
local X server.
|
||||
'''
|
||||
"""
|
||||
guid_cmd = self.common_guid_args(vm)
|
||||
guid_cmd.extend(['-d', str(vm.xid)])
|
||||
|
||||
@ -212,10 +216,10 @@ class GUILauncher(object):
|
||||
|
||||
@asyncio.coroutine
|
||||
def start_gui_for_stubdomain(self, vm, force=False):
|
||||
'''Start GUI daemon (qubes-guid) connected to a stubdomain
|
||||
"""Start GUI daemon (qubes-guid) connected to a stubdomain
|
||||
|
||||
This function is a coroutine.
|
||||
'''
|
||||
"""
|
||||
want_stubdom = force
|
||||
if not want_stubdom and \
|
||||
vm.features.check_with_template('gui-emulated', False):
|
||||
@ -239,17 +243,22 @@ class GUILauncher(object):
|
||||
|
||||
@asyncio.coroutine
|
||||
def start_gui(self, vm, force_stubdom=False, monitor_layout=None):
|
||||
'''Start GUI daemon regardless of start event.
|
||||
"""Start GUI daemon regardless of start event.
|
||||
|
||||
This function is a coroutine.
|
||||
|
||||
:param vm: VM for which GUI daemon should be started
|
||||
:param force_stubdom: Force GUI daemon for stubdomain, even if the
|
||||
one for target AppVM is running.
|
||||
'''
|
||||
:param monitor_layout: monitor layout configuration
|
||||
"""
|
||||
guivm = getattr(vm, 'guivm', None)
|
||||
if guivm != vm.app.local_name:
|
||||
vm.log.info('GUI connected to {}. Skipping.'.format(guivm))
|
||||
return
|
||||
|
||||
if vm.virt_mode == 'hvm':
|
||||
yield from self.start_gui_for_stubdomain(vm,
|
||||
force=force_stubdom)
|
||||
yield from self.start_gui_for_stubdomain(vm, force=force_stubdom)
|
||||
|
||||
if not vm.features.check_with_template('gui', True):
|
||||
return
|
||||
@ -259,7 +268,7 @@ class GUILauncher(object):
|
||||
|
||||
@asyncio.coroutine
|
||||
def send_monitor_layout(self, vm, layout=None, startup=False):
|
||||
'''Send monitor layout to a given VM
|
||||
"""Send monitor layout to a given VM
|
||||
|
||||
This function is a coroutine.
|
||||
|
||||
@ -268,7 +277,7 @@ class GUILauncher(object):
|
||||
local X server.
|
||||
:param startup:
|
||||
:return: None
|
||||
'''
|
||||
"""
|
||||
# pylint: disable=no-self-use
|
||||
if vm.features.check_with_template('no-monitor-layout', False) \
|
||||
or not vm.is_running():
|
||||
@ -293,8 +302,10 @@ class GUILauncher(object):
|
||||
pass
|
||||
|
||||
try:
|
||||
yield from asyncio.get_event_loop().run_in_executor(None,
|
||||
functools.partial(vm.run_service_for_stdio,
|
||||
yield from asyncio.get_event_loop(). \
|
||||
run_in_executor(None,
|
||||
functools.partial(
|
||||
vm.run_service_for_stdio,
|
||||
'qubes.SetMonitorLayout',
|
||||
input=''.join(layout).encode(),
|
||||
autostart=False))
|
||||
@ -302,9 +313,11 @@ class GUILauncher(object):
|
||||
vm.log.warning('Failed to send monitor layout: %s', e.stderr)
|
||||
|
||||
def send_monitor_layout_all(self):
|
||||
'''Send monitor layout to all (running) VMs'''
|
||||
"""Send monitor layout to all (running) VMs"""
|
||||
monitor_layout = get_monitor_layout()
|
||||
for vm in self.app.domains:
|
||||
if getattr(vm, 'guivm', None) != vm.app.local_name:
|
||||
continue
|
||||
if vm.klass == 'AdminVM':
|
||||
continue
|
||||
if vm.is_running():
|
||||
@ -314,8 +327,10 @@ class GUILauncher(object):
|
||||
monitor_layout))
|
||||
|
||||
def on_domain_spawn(self, vm, _event, **kwargs):
|
||||
'''Handler of 'domain-spawn' event, starts GUI daemon for stubdomain'''
|
||||
"""Handler of 'domain-spawn' event, starts GUI daemon for stubdomain"""
|
||||
try:
|
||||
if getattr(vm, 'guivm', None) != vm.app.local_name:
|
||||
return
|
||||
if not vm.features.check_with_template('gui', True):
|
||||
return
|
||||
if vm.virt_mode == 'hvm' and \
|
||||
@ -325,8 +340,10 @@ class GUILauncher(object):
|
||||
vm.log.warning('Failed to start GUI for %s: %s', vm.name, str(e))
|
||||
|
||||
def on_domain_start(self, vm, _event, **kwargs):
|
||||
'''Handler of 'domain-start' event, starts GUI daemon for actual VM'''
|
||||
"""Handler of 'domain-start' event, starts GUI daemon for actual VM"""
|
||||
try:
|
||||
if getattr(vm, 'guivm', None) != vm.app.local_name:
|
||||
return
|
||||
if not vm.features.check_with_template('gui', True):
|
||||
return
|
||||
if kwargs.get('start_guid', 'True') == 'True':
|
||||
@ -335,20 +352,22 @@ class GUILauncher(object):
|
||||
vm.log.warning('Failed to start GUI for %s: %s', vm.name, str(e))
|
||||
|
||||
def on_connection_established(self, _subject, _event, **_kwargs):
|
||||
'''Handler of 'connection-established' event, used to launch GUI
|
||||
daemon for domains started before this tool. '''
|
||||
"""Handler of 'connection-established' event, used to launch GUI
|
||||
daemon for domains started before this tool. """
|
||||
|
||||
monitor_layout = get_monitor_layout()
|
||||
self.app.domains.clear_cache()
|
||||
for vm in self.app.domains:
|
||||
if vm.klass == 'AdminVM':
|
||||
continue
|
||||
if getattr(vm, 'guivm', None) != vm.app.local_name:
|
||||
continue
|
||||
if not vm.features.check_with_template('gui', True):
|
||||
continue
|
||||
power_state = vm.get_power_state()
|
||||
if power_state == 'Running':
|
||||
asyncio.ensure_future(self.start_gui(vm,
|
||||
monitor_layout=monitor_layout))
|
||||
asyncio.ensure_future(
|
||||
self.start_gui(vm, monitor_layout=monitor_layout))
|
||||
elif power_state == 'Transient':
|
||||
# it is still starting, we'll get 'domain-start' event when
|
||||
# fully started
|
||||
@ -356,21 +375,23 @@ class GUILauncher(object):
|
||||
asyncio.ensure_future(self.start_gui_for_stubdomain(vm))
|
||||
|
||||
def register_events(self, events):
|
||||
'''Register domain startup events in app.events dispatcher'''
|
||||
"""Register domain startup events in app.events dispatcher"""
|
||||
events.add_handler('domain-spawn', self.on_domain_spawn)
|
||||
events.add_handler('domain-start', self.on_domain_start)
|
||||
events.add_handler('connection-established',
|
||||
self.on_connection_established)
|
||||
|
||||
|
||||
def x_reader(conn, callback):
|
||||
'''Try reading something from X connection to check if it's still alive.
|
||||
"""Try reading something from X connection to check if it's still alive.
|
||||
In case it isn't, call *callback*.
|
||||
'''
|
||||
"""
|
||||
try:
|
||||
conn.poll_for_event()
|
||||
except xcffib.ConnectionException:
|
||||
callback()
|
||||
|
||||
|
||||
if 'XDG_RUNTIME_DIR' in os.environ:
|
||||
pidfile_path = os.path.join(os.environ['XDG_RUNTIME_DIR'],
|
||||
'qvm-start-gui.pid')
|
||||
@ -381,18 +402,20 @@ else:
|
||||
parser = qubesadmin.tools.QubesArgumentParser(
|
||||
description='start GUI for qube(s)', vmname_nargs='*')
|
||||
parser.add_argument('--watch', action='store_true',
|
||||
help='Keep watching for further domains startups, must be used with --all')
|
||||
help='Keep watching for further domains'
|
||||
' startups, must be used with --all')
|
||||
parser.add_argument('--force-stubdomain', action='store_true',
|
||||
help='Start GUI to stubdomain-emulated VGA, even if gui-agent is running '
|
||||
'in the VM')
|
||||
help='Start GUI to stubdomain-emulated VGA,'
|
||||
' even if gui-agent is running in the VM')
|
||||
parser.add_argument('--pidfile', action='store', default=pidfile_path,
|
||||
help='Pidfile path to create in --watch mode')
|
||||
parser.add_argument('--notify-monitor-layout', action='store_true',
|
||||
help='Notify running instance in --watch mode about changed monitor layout')
|
||||
help='Notify running instance in --watch mode'
|
||||
' about changed monitor layout')
|
||||
|
||||
|
||||
def main(args=None):
|
||||
''' Main function of qvm-start-gui tool'''
|
||||
""" Main function of qvm-start-gui tool"""
|
||||
args = parser.parse_args(args)
|
||||
if args.watch and not args.all_domains:
|
||||
parser.error('--watch option must be used with --all')
|
||||
|
Loading…
Reference in New Issue
Block a user