1
2 """
3 Plugin for our managing the OMERO database.
4
5 Plugin read by omero.cli.Cli during initialization. The method(s)
6 defined here will be added to the Cli class for later use.
7
8 Copyright 2008 Glencoe Software, Inc. All rights reserved.
9 Use is subject to license terms supplied in LICENSE.txt
10
11 """
12
13 from exceptions import Exception
14 from omero.cli import Arguments, BaseControl, VERSION
15 import omero.java
16 import time
17
18 HELP=""" omero db [ script ]
19
20 Database tools:
21
22 script - Generates a script for creating an OMERO database
23
24 """
26
27 - def help(self, args = None):
29
30 - def _lookup(self, data, data2, key, map, hidden = False):
31 """
32 Read values from data and data2. If value is contained in data
33 then use it without question. If the value is in data2, offer
34 it as a default
35 """
36 map[key] = data.properties.getProperty("omero.db."+key)
37 if not map[key] or map[key] == "":
38 if data2:
39 default = data2.properties.getProperty("omero.db."+key)
40 else:
41 default = ""
42 map[key] = self.ctx.input("Please enter omero.db.%s [%s]: " % (key, default), hidden)
43 if not map[key] or map[key] == "":
44 map[key] = default
45 if not map[key] or map[key] == "":
46 self.ctx.die(1, "No value entered")
47
49
50 root_pass = self._ask_for_password(" for OMERO root user", root_pass)
51
52 server_jar = self.ctx.dir / "lib" / "server" / "server.jar"
53 p = omero.java.popen(["-cp",str(server_jar),"ome.security.PasswordUtil",root_pass])
54 rc = p.wait()
55 if rc != 0:
56 self.ctx.die(rc, "PasswordUtil failed: %s" % p.communicate() )
57 value = p.communicate()[0]
58 if not value or len(value) == 0:
59 self.ctx.die(100, "Encoded password is empty")
60 return value.strip()
61
62 - def _copy(self, input_path, output, func):
63 input = open(str(input_path))
64 try:
65 for s in input.xreadlines():
66 output.write(func(s))
67 finally:
68 input.close()
69
71 def replace_method(str_in):
72 str_out = str_in.replace("@ROOTPASS@",root_pass)
73 str_out = str_out.replace("@DBVERSION@",db_vers)
74 str_out = str_out.replace("@DBPATCH@",db_patch)
75 return str_out
76 return replace_method
77
79 sql_directory = self.ctx.dir / "sql" / "psql" / ("%s__%s" % (db_vers, db_patch))
80 if not sql_directory.exists():
81 self.ctx.die(2, "Invalid Database version/patch: %s does not exist" % sql_directory)
82 return sql_directory
83
84 - def _create(self, sql_directory, db_vers, db_patch, password_hash, location = None):
85 sql_directory = self.ctx.dir / "sql" / "psql" / ("%s__%s" % (db_vers, db_patch))
86 if not sql_directory.exists():
87 self.ctx.die(2, "Invalid Database version/patch: %s does not exist" % sql_directory)
88
89 script = "%s__%s.sql" % (db_vers, db_patch)
90 if not location:
91 location = path().getcwd() / script
92
93 output = open(location, 'w')
94 print "Saving to " + location
95
96 try:
97 output.write("""
98 --
99 -- GENERATED %s from %s
100 --
101 -- This file was created by the bin/omero db script command
102 -- and contains an MD5 version of your OMERO root users's password.
103 -- You should think about deleting it as soon as possible.
104 --
105 -- To create your database:
106 --
107 -- createdb omero
108 -- createlang plpgsql omero
109 -- psql omero < %s
110 --
111
112 BEGIN;
113 """ % ( time.ctime(time.time()), sql_directory, script ) )
114 self._copy(sql_directory/"schema.sql", output, str)
115 self._copy(sql_directory/"data.sql", output, self._make_replace(password_hash, db_vers, db_patch))
116 self._copy(sql_directory/"views.sql", output, str)
117 output.write("COMMIT;\n")
118 finally:
119 output.flush()
120 output.close()
121
123 args = Arguments(*args)
124 root_pass = None
125 try:
126 root_pass = args.args[0]
127 except Exception, e:
128 self.ctx.dbg("While getting arguments:" + str(e))
129 password_hash = self._get_password_hash(root_pass)
130 self.ctx.out("""UPDATE password SET hash = '%s' WHERE experimenter_id = 0;""" % password_hash)
131
133 args = Arguments(*args)
134
135 data = self.ctx.initData({})
136 try:
137 data2 = self.ctx.initData({})
138 output = self.ctx.readDefaults()
139 self.ctx.parsePropertyFile(data2, output)
140 except Exception, e:
141 self.ctx.dbg(str(e))
142 data2 = None
143 map = {}
144 root_pass = None
145 try:
146 data.properties.setProperty("omero.db.version", args.args[0])
147 self.ctx.out("Using %s for version" % args.args[0])
148 data.properties.setProperty("omero.db.patch", args.args[1])
149 self.ctx.out("Using %s for patch" % args.args[1])
150 root_pass = args.args[2]
151 self.ctx.out("Using password from commandline")
152 except Exception, e:
153 self.ctx.dbg("While getting arguments:"+str(e))
154 self._lookup(data, data2, "version", map)
155 self._lookup(data, data2, "patch", map)
156 sql = self._sql_directory(map["version"],map["patch"])
157 map["pass"] = self._get_password_hash(root_pass)
158 self._create(sql,map["version"],map["patch"],map["pass"])
159
160 try:
161 register("db", DatabaseControl)
162 except NameError:
163 DatabaseControl()._main()
164