Statistics
| Branch: | Tag: | Revision:

root / snf-image-helper / tasks / 50EnforcePersonality.py @ aa4fc6bb

History | View | Annotate | Download (1.2 kB)

1
#!/usr/bin/env python
2
#
3
# Copyright (c) 2011 Greek Research and Technology Network
4
#
5
"""Personalize an Image by injecting files
6

7
This hook injects files into the filesystem of an Image.
8
The files are passed to the hook through the Ganeti
9
OS interface and found in the variable OSP_IMG_PERSONALITY.
10

11
"""
12

    
13
import sys
14
import os
15
import json
16
import datetime
17
import base64
18

    
19

    
20
def timestamp():
21
    now = datetime.datetime.now()
22
    current_time = now.strftime("%Y%m%d.%H%M%S")
23
    return current_time
24

    
25

    
26
def main():
27
    if os.environ.has_key('SNF_IMAGE_PERSONALITY'):
28
        osp_img_personality = os.environ['SNF_IMAGE_PERSONALITY']
29
        files = json.loads(osp_img_personality)
30
        for f in files:
31
            if os.path.lexists(f['path']):
32
                backup_file = f['path'] + '.bak.' + timestamp()
33
                os.rename(f['path'],backup_file)
34
            file = file(f['path'], 'w')
35
            file.write(base64.b64decode(f['contents']))
36
            file.close()
37
            os.chmod(f['path'],0440)
38
        sys.stderr.write('Successful personalization of Image')
39
    else:
40
        sys.stderr.write('This Image has no personality (0 files to inject)')
41
    return 0
42

    
43

    
44
if __name__ == "__main__":
45
    sys.exit(main())
46

    
47
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :