use the FQDN for PLC_WWW_HOST rather than localhost to get cron.php
[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 deprecated (message):
58     print "%s: deprecated usage"%sys.argv[0]
59     print message
60     sys.exit(1)
61
62 def main():
63     plc = PLCConfiguration()
64     fileobjs = []
65     output = None
66     category = {}
67     variable = {}
68     group = {}
69     package = {}
70     save = False
71
72     # Standard options
73     shortopts = "hs:"
74     longopts = ["shell", "bash", "python",
75                 "php",
76                 "xml",
77                 "variables",
78                 "packages",
79                 "groups",
80                 "comps",
81                 "category=", "variable=", "value=",
82                 "group=", "package=", "type=",
83                 "help",
84                 "save="]
85
86     try:
87         (opts, argv) = getopt.gnu_getopt(sys.argv[1:], shortopts, longopts)
88     except Exception, err:
89         sys.stderr.write("Error: " + str(err) + os.linesep)
90         sys.exit(1)
91
92     for (opt, optval) in opts:
93         if opt == "--shell" or \
94              opt == "--bash" or \
95              opt == "--python":
96             output = plc.output_shell
97         elif opt == "--php":
98             output = plc.output_php
99         elif opt == "--xml":
100             output = plc.output_xml
101         elif opt == "--variables":
102             output = plc.output_variables
103         elif opt == "--packages":
104 #            output = plc.output_packages
105             deprecated("option --packages deprecated -- use .lst files instead")
106         elif opt == "--groups":
107 #            output = plc.output_groups
108             deprecated("option --groups deprecated -- use .lst files instead")
109         elif opt == "--comps":
110 #            output = plc.output_comps
111             deprecated("option --comps deprecated -- use .lst files instead")
112         elif opt == "--category":
113             category['id'] = optval
114         elif opt == "--variable":
115             variable['id'] = optval
116         elif opt == "--value":
117             variable['value'] = optval
118         elif opt == "--group":
119 #            group['id'] = optval
120             deprecated("option --group deprecated -- use .lst files instead")
121         elif opt == "--package":
122 #            package['name'] = optval
123             deprecated("option --package deprecated -- use .lst files instead")
124         elif opt == "--type":
125             package['type'] = optval
126         elif opt == '-s' or opt == "--save":
127             if not optval:
128                 usage()
129             print 'parsed save option',optval
130             save = optval
131         elif opt == '-h' or opt == "--help":
132             usage()
133
134     # Try the default
135     if not argv:
136         argv = ["/etc/planetlab/plc_config.xml"]
137
138     # Merge all files
139     for file in argv:
140         try:
141             plc.load(file)
142         except IOError:
143             pass
144         except Exception, err:
145             sys.stderr.write("Error: %s: %s" % (file, str(err)) + os.linesep)
146             sys.exit(1)
147
148     # --category, --variable, --value
149     if category.has_key('id') and variable.has_key('id'):
150         if variable.has_key('value'):
151             plc.set(category, variable)
152         else:
153             (category, variable) = plc.get(category['id'], variable['id'])
154             if variable.has_key('value'):
155                 print variable['value']
156
157     # --shell, --php, --xml
158     if output is not None:
159         sys.stdout.write(output())
160
161     # --save
162     if save:
163         # create directory if needed
164         # so that plc.d/{api,postgres} can create configs/site.xml 
165         dirname = os.path.dirname (save)
166         if (not os.path.exists (dirname)):
167             os.makedirs(dirname,0755)
168             if (not os.path.exists (dirname)):
169                 print "Cannot create dir %s - exiting" % dirname
170                 sys.exit(1)
171         
172         plc.save(save)
173
174
175 if __name__ == '__main__':
176     main()