Add image-properties support in helper
authorNikos Skalkotos <skalkoto@grnet.gr>
Wed, 9 Nov 2011 13:50:04 +0000 (15:50 +0200)
committerNikos Skalkotos <skalkoto@grnet.gr>
Wed, 9 Nov 2011 13:50:04 +0000 (15:50 +0200)
snf-image-helper/Makefile.am
snf-image-helper/decode-properties.py [new file with mode: 0755]
snf-image-helper/snf-image-helper.in
snf-image-helper/tasks/40AddDeleteUnattendScript.in
snf-image-helper/tasks/40DeleteSSHKeys.in
snf-image-helper/tasks/40InstallUnattend.in
snf-image-helper/tasks/40SELinuxAutorelabel.in
snf-image-helper/tasks/50AssignHostname.in
snf-image-helper/tasks/50ChangePassword.in

index 9d4ccc5..9a836e3 100644 (file)
@@ -7,7 +7,7 @@ SUBDIRS = tasks
 
 dist_doc_DATA = COPYING AUTHORS ChangeLog
 dist_bin_SCRIPTS = snf-image-helper
-dist_scripts_SCRIPTS= snf-passtohash.py inject-files.py
+dist_scripts_SCRIPTS= snf-passtohash.py inject-files.py decode-properties.py
 dist_common_DATA = common.sh unattend.xml
 
 edit = sed \
diff --git a/snf-image-helper/decode-properties.py b/snf-image-helper/decode-properties.py
new file mode 100755 (executable)
index 0000000..a692c50
--- /dev/null
@@ -0,0 +1,62 @@
+#!/usr/bin/env python
+
+"""Decode a json encoded string with properties
+
+This program decodes a json encoded properties string and outputs it in a
+bash sourcable way. The properties are passed to the program through a JSON
+string either read from a file or from standard input and are outputed to a
+target file.
+"""
+
+import sys
+import os
+import subprocess
+import json
+from StringIO import StringIO
+from optparse import OptionParser
+
+def parse_arguments(input_args):
+    usage = "Usage: %prog [options] <output_file>"
+    parser = OptionParser(usage=usage)
+    parser.add_option("-i", "--input",
+                        action="store",type='string', dest="input_file",
+                        help="get input from FILE instead of stdin",
+                        metavar="FILE")
+
+    opts, args = parser.parse_args(input_args)
+
+    if len(args) != 1:
+        parser.error('output file is missing')
+    output_file = args[0]
+   
+    if opts.input_file is not None:
+        if not os.path.isfile(opts.input_file):
+            parser.error('input file does not exist')
+    return (opts.input_file, output_file)
+
+
+def main():
+    (input_file, output_file) = parse_arguments(sys.argv[1:])
+
+    infh = sys.stdin if input_file is None else open(input_file, 'r')
+    outfh = open(output_file, 'w')
+
+    properties = json.load(infh)
+    for key, value in properties.items():
+        os.environ['SNF_IMAGE_PROPERTY_' + key] = value
+
+    output = StringIO(subprocess.check_output(['bash', '-c', 'set']))
+    for line in iter(output):
+        if line.startswith('SNF_IMAGE_PROPERTY_'):
+            outfh.write(line)
+
+    infh.close()
+    outfh.close()
+    return 0
+
+
+if __name__ == "__main__":
+    sys.exit(main())
+
+# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
index 5be668e..d6dcc2c 100644 (file)
@@ -59,6 +59,16 @@ else
     log_error "Floppy does not contain \`rules\' file"
 fi
 
+if [ -n "$SNF_IMAGE_PROPERTIES" ]; then
+    properties=$(mktemp --tmpdir properties.XXXXXX)
+    add_cleanup rm "$properties"
+    echo "SNF_IMAGE_PROPERTIES" |
+        "@scriptsdir@/snf-decode-properties.py" > "$properties"
+    source "$properties"
+else
+    log_error "SNF_IMAGE_PROPERTIES variable is missing"
+fi
+
 # Image mount point...
 target=$(mktemp -d --tmpdir target.XXXXXX)
 add_cleanup rmdir "$target"
index c13a0cd..d0f425b 100644 (file)
@@ -14,7 +14,7 @@ if [ ! -d "$SNF_IMAGE_TARGET" ]; then
        log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing" 
 fi
 
-if [ "$SNF_IMAGE_OS" = "windows" ]; then
+if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "windows" ]; then
     # Make sure Unattend.xml is removed after setup has finished
     mkdir -p "$SNF_IMAGE_TARGET/Windows/Setup/Scripts"
     echo "del /Q /F C:\Unattend.xml" > "$SNF_IMAGE_TARGET/Windows/Setup/Scripts/SetupComplete.cmd"
index 763a420..b6f5e32 100644 (file)
@@ -16,7 +16,7 @@ fi
 
 target="$SNF_IMAGE_TARGET"
 
-if [ "$SNF_IMAGE_OS" != "linux" ]; then
+if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" != "linux" ]; then
     cleanup
     trap - EXIT
     exit 0
index 2323813..daa960b 100644 (file)
@@ -14,7 +14,7 @@ if [ -z "$SNF_IMAGE_TARGET" ]; then
     log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"
 fi
 
-if [ "$SNF_IMAGE_OS" != "windows" ]; then
+if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" != "windows" ]; then
     exit 0
 fi
 
index 43692a3..36c6687 100644 (file)
@@ -14,7 +14,7 @@ if [ ! -d "$SNF_IMAGE_TARGET" ]; then
        log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing" 
 fi
 
-if [ "$SNF_IMAGE_OS" = "linux" ]; then
+if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "linux" ]; then
     distro=$(get_base_distro $SNF_IMAGE_TARGET)
 
     if [ "$distro" = "redhat" ]; then
index 22c28e8..5109355 100644 (file)
@@ -62,9 +62,9 @@ if [ -z "$SNF_IMAGE_HOSTNAME" ]; then
     log_error "Hostname is missing"
 fi
 
-if [ "$SNF_IMAGE_OS" = "windows" ]; then
+if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "windows" ]; then
     windows_hostname "$SNF_IMAGE_TARGET" "$SNF_IMAGE_HOSTNAME"
-elif [ "$SNF_IMAGE_OS" = "linux" ]; then
+elif [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "linux" ]; then
     linux_hostname "$SNF_IMAGE_TARGET" "$SNF_IMAGE_HOSTNAME"
 fi
 
index 3cdea2a..b9624e0 100644 (file)
@@ -66,9 +66,9 @@ if [ -z "$SNF_IMAGE_PASSWORD" ]; then
     log_error "Password is missing"
 fi
 
-if [ "$SNF_IMAGE_OS" = "windows" ]; then
+if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "windows" ]; then
     windows_password "$SNF_IMAGE_TARGET" "$SNF_IMAGE_PASSWORD"
-elif [ "$SNF_IMAGE_OS" = "linux" ]; then
+elif [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "linux" ]; then
     linux_password "$SNF_IMAGE_TARGET" "$SNF_IMAGE_PASSWORD"
 fi