Search in sources :

Example 26 with ServiceHub

use of io.fabric8.maven.docker.service.ServiceHub in project docker-maven-plugin by fabric8io.

the class StartContainerExecutorTest method testStartContainers.

@Test
public void testStartContainers(@Mocked ServiceHub hub, @Mocked DockerAccess dockerAccess, @Mocked ContainerTracker containerTracker, @Mocked Logger log) throws IOException, ExecException {
    // Given
    new Expectations() {

        {
            dockerAccess.createContainer((ContainerCreateConfig) any, anyString);
            result = "container-name";
            dockerAccess.getContainer(anyString);
            result = new ContainerDetails(JsonFactory.newJsonObject("{\"NetworkSettings\":{\"IPAddress\":\"192.168.1.2\"}}"));
            QueryService queryService = new QueryService(dockerAccess);
            hub.getQueryService();
            result = queryService;
            hub.getRunService();
            result = new RunService(dockerAccess, queryService, containerTracker, new LogOutputSpecFactory(true, true, null), log);
        }
    };
    Properties projectProps = new Properties();
    StartContainerExecutor startContainerExecutor = new StartContainerExecutor.Builder().serviceHub(hub).projectProperties(projectProps).portMapping(new PortMapping(Collections.emptyList(), projectProps)).gavLabel(new GavLabel("io.fabric8:test:0.1.0")).basedir(new File("/tmp/foo")).containerNamePattern("test-").buildTimestamp(new Date()).exposeContainerProps("docker.container").imageConfig(new ImageConfiguration.Builder().name("name").alias("alias").runConfig(new RunImageConfiguration.Builder().build()).build()).build();
    // When
    String containerId = startContainerExecutor.startContainer();
    // Then
    assertEquals("container-name", containerId);
    assertEquals("container-name", projectProps.getProperty("docker.container.alias.id"));
    assertEquals("192.168.1.2", projectProps.getProperty("docker.container.alias.ip"));
}
Also used : Expectations(mockit.Expectations) RunService(io.fabric8.maven.docker.service.RunService) LogOutputSpecFactory(io.fabric8.maven.docker.log.LogOutputSpecFactory) Properties(java.util.Properties) Date(java.util.Date) GavLabel(io.fabric8.maven.docker.util.GavLabel) QueryService(io.fabric8.maven.docker.service.QueryService) PortMapping(io.fabric8.maven.docker.access.PortMapping) File(java.io.File) RunImageConfiguration(io.fabric8.maven.docker.config.RunImageConfiguration) ContainerDetails(io.fabric8.maven.docker.model.ContainerDetails) Test(org.junit.Test)

Example 27 with ServiceHub

use of io.fabric8.maven.docker.service.ServiceHub in project fabric8-maven-plugin by fabric8io.

the class DockerImageWatcher method watch.

@Override
public void watch(List<ImageConfiguration> configs, final Set<HasMetadata> resources, PlatformMode mode) {
    BuildService.BuildContext buildContext = getContext().getBuildContext();
    WatchService.WatchContext watchContext = getContext().getWatchContext();
    // add a image customizer
    watchContext = new WatchService.WatchContext.Builder(watchContext).imageCustomizer(imageConfiguration -> buildImage(imageConfiguration)).containerRestarter(imageWatcher -> restartContainer(imageWatcher, resources)).build();
    ServiceHub hub = getContext().getServiceHub();
    try {
        hub.getWatchService().watch(watchContext, buildContext, configs);
    } catch (Exception ex) {
        throw new RuntimeException("Error while watching", ex);
    }
}
Also used : ReplicaSet(io.fabric8.kubernetes.api.model.apps.ReplicaSet) Task(io.fabric8.maven.docker.util.Task) Container(io.fabric8.kubernetes.api.model.Container) ReplicationControllerSpec(io.fabric8.kubernetes.api.model.ReplicationControllerSpec) Date(java.util.Date) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) OpenshiftHelper(io.fabric8.maven.core.util.kubernetes.OpenshiftHelper) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) ReplicaSetSpec(io.fabric8.kubernetes.api.model.apps.ReplicaSetSpec) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) WatcherContext(io.fabric8.maven.watcher.api.WatcherContext) DeploymentSpec(io.fabric8.kubernetes.api.model.apps.DeploymentSpec) PlatformMode(io.fabric8.maven.core.config.PlatformMode) ImageNameFormatter(io.fabric8.maven.docker.util.ImageNameFormatter) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) BaseWatcher(io.fabric8.maven.watcher.api.BaseWatcher) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) Set(java.util.Set) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) WatchService(io.fabric8.maven.docker.service.WatchService) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ServiceHub(io.fabric8.maven.docker.service.ServiceHub) List(java.util.List) DeploymentConfigSpec(io.fabric8.openshift.api.model.DeploymentConfigSpec) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) BuildService(io.fabric8.maven.docker.service.BuildService) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) KubernetesHelper(io.fabric8.maven.core.util.kubernetes.KubernetesHelper) ClusterAccess(io.fabric8.maven.core.access.ClusterAccess) KubernetesResourceUtil(io.fabric8.maven.core.util.kubernetes.KubernetesResourceUtil) ServiceHub(io.fabric8.maven.docker.service.ServiceHub) BuildService(io.fabric8.maven.docker.service.BuildService) WatchService(io.fabric8.maven.docker.service.WatchService) DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 28 with ServiceHub

use of io.fabric8.maven.docker.service.ServiceHub in project fabric8-maven-plugin by fabric8io.

the class WatchMojo method executeInternal.

@Override
protected synchronized void executeInternal(ServiceHub hub) throws MojoExecutionException {
    if (hub != null) {
        this.hub = hub;
    }
    URL masterUrl = kubernetes.getMasterUrl();
    KubernetesResourceUtil.validateKubernetesMasterUrl(masterUrl);
    File manifest;
    boolean isOpenshift = OpenshiftHelper.isOpenShift(kubernetes);
    if (isOpenshift) {
        manifest = openshiftManifest;
    } else {
        manifest = kubernetesManifest;
    }
    try {
        Set<HasMetadata> resources = KubernetesResourceUtil.loadResources(manifest);
        WatcherContext context = getWatcherContext();
        WatcherManager.watch(getResolvedImages(), resources, context);
    } catch (KubernetesClientException ex) {
        KubernetesResourceUtil.handleKubernetesClientException(ex, this.log);
    } catch (Exception ex) {
        throw new MojoExecutionException("An error has occurred while while trying to watch the resources", ex);
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) WatcherContext(io.fabric8.maven.watcher.api.WatcherContext) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) File(java.io.File) URL(java.net.URL) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Aggregations

ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)12 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)9 File (java.io.File)8 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)5 VolumeConfiguration (io.fabric8.maven.docker.config.VolumeConfiguration)5 QueryService (io.fabric8.maven.docker.service.QueryService)5 RunService (io.fabric8.maven.docker.service.RunService)5 IOException (java.io.IOException)5 RunImageConfiguration (io.fabric8.maven.docker.config.RunImageConfiguration)4 LogDispatcher (io.fabric8.maven.docker.log.LogDispatcher)4 Test (org.junit.Test)4 PortMapping (io.fabric8.maven.docker.access.PortMapping)3 RunVolumeConfiguration (io.fabric8.maven.docker.config.RunVolumeConfiguration)3 LogOutputSpecFactory (io.fabric8.maven.docker.log.LogOutputSpecFactory)3 BuildService (io.fabric8.maven.docker.service.BuildService)3 JibBuildService (io.fabric8.maven.docker.service.JibBuildService)3 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)2 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)2 Fabric8ServiceHub (io.fabric8.maven.core.service.Fabric8ServiceHub)2 DockerAccess (io.fabric8.maven.docker.access.DockerAccess)2