Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c4c2ece

Browse files
author
Richard Boulton
committed
Add fabric commands for doing topic_changes
topic_change:prepare builds CSV files for whitehall and publisher, listing the changes required on each slug to change them from being tagged with a source topic to being tagged with a destination topic. topic_change:process then applies a set of such CSVs
1 parent 0a8dc33 commit c4c2ece

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

fabfile.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import rkhunter
3434
import search
3535
import statsd
36+
import topic_change
3637
import vm
3738

3839
HERE = os.path.dirname(__file__)

topic_change.py

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
from fabric import api
2+
3+
import glob
4+
import os
5+
import re
6+
import time
7+
import util
8+
9+
10+
@api.task
11+
def prepare(output_directory, source_topic, destination_topic):
12+
"""Build CSV files for moving all content.
13+
14+
:param output_directory: The directory that CSV files should be placed in.
15+
16+
:param source_topic: The existing topic tag content is tagged to.
17+
18+
:param source_topic: The new topic tag that content should be tagged to.
19+
20+
A separate CSV file is created for each app, by running tasks on an
21+
appropriate backend machine.
22+
23+
The CSV files are then downloaded to the local directory specified.
24+
25+
"""
26+
if not os.path.isdir(output_directory):
27+
os.makedirs(output_directory)
28+
prepare_publisher_csv(output_directory, source_topic, destination_topic)
29+
prepare_whitehall_csv(output_directory, source_topic, destination_topic)
30+
31+
32+
@api.task
33+
def process(file_pattern):
34+
"""Process topic change CSVs.
35+
36+
Processes all CSVs matching the given file pattern glob. This will apply
37+
the listed changes to content in whitehall and publisher (updating all the
38+
appropriate things).
39+
40+
:param file_pattern: A glob-style pattern used to determine which CSV files
41+
to process.
42+
43+
"""
44+
paths_by_app = {}
45+
for path in glob.glob(file_pattern):
46+
app = os.path.basename(path).split("_")[0]
47+
paths_by_app.setdefault(app, []).append(path)
48+
49+
process_publisher_csvs(paths_by_app.pop("publisher", []))
50+
process_whitehall_csvs(paths_by_app.pop("whitehall", []))
51+
if paths_by_app:
52+
print "WARNING: Ignoring files for unknown apps: %s" % (
53+
', '.join(paths_by_app.keys()))
54+
55+
56+
def slugify(topic):
57+
return re.sub('[^a-z0-9]', '_', topic.lower())
58+
59+
60+
def build_filename(app, source_topic, destination_topic):
61+
return '__'.join([
62+
app,
63+
slugify(source_topic),
64+
slugify(destination_topic),
65+
str(int(time.time())),
66+
]) + '.csv'
67+
68+
69+
def prepare_publisher_csv(output_directory, source_topic, destination_topic):
70+
util.use_random_host('class-backend')
71+
print("Fetching publisher change CSV from %s" % (api.env.host_string,))
72+
73+
filename = build_filename('publisher', source_topic, destination_topic)
74+
remote_path = '/var/apps/publisher/tmp/%s' % (filename,)
75+
util.rake('publisher', 'topic_changes:prepare',
76+
source_topic,
77+
destination_topic,
78+
remote_path)
79+
api.get(remote_path, os.path.join(output_directory, filename))
80+
api.sudo("rm '%s'" % (remote_path,), user="deploy")
81+
82+
83+
def prepare_whitehall_csv(output_directory, source_topic, destination_topic):
84+
util.use_random_host('class-whitehall_backend')
85+
print("Fetching whitehall change CSV from %s" % (api.env.host_string,))
86+
87+
filename = build_filename('whitehall', source_topic, destination_topic)
88+
remote_path = '/var/apps/whitehall/tmp/%s' % (filename,)
89+
util.rake('whitehall', 'topic_retagging_csv_export',
90+
SOURCE=source_topic,
91+
DESTINATION=destination_topic,
92+
CSV_LOCATION=remote_path,
93+
)
94+
api.get(remote_path, os.path.join(output_directory, filename))
95+
api.sudo("rm '%s'" % (remote_path,), user="deploy")
96+
97+
98+
def process_publisher_csvs(paths):
99+
util.use_random_host('class-backend')
100+
for path in paths:
101+
process_publisher_csv(path)
102+
103+
104+
def process_publisher_csv(path):
105+
filename = os.path.basename(path)
106+
print("Applying publisher change CSV %s on %s" % (
107+
filename,
108+
api.env.host_string,
109+
))
110+
111+
remote_path = '/var/apps/publisher/tmp/%s' % (filename, )
112+
api.put(path, remote_path, use_sudo=True)
113+
util.rake('publisher', 'topic_changes:process', remote_path)
114+
api.sudo("rm '%s'" % (remote_path,))
115+
116+
117+
def process_whitehall_csvs(paths):
118+
util.use_random_host('class-whitehall_backend')
119+
for path in paths:
120+
process_whitehall_csv(path)
121+
122+
123+
def process_whitehall_csv(path):
124+
filename = os.path.basename(path)
125+
print("Applying whitehall change CSV %s on %s" % (
126+
filename,
127+
api.env.host_string,
128+
))
129+
130+
remote_path = '/var/apps/whitehall/tmp/%s' % (filename, )
131+
api.put(path, remote_path, use_sudo=True)
132+
util.rake('whitehall', 'process_topic_retagging_csv',
133+
CSV_LOCATION=remote_path)
134+
api.sudo("rm '%s'" % (remote_path,))

0 commit comments

Comments
 (0)