Merge lp:~thomir-deactivatedaccount/core-result-checker/trunk-add-worker into lp:core-result-checker

Proposed by Thomi Richards
Status: Merged
Approved by: Thomi Richards
Approved revision: 13
Merged at revision: 11
Proposed branch: lp:~thomir-deactivatedaccount/core-result-checker/trunk-add-worker
Merge into: lp:core-result-checker
Diff against target: 165 lines (+111/-8)
4 files modified
.bzrignore (+1/-0)
core-service.conf (+8/-0)
core_result_checker/__init__.py (+99/-7)
requirements.txt (+3/-1)
To merge this branch: bzr merge lp:~thomir-deactivatedaccount/core-result-checker/trunk-add-worker
Reviewer Review Type Date Requested Status
Celso Providelo (community) Approve
Review via email: mp+255312@code.launchpad.net

Commit message

Basic implementation for the result-checker.

Description of the change

Basic implementation for the result-checker.

To post a comment you must log in.
13. By Thomi Richards

Check success of swift post operation, and log error as appropriate.

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

Thomi,

Very cool, let's see it in action.

[]

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file '.bzrignore'
2--- .bzrignore 2015-03-25 04:59:12 +0000
3+++ .bzrignore 2015-04-06 23:23:40 +0000
4@@ -1,1 +1,2 @@
5 core_result_checker.egg-info
6+secret.conf
7
8=== added file 'core-service.conf'
9--- core-service.conf 1970-01-01 00:00:00 +0000
10+++ core-service.conf 2015-04-06 23:23:40 +0000
11@@ -0,0 +1,8 @@
12+[amqp]
13+uris = amqp://guest:guest@localhost:5672//
14+
15+[nova]
16+os_username = foo
17+os_password = <redacted>
18+os_tenant_name = foo_project
19+os_auth_url = http://172.20.161.138:5000/v2.0/
20\ No newline at end of file
21
22=== modified file 'core_result_checker/__init__.py'
23--- core_result_checker/__init__.py 2015-04-03 01:43:12 +0000
24+++ core_result_checker/__init__.py 2015-04-06 23:23:40 +0000
25@@ -19,14 +19,65 @@
26 import configparser
27 import logging
28 import os
29-import select
30
31-from uservice_utils.logging import configure_service_logging
32+import kombu
33+from swiftclient.service import SwiftService
34+from uservice_utils.logging import (
35+ configure_service_logging,
36+ ExtraLogger,
37+)
38+from uservice_utils.queue import (
39+ DefaultRetryPolicy,
40+ MessageActions,
41+ SimpleRabbitQueueWorker,
42+)
43
44 from core_result_checker import constants
45
46
47-logger = logging.getLogger(__name__)
48+class Worker(object):
49+
50+ def __init__(self, swift_publisher):
51+ get_logger(__name__).info("Service Started.")
52+ self.swift_publisher = swift_publisher
53+
54+ def __call__(self, message):
55+ logger = get_logger(__name__, message)
56+ logger.info("Got: %r", message)
57+ try:
58+ channel = message['channel']
59+ device = message['device']
60+ image_name = message['image_name']
61+ except KeyError as e:
62+ logger.error("Unable to unpack incoming message: %s", str(e))
63+ return MessageActions.Retry
64+
65+ container_name = "core-{}-{}-{}".format(
66+ channel,
67+ device,
68+ image_name,
69+ )
70+ try:
71+ self.swift_publisher.make_container_public(container_name)
72+ except RuntimeError as e:
73+ logger.error(
74+ "Unable to publish swift container '%s'. Error: %s.",
75+ container_name,
76+ e
77+ )
78+ return MessageActions.Retry
79+ logger.info("Swift container made public OK.")
80+ return MessageActions.Acknowledge
81+
82+
83+def get_logger(name, extra={}):
84+ if logging.getLoggerClass() != ExtraLogger:
85+ logging.setLoggerClass(ExtraLogger)
86+ logger = logging.getLogger(name)
87+ e = constants.LOGGING_EXTRA.copy()
88+ e.update(extra)
89+ logger.set_extra_args(e)
90+ return logger
91
92
93 def read_config():
94@@ -45,6 +96,33 @@
95 return config
96
97
98+class SwiftPublisher(object):
99+
100+ """A class that knows how to make a swift container public."""
101+
102+ def __init__(self, nova_config):
103+ self.config = nova_config
104+
105+ def make_container_public(self, container_name):
106+ """Make a swift contianer public.
107+
108+ Raises RuntimeError on failure, returns None otherwise.
109+
110+ """
111+ swift = SwiftService(self.config)
112+ result = swift.post(
113+ container=container_name,
114+ options=dict(read_acl='.r:*')
115+ )
116+ if not result['success']:
117+ raise RuntimeError(
118+ "Unable to make swift container '%s' public: %s" % (
119+ container_name,
120+ result['error']
121+ )
122+ )
123+
124+
125 def main():
126 config = read_config()
127 log_path = os.path.abspath(
128@@ -54,7 +132,21 @@
129 config['logstash'] if 'logstash' in config else None
130 )
131
132- logger.info('Started!', extra=constants.LOGGING_EXTRA)
133-
134- # for now, do nothing at all.
135- select.select([], [], [])
136+ amqp_uris = config.get('amqp', 'uris').split()
137+ swift_publisher = SwiftPublisher(config['nova'])
138+ worker = Worker(swift_publisher)
139+ retry_policy = DefaultRetryPolicy(
140+ max_retries=3,
141+ dead_queue='core.deadletters.{}'.format(constants.API_VERSION)
142+ )
143+ try:
144+ with kombu.Connection(amqp_uris) as connection:
145+ queue_monitor = SimpleRabbitQueueWorker(
146+ connection,
147+ "core.result.{}".format(constants.API_VERSION),
148+ worker,
149+ retry_policy,
150+ )
151+ queue_monitor.run()
152+ except KeyboardInterrupt:
153+ print("Bye")
154
155=== modified file 'requirements.txt'
156--- requirements.txt 2015-04-03 01:41:47 +0000
157+++ requirements.txt 2015-04-06 23:23:40 +0000
158@@ -1,3 +1,5 @@
159 kombu==3.0.24
160+python-keystoneclient==1.3.0
161 python-logstash==0.4.2
162-uservice-utils==1.0.2.1
163+python-swiftclient==2.4.0
164+uservice-utils==1.0.2.1
165\ No newline at end of file

Subscribers

People subscribed via source and target branches

to all changes: