Search in sources :

Example 1 with ProvisioningException

use of org.guvnor.ala.exceptions.ProvisioningException in project kie-wb-common by kiegroup.

the class DockerRuntimeExecExecutor method create.

private Optional<DockerRuntime> create(final DockerRuntimeConfig runtimeConfig) throws ProvisioningException {
    if (runtimeConfig.isPull()) {
        try {
            LOG.info("Pulling Docker Image: " + runtimeConfig.getImage());
            docker.getDockerClient(runtimeConfig.getProviderId()).pull(runtimeConfig.getImage());
        } catch (DockerException | InterruptedException ex) {
            LOG.error(ex.getMessage(), ex);
            throw new ProvisioningException("Error Pulling Docker Image: " + runtimeConfig.getImage() + "with error: " + ex.getMessage());
        }
    }
    final String[] ports = { runtimeConfig.getPort() };
    final Map<String, List<PortBinding>> portBindings = new HashMap<>();
    final Optional<DockerProvider> _dockerProvider = runtimeRegistry.getProvider(runtimeConfig.getProviderId(), DockerProvider.class);
    if (!_dockerProvider.isPresent()) {
        return Optional.empty();
    }
    final DockerProvider dockerProvider = _dockerProvider.get();
    final List<PortBinding> randomPort = new ArrayList<>();
    final PortBinding randomPortBinding = PortBinding.randomPort(dockerProvider.getConfig().getHostIp());
    randomPort.add(randomPortBinding);
    portBindings.put(runtimeConfig.getPort(), randomPort);
    final HostConfig hostConfig = HostConfig.builder().portBindings(portBindings).build();
    final ContainerConfig containerConfig = ContainerConfig.builder().hostConfig(hostConfig).image(runtimeConfig.getImage()).exposedPorts(ports).build();
    final ContainerCreation creation;
    try {
        creation = docker.getDockerClient(runtimeConfig.getProviderId()).createContainer(containerConfig);
        docker.getDockerClient(runtimeConfig.getProviderId()).startContainer(creation.id());
    } catch (DockerException | InterruptedException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new ProvisioningException("Error Creating Docker Container with image: " + runtimeConfig.getImage() + "with error: " + ex.getMessage(), ex);
    }
    final String id = creation.id();
    String shortId = id.substring(0, 12);
    String host = "";
    try {
        docker.getDockerClient(runtimeConfig.getProviderId()).inspectContainer(id);
        host = docker.getDockerClient(runtimeConfig.getProviderId()).getHost();
    } catch (DockerException | InterruptedException ex) {
        throw new ProvisioningException("Error Getting Docker Container info: " + id + "with error: " + ex.getMessage(), ex);
    }
    DockerRuntimeEndpoint dockerRuntimeEndpoint = new DockerRuntimeEndpoint();
    dockerRuntimeEndpoint.setHost(host);
    dockerRuntimeEndpoint.setPort(Integer.valueOf(runtimeConfig.getPort()));
    dockerRuntimeEndpoint.setContext("");
    return Optional.of(new DockerRuntime(shortId, buildRuntimeName(runtimeConfig, shortId), runtimeConfig, dockerProvider, dockerRuntimeEndpoint, new DockerRuntimeInfo(), new DockerRuntimeState(RUNNING, new Date().toString())));
}
Also used : DockerException(com.spotify.docker.client.DockerException) HashMap(java.util.HashMap) DockerProvider(org.guvnor.ala.docker.model.DockerProvider) DockerRuntimeEndpoint(org.guvnor.ala.docker.model.DockerRuntimeEndpoint) ArrayList(java.util.ArrayList) Date(java.util.Date) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) DockerRuntime(org.guvnor.ala.docker.model.DockerRuntime) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) PortBinding(com.spotify.docker.client.messages.PortBinding) ProvisioningException(org.guvnor.ala.exceptions.ProvisioningException) HostConfig(com.spotify.docker.client.messages.HostConfig) DockerRuntimeState(org.guvnor.ala.docker.model.DockerRuntimeState) ArrayList(java.util.ArrayList) List(java.util.List) DockerRuntimeInfo(org.guvnor.ala.docker.model.DockerRuntimeInfo)

Example 2 with ProvisioningException

use of org.guvnor.ala.exceptions.ProvisioningException in project kie-wb-common by kiegroup.

the class OpenShiftRuntimeExecExecutor method create.

