Search in sources :

Example 1 with State

use of io.fabric8.agent.service.State in project che by eclipse.

the class OpenShiftConnector method createContainer.

/**
     * @param createContainerParams
     * @return
     * @throws IOException
     */
@Override
public ContainerCreated createContainer(CreateContainerParams createContainerParams) throws IOException {
    String containerName = KubernetesStringUtils.convertToContainerName(createContainerParams.getContainerName());
    String workspaceID = getCheWorkspaceId(createContainerParams);
    // Generate workspaceID if CHE_WORKSPACE_ID env var does not exist
    workspaceID = workspaceID.isEmpty() ? KubernetesStringUtils.generateWorkspaceID() : workspaceID;
    // imageForDocker is the docker version of the image repository. It's needed for other
    // OpenShiftConnector API methods, but is not acceptable as an OpenShift name
    String imageForDocker = createContainerParams.getContainerConfig().getImage();
    // imageStreamTagName is imageForDocker converted into a form that can be used
    // in OpenShift
    String imageStreamTagName = KubernetesStringUtils.convertPullSpecToTagName(imageForDocker);
    // imageStreamTagName is not enough to fill out a pull spec; it is only the tag, so we
    // have to get the ImageStreamTag from the tag, and then get the full ImageStreamTag name
    // from that tag. This works because the tags used in Che are unique.
    ImageStreamTag imageStreamTag = getImageStreamTagFromRepo(imageStreamTagName);
    String imageStreamTagPullSpec = imageStreamTag.getMetadata().getName();
    // Next we need to get the address of the registry where the ImageStreamTag is stored
    String imageStreamName = KubernetesStringUtils.getImageStreamNameFromPullSpec(imageStreamTagPullSpec);
    ImageStream imageStream = openShiftClient.imageStreams().inNamespace(openShiftCheProjectName).withName(imageStreamName).get();
    if (imageStream == null) {
        throw new OpenShiftException("ImageStream not found");
    }
    String registryAddress = imageStream.getStatus().getDockerImageRepository().split("/")[0];
    // The above needs to be combined to form a pull spec that will work when defining a container.
    String dockerPullSpec = String.format("%s/%s/%s", registryAddress, openShiftCheProjectName, imageStreamTagPullSpec);
    Set<String> containerExposedPorts = createContainerParams.getContainerConfig().getExposedPorts().keySet();
    Set<String> imageExposedPorts = inspectImage(InspectImageParams.create(imageForDocker)).getConfig().getExposedPorts().keySet();
    Set<String> exposedPorts = getExposedPorts(containerExposedPorts, imageExposedPorts);
    boolean runContainerAsRoot = runContainerAsRoot(imageForDocker);
    String[] envVariables = createContainerParams.getContainerConfig().getEnv();
    String[] volumes = createContainerParams.getContainerConfig().getHostConfig().getBinds();
    Map<String, String> additionalLabels = createContainerParams.getContainerConfig().getLabels();
    String containerID;
    try {
        createOpenShiftService(workspaceID, exposedPorts, additionalLabels);
        String deploymentName = createOpenShiftDeployment(workspaceID, dockerPullSpec, containerName, exposedPorts, envVariables, volumes, runContainerAsRoot);
        containerID = waitAndRetrieveContainerID(deploymentName);
        if (containerID == null) {
            throw new OpenShiftException("Failed to get the ID of the container running in the OpenShift pod");
        }
    } catch (IOException e) {
        // Make sure we clean up deployment and service in case of an error -- otherwise Che can end up
        // in an inconsistent state.
        LOG.info("Error while creating Pod, removing deployment");
        String deploymentName = CHE_OPENSHIFT_RESOURCES_PREFIX + workspaceID;
        cleanUpWorkspaceResources(deploymentName);
        openShiftClient.resource(imageStreamTag).delete();
        throw e;
    }
    return new ContainerCreated(containerID, null);
}
Also used : ContainerCreated(org.eclipse.che.plugin.docker.client.json.ContainerCreated) OpenShiftException(org.eclipse.che.plugin.openshift.client.exception.OpenShiftException) ImageStreamTag(io.fabric8.openshift.api.model.ImageStreamTag) ImageStream(io.fabric8.openshift.api.model.ImageStream) IOException(java.io.IOException)

Example 2 with State

use of io.fabric8.agent.service.State in project fabric8 by jboss-fuse.

the class MavenProxySnapshotResolutionTest method snapshotIsAvailableInDefaultRepositoryActingAsRemote.

