Merge lp:~cprov/adt-cloud-worker/swift-uploads into lp:adt-cloud-worker

Proposed by Celso Providelo
Status: Merged
Merged at revision: 8
Proposed branch: lp:~cprov/adt-cloud-worker/swift-uploads
Merge into: lp:adt-cloud-worker
Diff against target: 75 lines (+33/-7)
1 file modified
adt-cloud-worker.py (+33/-7)
To merge this branch: bzr merge lp:~cprov/adt-cloud-worker/swift-uploads
Reviewer Review Type Date Requested Status
Evan (community) Approve
Review via email: mp+251635@code.launchpad.net

Description of the change

Uploading adt results to swift as adt-<req_id>/results.tgz.

To post a comment you must log in.
Revision history for this message
Evan (ev) wrote :

Looks good. Just a couple of side questions.

review: Approve
9. By Celso Providelo

Worker uploads results.tgz.tmp to swift and rely on result checker to rename it if it's valid.

Revision history for this message
Celso Providelo (cprov) wrote :

Evan,

Thanks for the review, comments addressed.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'adt-cloud-worker.py'
2--- adt-cloud-worker.py 2015-03-03 16:27:28 +0000
3+++ adt-cloud-worker.py 2015-03-03 19:03:39 +0000
4@@ -31,6 +31,8 @@
5 import os
6 import shutil
7 import subprocess
8+from swiftclient import client
9+import tarfile
10 import tempfile
11
12 logger = get_logger(__name__)
13@@ -61,31 +63,55 @@
14 """
15 logger.info('Got: {}'.format(body))
16 # TODO: body validation
17- message.ack()
18 body['worker'] = self.name
19 # Run requested tests safely.
20 try:
21 # Create and temporary directory for storing adt results.
22 result_dir = tempfile.mkdtemp(
23 prefix='adt-{}-{}'.format(self.name, body['request_id']))
24+ # Run `adt-run`.
25 adt_kwargs = body.copy()
26 adt_kwargs['result_dir'] = result_dir
27 body['exit_code'] = self.run_adt(**adt_kwargs)
28- # TODO: tar/gzip the output directory.
29- # TODO: upload to swift instance at 'swift_container'
30+ # Compress the results directory in a temporary file.
31+ _unused, tarball_path = tempfile.mkstemp()
32+ with tarfile.open(tarball_path, "w:gz") as tar:
33+ for entry in os.listdir(result_dir):
34+ tar.add(os.path.join(result_dir, entry), arcname=entry)
35+ # Upload the results tarball to swift with a temporary name
36+ # 'adt-<req_id>/results.tgz.tmp', result checker will rename
37+ # if after verification.
38+ swift_client = client.Connection(
39+ authurl=self.config.get('nova', 'os_auth_url'),
40+ user=self.config.get('nova', 'os_username'),
41+ key=self.config.get('nova', 'os_password'),
42+ os_options={
43+ 'tenant_name': self.config.get('nova', 'os_tenant_name'),
44+ 'region_name': self.config.get('nova', 'os_region_name'),
45+ }, auth_version=(
46+ '/v1.' in self.config.get('nova','os_auth_url') and '1.0'
47+ or '2.0'))
48+ container_name = 'adt-{}'.format(body['request_id'])
49+ swift_client.put_container(container_name)
50+ with open(tarball_path) as fd:
51+ swift_client.put_object(
52+ container_name, obj='results.tgz.tmp', contents=fd,
53+ content_type='application/x-compressed')
54 except Exception as e:
55 # Unexpected failures ...
56 logger.error(e, exc_info=True)
57 body['exit_code'] = '100'
58 finally:
59- # Drop the result directory and post results
60- shutil.rmtree(result_dir)
61+ # The post results
62 # TODO: send error logging to result-checker in the message
63- # TODO: resilience agains rabbit hiccups, otherwise the request
64- # will be lost.
65 queue = self.connection.SimpleQueue('adt.results')
66 queue.put(body)
67 queue.close()
68+ # Drop the result directory and result tarball.
69+ shutil.rmtree(result_dir)
70+ os.remove(tarball_path)
71+ message.ack()
72+ logger.info('Done!')
73
74 def run_adt(self, **kwargs):
75 """Run adt-run according to the given request parameters.

Subscribers

People subscribed via source and target branches