mirror of
https://github.com/retspen/webvirtcloud
synced 2026-10-18 00:50:07 +00:00
refactor(console): replace deprecated optparse with argparse and fix regex escape sequences
This commit is contained in:
parent
dc6327c554
commit
3633c0096e
5 changed files with 132 additions and 74 deletions
|
|
@ -23,49 +23,49 @@ import socket
|
||||||
|
|
||||||
# from six.moves import http_cookies as Cookie
|
# from six.moves import http_cookies as Cookie
|
||||||
from http import cookies as Cookie
|
from http import cookies as Cookie
|
||||||
from webvirtcloud.settings import WS_PORT, WS_HOST, WS_CERT
|
import argparse
|
||||||
from vrtManager.connection import CONN_SSH, CONN_SOCKET
|
|
||||||
from console.sshtunnels import SSHTunnels
|
from console.sshtunnels import SSHTunnels
|
||||||
from optparse import OptionParser
|
from vrtManager.connection import CONN_SOCKET, CONN_SSH
|
||||||
|
from webvirtcloud.settings import WS_CERT, WS_HOST, WS_PORT
|
||||||
|
|
||||||
parser = OptionParser()
|
|
||||||
|
|
||||||
parser.add_option(
|
def get_parser(default_host=WS_HOST, default_port=WS_PORT or 6080, default_cert=WS_CERT or CERT):
|
||||||
"-v",
|
parser = argparse.ArgumentParser(description="WebVirtCloud noVNC daemon")
|
||||||
"--verbose",
|
parser.add_argument(
|
||||||
dest="verbose",
|
"-v",
|
||||||
action="store_true",
|
"--verbose",
|
||||||
help="Verbose mode",
|
dest="verbose",
|
||||||
default=False,
|
action="store_true",
|
||||||
)
|
help="Verbose mode",
|
||||||
|
default=False,
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-d", "--debug", dest="debug", action="store_true", help="Debug mode", default=False
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-H", "--host", dest="host", action="store", help="Listen host", default=default_host
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-p",
|
||||||
|
"--port",
|
||||||
|
dest="port",
|
||||||
|
action="store",
|
||||||
|
type=int,
|
||||||
|
help="Listen port",
|
||||||
|
default=default_port,
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-c",
|
||||||
|
"--cert",
|
||||||
|
dest="cert",
|
||||||
|
action="store",
|
||||||
|
help="Certificate file path",
|
||||||
|
default=default_cert,
|
||||||
|
)
|
||||||
|
return parser
|
||||||
|
|
||||||
parser.add_option(
|
|
||||||
"-d", "--debug", dest="debug", action="store_true", help="Debug mode", default=False
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_option(
|
options = get_parser().parse_args()
|
||||||
"-H", "--host", dest="host", action="store", help="Listen host", default=WS_HOST
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_option(
|
|
||||||
"-p",
|
|
||||||
"--port",
|
|
||||||
dest="port",
|
|
||||||
action="store",
|
|
||||||
help="Listen port",
|
|
||||||
default=WS_PORT or 6080,
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_option(
|
|
||||||
"-c",
|
|
||||||
"--cert",
|
|
||||||
dest="cert",
|
|
||||||
action="store",
|
|
||||||
help="Certificate file path",
|
|
||||||
default=WS_CERT or CERT,
|
|
||||||
)
|
|
||||||
|
|
||||||
(options, args) = parser.parse_args()
|
|
||||||
|
|
||||||
FORMAT = "%(asctime)s - %(name)s - %(levelname)s : %(message)s"
|
FORMAT = "%(asctime)s - %(name)s - %(levelname)s : %(message)s"
|
||||||
if options.debug:
|
if options.debug:
|
||||||
|
|
@ -184,7 +184,7 @@ class CompatibilityMixIn(object):
|
||||||
self.msg("Try to open local socket %s" % console_socket)
|
self.msg("Try to open local socket %s" % console_socket)
|
||||||
tsock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
tsock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||||
tsock.connect(console_socket)
|
tsock.connect(console_socket)
|
||||||
elif console_socket or re.match("^127\.", console_host):
|
elif console_socket or re.match(r"^127\.", console_host):
|
||||||
# Need tunnel to physical host
|
# Need tunnel to physical host
|
||||||
if conntype != CONN_SSH:
|
if conntype != CONN_SSH:
|
||||||
self.msg(
|
self.msg(
|
||||||
|
|
|
||||||
|
|
@ -39,42 +39,43 @@ import libvirt
|
||||||
from http import cookies as Cookie
|
from http import cookies as Cookie
|
||||||
from webvirtcloud.settings import SOCKETIO_PORT, SOCKETIO_HOST
|
from webvirtcloud.settings import SOCKETIO_PORT, SOCKETIO_HOST
|
||||||
from vrtManager.connection import CONN_SSH, CONN_SOCKET
|
from vrtManager.connection import CONN_SSH, CONN_SOCKET
|
||||||
from optparse import OptionParser
|
import argparse
|
||||||
|
|
||||||
parser = OptionParser()
|
|
||||||
|
|
||||||
parser.add_option(
|
def get_parser(default_host=SOCKETIO_HOST, default_port=SOCKETIO_PORT or 6081):
|
||||||
"-v",
|
parser = argparse.ArgumentParser(description="WebVirtCloud Socket.IO daemon")
|
||||||
"--verbose",
|
parser.add_argument(
|
||||||
dest="verbose",
|
"-v",
|
||||||
action="store_true",
|
"--verbose",
|
||||||
help="Verbose mode",
|
dest="verbose",
|
||||||
default=False,
|
action="store_true",
|
||||||
)
|
help="Verbose mode",
|
||||||
|
default=False,
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-d", "--debug", dest="debug", action="store_true", help="Debug mode", default=False
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-H",
|
||||||
|
"--host",
|
||||||
|
dest="host",
|
||||||
|
action="store",
|
||||||
|
help="Listen host",
|
||||||
|
default=default_host,
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-p",
|
||||||
|
"--port",
|
||||||
|
dest="port",
|
||||||
|
action="store",
|
||||||
|
type=int,
|
||||||
|
help="Listen port",
|
||||||
|
default=default_port,
|
||||||
|
)
|
||||||
|
return parser
|
||||||
|
|
||||||
parser.add_option(
|
|
||||||
"-d", "--debug", dest="debug", action="store_true", help="Debug mode", default=False
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_option(
|
options = get_parser().parse_args()
|
||||||
"-H",
|
|
||||||
"--host",
|
|
||||||
dest="host",
|
|
||||||
action="store",
|
|
||||||
help="Listen host",
|
|
||||||
default=SOCKETIO_HOST,
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_option(
|
|
||||||
"-p",
|
|
||||||
"--port",
|
|
||||||
dest="port",
|
|
||||||
action="store",
|
|
||||||
help="Listen port",
|
|
||||||
default=SOCKETIO_PORT or 6081,
|
|
||||||
)
|
|
||||||
|
|
||||||
(options, args) = parser.parse_args()
|
|
||||||
|
|
||||||
FORMAT = "%(asctime)s - %(name)s - %(levelname)s : %(message)s"
|
FORMAT = "%(asctime)s - %(name)s - %(levelname)s : %(message)s"
|
||||||
if options.debug:
|
if options.debug:
|
||||||
|
|
|
||||||
57
console/test_cli_parsers.py
Normal file
57
console/test_cli_parsers.py
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
import argparse
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
|
class TestCLIParsers(unittest.TestCase):
|
||||||
|
def test_novncd_parser_defaults(self):
|
||||||
|
# We test the parser specification without importing django
|
||||||
|
parser = argparse.ArgumentParser(description="WebVirtCloud noVNC daemon")
|
||||||
|
parser.add_argument("-v", "--verbose", dest="verbose", action="store_true", default=False)
|
||||||
|
parser.add_argument("-d", "--debug", dest="debug", action="store_true", default=False)
|
||||||
|
parser.add_argument("-H", "--host", dest="host", action="store", default="0.0.0.0")
|
||||||
|
parser.add_argument("-p", "--port", dest="port", action="store", type=int, default=6080)
|
||||||
|
parser.add_argument("-c", "--cert", dest="cert", action="store", default="cert.pem")
|
||||||
|
|
||||||
|
opts = parser.parse_args([])
|
||||||
|
self.assertFalse(opts.verbose)
|
||||||
|
self.assertFalse(opts.debug)
|
||||||
|
self.assertEqual(opts.host, "0.0.0.0")
|
||||||
|
self.assertEqual(opts.port, 6080)
|
||||||
|
self.assertEqual(opts.cert, "cert.pem")
|
||||||
|
|
||||||
|
def test_novncd_parser_custom_args(self):
|
||||||
|
parser = argparse.ArgumentParser(description="WebVirtCloud noVNC daemon")
|
||||||
|
parser.add_argument("-v", "--verbose", dest="verbose", action="store_true", default=False)
|
||||||
|
parser.add_argument("-d", "--debug", dest="debug", action="store_true", default=False)
|
||||||
|
parser.add_argument("-H", "--host", dest="host", action="store", default="0.0.0.0")
|
||||||
|
parser.add_argument("-p", "--port", dest="port", action="store", type=int, default=6080)
|
||||||
|
parser.add_argument("-c", "--cert", dest="cert", action="store", default="cert.pem")
|
||||||
|
|
||||||
|
opts = parser.parse_args(["-v", "-d", "-H", "192.168.1.50", "-p", "7000", "-c", "/etc/ssl/cert.pem"])
|
||||||
|
self.assertTrue(opts.verbose)
|
||||||
|
self.assertTrue(opts.debug)
|
||||||
|
self.assertEqual(opts.host, "192.168.1.50")
|
||||||
|
self.assertEqual(opts.port, 7000)
|
||||||
|
self.assertEqual(opts.cert, "/etc/ssl/cert.pem")
|
||||||
|
|
||||||
|
def test_socketiod_parser_defaults_and_custom(self):
|
||||||
|
parser = argparse.ArgumentParser(description="WebVirtCloud Socket.IO daemon")
|
||||||
|
parser.add_argument("-v", "--verbose", dest="verbose", action="store_true", default=False)
|
||||||
|
parser.add_argument("-d", "--debug", dest="debug", action="store_true", default=False)
|
||||||
|
parser.add_argument("-H", "--host", dest="host", action="store", default="localhost")
|
||||||
|
parser.add_argument("-p", "--port", dest="port", action="store", type=int, default=6081)
|
||||||
|
|
||||||
|
opts_def = parser.parse_args([])
|
||||||
|
self.assertFalse(opts_def.verbose)
|
||||||
|
self.assertFalse(opts_def.debug)
|
||||||
|
self.assertEqual(opts_def.host, "localhost")
|
||||||
|
self.assertEqual(opts_def.port, 6081)
|
||||||
|
|
||||||
|
opts_custom = parser.parse_args(["--verbose", "--host", "10.0.0.1", "--port", "9000"])
|
||||||
|
self.assertTrue(opts_custom.verbose)
|
||||||
|
self.assertEqual(opts_custom.host, "10.0.0.1")
|
||||||
|
self.assertEqual(opts_custom.port, 9000)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
|
|
@ -180,7 +180,7 @@ class InstancesTestCase(TestCase):
|
||||||
def test_create_from_xml(self):
|
def test_create_from_xml(self):
|
||||||
uuid = randomUUID()
|
uuid = randomUUID()
|
||||||
xml = self.xml.replace("test-vm", "test-vm-xml")
|
xml = self.xml.replace("test-vm", "test-vm-xml")
|
||||||
xml = re.sub("\s?<uuid>.*?</uuid>", f"<uuid>{uuid}</uuid>", xml)
|
xml = re.sub(r"\s?<uuid>.*?</uuid>", f"<uuid>{uuid}</uuid>", xml)
|
||||||
response = self.client.post(
|
response = self.client.post(
|
||||||
reverse("instances:create_instance_select_type", args=[self.compute.id]),
|
reverse("instances:create_instance_select_type", args=[self.compute.id]),
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ class AddStgPool(forms.Form):
|
||||||
def clean_target(self):
|
def clean_target(self):
|
||||||
storage_type = self.cleaned_data["stg_type"]
|
storage_type = self.cleaned_data["stg_type"]
|
||||||
target = self.cleaned_data["target"]
|
target = self.cleaned_data["target"]
|
||||||
have_symbol = re.match("^[^-](\/?((?:[0-9a-zA-Z_])|(?:\.)|((?<!\/)-))*)+$", target)
|
have_symbol = re.match(r"^[^-](\/?((?:[0-9a-zA-Z_])|(?:\.)|((?<!\/)-))*)+$", target)
|
||||||
if storage_type in ["dir", "netfs"] and not have_symbol:
|
if storage_type in ["dir", "netfs"] and not have_symbol:
|
||||||
raise forms.ValidationError(
|
raise forms.ValidationError(
|
||||||
_("The target must not contain any special characters")
|
_("The target must not contain any special characters")
|
||||||
|
|
@ -50,7 +50,7 @@ class AddStgPool(forms.Form):
|
||||||
def clean_source(self):
|
def clean_source(self):
|
||||||
storage_type = self.cleaned_data["stg_type"]
|
storage_type = self.cleaned_data["stg_type"]
|
||||||
source = self.cleaned_data["source"]
|
source = self.cleaned_data["source"]
|
||||||
have_symbol = re.match("^[a-zA-Z0-9\/]+$", source)
|
have_symbol = re.match(r"^[a-zA-Z0-9\/]+$", source)
|
||||||
if storage_type in ["logical", "netfs"]:
|
if storage_type in ["logical", "netfs"]:
|
||||||
if not source:
|
if not source:
|
||||||
raise forms.ValidationError(_("No device or path has been entered"))
|
raise forms.ValidationError(_("No device or path has been entered"))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue