Search in sources :

Example 36 with Repository

use of io.fabric8.agent.model.Repository 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 37 with Repository

use of io.fabric8.agent.model.Repository in project fabric8 by jboss-fuse.

the class DeploymentAgentTest method testFeatureRepoResolution.

@Test
@SuppressWarnings("unchecked")
public void testFeatureRepoResolution() throws Exception {
    CommandSupport.executeCommand("fabric:create --force --clean -n --wait-for-provisioning");
    // We are just want to use a feature repository that is not part of the distribution.
    CommandSupport.executeCommand("fabric:profile-create --parent feature-camel test-profile");
    CommandSupport.executeCommand("fabric:version-create --parent 1.0 1.1");
    CommandSupport.executeCommand("fabric:profile-edit --repository mvn:io.fabric8.examples.fabric-camel-dosgi/features/" + System.getProperty("fabric.version") + "/xml/features test-profile 1.1");
    CommandSupport.executeCommand("fabric:profile-edit --feature fabric-dosgi test-profile 1.1");
    // We remove all repositories from agent config but the maven central to rely on the fabric-maven-proxy.
    // Also remove local repository
    CommandSupport.executeCommand("fabric:profile-edit --pid io.fabric8.agent/org.ops4j.pax.url.mvn.repositories=http://repo1.maven.org/maven2@id=m2central default 1.1");
    CommandSupport.executeCommand("fabric:profile-edit --pid test-profile 1.1");
    BundleContext moduleContext = ServiceLocator.getSystemContext();
    ServiceProxy<FabricService> fabricProxy = ServiceProxy.createServiceProxy(moduleContext, FabricService.class);
    try {
        FabricService fabricService = fabricProxy.getService();
        Set<Container> containers = ContainerBuilder.create().withName("smoke_cnt_a").withProfiles("test-profile").assertProvisioningResult().build(fabricService);
        try {
            // We want to remove all repositories from fabric-agent.
            for (Container container : containers) {
                CommandSupport.executeCommand("fabric:container-upgrade 1.1 " + container.getId());
                System.out.flush();
            }
            ProvisionSupport.provisioningSuccess(containers, ProvisionSupport.PROVISION_TIMEOUT);
            CommandSupport.executeCommand("fabric:container-list");
            for (Container container : containers) {
                CommandSupport.executeCommand("fabric:container-connect -u admin -p admin " + container.getId() + " osgi:list");
                CommandSupport.executeCommand("fabric:container-connect -u admin -p admin " + container.getId() + " config:proplist --pid org.ops4j.pax.url.mvn");
                System.out.flush();
            }
        } finally {
            ContainerBuilder.stop(fabricService, containers);
        }
    } finally {
        fabricProxy.close();
    }
}
Also used : Container(io.fabric8.api.Container) FabricService(io.fabric8.api.FabricService) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 38 with Repository

use of io.fabric8.agent.model.Repository in project fabric8 by jboss-fuse.

the class CommandLineFabricControllerManager method createFabric.

@Override
public FabricController createFabric() throws Exception {
    if (workDirectory == null) {
        workDirectory = createTempDirectory();
    }
    String version = System.getProperty("fabric8-version", "1.2.0-SNAPSHOT");
    String home = System.getProperty("user.home", "~");
    String repo = home + "/.m2/repository";
    File distro = new File(repo, "io/fabric8/fabric8-karaf/" + version + "/fabric8-karaf-" + version + ".tar.gz");
    FabricAssertions.assertFileExists(distro);
    installDir = new File(workDirectory, "fabric8-karaf-" + version);
    killInstanceProcesses(getInstancesFile());
    if (workDirectory.exists()) {
        Files.recursiveDelete(workDirectory);
    }
    workDirectory.mkdirs();
    executeCommand(workDirectory, "tar", "zxf", distro.getAbsolutePath());
    FabricAssertions.assertDirectoryExists(installDir);
    assertTrue("install dir does not exist: " + installDir.getAbsolutePath(), installDir.exists());
    assertTrue("install dir is not a directory: " + installDir.getAbsolutePath(), installDir.isDirectory());
    System.out.println("About to boot up the fabric8 at: " + installDir.getAbsolutePath());
    File shellScript = new File(installDir, startFabricScriptName);
    FabricAssertions.assertFileExists(shellScript);
    executeCommand(installDir, "./" + startFabricScriptName);
    final FabricController restApi = createFabricController();
    return restApi;
}
Also used : FabricController(io.fabric8.testkit.FabricController) JolokiaFabricController(io.fabric8.testkit.jolokia.JolokiaFabricController) File(java.io.File)

Example 39 with Repository

use of io.fabric8.agent.model.Repository in project halyard by spinnaker.

