% def tmp = [ dbSuperuser : 'root', dbSuperpass : '', dbUser : 'tigase', dbPass : 'tigase12', dbName : 'tigasedb', dbHost : 'localhost', dbParams : '' ] + config; def title = 'Database connectivity check ' tmp.each { k,v -> if (request.getParameter(k)) { Object old = tmp[k]; if (old instanceof Boolean) { tmp[k] = Boolean.parseBoolean(request.getParameter(k)); } else { tmp[k] = request.getParameter(k); } } } config.clear(); config.putAll(tmp); def dbHelper = null; dbHelper = [ loadSQLQueries : { resource, repo -> def query = ""; def queries = []; new java.io.File('database/'+resource+'.sql').eachLine { line -> if (line.startsWith("-- QUERY START:")) { query = ""; } else if (line.startsWith("-- QUERY END:")) { query.trim(); if (query.endsWith(";")) query = query.substring(0, query.length()-1); else if (query.endsWith("//")) query = query.substring(0, query.length()-2); config.each { k,v -> query = query.replace('${' + k + '}', String.valueOf(v)); } queries.add(query); } else { if ((line.trim().startsWith("source") || line.trim().startsWith("run") || line.trim().startsWith("\\i")) && line.trim().contains("sql")) { if ((line.trim().startsWith("source") || line.trim().startsWith("run") || line.trim().startsWith("\\i")) && line.trim().contains("sql")) { java.util.regex.Matcher matcher = java.util.regex.Pattern.compile(repo.getDatabaseType().name() + "-(.*).sql").matcher(line); if (matcher.find()) { queries.addAll(dbHelper.loadSQLQueries(repo.getDatabaseType().name() + "-" + matcher.group(1), repo)); } } } if (line.isEmpty() || line.trim().startsWith("--")) return; query += ' ' + line.trim(); } }; return queries; }, loadSchemaQueries : { repo -> def queries = []; ['-schema-5-1-schema', '-schema-5-1-sp', '-schema-5-1-props'].each { prefix -> def fullprefix = repo.getDatabaseType().name() + prefix; queries.addAll(dbHelper.loadSQLQueries(fullprefix, repo)); } return queries; }, validataDBSchema : { repo, config -> def result = [error:false]; def schemaVer = null; def usersCount = null; try { def schemaOk = false; def stmt = repo.createStatement(null); def rs = stmt.executeQuery("select count(*) from tig_users"); while (rs.next()) { usersCount = rs.getLong(1) schemaVer = "Unknown"; } def schemaVerQuery = "select TigGetDBProperty('schema-version')"; if (repo.getDatabaseType().name() == 'sqlserver') { schemaVerQuery = "select dbo.TigGetDBProperty('schema-version')"; } else if (repo.getDatabaseType().name() == 'derby') { schemaVerQuery = "values TigGetDBProperty('schema-version')"; } rs = stmt.executeQuery(schemaVerQuery); if (rs.next()) { schemaVer = rs.getString(1); if ("5.1".equals(schemaVer)) { schemaOk = true; } } if (schemaOk) { result.msg = "Schema OK, accounts number: " + usersCount; return result; } } catch (Exception ex) { result.error = ex.getMessage(); } if (schemaVer == null) { try { def stmt = repo.createStatement(null); // load schema from files def schema = dbHelper.loadSchemaQueries(repo); schema.each { query -> stmt.execute(query); } schemaVer = '5.1'; result.error = false; result.msg = "New schema loaded OK"; } catch (Exception ex) { result.error = "Could not load schema: " + ex.getMessage(); } } else { result.error = true; result.msg = "Old schema, users: ${usersCount} (version: ${schemaVer})"; } return result; }, validateDBConversion : { repo, config -> def result = [error:false]; def schemaVer = null; try { def schemaOk = false; def schemaVerQuery = "select TigGetDBProperty('schema-version')"; if (repo.getDatabaseType().name() == 'sqlserver') { schemaVerQuery = "select dbo.TigGetDBProperty('schema-version')"; } else if (repo.getDatabaseType().name() == 'derby') { schemaVerQuery = "values TigGetDBProperty('schema-version')"; } def stmt = repo.createStatement(null); def rs = stmt.executeQuery(schemaVerQuery); if (rs.next()) { schemaVer = rs.getString(1); if ("5.1".equals(schemaVer)) { schemaOk = true; } } if (schemaOk) { result.msg = "Schema OK"; return result; } } catch (Exception ex) { result.error = ex.getMessage(); } try { def stmt = repo.createStatement(null); // load schema from files def schema = []; if (schemaVer == null) { schema = dbHelper.loadSQLQueries(repo.getDatabaseType() + "-schema-upgrade-to-4", repo); } if ("4.0".equals(schemaVer)) { schema = dbHelper.loadSQLQueries(repo.getDatabaseType() + "-schema-upgrade-to-5-1", repo); } schema.findAll { query -> !query.isEmpty() }.each { query -> stmt.execute(query); } schemaVer = '5.1'; result.error = false; result.msg = "Conversion completed"; } catch (Exception ex) { result.error = "Could not upgrade schema: " + ex.getMessage(); } return result; }, addXmppAdminAccount : { repo, config -> def admins = config.admins.split(",").collect { tigase.xmpp.BareJID.bareJIDInstance(it.trim()) }; if (admins.size() < 1) return [error:"No admin users entered"]; if (!config.adminPwd || config.adminPwd.isEmpty()) return [error: 'No admin password enetered']; def authRepo = tigase.db.RepositoryFactory.getAuthRepository(null, repo.getResourceUri(), null); admins.each { admin -> try { authRepo.addUser(admin, config.adminPwd); } catch (tigase.db.UserExistsException e) { } catch (Exception e) { // in some cases we may got other exceptions but mostly they mean that user exists in store } } return [msg:"All admin users added"]; }, postInstallation : { repo, config -> try { def queries = dbHelper.loadSQLQueries(repo.getDatabaseType().name() + "-installer-post", repo); def stmt = repo.createStatement(null); queries.each { query -> stmt.execute(query); } } catch (Exception ex) { return [error:ex.getMessage()]; } return [msg:'Completed']; }, socks5SchemaLoad : { repo, config -> if (!config.socks5Component) return [msg:'Socks5 component disabled - skipping']; try { def stmt = repo.createStatement(null); def conn = stmt.getConnection(); def dbm = conn.getMetaData(); def tables = dbm.getTables(null, null, "tig_socks5_users", null); def tablesDerby = dbm.getTables(null, null, "TIG_SOCKS5_USERS", null); if (tables.next()) { return [msg:"Socks5 schema exists, skipping schema load"]; } else if (tablesDerby.next()) { return [msg:"Socks5 schema exists, skipping schema load"]; } def queries = dbHelper.loadSQLQueries(repo.getDatabaseType().name() + "-socks5-schema", repo); queries.each { query -> stmt.execute(query); } } catch (Exception ex) { return [error:"Could not load Socks5 schema: " + ex.getMessage()]; } return [msg:'Completed']; }, pubsubSchemaLoad : { repo, config -> if (!config.PubSubComponent) return [msg:'PubSub component disabled - skipping']; try { def stmt = repo.createStatement(null); def conn = stmt.getConnection(); def dbm = conn.getMetaData(); def tables = dbm.getTables(null, null, "tig_pubsub_nodes", null); def tablesDerby = dbm.getTables(null, null, "tig_pubsub_nodes".toUpperCase(), null); if (tables.next()) { return [msg:"PubSub schema exists, skipping schema load"]; } else if (tablesDerby.next()) { return [msg:"PubSub schema exists, skipping schema load"]; } def queries = dbHelper.loadSQLQueries(repo.getDatabaseType().name() + "-pubsub-schema", repo); queries.each { query -> stmt.execute(query); } } catch (Exception ex) { return [error:"Could not load PubSub schema: " + ex.getMessage()]; } return [msg:'Completed']; } ]; def stateStyle = { stateIn -> def state = (stateIn instanceof String) ? stateIn : stateIn.name(); if ('ok' == state) { return "color: green;"; } else if ('error' == state) { return "color: red;"; } else if ("warning" == state || "skipped" == state) { return "color: orange;"; } return ""; } def stateName = { state -> return state.name().toUpperCase(); } %> ${ util.include('header', [title:'Setup: ' + title]) } <% def dbUri = ((config.dbType?.toLowerCase()) == "derby") ? "jdbc:derby:${config.dbName};create=true" : "jdbc:${config.dbType?.toLowerCase()}://${config.dbHost}/${config.dbName}?user=${config.dbUser}&password=${config.dbPass}".toString(); %>
Checking connection to database | ${stateName(result)} |
${getLog()} | |
Checking if database exists | ${stateName(result)} |
${getLog()} | |
Checking database schema | ${stateName(result)} |
${getLog()} | |
Adding XMPP admin accounts | ${stateName(result)} |
${getLog()} | |
Loading socks5 component schema | ${stateName(result)} |
${getLog()} | |
Loading PubSub component schema | ${stateName(result)} |
${getLog()} | |
Post installation actions | ${stateName(result)} |
${getLog()} |