-
Notifications
You must be signed in to change notification settings - Fork 12
/
remote.py
306 lines (255 loc) · 10 KB
/
remote.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
from . import remote_core
from .environment import Environment
from .utils import notify, cmd
import execnet
import logging
import os
import os.path
import subprocess
import sys
import tempfile
# Monkeypatch execnet to support vagrant ssh.
def new_ssh_args(spec):
from execnet.gateway_io import popen_bootstrapline
remotepython = spec.python or 'python'
if spec.type == 'vagrant':
args = ['vagrant', 'ssh', spec.ssh, '--', '-C']
else:
args = ['ssh', '-C']
if spec.ssh_config is not None:
args.extend(['-F', str(spec.ssh_config)])
remotecmd = '%s -c "%s"' % (remotepython, popen_bootstrapline)
if spec.type == 'vagrant':
args.extend([remotecmd])
else:
args.extend([spec.ssh, remotecmd])
return args
import execnet.gateway_io
execnet.gateway_io.ssh_args = new_ssh_args
logger = logging.getLogger('batou.remote')
def main(environment, timeout, dirty):
environment = Environment(environment)
environment.load()
if timeout is not None:
environment.timeout = timeout
if environment.update_method == 'rsync':
pass
elif not dirty:
check_clean_hg_repository()
environment.load_secrets()
deployment = RemoteDeployment(environment, dirty)
try:
deployment()
except Exception:
logger.error('', exc_info=True)
sys.exit(1)
else:
notify('Deployment finished',
'{} was deployed successfully.'.format(environment.name))
def check_clean_hg_repository():
# Safety belt that we're acting on a clean repository.
try:
status, _ = cmd('hg -q stat', silent=True)
except RuntimeError:
logger.error('Unable to check repository status. '
'Is there an HG repository here?')
sys.exit(1)
else:
status = status.strip()
if status.strip():
logger.error("""\
Your repository has uncommitted changes.
I am refusing to deploy in this situation as the results will be unpredictable.
Please commit and push first.
""")
logger.error(status)
sys.exit(1)
try:
cmd('hg -q outgoing -l 1', acceptable_returncodes=[1])
except RuntimeError:
logger.error("""\
Your repository has outgoing changes.
I am refusing to deploy in this situation as the results will be unpredictable.
Please push first.
""")
sys.exit(1)
class RemoteDeployment(object):
def __init__(self, environment, dirty):
self.environment = environment
self.dirty = dirty
self.upstream = cmd('hg showconfig paths')[0].split('\n')[0].strip()
assert self.upstream.startswith('paths.default')
self.upstream = self.upstream.split('=')[1]
self.repository_root = subprocess.check_output(['hg', 'root']).strip()
self.deployment_base = os.path.relpath(
self.environment.base_dir, self.repository_root)
assert (self.deployment_base == '.' or
self.deployment_base[0] not in ['.', '/'])
def __call__(self):
remotes = {}
for host in self.environment.hosts.values():
remote = RemoteHost(host, self)
remotes[host] = remote
remote.connect()
# Bootstrap and get channel to batou environment
for remote in remotes.values():
remote.start()
ref_remote = remotes.values()[0]
roots = ref_remote.roots_in_order()
for host, component in roots:
host = self.environment.hosts[host]
remote = remotes[host]
remote.deploy_component(component)
for remote in remotes.values():
remote.gateway.exit()
class RPCWrapper(object):
def __init__(self, host):
self.host = host
def __getattr__(self, name):
def call(*args, **kw):
logger.debug('rpc {}: {}(*{}, **{})'.format
(self.host.host.fqdn, name, args, kw))
self.host.channel.send((name, args, kw))
result = self.host.channel.receive()
logger.debug('result: {}'.format(result))
try:
result[0]
except (TypeError, IndexError):
pass
else:
if result[0] == 'batou-remote-core-error':
logger.error(result[1])
raise RuntimeError('Remote exception encountered.')
return result
return call
def send_file(self, local, remote):
self.host.channel.send(('send_file', [remote], {}))
with open(local, 'r') as f:
while True:
data = f.read(64*1024)
if not data:
self.host.channel.send(('finish', ''))
break
else:
self.host.channel.send((None, data))
result = self.host.channel.receive()
logger.debug('result: {}'.format(result))
try:
result[0]
except TypeError:
pass
else:
if result[0] == 'batou-remote-core-error':
logger.error(result[1])
raise RuntimeError('Remote exception encountered.')
assert result == 'OK'
class RemoteHost(object):
gateway = None
def __init__(self, host, deployment):
self.host = host
self.deployment = deployment
self.rpc = RPCWrapper(self)
def connect(self, interpreter='python2.7'):
if not self.gateway:
logger.info('{}: connecting'.format(self.host.fqdn))
else:
logger.info('{}: reconnecting'.format(self.host.fqdn))
self.gateway.exit()
self.gateway = execnet.makegateway(
"ssh={}//python={}//type={}".format(
self.host.fqdn, interpreter,
self.deployment.environment.connect_method))
self.channel = self.gateway.remote_exec(remote_core)
if self.rpc.whoami() != self.deployment.environment.service_user:
self.gateway.exit()
self.gateway = execnet.makegateway(
"ssh={}//python=sudo -u {} {}//type={}".format(
self.host.fqdn,
self.deployment.environment.service_user,
interpreter,
self.deployment.environment.connect_method))
self.channel = self.gateway.remote_exec(remote_core)
def update_hg_bundle(self):
heads = self.rpc.current_heads()
if not heads:
raise ValueError("Remote repository did not find any heads. "
"Can not continue creating a bundle.")
fd, bundle_file = tempfile.mkstemp()
os.close(fd)
bases = ' '.join('--base {}'.format(x) for x in heads)
cmd('hg -qy bundle {} {}'.format(bases, bundle_file),
acceptable_returncodes=[0, 1])
have_changes = os.stat(bundle_file).st_size > 0
self.rpc.send_file(
bundle_file, self.remote_repository + '/batou-bundle.hg')
os.unlink(bundle_file)
if have_changes:
self.rpc.unbundle_code()
def update_hg(self):
env = self.deployment.environment
if env.update_method == 'pull':
self.rpc.pull_code(
upstream=self.deployment.upstream)
elif env.update_method == 'bundle':
self.update_hg_bundle()
remote_id = self.rpc.update_working_copy(env.branch)
local_id, _ = cmd('hg id -i')
if self.deployment.dirty:
local_id = local_id.replace('+', '')
local_id = local_id.strip()
if remote_id != local_id:
raise RuntimeError(
'Working copy parents differ. Local: {} Remote: {}'.format(
local_id, remote_id))
remote_id = self.rpc.update_working_copy(env.branch)
local_id, _ = cmd('hg id -i')
if self.deployment.dirty:
local_id = local_id.replace('+', '')
local_id = local_id.strip()
if remote_id != local_id:
raise RuntimeError(
'Working copy parents differ. Local: {} Remote: {}'.format(
local_id, remote_id))
def update_rsync(self):
env = self.deployment.environment
blacklist = ['.batou', 'work', '.git', '.hg', '.vagrant']
for source in os.listdir(env.base_dir):
if source in blacklist:
continue
rsync = execnet.RSync(os.path.join(env.base_dir, source))
rsync.add_target(self.gateway,
os.path.join(self.remote_base, source))
rsync.send()
def start(self):
logger.info('{}: bootstrapping'.format(self.host.fqdn))
self.rpc.lock()
env = self.deployment.environment
self.remote_repository = self.rpc.ensure_repository(
env.target_directory, env.update_method)
self.remote_base = os.path.join(
self.remote_repository, self.deployment.deployment_base)
if env.update_method in ['pull', 'bundle']:
self.update_hg()
elif env.update_method == 'rsync':
self.update_rsync()
else:
raise ValueError(
'unsupported update method: {}'.format(env.update_method))
self.rpc.build_batou(self.deployment.deployment_base)
# Now, replace the basic interpreter connection, with a "real" one that
# has all our dependencies installed.
self.connect(self.remote_base + '/.batou/bin/python')
# Since we reconnected, any state on the remote side has been lost, so
# we need to set the target directory again (which we only can know
# about locally). XXX This is quite convoluted.
self.rpc.ensure_repository(env.target_directory, env.update_method)
self.rpc.setup_deployment(
self.deployment.deployment_base,
env.name,
self.host.fqdn,
env.overrides)
def deploy_component(self, component):
logger.info('Deploying {}/{}'.format(self.host.fqdn, component))
self.rpc.deploy(component)
def roots_in_order(self):
return self.rpc.roots_in_order()