MyPLC: portable self-contained PLC installation
[myplc.git] / plc-config
1 #!/usr/bin/python
2 #
3 # Script for basic access to the PlanetLab Central (PLC) configuration
4 # file store.
5 #
6 # Mark Huang <mlhuang@cs.princeton.edu>
7 # Copyright (C) 2006 The Trustees of Princeton University
8 #
9 # $Id$
10 #
11
12 import sys
13 import os
14 import fcntl
15 import getopt
16 import signal
17 from plc_config import PLCConfiguration
18
19
20 def usage():
21     print """
22 Script to access the PLC configuration file store.
23     
24 Usage: %s [OPTION]... [FILES]
25         Conversion:
26
27         --shell         Output shell configuration file
28         --python        Output Python configuration file
29         --php           Output PHP configuration file
30
31         Information:
32
33         --variables     List names of all variables
34         --packages      List names of all packages
35         --comps         List comps.xml from configuration
36
37         Basic variable value manipulation:
38
39         --category=     Category identifier
40         --variable=     Variable identifier
41         --value=        Variable value
42
43         Basic package list manipulation:
44
45         --group=        Package group identifier
46         --package=      Package name
47         --type=         Package type
48
49         Miscellaneous:
50
51         -h, --help      This message
52         -s, --save      Save changes to first configuration file
53 """.lstrip() % sys.argv[0]
54     sys.exit(1)
55
56
57 def main():
58     plc = PLCConfiguration()
59     fileobjs = []
60     output = None
61     category = {}
62     variable = {}
63     group = {}
64     package = {}
65     save = False
66
67     # Standard options
68     shortopts = "hs"
69     longopts = ["shell", "bash", "python",
70                 "php",
71                 "xml",
72                 "variables",
73                 "packages",
74                 "comps",
75                 "category=", "variable=", "value=",
76                 "group=", "package=", "type=",
77                 "help",
78                 "save"]
79
80     try:
81         (opts, argv) = getopt.gnu_getopt(sys.argv[1:], shortopts, longopts)
82     except Exception, err:
83         sys.stderr.write("Error: " + str(err) + os.linesep)
84         sys.exit(1)
85
86     for (opt, optval) in opts:
87         if opt == "--shell" or \
88              opt == "--bash" or \
89              opt == "--python":
90             output = plc.output_shell
91         elif opt == "--php":
92             output = plc.output_php
93         elif opt == "--xml":
94             output = plc.output_xml
95         elif opt == "--variables":
96             output = plc.output_variables
97         elif opt == "--packages":
98             output = plc.output_packages
99         elif opt == "--comps":
100             output = plc.output_comps
101         elif opt == "--category":
102             category['id'] = optval
103         elif opt == "--variable":
104             variable['id'] = optval
105         elif opt == "--value":
106             variable['value'] = optval
107         elif opt == "--group":
108             group['id'] = optval
109         elif opt == "--package":
110             package['name'] = optval
111         elif opt == "--type":
112             package['type'] = optval
113         elif opt == '-s' or opt == "--save":
114             save = True
115         elif opt == '-h' or opt == "--help":
116             usage()
117
118     # Try the default
119     if not argv:
120         argv = ["/etc/planetlab/plc_config.xml"]
121
122     # Merge all files
123     for file in argv:
124         try:
125             plc.load(file)
126         except IOError:
127             pass
128         except Exception, err:
129             sys.stderr.write("Error: %s: %s" % (file, str(err)) + os.linesep)
130             sys.exit(1)
131
132     # --category, --variable, --value
133     if category.has_key('id') and variable.has_key('id'):
134         if variable.has_key('value'):
135             plc.set(category, variable)
136         else:
137             (category, variable) = plc.get(category['id'], variable['id'])
138             if variable.has_key('value'):
139                 print variable['value']
140
141     # --shell, --php, --xml
142     if output is not None:
143         sys.stdout.write(output())
144
145     # --save
146     if save:
147         plc.save()
148
149
150 if __name__ == '__main__':
151     main()