|
6 | 6 |
|
7 | 7 | from jinja2 import Template
|
8 | 8 |
|
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 | + |
10 | 18 |
|
11 | 19 | def validate_classes(campaign_class):
|
12 | 20 | """Checks that the campaign class is valid"""
|
13 |
| - if campaign_class in ['red', 'black', 'green']: |
| 21 | + if campaign_class in CAMPAIGN_CLASSES: |
14 | 22 | 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) |
16 | 24 |
|
17 | 25 |
|
18 | 26 | @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) |
26 | 33 | }
|
27 | 34 |
|
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) |
36 | 35 |
|
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 | + |
38 | 52 |
|
39 | 53 | @task
|
40 | 54 | @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) |
43 | 65 |
|
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) |
45 | 76 |
|
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') |
50 | 77 |
|
51 | 78 | @task
|
52 | 79 | @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