private Optional<OpenShiftRuntime> create(final OpenShiftRuntimeConfig runtimeConfig) throws ProvisioningException {
    final Optional<OpenShiftProvider> _openshiftProvider = runtimeRegistry.getProvider(runtimeConfig.getProviderId(), OpenShiftProvider.class);
    if (!_openshiftProvider.isPresent()) {
        return Optional.empty();
    }
    OpenShiftProvider openshiftProvider = _openshiftProvider.get();
    OpenShiftClient openshiftClient = openshift.getOpenShiftClient(openshiftProvider);
    LOG.info("Creating runtime...");
    OpenShiftRuntimeState runtimeState;
    try {
        runtimeState = openshiftClient.create(runtimeConfig);
    } catch (OpenShiftClientException ex) {
        throw new ProvisioningException(ex.getMessage(), ex);
    }
    final String id = runtimeConfig.getRuntimeId().toString();
    LOG.info("Created runtime: " + id);
    OpenShiftRuntimeEndpoint endpoint = openshiftClient.getRuntimeEndpoint(id);
    return Optional.of(new OpenShiftRuntime(id, buildRuntimeName(runtimeConfig, id), runtimeConfig, openshiftProvider, endpoint, new OpenShiftRuntimeInfo(runtimeConfig), runtimeState));
}
Also used : OpenShiftRuntime(org.guvnor.ala.openshift.model.OpenShiftRuntime) OpenShiftClient(org.guvnor.ala.openshift.access.OpenShiftClient) ProvisioningException(org.guvnor.ala.exceptions.ProvisioningException) OpenShiftProvider(org.guvnor.ala.openshift.model.OpenShiftProvider) OpenShiftRuntimeState(org.guvnor.ala.openshift.model.OpenShiftRuntimeState) OpenShiftClientException(org.guvnor.ala.openshift.access.exceptions.OpenShiftClientException) OpenShiftRuntimeInfo(org.guvnor.ala.openshift.model.OpenShiftRuntimeInfo) OpenShiftRuntimeEndpoint(org.guvnor.ala.openshift.model.OpenShiftRuntimeEndpoint)

Example 3 with ProvisioningException

use of org.guvnor.ala.exceptions.ProvisioningException in project kie-wb-common by kiegroup.

the class WildflyProviderConfigExecutor method apply.

@Override
public Optional<WildflyProvider> apply(final WildflyProviderConfig wildflyProviderConfig) {
    if (wildflyProviderConfig.getName() == null || wildflyProviderConfig.getName().isEmpty()) {
        throw new ProvisioningException("No name was provided for the WildflyProviderConfig.getName() " + "configuration parameter. You might probably have to properly set " + "the pipeline input parameter: " + ProviderConfig.PROVIDER_NAME);
    }
    Provider provider = runtimeRegistry.getProvider(wildflyProviderConfig.getName());
    WildflyProvider wildflyProvider;
    if (provider != null) {
        if (!(provider instanceof WildflyProvider)) {
            throw new ProvisioningException("The provider: " + wildflyProviderConfig.getName() + " must be an instance of " + WildflyProviderConfig.class + " but is: " + provider.getClass());
        } else {
            wildflyProvider = (WildflyProvider) provider;
        }
    } else {
        wildflyProvider = new WildflyProviderImpl(new WildflyProviderConfigImpl(wildflyProviderConfig.getName(), wildflyProviderConfig.getHost(), wildflyProviderConfig.getPort(), wildflyProviderConfig.getManagementPort(), wildflyProviderConfig.getUser(), wildflyProviderConfig.getPassword()));
        runtimeRegistry.registerProvider(wildflyProvider);
    }
    return Optional.of(wildflyProvider);
}
Also used : WildflyProviderConfig(org.guvnor.ala.wildfly.config.WildflyProviderConfig) WildflyProviderImpl(org.guvnor.ala.wildfly.model.WildflyProviderImpl) ProvisioningException(org.guvnor.ala.exceptions.ProvisioningException) WildflyProviderConfigImpl(org.guvnor.ala.wildfly.config.impl.WildflyProviderConfigImpl) Provider(org.guvnor.ala.runtime.providers.Provider) WildflyProvider(org.guvnor.ala.wildfly.model.WildflyProvider) WildflyProvider(org.guvnor.ala.wildfly.model.WildflyProvider)

Example 4 with ProvisioningException

use of org.guvnor.ala.exceptions.ProvisioningException in project kie-wb-common by kiegroup.

the class WildflyRuntimeExecExecutor method destroy.

@Override
public void destroy(final RuntimeId runtimeId) {
    final Optional<WildflyProvider> _wildflyProvider = runtimeRegistry.getProvider(runtimeId.getProviderId(), WildflyProvider.class);
    WildflyProvider wildflyProvider = _wildflyProvider.get();
    int result = wildfly.getWildflyClient(wildflyProvider).undeploy(runtimeId.getId());
    if (result != 200) {
        throw new ProvisioningException("UnDeployment to Wildfly Failed with error code: " + result);
    }
    runtimeRegistry.deregisterRuntime(runtimeId);
}
Also used : ProvisioningException(org.guvnor.ala.exceptions.ProvisioningException) WildflyRuntimeEndpoint(org.guvnor.ala.wildfly.model.WildflyRuntimeEndpoint) WildflyProvider(org.guvnor.ala.wildfly.model.WildflyProvider)

