certbot/certbot-apache/certbot_apache/display_ops.py

104 lines
3.3 KiB
Python
Raw Normal View History

2015-06-25 21:57:10 -04:00
"""Contains UI methods for Apache operations."""
2015-06-26 17:47:03 -04:00
import logging
2015-06-25 20:02:09 -04:00
import os
2015-06-26 17:47:03 -04:00
2015-06-25 20:02:09 -04:00
import zope.component
from certbot import errors
from certbot import interfaces
2015-06-25 20:02:09 -04:00
import certbot.display.util as display_util
2015-06-25 20:02:09 -04:00
2015-06-26 17:47:03 -04:00
logger = logging.getLogger(__name__)
2015-06-25 20:02:09 -04:00
def select_vhost(domain, vhosts):
"""Select an appropriate Apache Vhost.
:param vhosts: Available Apache Virtual Hosts
:type vhosts: :class:`list` of type `~obj.Vhost`
2015-06-26 17:47:03 -04:00
:returns: VirtualHost or `None`
:rtype: `~obj.Vhost` or `None`
2015-06-25 20:02:09 -04:00
"""
if not vhosts:
return None
while True:
code, tag = _vhost_menu(domain, vhosts)
if code == display_util.HELP:
_more_info_vhost(vhosts[tag])
elif code == display_util.OK:
return vhosts[tag]
else:
return None
def _vhost_menu(domain, vhosts):
"""Select an appropriate Apache Vhost.
:param vhosts: Available Apache Virtual Hosts
:type vhosts: :class:`list` of type `~obj.Vhost`
:returns: Display tuple - ('code', tag')
:rtype: `tuple`
"""
2015-06-26 17:47:03 -04:00
# Free characters in the line of display text (9 is for ' | ' formatting)
free_chars = display_util.WIDTH - len("HTTPS") - len("Enabled") - 9
if free_chars < 2:
logger.debug("Display size is too small for "
"certbot_apache.display_ops._vhost_menu()")
2015-06-26 17:47:03 -04:00
# This runs the edge off the screen, but it doesn't cause an "error"
filename_size = 1
disp_name_size = 1
else:
# Filename is a bit more important and probably longer with 000-*
filename_size = int(free_chars * .6)
disp_name_size = free_chars - filename_size
2015-06-25 20:02:09 -04:00
choices = []
for vhost in vhosts:
2015-07-22 05:05:01 -04:00
if len(vhost.get_names()) == 1:
disp_name = next(iter(vhost.get_names()))
elif len(vhost.get_names()) == 0:
2015-06-25 20:02:09 -04:00
disp_name = ""
else:
disp_name = "Multiple Names"
choices.append(
2015-06-26 17:47:03 -04:00
"{fn:{fn_size}s} | {name:{name_size}s} | {https:5s} | "
"{active:7s}".format(
fn=os.path.basename(vhost.filep)[:filename_size],
name=disp_name[:disp_name_size],
https="HTTPS" if vhost.ssl else "",
active="Enabled" if vhost.enabled else "",
fn_size=filename_size,
name_size=disp_name_size)
2015-06-25 20:02:09 -04:00
)
try:
code, tag = zope.component.getUtility(interfaces.IDisplay).menu(
"We were unable to find a vhost with a ServerName "
"or Address of {0}.{1}Which virtual host would you "
"like to choose?\n(note: conf files with multiple "
2016-03-15 15:07:46 -04:00
"vhosts are not yet supported)".format(domain, os.linesep),
choices, help_label="More Info", ok_label="Select")
except errors.MissingCommandlineFlag as e:
msg = ("Failed to run Apache plugin non-interactively{1}{0}{1}"
"(The best solution is to add ServerName or ServerAlias "
"entries to the VirtualHost directives of your apache "
"configuration files.)".format(e, os.linesep))
raise errors.MissingCommandlineFlag(msg)
2015-06-25 20:02:09 -04:00
return code, tag
def _more_info_vhost(vhost):
zope.component.getUtility(interfaces.IDisplay).notification(
2015-06-25 21:57:10 -04:00
"Virtual Host Information:{0}{1}{0}{2}".format(
os.linesep, "-" * (display_util.WIDTH - 4), str(vhost)),
height=display_util.HEIGHT)