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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
#!/usr/bin/env python
# Turku backups - client agent
# Copyright 2015 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published by
# the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
# SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
import uuid
import string
import random
import json
import os
import copy
import subprocess
import platform
import urlparse
import httplib
class RuntimeLock():
name = None
file = None
def __init__(self, name):
import fcntl
file = open(name, 'w')
try:
fcntl.lockf(file, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError, e:
import errno
if e.errno in (errno.EACCES, errno.EAGAIN):
raise
file.write('%10s\n' % os.getpid())
file.flush()
file.seek(0)
self.name = name
self.file = file
def close(self):
if self.file:
self.file.close()
self.file = None
os.unlink(self.name)
def __del__(self):
self.close()
def __enter__(self):
self.file.__enter__()
return self
def __exit__(self, exc, value, tb):
result = self.file.__exit__(exc, value, tb)
self.close()
return result
def acquire_lock(name):
return RuntimeLock(name)
def json_dump_p(obj, f):
"""Calls json.dump with standard (pretty) formatting"""
return json.dump(obj, f, sort_keys=True, indent=4, separators=(',', ': '))
def json_dumps_p(obj):
"""Calls json.dumps with standard (pretty) formatting"""
return json.dumps(obj, sort_keys=True, indent=4, separators=(',', ': '))
def json_load_file(file):
with open(file) as f:
try:
return json.load(f)
except ValueError, e:
e.args += (file,)
raise
def dict_merge(s, m):
"""Recursively merge one dict into another."""
if not isinstance(m, dict):
return m
out = copy.deepcopy(s)
for k, v in m.items():
if k in out and isinstance(out[k], dict):
out[k] = dict_merge(out[k], v)
else:
out[k] = copy.deepcopy(v)
return out
def load_config(config_dir):
config = {}
config['config_dir'] = config_dir
config_d = os.path.join(config['config_dir'], 'config.d')
sources_d = os.path.join(config['config_dir'], 'sources.d')
# Merge in config.d/*.json to the root level
config_files = []
if os.path.isdir(config_d):
config_files = [
os.path.join(config_d, fn)
for fn in os.listdir(config_d)
if fn.endswith('.json')
and os.path.isfile(os.path.join(config_d, fn))
and os.access(os.path.join(config_d, fn), os.R_OK)
]
config_files.sort()
for file in config_files:
config = dict_merge(config, json_load_file(file))
if 'var_dir' not in config:
config['var_dir'] = '/var/lib/turku-agent'
var_config_d = os.path.join(config['var_dir'], 'config.d')
# Load /var config.d files
var_config = {}
var_config_files = []
if os.path.isdir(var_config_d):
var_config_files = [
os.path.join(var_config_d, fn)
for fn in os.listdir(var_config_d)
if fn.endswith('.json')
and os.path.isfile(os.path.join(var_config_d, fn))
and os.access(os.path.join(var_config_d, fn), os.R_OK)
]
var_config_files.sort()
for file in var_config_files:
var_config = dict_merge(var_config, json_load_file(file))
# /etc gets priority over /var
var_config = dict_merge(var_config, config)
config = var_config
if 'lock_dir' not in config:
config['lock_dir'] = '/var/lock'
if 'rsyncd_command' not in config:
config['rsyncd_command'] = ['rsync']
if 'rsyncd_local_address' not in config:
config['rsyncd_local_address'] = '127.0.0.1'
if 'rsyncd_local_port' not in config:
config['rsyncd_local_port'] = 27873
if 'ssh_command' not in config:
config['ssh_command'] = ['ssh']
var_sources_d = os.path.join(config['var_dir'], 'sources.d')
# Validate the unit name
if 'unit_name' not in config:
config['unit_name'] = platform.node()
# If this isn't in the on-disk config, don't write it; just
# generate it every time
# Pull the SSH public key
if os.path.isfile(os.path.join(config['var_dir'], 'ssh_key.pub')):
with open(os.path.join(config['var_dir'], 'ssh_key.pub')) as f:
config['ssh_public_key'] = f.read().rstrip()
config['ssh_public_key_file'] = os.path.join(config['var_dir'], 'ssh_key.pub')
config['ssh_private_key_file'] = os.path.join(config['var_dir'], 'ssh_key')
elif os.path.isfile(os.path.join(config['config_dir'], 'id_rsa.pub')):
# XXX Legacy
with open(os.path.join(config['config_dir'], 'id_rsa.pub')) as f:
config['ssh_public_key'] = f.read().rstrip()
config['ssh_public_key_file'] = os.path.join(config['config_dir'], 'id_rsa.pub')
config['ssh_private_key_file'] = os.path.join(config['config_dir'], 'id_rsa')
sources_config = {}
# Merge in sources.d/*.json to the sources dict
sources_files = []
if os.path.isdir(sources_d):
sources_files = [
os.path.join(sources_d, fn)
for fn in os.listdir(sources_d)
if fn.endswith('.json')
and os.path.isfile(os.path.join(sources_d, fn))
and os.access(os.path.join(sources_d, fn), os.R_OK)
]
sources_files.sort()
var_sources_files = []
if os.path.isdir(var_sources_d):
var_sources_files = [
os.path.join(var_sources_d, fn)
for fn in os.listdir(var_sources_d)
if fn.endswith('.json')
and os.path.isfile(os.path.join(var_sources_d, fn))
and os.access(os.path.join(var_sources_d, fn), os.R_OK)
]
var_sources_files.sort()
sources_files += var_sources_files
for file in sources_files:
sources_config = dict_merge(sources_config, json_load_file(file))
# Check for required sources options
for s in sources_config.keys():
if 'path' not in sources_config[s]:
del sources_config[s]
config['sources'] = sources_config
return config
def fill_config(config):
config_d = os.path.join(config['config_dir'], 'config.d')
sources_d = os.path.join(config['config_dir'], 'sources.d')
var_config_d = os.path.join(config['var_dir'], 'config.d')
var_sources_d = os.path.join(config['var_dir'], 'sources.d')
# Create required directories
for d in (config_d, sources_d, var_config_d, var_sources_d):
if not os.path.isdir(d):
os.makedirs(d)
# Validate the machine UUID/secret
write_uuid_data = False
if 'machine_uuid' not in config:
config['machine_uuid'] = str(uuid.uuid4())
write_uuid_data = True
if 'machine_secret' not in config:
config['machine_secret'] = ''.join(
random.choice(string.ascii_letters + string.digits)
for i in range(30)
)
write_uuid_data = True
# Write out the machine UUID/secret if needed
if write_uuid_data:
with open(os.path.join(var_config_d, '10-machine_uuid.json'), 'w') as f:
os.fchmod(f.fileno(), 0o600)
json_dump_p({
'machine_uuid': config['machine_uuid'],
'machine_secret': config['machine_secret'],
}, f)
# Restoration configuration
write_restore_data = False
if 'restore_path' not in config:
config['restore_path'] = '/var/backups/turku-agent/restore'
write_restore_data = True
if 'restore_module' not in config:
config['restore_module'] = 'turku-restore'
write_restore_data = True
if 'restore_username' not in config:
config['restore_username'] = str(uuid.uuid4())
write_restore_data = True
if 'restore_password' not in config:
config['restore_password'] = ''.join(
random.choice(string.ascii_letters + string.digits)
for i in range(30)
)
write_restore_data = True
if write_restore_data:
with open(os.path.join(var_config_d, '10-restore.json'), 'w') as f:
os.fchmod(f.fileno(), 0o600)
restore_out = {
'restore_path': config['restore_path'],
'restore_module': config['restore_module'],
'restore_username': config['restore_username'],
'restore_password': config['restore_password'],
}
json_dump_p(restore_out, f)
if not os.path.isdir(config['restore_path']):
os.makedirs(config['restore_path'])
# Generate the SSH keypair if it doesn't exist
if 'ssh_private_key_file' not in config:
subprocess.check_call([
'ssh-keygen', '-t', 'rsa', '-N', '', '-C', 'turku',
'-f', os.path.join(config['var_dir'], 'ssh_key')
])
with open(os.path.join(config['var_dir'], 'ssh_key.pub')) as f:
config['ssh_public_key'] = f.read().rstrip()
config['ssh_public_key_file'] = os.path.join(config['var_dir'], 'ssh_key.pub')
config['ssh_private_key_file'] = os.path.join(config['var_dir'], 'ssh_key')
for s in config['sources']:
# Check for missing usernames/passwords
if not ('username' in config['sources'][s] or 'password' in config['sources'][s]):
sources_secrets_d = os.path.join(config['config_dir'], 'sources_secrets.d')
if os.path.isfile(os.path.join(sources_secrets_d, s + '.json')):
# XXX Legacy
j = json_load_file(os.path.join(sources_secrets_d, s + '.json'))
config['sources'][s]['username'] = j['username']
config['sources'][s]['password'] = j['password']
else:
if 'username' not in config['sources'][s]:
config['sources'][s]['username'] = str(uuid.uuid4())
if 'password' not in config['sources'][s]:
config['sources'][s]['password'] = ''.join(
random.choice(string.ascii_letters + string.digits)
for i in range(30)
)
with open(os.path.join(var_sources_d, '10-' + s + '.json'), 'w') as f:
os.fchmod(f.fileno(), 0o600)
json_dump_p({
s: {
'username': config['sources'][s]['username'],
'password': config['sources'][s]['password'],
}
}, f)
def migrate_configs(config):
import shutil
config_d = os.path.join(config['config_dir'], 'config.d')
sources_secrets_d = os.path.join(config['config_dir'], 'sources_secrets.d')
var_config_d = os.path.join(config['var_dir'], 'config.d')
# Avoid ETCBZR in the deployed Puppet environment
cleanup_migration = True
if 'cleanup_migration' in config:
cleanup_migration = config['cleanup_migration']
file_migrations = (
(os.path.join(config['config_dir'], 'id_rsa'), os.path.join(config['var_dir'], 'ssh_key')),
(os.path.join(config['config_dir'], 'id_rsa.pub'), os.path.join(config['var_dir'], 'ssh_key.pub')),
(os.path.join(config_d, '10-machine_uuid.json'), os.path.join(var_config_d, '10-machine_uuid.json')),
(os.path.join(config_d, '10-restore.json'), os.path.join(var_config_d, '10-restore.json')),
)
file_deletions = (
os.path.join(config['config_dir'], 'rsyncd.conf'),
os.path.join(config['config_dir'], 'rsyncd.secrets'),
os.path.join(config['var_dir'], 'server_config.json'),
)
for src, dst in file_migrations:
if os.path.isfile(src) and (not os.path.isfile(dst)):
if cleanup_migration:
shutil.move(src, dst)
else:
shutil.copy2(src, dst)
if cleanup_migration:
for file in file_deletions:
if os.path.isfile(file):
os.remove(file)
# Generated secrets have already been replicated in var_sources_d
if os.path.isdir(sources_secrets_d):
shutil.rmtree(sources_secrets_d)
def api_call(api_url, cmd, post_data, timeout=5):
url = urlparse.urlparse(api_url)
if url.scheme == 'https':
h = httplib.HTTPSConnection(url.netloc, timeout=timeout)
else:
h = httplib.HTTPConnection(url.netloc, timeout=timeout)
out = json.dumps(post_data)
h.putrequest('POST', '%s/%s' % (url.path, cmd))
h.putheader('Content-Type', 'application/json')
h.putheader('Content-Length', len(out))
h.putheader('Accept', 'application/json')
h.endheaders()
h.send(out)
res = h.getresponse()
if not res.status == httplib.OK:
raise Exception('Received error %d (%s) from API server' % (res.status, res.reason))
if not res.getheader('content-type') == 'application/json':
raise Exception('Received invalid reply from API server')
try:
return json.load(res)
except ValueError:
raise Exception('Received invalid reply from API server')
|