use of jetbrains.buildServer.vcs.VcsException in project teamcity-git by JetBrains.
the class LogCommandImpl method call.
@Nullable
public String call() {
try {
GitCommandLine cmd = getCmd();
cmd.addParameters("log");
if (myCommitsNumber != 0)
cmd.addParameter("-n" + myCommitsNumber);
if (myFormat != null)
cmd.addParameter("--pretty=format:" + myFormat);
cmd.addParameter(myStartPoint);
cmd.addParameter("--");
return CommandUtil.runCommand(cmd.stdErrExpected(false)).getStdout().trim();
} catch (VcsException e) {
return null;
}
}
use of jetbrains.buildServer.vcs.VcsException in project teamcity-git by JetBrains.
the class GitCommandLine method getPrivateKey.
@Nullable
private File getPrivateKey(@NotNull AuthSettings authSettings) throws VcsException {
File privateKey = null;
final boolean useSshAskPass = myCtx.isUseSshAskPass();
try {
switch(authSettings.getAuthMethod()) {
case TEAMCITY_SSH_KEY:
privateKey = getUploadedPrivateKey(authSettings);
break;
case PRIVATE_KEY_FILE:
final String keyPath = authSettings.getPrivateKeyFilePath();
if (StringUtil.isEmpty(keyPath)) {
throw new VcsException("Authentication method is \"" + AuthenticationMethod.PRIVATE_KEY_FILE.uiName() + "\", but no private key path provided");
}
final File finalPrivateKey = createTmpKeyFile();
addPostAction(() -> FileUtil.delete(finalPrivateKey));
privateKey = finalPrivateKey;
FileUtil.copy(new File(keyPath), privateKey);
break;
case PRIVATE_KEY_DEFAULT:
// we do not decrypt default ssh keys
return null;
default:
return null;
}
final String passphrase = authSettings.getPassphrase();
if (useSshAskPass) {
withAskPassScript(passphrase, askPassPath -> {
addEnvParam("SSH_ASKPASS", askPassPath);
addEnvParam("DISPLAY", ":0.0");
});
} else {
final KeyPair keyPair = KeyPair.load(new JSch(), privateKey.getAbsolutePath());
OutputStream out = null;
try {
out = new BufferedOutputStream(new FileOutputStream(privateKey));
if (keyPair.isEncrypted() && !keyPair.decrypt(passphrase)) {
throw new VcsException("Wrong SSH key passphrase");
}
keyPair.writePrivateKey(out, null);
} finally {
FileUtil.close(out);
}
}
// set permissions to 600, without that ssh client rejects the key on *nix
privateKey.setReadable(false, false);
privateKey.setReadable(true, true);
privateKey.setWritable(false, false);
privateKey.setWritable(true, true);
return privateKey;
} catch (Exception e) {
if (privateKey != null)
FileUtil.delete(privateKey);
if (e instanceof VcsException)
throw (VcsException) e;
throw new VcsException(e);
}
}
use of jetbrains.buildServer.vcs.VcsException in project teamcity-git by JetBrains.
the class CommandUtil method isRecoverable.
public static boolean isRecoverable(@NotNull Exception e, AuthSettings authSettings, int attempt, int maxAttempts) {
boolean attemptsLeft = attempt < maxAttempts;
if (e instanceof ProcessTimeoutException || e instanceof GitExecTimeout)
return attemptsLeft;
if (!(e instanceof VcsException))
return false;
final VcsException ve = (VcsException) e;
if (isTimeoutError(ve) || isConnectionRefused(ve) || isConnectionReset(ve))
return attemptsLeft;
if (isCanceledError(ve))
return false;
if (e instanceof GitIndexCorruptedException)
return false;
if (authSettings.doesTokenNeedRefresh() && attempt == 1)
return true;
return attemptsLeft && !isRemoteAccessError(ve);
}
use of jetbrains.buildServer.vcs.VcsException in project teamcity-git by JetBrains.
the class CommandUtil method commandFailed.
@SuppressWarnings({ "ThrowableResultOfMethodCallIgnored" })
private static void commandFailed(final String cmdName, final ExecResult res) throws VcsException {
Throwable exception = res.getException();
String stderr = res.getStderr().trim();
String stdout = res.getStdout().trim();
int exitCode = res.getExitCode();
String message = cmdName + " command failed." + (exitCode != 0 ? "\nexit code: " + exitCode : "") + (!StringUtil.isEmpty(stdout) ? "\n" + "stdout: " + stdout : "") + (!StringUtil.isEmpty(stderr) ? "\n" + "stderr: " + stderr : "");
if (exception != null) {
message += "\nexception: ";
message += exception.getClass().getName();
String exceptionMessage = exception.getMessage();
if (!StringUtil.isEmpty(exceptionMessage))
message += ": " + exceptionMessage;
}
if (exception != null && isImportant(exception)) {
Writer stackWriter = new StringWriter();
exception.printStackTrace(new PrintWriter(stackWriter));
message += "\n" + stackWriter;
}
if (exception != null)
throw new VcsException(message, exception);
throw new VcsException(message);
}
use of jetbrains.buildServer.vcs.VcsException in project teamcity-git by JetBrains.
the class GitCommandLine method getUploadedPrivateKey.
@NotNull
private File getUploadedPrivateKey(@NotNull AuthSettings authSettings) throws Exception {
final String keyId = authSettings.getTeamCitySshKeyId();
final VcsRoot root = authSettings.getRoot();
if (keyId == null || root == null || mySshKeyManager == null) {
final String msg = "Failed to locate uploaded SSH key " + keyId + " for vcs root %s " + (mySshKeyManager == null ? ": null ssh key manager" : "");
Loggers.VCS.warn(String.format(msg, LogUtil.describe(root)));
throw new VcsException(String.format(msg, root == null ? null : root.getName()));
}
final TeamCitySshKey key = mySshKeyManager.getKey(root);
if (key == null) {
throw new VcsException("Failed to locate uploaded SSH key " + keyId + " for vcs root " + root.getName());
}
final File privateKey = createTmpKeyFile();
addPostAction(() -> FileUtil.delete(privateKey));
FileUtil.writeFileAndReportErrors(privateKey, new String(key.getPrivateKey()));
return privateKey;
}
Aggregations