@Test
public void snapshotIsAvailableInDefaultRepositoryActingAsRemote() throws IOException, InvalidMavenArtifactRequest {
    File differentLocalRepository = initFileRepository("dlr");
    File defaultRepository = initFileRepository("dr");
    MavenResolver resolver = new ResolverBuilder().withRemoteRepositories(Collections.<File>emptyList()).withUpdatePolicy(RepositoryPolicy.UPDATE_POLICY_NEVER).withDefaultRepositories(Collections.singletonList(defaultRepository)).build();
    MavenDownloadProxyServlet servlet = new MavenDownloadProxyServlet(resolver, runtime, null, 1, 0);
    servlet.start();
    mvnDeploy(differentLocalRepository, defaultRepository, "io.fabric8.test", "universalis-api", "0.1.0-SNAPSHOT", at("10:00"), "a");
    // Here's expected state of repository where SNAPSHOT was `mvn deploy`ed
    assertFalse(new File(defaultRepository, "io/fabric8/test/universalis-api/0.1.0-SNAPSHOT/maven-metadata-local.xml").isFile());
    assertTrue(new File(defaultRepository, "io/fabric8/test/universalis-api/0.1.0-SNAPSHOT/maven-metadata.xml").isFile());
    File file = servlet.download("io/fabric8/test/universalis-api/0.1.0-SNAPSHOT/maven-metadata.xml");
    Metadata metadata = readMetadata(file);
    boolean checked = false;
    assertThat(metadata.getVersioning().getSnapshot().isLocalCopy(), is(false));
    for (SnapshotVersion snapshotVersion : metadata.getVersioning().getSnapshotVersions()) {
        if ("jar".equals(snapshotVersion.getExtension())) {
            assertThat(snapshotVersion.getVersion(), is("0.1.0-20170101.100000-1"));
            checked = true;
        }
    }
    assertTrue("We should find snapshot metadata", checked);
    // download artifact using version from metadata
    file = servlet.download("io/fabric8/test/universalis-api/0.1.0-20170101.100000-1/universalis-api-0.1.0-20170101.100000-1.jar");
    assertThat(FileUtils.readFileToString(file), equalTo("a"));
    mvnDeploy(differentLocalRepository, defaultRepository, "io.fabric8.test", "universalis-api", "0.1.0-SNAPSHOT", at("11:00"), "b");
    file = servlet.download("io/fabric8/test/universalis-api/0.1.0-SNAPSHOT/maven-metadata.xml");
    metadata = readMetadata(file);
    assertThat("No policy should prevent us from seeing newer snapshot from defaultRepository", metadata.getVersioning().getSnapshotVersions().get(0).getVersion(), is("0.1.0-20170101.110000-2"));
}
Also used : SnapshotVersion(org.apache.maven.artifact.repository.metadata.SnapshotVersion) MavenResolver(io.fabric8.maven.MavenResolver) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) File(java.io.File) Test(org.junit.Test)

Example 3 with State

use of io.fabric8.agent.service.State in project fabric8 by jboss-fuse.

the class MavenProxySnapshotResolutionTest method snapshotIsAvailableInDefaultRepository.

@Test
public void snapshotIsAvailableInDefaultRepository() throws IOException, InvalidMavenArtifactRequest {
    File defaultRepository = initFileRepository("dr");
    MavenResolver resolver = new ResolverBuilder().withRemoteRepositories(Collections.<File>emptyList()).withUpdatePolicy(RepositoryPolicy.UPDATE_POLICY_NEVER).withDefaultRepositories(Collections.singletonList(defaultRepository)).build();
    MavenDownloadProxyServlet servlet = new MavenDownloadProxyServlet(resolver, runtime, null, 1, 0);
    servlet.start();
    mvnInstall(defaultRepository, "io.fabric8.test", "universalis-api", "0.1.0-SNAPSHOT", at("10:00"), "a");
    // Here's expected state of repository where SNAPSHOT was `mvn install`ed
    assertFalse(new File(defaultRepository, "io/fabric8/test/universalis-api/0.1.0-SNAPSHOT/maven-metadata.xml").isFile());
    assertTrue(new File(defaultRepository, "io/fabric8/test/universalis-api/0.1.0-SNAPSHOT/maven-metadata-local.xml").isFile());
    File file = servlet.download("io/fabric8/test/universalis-api/0.1.0-SNAPSHOT/maven-metadata.xml");
    Metadata metadata = readMetadata(file);
    boolean checked = false;
    assertThat(metadata.getVersioning().getSnapshot().isLocalCopy(), is(true));
    for (SnapshotVersion snapshotVersion : metadata.getVersioning().getSnapshotVersions()) {
        if ("jar".equals(snapshotVersion.getExtension())) {
            assertThat(snapshotVersion.getVersion(), is("0.1.0-SNAPSHOT"));
            checked = true;
        }
    }
    assertTrue("We should find snapshot metadata", checked);
    // if metadata says it's "0.1.0-SNAPSHOT", we should have no problem downloading this artifact without
    // version transformation
    file = servlet.download("io/fabric8/test/universalis-api/0.1.0-SNAPSHOT/universalis-api-0.1.0-SNAPSHOT.jar");
    assertThat(FileUtils.readFileToString(file), equalTo("a"));
    mvnInstall(defaultRepository, "io.fabric8.test", "universalis-api", "0.1.0-SNAPSHOT", at("11:00"), "b");
    file = servlet.download("io/fabric8/test/universalis-api/0.1.0-SNAPSHOT/universalis-api-0.1.0-SNAPSHOT.jar");
    assertThat("No policy should prevent us from seeing newer snapshot from defaultRepository", FileUtils.readFileToString(file), equalTo("b"));
}
Also used : SnapshotVersion(org.apache.maven.artifact.repository.metadata.SnapshotVersion) MavenResolver(io.fabric8.maven.MavenResolver) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) File(java.io.File) Test(org.junit.Test)