Example 5 with ProvisioningException

use of org.guvnor.ala.exceptions.ProvisioningException in project kie-wb-common by kiegroup.

the class WildflyRuntimeExecExecutor method create.

private Optional<WildflyRuntime> create(final WildflyRuntimeConfiguration runtimeConfig) throws ProvisioningException {
    String warPath = runtimeConfig.getWarPath();
    final Optional<WildflyProvider> _wildflyProvider = runtimeRegistry.getProvider(runtimeConfig.getProviderId(), WildflyProvider.class);
    if (!_wildflyProvider.isPresent()) {
        throw new ProvisioningException("No Wildfly provider was found for providerId: " + runtimeConfig.getProviderId());
    }
    WildflyProvider wildflyProvider = _wildflyProvider.get();
    File file = new File(warPath);
    final String id = file.getName();
    WildflyAppState appState = wildfly.getWildflyClient(wildflyProvider).getAppState(id);
    if (UNKNOWN.equals(appState.getState())) {
        int result = wildfly.getWildflyClient(wildflyProvider).deploy(file);
        if (result != 200) {
            throw new ProvisioningException("Deployment to Wildfly Failed with error code: " + result);
        }
    } else if ((RUNNING.equals(appState.getState()) || STOPPED.equals(appState.getState())) && (isNullOrEmpty(runtimeConfig.getRedeployStrategy()) || "auto".equals(runtimeConfig.getRedeployStrategy()))) {
        wildfly.getWildflyClient(wildflyProvider).undeploy(id);
        int result = wildfly.getWildflyClient(wildflyProvider).deploy(file);
        if (result != 200) {
            throw new ProvisioningException("Deployment to Wildfly Failed with error code: " + result);
        }
    } else {
        throw new ProvisioningException("A runtime with the given identifier: " + id + " is already deployed");
    }
    String appContext = id.substring(0, id.lastIndexOf(".war"));
    WildflyRuntimeEndpoint endpoint = new WildflyRuntimeEndpoint();
    endpoint.setHost(wildfly.getWildflyClient(wildflyProvider).getHost());
    endpoint.setPort(wildfly.getWildflyClient(wildflyProvider).getPort());
    endpoint.setContext(appContext);
    return Optional.of(new WildflyRuntime(id, buildRuntimeName(runtimeConfig, id), runtimeConfig, wildflyProvider, endpoint, new WildflyRuntimeInfo(), new WildflyRuntimeState(RUNNING, new Date().toString())));
}
Also used : WildflyRuntimeEndpoint(org.guvnor.ala.wildfly.model.WildflyRuntimeEndpoint) WildflyRuntime(org.guvnor.ala.wildfly.model.WildflyRuntime) WildflyRuntimeInfo(org.guvnor.ala.wildfly.model.WildflyRuntimeInfo) ProvisioningException(org.guvnor.ala.exceptions.ProvisioningException) WildflyRuntimeState(org.guvnor.ala.wildfly.model.WildflyRuntimeState) File(java.io.File) WildflyAppState(org.guvnor.ala.wildfly.access.WildflyAppState) WildflyRuntimeEndpoint(org.guvnor.ala.wildfly.model.WildflyRuntimeEndpoint) Date(java.util.Date) WildflyProvider(org.guvnor.ala.wildfly.model.WildflyProvider)

Aggregations

ProvisioningException (org.guvnor.ala.exceptions.ProvisioningException)6 WildflyProvider (org.guvnor.ala.wildfly.model.WildflyProvider)3 Date (java.util.Date)2 OpenShiftProvider (org.guvnor.ala.openshift.model.OpenShiftProvider)2 Provider (org.guvnor.ala.runtime.providers.Provider)2 WildflyRuntimeEndpoint (org.guvnor.ala.wildfly.model.WildflyRuntimeEndpoint)2 DockerException (com.spotify.docker.client.DockerException)1 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)1 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)1 HostConfig (com.spotify.docker.client.messages.HostConfig)1 PortBinding (com.spotify.docker.client.messages.PortBinding)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 DockerProvider (org.guvnor.ala.docker.model.DockerProvider)1 DockerRuntime (org.guvnor.ala.docker.model.DockerRuntime)1 DockerRuntimeEndpoint (org.guvnor.ala.docker.model.DockerRuntimeEndpoint)1 DockerRuntimeInfo (org.guvnor.ala.docker.model.DockerRuntimeInfo)1 DockerRuntimeState (org.guvnor.ala.docker.model.DockerRuntimeState)1