the class LocalDebianServiceProvider method getInstallCommand.

@Override
public String getInstallCommand(DeploymentDetails deploymentDetails, GenerateService.ResolvedConfiguration resolvedConfiguration, Map<String, String> installCommands) {
    Map<String, Object> bindings = new HashMap<>();
    List<SpinnakerService.Type> serviceTypes = new ArrayList<>(installCommands.keySet()).stream().map(SpinnakerService.Type::fromCanonicalName).collect(Collectors.toList());
    List<String> upstartNames = getLocalServices(serviceTypes).stream().filter(i -> resolvedConfiguration.getServiceSettings(i.getService()).getEnabled()).map(i -> ((LocalDebianService) i).getUpstartServiceName()).filter(Objects::nonNull).collect(Collectors.toList());
    List<String> systemdServiceConfigs = upstartNames.stream().map(n -> n + ".service").collect(Collectors.toList());
    List<String> serviceInstalls = serviceTypes.stream().map(t -> installCommands.get(t.getCanonicalName())).collect(Collectors.toList());
    TemplatedResource resource = new StringReplaceJarResource("/debian/init.sh");
    bindings.put("services", Strings.join(upstartNames, " "));
    bindings.put("systemd-service-configs", Strings.join(systemdServiceConfigs, " "));
    String upstartInit = resource.setBindings(bindings).toString();
    BillOfMaterials.ArtifactSources artifactSources = artifactService.getArtifactSources(deploymentDetails.getDeploymentName());
    resource = new StringReplaceJarResource("/debian/install.sh");
    bindings = new HashMap<>();
    bindings.put("prepare-environment", "true");
    bindings.put("install-redis", "true");
    bindings.put("debian-repository", artifactSourcesConfig.mergeWithBomSources(artifactSources).getDebianRepository());
    bindings.put("install-commands", String.join("\n", serviceInstalls));
    bindings.put("service-action", "restart");
    bindings.put("upstart-init", upstartInit);
    return resource.setBindings(bindings).toString();
}
Also used : DeploymentDetails(com.netflix.spinnaker.halyard.deploy.deployment.v1.DeploymentDetails) java.util(java.util) ArtifactSourcesConfig(com.netflix.spinnaker.halyard.config.config.v1.ArtifactSourcesConfig) Autowired(org.springframework.beans.factory.annotation.Autowired) ArtifactService(com.netflix.spinnaker.halyard.deploy.services.v1.ArtifactService) GenerateService(com.netflix.spinnaker.halyard.deploy.services.v1.GenerateService) SpinnakerRuntimeSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerRuntimeSettings) Collectors(java.util.stream.Collectors) Component(org.springframework.stereotype.Component) LocalServiceProvider(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.local.LocalServiceProvider) StringReplaceJarResource(com.netflix.spinnaker.halyard.core.resource.v1.StringReplaceJarResource) RemoteAction(com.netflix.spinnaker.halyard.core.RemoteAction) BillOfMaterials(com.netflix.spinnaker.halyard.core.registry.v1.BillOfMaterials) SpinnakerService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.SpinnakerService) Strings(io.fabric8.utils.Strings) TemplatedResource(com.netflix.spinnaker.halyard.core.resource.v1.TemplatedResource) TemplatedResource(com.netflix.spinnaker.halyard.core.resource.v1.TemplatedResource) SpinnakerService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.SpinnakerService) BillOfMaterials(com.netflix.spinnaker.halyard.core.registry.v1.BillOfMaterials) StringReplaceJarResource(com.netflix.spinnaker.halyard.core.resource.v1.StringReplaceJarResource)

Example 40 with Repository

use of io.fabric8.agent.model.Repository in project fabric8-maven-plugin by fabric8io.

the class ImportMojo method executeInternal.

