use of io.fabric8.api.NameValidator 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.api.NameValidator in project fabric8 by jboss-fuse.
the class ChildAutoScaler method createContainers.
@Override
public void createContainers(AutoScaleRequest request) throws Exception {
int count = request.getDelta();
String profile = request.getProfile();
String version = request.getVersion();
FabricService fabricService = request.getFabricService();
CreateChildContainerOptions.Builder builder = null;
if (fabricService != null) {
builder = createAutoScaleOptions(request, fabricService);
}
if (builder != null) {
Set<String> ignoreContainerNames = new HashSet<>();
for (int i = 0; i < count; i++) {
final CreateChildContainerOptions.Builder configuredBuilder = builder.number(1).version(version).profiles(profile);
Container[] containers = fabricService.getContainers();
NameValidator nameValidator = Containers.createNameValidator(containers);
String name = Containers.createAutoScaleContainerName(containers, profile, containerProvider.getScheme(), nameValidator);
ignoreContainerNames.add(name);
CreateChildContainerOptions options = configuredBuilder.name(name).build();
LOG.info("Creating container name " + name + " version " + version + " profile " + profile + " " + count + " container(s)");
fabricService.createContainers(options);
}
} else {
LOG.warn("Could not create version " + version + " profile " + profile + " due to missing autoscale configuration");
}
}
use of io.fabric8.api.NameValidator in project fabric8 by jboss-fuse.
the class OpenShiftAutoScaler method createContainers.
@Override
public void createContainers(AutoScaleRequest request) throws Exception {
int count = request.getDelta();
String profile = request.getProfile();
String version = request.getVersion();
FabricService fabricService = request.getFabricService();
CreateOpenshiftContainerOptions.Builder builder = null;
if (fabricService != null) {
builder = createAutoScaleOptions(fabricService);
}
if (builder != null) {
// TODO this is actually generic to all providers! :)
for (int i = 0; i < count; i++) {
Container[] containers = fabricService.getContainers();
final CreateOpenshiftContainerOptions.Builder configuredBuilder = builder.number(1).version(version).profiles(profile);
NameValidator openShiftValidator = containerProvider.createNameValidator(configuredBuilder.build());
NameValidator fabricNameValidator = Containers.createNameValidator(fabricService.getContainers());
NameValidator nameValidator = Containers.joinNameValidators(openShiftValidator, fabricNameValidator);
String name = Containers.createAutoScaleContainerName(containers, profile, containerProvider.getScheme(), nameValidator);
CreateOpenshiftContainerOptions options = configuredBuilder.name(name).build();
LOG.info("Creating container name " + name + " version " + version + " profile " + profile + " " + count + " container(s)");
fabricService.createContainers(options);
}
} else {
LOG.warn("Could not create version " + version + " profile " + profile + " due to missing autoscale configuration");
}
}
use of io.fabric8.api.NameValidator in project fabric8 by jboss-fuse.
the class OpenshiftContainerProvider method createNameValidator.
/**
* Creates a name validator that checks there isn't an application of the given name already
*/
NameValidator createNameValidator(CreateOpenshiftContainerOptions options) {
IUser user = getOrCreateConnection(options).getUser();
final IDomain domain = getOrCreateDomain(user, options);
return new NameValidator() {
@Override
public boolean isValid(String name) {
IApplication application = domain.getApplicationByName(name);
return application == null;
}
};
}
use of io.fabric8.api.NameValidator in project fabric8 by jboss-fuse.
the class SshAutoScaler method createContainers.
@Override
public void createContainers(AutoScaleRequest request) throws Exception {
int count = request.getDelta();
String profile = request.getProfile();
String version = request.getVersion();
FabricService fabricService = request.getFabricService();
Container[] containers = fabricService.getContainers();
FabricRequirements requirements = request.getFabricRequirements();
List<? extends HostConfiguration> hostConfigurations = requirements.getSshHosts();
HostProfileCounter hostProfileCounter = new HostProfileCounter();
AutoScalers.createHostToProfileScaleMap(hostProfileCounter, hostConfigurations, containers);
for (int i = 0; i < count; i++) {
CreateSshContainerOptions.Builder builder = null;
NameValidator nameValidator = Containers.createNameValidator(containers);
String name = Containers.createAutoScaleContainerName(containers, profile, containerProvider.getScheme(), nameValidator);
if (fabricService != null) {
builder = createAutoScaleOptions(request, fabricService, hostProfileCounter);
}
if (builder != null) {
final CreateSshContainerOptions.Builder configuredBuilder = builder.number(1).version(version).profiles(profile);
CreateSshContainerOptions options = configuredBuilder.name(name).build();
LOG.info("Creating container name " + name + " version " + version + " profile " + profile + " " + count + " container(s)");
fabricService.createContainers(options);
}
}
}
Aggregations