root/cherokee/trunk/contrib/08to09.py

Revision 1977, 3.0 kB (checked in by alo, 3 months ago)

--

Line 
1 #!/usr/bin/env python
2
3 ##
4 ## Cherokee 0.8.x to 0.9.x configuration converter
5 ##
6 ## Copyright: Alvaro Lopez Ortega <alvaro@alobbs.com>
7 ## Licensed: GPL v2
8 ##
9
10 import sys
11
12 sys.path.append('../admin/')
13 from config import *
14
15 def save_result (content, file):
16     tmp = content.split('\n')
17     tmp.sort()
18
19     cont  = "# Converted by 08to09.py \n"
20     cont += '\n'.join(tmp)
21
22     f = open(file, "w+")
23     f.write (cont)
24     f.close()
25
26 sources = []
27 def add_source (in_source):
28     global sources
29
30     for n in range(len(sources)):
31         if sources[n] == in_source:
32             return n+1, False
33
34     sources.append (in_source)
35     return len(sources), True
36
37 def get_highest_balancer_source (cin, vserver, rule):
38     tmp = cin.keys('vserver!%s!rule!%s!handler!balancer!source' %(vserver, rule))
39     if tmp:
40         tmp = [int(x) for x in tmp]
41         tmp.sort()
42         return tmp[-1]+1
43     else:
44         return 1
45
46
47 def convert (fin, fout):
48     n   = 1
49     cin = Config(fin)
50
51     # Rewrite the 'source' entries
52     for v in cin.keys('vserver'):
53         for r in cin.keys('vserver!%s!rule'%(v)):
54             balancer = cin.get_val ('vserver!%s!rule!%s!handler!balancer'%(v,r))
55             if not balancer:
56                 continue
57
58             tipe = cin.get_val('vserver!%s!rule!%s!handler!balancer!type'%(v,r))
59             del(cin['vserver!%s!rule!%s!handler!balancer!type'%(v,r)])
60
61             for h in cin.keys('vserver!%s!rule!%s!handler!balancer'%(v,r)):
62                 if h == 'type': continue
63
64                 env  = {}
65                 host = cin.get_val('vserver!%s!rule!%s!handler!balancer!%s!host'%(v,r,h))
66                 inte = cin.get_val('vserver!%s!rule!%s!handler!balancer!%s!interpreter'%(v,r,h))
67
68                 tmp = cin.keys ('vserver!%s!rule!%s!handler!balancer!%s!env'%(v,r,h))
69                 if tmp:
70                     for e in tmp:
71                         env[e] = cin.get_val('vserver!%s!rule!%s!handler!balancer!%s!env!%s'%(v,r,h,e))
72                        
73                 sourcen, is_new = add_source ((tipe, host, inte, env))
74                 if is_new:
75                     cin['source!%d!type'%(sourcen)] = tipe
76                     cin['source!%d!host'%(sourcen)] = host
77                     if inte:
78                         cin['source!%d!interpreter'%(sourcen)] = inte
79                     if env:
80                         for k in env.keys():
81                             cin['source!%d!env!%s'%(sourcen,k)] = env[k]
82
83                 del(cin['vserver!%s!rule!%s!handler!balancer!%s'%(v,r,h)])
84
85                 h = get_highest_balancer_source(cin,v,r)
86                 cin['vserver!%s!rule!%s!handler!balancer!source!%d' %(v,r,h) ] = str(sourcen)
87
88     # Remove old server encoders
89     del (cin['server!encoder'])
90
91     save_result (str(cin), fout)
92                
93
94 def main ():
95     if len(sys.argv) < 3:
96         print "USAGE:"
97         print " %s /path/cherokee.conf.08 /path/cherokee.conf.09" % (sys.argv[0])
98         print
99         raise SystemExit
100
101     convert (sys.argv[1], sys.argv[2])
102    
103 if __name__ == "__main__":
104     main()
Note: See TracBrowser for help on using the browser.