Initial commit to support the personality feature
authorConstantinos Venetsanopoulos <cven@grnet.gr>
Mon, 3 Oct 2011 14:03:08 +0000 (17:03 +0300)
committerConstantinos Venetsanopoulos <cven@cslab.ece.ntua.gr>
Mon, 3 Oct 2011 14:03:08 +0000 (17:03 +0300)
 * Support for file injection functionality
 * Add the img_personality OS parameter

helper/tasks/50EnforcePersonality.py [new file with mode: 0644]
host/common.sh.in
host/create
host/parameters.list

diff --git a/helper/tasks/50EnforcePersonality.py b/helper/tasks/50EnforcePersonality.py
new file mode 100644 (file)
index 0000000..1e04603
--- /dev/null
@@ -0,0 +1,47 @@
+#!/usr/bin/env python
+#
+# Copyright (c) 2011 Greek Research and Technology Network
+#
+"""Personalize an Image by injecting files
+
+This hook injects files into the filesystem of an Image.
+The files are passed to the hook through the Ganeti
+OS interface and found in the variable OSP_IMG_PERSONALITY.
+
+"""
+
+import sys
+import os
+import json
+import datetime
+import base64
+
+
+def timestamp():
+    now = datetime.datetime.now()
+    current_time = now.strftime("%Y%m%d.%H%M%S")
+    return current_time
+
+
+def main():
+    if os.environ.has_key('OSP_IMG_PERSONALITY'):
+        osp_img_personality = os.environ['SNF_IMAGE_PERSONALITY']
+        files = json.loads(osp_img_personality)
+        for f in files:
+            if os.path.lexists(f['path']):
+                backup_file = f['path'] + '.bak.' + timestamp()
+                os.rename(f['path'],backup_file)
+            file = file(f['path'], 'w')
+            file.write(base64.b64decode(f['contents']))
+            file.close()
+            os.chmod(f['path'],0440)
+        sys.stderr.write('Successful personalization of Image')
+    else:
+        sys.stderr.write('This Image has no personality (0 files to inject)')
+    return 0
+
+
+if __name__ == "__main__":
+    sys.exit(main())
+
+# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
index fba34ce..175a446 100644 (file)
@@ -123,6 +123,9 @@ get_api20_arguments() {
     IMG_ID=$OSP_IMG_ID
     IMG_FORMAT=$OSP_IMG_FORMAT
     IMG_PASSWD=$OSP_IMG_PASSWD
+    if [ -e "$OSP_IMG_PERSONALITY" ]; then
+        IMG_PERSONALITY=$OSP_IMG_PERSONALITY
+    fi
 }
 
 map_disk0() {
index 98e23b5..dd3560d 100755 (executable)
@@ -83,6 +83,9 @@ snf_export_DEV=/dev/vda
 snf_export_TYPE=${IMG_FORMAT}
 snf_export_PASSWORD=${IMG_PASSWD}
 snf_export_HOSTNAME=${instance}
+if [ -e "$IMG_PERSONALITY" ]; then
+    snf_export_PERSONALITY=${IMG_PERSONALITY}
+fi
 
 create_floppy $floppy
 
index f9cd23e..7bebcd3 100644 (file)
@@ -1,3 +1,4 @@
 img_id The id of the image to be installed (the id will be the image's file name prefix)
 img_format The format of the image to be installed
-img_passwd The root password which will be injected into the image 
+img_passwd The root password which will be injected into the image
+img_personality The files to be injected into the image (base64 encoded in a json.dumps format)