moving conf_files creation scripts to nodeconfig
[myplc.git] / bin / plc-config
diff --git a/bin/plc-config b/bin/plc-config
new file mode 100755 (executable)
index 0000000..78fc6db
--- /dev/null
@@ -0,0 +1,176 @@
+#!/usr/bin/python
+#
+# Script for basic access to the PlanetLab Central (PLC) configuration
+# file store.
+#
+# Mark Huang <mlhuang@cs.princeton.edu>
+# Copyright (C) 2006 The Trustees of Princeton University
+#
+# $Id$
+#
+
+import sys
+import os
+import fcntl
+import getopt
+import signal
+from plc_config import PLCConfiguration
+
+
+def usage():
+    print """
+Script to access the PLC configuration file store.
+    
+Usage: %s [OPTION]... [FILES]
+        Conversion:
+
+        --shell         Output shell configuration file
+        --python        Output Python configuration file
+        --php           Output PHP configuration file
+
+        Information:
+
+        --variables     List names of all variables
+        --packages      List names of all packages
+        --comps         List comps.xml from configuration
+
+        Basic variable value manipulation:
+
+        --category=     Category identifier
+        --variable=     Variable identifier
+        --value=        Variable value
+
+        Basic package list manipulation:
+
+        --group=        Package group identifier
+        --package=      Package name
+        --type=         Package type
+
+        Miscellaneous:
+
+        -h, --help      This message
+        -s, --save      Save changes to first configuration file
+""".lstrip() % sys.argv[0]
+    sys.exit(1)
+
+
+def deprecated (message):
+    print "%s: deprecated usage"%sys.argv[0]
+    print message
+    sys.exit(1)
+
+def main():
+    plc = PLCConfiguration()
+    fileobjs = []
+    output = None
+    category = {}
+    variable = {}
+    group = {}
+    package = {}
+    save = False
+
+    # Standard options
+    shortopts = "hs:"
+    longopts = ["shell", "bash", "python",
+                "php",
+                "xml",
+                "variables",
+                "packages",
+                "groups",
+                "comps",
+                "category=", "variable=", "value=",
+                "group=", "package=", "type=",
+                "help",
+                "save="]
+
+    try:
+        (opts, argv) = getopt.gnu_getopt(sys.argv[1:], shortopts, longopts)
+    except Exception, err:
+        sys.stderr.write("Error: " + str(err) + os.linesep)
+        sys.exit(1)
+
+    for (opt, optval) in opts:
+        if opt == "--shell" or \
+             opt == "--bash" or \
+             opt == "--python":
+            output = plc.output_shell
+        elif opt == "--php":
+            output = plc.output_php
+        elif opt == "--xml":
+            output = plc.output_xml
+        elif opt == "--variables":
+            output = plc.output_variables
+        elif opt == "--packages":
+#            output = plc.output_packages
+            deprecated("option --packages deprecated -- use .lst files instead")
+        elif opt == "--groups":
+#            output = plc.output_groups
+            deprecated("option --groups deprecated -- use .lst files instead")
+        elif opt == "--comps":
+#            output = plc.output_comps
+            deprecated("option --comps deprecated -- use .lst files instead")
+        elif opt == "--category":
+            category['id'] = optval
+        elif opt == "--variable":
+            variable['id'] = optval
+        elif opt == "--value":
+            variable['value'] = optval
+        elif opt == "--group":
+#            group['id'] = optval
+            deprecated("option --group deprecated -- use .lst files instead")
+        elif opt == "--package":
+#            package['name'] = optval
+            deprecated("option --package deprecated -- use .lst files instead")
+        elif opt == "--type":
+            package['type'] = optval
+        elif opt == '-s' or opt == "--save":
+            if not optval:
+                usage()
+            print 'parsed save option',optval
+            save = optval
+        elif opt == '-h' or opt == "--help":
+            usage()
+
+    # Try the default
+    if not argv:
+        argv = ["/etc/planetlab/plc_config.xml"]
+
+    # Merge all files
+    for file in argv:
+        try:
+            plc.load(file)
+        except IOError:
+            pass
+        except Exception, err:
+            sys.stderr.write("Error: %s: %s" % (file, str(err)) + os.linesep)
+            sys.exit(1)
+
+    # --category, --variable, --value
+    if category.has_key('id') and variable.has_key('id'):
+        if variable.has_key('value'):
+            plc.set(category, variable)
+        else:
+            (category, variable) = plc.get(category['id'], variable['id'])
+            if variable.has_key('value'):
+                print variable['value']
+
+    # --shell, --php, --xml
+    if output is not None:
+        sys.stdout.write(output())
+
+    # --save
+    if save:
+        # create directory if needed
+        # so that plc.d/{api,postgres} can create configs/site.xml 
+        dirname = os.path.dirname (save)
+        if (not os.path.exists (dirname)):
+            os.makedirs(dirname,0755)
+            if (not os.path.exists (dirname)):
+                print "Cannot create dir %s - exiting" % dirname
+                sys.exit(1)
+        
+        plc.save(save)
+
+
+if __name__ == '__main__':
+    main()