Search in sources :

Example 31 with Check

use of io.fabric8.karaf.checks.Check in project fabric8 by jboss-fuse.

the class Offline method applyPatch.

protected void applyPatch(PatchData patch, ZipFile zipFile, File storage) throws IOException {
    log(DEBUG, "Applying patch: " + patch.getId() + " / " + patch.getDescription());
    File startupFile = new File(karafBase, "etc/startup.properties");
    File overridesFile = new File(karafBase, "etc/overrides.properties");
    List<String> startup = readLines(new File(karafBase, "etc/startup.properties"));
    List<String> overrides = readLines(overridesFile);
    List<Artifact> toExtract = new ArrayList<Artifact>();
    List<Artifact> toDelete = new ArrayList<Artifact>();
    for (String bundle : patch.getBundles()) {
        Artifact artifact = mvnurlToArtifact(bundle, true);
        if (artifact == null) {
            continue;
        }
        // Compute patch bundle version and range
        VersionRange range;
        Version oVer = VersionTable.getVersion(artifact.getVersion());
        String vr = patch.getVersionRange(bundle);
        String override;
        if (vr != null && !vr.isEmpty()) {
            override = bundle + OVERRIDE_RANGE + vr;
            range = VersionRange.parseVersionRange(vr);
        } else {
            override = bundle;
            Version v1 = new Version(oVer.getMajor(), oVer.getMinor(), 0);
            Version v2 = new Version(oVer.getMajor(), oVer.getMinor() + 1, 0);
            range = new VersionRange(false, v1, v2, true);
        }
        // Process overrides.properties
        boolean matching = false;
        boolean added = false;
        for (int i = 0; i < overrides.size(); i++) {
            String line = overrides.get(i).trim();
            if (!line.isEmpty() && !line.startsWith("#")) {
                Artifact overrideArtifact = mvnurlToArtifact(line, true);
                if (overrideArtifact != null) {
                    Version ver = VersionTable.getVersion(overrideArtifact.getVersion());
                    if (isSameButVersion(artifact, overrideArtifact) && range.contains(ver)) {
                        matching = true;
                        if (ver.compareTo(oVer) < 0) {
                            // Replace old override with the new one
                            overrides.set(i, override);
                            if (!added) {
                                log(DEBUG, "Replacing with artifact: " + override);
                                added = true;
                            }
                            // Remove old file
                            toDelete.add(overrideArtifact);
                            toExtract.remove(overrideArtifact);
                        }
                    }
                } else {
                    log(WARN, "Unable to convert to artifact: " + line);
                }
            }
        }
        // If there was not matching bundles, add it
        if (!matching) {
            overrides.add(override);
            log(DEBUG, "Adding artifact: " + override);
        }
        // Process startup.properties
        for (int i = 0; i < startup.size(); i++) {
            String line = startup.get(i).trim();
            if (!line.isEmpty() && !line.startsWith("#")) {
                int index = line.indexOf('=');
                String mvnUrl = Utils.pathToMvnurl(line.substring(0, index));
                if (mvnUrl != null) {
                    Artifact startupArtifact = mvnurlToArtifact(mvnUrl, true);
                    if (startupArtifact != null) {
                        Version ver = VersionTable.getVersion(startupArtifact.getVersion());
                        if (isSameButVersion(artifact, startupArtifact) && range.contains(ver)) {
                            matching = true;
                            // Now check versions
                            if (ver.compareTo(oVer) < 0) {
                                line = artifact.getPath() + line.substring(index);
                                startup.set(i, line);
                                log(DEBUG, "Overwriting startup.properties with: " + artifact);
                                added = true;
                            }
                        }
                    }
                }
            }
        }
        // Extract artifact
        if (!matching || added) {
            toExtract.add(artifact);
        }
    }
    // Extract / delete artifacts if needed
    if (zipFile != null) {
        for (Artifact artifact : toExtract) {
            log(DEBUG, "Extracting artifact: " + artifact);
            ZipEntry entry = zipFile.getEntry("repository/" + artifact.getPath());
            if (entry == null) {
                log(ERROR, "Could not find artifact in patch zip: " + artifact);
                continue;
            }
            File f = new File(karafBase, "system/" + artifact.getPath());
            if (!f.isFile()) {
                f.getParentFile().mkdirs();
                InputStream fis = zipFile.getInputStream(entry);
                FileOutputStream fos = new FileOutputStream(f);
                try {
                    IOUtils.copy(fis, fos);
                } finally {
                    IOUtils.closeQuietly(fis);
                    IOUtils.closeQuietly(fos);
                }
            }
        }
        for (Artifact artifact : toDelete) {
            String fileName = artifact.getPath();
            File file = new File(karafBase, "system/" + fileName);
            if (file.exists()) {
                log(DEBUG, "Removing old artifact " + artifact);
                file.delete();
            } else {
                log(WARN, "Could not find: " + file);
            }
        }
    }
    overrides = new ArrayList<String>(new HashSet<String>(overrides));
    Collections.sort(overrides);
    writeLines(overridesFile, overrides);
    writeLines(startupFile, startup);
    // update the remaining patch files (using either the patch ZIP file or the patch storage location)
    if (zipFile != null) {
        patchFiles(patch, zipFile);
    } else if (storage != null) {
        patchFiles(patch, storage);
    } else {
        throw new PatchException("Unable to update patch files: no access to patch ZIP file or patch storage location");
    }
    if (patch.getMigratorBundle() != null) {
        Artifact artifact = mvnurlToArtifact(patch.getMigratorBundle(), true);
        if (artifact != null) {
            // Copy it to the deploy dir
            File src = new File(karafBase, "system/" + artifact.getPath());
            File target = new File(new File(karafBase, "deploy"), artifact.getArtifactId() + ".jar");
            copy(src, target);
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) VersionRange(org.apache.felix.utils.version.VersionRange) Utils.mvnurlToArtifact(io.fabric8.patch.management.Utils.mvnurlToArtifact) Artifact(io.fabric8.patch.management.Artifact) Artifact.isSameButVersion(io.fabric8.patch.management.Artifact.isSameButVersion) Version(org.osgi.framework.Version) FileOutputStream(java.io.FileOutputStream) PatchException(io.fabric8.patch.management.PatchException) ZipFile(java.util.zip.ZipFile) File(java.io.File) HashSet(java.util.HashSet)

Example 32 with Check

use of io.fabric8.karaf.checks.Check in project fabric8 by jboss-fuse.

the class ArchetypeGenerateAction method doExecute.

@Override
protected Object doExecute() throws Exception {
    // if no directory then use workspace
    if (directory == null) {
        // must have a workspace location configured
        Preferences preferences = Preferences.userNodeForPackage(getClass());
        String location = preferences.get(ArchetypeWorkspace.PREFERENCE_WORKSPACE, null);
        if (location == null) {
            System.out.println("No workspace location has been set.");
            System.out.println("Use the archetype-workspace command to set a workspace first.");
            System.out.println("");
            return null;
        } else {
            System.out.println("Using current workspace: " + location);
            directory = location;
        }
    } else {
        System.out.println("Using directory as workspace: " + directory);
    }
    File target = new File(directory);
    // make sure the directory exists, auto-creating if missing
    if (!target.exists()) {
        target.mkdirs();
    }
    if (!target.exists() || !target.isDirectory()) {
        System.err.println("Workspace does not exists or is not a directory: " + directory);
        return null;
    }
    Archetype archetype = null;
    // try artifact first
    if (!isNullOrBlank(archetypeOrFilter)) {
        archetype = archetypeService.getArchetypeByArtifact(archetypeOrFilter);
        if (archetype == null) {
            // then by coordinate
            archetype = archetypeService.getArchetype(archetypeOrFilter);
        }
    }
    // no archetype yet so present a list where the user can select
    while (archetype == null) {
        List<Archetype> archetypes = archetypeService.listArchetypes(archetypeOrFilter, true);
        System.out.println("Choose archetype:");
        Iterator<Archetype> it = archetypes.iterator();
        int i = 0;
        while (it.hasNext()) {
            Archetype select = it.next();
            System.out.println(String.format("%4d: -> %-50s %s", ++i, select.artifactId, select.description));
        }
        boolean choosing = true;
        while (choosing) {
            // default select last
            String choose = ShellUtils.readLine(session, String.format("Choose a number or apply filter (case insensitive): %d: ", i), false);
            if (Strings.isNullOrBlank(choose)) {
                // user pressed enter so we select the last
                choose = "" + i;
            }
            try {
                int no = Integer.valueOf(choose);
                // is the number within range
                if (no >= 1 && no <= archetypes.size()) {
                    archetype = archetypes.get(no - 1);
                    break;
                } else {
                    System.out.println("Number " + no + " out of range. Please try again!");
                    continue;
                }
            } catch (NumberFormatException e) {
                // no its a filter, so we use this as filter, and show the list again
                archetypeOrFilter = choose;
                choosing = false;
                archetype = null;
            }
        }
    }
    // okay we have selected an archetype now
    File archetypeFile = fetchArchetype(archetype);
    if (archetypeFile == null || !archetypeFile.exists()) {
        System.err.println("No archetype found for \"" + archetypeOrFilter + "\" coordinates");
        return null;
    }
    System.out.println("----------------------------------------------------------------------------");
    System.out.println("Using archetype: " + archetype.artifactId);
    String defaultGroupId = "io.fabric8";
    String defaultArtifactId = archetype.artifactId + "-example";
    String defaultVersion = "1.0-SNAPSHOT";
    String defaultName = archetype.name;
    String defaultDescription = isNotBlank(archetype.description) ? archetype.description : "";
    System.out.println("----- Configure archetype -----");
    String groupId = ShellUtils.readLine(session, String.format("Define value for property 'groupId' (%s): ", defaultGroupId), false);
    String artifactId = ShellUtils.readLine(session, String.format("Define value for property 'artifactId' (%s): ", defaultArtifactId), false);
    String version = ShellUtils.readLine(session, String.format("Define value for property 'version' (%s): ", defaultVersion), false);
    groupId = isNullOrBlank(groupId) ? defaultGroupId : groupId;
    artifactId = isNullOrBlank(artifactId) ? defaultArtifactId : artifactId;
    version = isNullOrBlank(version) ? defaultVersion : version;
    String defaultPackageName = (groupId + "." + artifactId).replaceAll("-", ".");
    String packageName = ShellUtils.readLine(session, String.format("Define value for property 'package' (%s): ", defaultPackageName), false);
    // use artifact id as default directory name (maven does this also)
    String defaultDirectoryName = isNullOrBlank(artifactId) ? defaultArtifactId : artifactId;
    directory = ShellUtils.readLine(session, String.format("Define value for property 'directoryName' (%s): ", defaultDirectoryName), false);
    packageName = isNullOrBlank(packageName) ? defaultPackageName : packageName;
    directory = isNullOrBlank(directory) ? artifactId : directory;
    String name = ShellUtils.readLine(session, String.format("Define value for property 'name' (%s): ", defaultName), false);
    String description = ShellUtils.readLine(session, String.format("Define value for property 'description' (%s): ", defaultDescription), false);
    // use null to indicate we want out of the box description
    name = isNullOrBlank(name) ? null : name;
    description = isNullOrBlank(description) ? null : description;
    File childDir = new File(target, directory);
    ArchetypeHelper helper = new ArchetypeHelper(archetypeFile, childDir, groupId, artifactId, version, name, description);
    helper.setPackageName(packageName);
    Map<String, String> properties = helper.parseProperties();
    // if we have fabric8.profile as a property then lets configured it now, as its mandatory
    // and use artifactId as its default suggested value
    String profile = null;
    if (properties.containsKey("fabric8-profile")) {
        profile = properties.remove("fabric8-profile");
        String defaultProfile = isNullOrBlank(profile) ? artifactId : profile;
        String p = ShellUtils.readLine(session, String.format("Define value for property 'fabric8.profile' (%s): ", defaultProfile), false);
        profile = isNullOrBlank(p) ? defaultProfile : p;
    }
    // show additional properties and ask to use them as-is
    boolean mustChoose = false;
    if (!properties.isEmpty()) {
        // check if we must choose if there is an empty value or a value that has a ${ } token so we dont have a default value
        for (String value : properties.values()) {
            if (isNullOrBlank(value) || value.contains("$")) {
                mustChoose = true;
                break;
            }
        }
        if (!mustChoose) {
            System.out.println("----- Additional properties -----");
            for (String key : properties.keySet()) {
                System.out.println(String.format("Using property '%s' (%s): ", key, properties.get(key)));
            }
        }
        boolean choosing = true;
        while (mustChoose || choosing) {
            String confirm = null;
            if (!mustChoose) {
                confirm = ShellUtils.readLine(session, "Confirm additional properties configuration: (Y): ", false);
                confirm = isNullOrBlank(confirm) ? "Y" : confirm;
            }
            if (mustChoose || !"Y".equalsIgnoreCase(confirm)) {
                // ask for replacement properties suggesting the defaults
                if (!properties.isEmpty()) {
                    System.out.println("----- Configure additional properties -----");
                    for (String key : properties.keySet()) {
                        String value = properties.get(key);
                        // if the value is empty or a token, then do not show any default value
                        if (isNullOrBlank(value) || value.contains("$")) {
                            value = "";
                        }
                        String p = ShellUtils.readLine(session, String.format("Define value for property '%s' (%s): ", key, value), false);
                        p = isNullOrBlank(p) ? value : p;
                        properties.put(key, p);
                    }
                }
                mustChoose = false;
            } else {
                choosing = false;
            }
        }
    }
    // remover to include the profile back into properties
    if (profile != null) {
        properties.put("fabric8-profile", profile);
    }
    if (!properties.isEmpty()) {
        // set override properties
        helper.setOverrideProperties(properties);
    }
    String confirm = ShellUtils.readLine(session, "Create project: (Y): ", false);
    confirm = confirm == null || confirm.trim().equals("") ? "Y" : confirm;
    if ("Y".equalsIgnoreCase(confirm)) {
        System.out.println("----------------------------------------------------------------------------");
        System.out.println(String.format("Creating project in directory: %s", childDir.getCanonicalPath()));
        helper.execute();
        System.out.println("Project created successfully");
        System.out.println("");
    } else {
        System.out.println("----------------------------------------------------------------------------");
        System.out.println("Creating project aborted!");
        System.out.println("");
    }
    return null;
}
Also used : Archetype(io.fabric8.tooling.archetype.catalog.Archetype) ArchetypeHelper(io.fabric8.tooling.archetype.generator.ArchetypeHelper) Preferences(java.util.prefs.Preferences) File(java.io.File)

Example 33 with Check

use of io.fabric8.karaf.checks.Check in project fabric8-maven-plugin by fabric8io.

the class OpenshiftBuildServiceTest method testSuccessfulSecondBuild.

@Test
public void testSuccessfulSecondBuild() throws Exception {
    int nTries = 0;
    boolean bTestComplete = false;
    do {
        try {
            nTries++;
            BuildService.BuildServiceConfig config = defaultConfig.build();
            WebServerEventCollector<OpenShiftMockServer> collector = createMockServer(config, true, 50, true, true);
            OpenShiftMockServer mockServer = collector.getMockServer();
            OpenShiftClient client = mockServer.createOpenShiftClient();
            OpenshiftBuildService service = new OpenshiftBuildService(client, logger, dockerServiceHub, config);
            service.build(image);
            assertTrue(mockServer.getRequestCount() > 8);
            collector.assertEventsRecordedInOrder("build-config-check", "patch-build-config", "pushed");
            collector.assertEventsNotRecorded("new-build-config");
            bTestComplete = true;
        } catch (Fabric8ServiceException exception) {
            Throwable rootCause = getRootCause(exception);
            logger.warn("A problem encountered while running test {}, retrying..", exception.getMessage());
            // Let's wait for a while, and then retry again
            if (rootCause != null && rootCause instanceof IOException) {
                continue;
            }
        }
    } while (nTries < MAX_TIMEOUT_RETRIES && !bTestComplete);
}
Also used : OpenShiftMockServer(io.fabric8.openshift.client.server.mock.OpenShiftMockServer) Fabric8ServiceException(io.fabric8.maven.core.service.Fabric8ServiceException) BuildService(io.fabric8.maven.core.service.BuildService) DefaultOpenShiftClient(io.fabric8.openshift.client.DefaultOpenShiftClient) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) IOException(java.io.IOException) Test(org.junit.Test)

Example 34 with Check

use of io.fabric8.karaf.checks.Check in project fabric8-maven-plugin by fabric8io.

the class OpenshiftBuildServiceTest method createMockServer.

protected WebServerEventCollector<OpenShiftMockServer> createMockServer(BuildService.BuildServiceConfig config, boolean success, long buildDelay, boolean buildConfigExists, boolean imageStreamExists) {
    OpenShiftMockServer mockServer = new OpenShiftMockServer(false);
    WebServerEventCollector<OpenShiftMockServer> collector = new WebServerEventCollector<>(mockServer);
    BuildConfig bc = new BuildConfigBuilder().withNewMetadata().withName(projectName + config.getS2iBuildNameSuffix()).endMetadata().withNewSpec().endSpec().build();
    ImageStream imageStream = new ImageStreamBuilder().withNewMetadata().withName(projectName).endMetadata().withStatus(new ImageStreamStatusBuilder().addNewTagLike(new NamedTagEventListBuilder().addNewItem().withImage("abcdef0123456789").endItem().build()).endTag().build()).build();
    KubernetesList builds = new KubernetesListBuilder().withItems(new BuildBuilder().withNewMetadata().withName(projectName).endMetadata().build()).withNewMetadata().withResourceVersion("1").endMetadata().build();
    String buildStatus = success ? "Complete" : "Fail";
    Build build = new BuildBuilder().withNewMetadata().withResourceVersion("2").endMetadata().withNewStatus().withPhase(buildStatus).endStatus().build();
    if (!buildConfigExists) {
        mockServer.expect().get().withPath("/oapi/v1/namespaces/test/buildconfigs/" + projectName + config.getS2iBuildNameSuffix()).andReply(collector.record("build-config-check").andReturn(404, "")).once();
        mockServer.expect().post().withPath("/oapi/v1/namespaces/test/buildconfigs").andReply(collector.record("new-build-config").andReturn(201, bc)).once();
    } else {
        mockServer.expect().patch().withPath("/oapi/v1/namespaces/test/buildconfigs/" + projectName + config.getS2iBuildNameSuffix()).andReply(collector.record("patch-build-config").andReturn(200, bc)).once();
    }
    mockServer.expect().get().withPath("/oapi/v1/namespaces/test/buildconfigs/" + projectName + config.getS2iBuildNameSuffix()).andReply(collector.record("build-config-check").andReturn(200, bc)).always();
    if (!imageStreamExists) {
        mockServer.expect().get().withPath("/oapi/v1/namespaces/test/imagestreams/" + projectName).andReturn(404, "").once();
    }
    mockServer.expect().get().withPath("/oapi/v1/namespaces/test/imagestreams/" + projectName).andReturn(200, imageStream).always();
    mockServer.expect().post().withPath("/oapi/v1/namespaces/test/imagestreams").andReturn(201, imageStream).once();
    mockServer.expect().post().withPath("/oapi/v1/namespaces/test/buildconfigs/" + projectName + config.getS2iBuildNameSuffix() + "/instantiatebinary?commit=").andReply(collector.record("pushed").andReturn(201, imageStream)).once();
    mockServer.expect().get().withPath("/oapi/v1/namespaces/test/builds").andReply(collector.record("check-build").andReturn(200, builds)).always();
    mockServer.expect().get().withPath("/oapi/v1/namespaces/test/builds?labelSelector=openshift.io/build-config.name%3D" + projectName + config.getS2iBuildNameSuffix()).andReturn(200, builds).always();
    mockServer.expect().withPath("/oapi/v1/namespaces/test/builds/" + projectName).andReturn(200, build).always();
    mockServer.expect().withPath("/oapi/v1/namespaces/test/builds?fieldSelector=metadata.name%3D" + projectName + "&watch=true").andUpgradeToWebSocket().open().waitFor(buildDelay).andEmit(new WatchEvent(build, "MODIFIED")).done().always();
    return collector;
}
Also used : KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) ImageStreamStatusBuilder(io.fabric8.openshift.api.model.ImageStreamStatusBuilder) ImageStream(io.fabric8.openshift.api.model.ImageStream) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) WebServerEventCollector(io.fabric8.maven.core.util.WebServerEventCollector) BuildConfigBuilder(io.fabric8.openshift.api.model.BuildConfigBuilder) BuildBuilder(io.fabric8.openshift.api.model.BuildBuilder) ImageStreamBuilder(io.fabric8.openshift.api.model.ImageStreamBuilder) OpenShiftMockServer(io.fabric8.openshift.client.server.mock.OpenShiftMockServer) Build(io.fabric8.openshift.api.model.Build) NamedTagEventListBuilder(io.fabric8.openshift.api.model.NamedTagEventListBuilder) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) WatchEvent(io.fabric8.kubernetes.api.model.WatchEvent)