@Override
public void executeInternal() throws MojoExecutionException, MojoFailureException {
    if (!basedir.isDirectory() || !basedir.exists()) {
        throw new MojoExecutionException("No directory for base directory: " + basedir);
    }
    // lets check for a git repo
    String gitRemoteURL = null;
    Repository repository = null;
    try {
        repository = GitUtils.findRepository(basedir);
    } catch (IOException e) {
        throw new MojoExecutionException("Failed to find local git repository in current directory: " + e, e);
    }
    try {
        gitRemoteURL = GitUtils.getRemoteAsHttpsURL(repository);
    } catch (Exception e) {
        throw new MojoExecutionException("Failed to get the current git branch: " + e, e);
    }
    try {
        clusterAccess = new ClusterAccess(this.namespace);
        if (Strings.isNullOrBlank(projectName)) {
            projectName = basedir.getName();
        }
        KubernetesClient kubernetes = clusterAccess.createDefaultClient(log);
        KubernetesResourceUtil.validateKubernetesMasterUrl(kubernetes.getMasterUrl());
        String namespace = clusterAccess.getNamespace();
        OpenShiftClient openShiftClient = getOpenShiftClientOrJenkinsShift(kubernetes, namespace);
        if (gitRemoteURL != null) {
            // lets check we don't already have this project imported
            String branch = repository.getBranch();
            BuildConfig buildConfig = findBuildConfigForGitRepo(openShiftClient, namespace, gitRemoteURL, branch);
            if (buildConfig != null) {
                logBuildConfigLink(kubernetes, namespace, buildConfig, log);
                throw new MojoExecutionException("Project already imported into build " + getName(buildConfig) + " for URI: " + gitRemoteURL + " and branch: " + branch);
            } else {
                Map<String, String> annotations = new HashMap<>();
                annotations.put(Annotations.Builds.GIT_CLONE_URL, gitRemoteURL);
                buildConfig = createBuildConfig(kubernetes, namespace, projectName, gitRemoteURL, annotations);
                openShiftClient.buildConfigs().inNamespace(namespace).create(buildConfig);
                ensureExternalGitSecretsAreSetupFor(kubernetes, namespace, gitRemoteURL);
                logBuildConfigLink(kubernetes, namespace, buildConfig, log);
            }
        } else {
            // lets create an import a new project
            UserDetails userDetails = createGogsUserDetails(kubernetes, namespace);
            userDetails.setBranch(branchName);
            BuildConfigHelper.CreateGitProjectResults createGitProjectResults;
            try {
                createGitProjectResults = BuildConfigHelper.importNewGitProject(kubernetes, userDetails, basedir, namespace, projectName, remoteName, "Importing project from mvn fabric8:import", false);
            } catch (WebApplicationException e) {
                Response response = e.getResponse();
                if (response.getStatus() > 400) {
                    String message = getEntityMessage(response);
                    log.warn("Could not create the git repository: %s %s", e, message);
                    log.warn("Are your username and password correct in the Secret %s/%s?", secretNamespace, gogsSecretName);
                    log.warn("To re-enter your password rerun this command with -Dfabric8.passsword.retry=true");
                    throw new MojoExecutionException("Could not create the git repository. " + "Are your username and password correct in the Secret " + secretNamespace + "/" + gogsSecretName + "?" + e + message, e);
                } else {
                    throw e;
                }
            }
            BuildConfig buildConfig = createGitProjectResults.getBuildConfig();
            openShiftClient.buildConfigs().inNamespace(namespace).create(buildConfig);
            logBuildConfigLink(kubernetes, namespace, buildConfig, log);
        }
    } catch (KubernetesClientException e) {
        KubernetesResourceUtil.handleKubernetesClientException(e, this.log);
    } catch (Exception e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) WebApplicationException(javax.ws.rs.WebApplicationException) HashMap(java.util.HashMap) IOException(java.io.IOException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) PrompterException(org.codehaus.plexus.components.interactivity.PrompterException) WebApplicationException(javax.ws.rs.WebApplicationException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Response(javax.ws.rs.core.Response) Repository(org.eclipse.jgit.lib.Repository) UserDetails(io.fabric8.project.support.UserDetails) BuildConfigHelper(io.fabric8.project.support.BuildConfigHelper) ClusterAccess(io.fabric8.maven.core.access.ClusterAccess) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) BuildConfigHelper.createBuildConfig(io.fabric8.project.support.BuildConfigHelper.createBuildConfig) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Aggregations

File (java.io.File)49 Test (org.junit.Test)32 Git (org.eclipse.jgit.api.Git)30 GitPatchRepository (io.fabric8.patch.management.impl.GitPatchRepository)27 IOException (java.io.IOException)24 GitPatchManagementServiceImpl (io.fabric8.patch.management.impl.GitPatchManagementServiceImpl)20 HashMap (java.util.HashMap)15 ObjectId (org.eclipse.jgit.lib.ObjectId)13 MalformedURLException (java.net.MalformedURLException)11 Map (java.util.Map)11 ArrayList (java.util.ArrayList)10 PatchException (io.fabric8.patch.management.PatchException)9 RevCommit (org.eclipse.jgit.revwalk.RevCommit)8 Bundle (org.osgi.framework.Bundle)8 Version (org.osgi.framework.Version)8 MavenResolver (io.fabric8.maven.MavenResolver)7 HashSet (java.util.HashSet)7 Repository (io.fabric8.agent.model.Repository)6 BundleContext (org.osgi.framework.BundleContext)6 Feature (io.fabric8.agent.model.Feature)5