/
filesystemlinker.py
402 lines (325 loc) · 15.3 KB
/
filesystemlinker.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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#
# Paasmaker - Platform as a Service
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
import os
import json
import glob
import tempfile
import paasmaker
from paasmaker.pacemaker.prepare.base import BasePrepare, BasePrepareTest
import colander
class FilesystemLinkerConfigurationSchema(colander.MappingSchema):
pass
class FilesystemLinkerParametersSchema(colander.MappingSchema):
directories = colander.SchemaNode(
colander.Sequence(),
colander.SchemaNode(colander.String()),
title="Directories",
description="List of directories to be symlinked into a persistent filesystem location."
)
class FilesystemLinker(BasePrepare):
"""
Does some stuff.
Make sure you supply relative paths. If you have multiple entries where one is a child
directory of another, list the parent first and the child second.
This plugin depends on the application having one (and only one) filesystem service
allocated to it in the application manifest.
"""
# NOTE: This plugin can also be used to run commands prior to an instance starting.
MODES = {
paasmaker.util.plugin.MODE.PREPARE_COMMAND: FilesystemLinkerParametersSchema(),
paasmaker.util.plugin.MODE.RUNTIME_STARTUP: FilesystemLinkerParametersSchema()
}
OPTIONS_SCHEMA = FilesystemLinkerConfigurationSchema()
API_VERSION = "0.9.0"
# - parse services out of environment variable
# - look for protocol: directory (and only one, else error callback)
# - foreach user parameter directory, async check:
# - if target (FilesystemService persistent location) exists; if not create it
# - if source (master version of the files in SCM) exists, copy contents of source -> target
# - if source exists, delete it (to be replaced in next step)
# - add a symlink to target
def prepare(self, environment, instance_directory, callback, error_callback):
if 'PM_SERVICES' not in environment:
raise ValueError("Application environment variables haven't been set correctly.")
services = json.loads(environment['PM_SERVICES'])
parent_directory = None
for service in services.values():
if 'protocol' in service and service['protocol'] == 'directory':
if parent_directory is not None:
# there's more than one directory returned by this service!
error_callback("This application has more than one filesystem service assigned, but filesystem linker plugin only supports one service.")
return
parent_directory = service['directory']
if parent_directory is None:
error_callback("Filesystem linker plugin requires a filesystem service to be assigned to the application.")
return
self.callback = callback
self.error_callback = error_callback
self.instance_directory = instance_directory
self.parent_directory = parent_directory
self.directories = list(self.parameters['directories'])
self.directories.reverse()
# check directories for nested items: it doesn't make sense
# for the parent dir of one symlink to itself be a symlink
dirs = list(self.parameters['directories'])
dirs.sort()
for i, val in enumerate(dirs):
if len(dirs[i+1:i+2]) > 0:
if dirs[i+1:i+2][0].startswith("%s/" % val):
error_callback("Filesystem linker plugin parameters cannot contain nested directories: %s and %s" % (val, dirs[i+1:i+2][0]))
return
self._fetch_directory()
def _fetch_directory(self):
if len(self.directories) > 0:
directory = self.directories.pop()
self._process_directory(directory)
else:
self.callback("Linked %d directories." % len(self.parameters['directories']))
def _process_directory(self, directory):
persistent_path = os.path.abspath(os.path.join(self.parent_directory, directory))
instance_application_path = os.path.abspath(os.path.join(self.instance_directory, directory))
# first check that these are where we expect (i.e. children of instance_directory / parent_directory)
if not persistent_path.startswith(self.parent_directory) \
or not instance_application_path.startswith(self.instance_directory):
self.error_callback("Filesystem linker directory %d is not a relative path or contains ../" % directory)
return
if not os.path.exists(persistent_path):
try:
os.makedirs(persistent_path)
except OSError, ex:
self.error_callback("Couldn't create target %s" % persistent_path, ex)
return
if os.path.exists(instance_application_path):
# if the source path is a non-empty directory (i.e. there are files
# from the SCM in there), treat those files as authoritative copies,
# then delete the source so it can be replaced with a symlink
files_to_copy = glob.glob(os.path.join(instance_application_path, "*"))
if len(files_to_copy) > 0:
command = ['cp', '-rv'] + files_to_copy + [persistent_path]
self.log_fp = self.logger.takeover_file()
def remove_complete(code):
self.logger.untakeover_file(self.log_fp)
self.logger.info("rm command returned code: %d", code)
if code == 0:
self.logger.info("Source directory deleted successfully (ahead of symlinking).")
self._symlink_dir(persistent_path, instance_application_path)
else:
self.logger.error("Couldn't delete %s" % instance_application_path)
self.error_callback("Couldn't delete %s" % instance_application_path)
def copy_complete(code):
self.logger.untakeover_file(self.log_fp)
self.logger.info("cp command returned code: %d", code)
if code == 0:
self.logger.info("All files copied successfully.")
command = ['rm', '-rfv', instance_application_path]
self.log_fp = self.logger.takeover_file()
paasmaker.util.Popen(
command,
stdout=self.log_fp,
stderr=self.log_fp,
on_exit=remove_complete,
io_loop=self.configuration.io_loop
)
else:
self.logger.error("Couldn't copy files from %s" % instance_application_path)
self.error_callback("Couldn't copy files from %s" % instance_application_path)
paasmaker.util.Popen(
command,
stdout=self.log_fp,
stderr=self.log_fp,
on_exit=copy_complete,
io_loop=self.configuration.io_loop
)
else:
self._symlink_dir(persistent_path, instance_application_path)
def _symlink_dir(self, persistent_path, instance_application_path):
# check that all directories up to the symlink location exist,
# and create them if they don't (unlike the persistent path,
# where we refuse to go a mkdir, the instance path is managed
# by Paasmaker anyway)
symlink_containing_dir = os.path.abspath(os.path.join(instance_application_path, '..'))
if not os.path.exists(symlink_containing_dir):
os.makedirs(symlink_containing_dir)
# link from instance path to persistent path
os.symlink(persistent_path, instance_application_path)
# done processing this directory: check for another
self._fetch_directory()
class FilesystemLinkerTest(BasePrepareTest):
def setUp(self):
super(FilesystemLinkerTest, self).setUp()
self.logger = self.configuration.get_job_logger('teststartup_filesystemlinker')
self.persistent_directory = os.path.join(self.tempdir, "filesystemlinker_persistent")
self.instance_directory = os.path.join(self.tempdir, "filesystemlinker_instance")
self.sample_env = {
"PM_SERVICES": json.dumps(
{ "filesystemlinker": {
"directory": self.persistent_directory,
"protocol": "directory"
} }
)
}
self.registry.register(
'paasmaker.startup.filesystemlinker',
'paasmaker.heart.startup.filesystemlinker.FilesystemLinker',
{},
'Filesystem Linker'
)
def test_no_target_and_no_source(self):
# Basic test: neither source nor target exists, so check that the plugin created the target and made the source a symlink.
parameters = {
"directories": ['simple_target_directory']
}
plugin = self.registry.instantiate(
'paasmaker.startup.filesystemlinker',
paasmaker.util.plugin.MODE.PREPARE_COMMAND,
parameters,
self.logger
)
plugin.prepare(self.sample_env, self.instance_directory, self.success_callback, self.failure_callback)
self.wait()
self.assertTrue(self.success, "FilesystemLinker did not prepare properly: %s" % self.message)
self.assertTrue(os.path.islink(os.path.join(self.instance_directory, parameters['directories'][0])), "Symlink was not created in the instance directory.")
self.assertTrue(os.path.exists(os.path.join(self.persistent_directory, parameters['directories'][0])), "Target directory does not appear to exist.")
def test_invalid_directory_parameters(self):
# Linked directories nested at multiple levels don't make sense (if a is a symlink, then a/b can't also be a symlink)
parameters = {
"directories": [
'target_directory',
'target_2',
'target_directory/other_child',
'target_2/yet_another_child'
]
}
plugin = self.registry.instantiate(
'paasmaker.startup.filesystemlinker',
paasmaker.util.plugin.MODE.PREPARE_COMMAND,
parameters,
self.logger
)
plugin.prepare(self.sample_env, self.instance_directory, self.success_callback, self.failure_callback)
self.wait()
self.assertFalse(self.success, "FilesystemLinker should have failed to prepare()")
self.assertIn("cannot contain nested directories", self.message, "FilesystemLinker didn't report error: %s" % self.message)
def test_multiple_no_target_no_source(self):
# ... multiple linked directories that don't overlap should work, however.
parameters = {
"directories": [
'target_directory',
'other_directory/other_child',
'yet_another_directory/with/yet_another_child'
]
}
plugin = self.registry.instantiate(
'paasmaker.startup.filesystemlinker',
paasmaker.util.plugin.MODE.PREPARE_COMMAND,
parameters,
self.logger
)
plugin.prepare(self.sample_env, self.instance_directory, self.success_callback, self.failure_callback)
self.wait()
self.assertTrue(self.success, "FilesystemLinker did not prepare properly: %s" % self.message)
for dirname in parameters['directories']:
self.assertTrue(os.path.islink(os.path.join(self.instance_directory, dirname)), "Symlink %s was not created in the instance directory." % dirname)
self.assertTrue(os.path.exists(os.path.join(self.persistent_directory, dirname)), "Target directory %s does not appear to exist." % dirname)
def test_has_target_no_source(self):
# Create the target dir beforehand, and make sure the symlink still works.
parameters = {
"directories": [
'target_directory/that_already_exists'
]
}
target_directory = os.path.join(self.persistent_directory, parameters["directories"][0])
source_symlink = os.path.join(self.instance_directory, parameters['directories'][0])
os.makedirs(target_directory)
self.assertTrue(os.path.exists(target_directory), "Target directory didn't get created during test setup.")
plugin = self.registry.instantiate(
'paasmaker.startup.filesystemlinker',
paasmaker.util.plugin.MODE.PREPARE_COMMAND,
parameters,
self.logger
)
plugin.prepare(self.sample_env, self.instance_directory, self.success_callback, self.failure_callback)
self.wait()
self.assertTrue(self.success, "FilesystemLinker did not prepare properly: %s" % self.message)
self.assertTrue(os.path.islink(source_symlink), "Symlink was not created in the instance directory.")
self.assertTrue(os.path.exists(target_directory), "Target directory does not appear to exist.")
self.assertEqual(target_directory, os.path.realpath(source_symlink), "Symlink in instance directory does not point to target directory.")
def test_no_target_has_source(self):
# Create the source dir beforehand, insert two small files, and check that they're copied.
parameters = {
"directories": [
'yet_another_directory'
]
}
target_directory = os.path.join(self.persistent_directory, parameters["directories"][0])
source_location = os.path.join(self.instance_directory, parameters['directories'][0])
os.makedirs(source_location)
test_file = os.path.join(source_location, "test")
test_string = "FilesystemLinkerTest"
for i in range(5):
fp = open(test_file + str(i), 'w')
fp.write(test_string)
fp.close()
self.assertTrue(os.path.exists(test_file + str(i)), "Test source file didn't get created during test setup.")
plugin = self.registry.instantiate(
'paasmaker.startup.filesystemlinker',
paasmaker.util.plugin.MODE.PREPARE_COMMAND,
parameters,
self.logger
)
plugin.prepare(self.sample_env, self.instance_directory, self.success_callback, self.failure_callback)
self.wait()
self.assertTrue(self.success, "FilesystemLinker did not prepare properly: %s" % self.message)
self.assertTrue(os.path.islink(source_location), "Symlink was not created in the instance directory.")
self.assertTrue(os.path.exists(target_directory), "Target directory does not appear to exist.")
for i in range(5):
copied_test_file = os.path.join(target_directory, "test") + str(i)
self.assertTrue(os.path.exists(copied_test_file), "Test file doesn't appear to exist in target directory.")
fp = open(copied_test_file)
copied_contents = fp.read()
fp.close()
self.assertEqual(test_string, copied_contents, "Target directory does not appear to exist.")
self.assertEqual(copied_test_file, os.path.realpath(test_file + str(i)), "File path with symlink in instance directory does not point to file in target directory.")
def test_has_target_and_has_source(self):
# If both directories exist beforehand, check that a test file is copied correctly and that the subsequent symlink works.
parameters = {
"directories": [
'target_directory/that_already_exists'
]
}
target_directory = os.path.join(self.persistent_directory, parameters["directories"][0])
source_location = os.path.join(self.instance_directory, parameters['directories'][0])
test_file = os.path.join(source_location, "testtest.txt")
test_string = "The quick brown fox jumps over the lazy dog."
os.makedirs(target_directory)
self.assertTrue(os.path.exists(target_directory), "Target directory didn't get created during test setup.")
os.makedirs(source_location)
fp = open(test_file, 'w')
fp.write(test_string)
fp.close()
self.assertTrue(os.path.exists(test_file), "Test source file didn't get created during test setup.")
plugin = self.registry.instantiate(
'paasmaker.startup.filesystemlinker',
paasmaker.util.plugin.MODE.PREPARE_COMMAND,
parameters,
self.logger
)
plugin.prepare(self.sample_env, self.instance_directory, self.success_callback, self.failure_callback)
self.wait()
self.assertTrue(self.success, "FilesystemLinker did not prepare properly: %s" % self.message)
self.assertTrue(os.path.islink(source_location), "Symlink was not created in the instance directory.")
self.assertTrue(os.path.exists(target_directory), "Target directory does not appear to exist.")
copied_test_file = os.path.join(target_directory, "testtest.txt")
self.assertTrue(os.path.exists(copied_test_file), "Test file doesn't appear to exist in target directory.")
# the path in test_file should now be a symlink to the target directory
fp = open(test_file)
contents_via_symlink = fp.read()
fp.close()
self.assertEqual(test_string, contents_via_symlink, "Target directory does not appear to exist.")
self.assertEqual(target_directory, os.path.realpath(source_location), "Symlink in instance directory does not point to target directory.")
self.assertEqual(copied_test_file, os.path.realpath(test_file), "File path with symlink in instance directory does not point to file in target directory.")