view mercurial-agent/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialAgentSideVcsSupport.java @ 179:2b0eafc9bcf5 remote-run/dmitry.neverov/final_agent_caches

Mercurial mirrors on agent
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Tue, 01 Mar 2011 16:47:00 +0300
parents 5198b02fc5e9
children 8ec94ce51d84
line wrap: on
line source
/*
 * Copyright 2000-2011 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jetbrains.buildServer.buildTriggers.vcs.mercurial;

import jetbrains.buildServer.agent.AgentRunningBuild;
import jetbrains.buildServer.agent.BuildAgentConfiguration;
import jetbrains.buildServer.agent.BuildProgressLogger;
import jetbrains.buildServer.agent.vcs.AgentVcsSupport;
import jetbrains.buildServer.agent.vcs.IncludeRuleUpdater;
import jetbrains.buildServer.agent.vcs.UpdateByIncludeRules2;
import jetbrains.buildServer.agent.vcs.UpdatePolicy;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.*;
import jetbrains.buildServer.util.FileUtil;
import jetbrains.buildServer.vcs.CheckoutRules;
import jetbrains.buildServer.vcs.IncludeRule;
import jetbrains.buildServer.vcs.VcsException;
import jetbrains.buildServer.vcs.VcsRoot;
import org.jetbrains.annotations.NotNull;

import java.io.File;

public class MercurialAgentSideVcsSupport extends AgentVcsSupport implements UpdateByIncludeRules2 {

  private final MirrorManager myMirrorManager;

  public MercurialAgentSideVcsSupport(BuildAgentConfiguration agentConfiguration) {
    myMirrorManager = new MirrorManager(agentConfiguration.getCacheDirectory("mercurial"));
  }

  public IncludeRuleUpdater getUpdater(@NotNull final VcsRoot vcsRoot, @NotNull final CheckoutRules checkoutRules, @NotNull final String toVersion, @NotNull final File checkoutDirectory, @NotNull final AgentRunningBuild build, boolean cleanCheckoutRequested) throws VcsException {
    final BuildProgressLogger logger = build.getBuildLogger();
    final boolean useLocalMirrors = isUseLocalMirrors(build);
    return new IncludeRuleUpdater() {
      public void process(@NotNull final IncludeRule includeRule, @NotNull final File workingDir) throws VcsException {
        checkRuleIsValid(includeRule);
        Settings settings = new Settings(vcsRoot);
        if (useLocalMirrors) updateLocalMirror(vcsRoot, logger);
        if (settings.isValidRepository(workingDir)) {
          updateRepository(workingDir, settings, logger, useLocalMirrors);
        } else {
          cloneRepository(workingDir, settings, logger, useLocalMirrors);
        }
        updateWorkingDir(settings, workingDir, toVersion, logger);
      }

      public void dispose() throws VcsException {
      }
    };
  }

  @NotNull
  @Override
  public String getName() {
    return Constants.VCS_NAME;
  }

  @NotNull
  @Override
  public UpdatePolicy getUpdatePolicy() {
    return this;
  }

  private boolean isUseLocalMirrors(AgentRunningBuild build) {
    String value = build.getSharedConfigParameters().get("teamcity.hg.use.local.mirrors");
    return "true".equals(value);
  }

  private void cloneRepository(File workingDir, Settings settings, BuildProgressLogger logger, boolean useLocalMirrors) throws VcsException {
    String message = "No repository in working directory found, start cloning from " + (useLocalMirrors ? "local mirror" : "remote repository");
    logger.message(message);
    if (useLocalMirrors) {
      cloneFromLocalMirror(settings, workingDir);
    } else {
      cloneFromRemote(settings, workingDir);
    }
    logger.message("Repository successfully cloned to working directory: " + workingDir.getAbsolutePath());
  }

  private void updateRepository(File workingDir, Settings settings, BuildProgressLogger logger, boolean useLocalMirrors) throws VcsException {
    if (useLocalMirrors) {
      if (isClonedFromLocalMirror(settings, workingDir)) {
        logger.message("Repository in working directory found, start pulling changes");
        new PullCommand(settings, workingDir).execute();
        logger.message("Changes successfully pulled");
      } else {
        logger.message("Repository in working directory is cloned from remote repository, clone it from local mirror");
        FileUtil.delete(workingDir);
        cloneFromLocalMirror(settings, workingDir);
      }
    } else {
      if (isClonedFromLocalMirror(settings, workingDir)) {
        logger.message("Repository in working directory is cloned from local mirror, clone it from remote repository");
        FileUtil.delete(workingDir);
        cloneFromRemote(settings, workingDir);
      } else {
        logger.message("Repository in working directory found, start pulling changes");
        new PullCommand(settings, workingDir).execute();
        logger.message("Changes successfully pulled");
      }
    }
  }

  private void updateLocalMirror(VcsRoot root, BuildProgressLogger logger) throws VcsException {
    Settings settings = new Settings(root);
    File mirrorDir = myMirrorManager.getMirrorDir(settings.getRepositoryUrl());
    if (Settings.isValidRepository(mirrorDir)) {
      logger.message("Start pulling changes to local mirror at " + mirrorDir);
      new PullCommand(settings, mirrorDir).execute();
      logger.message("Local mirror changes successfully pulled");
    } else {
      logger.message("No local mirror found for " + settings.getRepositoryUrl() + ", create mirror at " + mirrorDir.getAbsolutePath());
      logger.message("Clone local mirror at " + mirrorDir);
      cloneRepository(settings, mirrorDir, settings.getRepositoryUrl());
      logger.message("Local mirror successfully cloned to " + mirrorDir);
    }
  }

  private void updateWorkingDir(final Settings settings, File workingDir, final String version, final BuildProgressLogger logger) throws VcsException {
    logger.message("Updating working directory from the local repository copy");
    UpdateCommand uc = new UpdateCommand(settings, workingDir);
    ChangeSet cs = new ChangeSet(version);
    uc.setToId(cs.getId());
    uc.execute();
    logger.message("Working directory updated successfully");
  }

  private void cloneFromLocalMirror(final Settings settings, File workingDir) throws VcsException {
    File mirrorDir = myMirrorManager.getMirrorDir(settings.getRepositoryUrl());
    try {
      cloneRepository(settings, workingDir, mirrorDir.getCanonicalPath());
    } catch (Exception e) {
      throw new VcsException("Failed to clone from local mirror at " + mirrorDir.getAbsolutePath(), e);
    }
  }

  private void cloneFromRemote(final Settings settings, File workingDir) throws VcsException {
    try {
      cloneRepository(settings, workingDir, settings.getRepositoryUrl());
    } catch (Exception e) {
      throw new VcsException("Failed to clone from remote repository " + settings.getRepositoryUrl(), e);
    }
  }

  private void cloneRepository(final Settings settings, File workingDir, String url) throws VcsException {
    CloneCommand cc = new CloneCommand(settings, workingDir);
    cc.setRepository(url);
    cc.setUpdateWorkingDir(false);
    cc.execute();
  }

  private void checkRuleIsValid(IncludeRule includeRule) throws VcsException {
    if (includeRule.getTo() != null && includeRule.getTo().length() > 0) {
      if (!".".equals(includeRule.getFrom()) && includeRule.getFrom().length() != 0) {
        throw new VcsException("Invalid include rule: " + includeRule.toString() + ", Mercurial plugin supports mapping of the form: +:.=>dir only.");
      }
    }
  }

  public boolean isClonedFromLocalMirror(Settings settings, File workingDir) {
    try {
      File mirrorDir = myMirrorManager.getMirrorDir(settings.getRepositoryUrl());
      File hgrc = new File(workingDir, ".hg" + File.separator + "hgrc");
      String config = FileUtil.readText(hgrc);
      return config.contains("default = " + mirrorDir.getCanonicalPath());
    } catch (Exception e) {
      return false;
    }
  }
}