This repository was archived by the owner on Nov 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathfabfile.py
245 lines (184 loc) · 5.61 KB
/
fabfile.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
from __future__ import print_function
from git import Repo
import os
import re
import textwrap
import time
from fabric import state
from fabric.api import (abort, env, hide, hosts, local, puts, run,
runs_once, serial, settings, sudo, task, warn)
from fabric.task_utils import crawl
# Other submodules such as mapit depend on puppet, so include this one first
import puppet
# Our command submodules
import app
import apt
import bundler
import cache
import ckan
import elasticsearch
import filebeat
import incident
import jenkins
import locksmith
import logstream
import mongo
import mysql
import nagios
import nginx
import ntp
import postgresql
import rabbitmq
import rbenv
import vm
import publisher_maintenance
HERE = os.path.dirname(__file__)
# When to warn that you haven't pulled the repo recently.
REPO_OUTDATED_TIME = 3600 * 24 * 5
REPO_OUTDATED_FILE = os.path.join(HERE, '.git/FETCH_HEAD')
def fetch_hosts(options=''):
command = "govuk_node_list %s" % options
with hide('running', 'stdout'):
if env.gateway:
with settings(host_string=env.gateway, gateway=None):
hosts = run(command).splitlines()
# Otherwise assume we're *in* the infrastructure
else:
hosts = local(command).splitlines()
return map(lambda host: host.split(".")[0], hosts)
def _check_repo_age():
if not os.path.exists(REPO_OUTDATED_FILE):
return
if time.time() - os.path.getmtime(REPO_OUTDATED_FILE) > REPO_OUTDATED_TIME:
repo = Repo(os.getcwd())
current_branch = repo.active_branch.name
if current_branch == 'master':
repo.remotes.origin.pull()
else:
warn('Your fabric-scripts may be out-of-date. Please `git pull` the repo')
@task
def help(name=""):
"""Show extended help for a task (e.g. 'fab help:search.reindex')"""
from fabric.main import show_commands
if not name:
puts("\nFor more information on a task run `fab help:<task>`.\n")
show_commands(None, 'short')
task = crawl(name, state.commands)
if task is None:
abort("%r is not a valid task name" % task)
puts(textwrap.dedent(task.__doc__).strip())
@task
def production():
"""Select production environment"""
env['environment'] = 'production'
env['aws_migration'] = False
env.gateway = 'jumpbox.publishing.service.gov.uk'
@task
def staging():
"""Select staging environment"""
env['environment'] = 'staging'
env['aws_migration'] = False
env.gateway = 'jumpbox.staging.publishing.service.gov.uk'
@task
def test():
"""Select test environment"""
env['environment'] = 'test'
env['aws_migration'] = True
env.gateway = 'jumpbox.pink.test.govuk.digital'
@task
def integration():
"""Select integration environment"""
env['environment'] = 'integration'
env['aws_migration'] = True
env.gateway = 'jumpbox.blue.integration.govuk.digital'
@task
def training():
"""Select training environment"""
env['environment'] = 'training'
env['aws_migration'] = True
env.gateway = 'jumpbox.training.govuk.digital'
@task
def aws_staging(stackname=None):
if not stackname:
stackname = 'blue'
"""Select GOV.UK AWS Staging environment"""
env['environment'] = 'staging'
env['aws_migration'] = True
env.gateway = 'jumpbox.blue.staging.govuk.digital'
@task
def staging_aws(stackname=None):
return aws_staging(stackname)
@task
def aws_production(stackname=None):
if not stackname:
stackname = 'blue'
"""Select GOV.UK AWS Production environment"""
env['environment'] = 'production'
env['aws_migration'] = True
env.gateway = 'jumpbox.blue.production.govuk.digital'
@task
def production_aws(stackname=None):
return aws_production(stackname)
@task
def ci():
"""Select CI environment"""
env['environment'] = 'ci'
env['aws_migration'] = False
env.gateway = 'ci-jumpbox.integration.publishing.service.gov.uk'
@task
def all():
"""Select all machines in current environment"""
env.hosts.extend(fetch_hosts())
@task(name='class')
def klass(*class_names):
"""Select a machine class"""
for class_name in class_names:
class_name = class_name.replace("-", "_")
env.hosts.extend(fetch_hosts("-c %s" % class_name))
@task
@serial
@hosts('localhost')
def puppet_class(*class_names):
"""Select all machines which include a given puppet class"""
for class_name in class_names:
env.hosts.extend(fetch_hosts("-C %s" % class_name))
@task
@runs_once
def application(app_name):
"""Select all machines which host a given application"""
class_name = 'govuk::apps::{}'.format(app_name.replace('-', '_'))
puppet_class(class_name)
@task
def node_type(node_name):
"""Select all machines of a given node type"""
class_name = 'govuk::node::s_{}'.format(node_name.replace('-', '_'))
puppet_class(class_name)
@task
@runs_once
@hosts(["localhost"])
def classes():
"""List available classes"""
with hide("everything"):
if not env["aws_migration"]:
classes = set()
for host in fetch_hosts():
classes.add(re.split('-[0-9]', host)[0].replace('-', '_'))
print("\n".join(classes))
else:
print(run("govuk_node_list --classes"))
@task
@runs_once
def hosts():
"""List selected hosts"""
me = state.commands['hosts']
hosts = me.get_hosts(None, None, None, env)
print('\n'.join(sorted(hosts)))
@task
def do(command):
"""Execute arbitrary commands"""
run(command)
@task
def sdo(command):
"""Execute arbitrary commands with sudo"""
sudo(command)
_check_repo_age()