Search in sources :

Example 21 with GitOperation

use of io.fabric8.git.internal.GitOperation in project fabric8 by jboss-fuse.

the class FabricGitFacade method remove.

@Override
public void remove(final String branch, final String path, final String commitMessage, final String authorName, final String authorEmail) {
    assertValid();
    final PersonIdent personIdent = new PersonIdent(authorName, authorEmail);
    gitWriteOperation(personIdent, new GitOperation<RevCommit>() {

        public RevCommit call(Git git, GitContext context) throws Exception {
            checkoutBranch(git, branch);
            File rootDir = getRootGitDirectory(git);
            RevCommit answer = doRemove(git, rootDir, branch, path, commitMessage, personIdent);
            context.commitMessage(commitMessage);
            return answer;
        }
    });
}
Also used : Git(org.eclipse.jgit.api.Git) PersonIdent(org.eclipse.jgit.lib.PersonIdent) GitContext(io.fabric8.api.GitContext) File(java.io.File) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 22 with GitOperation

use of io.fabric8.git.internal.GitOperation in project fabric8 by jboss-fuse.

the class FabricPatchServiceImpl method install.

@Override
public PatchResult install(final Patch patch, boolean simulation, final String versionId, boolean upload, final String username, final String password, final ProfileUpdateStrategy strategy) throws IOException {
    // we start from the same state as in standalone mode - after successful patch:add
    // we have other things to do in fabric env however:
    // 1. check prerequisites
    // 2. we don't care about current state of framework - it'll be managed by fabric-agent and we don't
    // necessary install a patch for this container we're in
    // 3. we don't do patchManagement.beginInstallation / patchManagement.commitInstallation here
    // this will be done later - after updated fabric-agent is started
    // 4. we don't have to analyze bundles/features/repositories updates - these will be handled simply by
    // updating profiles in specified version
    PatchKind kind = patch.getPatchData().isRollupPatch() ? PatchKind.ROLLUP : PatchKind.NON_ROLLUP;
    if (kind == PatchKind.NON_ROLLUP) {
        throw new UnsupportedOperationException("patch:fabric-install should be used for Rollup patches only");
    }
    String currentContainersVersionId = fabricService.getCurrentContainer().getVersionId();
    if (!simulation && versionId.equals(currentContainersVersionId)) {
        throw new UnsupportedOperationException("Can't install Rollup patch in current version. Please install" + " this patch in new version and then upgrade existing container(s)");
    }
    fabricService.adapt(ProfileService.class).getRequiredVersion(versionId);
    // just a list of new bundle locations - in fabric the updatable version depends on the moment we
    // apply the new version to existing containers.
    List<BundleUpdate> bundleUpdatesInThisPatch = bundleUpdatesInPatch(patch);
    Presentation.displayBundleUpdates(bundleUpdatesInThisPatch, true);
    PatchResult result = new PatchResult(patch.getPatchData(), simulation, System.currentTimeMillis(), bundleUpdatesInThisPatch, null);
    if (!simulation) {
        // update profile definitions stored in Git. We don't update ${karaf.home}/fabric, becuase it is used
        // only once - when importing profiles during fabric:create.
        // when fabric is already available, we have to update (Git) repository information
        GitOperation operation = new GitOperation() {

            @Override
            public Object call(Git git, GitContext context) throws Exception {
                // we can't pass git reference to patch-management
                // because patch-management private-packages git library
                // but we can leverage the write lock we have
                GitHelpers.checkoutBranch(git, versionId);
                // let's get back in history to the point before user changes (profile-edits), but not earlier
                // than last R patch
                String patchBranch = patchManagement.findLatestPatchRevision(git.getRepository().getDirectory(), versionId);
                // now install profiles from patch just like there were no user changes
                patchManagement.installProfiles(git.getRepository().getDirectory(), versionId, patch, strategy);
                // and finally we have to merge user and patch changes to profiles.
                patchManagement.mergeProfileChanges(patch, git.getRepository().getDirectory(), versionId, patchBranch);
                context.commitMessage("Installing rollup patch \"" + patch.getPatchData().getId() + "\"");
                return null;
            }
        };
        gitDataStore.gitOperation(new GitContext().requireCommit().setRequirePush(true), operation, null);
        if (upload) {
            PatchManagement.UploadCallback callback = new PatchManagement.UploadCallback() {

                @Override
                public void doWithUrlConnection(URLConnection connection) throws ProtocolException {
                    if (connection instanceof HttpURLConnection) {
                        ((HttpURLConnection) connection).setRequestMethod("PUT");
                    }
                    if (username != null && password != null) {
                        connection.setRequestProperty("Authorization", "Basic " + Base64Encoder.encode(username + ":" + password));
                    }
                }
            };
            patchManagement.uploadPatchArtifacts(patch.getPatchData(), fabricService.getMavenRepoUploadURI(), callback);
        }
    }
    return result;
}
Also used : PatchKind(io.fabric8.patch.management.PatchKind) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) ProfileService(io.fabric8.api.ProfileService) GitOperation(io.fabric8.git.internal.GitOperation) Git(org.eclipse.jgit.api.Git) HttpURLConnection(java.net.HttpURLConnection) GitContext(io.fabric8.api.GitContext) PatchManagement(io.fabric8.patch.management.PatchManagement) PatchResult(io.fabric8.patch.management.PatchResult) BundleUpdate(io.fabric8.patch.management.BundleUpdate)

Example 23 with GitOperation

use of io.fabric8.git.internal.GitOperation in project fabric8 by jboss-fuse.

the class FabricPatchServiceImpl method synchronize.

@Override
public String synchronize(final boolean verbose) throws Exception {
    final String[] remoteUrl = new String[] { null };
    patchManagement.pushPatchInfo(verbose);
    GitOperation operation = new GitOperation() {

        @Override
        public Object call(Git git, GitContext context) throws Exception {
            ProfileRegistry registry = fabricService.adapt(ProfileRegistry.class);
            Map<String, String> properties = registry.getDataStoreProperties();
            String username;
            String password;
            if (properties != null && properties.containsKey("gitRemoteUser") && properties.containsKey("gitRemotePassword")) {
                username = properties.get("gitRemoteUser");
                password = properties.get("gitRemotePassword");
            } else {
                username = ZooKeeperUtils.getContainerLogin(runtimeProperties);
                password = ZooKeeperUtils.generateContainerToken(runtimeProperties, curator);
            }
            remoteUrl[0] = git.getRepository().getConfig().getString("remote", "origin", "url");
            Iterable<PushResult> results = git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password)).setPushTags().setPushAll().call();
            logPushResult(results, git.getRepository(), verbose);
            return null;
        }
    };
    try {
        gitDataStore.gitOperation(new GitContext(), operation, null);
    } catch (FabricException e) {
        if (e.getCause() != null && e.getCause() instanceof TransportException) {
            LOG.warn("Problem when synchronizing patch information: " + e.getCause().getMessage());
        } else {
            throw e;
        }
    }
    return remoteUrl[0];
}
Also used : UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) GitOperation(io.fabric8.git.internal.GitOperation) Git(org.eclipse.jgit.api.Git) GitContext(io.fabric8.api.GitContext) FabricException(io.fabric8.api.FabricException) ProfileRegistry(io.fabric8.api.ProfileRegistry) PushResult(org.eclipse.jgit.transport.PushResult) TransportException(org.eclipse.jgit.api.errors.TransportException)

Aggregations

Git (org.eclipse.jgit.api.Git)22 GitContext (io.fabric8.api.GitContext)21 LockHandle (io.fabric8.api.LockHandle)14 IOException (java.io.IOException)8 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)8 File (java.io.File)7 PersonIdent (org.eclipse.jgit.lib.PersonIdent)7 FabricException (io.fabric8.api.FabricException)3 Profile (io.fabric8.api.Profile)3 GitVersion (io.fabric8.api.commands.GitVersion)2 GitOperation (io.fabric8.git.internal.GitOperation)2 MalformedURLException (java.net.MalformedURLException)2 KeeperException (org.apache.zookeeper.KeeperException)2 RevCommit (org.eclipse.jgit.revwalk.RevCommit)2 DataStoreTemplate (io.fabric8.api.DataStoreTemplate)1 ProfileRegistry (io.fabric8.api.ProfileRegistry)1 ProfileService (io.fabric8.api.ProfileService)1 Version (io.fabric8.api.Version)1 GitVersions (io.fabric8.api.commands.GitVersions)1 GitProxyService (io.fabric8.git.GitProxyService)1