OSDN Git Service

Add validate
[karesansui/karesansui.git] / bin / replicate_guest.py
index b1722a8..84f86a0 100755 (executable)
@@ -23,6 +23,7 @@ The only executable file format all disks.
 import os
 import os.path
 import sys
+import re
 import signal
 import logging
 from optparse import OptionParser
@@ -35,7 +36,8 @@ try:
     import karesansui
     from karesansui import __version__
     from karesansui.lib.virt.virt import KaresansuiVirtConnection
-    from karesansui.lib.utils import load_locale
+    from karesansui.lib.utils import load_locale, is_uuid
+    from karesansui.lib.const import PORT_MIN_NUMBER, PORT_MAX_NUMBER
 except ImportError:
     print >>sys.stderr, "[Error] karesansui package was not found."
     sys.exit(1)
@@ -55,12 +57,41 @@ def getopts():
     return optp.parse_args()
 
 def chkopts(opts):
-    if not opts.name:
-        raise KssCommandOptException('ERROR: -d or --dest-name option is required.')
-    if not opts.src_name:
-        raise KssCommandOptException('ERROR: -s or --src-name option is required.')
-    if not opts.pool:
-        raise KssCommandOptException('ERROR: -p or --pool option is required.')
+    reg = re.compile("[^a-zA-Z0-9\./_-]")
+
+    if opts.name:
+        if reg.search(opts.name):
+            raise KssCommandOptException('ERROR: Illigal option value. option=%s value=%s' % ('-d or --dest-name', opts.name))
+    else:
+        raise KssCommandOptException('ERROR: %s option is required.' % '-d or --dest-name')
+
+    if opts.src_name:
+        if reg.search(opts.src_name):
+            raise KssCommandOptException('ERROR: Illigal option value. option=%s value=%s' % ('-s or --src-name', opts.src_name))
+    else:
+        raise KssCommandOptException('ERROR: %s option is required.' % '-s or --src-name')
+
+    if opts.pool:
+        if reg.search(opts.pool):
+            raise KssCommandOptException('ERROR: Illigal option value. option=%s value=%s' % ('-p or --pool', opts.pool))
+    else:
+        raise KssCommandOptException('ERROR: %s option is required.' % '-p or --pool')
+
+    if opts.vnc_port:
+        reg = re.compile("^[0-9]{1,5}$")
+        if not reg.match(opts.vnc_port):
+            raise KssCommandOptException('ERROR: Illigal option value. option=%s value=%s' % ('-v or --vnc-port', opts.vnc_port))
+        if int(opts.vnc_port) < PORT_MIN_NUMBER or PORT_MAX_NUMBER < int(opts.vnc_port):
+            raise KssCommandOptException('ERROR: Illigal port number. port=%s' % (opts.vnc_port))
+
+    if opts.mac:
+        reg = re.compile("^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$")
+        if not reg.match(opts.mac):
+            raise KssCommandOptException('ERROR: Illigal MAC address. mac=%s' % (opts.mac))
+
+    if opts.uuid:
+        if not is_uuid(opts.uuid):
+            raise KssCommandOptException('ERROR: Illigal UUID. uuid=%s' % (opts.uuid))
 
 class ReplicateGuest(KssCommand):