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
=== modified file 'adt-cloud-worker.py'
--- adt-cloud-worker.py 2015-03-03 16:27:28 +0000
+++ adt-cloud-worker.py 2015-03-03 19:03:39 +0000
@@ -31,6 +31,8 @@
31import os31import os
32import shutil32import shutil
33import subprocess33import subprocess
34from swiftclient import client
35import tarfile
34import tempfile36import tempfile
3537
36logger = get_logger(__name__)38logger = get_logger(__name__)
@@ -61,31 +63,55 @@
61 """63 """
62 logger.info('Got: {}'.format(body))64 logger.info('Got: {}'.format(body))
63 # TODO: body validation65 # TODO: body validation
64 message.ack()
65 body['worker'] = self.name66 body['worker'] = self.name
66 # Run requested tests safely.67 # Run requested tests safely.
67 try:68 try:
68 # Create and temporary directory for storing adt results.69 # Create and temporary directory for storing adt results.
69 result_dir = tempfile.mkdtemp(70 result_dir = tempfile.mkdtemp(
70 prefix='adt-{}-{}'.format(self.name, body['request_id']))71 prefix='adt-{}-{}'.format(self.name, body['request_id']))
72 # Run `adt-run`.
71 adt_kwargs = body.copy()73 adt_kwargs = body.copy()
72 adt_kwargs['result_dir'] = result_dir74 adt_kwargs['result_dir'] = result_dir
73 body['exit_code'] = self.run_adt(**adt_kwargs)75 body['exit_code'] = self.run_adt(**adt_kwargs)
74 # TODO: tar/gzip the output directory.76 # Compress the results directory in a temporary file.
75 # TODO: upload to swift instance at 'swift_container'77 _unused, tarball_path = tempfile.mkstemp()
78 with tarfile.open(tarball_path, "w:gz") as tar:
79 for entry in os.listdir(result_dir):
80 tar.add(os.path.join(result_dir, entry), arcname=entry)
81 # Upload the results tarball to swift with a temporary name
82 # 'adt-<req_id>/results.tgz.tmp', result checker will rename
83 # if after verification.
84 swift_client = client.Connection(
85 authurl=self.config.get('nova', 'os_auth_url'),
86 user=self.config.get('nova', 'os_username'),
87 key=self.config.get('nova', 'os_password'),
88 os_options={
89 'tenant_name': self.config.get('nova', 'os_tenant_name'),
90 'region_name': self.config.get('nova', 'os_region_name'),
91 }, auth_version=(
92 '/v1.' in self.config.get('nova','os_auth_url') and '1.0'
93 or '2.0'))
94 container_name = 'adt-{}'.format(body['request_id'])
95 swift_client.put_container(container_name)
96 with open(tarball_path) as fd:
97 swift_client.put_object(
98 container_name, obj='results.tgz.tmp', contents=fd,
99 content_type='application/x-compressed')
76 except Exception as e:100 except Exception as e:
77 # Unexpected failures ... 101 # Unexpected failures ...
78 logger.error(e, exc_info=True)102 logger.error(e, exc_info=True)
79 body['exit_code'] = '100'103 body['exit_code'] = '100'
80 finally:104 finally:
81 # Drop the result directory and post results105 # The post results
82 shutil.rmtree(result_dir)
83 # TODO: send error logging to result-checker in the message106 # TODO: send error logging to result-checker in the message
84 # TODO: resilience agains rabbit hiccups, otherwise the request
85 # will be lost.
86 queue = self.connection.SimpleQueue('adt.results')107 queue = self.connection.SimpleQueue('adt.results')
87 queue.put(body)108 queue.put(body)
88 queue.close()109 queue.close()
110 # Drop the result directory and result tarball.
111 shutil.rmtree(result_dir)
112 os.remove(tarball_path)
113 message.ack()
114 logger.info('Done!')
89115
90 def run_adt(self, **kwargs):116 def run_adt(self, **kwargs):
91 """Run adt-run according to the given request parameters.117 """Run adt-run according to the given request parameters.

Subscribers

People subscribed via source and target branches