in software/database/src/main/java/org/apache/brooklyn/entity/database/postgresql/PostgreSqlSshDriver.java [255:323]
public void customize() {
// Some OSes start postgres during package installation
DynamicTasks.queue(SshEffectorTasks.ssh(sudoAsUser("postgres", "/etc/init.d/postgresql stop")).allowingNonZeroExitCode()).get();
newScript(CUSTOMIZING)
.body.append(
sudo("mkdir -p " + getDataDir()),
sudo("chown postgres:postgres " + getDataDir()),
sudo("chmod 700 " + getDataDir()),
sudo("touch " + getLogFile()),
sudo("chown postgres:postgres " + getLogFile()),
sudo("touch " + getPidFile()),
sudo("chown postgres:postgres " + getPidFile()),
alternativesGroup(
chainGroup(format("test -e %s", getInstallDir() + "/bin/initdb"),
sudoAsUser("postgres", getInstallDir() + "/bin/initdb -D " + getDataDir())),
callPgctl("initdb", true)))
.failOnNonZeroResultCode()
.execute();
String configUrl = getEntity().getConfig(PostgreSqlNode.CONFIGURATION_FILE_URL);
if (Strings.isBlank(configUrl)) {
// http://wiki.postgresql.org/wiki/Tuning_Your_PostgreSQL_Server
// If the same setting is listed multiple times, the last one wins.
DynamicTasks.queue(SshEffectorTasks.ssh(
executeCommandThenAsUserTeeOutputToFile(
chainGroup(
"echo \"listen_addresses = '*'\"",
"echo \"port = " + getEntity().getPostgreSqlPort() + "\"",
"echo \"max_connections = " + getEntity().getMaxConnections() + "\"",
"echo \"shared_buffers = " + getEntity().getSharedMemory() + "\"",
"echo \"external_pid_file = '" + getPidFile() + "'\""),
"postgres", getDataDir() + "/postgresql.conf")));
} else {
String contents = processTemplate(configUrl);
DynamicTasks.queue(
SshEffectorTasks.put("/tmp/postgresql.conf").contents(contents),
SshEffectorTasks.ssh(sudoAsUser("postgres", "cp /tmp/postgresql.conf " + getDataDir() + "/postgresql.conf")));
}
String authConfigUrl = getEntity().getConfig(PostgreSqlNode.AUTHENTICATION_CONFIGURATION_FILE_URL);
if (Strings.isBlank(authConfigUrl)) {
DynamicTasks.queue(SshEffectorTasks.ssh(
// TODO give users control which hosts can connect and the authentication mechanism
executeCommandThenAsUserTeeOutputToFile("echo \"host all all 0.0.0.0/0 md5\"", "postgres", getDataDir() + "/pg_hba.conf")));
} else {
String contents = processTemplate(authConfigUrl);
DynamicTasks.queue(
SshEffectorTasks.put("/tmp/pg_hba.conf").contents(contents),
SshEffectorTasks.ssh(sudoAsUser("postgres", "cp /tmp/pg_hba.conf " + getDataDir() + "/pg_hba.conf")));
}
// Wait for commands to complete before running the creation script
DynamicTasks.waitForLast();
if(Boolean.TRUE.equals(entity.getConfig(PostgreSqlNode.INITIALIZE_DB))){
initializeNewDatabase();
}
// Capture log file contents if there is an error configuring the database
try {
executeDatabaseCreationScript();
} catch (RuntimeException r) {
logTailOfPostgresLog();
throw Exceptions.propagate(r);
}
// Try establishing an external connection. If you get a "Connection refused...accepting TCP/IP connections
// on port 5432?" error then the port is probably closed. Check that the firewall allows external TCP/IP
// connections (netstat -nap). You can open a port with lokkit or by configuring the iptables.
}