You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by he...@apache.org on 2016/02/01 18:48:03 UTC
[27/51] [abbrv] [partial] brooklyn-library git commit: move subdir
from incubator up a level as it is promoted to its own repo (first
non-incubator commit!)
http://git-wip-us.apache.org/repos/asf/brooklyn-library/blob/02abbab0/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/mysql/MySqlNode.java
----------------------------------------------------------------------
diff --git a/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/mysql/MySqlNode.java b/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/mysql/MySqlNode.java
deleted file mode 100644
index 5003d4d..0000000
--- a/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/mysql/MySqlNode.java
+++ /dev/null
@@ -1,127 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.brooklyn.entity.database.mysql;
-
-import org.apache.brooklyn.api.catalog.Catalog;
-import org.apache.brooklyn.api.effector.Effector;
-import org.apache.brooklyn.api.entity.ImplementedBy;
-import org.apache.brooklyn.api.objs.HasShortName;
-import org.apache.brooklyn.api.sensor.AttributeSensor;
-import org.apache.brooklyn.config.ConfigKey;
-import org.apache.brooklyn.core.annotation.EffectorParam;
-import org.apache.brooklyn.core.config.ConfigKeys;
-import org.apache.brooklyn.core.config.MapConfigKey;
-import org.apache.brooklyn.core.effector.Effectors;
-import org.apache.brooklyn.core.entity.Attributes;
-import org.apache.brooklyn.core.location.PortRanges;
-import org.apache.brooklyn.core.sensor.BasicAttributeSensorAndConfigKey;
-import org.apache.brooklyn.core.sensor.BasicAttributeSensorAndConfigKey.StringAttributeSensorAndConfigKey;
-import org.apache.brooklyn.core.sensor.PortAttributeSensorAndConfigKey;
-import org.apache.brooklyn.core.sensor.Sensors;
-import org.apache.brooklyn.entity.database.DatastoreMixins.DatastoreCommon;
-import org.apache.brooklyn.entity.software.base.SoftwareProcess;
-import org.apache.brooklyn.util.core.flags.SetFromFlag;
-
-@Catalog(name="MySql Node", description="MySql is an open source relational database management system (RDBMS)", iconUrl="classpath:///mysql-logo-110x57.png")
-@ImplementedBy(MySqlNodeImpl.class)
-public interface MySqlNode extends SoftwareProcess, HasShortName, DatastoreCommon {
-
- // NOTE MySQL changes the minor version number of their GA release frequently, check for latest version if install fails
- @SetFromFlag("version")
- ConfigKey<String> SUGGESTED_VERSION = ConfigKeys.newConfigKeyWithDefault(SoftwareProcess.SUGGESTED_VERSION, "5.6.26");
-
- //http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.26-osx10.9-x86_64.tar.gz
- //http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.26-linux-glibc2.5-x86_64.tar.gz
- @SetFromFlag("downloadUrl")
- BasicAttributeSensorAndConfigKey<String> DOWNLOAD_URL = new StringAttributeSensorAndConfigKey(
- Attributes.DOWNLOAD_URL, "http://dev.mysql.com/get/Downloads/MySQL-${driver.majorVersion}/mysql-${version}-${driver.osTag}.tar.gz");
-
- @SetFromFlag("port")
- PortAttributeSensorAndConfigKey MYSQL_PORT = new PortAttributeSensorAndConfigKey("mysql.port", "MySQL port", PortRanges.fromString("3306, 13306+"));
-
- @SetFromFlag("dataDir")
- ConfigKey<String> DATA_DIR = ConfigKeys.newStringConfigKey(
- "mysql.datadir", "Directory for writing data files", null);
-
- @SetFromFlag("serverConf")
- MapConfigKey<Object> MYSQL_SERVER_CONF = new MapConfigKey<Object>(
- Object.class, "mysql.server.conf", "Configuration options for mysqld");
-
- ConfigKey<Object> MYSQL_SERVER_CONF_LOWER_CASE_TABLE_NAMES = MYSQL_SERVER_CONF.subKey("lower_case_table_names", "See MySQL guide. Set 1 to ignore case in table names (useful for OS portability)");
-
- @SetFromFlag("serverId")
- ConfigKey<Integer> MYSQL_SERVER_ID = ConfigKeys.newIntegerConfigKey("mysql.server_id", "Corresponds to server_id option", 0);
-
- @SetFromFlag("password")
- StringAttributeSensorAndConfigKey PASSWORD = new StringAttributeSensorAndConfigKey(
- "mysql.password", "Database admin password (or randomly generated if not set)", null);
-
- @SetFromFlag("socketUid")
- StringAttributeSensorAndConfigKey SOCKET_UID = new StringAttributeSensorAndConfigKey(
- "mysql.socketUid", "Socket uid, for use in file /tmp/mysql.sock.<uid>.3306 (or randomly generated if not set)", null);
-
- @SetFromFlag("generalLog")
- ConfigKey GENERAL_LOG = ConfigKeys.newBooleanConfigKey("mysql.general_log", "Enable general log", false);
-
- /** @deprecated since 0.7.0 use DATASTORE_URL */ @Deprecated
- AttributeSensor<String> MYSQL_URL = DATASTORE_URL;
-
- @SetFromFlag("configurationTemplateUrl")
- BasicAttributeSensorAndConfigKey<String> TEMPLATE_CONFIGURATION_URL = new StringAttributeSensorAndConfigKey(
- "mysql.template.configuration.url", "Template file (in freemarker format) for the mysql.conf file",
- "classpath://org/apache/brooklyn/entity/database/mysql/mysql.conf");
-
- AttributeSensor<Double> QUERIES_PER_SECOND_FROM_MYSQL = Sensors.newDoubleSensor("mysql.queries.perSec.fromMysql");
-
- interface ExportDumpEffector {
- ConfigKey<String> PATH = ConfigKeys.newStringConfigKey("path", "Where to export the dump to. Resolved against runtime directory if relative.", "dump.sql");
- ConfigKey<String> ADDITIONAL_OPTIONS = ConfigKeys.newStringConfigKey("additionalOptions", "Additional command line options to pass to mysqldump");
-
- Effector<Void> EXPORT_DUMP = Effectors.effector(Void.class, "export_dump")
- .description("Invokes mysqldump against the node")
- .parameter(PATH)
- .parameter(ADDITIONAL_OPTIONS)
- .buildAbstract();
- }
- Effector<Void> EXPORT_DUMP = ExportDumpEffector.EXPORT_DUMP;
-
- interface ImportDumpEffector {
- ConfigKey<String> PATH = ConfigKeys.newStringConfigKey("path", "Path to a file with SQL statements to import as the root user");
-
- Effector<Void> IMPORT_DUMP = Effectors.effector(Void.class, "import_dump")
- .description("Runs the sql statements in the file as the root user")
- .parameter(PATH)
- .buildAbstract();
- }
- Effector<Void> IMPORT_DUMP = ImportDumpEffector.IMPORT_DUMP;
-
- interface ChangePasswordEffector {
- ConfigKey<String> PASSWORD = ConfigKeys.newStringConfigKey("password", "New password to set");
-
- Effector<Void> CHANGE_PASSWORD = Effectors.effector(Void.class, "change_password")
- .description("Change the mysql root password")
- .parameter(PASSWORD)
- .buildAbstract();
- }
- Effector<Void> CHANGE_PASSWORD = ChangePasswordEffector.CHANGE_PASSWORD;
-
- @org.apache.brooklyn.core.annotation.Effector(description = "Execute SQL script on the node as the root user")
- public String executeScript(@EffectorParam(name="commands") String commands);
-
-}
http://git-wip-us.apache.org/repos/asf/brooklyn-library/blob/02abbab0/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/mysql/MySqlNodeEffectors.java
----------------------------------------------------------------------
diff --git a/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/mysql/MySqlNodeEffectors.java b/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/mysql/MySqlNodeEffectors.java
deleted file mode 100644
index af68959..0000000
--- a/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/mysql/MySqlNodeEffectors.java
+++ /dev/null
@@ -1,87 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.brooklyn.entity.database.mysql;
-
-import org.apache.brooklyn.api.effector.Effector;
-import org.apache.brooklyn.core.effector.EffectorBody;
-import org.apache.brooklyn.core.effector.EffectorTasks;
-import org.apache.brooklyn.core.effector.Effectors;
-import org.apache.brooklyn.entity.database.mysql.MySqlNode.ChangePasswordEffector;
-import org.apache.brooklyn.entity.database.mysql.MySqlNode.ExportDumpEffector;
-import org.apache.brooklyn.entity.database.mysql.MySqlNode.ImportDumpEffector;
-import org.apache.brooklyn.location.ssh.SshMachineLocation;
-import org.apache.brooklyn.util.core.config.ConfigBag;
-import org.apache.brooklyn.util.core.task.DynamicTasks;
-import org.apache.brooklyn.util.core.task.ssh.SshTasks;
-
-import com.google.common.base.Preconditions;
-import com.google.common.base.Strings;
-
-public class MySqlNodeEffectors {
- public static class ExportDumpEffectoryBody extends EffectorBody<Void> implements ExportDumpEffector {
- @Override
- public Void call(ConfigBag parameters) {
- String path = parameters.get(PATH);
- String additionalOptions = Strings.nullToEmpty(parameters.get(ADDITIONAL_OPTIONS));
- //TODO additionalOptions, path are not sanitized and are coming from the user.
- //Should we try to sanitize (potentially limiting the range of possible inputs),
- //or just assume the user has full machine access anyway?
- ((MySqlNodeImpl)entity()).getDriver().dumpDatabase(additionalOptions, path);
- return null;
- }
- }
- public static Effector<Void> EXPORT_DUMP = Effectors.effector(ExportDumpEffector.EXPORT_DUMP)
- .impl(new ExportDumpEffectoryBody())
- .build();
-
- public static class ImportDumpEffectorBody extends EffectorBody<Void> implements ImportDumpEffector {
- @Override
- public Void call(ConfigBag parameters) {
- String path = Preconditions.checkNotNull(parameters.get(PATH), "path is required");
- // TODO sanitize path?
- ((MySqlNodeImpl)entity()).getDriver().executeScriptFromInstalledFileAsync(path);
- return null;
- }
- }
- public static Effector<Void> IMPORT_DUMP = Effectors.effector(ImportDumpEffector.IMPORT_DUMP)
- .impl(new ImportDumpEffectorBody())
- .build();
-
- public static class ChangePasswordEffectorBody extends EffectorBody<Void> implements ChangePasswordEffector {
- @Override
- public Void call(ConfigBag parameters) {
- String newPass = Preconditions.checkNotNull(parameters.get(PASSWORD), "password is required");
- String oldPass = entity().getAttribute(MySqlNode.PASSWORD);
- entity().sensors().set(MySqlNode.PASSWORD, newPass);
- MySqlDriver driver = ((MySqlNodeImpl)entity()).getDriver();
- driver.changePassword(oldPass, newPass);
- SshMachineLocation machine = EffectorTasks.getSshMachine(entity());
- DynamicTasks.queue(
- SshTasks.newSshExecTaskFactory(machine,
- "cd "+entity().getAttribute(MySqlNode.RUN_DIR),
- "sed -i'' -e 's@^\\(\\s*password\\s*=\\s*\\).*$@\\1" + newPass.replace("\\", "\\\\") + "@g' mymysql.cnf")
- .requiringExitCodeZero()
- .summary("Change root password"));
- return null;
- }
- }
- public static Effector<Void> CHANGE_PASSWORD = Effectors.effector(ChangePasswordEffector.CHANGE_PASSWORD)
- .impl(new ChangePasswordEffectorBody())
- .build();
-}
http://git-wip-us.apache.org/repos/asf/brooklyn-library/blob/02abbab0/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/mysql/MySqlNodeImpl.java
----------------------------------------------------------------------
diff --git a/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/mysql/MySqlNodeImpl.java b/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/mysql/MySqlNodeImpl.java
deleted file mode 100644
index e2b0c2e..0000000
--- a/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/mysql/MySqlNodeImpl.java
+++ /dev/null
@@ -1,167 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.brooklyn.entity.database.mysql;
-
-import java.util.Map;
-
-import org.apache.brooklyn.api.entity.Entity;
-import org.apache.brooklyn.core.effector.EffectorBody;
-import org.apache.brooklyn.core.location.Locations;
-import org.apache.brooklyn.entity.software.base.SoftwareProcessImpl;
-import org.apache.brooklyn.feed.ssh.SshFeed;
-import org.apache.brooklyn.feed.ssh.SshPollConfig;
-import org.apache.brooklyn.feed.ssh.SshPollValue;
-import org.apache.brooklyn.location.ssh.SshMachineLocation;
-import org.apache.brooklyn.util.collections.MutableMap;
-import org.apache.brooklyn.util.core.config.ConfigBag;
-import org.apache.brooklyn.util.guava.Maybe;
-import org.apache.brooklyn.util.text.Identifiers;
-import org.apache.brooklyn.util.text.Strings;
-import org.apache.brooklyn.util.time.Duration;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.google.common.base.Function;
-
-public class MySqlNodeImpl extends SoftwareProcessImpl implements MySqlNode {
-
- private static final Logger LOG = LoggerFactory.getLogger(MySqlNodeImpl.class);
-
- private SshFeed feed;
-
- public MySqlNodeImpl() {
- }
-
- public MySqlNodeImpl(Entity parent) {
- this(MutableMap.of(), parent);
- }
-
- public MySqlNodeImpl(Map<?,?> flags) {
- super(flags, null);
- }
-
- public MySqlNodeImpl(Map<?,?> flags, Entity parent) {
- super(flags, parent);
- }
-
- @Override
- public Class<?> getDriverInterface() {
- return MySqlDriver.class;
- }
-
- @Override
- public MySqlDriver getDriver() {
- return (MySqlDriver) super.getDriver();
- }
-
- @Override
- public void init() {
- super.init();
- getMutableEntityType().addEffector(EXECUTE_SCRIPT, new EffectorBody<String>() {
- @Override
- public String call(ConfigBag parameters) {
- return executeScript((String)parameters.getStringKey("commands"));
- }
- });
- getMutableEntityType().addEffector(MySqlNodeEffectors.EXPORT_DUMP);
- getMutableEntityType().addEffector(MySqlNodeEffectors.IMPORT_DUMP);
- getMutableEntityType().addEffector(MySqlNodeEffectors.CHANGE_PASSWORD);
- }
-
- @Override
- protected void connectSensors() {
- super.connectSensors();
- sensors().set(DATASTORE_URL, String.format("mysql://%s:%s/", getAttribute(HOSTNAME), getAttribute(MYSQL_PORT)));
-
- /*
- * TODO status gives us things like:
- * Uptime: 2427 Threads: 1 Questions: 581 Slow queries: 0 Opens: 53 Flush tables: 1 Open tables: 35 Queries per second avg: 0.239
- * So can extract lots of sensors from that.
- */
- Maybe<SshMachineLocation> machine = Locations.findUniqueSshMachineLocation(getLocations());
- boolean retrieveUsageMetrics = getConfig(RETRIEVE_USAGE_METRICS);
-
- if (machine.isPresent()) {
- String cmd = getDriver().getStatusCmd();
- feed = SshFeed.builder()
- .entity(this)
- .period(Duration.FIVE_SECONDS)
- .machine(machine.get())
- .poll(new SshPollConfig<Double>(QUERIES_PER_SECOND_FROM_MYSQL)
- .command(cmd)
- .onSuccess(new Function<SshPollValue, Double>() {
- @Override
- public Double apply(SshPollValue input) {
- String q = Strings.getFirstWordAfter(input.getStdout(), "Queries per second avg:");
- if (q==null) return null;
- return Double.parseDouble(q);
- }})
- .setOnFailureOrException(null)
- .enabled(retrieveUsageMetrics))
- .poll(new SshPollConfig<Boolean>(SERVICE_PROCESS_IS_RUNNING)
- .command(cmd)
- .setOnSuccess(true)
- .setOnFailureOrException(false)
- .suppressDuplicates(true))
- .build();
- } else {
- LOG.warn("Location(s) {} not an ssh-machine location, so not polling for status; setting serviceUp immediately", getLocations());
- sensors().set(SERVICE_UP, true);
- }
- }
-
- @Override
- protected void disconnectSensors() {
- if (feed != null) feed.stop();
- super.disconnectSensors();
- }
-
- public int getPort() {
- return getAttribute(MYSQL_PORT);
- }
-
- public synchronized String getSocketUid() {
- String result = getAttribute(MySqlNode.SOCKET_UID);
- if (Strings.isBlank(result)) {
- result = Identifiers.makeRandomId(6);
- sensors().set(MySqlNode.SOCKET_UID, result);
- }
- return result;
- }
-
- public String getPassword() {
- String result = getAttribute(MySqlNode.PASSWORD);
- if (Strings.isBlank(result)) {
- result = Identifiers.makeRandomId(6);
- sensors().set(MySqlNode.PASSWORD, result);
- }
- return result;
- }
-
- @Override
- public String getShortName() {
- return "MySQL";
- }
-
- @Override
- public String executeScript(String commands) {
- return getDriver().executeScriptAsync(commands).block().getStdout();
- }
-
-}
http://git-wip-us.apache.org/repos/asf/brooklyn-library/blob/02abbab0/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/mysql/MySqlRowParser.java
----------------------------------------------------------------------
diff --git a/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/mysql/MySqlRowParser.java b/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/mysql/MySqlRowParser.java
deleted file mode 100644
index f7f98b9..0000000
--- a/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/mysql/MySqlRowParser.java
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.brooklyn.entity.database.mysql;
-
-import java.util.Map;
-
-import org.apache.brooklyn.util.collections.MutableMap;
-import org.apache.brooklyn.util.text.Strings;
-
-public class MySqlRowParser {
- public static Map<String, String> parseSingle(String row) {
- Map<String, String> values = MutableMap.of();
- String[] lines = row.split("\\n");
- for (String line : lines) {
- if (line.startsWith("*")) continue; // row delimiter
- String[] arr = line.split(":", 2);
- String key = arr[0].trim();
- String value = Strings.emptyToNull(arr[1].trim());
- values.put(key, value);
- }
- return values;
- };
-}
http://git-wip-us.apache.org/repos/asf/brooklyn-library/blob/02abbab0/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/mysql/MySqlSshDriver.java
----------------------------------------------------------------------
diff --git a/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/mysql/MySqlSshDriver.java b/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/mysql/MySqlSshDriver.java
deleted file mode 100644
index 868d158..0000000
--- a/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/mysql/MySqlSshDriver.java
+++ /dev/null
@@ -1,319 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.brooklyn.entity.database.mysql;
-
-import static java.lang.String.format;
-import static org.apache.brooklyn.util.JavaGroovyEquivalents.groovyTruth;
-import static org.apache.brooklyn.util.ssh.BashCommands.commandsToDownloadUrlsAs;
-import static org.apache.brooklyn.util.ssh.BashCommands.installPackage;
-
-import java.io.BufferedWriter;
-import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.io.Reader;
-import java.io.StringReader;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-
-import org.apache.brooklyn.api.location.OsDetails;
-import org.apache.brooklyn.api.mgmt.Task;
-import org.apache.brooklyn.core.effector.EffectorTasks;
-import org.apache.brooklyn.core.effector.ssh.SshEffectorTasks;
-import org.apache.brooklyn.core.entity.Attributes;
-import org.apache.brooklyn.core.entity.Entities;
-import org.apache.brooklyn.core.location.BasicOsDetails.OsVersions;
-import org.apache.brooklyn.entity.database.DatastoreMixins;
-import org.apache.brooklyn.entity.software.base.AbstractSoftwareProcessSshDriver;
-import org.apache.brooklyn.location.ssh.SshMachineLocation;
-import org.apache.brooklyn.util.collections.MutableMap;
-import org.apache.brooklyn.util.core.task.DynamicTasks;
-import org.apache.brooklyn.util.core.task.Tasks;
-import org.apache.brooklyn.util.core.task.ssh.SshTasks;
-import org.apache.brooklyn.util.core.task.system.ProcessTaskWrapper;
-import org.apache.brooklyn.util.exceptions.Exceptions;
-import org.apache.brooklyn.util.io.FileUtil;
-import org.apache.brooklyn.util.net.Urls;
-import org.apache.brooklyn.util.os.Os;
-import org.apache.brooklyn.util.ssh.BashCommands;
-import org.apache.brooklyn.util.stream.Streams;
-import org.apache.brooklyn.util.text.ComparableVersion;
-import org.apache.brooklyn.util.text.Identifiers;
-import org.apache.brooklyn.util.text.Strings;
-import org.apache.brooklyn.util.time.CountdownTimer;
-import org.apache.brooklyn.util.time.Duration;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.google.common.collect.ImmutableMap;
-
-/**
- * The SSH implementation of the {@link MySqlDriver}.
- */
-public class MySqlSshDriver extends AbstractSoftwareProcessSshDriver implements MySqlDriver {
-
- public static final Logger log = LoggerFactory.getLogger(MySqlSshDriver.class);
-
- public MySqlSshDriver(MySqlNodeImpl entity, SshMachineLocation machine) {
- super(entity, machine);
-
- entity.sensors().set(Attributes.LOG_FILE_LOCATION, getLogFile());
- }
-
- public String getOsTag() {
- // e.g. "osx10.6-x86_64"; see http://www.mysql.com/downloads/mysql/#downloads
- OsDetails os = getLocation().getOsDetails();
- if (os == null) return "linux-glibc2.5-x86_64";
- if (os.isMac()) {
- String osp1 = os.getVersion()==null ? "osx10.8" //lowest common denominator
- : new ComparableVersion(os.getVersion()).isGreaterThanOrEqualTo(OsVersions.MAC_10_9) ? "osx10.9"
- : "osx10.8"; //lowest common denominator
- if (!os.is64bit()) {
- throw new IllegalStateException("Only 64 bit MySQL build is available for OS X");
- }
- return osp1+"-x86_64";
- }
- //assume generic linux
- String osp1 = "linux-glibc2.5";
- String osp2 = os.is64bit() ? "x86_64" : "i686";
- return osp1+"-"+osp2;
- }
-
- public String getBaseDir() { return getExpandedInstallDir(); }
-
- public String getDataDir() {
- String result = entity.getConfig(MySqlNode.DATA_DIR);
- return Strings.isBlank(result) ? "." : result;
- }
-
- public String getLogFile() {
- return Urls.mergePaths(getRunDir(), "console.log");
- }
-
- public String getConfigFile() {
- return "mymysql.cnf";
- }
-
- // Only invoked to determine the default download URL form the specified version.
- public String getMajorVersion() {
- return getEntity().config().get(MySqlNode.SUGGESTED_VERSION).replaceAll("(\\d+\\.\\d+)\\.\\d+", "$1");
- }
-
- public String getDefaultUnpackedDirectoryName() {
- return Strings.removeAllFromEnd(resolver.getFilename(), ".tar.gz");
- }
-
- @Override
- public void preInstall() {
- resolver = Entities.newDownloader(this);
- String unpackedDirectoryName = resolver.getUnpackedDirectoryName(getDefaultUnpackedDirectoryName());
- setExpandedInstallDir(Os.mergePaths(getInstallDir(), unpackedDirectoryName));
- }
-
- @Override
- public void install() {
- List<String> urls = resolver.getTargets();
- String saveAs = resolver.getFilename();
-
- List<String> commands = new LinkedList<String>();
- commands.add(BashCommands.INSTALL_TAR);
- commands.add(BashCommands.INSTALL_CURL);
-
- commands.add("echo installing extra packages");
- commands.add(installPackage(ImmutableMap.of("yum", "libgcc_s.so.1"), null));
- commands.add(installPackage(ImmutableMap.of("yum", "libaio.so.1 libncurses.so.5", "apt", "libaio1 libaio-dev"), null));
-
- // these deps are only needed on some OS versions but others don't need them
- commands.add(installPackage(ImmutableMap.of("yum", "libaio", "apt", "ia32-libs"), null));
- commands.add("echo finished installing extra packages");
- commands.addAll(commandsToDownloadUrlsAs(urls, saveAs));
- commands.add(format("tar xfvz %s", saveAs));
-
- newScript(INSTALLING).body.append(commands).execute();
- }
-
- @Override
- public MySqlNodeImpl getEntity() { return (MySqlNodeImpl) super.getEntity(); }
- public int getPort() { return getEntity().getPort(); }
- public String getSocketUid() { return getEntity().getSocketUid(); }
- public String getPassword() { return getEntity().getPassword(); }
-
- @Override
- public void customize() {
- copyDatabaseConfigScript();
-
- newScript(CUSTOMIZING)
- .updateTaskAndFailOnNonZeroResultCode()
- .body.append(
- "chmod 600 "+getConfigFile(),
- getBaseDir()+"/scripts/mysql_install_db "+
- "--basedir="+getBaseDir()+" --datadir="+getDataDir()+" "+
- "--defaults-file="+getConfigFile())
- .execute();
-
- // launch, then we will configure it
- launch();
-
- // Wrap in inessential task to allow the stop step to execute even if any of the nested
- // tasks fail - poor man's try-catch for tasks.
- Task<Void> configTask = DynamicTasks.<Void>queue("execute scripts", new Runnable() {
- @Override
- public void run() {
- Tasks.markInessential();
- CountdownTimer timer = Duration.seconds(20).countdownTimer();
- boolean hasCreationScript = copyDatabaseCreationScript();
- timer.waitForExpiryUnchecked();
-
- changePassword("", getPassword());
-
- if (hasCreationScript)
- executeScriptFromInstalledFileAsync("creation-script.sql").asTask().getUnchecked();
- }
- });
-
- // not sure necessary to stop then subsequently launch, but seems safest
- // (if skipping, use a flag in launch to indicate we've just launched it)
- stop();
-
- // Fail if any of the tasks above failed, they are marked inessential so the
- // errors don't propagate automatically.
- if (configTask.isError()) {
- configTask.getUnchecked();
- }
- }
-
- @Override
- public void changePassword(String oldPass, String newPass) {
- DynamicTasks.queue(
- SshEffectorTasks.ssh(
- "cd "+getRunDir(),
- getBaseDir()+"/bin/mysqladmin --defaults-file="+getConfigFile()+" --password=" + oldPass + " password "+newPass)
- .summary("setting password")
- .requiringExitCodeZero());
- }
-
- protected void copyDatabaseConfigScript() {
- newScript(CUSTOMIZING).execute(); //create the directory
-
- String configScriptContents = processTemplate(entity.getAttribute(MySqlNode.TEMPLATE_CONFIGURATION_URL));
- Reader configContents = new StringReader(configScriptContents);
-
- getMachine().copyTo(configContents, Urls.mergePaths(getRunDir(), getConfigFile()));
- }
-
- protected boolean copyDatabaseCreationScript() {
- String creationScriptContents = DatastoreMixins.getDatabaseCreationScriptAsString(entity);
- if (creationScriptContents==null) return false;
-
- File templateFile = null;
- BufferedWriter writer = null;
- try {
- templateFile = File.createTempFile("mysql", null);
- FileUtil.setFilePermissionsTo600(templateFile);
- writer = new BufferedWriter(new FileWriter(templateFile));
- writer.write(creationScriptContents);
- writer.flush();
- copyTemplate(templateFile.getAbsoluteFile(), getRunDir() + "/creation-script.sql");
- } catch (IOException e) {
- throw Exceptions.propagate(e);
- } finally {
- if (writer != null) Streams.closeQuietly(writer);
- if (templateFile != null) templateFile.delete();
- }
- return true;
- }
-
- public String getMySqlServerOptionsString() {
- Map<String, Object> options = entity.getConfig(MySqlNode.MYSQL_SERVER_CONF);
- StringBuilder result = new StringBuilder();
- if (groovyTruth(options)) {
- for (Map.Entry<String, Object> entry : options.entrySet()) {
- result.append(entry.getKey());
- String value = entry.getValue().toString();
- if (!Strings.isEmpty(value)) {
- result.append(" = ").append(value);
- }
- result.append('\n');
- }
- }
- return result.toString();
- }
-
- @Override
- public void launch() {
- entity.sensors().set(MySqlNode.PID_FILE, getRunDir() + "/" + AbstractSoftwareProcessSshDriver.PID_FILENAME);
- newScript(MutableMap.of("usePidFile", true), LAUNCHING)
- .updateTaskAndFailOnNonZeroResultCode()
- .body.append(format("nohup %s/bin/mysqld --defaults-file=%s --user=`whoami` > %s 2>&1 < /dev/null &", getBaseDir(), getConfigFile(), getLogFile()))
- .execute();
- }
-
- @Override
- public boolean isRunning() {
- return newScript(MutableMap.of("usePidFile", false), CHECK_RUNNING)
- .body.append(getStatusCmd())
- .execute() == 0;
- }
-
- @Override
- public void stop() {
- newScript(MutableMap.of("usePidFile", true), STOPPING).execute();
- }
-
- @Override
- public void kill() {
- newScript(MutableMap.of("usePidFile", true), KILLING).execute();
- }
-
- @Override
- public String getStatusCmd() {
- return format("%s/bin/mysqladmin --defaults-file=%s status", getBaseDir(), Urls.mergePaths(getRunDir(), getConfigFile()));
- }
-
- @Override
- public ProcessTaskWrapper<Integer> executeScriptAsync(String commands) {
- String filename = "mysql-commands-"+Identifiers.makeRandomId(8);
- DynamicTasks.queue(SshEffectorTasks.put(Urls.mergePaths(getRunDir(), filename)).contents(commands).summary("copying datastore script to execute "+filename));
- return executeScriptFromInstalledFileAsync(filename);
- }
-
- @Override
- public ProcessTaskWrapper<Integer> executeScriptFromInstalledFileAsync(String filenameAlreadyInstalledAtServer) {
- SshMachineLocation machine = EffectorTasks.getSshMachine(entity);
- return DynamicTasks.queue(
- SshTasks.newSshExecTaskFactory(machine,
- "cd "+getRunDir(),
- getBaseDir()+"/bin/mysql --defaults-file="+getConfigFile()+" < "+filenameAlreadyInstalledAtServer)
- .requiringExitCodeZero()
- .summary("executing datastore script "+filenameAlreadyInstalledAtServer));
- }
-
- @Override
- public ProcessTaskWrapper<Integer> dumpDatabase(String additionalOptions, String dumpDestination) {
- SshMachineLocation machine = EffectorTasks.getSshMachine(entity);
- return DynamicTasks.queue(
- SshTasks.newSshExecTaskFactory(machine,
- "cd "+getRunDir(),
- getBaseDir()+"/bin/mysqldump --defaults-file="+getConfigFile()+" "+additionalOptions+" > "+dumpDestination)
- .requiringExitCodeZero()
- .summary("Dumping database to " + dumpDestination));
- }
-
-}
http://git-wip-us.apache.org/repos/asf/brooklyn-library/blob/02abbab0/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/mysql/ReplicationSnapshot.java
----------------------------------------------------------------------
diff --git a/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/mysql/ReplicationSnapshot.java b/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/mysql/ReplicationSnapshot.java
deleted file mode 100644
index 48af15a..0000000
--- a/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/mysql/ReplicationSnapshot.java
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.brooklyn.entity.database.mysql;
-
-public class ReplicationSnapshot {
- private String entityId;
- private String snapshotPath;
- private String binLogName;
- private int binLogPosition;
-
- public ReplicationSnapshot(String entityId, String snapshotPath, String binLogName, int binLogPosition) {
- this.entityId = entityId;
- this.snapshotPath = snapshotPath;
- this.binLogName = binLogName;
- this.binLogPosition = binLogPosition;
- }
-
- public String getEntityId() {
- return entityId;
- }
- public void setEntityId(String entityId) {
- this.entityId = entityId;
- }
- public String getSnapshotPath() {
- return snapshotPath;
- }
- public void setSnapshotPath(String snapshotPath) {
- this.snapshotPath = snapshotPath;
- }
- public String getBinLogName() {
- return binLogName;
- }
- public void setBinLogName(String binLogName) {
- this.binLogName = binLogName;
- }
- public int getBinLogPosition() {
- return binLogPosition;
- }
- public void setBinLogPosition(int binLogPosition) {
- this.binLogPosition = binLogPosition;
- }
-}
http://git-wip-us.apache.org/repos/asf/brooklyn-library/blob/02abbab0/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/postgresql/PostgreSqlDriver.java
----------------------------------------------------------------------
diff --git a/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/postgresql/PostgreSqlDriver.java b/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/postgresql/PostgreSqlDriver.java
deleted file mode 100644
index dc267b4..0000000
--- a/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/postgresql/PostgreSqlDriver.java
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.brooklyn.entity.database.postgresql;
-
-import org.apache.brooklyn.entity.software.base.SoftwareProcessDriver;
-import org.apache.brooklyn.util.core.task.system.ProcessTaskWrapper;
-
-/**
- * The {@link org.apache.brooklyn.entity.software.base.SoftwareProcessDriver} for PostgreSQL.
- */
-public interface PostgreSqlDriver extends SoftwareProcessDriver {
-
- String getStatusCmd();
-
- ProcessTaskWrapper<Integer> executeScriptAsync(String commands);
-}
http://git-wip-us.apache.org/repos/asf/brooklyn-library/blob/02abbab0/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/postgresql/PostgreSqlNode.java
----------------------------------------------------------------------
diff --git a/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/postgresql/PostgreSqlNode.java b/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/postgresql/PostgreSqlNode.java
deleted file mode 100644
index cd97719..0000000
--- a/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/postgresql/PostgreSqlNode.java
+++ /dev/null
@@ -1,115 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.brooklyn.entity.database.postgresql;
-
-import org.apache.brooklyn.api.catalog.Catalog;
-import org.apache.brooklyn.api.effector.Effector;
-import org.apache.brooklyn.api.entity.ImplementedBy;
-import org.apache.brooklyn.api.objs.HasShortName;
-import org.apache.brooklyn.config.ConfigKey;
-import org.apache.brooklyn.core.config.ConfigKeys;
-import org.apache.brooklyn.core.effector.Effectors;
-import org.apache.brooklyn.core.location.PortRanges;
-import org.apache.brooklyn.core.sensor.BasicAttributeSensorAndConfigKey;
-import org.apache.brooklyn.core.sensor.PortAttributeSensorAndConfigKey;
-import org.apache.brooklyn.entity.database.DatabaseNode;
-import org.apache.brooklyn.entity.database.DatastoreMixins;
-import org.apache.brooklyn.entity.database.DatastoreMixins.DatastoreCommon;
-import org.apache.brooklyn.entity.software.base.SoftwareProcess;
-import org.apache.brooklyn.util.core.flags.SetFromFlag;
-
-/**
- * PostgreSQL database node entity.
- * <p>
- * <ul>
- * <li>You may need to increase shared memory settings in the kernel depending on the setting of
- * the {@link #SHARED_MEMORY_BUFFER} key. The minimumm value is <em>128kB</em>. See the PostgreSQL
- * <a href="http://www.postgresql.org/docs/9.1/static/kernel-resources.html">documentation</a>.
- * <li>You will also need to enable passwordless sudo.
- * </ul>
- */
-@Catalog(name="PostgreSQL Node", description="PostgreSQL is an object-relational database management system (ORDBMS)", iconUrl="classpath:///postgresql-logo-200px.png")
-@ImplementedBy(PostgreSqlNodeImpl.class)
-public interface PostgreSqlNode extends SoftwareProcess, HasShortName, DatastoreCommon, DatabaseNode {
-
- @SetFromFlag("version")
- ConfigKey<String> SUGGESTED_VERSION = ConfigKeys.newConfigKeyWithDefault(SoftwareProcess.SUGGESTED_VERSION, "9.3-1");//"9.1-4");
-
- @SetFromFlag("configFileUrl")
- ConfigKey<String> CONFIGURATION_FILE_URL = ConfigKeys.newStringConfigKey(
- "postgresql.config.file.url", "URL where PostgreSQL configuration file can be found; "
- + "if not supplied the blueprint uses the default and customises it");
-
- @SetFromFlag("authConfigFileUrl")
- ConfigKey<String> AUTHENTICATION_CONFIGURATION_FILE_URL = ConfigKeys.newStringConfigKey(
- "postgresql.authConfig.file.url", "URL where PostgreSQL host-based authentication configuration file can be found; "
- + "if not supplied the blueprint uses the default and customises it");
-
- @SetFromFlag("port")
- PortAttributeSensorAndConfigKey POSTGRESQL_PORT = new PortAttributeSensorAndConfigKey(
- "postgresql.port", "PostgreSQL port", PortRanges.fromString("5432+"));
-
- @SetFromFlag("sharedMemory")
- ConfigKey<String> SHARED_MEMORY = ConfigKeys.newStringConfigKey(
- "postgresql.sharedMemory", "Size of shared memory buffer (must specify as kB, MB or GB, minimum 128kB)", "4MB");
-
- @SetFromFlag("maxConnections")
- ConfigKey<Integer> MAX_CONNECTIONS = ConfigKeys.newIntegerConfigKey(
- "postgresql.maxConnections", "Maximum number of connections to the database", 100);
-
- @SetFromFlag("disconnectOnStop")
- ConfigKey<Boolean> DISCONNECT_ON_STOP = ConfigKeys.newBooleanConfigKey(
- "postgresql.disconnect.on.stop", "If true, PostgreSQL will immediately disconnet (pg_ctl -m immediate stop) all current connections when the node is stopped", true);
-
- @SetFromFlag("pollPeriod")
- ConfigKey<Long> POLL_PERIOD = ConfigKeys.newLongConfigKey(
- "postgresql.sensorpoll", "Poll period (in milliseconds)", 1000L);
-
- @SetFromFlag("initializeDB")
- ConfigKey<Boolean> INITIALIZE_DB = ConfigKeys.newBooleanConfigKey(
- "postgresql.initialize", "If true, PostgreSQL will create a new user and database", false);
-
- @SetFromFlag("username")
- BasicAttributeSensorAndConfigKey<String> USERNAME = new BasicAttributeSensorAndConfigKey<>(
- String.class, "postgresql.username", "Username of the database user");
-
- String DEFAULT_USERNAME = "postgresqluser";
-
- @SetFromFlag("password")
- BasicAttributeSensorAndConfigKey<String> PASSWORD = new BasicAttributeSensorAndConfigKey<>(
- String.class, "postgresql.password",
- "Password for the database user, auto-generated if not set");
-
- @SetFromFlag("database")
- BasicAttributeSensorAndConfigKey<String> DATABASE = new BasicAttributeSensorAndConfigKey<>(
- String.class, "postgresql.database", "Database to be used");
-
- String DEFAULT_DB_NAME = "db";
-
- Effector<String> EXECUTE_SCRIPT = Effectors.effector(DatastoreMixins.EXECUTE_SCRIPT)
- .description("Executes the given script contents using psql")
- .buildAbstract();
-
- Integer getPostgreSqlPort();
- String getSharedMemory();
- Integer getMaxConnections();
-
- String executeScript(String commands);
-
-}
http://git-wip-us.apache.org/repos/asf/brooklyn-library/blob/02abbab0/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/postgresql/PostgreSqlNodeChefImplFromScratch.java
----------------------------------------------------------------------
diff --git a/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/postgresql/PostgreSqlNodeChefImplFromScratch.java b/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/postgresql/PostgreSqlNodeChefImplFromScratch.java
deleted file mode 100644
index 96e65a5..0000000
--- a/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/postgresql/PostgreSqlNodeChefImplFromScratch.java
+++ /dev/null
@@ -1,168 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.brooklyn.entity.database.postgresql;
-
-import org.apache.brooklyn.api.effector.Effector;
-import org.apache.brooklyn.config.ConfigKey;
-import org.apache.brooklyn.core.config.ConfigKeys;
-import org.apache.brooklyn.core.effector.EffectorBody;
-import org.apache.brooklyn.core.effector.Effectors;
-import org.apache.brooklyn.core.effector.ssh.SshEffectorTasks;
-import org.apache.brooklyn.core.entity.Entities;
-import org.apache.brooklyn.core.location.Locations;
-import org.apache.brooklyn.entity.chef.ChefConfig;
-import org.apache.brooklyn.entity.chef.ChefLifecycleEffectorTasks;
-import org.apache.brooklyn.entity.chef.ChefServerTasks;
-import org.apache.brooklyn.entity.stock.EffectorStartableImpl;
-import org.apache.brooklyn.feed.ssh.SshFeed;
-import org.apache.brooklyn.feed.ssh.SshPollConfig;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.apache.brooklyn.location.ssh.SshMachineLocation;
-import org.apache.brooklyn.util.collections.Jsonya;
-import org.apache.brooklyn.util.core.ResourceUtils;
-import org.apache.brooklyn.util.core.config.ConfigBag;
-import org.apache.brooklyn.util.core.task.DynamicTasks;
-import org.apache.brooklyn.util.guava.Maybe;
-import org.apache.brooklyn.util.ssh.BashCommands;
-
-public class PostgreSqlNodeChefImplFromScratch extends EffectorStartableImpl implements PostgreSqlNode {
-
- private static final Logger LOG = LoggerFactory.getLogger(PostgreSqlNodeChefImplFromScratch.class);
-
- public static final Effector<String> EXECUTE_SCRIPT = Effectors.effector(String.class, "executeScript")
- .description("invokes a script")
- .parameter(ExecuteScriptEffectorBody.SCRIPT)
- .impl(new ExecuteScriptEffectorBody()).build();
-
- private SshFeed feed;
-
- public void init() {
- super.init();
- new ChefPostgreSqlLifecycle().attachLifecycleEffectors(this);
- }
-
- @Override
- public Integer getPostgreSqlPort() { return getAttribute(POSTGRESQL_PORT); }
-
- @Override
- public String getSharedMemory() { return getConfig(SHARED_MEMORY); }
-
- @Override
- public Integer getMaxConnections() { return getConfig(MAX_CONNECTIONS); }
-
- @Override
- public String getShortName() {
- return "PostgreSQL";
- }
-
- public static class ChefPostgreSqlLifecycle extends ChefLifecycleEffectorTasks {
- {
- usePidFile("/var/run/postgresql/*.pid");
- useService("postgresql");
- }
- protected void startWithKnifeAsync() {
- Entities.warnOnIgnoringConfig(entity(), ChefConfig.CHEF_LAUNCH_RUN_LIST);
- Entities.warnOnIgnoringConfig(entity(), ChefConfig.CHEF_LAUNCH_ATTRIBUTES);
-
- DynamicTasks.queue(
- ChefServerTasks
- .knifeConvergeRunList("postgresql::server")
- .knifeAddAttributes(Jsonya
- .at("postgresql", "config").add(
- "port", entity().getPostgreSqlPort(),
- "listen_addresses", "*").getRootMap())
- .knifeAddAttributes(Jsonya
- .at("postgresql", "pg_hba").list().map().add(
- "type", "host", "db", "all", "user", "all",
- "addr", "0.0.0.0/0", "method", "md5").getRootMap())
- // no other arguments currenty supported; chef will pick a password for us
- );
- }
- protected void postStartCustom() {
- super.postStartCustom();
-
- // now run the creation script
- String creationScript;
- String creationScriptUrl = entity().getConfig(PostgreSqlNode.CREATION_SCRIPT_URL);
- if (creationScriptUrl != null) {
- creationScript = ResourceUtils.create(entity()).getResourceAsString(creationScriptUrl);
- } else {
- creationScript = entity().getConfig(PostgreSqlNode.CREATION_SCRIPT_CONTENTS);
- }
- entity().executeScript(creationScript);
-
- // and finally connect sensors
- entity().connectSensors();
- }
- protected void preStopCustom() {
- entity().disconnectSensors();
- super.preStopCustom();
- }
- protected PostgreSqlNodeChefImplFromScratch entity() {
- return (PostgreSqlNodeChefImplFromScratch) super.entity();
- }
- }
-
- public static class ExecuteScriptEffectorBody extends EffectorBody<String> {
- public static final ConfigKey<String> SCRIPT = ConfigKeys.newStringConfigKey("script", "contents of script to run");
-
- public String call(ConfigBag parameters) {
- return DynamicTasks.queue(SshEffectorTasks.ssh(
- BashCommands.pipeTextTo(
- parameters.get(SCRIPT),
- BashCommands.sudoAsUser("postgres", "psql --file -")))
- .requiringExitCodeZero()).getStdout();
- }
- }
-
- protected void connectSensors() {
- sensors().set(DATASTORE_URL, String.format("postgresql://%s:%s/", getAttribute(HOSTNAME), getAttribute(POSTGRESQL_PORT)));
-
- Maybe<SshMachineLocation> machine = Locations.findUniqueSshMachineLocation(getLocations());
-
- if (machine.isPresent()) {
- feed = SshFeed.builder()
- .entity(this)
- .machine(machine.get())
- .poll(new SshPollConfig<Boolean>(SERVICE_UP)
- .command("ps -ef | grep [p]ostgres")
- .setOnSuccess(true)
- .setOnFailureOrException(false))
- .build();
- } else {
- LOG.warn("Location(s) {} not an ssh-machine location, so not polling for status; setting serviceUp immediately", getLocations());
- }
- }
-
- protected void disconnectSensors() {
- if (feed != null) feed.stop();
- }
-
- @Override
- public String executeScript(String commands) {
- return Entities.invokeEffector(this, this, EXECUTE_SCRIPT,
- ConfigBag.newInstance().configure(ExecuteScriptEffectorBody.SCRIPT, commands).getAllConfig()).getUnchecked();
- }
-
- @Override
- public void populateServiceNotUpDiagnostics() {
- // TODO no-op currently; should check ssh'able etc
- }
-}
http://git-wip-us.apache.org/repos/asf/brooklyn-library/blob/02abbab0/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/postgresql/PostgreSqlNodeImpl.java
----------------------------------------------------------------------
diff --git a/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/postgresql/PostgreSqlNodeImpl.java b/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/postgresql/PostgreSqlNodeImpl.java
deleted file mode 100644
index 0421a98..0000000
--- a/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/postgresql/PostgreSqlNodeImpl.java
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.brooklyn.entity.database.postgresql;
-
-import org.apache.brooklyn.core.effector.EffectorBody;
-import org.apache.brooklyn.entity.software.base.SoftwareProcessImpl;
-import org.apache.brooklyn.util.core.config.ConfigBag;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public class PostgreSqlNodeImpl extends SoftwareProcessImpl implements PostgreSqlNode {
-
- @SuppressWarnings("unused")
- private static final Logger LOG = LoggerFactory.getLogger(PostgreSqlNodeImpl.class);
-
- public Class<?> getDriverInterface() {
- return PostgreSqlDriver.class;
- }
- @Override
- public PostgreSqlDriver getDriver() {
- return (PostgreSqlDriver) super.getDriver();
- }
-
- @Override
- public Integer getPostgreSqlPort() { return getAttribute(POSTGRESQL_PORT); }
-
- @Override
- public String getSharedMemory() { return getConfig(SHARED_MEMORY); }
-
- @Override
- public Integer getMaxConnections() { return getConfig(MAX_CONNECTIONS); }
-
- @Override
- public void init() {
- super.init();
- getMutableEntityType().addEffector(EXECUTE_SCRIPT, new EffectorBody<String>() {
- @Override
- public String call(ConfigBag parameters) {
- return executeScript((String) parameters.getStringKey("commands"));
- }
- });
- }
-
- @Override
- protected void connectSensors() {
- super.connectSensors();
- connectServiceUpIsRunning();
- // in jdbc url it is postgresql:// but elsewhere it is usually postgres://
- // TODO add a public JDBC_URL ?
- // include user:pass@url ? (for all datastore types, and if so,
- // make sure ".with_credentials" or similar is in name so it is masked in ui)
- sensors().set(DATASTORE_URL, String.format("postgresql://%s:%s/", getAttribute(HOSTNAME), getAttribute(POSTGRESQL_PORT)));
- }
-
- @Override
- protected void disconnectSensors() {
- disconnectServiceUpIsRunning();
- super.disconnectSensors();
- }
-
- @Override
- public String getShortName() {
- return "PostgreSQL";
- }
-
- @Override
- public String executeScript(String commands) {
- return getDriver()
- .executeScriptAsync(commands)
- .block()
- .getStdout();
- }
-}
http://git-wip-us.apache.org/repos/asf/brooklyn-library/blob/02abbab0/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/postgresql/PostgreSqlSpecs.java
----------------------------------------------------------------------
diff --git a/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/postgresql/PostgreSqlSpecs.java b/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/postgresql/PostgreSqlSpecs.java
deleted file mode 100644
index b46374d..0000000
--- a/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/postgresql/PostgreSqlSpecs.java
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.brooklyn.entity.database.postgresql;
-
-import org.apache.brooklyn.api.entity.EntitySpec;
-import org.apache.brooklyn.entity.chef.ChefConfig;
-import org.apache.brooklyn.entity.chef.ChefConfig.ChefModes;
-
-/**
- * Utiltiy for creating specs for {@link PostgreSqlNode} instances.
- */
-public class PostgreSqlSpecs {
-
- private PostgreSqlSpecs() {}
-
- public static EntitySpec<PostgreSqlNode> spec() {
- return EntitySpec.create(PostgreSqlNode.class);
- }
-
- /** Requires {@code knife}. */
- public static EntitySpec<PostgreSqlNode> specChef() {
- EntitySpec<PostgreSqlNode> spec = EntitySpec.create(PostgreSqlNode.class, PostgreSqlNodeChefImplFromScratch.class);
- spec.configure(ChefConfig.CHEF_MODE, ChefModes.KNIFE);
- return spec;
- }
-}
http://git-wip-us.apache.org/repos/asf/brooklyn-library/blob/02abbab0/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/postgresql/PostgreSqlSshDriver.java
----------------------------------------------------------------------
diff --git a/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/postgresql/PostgreSqlSshDriver.java b/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/postgresql/PostgreSqlSshDriver.java
deleted file mode 100644
index 059c348..0000000
--- a/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/postgresql/PostgreSqlSshDriver.java
+++ /dev/null
@@ -1,471 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.brooklyn.entity.database.postgresql;
-
-import static java.lang.String.format;
-import static org.apache.brooklyn.util.ssh.BashCommands.INSTALL_WGET;
-import static org.apache.brooklyn.util.ssh.BashCommands.alternativesGroup;
-import static org.apache.brooklyn.util.ssh.BashCommands.chainGroup;
-import static org.apache.brooklyn.util.ssh.BashCommands.dontRequireTtyForSudo;
-import static org.apache.brooklyn.util.ssh.BashCommands.executeCommandThenAsUserTeeOutputToFile;
-import static org.apache.brooklyn.util.ssh.BashCommands.fail;
-import static org.apache.brooklyn.util.ssh.BashCommands.ifExecutableElse0;
-import static org.apache.brooklyn.util.ssh.BashCommands.ifExecutableElse1;
-import static org.apache.brooklyn.util.ssh.BashCommands.installPackage;
-import static org.apache.brooklyn.util.ssh.BashCommands.sudo;
-import static org.apache.brooklyn.util.ssh.BashCommands.sudoAsUser;
-import static org.apache.brooklyn.util.ssh.BashCommands.warn;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-
-import javax.annotation.Nullable;
-
-import org.apache.brooklyn.api.location.OsDetails;
-import org.apache.brooklyn.core.effector.ssh.SshEffectorTasks;
-import org.apache.brooklyn.core.entity.Attributes;
-import org.apache.brooklyn.core.sensor.BasicAttributeSensorAndConfigKey;
-import org.apache.brooklyn.location.ssh.SshMachineLocation;
-import org.apache.brooklyn.util.collections.MutableList;
-import org.apache.brooklyn.util.collections.MutableMap;
-import org.apache.brooklyn.util.core.task.DynamicTasks;
-import org.apache.brooklyn.util.core.task.ssh.SshTasks;
-import org.apache.brooklyn.util.core.task.ssh.SshTasks.OnFailingTask;
-import org.apache.brooklyn.util.core.task.system.ProcessTaskWrapper;
-import org.apache.brooklyn.util.exceptions.Exceptions;
-import org.apache.brooklyn.util.net.Urls;
-import org.apache.brooklyn.util.os.Os;
-import org.apache.brooklyn.util.stream.Streams;
-import org.apache.brooklyn.util.text.Identifiers;
-import org.apache.brooklyn.util.text.StringEscapes;
-import org.apache.brooklyn.util.text.StringFunctions;
-import org.apache.brooklyn.util.text.Strings;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.apache.brooklyn.entity.database.DatastoreMixins;
-import org.apache.brooklyn.entity.software.base.AbstractSoftwareProcessSshDriver;
-import org.apache.brooklyn.entity.software.base.SoftwareProcess;
-
-import com.google.common.base.Charsets;
-import com.google.common.base.Function;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Iterables;
-import com.google.common.io.Files;
-
-/**
- * The SSH implementation of the {@link PostgreSqlDriver}.
- */
-public class PostgreSqlSshDriver extends AbstractSoftwareProcessSshDriver implements PostgreSqlDriver {
-
- public static final Logger log = LoggerFactory.getLogger(PostgreSqlSshDriver.class);
-
- public PostgreSqlSshDriver(PostgreSqlNodeImpl entity, SshMachineLocation machine) {
- super(entity, machine);
-
- entity.sensors().set(Attributes.LOG_FILE_LOCATION, getLogFile());
- }
-
- /*
- * TODO this is much messier than we would like because postgres runs as user postgres,
- * meaning the dirs must be RW by that user, and accessible (thus all parent paths),
- * which may rule out putting it in a location used by the default user.
- * Two irritating things:
- * * currently we sometimes make up a different onbox base dir;
- * * currently we put files to /tmp for staging
- * Could investigate if it really needs to run as user postgres;
- * could also see whether default user can be added to group postgres,
- * and the run dir (and all parents) made accessible to group postgres.
- */
- @Override
- public void install() {
- String version = getEntity().getConfig(SoftwareProcess.SUGGESTED_VERSION);
- String majorMinorVersion = version.substring(0, version.lastIndexOf("-"));
- String shortVersion = majorMinorVersion.replace(".", "");
-
- String altTarget = "/opt/brooklyn/postgres/";
- String altInstallDir = Urls.mergePaths(altTarget, "install/"+majorMinorVersion);
-
- Iterable<String> pgctlLocations = ImmutableList.of(
- altInstallDir+"/bin",
- "/usr/lib/postgresql/"+majorMinorVersion+"/bin/",
- "/opt/local/lib/postgresql"+shortVersion+"/bin/",
- "/usr/pgsql-"+majorMinorVersion+"/bin",
- "/usr/local/bin/",
- "/usr/bin/",
- "/bin/");
-
- DynamicTasks.queueIfPossible(SshTasks.dontRequireTtyForSudo(getMachine(),
- // sudo is absolutely required here, in customize we set user to postgres
- OnFailingTask.FAIL)).orSubmitAndBlock();
- DynamicTasks.waitForLast();
-
- // Check whether we can find a usable pg_ctl, and if not install one
- MutableList<String> findOrInstall = MutableList.<String>of()
- .append("which pg_ctl")
- .appendAll(Iterables.transform(pgctlLocations, StringFunctions.formatter("test -x %s/pg_ctl")))
- .append(installPackage(ImmutableMap.of(
- "yum", "postgresql"+shortVersion+" postgresql"+shortVersion+"-server",
- "apt", "postgresql-"+majorMinorVersion,
- "port", "postgresql"+shortVersion+" postgresql"+shortVersion+"-server"
- ), null))
- // due to impl of installPackage, it will not come to the line below I don't think
- .append(warn(format("WARNING: failed to find or install postgresql %s binaries", majorMinorVersion)));
-
- // Link to correct binaries folder (different versions of pg_ctl and psql don't always play well together)
- MutableList<String> linkFromHere = MutableList.<String>of()
- .append(ifExecutableElse1("pg_ctl", chainGroup(
- "PG_EXECUTABLE=`which pg_ctl`",
- "PG_DIR=`dirname $PG_EXECUTABLE`",
- "echo 'found pg_ctl in '$PG_DIR' on path so linking PG bin/ to that dir'",
- "ln -s $PG_DIR bin")))
- .appendAll(Iterables.transform(pgctlLocations, givenDirIfFileExistsInItLinkToDir("pg_ctl", "bin")))
- .append(fail(format("WARNING: failed to find postgresql %s binaries for pg_ctl, may already have another version installed; aborting", majorMinorVersion), 9));
-
- newScript(INSTALLING)
- .body.append(
- dontRequireTtyForSudo(),
- ifExecutableElse0("yum", getYumRepository(version, majorMinorVersion, shortVersion)),
- ifExecutableElse0("apt-get", getAptRepository()),
- "rm -f bin", // if left over from previous incomplete/failed install (not sure why that keeps happening!)
- alternativesGroup(findOrInstall),
- alternativesGroup(linkFromHere))
- .failOnNonZeroResultCode()
- .queue();
-
- // check that the proposed install dir is one that user postgres can access
- // TODO if command above fails then the following `getUnchecked` blocks forever
- if (DynamicTasks.queue(SshEffectorTasks.ssh(sudoAsUser("postgres", "ls "+getInstallDir())).allowingNonZeroExitCode()
- .summary("check postgres user can access install dir")).asTask().getUnchecked()!=0) {
- log.info("Postgres install dir "+getInstallDir()+" for "+getEntity()+" is not accessible to user 'postgres'; " + "using "+altInstallDir+" instead");
- String newRunDir = Urls.mergePaths(altTarget, "apps", getEntity().getApplication().getId(), getEntity().getId());
- if (DynamicTasks.queue(SshEffectorTasks.ssh("ls "+altInstallDir+"/pg_ctl").allowingNonZeroExitCode()
- .summary("check whether "+altInstallDir+" is set up")).asTask().getUnchecked()==0) {
- // alt target already exists with binary; nothing to do for install
- } else {
- DynamicTasks.queue(SshEffectorTasks.ssh(
- "mkdir -p "+altInstallDir,
- "rm -rf '"+altInstallDir+"'",
- "mv "+getInstallDir()+" "+altInstallDir,
- "rm -rf '"+getInstallDir()+"'",
- "ln -s "+altInstallDir+" "+getInstallDir(),
- "mkdir -p " + newRunDir,
- "chown -R postgres:postgres "+altTarget).runAsRoot().requiringExitCodeZero()
- .summary("move install dir from user to postgres owned space"));
- }
- DynamicTasks.waitForLast();
- setInstallDir(altInstallDir);
- setRunDir(newRunDir);
- }
- }
-
- private String getYumRepository(String version, String majorMinorVersion, String shortVersion) {
- // postgres becomes available if you add the repos using an RPM such as
- // http://yum.postgresql.org/9.3/redhat/rhel-6-i386/pgdg-centos93-9.3-1.noarch.rpm
- // fedora, rhel, sl, and centos supported for RPM's
-
- OsDetails osDetails = getMachine().getMachineDetails().getOsDetails();
- String arch = osDetails.getArch();
- String osMajorVersion = osDetails.getVersion();
- String osName = osDetails.getName();
-
- log.debug("postgres detecting yum information for "+getEntity()+" at "+getMachine()+": "+osName+", "+osMajorVersion+", "+arch);
-
- if (osName==null) osName = ""; else osName = osName.toLowerCase();
-
- if (osName.equals("ubuntu")) return "echo skipping yum repo setup as this is not an rpm environment";
-
- if (osName.equals("rhel") || osName.contains("red hat")) osName = "redhat";
- else if (osName.equals("centos")) osName = "centos";
- else if (osName.equals("sl") || osName.startsWith("scientific")) osName = "sl";
- else if (osName.equals("fedora")) osName = "fedora";
- else {
- log.debug("insufficient OS family information '"+osName+"' for "+getMachine()+" when installing "+getEntity()+" (yum repos); treating as centos");
- osName = "centos";
- }
-
- if (Strings.isBlank(arch)) {
- log.warn("Insuffient architecture information '"+arch+"' for "+getMachine()+"when installing "+getEntity()+"; treating as x86_64");
- arch = "x86_64";
- }
-
- if (Strings.isBlank(osMajorVersion)) {
- if (osName.equals("fedora")) osMajorVersion = "20";
- else osMajorVersion = "6";
- log.warn("Insuffient OS version information '"+getMachine().getOsDetails().getVersion()+"' for "+getMachine()+"when installing "+getEntity()+" (yum repos); treating as "+osMajorVersion);
- } else {
- if (osMajorVersion.indexOf(".")>0)
- osMajorVersion = osMajorVersion.substring(0, osMajorVersion.indexOf('.'));
- }
-
- return chainGroup(
- INSTALL_WGET,
- sudo(format("wget http://yum.postgresql.org/%s/redhat/rhel-%s-%s/pgdg-%s%s-%s.noarch.rpm", majorMinorVersion, osMajorVersion, arch, osName, shortVersion, version)),
- sudo(format("rpm -Uvh pgdg-%s%s-%s.noarch.rpm", osName, shortVersion, version))
- );
- }
-
- private String getAptRepository() {
- return chainGroup(
- INSTALL_WGET,
- "wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo tee -a apt-key add -",
- "echo \"deb http://apt.postgresql.org/pub/repos/apt/ $(sudo lsb_release --codename --short)-pgdg main\" | sudo tee -a /etc/apt/sources.list.d/postgresql.list"
- );
- }
-
- private static Function<String, String> givenDirIfFileExistsInItLinkToDir(final String filename, final String linkToMake) {
- return new Function<String, String>() {
- public String apply(@Nullable String dir) {
- return ifExecutableElse1(Urls.mergePaths(dir, filename),
- chainGroup("echo 'found "+filename+" in "+dir+" so linking to it in "+linkToMake+"'", "ln -s "+dir+" "+linkToMake));
- }
- };
- }
-
- @Override
- 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.
- }
-
- private void initializeNewDatabase() {
- String createUserCommand = String.format(
- "\"CREATE USER %s WITH PASSWORD '%s'; \"",
- StringEscapes.escapeSql(getUsername()),
- StringEscapes.escapeSql(getUserPassword())
- );
- String createDatabaseCommand = String.format(
- "\"CREATE DATABASE %s OWNER %s\"",
- StringEscapes.escapeSql(getDatabaseName()),
- StringEscapes.escapeSql(getUsername()));
- newScript("initializing user and database")
- .body.append(
- "cd " + getInstallDir(),
- callPgctl("start", true),
- sudoAsUser("postgres", getInstallDir() + "/bin/psql -p " + entity.getAttribute(PostgreSqlNode.POSTGRESQL_PORT) +
- " --command="+ createUserCommand),
- sudoAsUser("postgres", getInstallDir() + "/bin/psql -p " + entity.getAttribute(PostgreSqlNode.POSTGRESQL_PORT) +
- " --command="+ createDatabaseCommand),
- callPgctl("stop", true))
- .failOnNonZeroResultCode().execute();
- }
-
- private String getConfigOrDefault(BasicAttributeSensorAndConfigKey<String> key, String def) {
- String config = entity.getConfig(key);
- if(Strings.isEmpty(config)) {
- config = def;
- log.debug(entity + " has no config specified for " + key + "; using default `" + def + "`");
- entity.sensors().set(key, config);
- }
- return config;
- }
-
- protected String getDatabaseName() {
- return getConfigOrDefault(PostgreSqlNode.DATABASE, PostgreSqlNode.DEFAULT_DB_NAME);
- }
-
- protected String getUsername(){
- return getConfigOrDefault(PostgreSqlNode.USERNAME, PostgreSqlNode.DEFAULT_USERNAME);
- }
-
- protected String getUserPassword() {
- return getConfigOrDefault(PostgreSqlNode.PASSWORD, Strings.makeRandomId(8));
- }
-
- protected void executeDatabaseCreationScript() {
- if (copyDatabaseCreationScript()) {
- newScript("running postgres creation script")
- .body.append(
- "cd " + getInstallDir(),
- callPgctl("start", true),
- sudoAsUser("postgres", getInstallDir() + "/bin/psql -p " + entity.getAttribute(PostgreSqlNode.POSTGRESQL_PORT) + " --file " + getRunDir() + "/creation-script.sql"),
- callPgctl("stop", true))
- .failOnNonZeroResultCode()
- .execute();
- }
- }
-
- private boolean installFile(InputStream contents, String destName) {
- String uid = Identifiers.makeRandomId(8);
- // TODO currently put in /tmp for staging, since run dir may not be accessible to ssh user
- getMachine().copyTo(contents, "/tmp/"+destName+"_"+uid);
- DynamicTasks.queueIfPossible(SshEffectorTasks.ssh(
- "cd "+getRunDir(),
- "mv /tmp/"+destName+"_"+uid+" "+destName,
- "chown postgres:postgres "+destName,
- "chmod 644 "+destName)
- .runAsRoot().requiringExitCodeZero())
- .orSubmitAndBlock(getEntity()).andWaitForSuccess();
- return true;
- }
- private boolean copyDatabaseCreationScript() {
- InputStream creationScript = DatastoreMixins.getDatabaseCreationScript(entity);
- if (creationScript==null)
- return false;
- return installFile(creationScript, "creation-script.sql");
- }
-
- public String getDataDir() {
- return getRunDir() + "/data";
- }
-
- public String getLogFile() {
- return getRunDir() + "/postgresql.log";
- }
-
- public String getPidFile() {
- return getRunDir() + "/postgresql.pid";
- }
-
- /** @deprecated since 0.7.0 renamed {@link #logTailOfPostgresLog()} */
- @Deprecated
- public void copyLogFileContents() { logTailOfPostgresLog(); }
- public void logTailOfPostgresLog() {
- try {
- File file = Os.newTempFile("postgresql-"+getEntity().getId(), "log");
- int result = getMachine().copyFrom(getLogFile(), file.getAbsolutePath());
- if (result != 0) throw new IllegalStateException("Could not access log file " + getLogFile());
- log.info("Saving {} contents as {}", getLogFile(), file);
- Streams.logStreamTail(log, "postgresql.log", Streams.byteArrayOfString(Files.toString(file, Charsets.UTF_8)), 1024);
- file.delete();
- } catch (IOException ioe) {
- log.debug("Error reading copied log file: {}", ioe);
- }
- }
-
- protected String callPgctl(String command, boolean waitForIt) {
- return sudoAsUser("postgres", getInstallDir() + "/bin/pg_ctl -D " + getDataDir() +
- " -l " + getLogFile() + (waitForIt ? " -w " : " ") + command);
- }
-
- @Override
- public void launch() {
- log.info(String.format("Starting entity %s at %s", this, getLocation()));
- newScript(MutableMap.of("usePidFile", false), LAUNCHING)
- .body.append(callPgctl("start", false))
- .execute();
- }
-
- @Override
- public boolean isRunning() {
- return newScript(MutableMap.of("usePidFile", getPidFile()), CHECK_RUNNING)
- .body.append(getStatusCmd())
- .execute() == 0;
- }
-
- @Override
- public void stop() {
- newScript(MutableMap.of("usePidFile", false), STOPPING)
- .body.append(callPgctl((entity.getConfig(PostgreSqlNode.DISCONNECT_ON_STOP) ? "-m immediate " : "") + "stop", false))
- .failOnNonZeroResultCode()
- .execute();
- newScript(MutableMap.of("usePidFile", getPidFile(), "processOwner", "postgres"), STOPPING).execute();
- }
-
- @Override
- public PostgreSqlNodeImpl getEntity() {
- return (PostgreSqlNodeImpl) super.getEntity();
- }
-
- @Override
- public String getStatusCmd() {
- return callPgctl("status", false);
- }
-
- public ProcessTaskWrapper<Integer> executeScriptAsync(String commands) {
- String filename = "postgresql-commands-"+Identifiers.makeRandomId(8);
- installFile(Streams.newInputStreamWithContents(commands), filename);
- return executeScriptFromInstalledFileAsync(filename);
- }
-
- public ProcessTaskWrapper<Integer> executeScriptFromInstalledFileAsync(String filenameAlreadyInstalledAtServer) {
- return DynamicTasks.queue(
- SshEffectorTasks.ssh(
- "cd "+getRunDir(),
- sudoAsUser("postgres", getInstallDir() + "/bin/psql -p " + entity.getAttribute(PostgreSqlNode.POSTGRESQL_PORT) + " --file " + filenameAlreadyInstalledAtServer))
- .summary("executing datastore script "+filenameAlreadyInstalledAtServer));
- }
-
-}
http://git-wip-us.apache.org/repos/asf/brooklyn-library/blob/02abbab0/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/rubyrep/RubyRepDriver.java
----------------------------------------------------------------------
diff --git a/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/rubyrep/RubyRepDriver.java b/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/rubyrep/RubyRepDriver.java
deleted file mode 100644
index ab4eec0..0000000
--- a/brooklyn-library/software/database/src/main/java/org/apache/brooklyn/entity/database/rubyrep/RubyRepDriver.java
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.brooklyn.entity.database.rubyrep;
-
-import org.apache.brooklyn.entity.software.base.SoftwareProcessDriver;
-
-/**
- * The driver interface for {@link RubyRepNode}.
- */
-public interface RubyRepDriver extends SoftwareProcessDriver {
-
-}