Package omero :: Package install :: Module change_ports
[hide private]
[frames] | no frames]

Source Code for Module omero.install.change_ports

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