1
2
3 """
4
5 Function for changing the ports used by Glacier2
6 and the IceGrid registry. To run more than one OMERO
7 instance on a machine, it's necessary to modify these.
8
9 Copyright 2009 Glencoe Software, Inc. All rights reserved.
10 Use is subject to license terms supplied in LICENSE.txt
11
12 :author: Josh Moore <josh@glencoesoftware.com>
13
14 """
15
16
17 import re, sys, exceptions
18 import fileinput
19 from path import path
20
21 DIR = path(".")
22 ETC = DIR / "etc"
23 GRID = ETC / "grid"
24
26 m = re.match("^.*?\D%s\D.*?$" % port, line)
27 if not m:
28 m = re.match("^.*?\D%s$" % port, line)
29 return m
30
31 -def change_ports(glacier2, glacier2insecure, registry, revert = False):
32 """
33 Parses the etc configuration files to change
34 the current port values. If the files have
35 been noticeably changed, this method may fail.
36
37 Example::
38
39 ./etc/ice.config: ## omero.port=4064 (default)
40 or
41 ./etc/ice.config: omero.port=4064
42 ./grid/default.xml: <variable name="ROUTERPORT" value="4064"/>
43 ./grid/windefault.xml: <variable name="ROUTERPORT" value="4064"/>
44 ./internal.cfg:Ice.Default.Locator=IceGrid/Locator:tcp -h 127.0.0.1 -p 4061
45 ./master.cfg:IceGrid.Registry.Client.Endpoints=tcp -h 127.0.0.1 -p 4061
46
47 """
48
49 if revert:
50 f_glacier2 = str(int(glacier2))
51 f_glacier2insecure = str(int(glacier2insecure))
52 f_registry = str(int(registry))
53 t_glacier2 = "4064"
54 t_glacier2insecure = "4063"
55 t_registry = "4061"
56 else:
57 t_glacier2 = str(int(glacier2))
58 t_glacier2insecure = str(int(glacier2insecure))
59 t_registry = str(int(registry))
60 f_glacier2 = "4064"
61 f_glacier2insecure = "4063"
62 f_registry = "4061"
63
64 def check_line (l, s, f, t, done):
65 """
66 @param l: the line
67 @param s: the string that denotes this line is supposed to change
68 @param f: from port
69 @param t: to port
70 @return: the line, changed if needed
71 """
72 if l.find(s) >= 0 and line_has_port(l, f):
73 print l.replace(f, t),
74 done.add(fileinput.filename())
75 return True
76 return False
77
78 cfgs = [ str(x) for x in ETC.files("*.cfg") ]
79 found_reg = set()
80 for line in fileinput.input(cfgs, inplace=1):
81 if check_line(line, "Ice.Default.Locator", f_registry, t_registry, found_reg):
82 continue
83 elif check_line(line, "IceGrid.Registry.Client.Endpoints", f_registry, t_registry, found_reg):
84 continue
85 print line,
86 fileinput.close()
87
88 xmls = [ str(x) for x in GRID.files("*.xml") ]
89
90 found_ssl = set()
91 found_tcp = set()
92 for line in fileinput.input(xmls, inplace=1):
93 if check_line(line, "ROUTERPORT", f_glacier2, t_glacier2, found_ssl):
94 continue
95 elif check_line(line, "ROUTER", f_glacier2insecure, t_glacier2insecure, found_tcp):
96 continue
97 print line,
98 fileinput.close()
99
100 ice_cfg = ETC.files("ice.config")
101 for x in ice_cfg:
102 x.write_text("omero.port=%s\n" % t_glacier2, append=True)
103 print "Appended omero.port=%s to %s" % (t_glacier2, x)
104
105 for x in ((found_reg, f_registry, t_registry), (found_tcp, f_glacier2insecure, t_glacier2insecure), (found_ssl, f_glacier2, t_glacier2)):
106 if x[0]:
107 print "Converted: %s=>%s in %s" % (x[1], x[2], ", ".join(x[0]))
108 else:
109 print "No values found for %s" % x[1]
110
111 if __name__ == "__main__":
112 try:
113 if len(sys.argv) < 3 or len(sys.argv) > 5:
114 print """ %s [--revert] <glacier2 port> <icegrid registry port> [<glacier2 insecure port>]
115
116 Changes all 4064 ports to the given glacier2 port
117 and all 4061 ports to the given registry port. You will
118 need to give your clients the new glacier2 port.""" % sys.argv[0]
119 sys.exit(2)
120 else:
121 args = sys.argv[1:]
122 try:
123 idx = args.index("--revert")
124 args.pop(idx)
125 revert = True
126 except ValueError:
127 revert = False
128 glacier2 = int(args[0])
129 registry = int(args[1])
130 glacier2insecure = len(args) > 2 and int(args[2]) or 4063
131 change_ports(glacier2, glacier2insecure, registry, revert)
132 sys.exit(0)
133 except exceptions.Exception, e:
134 print "Failed to set ports: ", e
135 sys.exit(1)
136