Search in sources :

Example 46 with GitContext

use of io.fabric8.api.GitContext 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 47 with GitContext

use of io.fabric8.api.GitContext 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

GitContext (io.fabric8.api.GitContext)41 Git (org.eclipse.jgit.api.Git)22 DefaultPullPushPolicy (io.fabric8.git.internal.DefaultPullPushPolicy)18 Test (org.junit.Test)18 LockHandle (io.fabric8.api.LockHandle)15 ArrayList (java.util.ArrayList)13 File (java.io.File)12 Ref (org.eclipse.jgit.lib.Ref)11 IOException (java.io.IOException)9 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)8 PushResult (org.eclipse.jgit.transport.PushResult)8 PersonIdent (org.eclipse.jgit.lib.PersonIdent)7 FabricException (io.fabric8.api.FabricException)5 Profile (io.fabric8.api.Profile)4 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 GitVersion (io.fabric8.api.commands.GitVersion)2 GitDataStore (io.fabric8.git.GitDataStore)2 GitService (io.fabric8.git.GitService)2 PushPolicyResult (io.fabric8.git.PullPushPolicy.PushPolicyResult)2 GitOperation (io.fabric8.git.internal.GitOperation)2