use of io.fabric8.annotations.Configuration in project fabric8 by jboss-fuse.
the class FabricServiceImpl method createContainers.
@Override
public CreateContainerMetadata[] createContainers(CreateContainerOptions options, CreationStateListener listener) {
assertValid();
try {
final ContainerProvider provider = getProvider(options.getProviderType());
if (provider == null) {
throw new FabricException("Unable to find a container provider supporting '" + options.getProviderType() + "'");
}
if (!provider.isValidProvider()) {
throw new FabricException("The provider '" + options.getProviderType() + "' is not valid in current environment");
}
String originalName = options.getName();
if (originalName == null || originalName.length() == 0) {
throw new FabricException("A name must be specified when creating containers");
}
// Only allow containers with valid names to get created.
FabricValidations.validateContainerName(originalName);
if (listener == null) {
listener = new NullCreationStateListener();
}
validateProfileDependencies(options);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Map optionsMap = mapper.readValue(mapper.writeValueAsString(options), Map.class);
String versionId = options.getVersion() != null ? options.getVersion() : dataStore.get().getDefaultVersion();
Set<String> profileIds = options.getProfiles();
if (profileIds == null || profileIds.isEmpty()) {
profileIds = new LinkedHashSet<String>();
profileIds.add("default");
}
optionsMap.put("version", versionId);
optionsMap.put("profiles", profileIds);
optionsMap.put("number", 0);
// assign parent resolver if it's a child container, else assign global resolver policy
if (bootstrapConfig != null) {
String configuredGlobalResolver = bootstrapConfig.getGlobalResolver();
if (!Strings.isNullOrEmpty(configuredGlobalResolver)) {
optionsMap.put("globalResolver", configuredGlobalResolver);
if (optionsMap.get("resolver") == null) {
if (optionsMap.get("parent") == null) {
optionsMap.put("resolver", configuredGlobalResolver);
}
}
}
}
final List<CreateContainerMetadata> metadatas = new CopyOnWriteArrayList<CreateContainerMetadata>();
int orgNumber = options.getNumber();
int number = Math.max(orgNumber, 1);
if (orgNumber > 1) {
originalName = originalName + "1";
}
final CountDownLatch latch = new CountDownLatch(number);
Set<String> ignoreContainerNames = new HashSet<>();
Container[] containers = getContainers();
// check that there is no container with the given name
for (Container container : containers) {
if (container.getId().equals(options.getName())) {
throw new IllegalArgumentException("A container with name " + options.getName() + " already exists.");
}
}
for (int i = 1; i <= number; i++) {
NameValidator validator = Containers.createNameValidator(containers, ignoreContainerNames);
final String containerName = Containers.createUniqueContainerName(containers, originalName, validator);
ignoreContainerNames.add(containerName);
optionsMap.put("name", containerName);
// Check if datastore configuration has been specified and fallback to current container settings.
if (!hasValidDataStoreProperties(optionsMap)) {
optionsMap.put("dataStoreProperties", profileRegistry.get().getDataStoreProperties());
}
Class cl = options.getClass().getClassLoader().loadClass(options.getClass().getName() + "$Builder");
CreateContainerBasicOptions.Builder builder = (CreateContainerBasicOptions.Builder) mapper.readValue(mapper.writeValueAsString(optionsMap), cl);
// We always want to pass the obfuscated version of the password to the container provider.
builder = (CreateContainerBasicOptions.Builder) builder.zookeeperPassword(PasswordEncoder.encode(getZookeeperPassword()));
final CreateContainerOptions containerOptions = builder.build();
final CreationStateListener containerListener = listener;
final FabricService fabricService = this;
new Thread("Creating container " + containerName) {
public void run() {
try {
if (dataStore.get().hasContainer(containerName)) {
CreateContainerBasicMetadata metadata = new CreateContainerBasicMetadata();
metadata.setContainerName(containerName);
metadata.setCreateOptions(containerOptions);
metadata.setFailure(new IllegalArgumentException("A container with name " + containerName + " already exists."));
metadatas.add(metadata);
return;
}
dataStore.get().createContainerConfig(containerOptions);
CreateContainerMetadata metadata = provider.create(containerOptions, containerListener);
if (metadata.isSuccess()) {
Container parent = containerOptions.getParent() != null ? getContainer(containerOptions.getParent()) : null;
// TODO: We need to make sure that this entries are somehow added even to ensemble servers.
if (!containerOptions.isEnsembleServer()) {
dataStore.get().createContainerConfig(metadata);
}
ContainerImpl container = new ContainerImpl(parent, metadata.getContainerName(), FabricServiceImpl.this);
metadata.setContainer(container);
LOGGER.info("The container " + metadata.getContainerName() + " has been successfully created");
} else {
LOGGER.warn("The creation of the container " + metadata.getContainerName() + " has failed", metadata.getFailure());
dataStore.get().deleteContainer(fabricService, containerOptions.getName());
}
metadatas.add(metadata);
} catch (Throwable t) {
CreateContainerBasicMetadata metadata = new CreateContainerBasicMetadata();
metadata.setContainerName(containerName);
metadata.setCreateOptions(containerOptions);
metadata.setFailure(t);
metadatas.add(metadata);
dataStore.get().deleteContainer(fabricService, containerOptions.getName());
} finally {
latch.countDown();
}
}
}.start();
}
if (!latch.await(30, TimeUnit.MINUTES)) {
throw new FabricException("Timeout waiting for container creation");
}
return metadatas.toArray(new CreateContainerMetadata[metadatas.size()]);
} catch (Exception e) {
LOGGER.error("Failed to create containers " + e, e);
throw FabricException.launderThrowable(e);
}
}
use of io.fabric8.annotations.Configuration in project fabric8 by jboss-fuse.
the class ZooKeeperClusterBootstrapImpl method activate.
@Activate
void activate(BundleContext bundleContext, Map<String, ?> configuration) throws Exception {
this.bundleContext = bundleContext;
this.configurer.configure(configuration, this);
BootstrapConfiguration bootConfig = bootstrapConfiguration.get();
CreateEnsembleOptions options = bootConfig.getBootstrapOptions();
if (options.isEnsembleStart()) {
startBundles(options);
}
activateComponent();
}
use of io.fabric8.annotations.Configuration in project fabric8 by jboss-fuse.
the class ZooKeeperClusterBootstrapImpl method cleanInternal.
private BootstrapConfiguration cleanInternal(final BundleContext syscontext, final BootstrapConfiguration bootConfig, RuntimeProperties runtimeProps) throws TimeoutException {
LOGGER.debug("Begin clean fabric");
try {
Configuration zkClientCfg = null;
Configuration zkServerCfg = null;
Configuration[] configsSet = configAdmin.get().listConfigurations("(|(service.factoryPid=io.fabric8.zookeeper.server)(service.pid=io.fabric8.zookeeper))");
if (configsSet != null) {
for (Configuration cfg : configsSet) {
// let's explicitly delete client config first
if ("io.fabric8.zookeeper".equals(cfg.getPid())) {
zkClientCfg = cfg;
}
if ("io.fabric8.zookeeper.server".equals(cfg.getFactoryPid())) {
zkServerCfg = cfg;
}
}
}
File karafData = new File(data);
// Setup the listener for unregistration of {@link BootstrapConfiguration}
final CountDownLatch unregisterLatch = new CountDownLatch(1);
ServiceListener listener = new ServiceListener() {
@Override
public void serviceChanged(ServiceEvent event) {
if (event.getType() == ServiceEvent.UNREGISTERING) {
LOGGER.debug("Unregistering BootstrapConfiguration");
bootConfig.getComponentContext().getBundleContext().removeServiceListener(this);
unregisterLatch.countDown();
}
}
};
String filter = "(objectClass=" + BootstrapConfiguration.class.getName() + ")";
// FABRIC-1052: register listener using the same bundle context that is used for listeners related to SCR
bootConfig.getComponentContext().getBundleContext().addServiceListener(listener, filter);
CountDownLatch unregisterLatch2 = null;
if (syscontext.getServiceReference(CuratorComplete.class) != null) {
unregisterLatch2 = new CountDownLatch(1);
final CountDownLatch finalUnregisterLatch = unregisterLatch2;
listener = new ServiceListener() {
@Override
public void serviceChanged(ServiceEvent event) {
if (event.getType() == ServiceEvent.UNREGISTERING) {
LOGGER.debug("Unregistering CuratorComplete");
bootConfig.getComponentContext().getBundleContext().removeServiceListener(this);
finalUnregisterLatch.countDown();
}
}
};
bootConfig.getComponentContext().getBundleContext().addServiceListener(listener, "(objectClass=" + CuratorComplete.class.getName() + ")");
}
// Disable the BootstrapConfiguration component
// ENTESB-4827: disabling BootstrapConfiguration leads to deactivation of FabricService and ProfileUrlHandler
// and we have race condition if we're --cleaning after recently created fabric. previous fabric
// started FabricConfigAdminBridge which scheduled CM updates for tens of PIDs - among others,
// org.ops4j.pax.web, which leads to an attempt to reconfigure Jetty with "profile:jetty.xml"
// and if we disable ProfileUrlHandler we may loose Jetty instance
LOGGER.debug("Disable BootstrapConfiguration");
ComponentContext componentContext = bootConfig.getComponentContext();
componentContext.disableComponent(BootstrapConfiguration.COMPONENT_NAME);
if (!unregisterLatch.await(30, TimeUnit.SECONDS))
throw new TimeoutException("Timeout for unregistering BootstrapConfiguration service");
if (unregisterLatch2 != null && !unregisterLatch2.await(30, TimeUnit.SECONDS))
throw new TimeoutException("Timeout for unregistering CuratorComplete service");
// Do the cleanup
runtimeProps.clearRuntimeAttributes();
cleanConfigurations(syscontext, zkClientCfg, zkServerCfg);
cleanZookeeperDirectory(karafData);
cleanGitDirectory(karafData);
// Setup the registration listener for the new {@link BootstrapConfiguration}
final CountDownLatch registerLatch = new CountDownLatch(1);
final AtomicReference<ServiceReference<?>> sref = new AtomicReference<ServiceReference<?>>();
listener = new ServiceListener() {
@Override
public void serviceChanged(ServiceEvent event) {
if (event.getType() == ServiceEvent.REGISTERED) {
LOGGER.debug("Registered BootstrapConfiguration");
syscontext.removeServiceListener(this);
sref.set(event.getServiceReference());
registerLatch.countDown();
}
}
};
syscontext.addServiceListener(listener, "(objectClass=" + BootstrapConfiguration.class.getName() + ")");
// Enable the {@link BootstrapConfiguration} component and await the registration of the respective service
LOGGER.debug("Enable BootstrapConfiguration");
componentContext.enableComponent(BootstrapConfiguration.COMPONENT_NAME);
if (!registerLatch.await(30, TimeUnit.SECONDS))
throw new TimeoutException("Timeout for registering BootstrapConfiguration service");
return (BootstrapConfiguration) syscontext.getService(sref.get());
} catch (RuntimeException rte) {
throw rte;
} catch (TimeoutException toe) {
throw toe;
} catch (Exception ex) {
throw new FabricException("Unable to delete zookeeper configuration", ex);
} finally {
LOGGER.debug("End clean fabric");
}
}
use of io.fabric8.annotations.Configuration in project fabric8 by jboss-fuse.
the class ZooKeeperClusterBootstrapImpl method create.
@Override
public void create(CreateEnsembleOptions options) {
assertValid();
try {
// Wait for bootstrap to be complete
ServiceLocator.awaitService(BootstrapComplete.class);
LOGGER.info("Create fabric with: {}", options);
stopBundles();
BundleContext syscontext = bundleContext.getBundle(0).getBundleContext();
long bootstrapTimeout = options.getBootstrapTimeout();
RuntimeProperties runtimeProps = runtimeProperties.get();
BootstrapConfiguration bootConfig = bootstrapConfiguration.get();
if (options.isClean()) {
bootConfig = cleanInternal(syscontext, bootConfig, runtimeProps);
}
// before we start fabric, register CM listener that'll mark end of work of FabricConfigAdminBridge
final CountDownLatch fcabLatch = new CountDownLatch(1);
final ServiceRegistration<ConfigurationListener> registration = bundleContext.registerService(ConfigurationListener.class, new ConfigurationListener() {
@Override
public void configurationEvent(ConfigurationEvent event) {
if (event.getType() == ConfigurationEvent.CM_UPDATED && event.getPid() != null && event.getPid().equals(Constants.CONFIGADMIN_BRIDGE_PID)) {
fcabLatch.countDown();
}
}
}, null);
BootstrapCreateHandler createHandler = new BootstrapCreateHandler(syscontext, bootConfig, runtimeProps);
createHandler.bootstrapFabric(name, homeDir, options);
startBundles(options);
long startTime = System.currentTimeMillis();
ServiceLocator.awaitService(FabricComplete.class, bootstrapTimeout, TimeUnit.MILLISECONDS);
// FabricComplete is registered somewhere in the middle of registering CuratorFramework (SCR activates
// it when CuratorFramework is registered), but CuratorFramework leads to activation of >100 SCR
// components, so let's wait for new CuratorComplete service - it is registered after registration
// of CuratorFramework finishes
ServiceLocator.awaitService(CuratorComplete.class, bootstrapTimeout, TimeUnit.MILLISECONDS);
CuratorFramework curatorFramework = ServiceLocator.getService(CuratorFramework.class);
Map<String, String> dataStoreProperties = options.getDataStoreProperties();
if (ZooKeeperUtils.exists(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL.getPath()) == null)
ZooKeeperUtils.create(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL.getPath());
if (dataStoreProperties != null) {
String remoteUrl = dataStoreProperties.get(Constants.GIT_REMOTE_URL);
if (remoteUrl != null) {
ZooKeeperUtils.create(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL_URL.getPath());
ZooKeeperUtils.add(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL_URL.getPath(), remoteUrl);
}
String remoteUser = dataStoreProperties.get("gitRemoteUser");
if (remoteUser != null) {
ZooKeeperUtils.create(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL_USER.getPath());
ZooKeeperUtils.add(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL_USER.getPath(), remoteUser);
}
String remotePassword = dataStoreProperties.get("gitRemotePassword");
if (remotePassword != null) {
ZooKeeperUtils.create(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL_PASSWORD.getPath());
ZooKeeperUtils.add(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL_PASSWORD.getPath(), remotePassword);
}
}
// HttpService is registered differently. not as SCR component activation, but after
// FabricConfigAdminBridge updates (or doesn't touch) org.ops4j.pax.web CM configuration
// however in fabric, this PID contains URL to jetty configuration in the form "profile:jetty.xml"
// so we have to have FabricService and ProfileUrlHandler active
// some ARQ (single container instance) tests failed because tests ended without http service running
// of course we have to think if all fabric instances need http service
ServiceLocator.awaitService("org.osgi.service.http.HttpService", bootstrapTimeout, TimeUnit.MILLISECONDS);
// and last wait - too much synchronization never hurts
fcabLatch.await(bootstrapTimeout, TimeUnit.MILLISECONDS);
registration.unregister();
long timeDiff = System.currentTimeMillis() - startTime;
createHandler.waitForContainerAlive(name, syscontext, bootstrapTimeout);
if (options.isWaitForProvision() && options.isAgentEnabled()) {
long currentTime = System.currentTimeMillis();
createHandler.waitForSuccessfulDeploymentOf(name, syscontext, bootstrapTimeout - (currentTime - startTime));
}
} catch (RuntimeException rte) {
throw rte;
} catch (Exception ex) {
throw new FabricException("Unable to create zookeeper server configuration", ex);
}
}
use of io.fabric8.annotations.Configuration in project fabric8 by jboss-fuse.
the class MQManager method createOrUpdateProfile.
/**
* Creates or updates the broker profile for the given DTO and updates the requirements so that the
* minimum number of instances of the profile is updated
*/
public static Profile createOrUpdateProfile(MQBrokerConfigDTO dto, FabricService fabricService, RuntimeProperties runtimeProperties) throws IOException {
FabricRequirements requirements = fabricService.getRequirements();
MQService mqService = createMQService(fabricService, runtimeProperties);
Map<String, String> configuration = new HashMap<String, String>();
List<String> properties = dto.getProperties();
String version = dto.version();
boolean changeInCurrentVersion = requirements.getVersion().equals(dto.getVersion());
if (properties != null) {
for (String entry : properties) {
String[] parts = entry.split("=", 2);
if (parts.length == 2) {
configuration.put(parts[0], parts[1]);
} else {
configuration.put(parts[0], "");
}
}
}
String data = dto.getData();
String profileName = dto.profile();
try {
FabricValidations.validateProfileName(profileName);
} catch (IllegalArgumentException e) {
// we do not want exception in the server log, so print the error message to the console
System.out.println(e.getMessage());
return null;
}
String brokerName = dto.getBrokerName();
if (data == null) {
// lets use a relative path so we work on any karaf container
data = "${runtime.data}" + brokerName;
}
configuration.put(DATA, data);
for (Map.Entry<String, String> port : dto.getPorts().entrySet()) {
configuration.put(port.getKey() + "-port", port.getValue());
}
BrokerKind kind = dto.kind();
configuration.put(KIND, kind.toString());
String config = dto.getConfigUrl();
if (config != null) {
configuration.put(CONFIG_URL, mqService.getConfig(version, config));
}
String group = dto.getGroup();
if (group != null) {
configuration.put(GROUP, group);
}
Maps.setStringValues(configuration, NETWORKS, dto.getNetworks());
String networksUserName = dto.getNetworksUserName();
if (networksUserName != null) {
configuration.put(NETWORK_USER_NAME, networksUserName);
}
String networksPassword = dto.getNetworksPassword();
if (networksPassword != null) {
configuration.put(NETWORK_PASSWORD, networksPassword);
}
String parentProfile = dto.getParentProfile();
if (parentProfile != null) {
configuration.put(PARENT, parentProfile);
}
Boolean ssl = dto.getSsl();
if (ssl != null) {
configuration.put(SSL, ssl.toString());
}
Integer replicas = dto.getReplicas();
if (replicas != null) {
configuration.put(REPLICAS, replicas.toString());
}
Integer minInstances = dto.getMinimumInstances();
if (minInstances != null) {
configuration.put(MINIMUM_INSTANCES, minInstances.toString());
}
Profile profile = mqService.createOrUpdateMQProfile(version, profileName, brokerName, configuration, dto.kind().equals(BrokerKind.Replicated));
String profileId = profile.getId();
ProfileRequirements profileRequirement = requirements.getOrCreateProfileRequirement(profileId);
Integer minimumInstances = profileRequirement.getMinimumInstances();
// lets reload the DTO as we may have inherited some values from the parent profile
List<MQBrokerConfigDTO> list = createConfigDTOs(mqService, profile);
// lets assume 2 required instances for master/slave unless folks use
// N+1 or replicated
int requiredInstances = 2;
if (list.size() == 1) {
MQBrokerConfigDTO loadedDTO = list.get(0);
requiredInstances = loadedDTO.requiredInstances();
} else {
// assume N+1 broker as there's more than one broker in the profile; so lets set the required size to N+1
requiredInstances = list.size() + 1;
}
if (changeInCurrentVersion && (minimumInstances == null || minimumInstances.intValue() < requiredInstances)) {
profileRequirement.setMinimumInstances(requiredInstances);
fabricService.setRequirements(requirements);
}
String clientProfile = dto.clientProfile();
if (Strings.isNotBlank(clientProfile)) {
String clientParentProfile = dto.getClientParentProfile();
if (Strings.isNullOrBlank(clientParentProfile)) {
clientParentProfile = "mq-client-base";
}
mqService.createOrUpdateMQClientProfile(version, clientProfile, group, clientParentProfile);
}
return profile;
}
Aggregations