zone-vnc-console revision 4623
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews# Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
4a14ce5ba00ab7bc55c99ffdcf59c7a4ab902721Automatic Updater# Licensed under the Apache License, Version 2.0 (the "License"); you may
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews# not use this file except in compliance with the License. You may obtain
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews# a copy of the License at
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews# Unless required by applicable law or agreed to in writing, software
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews# License for the specific language governing permissions and limitations
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews# under the License.
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrewsfrom subprocess import CalledProcessError, check_call, Popen
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark AndrewsXSTARTUPHDR = "# WARNING: THIS FILE GENERATED BY SMF.\n" + \
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews "# DO NOT EDIT THIS FILE. EDITS WILL BE LOST.\n"
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark AndrewsXRESOURCES = "[[ -f ~/.Xresources ]] && /usr/bin/xrdb -merge ~/.Xresources\n"
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews# Borderless, Monospsce font, point size 14, white foreground on black
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews# background are reasonable defaults.
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark AndrewsXTERMOPTS = ' -b 0 -fa Monospace -fs 14 -fg white -bg black -title ' + \
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews '"Zone Console: $ZONENAME"'
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews# Enclose command in comments to prevent xterm consuming zlogin opts
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark AndrewsZLOGINOPTS = ' -e "/usr/bin/pfexec /usr/sbin/zlogin -C -E $ZONENAME"\n'
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark AndrewsXSTARTUP = XSTARTUPHDR + XRESOURCES + XTERM + XTERMOPTS + ZLOGINOPTS
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews # NOTE: 'geometry' below is that which matches the size of standard
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews # 80 character undecorated xterm window using font style specified in
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews # XTERMOPTS. The geometry doesn't matter too much because the display
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews # will be resized using xrandr once the xterm geometry is established.
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews cmd = [VNCSERVER, "-name", desktop_name, "-SecurityTypes=None",
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews "-geometry", "964x580", "-localhost", "-autokill"]
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews vnc = Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews if line.startswith("New '%s' desktop is" % desktop_name):
f7b41fd9291b8f4dba27e2b57e1d93f0913a4f1dMark Andrews # set host prop
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews # set port num prop
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews cmd = [SVCCFG, '-s', fmri, 'setprop', 'vnc/port', '=', 'integer:',
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews svccfg = subprocess.Popen(cmd, stdout=subprocess.PIPE,
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews print "Error updating 'vnc/port' property: " + err
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews # first kill the SMF contract
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews check_call(["/usr/bin/pkill", "-c", sys.argv[2]])
9fbbfb5757a1e3e86d7dea62c4e63ffc2303ca2bAutomatic Updater # 1 is returncode if no SMF contract processes were matched,
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews # meaning they have already terminated.
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews print "failed to kill the SMF contract: %s" % cpe
a057e8e33baa5fa369be28a9680585200ce3ff73Mark Andrews # reset port num prop to initial zero value
a057e8e33baa5fa369be28a9680585200ce3ff73Mark Andrews cmd = [SVCCFG, '-s', fmri, 'setprop', 'vnc/port', '=', 'integer:',
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews svccfg = subprocess.Popen(cmd, stdout=subprocess.PIPE,
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews print "Error resetting 'vnc/port' property: " + err
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews print("VNC console service not available on this compute node. "
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews "%s is missing. Run 'pkg install x11/server/xvnc'"
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews print("VNC console service not available on this compute node. "
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews "%s is missing. Run 'pkg install terminal/xterm'"
a057e8e33baa5fa369be28a9680585200ce3ff73Mark Andrews xstartup_path = os.path.join(vncdir, 'xstartup')
dba3c818ae00b10388d31703e86a28415db398acTinderbox User # Always clobber xstartup
43b94483957d3168796a816ed86cf097518817dcTinderbox User # stemp tuple = [fd, path]
72938578c985138165e7a4b0a38f16daacbad95eAutomatic Updater """ Try to determine xterm window geometry and resize the Xvnc display
72938578c985138165e7a4b0a38f16daacbad95eAutomatic Updater to match using XRANDR. Treat failure as non-fatal since an
dba3c818ae00b10388d31703e86a28415db398acTinderbox User incorrectly sized console is arguably better than none.
dba3c818ae00b10388d31703e86a28415db398acTinderbox User def _get_window_geometry(display, windowname):
72938578c985138165e7a4b0a38f16daacbad95eAutomatic Updater """ Find the xterm xwindow by name/title and extract its geometry
72938578c985138165e7a4b0a38f16daacbad95eAutomatic Updater Returns: tuple of window [width, height]
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews UninitializedWindowError if window not yet initialized
bf056b7184b38281c1b0bf0cf21b5982fa1a4edaMark Andrews UnmappedWindowError if window is not viewable/mapped
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews cmd = [XWININFO, '-d', display, '-name', windowname]
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews xwininfo = subprocess.Popen(cmd, stdout=subprocess.PIPE,
97e74139b19368e385a3564746d42db70879195eAutomatic Updater print "Error finding console xwindow info: " + err
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews # Window is not mapped yet.
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews # What, no width and height???
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews print "No window geometry info returned by " + XWINFINFO
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews while uninit_count < retries and unmap_count < retries:
f7b41fd9291b8f4dba27e2b57e1d93f0913a4f1dMark Andrews print "Discovered xterm geometry: %d x %d" % (width, height)
e2e4d321999340802f77adaacd19c797d04b4b95Automatic Updater print "xterm window not initialized yet. Retrying in %ds" \
e2e4d321999340802f77adaacd19c797d04b4b95Automatic Updater print "xterm window is taking too long to initialize"
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews print "Discovered xterm not mapped yet. Retrying in %ds" \
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews print "Discovered xterm window is taking too long to map"
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews print "Too many failed attempts to discover xterm window geometry"
3b4098640dd85040270f39b9a5ee5e22de99d3d6Mark Andrews # Generate a mode line for width and height, with a refresh of 60.0Hz
3b4098640dd85040270f39b9a5ee5e22de99d3d6Mark Andrews cmd = [GTF, str(width), str(height), '60.0', '-x']
3b4098640dd85040270f39b9a5ee5e22de99d3d6Mark Andrews gtf = subprocess.Popen(cmd, stdout=subprocess.PIPE,
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews print "Error creating new modeline for VNC display: " + err
7be2f6d5df28b207e3e385c555eb4f740150528dTinderbox User print "New optimal modeline for Xvnc server: " + modeline
7be2f6d5df28b207e3e385c555eb4f740150528dTinderbox User # Create a new mode for the Xvnc server using the modeline generated by gtf
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews newmode = subprocess.Popen(cmd, stdout=subprocess.PIPE,
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews print "Error creating new xrandr modeline for VNC display: " + err
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews # Add the new mode to the default display output
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews cmd = [XRANDR, '-d', display, '--addmode', 'default', modename]
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews addmode = subprocess.Popen(cmd, stdout=subprocess.PIPE,
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews print "Error adding new xrandr modeline for VNC display: " + err
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews # Activate the new mode on the default display output
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews cmd = [XRANDR, '-d', display, '--output', 'default', '--mode', modename]
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews addmode = subprocess.Popen(cmd, stdout=subprocess.PIPE,
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews print "Error setting new xrandr modeline for VNC display: " + err