Setting tag myplc-5.3-5
[myplc.git] / bin / 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
10 import sys
11 import os
12 import fcntl
13 import getopt
14 import signal
15 from plc_config import PLCConfiguration
16
17
18 def usage():
19     print """
20 Script to access the PLC configuration file store.
21     
22 Usage: %s [OPTION]... [FILES]
23         Conversion:
24
25         --shell         Output shell configuration file
26         --python        Output Python configuration file
27         --php           Output PHP configuration file
28
29         Information:
30
31         --variables     List names of all variables
32         --packages      List names of all packages
33         --comps         List comps.xml from configuration
34
35         Basic variable value manipulation:
36
37         --category=     Category identifier
38         --variable=     Variable identifier
39         --value=        Variable value
40
41         Basic package list manipulation:
42
43         --group=        Package group identifier
44         --package=      Package name
45         --type=         Package type
46
47         Miscellaneous:
48
49         -h, --help      This message
50         -s, --save      Save changes to first configuration file
51 """.lstrip() % sys.argv[0]
52     sys.exit(1)
53
54
55 def deprecated (message):
56     print "%s: deprecated usage"%sys.argv[0]
57     print message
58     sys.exit(1)
59
60 def main():
61     plc = PLCConfiguration()
62     fileobjs = []
63     output = None
64     category = {}
65     variable = {}
66     group = {}
67     package = {}
68     save = False
69
70     # Standard options
71     shortopts = "hs:"
72     longopts = ["shell", "bash", "python",
73                 "php",
74                 "xml",
75                 "variables",
76                 "packages",
77                 "groups",
78                 "comps",
79                 "category=", "variable=", "value=",
80                 "group=", "package=", "type=",
81                 "help",
82                 "save="]
83
84     try:
85         (opts, argv) = getopt.gnu_getopt(sys.argv[1:], shortopts, longopts)
86     except Exception, err:
87         sys.stderr.write("Error: " + str(err) + os.linesep)
88         sys.exit(1)
89
90     for (opt, optval) in opts:
91         if opt == "--shell" or \
92              opt == "--bash" or \
93              opt == "--python":
94             output = plc.output_shell
95         elif opt == "--php":
96             output = plc.output_php
97         elif opt == "--xml":
98             output = plc.output_xml
99         elif opt == "--variables":
100             output = plc.output_variables
101         elif opt == "--packages":
102 #            output = plc.output_packages
103             deprecated("option --packages deprecated -- use .lst files instead")
104         elif opt == "--groups":
105 #            output = plc.output_groups
106             deprecated("option --groups deprecated -- use .lst files instead")
107         elif opt == "--comps":
108 #            output = plc.output_comps
109             deprecated("option --comps deprecated -- use .lst files instead")
110         elif opt == "--category":
111             category['id'] = optval
112         elif opt == "--variable":
113             variable['id'] = optval
114         elif opt == "--value":
115             variable['value'] = optval
116         elif opt == "--group":
117 #            group['id'] = optval
118             deprecated("option --group deprecated -- use .lst files instead")
119         elif opt == "--package":
120 #            package['name'] = optval
121             deprecated("option --package deprecated -- use .lst files instead")
122         elif opt == "--type":
123             package['type'] = optval
124         elif opt == '-s' or opt == "--save":
125             if not optval:
126                 usage()
127             print 'parsed save option',optval
128             save = optval
129         elif opt == '-h' or opt == "--help":
130             usage()
131
132     # Try the default
133     if not argv:
134         argv = ["/etc/planetlab/plc_config.xml"]
135
136     # Merge all files
137     for file in argv:
138         try:
139             plc.load(file)
140         except IOError:
141             pass
142         except Exception, err:
143             sys.stderr.write("Error: %s: %s" % (file, str(err)) + os.linesep)
144             sys.exit(1)
145
146     # --category, --variable, --value
147     if category.has_key('id') and variable.has_key('id'):
148         if variable.has_key('value'):
149             plc.set(category, variable)
150         else:
151             (category, variable) = plc.get(category['id'], variable['id'])
152             if variable.has_key('value'):
153                 print variable['value']
154
155     # --shell, --php, --xml
156     if output is not None:
157         sys.stdout.write(output())
158
159     # --save
160     if save:
161         # create directory if needed
162         # so that plc.d/{api,postgres} can create configs/site.xml 
163         dirname = os.path.dirname (save)
164         if (not os.path.exists (dirname)):
165             os.makedirs(dirname,0755)
166             if (not os.path.exists (dirname)):
167                 print "Cannot create dir %s - exiting" % dirname
168                 sys.exit(1)
169         
170         plc.save(save)
171
172
173 if __name__ == '__main__':
174     main()