Example 35 with Check

use of io.fabric8.karaf.checks.Check in project fabric8-maven-plugin by fabric8io.

the class DockerHealthCheckEnricherTest method testUnmatchingHealthCheck.

@Test
public void testUnmatchingHealthCheck() throws Exception {
    // Setup mock behaviour
    new Expectations() {

        {
            context.getImages();
            result = Arrays.asList(new ImageConfiguration.Builder().alias("myImage").buildConfig(new BuildImageConfiguration.Builder().healthCheck(new HealthCheckConfiguration.Builder().mode(HealthCheckMode.cmd).cmd("/bin/check").timeout("1s").interval("1h1s").retries(3).build()).build()).build());
        }
    };
    KubernetesListBuilder builder = createDeployment("myUnmatchingImage");
    DockerHealthCheckEnricher enricher = new DockerHealthCheckEnricher(context);
    enricher.addMissingResources(builder);
    KubernetesList list = builder.build();
    assertEquals(1, list.getItems().size());
    assertNoProbes(list.getItems().get(0));
}
Also used : Expectations(mockit.Expectations) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)35 IOException (java.io.IOException)21 File (java.io.File)17 ArrayList (java.util.ArrayList)16 HashMap (java.util.HashMap)14 FabricService (io.fabric8.api.FabricService)11 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)11 Container (io.fabric8.api.Container)9 PatchException (io.fabric8.patch.management.PatchException)9 Expectations (mockit.Expectations)9 Profile (io.fabric8.api.Profile)8 Map (java.util.Map)7 TreeMap (java.util.TreeMap)7 Version (io.fabric8.api.Version)6 AuthConfig (io.fabric8.maven.docker.access.AuthConfig)6 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)6 Check (io.fabric8.karaf.checks.Check)5 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)5 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)5 Patch (io.fabric8.patch.management.Patch)5