use of io.fabric8.kubernetes.api.model in project fabric8 by jboss-fuse.
the class ZooKeeperClusterBootstrapImpl method stopBundles.
private void stopBundles() throws BundleException {
BundleUtils bundleUtils = new BundleUtils(bundleContext);
bundleUtils.findAndStopBundle("io.fabric8.fabric-agent");
// bundleUtils.findAndStopBundle("org.ops4j.pax.web.pax-web-jetty");
}
use of io.fabric8.kubernetes.api.model in project fabric8 by jboss-fuse.
the class ZooKeeperClusterBootstrapImpl method startBundles.
private void startBundles(CreateEnsembleOptions options) throws BundleException {
BundleUtils bundleUtils = new BundleUtils(bundleContext);
Bundle agentBundle = bundleUtils.findBundle("io.fabric8.fabric-agent");
if (agentBundle != null && options.isAgentEnabled()) {
agentBundle.start();
}
// Bundle webBundle = bundleUtils.findBundle("org.ops4j.pax.web.pax-web-jetty");
// if (webBundle != null) {
// webBundle.start();
// }
}
use of io.fabric8.kubernetes.api.model in project fabric8 by jboss-fuse.
the class MQServiceImpl method createOrUpdateMQProfile.
@Override
public Profile createOrUpdateMQProfile(String versionId, String profileId, String brokerName, Map<String, String> configs, boolean replicated) {
Version version = profileService.getRequiredVersion(versionId);
String parentProfileName = null;
if (configs != null && configs.containsKey("parent")) {
parentProfileName = configs.remove("parent");
}
if (Strings.isNullOrBlank(parentProfileName)) {
parentProfileName = replicated ? MQ_PROFILE_REPLICATED : MQ_PROFILE_BASE;
}
Profile parentProfile = version.getRequiredProfile(parentProfileName);
if (brokerName == null || profileId == null) {
return parentProfile;
}
String pidName = getBrokerPID(brokerName);
// lets check we have a config value
ProfileBuilder builder;
Profile overlay;
// create a profile if it doesn't exist
Map<String, String> config = null;
boolean create = !version.hasProfile(profileId);
if (create) {
builder = ProfileBuilder.Factory.create(versionId, profileId);
if (parentProfile != null) {
builder.addParent(parentProfile.getId());
}
overlay = profileService.getOverlayProfile(parentProfile);
} else {
Profile profile = version.getRequiredProfile(profileId);
builder = ProfileBuilder.Factory.createFrom(profile);
config = builder.getConfiguration(pidName);
overlay = profileService.getOverlayProfile(profile);
}
Map<String, String> parentProfileConfig = ProfileBuilder.Factory.createFrom(overlay).getConfiguration(MQ_PID_TEMPLATE);
if (config == null) {
config = parentProfileConfig;
}
if (configs != null && "true".equals(configs.get("ssl"))) {
// Only generate the keystore file if it does not exist.
// [TOOD] Fix direct data access! This should be part of the ProfileBuilder
byte[] keystore = overlay.getFileConfiguration("keystore.jks");
if (keystore == null) {
try {
String host = configs.get("keystore.cn");
if (host == null) {
host = configs.get(GROUP);
if (host == null) {
host = "localhost";
}
configs.put("keystore.cn", host);
}
String password = configs.get("keystore.password");
if (password == null) {
password = generatePassword(8);
configs.put("keystore.password", password);
}
File keystoreFile = io.fabric8.utils.Files.createTempFile(runtimeProperties.getDataPath());
keystoreFile.delete();
LOG.info("Generating ssl keystore...");
int rc = system("keytool", "-genkey", "-storetype", "JKS", "-storepass", password, "-keystore", keystoreFile.getCanonicalPath(), "-keypass", password, "-alias", host, "-keyalg", "RSA", "-keysize", "4096", "-dname", String.format("cn=%s", host), "-validity", "3650");
if (rc != 0) {
throw new IOException("keytool failed with exit code: " + rc);
}
keystore = Files.readBytes(keystoreFile);
keystoreFile.delete();
LOG.info("Keystore generated");
builder.addFileConfiguration("keystore.jks", keystore);
configs.put("keystore.file", "profile:keystore.jks");
} catch (IOException e) {
LOG.error("Failed to generate keystore.jks: " + e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
}
}
// [TOOD] Fix direct data access! This should be part of the ProfileBuilder
byte[] truststore = overlay.getFileConfiguration("truststore.jks");
if (truststore == null && configs.get("keystore.password") != null) {
try {
String password = configs.get("truststore.password");
if (password == null) {
password = configs.get("keystore.password");
configs.put("truststore.password", password);
}
File keystoreFile = io.fabric8.utils.Files.createTempFile(runtimeProperties.getDataPath());
Files.writeToFile(keystoreFile, keystore);
File certFile = io.fabric8.utils.Files.createTempFile(runtimeProperties.getDataPath());
certFile.delete();
LOG.info("Exporting broker certificate to create truststore.jks");
int rc = system("keytool", "-exportcert", "-rfc", "-keystore", keystoreFile.getCanonicalPath(), "-storepass", configs.get("keystore.password"), "-alias", configs.get("keystore.cn"), "--file", certFile.getCanonicalPath());
keystoreFile.delete();
if (rc != 0) {
throw new IOException("keytool failed with exit code: " + rc);
}
LOG.info("Creating truststore.jks");
File truststoreFile = io.fabric8.utils.Files.createTempFile(runtimeProperties.getDataPath());
truststoreFile.delete();
rc = system("keytool", "-importcert", "-noprompt", "-keystore", truststoreFile.getCanonicalPath(), "-storepass", password, "--file", certFile.getCanonicalPath());
certFile.delete();
if (rc != 0) {
throw new IOException("keytool failed with exit code: " + rc);
}
truststore = Files.readBytes(truststoreFile);
truststoreFile.delete();
builder.addFileConfiguration("truststore.jks", truststore);
configs.put("truststore.file", "profile:truststore.jks");
} catch (IOException e) {
LOG.error("Failed to generate truststore.jks due: " + e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
}
}
}
config.put("broker-name", brokerName);
if (configs != null) {
config.putAll(configs);
}
// lets check we've a bunch of config values inherited from the template
String[] propertiesToDefault = { CONFIG_URL, STANDBY_POOL, CONNECTORS };
for (String key : propertiesToDefault) {
if (config.get(key) == null) {
String defaultValue = parentProfileConfig.get(key);
if (Strings.isNotBlank(defaultValue)) {
config.put(key, defaultValue);
}
}
}
// config map is not from "official" profile, so it doesn't have to use felix' Properties class
builder.addConfiguration(pidName, config);
Profile profile = builder.getProfile();
return create ? profileService.createProfile(profile) : profileService.updateProfile(profile);
}
use of io.fabric8.kubernetes.api.model in project fabric8 by jboss-fuse.
the class HealthCheck method healthList.
@Override
public List<HealthStatus> healthList() {
List<HealthStatus> answer = new ArrayList<HealthStatus>();
FabricStatus status = fabricService.getFabricStatus();
Collection<ProfileStatus> statuses = status.getProfileStatusMap().values();
for (ProfileStatus profile : statuses) {
String id = profile.getProfile();
int instances = profile.getCount();
Integer minimum = profile.getMinimumInstances();
Integer maximum = profile.getMaximumInstances();
double healthPercent = profile.getHealth(instances);
String level = "INFO";
String message = "Profile " + id + " has health " + percentInstance.format(healthPercent);
if (minimum != null) {
if (instances <= 0) {
level = "ERROR";
message = "Profile " + id + " has no instances running! Should have at least " + minimum;
} else if (instances < minimum) {
level = "WARNING";
message = "Profile " + id + " needs more instances running. Should have at least " + minimum + " but currently has only " + instances;
}
}
if (maximum != null && level.equals("INFO") && instances > maximum) {
level = "WARNING";
message = "Profile " + id + " has too many instances running. Should have at most " + maximum + " but currently has only " + instances;
}
answer.add(new HealthStatus("io.fabric8.profileHealth", id, level, message, instances, minimum, maximum, healthPercent));
}
String worries = "";
for (HealthStatus hs : answer) {
if ("WARNING".equals(hs.getLevel()) || "ERROR".equals(hs.getLevel())) {
worries += hs + " , ";
}
}
if ("".equals(worries)) {
this.currentStatus = "Good";
} else {
this.currentStatus = "Getting Worried {" + worries + " }";
}
return answer;
}
use of io.fabric8.kubernetes.api.model in project fabric8 by jboss-fuse.
the class ContainerProviderUtils method buildInstallAndStartScript.
/**
* Creates a shell script for installing and starting up a container.
*
* @param options
* @return
* @throws MalformedURLException
*/
public static String buildInstallAndStartScript(String name, CreateRemoteContainerOptions options) throws MalformedURLException, URISyntaxException {
String distFilename = String.format(DISTNAME_PATTERN, "karaf", FabricConstants.FABRIC_VERSION);
String systemDistPath = String.format(SYSTEM_DIST, "karaf", FabricConstants.FABRIC_VERSION);
StringBuilder sb = new StringBuilder();
sb.append("#!/bin/bash").append("\n");
if (logger.isTraceEnabled()) {
sb.append("set -x ").append("\n");
sb.append("export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }' ").append("\n");
}
// Export environmental variables
HashMap<String, String> environmentalVariables = new HashMap<>();
if (options.getEnvironmentalVariables() != null) {
environmentalVariables.putAll(options.getEnvironmentalVariables());
}
for (Map.Entry<String, String> entry : environmentalVariables.entrySet()) {
sb.append("export ").append(entry.getKey()).append("=\"").append(entry.getValue()).append("\"").append("\n");
}
sb.append(RUN_FUNCTION).append("\n");
sb.append(SUDO_N_FUNCTION).append("\n");
sb.append(DOWNLOAD_FUNCTION).append("\n");
sb.append(MAVEN_DOWNLOAD_FUNCTION).append("\n");
sb.append(UPDATE_PKGS).append("\n");
sb.append(INSTALL_CURL).append("\n");
sb.append(INSTALL_UNZIP).append("\n");
sb.append(INSTALL_JDK).append("\n");
sb.append(VALIDATE_REQUIREMENTS).append("\n");
sb.append(EXIT_IF_NOT_EXISTS).append("\n");
sb.append(COPY_NODE_METADATA).append("\n");
sb.append(KARAF_CHECK).append("\n");
sb.append(REPLACE_IN_FILE).append("\n");
sb.append(REPLACE_PROPERTY_VALUE).append("\n");
sb.append(UNCOMMENT_LINE).append("\n");
sb.append(CONFIGURE_HOSTNAMES).append("\n");
sb.append(FIND_FREE_PORT).append("\n");
sb.append(WAIT_FOR_PORT).append("\n");
sb.append(EXTRACT_ZIP).append("\n");
sb.append(GENERATE_SSH_KEYS).append("\n");
sb.append("run mkdir -p ").append(options.getPath()).append("\n");
sb.append("run cd ").append(options.getPath()).append("\n");
sb.append("run mkdir -p ").append(name).append("\n");
sb.append("run cd ").append(name).append("\n");
// We need admin access to be able to install curl & java.
if (options.isAdminAccess()) {
// This is not really needed.
// Its just here as a silly workaround for some cases which fail to get the first thing installed.
sb.append("update_pkgs").append("\n");
sb.append("install_openjdk").append("\n");
sb.append("install_curl").append("\n");
sb.append("install_unzip").append("\n");
}
sb.append("validate_requirements").append("\n");
List<String> fallbackRepositories = new ArrayList<String>();
List<String> optionsRepos = options.getFallbackRepositories();
if (optionsRepos != null) {
fallbackRepositories.addAll(optionsRepos);
}
extractZipIntoDirectory(sb, options.getProxyUri(), "io.fabric8", "fabric8-karaf", FabricConstants.FABRIC_VERSION, fallbackRepositories);
sb.append("run cd `").append(FIRST_FABRIC_DIRECTORY).append("`\n");
sb.append("run mkdir -p ").append(systemDistPath).append("\n");
sb.append("run cp ../").append(distFilename).append(" ").append(systemDistPath).append("/\n");
sb.append("run rm ../").append(distFilename).append("\n");
sb.append("run chmod +x bin/*").append("\n");
List<String> lines = new ArrayList<String>();
String globalResolver = options.getResolver() != null ? options.getResolver() : ZkDefs.DEFAULT_RESOLVER;
lines.add(ZkDefs.GLOBAL_RESOLVER_PROPERTY + "=" + globalResolver);
if (options.getBindAddress() != null && !options.getBindAddress().isEmpty()) {
lines.add(ZkDefs.BIND_ADDRESS + "=" + options.getBindAddress());
}
if (options.getManualIp() != null && !options.getManualIp().isEmpty()) {
lines.add(ZkDefs.MANUAL_IP + "=" + options.getManualIp());
}
appendFile(sb, "etc/system.properties", lines);
replacePropertyValue(sb, "etc/system.properties", "karaf.name", name);
String dataStoreFile = "etc/" + Constants.DATASTORE_PID + ".cfg";
for (Map.Entry<String, String> entry : options.getDataStoreProperties().entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
replacePropertyValue(sb, dataStoreFile, key, value);
uncomment_line(sb, dataStoreFile, key);
}
// Apply port range
sb.append("BIND_ADDRESS=").append(options.getBindAddress() != null && !options.getBindAddress().isEmpty() ? options.getBindAddress() : "0.0.0.0").append("\n");
sb.append("SSH_PORT=").append("\"").append("`find_free_port ").append(Ports.mapPortToRange(DEFAULT_SSH_PORT, options.getMinimumPort(), options.getMaximumPort())).append(" ").append(options.getMaximumPort()).append("`\"").append("\n");
sb.append("RMI_REGISTRY_PORT=").append("\"").append("`find_free_port ").append(Ports.mapPortToRange(DEFAULT_RMI_REGISTRY_PORT, options.getMinimumPort(), options.getMaximumPort())).append(" ").append(options.getMaximumPort()).append("`\"").append("\n");
sb.append("RMI_SERVER_PORT=").append("\"").append("`find_free_port ").append(Ports.mapPortToRange(DEFAULT_RMI_SERVER_PORT, options.getMinimumPort(), options.getMaximumPort())).append(" ").append(options.getMaximumPort()).append("`\"").append("\n");
sb.append("JMX_SERVER_URL=\"").append("service:jmx:rmi:\\/\\/${BIND_ADDRESS}:${RMI_SERVER_PORT}\\/jndi\\/rmi:\\/\\/${BIND_ADDRESS}:${RMI_REGISTRY_PORT}\\/karaf-").append(name).append("\"\n");
sb.append("HTTP_PORT=").append("\"").append("`find_free_port ").append(Ports.mapPortToRange(DEFAULT_HTTP_PORT, options.getMinimumPort(), options.getMaximumPort())).append(" ").append(options.getMaximumPort()).append("`\"").append("\n");
replacePropertyValue(sb, "etc/org.apache.karaf.shell.cfg", "sshPort", "$SSH_PORT");
replacePropertyValue(sb, "etc/org.apache.karaf.shell.cfg", "sshHost", "$BIND_ADDRESS");
replacePropertyValue(sb, "etc/org.apache.karaf.management.cfg", "rmiRegistryPort", "$RMI_REGISTRY_PORT");
replacePropertyValue(sb, "etc/org.apache.karaf.management.cfg", "rmiServerPort", "$RMI_SERVER_PORT");
replacePropertyValue(sb, "etc/org.apache.karaf.management.cfg", "rmiServerHost", "$BIND_ADDRESS");
replacePropertyValue(sb, "etc/org.apache.karaf.management.cfg", "rmiRegistryHost", "$BIND_ADDRESS");
// ENTESB-2733: do not change serviceUrl, let's leave it with placeholders
// replacePropertyValue(sb, "etc/org.apache.karaf.management.cfg", "serviceUrl", "$JMX_SERVER_URL");
replacePropertyValue(sb, "etc/org.ops4j.pax.web.cfg", "org.osgi.service.http.port", "$HTTP_PORT");
replaceLineInFile(sb, "etc/jetty.xml", String.valueOf(DEFAULT_HTTP_PORT), "$HTTP_PORT");
appendFile(sb, "etc/system.properties", Arrays.asList(ZkDefs.MINIMUM_PORT + "=" + options.getMinimumPort()));
appendFile(sb, "etc/system.properties", Arrays.asList(ZkDefs.MAXIMUM_PORT + "=" + options.getMaximumPort()));
appendFile(sb, "etc/system.properties", Arrays.asList("\n"));
// Read all system properties
for (Map.Entry<String, Properties> entry : options.getSystemProperties().entrySet()) {
Properties sysprops = entry.getValue();
for (Map.Entry syspropEntry : sysprops.entrySet()) {
Object type = syspropEntry.getKey();
Object value = syspropEntry.getValue();
appendFile(sb, "etc/system.properties", Arrays.asList(type + "=" + value));
}
}
// TODO: Be simple & move all of the code below under system properties MAP.
if (options.getPreferredAddress() != null) {
appendFile(sb, "etc/system.properties", Arrays.asList(HostUtils.PREFERED_ADDRESS_PROPERTY_NAME + "=" + options.getPreferredAddress()));
}
String zkPasswordEncode = System.getProperty("zookeeper.password.encode", "true");
if (options.isEnsembleServer()) {
appendFile(sb, "etc/system.properties", Arrays.asList("zookeeper.password = " + options.getZookeeperPassword()));
appendFile(sb, "etc/system.properties", Arrays.asList("zookeeper.password.encode = " + zkPasswordEncode));
appendFile(sb, "etc/system.properties", Arrays.asList(CreateEnsembleOptions.ENSEMBLE_AUTOSTART + "=true"));
appendFile(sb, "etc/system.properties", Arrays.asList(CreateEnsembleOptions.AGENT_AUTOSTART + "=true"));
appendFile(sb, "etc/system.properties", Arrays.asList(CreateEnsembleOptions.PROFILES_AUTOIMPORT_PATH + "=${runtime.home}/fabric/import/"));
if (options.getUsers() != null) {
appendFile(sb, "etc/users.properties", Arrays.asList("\n"));
for (Map.Entry<String, String> entry : options.getUsers().entrySet()) {
appendFile(sb, "etc/users.properties", Arrays.asList(entry.getKey() + "=" + entry.getValue()));
}
}
} else if (options.getZookeeperUrl() != null) {
appendFile(sb, "etc/system.properties", Arrays.asList("zookeeper.url = " + options.getZookeeperUrl()));
appendFile(sb, "etc/system.properties", Arrays.asList("zookeeper.password = " + options.getZookeeperPassword()));
appendFile(sb, "etc/system.properties", Arrays.asList("zookeeper.password.encode = " + zkPasswordEncode));
appendFile(sb, "etc/system.properties", Arrays.asList(CreateEnsembleOptions.AGENT_AUTOSTART + "=true"));
appendToLineInFile(sb, "etc/org.apache.karaf.features.cfg", "featuresBoot=", "fabric-agent,fabric-git,");
}
// Add the proxyURI to the list of repositories
if (options.getProxyUri() != null) {
appendToLineInFile(sb, "etc/org.ops4j.pax.url.mvn.cfg", "repositories=", options.getProxyUri().toString() + ",");
}
// Just for ensemble servers we want to copy their creation metadata for import.
if (options.isEnsembleServer()) {
CreateContainerMetadata metadata = options.getMetadataMap().get(name);
if (metadata != null) {
byte[] metadataPayload = ObjectUtils.toBytes(metadata);
if (metadataPayload != null && metadataPayload.length > 0) {
sb.append("copy_node_metadata ").append(name).append(" ").append(new String(Base64Encoder.encode(metadataPayload))).append("\n");
}
}
}
sb.append("generate_ssh_keys").append("\n");
sb.append("configure_hostnames").append(" ").append(options.getHostNameContext()).append("\n");
String jvmOptions = options.getJvmOpts();
if (jvmOptions == null) {
jvmOptions = "-XX:+UnlockDiagnosticVMOptions -XX:+UnsyncloadClass";
} else if (!jvmOptions.contains("-XX:+UnlockDiagnosticVMOptions -XX:+UnsyncloadClass")) {
jvmOptions = jvmOptions + " -XX:+UnlockDiagnosticVMOptions -XX:+UnsyncloadClass";
}
if (!jvmOptions.contains("-server")) {
jvmOptions = jvmOptions + " -server";
}
environmentalVariables.put("JAVA_OPTS", jvmOptions);
ArrayList<String> setenv = new ArrayList<String>();
for (Map.Entry<String, String> entry : environmentalVariables.entrySet()) {
setenv.add("export " + entry.getKey() + "=\"" + entry.getValue() + "\"\n");
}
writeFile(sb, "bin/setenv", setenv);
sb.append("nohup bin/start &").append("\n");
sb.append("karaf_check `pwd`").append("\n");
sb.append("wait_for_port $SSH_PORT").append("\n");
sb.append("wait_for_port $RMI_REGISTRY_PORT").append("\n");
return sb.toString();
}
Aggregations