Revision 8b9d0a95

b/helper/tasks/50EnforcePersonality.py
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('OSP_IMG_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 :
b/host/common.sh.in
123 123
    IMG_ID=$OSP_IMG_ID
124 124
    IMG_FORMAT=$OSP_IMG_FORMAT
125 125
    IMG_PASSWD=$OSP_IMG_PASSWD
126
    if [ -e "$OSP_IMG_PERSONALITY" ]; then
127
        IMG_PERSONALITY=$OSP_IMG_PERSONALITY
128
    fi
126 129
}
127 130

  
128 131
map_disk0() {
b/host/create
83 83
snf_export_TYPE=${IMG_FORMAT}
84 84
snf_export_PASSWORD=${IMG_PASSWD}
85 85
snf_export_HOSTNAME=${instance}
86
if [ -e "$IMG_PERSONALITY" ]; then
87
    snf_export_PERSONALITY=${IMG_PERSONALITY}
88
fi
86 89

  
87 90
create_floppy $floppy
88 91

  
b/host/parameters.list
1 1
img_id The id of the image to be installed (the id will be the image's file name prefix)
2 2
img_format The format of the image to be installed
3
img_passwd The root password which will be injected into the image 
3
img_passwd The root password which will be injected into the image
4
img_personality The files to be injected into the image (base64 encoded in a json.dumps format)

Also available in: Unified diff