Example 4 with State

use of io.fabric8.agent.service.State in project fabric8 by jboss-fuse.

the class GitHttpServerRegistrationHandler method updateMasterUrl.

private void updateMasterUrl(Group<GitNode> group) {
    try {
        if (group.isMaster()) {
            LOGGER.debug("Git repo is the master");
            if (!isMaster.getAndSet(true)) {
                registerServlet(dataPath, realm, roles);
            }
        } else {
            LOGGER.debug("Git repo is not the master");
            if (isMaster.getAndSet(false)) {
                unregisterServlet();
            }
        }
        GitNode state = createState();
        group.update(state);
        String url = state.getUrl();
        gitRemoteUrl.set(ZooKeeperUtils.getSubstitutedData(curator.get(), url));
    } catch (Exception e) {
        LOGGER.debug("Failed to update master git server url.", e);
    }
}
Also used : GitNode(io.fabric8.git.GitNode) InstanceNotFoundException(javax.management.InstanceNotFoundException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) MalformedObjectNameException(javax.management.MalformedObjectNameException)

Example 5 with State

use of io.fabric8.agent.service.State in project fabric8 by jboss-fuse.

the class Agent method loadResources.

// 
// State support
// 
public static Callable<Map<String, Resource>> loadResources(DownloadManager manager, Map<String, Map<VersionRange, Map<String, String>>> metadata, Set<String> uris) throws MultiException, InterruptedException, MalformedURLException {
    final Map<String, Resource> resources = new HashMap<>();
    final Downloader downloader = manager.createDownloader();
    final MetadataBuilder builder = new MetadataBuilder(metadata);
    final DownloadCallback callback = new DownloadCallback() {

        @Override
        public void downloaded(StreamProvider provider) throws Exception {
            String uri = provider.getUrl();
            Map<String, String> headers = builder.getMetadata(uri, provider.getFile());
            Resource resource = ResourceBuilder.build(uri, headers);
            synchronized (resources) {
                resources.put(uri, resource);
            }
        }
    };
    for (String uri : uris) {
        downloader.download(uri, callback);
    }
    return new Callable<Map<String, Resource>>() {

        @Override
        public Map<String, Resource> call() throws Exception {
            downloader.await();
            return resources;
        }
    };
}
Also used : StreamProvider(io.fabric8.agent.download.StreamProvider) HashMap(java.util.HashMap) DownloadCallback(io.fabric8.agent.download.DownloadCallback) Resource(org.osgi.resource.Resource) Downloader(io.fabric8.agent.download.Downloader) Callable(java.util.concurrent.Callable)

Aggregations

IOException (java.io.IOException)15 File (java.io.File)11 Test (org.junit.Test)10 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 Bundle (org.osgi.framework.Bundle)6 BundleUpdate (io.fabric8.patch.management.BundleUpdate)5 BundleException (org.osgi.framework.BundleException)5 BufferState (io.fabric8.dosgi.io.ProtocolCodec.BufferState)4 ByteBuffer (java.nio.ByteBuffer)4 Buffer (org.fusesource.hawtbuf.Buffer)4 Downloader (io.fabric8.agent.download.Downloader)3 Container (io.fabric8.kubernetes.api.model.Container)3 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)3 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 Map (java.util.Map)3 DownloadCallback (io.fabric8.agent.download.DownloadCallback)2