Reviewed-By: Paulo Vital <pvital(a)linux.vnet.ibm.com>
On Mon, 2015-10-19 at 19:10 -0200, Aline Manera wrote:
validate_repo_url() and check_url_path() functions are used only by
Kimchi so
move it from Wok to Kimchi.
Signed-off-by: Aline Manera <alinefm(a)linux.vnet.ibm.com>
---
src/wok/plugins/kimchi/isoinfo.py | 3 +-
src/wok/plugins/kimchi/model/config.py | 3 +-
src/wok/plugins/kimchi/repositories.py | 2 +-
src/wok/plugins/kimchi/utils.py | 58
+++++++++++++++++++++++++++++++++
src/wok/plugins/kimchi/vmtemplate.py | 2 +-
src/wok/plugins/kimchi/xmlutils/disk.py | 2 +-
src/wok/utils.py | 55 -----------------------
--------
7 files changed, 65 insertions(+), 60 deletions(-)
diff --git a/src/wok/plugins/kimchi/isoinfo.py
b/src/wok/plugins/kimchi/isoinfo.py
index bc8d8a4..731c45a 100644
--- a/src/wok/plugins/kimchi/isoinfo.py
+++ b/src/wok/plugins/kimchi/isoinfo.py
@@ -29,7 +29,8 @@ import urllib2
from wok.exception import IsoFormatError
-from wok.utils import check_url_path, wok_log
+from wok.plugins.kimchi.utils import check_url_path
+from wok.utils import wok_log
iso_dir = [
diff --git a/src/wok/plugins/kimchi/model/config.py
b/src/wok/plugins/kimchi/model/config.py
index b6cc4d9..680d7dc 100644
--- a/src/wok/plugins/kimchi/model/config.py
+++ b/src/wok/plugins/kimchi/model/config.py
@@ -24,7 +24,8 @@ from wok.basemodel import Singleton
from wok.config import config as kconfig
from wok.config import get_version
from wok.exception import NotFoundError
-from wok.utils import check_url_path, run_command, wok_log
+from wok.plugins.kimchi.utils import check_url_path
+from wok.utils import run_command, wok_log
from ..config import find_qemu_binary
from ..distroloader import DistroLoader
diff --git a/src/wok/plugins/kimchi/repositories.py
b/src/wok/plugins/kimchi/repositories.py
index c6e061f..5c395c2 100644
--- a/src/wok/plugins/kimchi/repositories.py
+++ b/src/wok/plugins/kimchi/repositories.py
@@ -26,7 +26,7 @@ from ConfigParser import ConfigParser
from wok.basemodel import Singleton
from wok.exception import InvalidOperation, InvalidParameter
from wok.exception import OperationFailed, NotFoundError,
MissingParameter
-from wok.utils import validate_repo_url
+from wok.plugins.kimchi.utils import validate_repo_url
from config import kimchiLock
from yumparser import get_yum_repositories, write_repo_to_file
diff --git a/src/wok/plugins/kimchi/utils.py
b/src/wok/plugins/kimchi/utils.py
index 92ca83a..4462ce6 100644
--- a/src/wok/plugins/kimchi/utils.py
+++ b/src/wok/plugins/kimchi/utils.py
@@ -18,11 +18,19 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
#
+import contextlib
+import os
import re
+import urllib2
+from httplib import HTTPConnection, HTTPException
+from urlparse import urlparse
from wok.exception import InvalidParameter
+MAX_REDIRECTION_ALLOWED = 5
+
+
def _uri_to_name(collection, uri):
expr = '/plugins/kimchi/%s/(.*?)$' % collection
m = re.match(expr, uri)
@@ -37,3 +45,53 @@ def template_name_from_uri(uri):
def pool_name_from_uri(uri):
return _uri_to_name('storagepools', uri)
+
+
+def check_url_path(path, redirected=0):
+ if redirected > MAX_REDIRECTION_ALLOWED:
+ return False
+ try:
+ code = ''
+ parse_result = urlparse(path)
+ server_name = parse_result.netloc
+ urlpath = parse_result.path
+ if not urlpath:
+ # Just a server, as with a repo.
+ with contextlib.closing(urllib2.urlopen(path)) as res:
+ code = res.getcode()
+ else:
+ # socket.gaierror could be raised,
+ # which is a child class of IOError
+ conn = HTTPConnection(server_name, timeout=15)
+ # Don't try to get the whole file:
+ conn.request('HEAD', path)
+ response = conn.getresponse()
+ code = response.status
+ conn.close()
+ if code == 200:
+ return True
+ elif code == 301 or code == 302:
+ for header in response.getheaders():
+ if header[0] == 'location':
+ return check_url_path(header[1], redirected+1)
+ else:
+ return False
+ except (urllib2.URLError, HTTPException, IOError, ValueError):
+ return False
+ return True
+
+
+def validate_repo_url(url):
+ url_parts = url.split('://') # [0] = prefix, [1] = rest of URL
+
+ if url_parts[0] == '':
+ raise InvalidParameter("KCHREPOS0002E")
+
+ if url_parts[0] in ['http', 'https', 'ftp']:
+ if not check_url_path(url):
+ raise InvalidParameter("WOKUTILS0001E", {'url': url})
+ elif url_parts[0] == 'file':
+ if not os.path.exists(url_parts[1]):
+ raise InvalidParameter("WOKUTILS0001E", {'url': url})
+ else:
+ raise InvalidParameter("KCHREPOS0002E")
diff --git a/src/wok/plugins/kimchi/vmtemplate.py
b/src/wok/plugins/kimchi/vmtemplate.py
index 3edf3a5..3d0b442 100644
--- a/src/wok/plugins/kimchi/vmtemplate.py
+++ b/src/wok/plugins/kimchi/vmtemplate.py
@@ -27,7 +27,7 @@ from lxml.builder import E
from wok.exception import InvalidParameter, ImageFormatError,
IsoFormatError
from wok.exception import MissingParameter, OperationFailed
-from wok.utils import check_url_path
+from wok.plugins.kimchi.utils import check_url_path
import imageinfo
import osinfo
diff --git a/src/wok/plugins/kimchi/xmlutils/disk.py
b/src/wok/plugins/kimchi/xmlutils/disk.py
index 126ce77..84be91d 100644
--- a/src/wok/plugins/kimchi/xmlutils/disk.py
+++ b/src/wok/plugins/kimchi/xmlutils/disk.py
@@ -27,7 +27,7 @@ from lxml import objectify
from lxml.builder import E
from wok.exception import InvalidParameter, NotFoundError
-from wok.utils import check_url_path
+from wok.plugins.kimchi.utils import check_url_path
BUS_TO_DEV_MAP = {'ide': 'hd', 'virtio': 'vd',
'scsi': 'sd'}
diff --git a/src/wok/utils.py b/src/wok/utils.py
index b426dfe..ccc8534 100644
--- a/src/wok/utils.py
+++ b/src/wok/utils.py
@@ -21,7 +21,6 @@
#
import cherrypy
-import contextlib
import grp
import os
import psutil
@@ -29,20 +28,16 @@ import pwd
import re
import subprocess
import traceback
-import urllib2
import xml.etree.ElementTree as ET
from cherrypy.lib.reprconf import Parser
-from httplib import HTTPConnection, HTTPException
from multiprocessing import Process, Queue
from threading import Timer
-from urlparse import urlparse
from wok.asynctask import AsyncTask
from wok.config import paths, PluginPaths
from wok.exception import InvalidParameter, TimeoutExpired
-MAX_REDIRECTION_ALLOWED = 5
wok_log = cherrypy.log.error_log
task_id = 0
@@ -127,40 +122,6 @@ def import_module(module_name, class_name=''):
return __import__(module_name, globals(), locals(),
[class_name])
-def check_url_path(path, redirected=0):
- if redirected > MAX_REDIRECTION_ALLOWED:
- return False
- try:
- code = ''
- parse_result = urlparse(path)
- server_name = parse_result.netloc
- urlpath = parse_result.path
- if not urlpath:
- # Just a server, as with a repo.
- with contextlib.closing(urllib2.urlopen(path)) as res:
- code = res.getcode()
- else:
- # socket.gaierror could be raised,
- # which is a child class of IOError
- conn = HTTPConnection(server_name, timeout=15)
- # Don't try to get the whole file:
- conn.request('HEAD', path)
- response = conn.getresponse()
- code = response.status
- conn.close()
- if code == 200:
- return True
- elif code == 301 or code == 302:
- for header in response.getheaders():
- if header[0] == 'location':
- return check_url_path(header[1], redirected+1)
- else:
- return False
- except (urllib2.URLError, HTTPException, IOError, ValueError):
- return False
- return True
-
-
def run_command(cmd, timeout=None, silent=False):
"""
cmd is a sequence of command arguments.
@@ -307,22 +268,6 @@ def probe_file_permission_as_user(file, user):
return queue.get()
-def validate_repo_url(url):
- url_parts = url.split('://') # [0] = prefix, [1] = rest of URL
-
- if url_parts[0] == '':
- raise InvalidParameter("KCHREPOS0002E")
-
- if url_parts[0] in ['http', 'https', 'ftp']:
- if not check_url_path(url):
- raise InvalidParameter("WOKUTILS0001E", {'url': url})
- elif url_parts[0] == 'file':
- if not os.path.exists(url_parts[1]):
- raise InvalidParameter("WOKUTILS0001E", {'url': url})
- else:
- raise InvalidParameter("KCHREPOS0002E")
-
-
def get_next_clone_name(all_names, basename, name_suffix=''):
"""Find the next available name for a cloned resource.