Package omero :: Package plugins :: Module web
[hide private]
[frames] | no frames]

Source Code for Module omero.plugins.web

  1  #!/usr/bin/env python 
  2  """ 
  3     Plugin for our configuring the OMERO.web installation 
  4   
  5     Copyright 2009 University of Dundee. All rights reserved. 
  6     Use is subject to license terms supplied in LICENSE.txt 
  7   
  8  """ 
  9   
 10  from exceptions import Exception 
 11  from omero.cli import Arguments, BaseControl, VERSION 
 12  import omero.java 
 13  import time 
 14   
 15  HELP=""" omero web settings 
 16   
 17  OMERO.web tools: 
 18   
 19       settings           - Configuration for web 
 20        
 21  For advance use: 
 22       custom_settings    - Creates only custom_settings.py 
 23       initial            - Creates initial_data.json 
 24       syncdb             - Synchronise local database 
 25   
 26  """ 
27 -class WebControl(BaseControl):
28
29 - def help(self, args = None):
30 self.ctx.out(HELP)
31
32 - def _get_password_hash(self, root_pass=None):
33 34 root_pass = self._ask_for_password(" for OMERO.web administrator") 35 36 import sha, random 37 algo = 'sha1' 38 salt = sha.new(str(random.random())).hexdigest()[:5] 39 hsh = sha.new(salt+root_pass).hexdigest() 40 value = '%s$%s$%s' % (algo, salt, hsh) 41 return value.strip()
42
43 - def _get_username_and_email(self, username=None, email=None):
44 while not username or len(username) < 1: 45 username = self.ctx.input("Please enter Username for OMERO.web administrator: ") 46 if username == None or username == "": 47 self.ctx.err("Username cannot be empty") 48 continue 49 break 50 while not email or len(email) < 1: 51 email = self.ctx.input("Please enter Email address: ") 52 if email == None or email == "": 53 self.ctx.err("Email cannot be empty") 54 continue 55 break 56 return {"username":username, "email":email}
57
58 - def _create_superuser(self, location, username, email, passwd):
59 output = open(location, 'w') 60 61 try: 62 output.write("""[ 63 { 64 "pk": 1, 65 "model": "auth.user", 66 "fields": { 67 "username": "%s", 68 "first_name": "", 69 "last_name": "", 70 "is_active": true, 71 "is_superuser": true, 72 "is_staff": true, 73 "last_login": "%s", 74 "groups": [], 75 "user_permissions": [], 76 "password": "%s", 77 "email": "%s", 78 "date_joined": "%s" 79 } 80 }, 81 { 82 "pk": 1, 83 "model": "webadmin.gateway", 84 "fields": { 85 "server": "omero", 86 "host": "localhost", 87 "port": 4063 88 } 89 } 90 ]""" % (username, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), passwd, email, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) 91 finally: 92 output.flush() 93 output.close() 94 95 self.ctx.out("Saved to " + location)
96
97 - def _setup_server(self, email_server=None, app_host=None, sender_address=None, smtp_server=None):
98 settings = dict() 99 100 while not app_host or len(app_host) < 1: 101 app_host = self.ctx.input("Please enter the domain you want to run OMERO.web on (http://www.domain.com:8000/):") 102 if app_host == None or app_host == "": 103 self.ctx.err("Domain cannot be empty") 104 continue 105 settings["APPLICATION_HOST"] = app_host 106 break 107 108 while not sender_address or len(sender_address) < 1 : 109 sender_address = self.ctx.input("Please enter the Email address you want to send from (omero_admin@example.com): ") 110 if sender_address == None or sender_address == "": 111 self.ctx.err("Email cannot be empty") 112 continue 113 break 114 115 while not smtp_server or len(smtp_server) < 1 : 116 smtp_server = self.ctx.input("Please enter the SMTP server host you want to send from (smtp.example.com): ") 117 if smtp_server == None or smtp_server == "": 118 self.ctx.err("SMTP server host cannot be empty") 119 continue 120 121 smtp_port = self.ctx.input("Optional: please enter the SMTP server port (default 25): ") 122 smtp_user = self.ctx.input("Optional: Please enter the SMTP server username: ") 123 smtp_password = self.ctx.input("Optional: Password: ", hidden=True) 124 smtp_tls = self.ctx.input("Optional: TSL? (yes/no): ") 125 if smtp_tls == "yes": 126 smtp_tls = True 127 else: 128 smtp_tls = False 129 break 130 131 settings["SERVER_EMAIL"] = sender_address 132 settings["EMAIL_HOST"] = smtp_server 133 134 if smtp_port: 135 settings["EMAIL_PORT"] = smtp_port 136 if smtp_user: 137 settings["EMAIL_HOST_USER"] = smtp_user 138 if smtp_password: 139 settings["EMAIL_HOST_PASSWORD"] = smtp_password 140 if smtp_tls: 141 settings["EMAIL_USE_TLS"] = smtp_tls 142 143 return settings
144
145 - def _update_settings(self, location, settings=None):
146 output = open(location, 'w') 147 148 try: 149 output.write("""#!/usr/bin/env python 150 # 151 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 152 # # Django custom settings for OMERO.web project. # # 153 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 154 # 155 # 156 # Copyright (c) 2009 University of Dundee. 157 # 158 # This program is free software: you can redistribute it and/or modify 159 # it under the terms of the GNU Affero General Public License as 160 # published by the Free Software Foundation, either version 3 of the 161 # License, or (at your option) any later version. 162 # 163 # This program is distributed in the hope that it will be useful, 164 # but WITHOUT ANY WARRANTY; without even the implied warranty of 165 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 166 # GNU Affero General Public License for more details. 167 # 168 # You should have received a copy of the GNU Affero General Public License 169 # along with this program. If not, see <http://www.gnu.org/licenses/>. 170 # 171 # Author: Aleksandra Tarkowska <A(dot)Tarkowska(at)dundee(dot)ac(dot)uk>, 2008. 172 # 173 # Version: 1.0 174 175 # Notification 176 # Application allows to notify user about new shares 177 """) 178 if settings.has_key('SERVER_EMAIL'): 179 output.write("""SERVER_EMAIL = '%s' 180 """ % settings["SERVER_EMAIL"]) 181 if settings.has_key('EMAIL_HOST'): 182 output.write("""EMAIL_HOST = '%s' 183 """ % settings["EMAIL_HOST"]) 184 if settings.has_key('EMAIL_PORT'): 185 output.write("""EMAIL_PORT = %s 186 """ % settings["EMAIL_PORT"]) 187 if settings.has_key('EMAIL_HOST_USER'): 188 output.write("""EMAIL_HOST_USER = '%s' 189 """ % settings["EMAIL_HOST_USER"]) 190 if settings.has_key('EMAIL_HOST_PASSWORD'): 191 output.write("""EMAIL_HOST_PASSWORD = '%s' 192 """ % settings["EMAIL_HOST_PASSWORD"]) 193 if settings.has_key('EMAIL_USE_TLS'): 194 if settings["EMAIL_USE_TLS"]: 195 output.write("""EMAIL_USE_TLS = 'True' 196 """) 197 else: 198 output.write("""EMAIL_USE_TLS = 'False' 199 """) 200 201 output.write(""" 202 APPLICATION_HOST='%s' 203 """ % settings["APPLICATION_HOST"]) 204 finally: 205 output.flush() 206 output.close() 207 208 self.ctx.out("Saved to " + location)
209
210 - def _get_yes_or_no(self, file_name, answer=None):
211 while answer != "yes" and answer != "no": 212 answer = self.ctx.input("%s already exist. Do you want to ovewrite it? (yes/no)" % file_name) 213 if answer != "yes" and answer != "no": 214 self.ctx.err("Answer yes or no") 215 continue 216 break 217 return answer
218
219 - def custom_settings(self, do_exit=True, *args):
220 location = self.ctx.dir / "lib" / "python" / "omeroweb" / "custom_settings.py" 221 222 if location.exists(): 223 if self._get_yes_or_no("%s" % location) == 'no': 224 if do_exit: 225 sys.exit() 226 else: 227 return 228 else: 229 self.ctx.out("You just installed OMERO, which means you didn't have settings configured in OMERO.web.") 230 231 settings = self._setup_server() 232 self._update_settings(location, settings)
233
234 - def initial(self, do_exit=True, *args):
235 details = dict() 236 location = self.ctx.dir / "lib" / "python" / "omeroweb" / "initial_data.json" 237 238 if location.exists(): 239 if self._get_yes_or_no("%s" % location) == 'no': 240 if do_exit: 241 sys.exit() 242 else: 243 return 244 245 details["user"] = self._get_username_and_email() 246 details["passwd"] = self._get_password_hash() 247 self._create_superuser(location, details["user"]["username"], details["user"]["email"], details["passwd"])
248
249 - def syncdb(self, do_exit=True, *args):
250 self.ctx.out("Database synchronization...") 251 omero_web = self.ctx.dir / "lib" / "python" / "omeroweb" 252 253 if os.path.isfile(os.path.join(omero_web, 'db.sqlite3')): 254 if self._get_yes_or_no("Local database") == 'no': 255 if do_exit: 256 sys.exit() 257 else: 258 return 259 else: 260 try: 261 os.remove(os.path.join(omero_web, 'db.sqlite3')) 262 except Exception, e: 263 self.ctx.err("'db.sqlite3' was not deleted becuase: %s" % str(e)) 264 sys.exit() 265 else: 266 self.ctx.out("Old database file 'db.sqlite3' was deleted successfully.") 267 268 if not os.path.isfile(os.path.join(omero_web, 'custom_settings.py')): 269 self.ctx.err("custom_settings.py does not exist. Please run bin/omero web custom_settings") 270 sys.exit() 271 if not os.path.isfile(os.path.join(omero_web, 'initial_data.json')): 272 self.ctx.out("initial_data.json does not exist. Please run bin/omero web initial") 273 sys.exit() 274 275 args = ["python", "manage.py", "syncdb", "--noinput"] 276 rv = self.ctx.call(args, cwd = omero_web) 277 if rv != 0: 278 self.ctx.die(121, "OMERO.web was not configured.\n") 279 else: 280 self.ctx.out("OMERO.web was configured successful. Please start the application.\n")
281
282 - def settings(self, *args):
283 self.custom_settings(do_exit=False) 284 self.ctx.out("\n") 285 self.initial(do_exit=False) 286 self.ctx.out("\n") 287 self.syncdb(do_exit=False)
288 289 try: 290 register("web", WebControl) 291 except NameError: 292 WebControl()._main() 293