root / snf-image-helper / tasks / 50EnforcePersonality.py @ 3179b447
History | View | Annotate | Download (1.2 kB)
1 | 8b9d0a95 | Constantinos Venetsanopoulos | #!/usr/bin/env python
|
---|---|---|---|
2 | 8b9d0a95 | Constantinos Venetsanopoulos | #
|
3 | 8b9d0a95 | Constantinos Venetsanopoulos | # Copyright (c) 2011 Greek Research and Technology Network
|
4 | 8b9d0a95 | Constantinos Venetsanopoulos | #
|
5 | 8b9d0a95 | Constantinos Venetsanopoulos | """Personalize an Image by injecting files
|
6 | 8b9d0a95 | Constantinos Venetsanopoulos |
|
7 | 8b9d0a95 | Constantinos Venetsanopoulos | This hook injects files into the filesystem of an Image.
|
8 | 8b9d0a95 | Constantinos Venetsanopoulos | The files are passed to the hook through the Ganeti
|
9 | 8b9d0a95 | Constantinos Venetsanopoulos | OS interface and found in the variable OSP_IMG_PERSONALITY.
|
10 | 8b9d0a95 | Constantinos Venetsanopoulos |
|
11 | 8b9d0a95 | Constantinos Venetsanopoulos | """
|
12 | 8b9d0a95 | Constantinos Venetsanopoulos | |
13 | 8b9d0a95 | Constantinos Venetsanopoulos | import sys |
14 | 8b9d0a95 | Constantinos Venetsanopoulos | import os |
15 | 8b9d0a95 | Constantinos Venetsanopoulos | import json |
16 | 8b9d0a95 | Constantinos Venetsanopoulos | import datetime |
17 | 8b9d0a95 | Constantinos Venetsanopoulos | import base64 |
18 | 8b9d0a95 | Constantinos Venetsanopoulos | |
19 | 8b9d0a95 | Constantinos Venetsanopoulos | |
20 | 8b9d0a95 | Constantinos Venetsanopoulos | def timestamp(): |
21 | 8b9d0a95 | Constantinos Venetsanopoulos | now = datetime.datetime.now() |
22 | 8b9d0a95 | Constantinos Venetsanopoulos | current_time = now.strftime("%Y%m%d.%H%M%S")
|
23 | 8b9d0a95 | Constantinos Venetsanopoulos | return current_time
|
24 | 8b9d0a95 | Constantinos Venetsanopoulos | |
25 | 8b9d0a95 | Constantinos Venetsanopoulos | |
26 | 8b9d0a95 | Constantinos Venetsanopoulos | def main(): |
27 | 2f221381 | Constantinos Venetsanopoulos | if os.environ.has_key('SNF_IMAGE_PERSONALITY'): |
28 | 8b9d0a95 | Constantinos Venetsanopoulos | osp_img_personality = os.environ['SNF_IMAGE_PERSONALITY']
|
29 | 8b9d0a95 | Constantinos Venetsanopoulos | files = json.loads(osp_img_personality) |
30 | 8b9d0a95 | Constantinos Venetsanopoulos | for f in files: |
31 | 8b9d0a95 | Constantinos Venetsanopoulos | if os.path.lexists(f['path']): |
32 | 8b9d0a95 | Constantinos Venetsanopoulos | backup_file = f['path'] + '.bak.' + timestamp() |
33 | 8b9d0a95 | Constantinos Venetsanopoulos | os.rename(f['path'],backup_file)
|
34 | 8b9d0a95 | Constantinos Venetsanopoulos | file = file(f['path'], 'w') |
35 | 8b9d0a95 | Constantinos Venetsanopoulos | file.write(base64.b64decode(f['contents'])) |
36 | 8b9d0a95 | Constantinos Venetsanopoulos | file.close()
|
37 | 8b9d0a95 | Constantinos Venetsanopoulos | os.chmod(f['path'],0440) |
38 | 8b9d0a95 | Constantinos Venetsanopoulos | sys.stderr.write('Successful personalization of Image')
|
39 | 8b9d0a95 | Constantinos Venetsanopoulos | else:
|
40 | 8b9d0a95 | Constantinos Venetsanopoulos | sys.stderr.write('This Image has no personality (0 files to inject)')
|
41 | 8b9d0a95 | Constantinos Venetsanopoulos | return 0 |
42 | 8b9d0a95 | Constantinos Venetsanopoulos | |
43 | 8b9d0a95 | Constantinos Venetsanopoulos | |
44 | 8b9d0a95 | Constantinos Venetsanopoulos | if __name__ == "__main__": |
45 | 8b9d0a95 | Constantinos Venetsanopoulos | sys.exit(main()) |
46 | 8b9d0a95 | Constantinos Venetsanopoulos | |
47 | 8b9d0a95 | Constantinos Venetsanopoulos | # vim: set sta sts=4 shiftwidth=4 sw=4 et ai : |