-
Notifications
You must be signed in to change notification settings - Fork 1
/
swiftq-example.py
executable file
·165 lines (133 loc) · 5.51 KB
/
swiftq-example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python3
# swiftq-example.py (part of voipgrid/swiftdrop) contains examples how to
# handle swiftdrop queued emails
# Copyright (C) 2019 Walter Doekes, Harm Geerts, VoIPGRID B.V.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from argparse import ArgumentParser
from configparser import ConfigParser
from random import choice
from swiftclient import Connection
import sys
class BogoFileLock:
def __init__(self):
self._files = set()
def acquire(self, filename):
assert filename not in self._files, (filename, self._files)
if choice([True, False]):
print('BogoLock.acquire(', filename, ')')
self._files.add(filename)
else:
raise ValueError(
'{} already locked by someone else (example)'.format(filename))
def release(self, filename):
assert filename in self._files, (filename, self._files)
print('BogoLock.release(', filename, ')')
self._files.remove(filename)
BOGO_FILE_LOCK = BogoFileLock()
class locked:
def __init__(self, lock, id_):
self.lock = lock
self.id_ = id_
def __enter__(self):
self.lock.acquire(self.id_)
def __exit__(self, type, value, tb):
self.lock.release(self.id_)
class SwiftEmailViewer(object):
def __init__(self, config_section):
self.conn = self.get_connection(config_section)
self.container = config_section['container']
def get_connection(self, config):
timeout = (int(config['timeout']) if config.get('timeout') else None)
if config['auth_version'] == '3':
# Keystone v3
os_options = {}
for option, value in config.items():
if option.startswith('os_options_') and value:
short_option = option[len('os_options_'):]
os_options[short_option] = config.get(option)
connection = Connection(
auth_version='3', authurl=config['authurl'],
user=config['user'], key=config['key'],
os_options=os_options, timeout=timeout)
elif config['auth_version'] == '1':
# Legacy auth
connection = Connection(
auth_version='1', authurl=config['authurl'],
user=config['user'], key=config['key'],
tenant_name=config['tenant_name'], timeout=timeout)
else:
raise NotImplementedError('auth_version? {!r}'.format(config))
return connection
def list(self, subdir):
assert subdir in ('cur', 'processing', 'retry', 'failed', 'done')
resp_headers, obj_contents = self.conn.get_container(
self.container, path=subdir)
for f in obj_contents:
print('{} {}'.format(f['last_modified'], f['name']))
def dequeue(self, subdir, filename): # move from cur->processing
assert subdir in ('cur', 'retry') # 'failed', 'done'
with locked(BOGO_FILE_LOCK, filename):
obj = self._download('{}/{}'.format(subdir, filename))
self._rename(
'{}/{}'.format(subdir, filename),
'processing/{}'.format(filename))
print(obj)
def finish(self, subdir, filename):
assert subdir in ('retry', 'failed', 'done')
with locked(BOGO_FILE_LOCK, filename):
self._rename(
'processing/{}'.format(filename),
'{}/{}'.format(subdir, filename))
def _download(self, path):
resp_headers, obj_contents = self.conn.get_object(self.container, path)
#print(resp_headers)
#print(obj_contents)
return obj_contents # bytes()
def _rename(self, path, newpath):
ret = self.conn.copy_object(
self.container, path, destination='/{}/{}'.format(
self.container, newpath))
assert ret is None, ret
ret = self.conn.delete_object(self.container, path)
assert ret is None, ret
def main():
parser = ArgumentParser(
description='List/dequeue mails from swiftdrop.')
parser.add_argument(
'--config', metavar='FILE', default='/etc/swiftdrop.ini',
help='The configuration file')
parser.add_argument(
'--section', metavar='SECTION', default='DEFAULT',
help='Which section from the config to use')
parser.add_argument(
'command', choices=('list', 'dequeue', 'finish'),
help='What to do')
parser.add_argument(
'args', nargs='+', help='Command parameters')
args = parser.parse_args()
config = ConfigParser(allow_no_value=True)
with open(args.config, 'r') as f:
config.read_file(f)
app = SwiftEmailViewer(config[args.section])
cmd = getattr(app, args.command)
cmd(*args.args)
if __name__ == '__main__':
try:
main()
except Exception as e:
import traceback
traceback.print_exc()
print('mail transport unavailable: {}'.format(e), file=sys.stderr)
sys.exit(1)