Merge lp:~bladernr/checkbox/1621986-virt-accept-urls into lp:checkbox

Proposed by Jeff Lane 
Status: Merged
Approved by: Sylvain Pineau
Approved revision: 4494
Merged at revision: 4493
Proposed branch: lp:~bladernr/checkbox/1621986-virt-accept-urls
Merge into: lp:checkbox
Diff against target: 206 lines (+60/-74)
2 files modified
providers/plainbox-provider-checkbox/bin/virtualization (+60/-50)
providers/plainbox-provider-checkbox/jobs/virtualization.txt.in (+0/-24)
To merge this branch: bzr merge lp:~bladernr/checkbox/1621986-virt-accept-urls
Reviewer Review Type Date Requested Status
Sylvain Pineau (community) Approve
Jeff Lane  Approve
Review via email: mp+305491@code.launchpad.net

Description of the change

--image and the config file cna now accept the following items:

http://mywebsite.com/images/
 - relative URL and script will find files in the same format as cloud-images.ubuntu.com, e.d. SERIES-server-ARCH-disk1.img

http://mywebsite.com/images/mycloudimage.img
 - Absolute URL to an image with an arbitrary name.

file:///path/to/local/cloudimage.img
 - URL for local filesystem path to image needed

/path/to/local/cloudimage.img
 - absolute local path to image needed

THe web URLs can point to ANY arbitrary location that has the cloud image we need. THe first one is intended for local mirrors of cloud-image, thelatter to call out any cloud image you want to boot as test.

Also, if you provide NONE of these, the default behaviour remains in that the script will go to cloud-images.ubuntu.com to get the proper image.

The above can be passed via the --image option:

./virtualization kvm --image http://somedomain.net/image.img

or via ENV Var as defined in /etc/xdg/canonical-certification.conf.

Additionally, I did some PEP8 cleanup, and removed a vestigal XenTest class that will never be used.

To post a comment you must log in.
Revision history for this message
Sylvain Pineau (sylvain-pineau) wrote :

Thanks a lot for this url option, just one thing though. If you remove the xen class, please also remove the virtualization/xen* jobs from jobs/virtualization.txt.in

review: Needs Fixing
4494. By Jeff Lane 

Removed long forgotten Xen jobs from jobs/virtualization.txt.in

Revision history for this message
Jeff Lane  (bladernr) wrote :

Whoops... thanks for the reminder. I forgot those were even there. Fixed.

review: Approve
Revision history for this message
Sylvain Pineau (sylvain-pineau) wrote :

+1, thanks Jeff

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'providers/plainbox-provider-checkbox/bin/virtualization'
2--- providers/plainbox-provider-checkbox/bin/virtualization 2016-08-09 19:59:36 +0000
3+++ providers/plainbox-provider-checkbox/bin/virtualization 2016-09-12 17:56:36 +0000
4@@ -44,13 +44,11 @@
5 import tarfile
6 import time
7 import urllib.request
8+from urllib.parse import urlparse
9
10 DEFAULT_TIMEOUT = 500
11
12
13-class XENTest(object):
14- pass
15-
16 # The "TAR" type is a tarball that contains both
17 # a disk image and a kernel binary. This is useful
18 # on architectures that don't (yet) have a bootloader
19@@ -110,9 +108,9 @@
20 'qemu_bin': 'qemu-system-ppc64',
21 'qemu_disk_type': QEMU_DISK_TYPE_VIRTIO,
22 'qemu_extra_args': [
23- '-enable-kvm',
24+ '-enable-kvm',
25 '-machine', 'pseries,usb=off',
26- '-cpu', 'POWER8',
27+ '-cpu', 'POWER8',
28 ],
29 },
30 's390x': {
31@@ -121,7 +119,7 @@
32 'qemu_bin': 'qemu-system-s390x',
33 'qemu_disk_type': QEMU_DISK_TYPE_VIRTIO,
34 'qemu_extra_args': [
35- '-enable-kvm',
36+ '-enable-kvm',
37 '-machine', 's390-ccw-virtio-2.5',
38 ],
39 },
40@@ -200,35 +198,68 @@
41 self.arch = check_output(['dpkg', '--print-architecture'],
42 universal_newlines=True).strip()
43 self.qemu_config = QEMU_ARCH_CONFIG[self.arch]
44-
45- def download_image(self):
46- """
47- Downloads Cloud image for same release as host machine
48- """
49-
50- # Check Ubuntu release info. Example {quantal, precise}
51- release = lsb_release.get_lsb_information()["CODENAME"]
52-
53- # Construct URL
54- cloud_url = "http://cloud-images.ubuntu.com"
55-
56+ self.release = lsb_release.get_lsb_information()["CODENAME"]
57+
58+ def url_to_path(self, image_path):
59+ """
60+ Test the provided image path to determine if it's a URL or or a simple
61+ file path
62+ """
63+ url = urlparse(image_path)
64+ if url.scheme == '' or url.scheme == 'file':
65+ # Gives us path wheter we specify a filesystem path or a file URL
66+ logging.debug("Cloud image exists locally at %s" % url.path)
67+ return url.path
68+ elif url.scheme == 'http' or url.scheme == 'ftp':
69+ # Gives us the stuff needed to build the URL to download the image
70+ return self.download_image(image_path)
71+
72+ def construct_cloud_filename(self):
73+ """
74+ Build a URL for official Ubuntu images hosted either at
75+ cloud-images.ubuntu.com or on a maas server hosting a mirror of
76+ cloud-images.ubuntu.com
77+ """
78 if self.qemu_config['cloudimg_type'] == CLOUD_IMAGE_TYPE_TAR:
79 cloud_iso = "%s-server-cloudimg-%s.tar.gz" % (
80- release, self.qemu_config['cloudimg_arch'])
81+ self.release, self.qemu_config['cloudimg_arch'])
82 elif self.qemu_config['cloudimg_type'] == CLOUD_IMAGE_TYPE_DISK:
83 cloud_iso = "%s-server-cloudimg-%s-disk1.img" % (
84- release, self.qemu_config['cloudimg_arch'])
85+ self.release, self.qemu_config['cloudimg_arch'])
86 else:
87 logging.error("Unknown cloud image type")
88- return False
89- image_url = "/".join((
90- cloud_url, release, "current", cloud_iso))
91+ sys.exit(1)
92+ return cloud_iso
93
94+ def download_image(self, image_url=None):
95+ """
96+ Downloads Cloud image for same release as host machine
97+ """
98+ if image_url is None:
99+ # If we have not specified a URL to get our images from, default
100+ # to ubuntu.com
101+ cloud_url = "http://cloud-images.ubuntu.com"
102+ cloud_iso = self.construct_cloud_filename()
103+ full_url = "/".join((
104+ cloud_url, self.release, "current", cloud_iso))
105+ else:
106+ url = urlparse(image_url)
107+ if url.path.endswith('/') or url.path == '':
108+ # If we have a relative URL (MAAS server mirror)
109+ cloud_url = image_url
110+ cloud_iso = self.construct_cloud_filename()
111+ full_url = "/".join((
112+ cloud_url, cloud_iso))
113+ else:
114+ # Assume anything else is an absolute URL to a remote server
115+ cloud_iso = url.path.split('/')[-1]
116+ cloud_url = "{}://{}".format(url.scheme, url.netloc)
117+ full_url = image_url
118 logging.debug("Downloading {}, from {}".format(cloud_iso, cloud_url))
119
120 # Attempt download
121 try:
122- resp = urllib.request.urlretrieve(image_url, cloud_iso)
123+ resp = urllib.request.urlretrieve(full_url, cloud_iso)
124 except (IOError,
125 OSError,
126 urllib.error.HTTPError,
127@@ -362,6 +393,10 @@
128 logging.debug('No image specified, downloading one now.')
129 # Download cloud image
130 self.image = self.download_image()
131+ else:
132+ logging.debug('Cloud image location specified: %s.' %
133+ self.image)
134+ self.image = self.url_to_path(self.image)
135
136 if self.image and os.path.isfile(self.image):
137
138@@ -410,35 +445,10 @@
139 def test_kvm(args):
140 print("Executing KVM Test", file=sys.stderr)
141
142- DEFAULT_CFG = "/etc/xdg/virtualization.cfg"
143 image = ""
144 timeout = ""
145
146- # Configuration data can come from three sources.
147- # Lowest priority is the config file.
148- config_file = DEFAULT_CFG
149- config = configparser.SafeConfigParser()
150-
151- try:
152- config.readfp(open(config_file))
153- except IOError:
154- logging.warn("Config file %s was not found" % DEFAULT_CFG)
155- logging.warn("Now looking for ENV variables and command line arguments instead")
156- else:
157- try:
158- timeout = config.getfloat("KVM", "timeout")
159- except ValueError:
160- logging.warning('Invalid or Empty timeout in config file. '
161- 'Falling back to default')
162- except configparser.NoSectionError as e:
163- logging.exception(e)
164-
165- try:
166- image = config.get("KVM", "image")
167- except configparser.NoSectionError:
168- logging.exception('Invalid or Empty image in config file.')
169-
170- # Next in priority are environment variables.
171+ # First in priority are environment variables.
172 if 'KVM_TIMEOUT' in os.environ:
173 try:
174 timeout = float(os.environ['KVM_TIMEOUT'])
175
176=== modified file 'providers/plainbox-provider-checkbox/jobs/virtualization.txt.in'
177--- providers/plainbox-provider-checkbox/jobs/virtualization.txt.in 2016-03-21 16:13:00 +0000
178+++ providers/plainbox-provider-checkbox/jobs/virtualization.txt.in 2016-09-12 17:56:36 +0000
179@@ -14,27 +14,3 @@
180 cloud image.
181 _summary:
182 Verify KVM guest boots
183-
184-plugin: shell
185-category_id: 2013.com.canonical.plainbox::virtualization
186-id: virtualization/xen_ok
187-requires: package.name == 'libvirt-bin'
188-user: root
189-estimated_duration: 1.0
190-command: virsh -c xen:/// domstate Domain-0
191-_description:
192- Test to verify that the Xen Hypervisor is running.
193-_summary:
194- Verify Xen is running
195-
196-plugin: shell
197-category_id: 2013.com.canonical.plainbox::virtualization
198-id: virtualization/xen_check_vm
199-depends: virtualization/xen_ok
200-user: root
201-estimated_duration: 300.0
202-command: xen_test /images/xentest.img /images/xentest.xml
203-_description:
204- Test to check that a Xen domU image can boot and run on Xen on Ubuntu
205-_summary:
206- Verify Xen domU can boot

Subscribers

People subscribed via source and target branches