Skip to content

Commit 40e8afd

Browse files
committed
Automate emergency publishing to frontend + static
To publish emergency content we need frontend + static applications checked out locally, make changes, commit, wait for CI to build, merge, deploy etc. In the case of an emergency this is going to take too long. This task builds upon prior art to automate all of the steps in the opsmanual (soon to be updated). See opsmanual for more info on the available tasks.
1 parent c5311c8 commit 40e8afd

File tree

1 file changed

+68
-31
lines changed

1 file changed

+68
-31
lines changed

campaigns.py

Lines changed: 68 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,89 @@
66

77
from jinja2 import Template
88

9-
import vm
9+
import app
10+
11+
12+
env['eagerly_disconnect'] = True
13+
14+
15+
APPLICATIONS = ['frontend', 'static']
16+
CAMPAIGN_CLASSES = ['red', 'black', 'green']
17+
1018

1119
def validate_classes(campaign_class):
1220
"""Checks that the campaign class is valid"""
13-
if campaign_class in ['red', 'black', 'green']:
21+
if campaign_class in CAMPAIGN_CLASSES:
1422
return campaign_class
15-
raise Exception, "Invalid class %s, valid values are 'red', 'black', 'green'" % campaign_class
23+
raise Exception, "Invalid class {}, valid values are {}".format(campaign_class, CAMPAIGN_CLASSES)
1624

1725

1826
@runs_once
19-
def homepage_template():
20-
"""Promotes a campaign to the homepage of GOV.UK"""
21-
context = {
22-
'heading': prompt("Heading for campaign:"),
23-
'extra_info': prompt("Extra information for campaign:"),
24-
'more_info': prompt("Link for more information:"),
25-
'campaign_class': prompt("Campaign class:", validate=validate_classes)
27+
def set_context():
28+
env['context'] = {
29+
'heading': prompt("Heading for campaign:", 'heading'),
30+
'extra_info': prompt("Extra information for campaign:", 'extra_info'),
31+
'more_info': prompt("Link for more information:", 'more_info'),
32+
'campaign_class': prompt("Campaign class:", 'campaign_class', validate=validate_classes)
2633
}
2734

28-
template = Template("""<div id="campaign" class="{{ campaign_class }}">
29-
<div class="campaign-inner">
30-
<h1>{{ heading|e }}</h1>
31-
<p>{{ extra_info|e }}</p>
32-
<a href="{{ more_info|e }}">More information</a>
33-
</div>
34-
</div>""")
35-
env['template_contents'] = template.render(context)
3635

37-
print "Template contents:\n%s" % env['template_contents']
36+
def template(app):
37+
if app == 'frontend':
38+
template = Template("""<div id="campaign" class="{{ campaign_class }}">
39+
<div class="campaign-inner">
40+
<h1>{{ heading|e }}</h1>
41+
<p>{{ extra_info|e }}</p>
42+
<a href="{{ more_info|e }}">More information</a>
43+
</div>
44+
</div>""")
45+
elif app == 'static':
46+
template = Template("""<p>{{ heading|e }}<br />
47+
{{ extra_info|e }}</p>
48+
<a href="#" class="right">{{ more_info|e }}</a>""")
49+
50+
env['template_contents'] = template.render(env.context)
51+
3852

3953
@task
4054
@roles('class-frontend')
41-
def deploy_to_homepage():
42-
execute(homepage_template)
55+
def deploy_banner(application):
56+
execute(template, application)
57+
if application == 'frontend':
58+
remote_filename = '/var/apps/frontend/app/views/root/_campaign_notification.html.erb'
59+
elif application == 'static':
60+
remote_filename = "/var/apps/static/app/views/notifications/banner_{}.erb".format(env.campaign_class)
61+
content = env['template_contents']
62+
put(StringIO.StringIO(content), remote_filename, use_sudo=True, mirror_local_mode=True)
63+
sudo('chown deploy:deploy %s' % remote_filename)
64+
execute(app.reload, application)
4365

44-
contents = env['template_contents']
66+
def remove_banner(application):
67+
if application == 'frontend':
68+
remote_filenames = ['/var/apps/frontend/app/views/root/_campaign_notification.html.erb']
69+
elif application == 'static':
70+
remote_filenames = ["/var/apps/static/app/views/notifications/banner_%s.erb" % i for i in CAMPAIGN_CLASSES]
71+
content = ''
72+
for remote_filename in remote_filenames:
73+
put(StringIO.StringIO(content), remote_filename, use_sudo=True, mirror_local_mode=True)
74+
sudo('chown deploy:deploy %s' % remote_filename)
75+
execute(app.reload, application)
4576

46-
remote_filename = '/var/apps/frontend/app/views/root/_campaign_notification.html.erb'
47-
put(StringIO.StringIO(contents), remote_filename, use_sudo=True, mirror_local_mode=True)
48-
sudo('chown deploy:deploy %s' % remote_filename)
49-
execute(vm.reload_unicorn, name='frontend')
5077

5178
@task
5279
@roles('class-frontend')
53-
def remove_from_homepage():
54-
remote_filename = '/var/apps/frontend/app/views/root/_campaign_notification.html.erb'
55-
put(StringIO.StringIO(''), remote_filename, use_sudo=True, mirror_local_mode=True)
56-
sudo('chown deploy:deploy %s' % remote_filename)
57-
execute(vm.reload_unicorn, name='frontend')
80+
def deploy_emergency_banner():
81+
"""Deploy an emergency banner to GOV.UK"""
82+
execute(set_context)
83+
for application in APPLICATIONS:
84+
# Black banners are only placed on the homepage i.e.'frontend'.
85+
# Don't deploy a black banner to the static application.
86+
if not (env.campaign_class == 'black' and application == 'static'):
87+
deploy_banner(application)
88+
89+
@task
90+
@roles('class-frontend')
91+
def remove_emergency_banner():
92+
"""Remove all banners from GOV.UK"""
93+
for application in APPLICATIONS:
94+
remove_banner(application)

0 commit comments

Comments
 (0)