3 # Copyright (c) 2011 Greek Research and Technology Network
5 """Personalize an Image by injecting files
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.
21 now = datetime.datetime.now()
22 current_time = now.strftime("%Y%m%d.%H%M%S")
27 if not os.environ.has_key('SNF_IMAGE_TARGET'):
28 sys.stderr.write('Error: SNF_IMAGE_TARGET variable is missing\n')
31 target = os.environ['SNF_IMAGE_TARGET']
32 if not os.path.isdir(target):
33 sys.stderr.write('Error: Target: `'+target+'\' is not a directory.\n')
36 if os.environ.has_key('SNF_IMAGE_PERSONALITY'):
37 osp_img_personality = os.environ['SNF_IMAGE_PERSONALITY']
38 files = json.loads(osp_img_personality)
40 real_path = target + '/' + f['path']
41 if os.path.lexists(real_path):
42 backup_file = real_path + '.bak.' + timestamp()
43 os.rename(real_path, backup_file)
44 file = file(real_path, 'w')
45 file.write(base64.b64decode(f['contents']))
47 os.chmod(real_path, 0440)
48 sys.stderr.write('Successful personalization of Image\n')
50 sys.stderr.write('This Image has no personality (0 files to inject)\n')
54 if __name__ == "__main__":
57 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :