4 # Provides: EnforcePersonality
5 # RunBefore: UmountImage
7 # Short-Description: Inject files to the instance
10 """Personalize an Image by injecting files
12 This hook injects files into the filesystem of an Image.
13 The files are passed to the hook through the Ganeti
14 OS interface and found in the variable OSP_IMG_PERSONALITY.
26 now = datetime.datetime.now()
27 current_time = now.strftime("%Y%m%d.%H%M%S")
32 if not os.environ.has_key('SNF_IMAGE_TARGET'):
33 sys.stderr.write('Error: SNF_IMAGE_TARGET variable is missing\n')
36 target = os.environ['SNF_IMAGE_TARGET']
37 if not os.path.isdir(target):
38 sys.stderr.write('Error: Target: `'+target+'\' is not a directory.\n')
41 if os.environ.has_key('SNF_IMAGE_PERSONALITY'):
42 osp_img_personality = os.environ['SNF_IMAGE_PERSONALITY']
43 files = json.loads(osp_img_personality)
45 real_path = target + '/' + f['path']
46 if os.path.lexists(real_path):
47 backup_file = real_path + '.bak.' + timestamp()
48 os.rename(real_path, backup_file)
49 newfile = open(real_path, 'w')
50 newfile.write(base64.b64decode(f['contents']))
52 os.chmod(real_path, 0440)
53 sys.stderr.write('Successful personalization of Image\n')
55 sys.stderr.write('This Image has no personality (0 files to inject)\n')
59 if __name__ == "__main__":
62 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :