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