Skip to content
Snippets Groups Projects
Commit 28a97af7 authored by tpylak's avatar tpylak
Browse files

DMV-5 last touches on ssh tunelling, first unit tests

SVN: 6386
parent 43b72c92
No related branches found
No related tags found
No related merge requests found
/*
* Copyright 2008 ETH Zuerich, CISD
*
* 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 ch.systemsx.cisd.common.highwatermark;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import ch.systemsx.cisd.common.Constants;
import ch.systemsx.cisd.common.highwatermark.HighwaterMarkWatcher.IFreeSpaceProvider;
import ch.systemsx.cisd.common.logging.LogCategory;
import ch.systemsx.cisd.common.logging.LogFactory;
import ch.systemsx.cisd.common.process.ProcessExecutionHelper;
import ch.systemsx.cisd.common.process.ProcessResult;
/**
* An <code>IFreeSpaceProvider</code> implementation for computing the free space on a remote
* computer.
*
* @author Christian Ribeaud
*/
public final class RemoteFreeSpaceProvider implements IFreeSpaceProvider
{
private static final char SPACE = ' ';
private static final String DF_COMMAND_TEMPLATE = "df -k %s";
private static final Logger machineLog =
LogFactory.getLogger(LogCategory.MACHINE, RemoteFreeSpaceProvider.class);
private static final Logger operationLog =
LogFactory.getLogger(LogCategory.OPERATION, RemoteFreeSpaceProvider.class);
private final File sshExecutable;
private final String host;
private final long millisToWaitForCompletion = Constants.MILLIS_TO_WAIT_BEFORE_TIMEOUT;
public RemoteFreeSpaceProvider(final String host, final File sshExecutable)
{
assert host != null : "Unspecified host";
assert sshExecutable != null : "Unspecified ssh executable";
this.host = host;
this.sshExecutable = sshExecutable;
}
private final static long parseKbytes(final String freeSpaceInKb, final String dfCommand)
throws IOException
{
try
{
final long kBytes = Long.parseLong(freeSpaceInKb);
if (kBytes < 0)
{
throw new IOException(String.format(
"Command line '%s' did not find free space in response.", dfCommand));
}
return kBytes;
} catch (final NumberFormatException ex)
{
throw new IOException(String.format(
"Command line '%s' did not return numeric data as expected.", dfCommand));
}
}
//
// IFreeSpaceProvider
//
public final long freeSpaceKb(final File file) throws IOException
{
assert file != null : "Unspecified remote file.";
final String path = file.getPath();
assert StringUtils.isNotEmpty(path) : "Empty path.";
final String dfCommand = String.format(DF_COMMAND_TEMPLATE, path);
final List<String> command =
ProcessExecutionHelper.createSshCommand(dfCommand, sshExecutable, host);
final ProcessResult processResult =
ProcessExecutionHelper.run(command, millisToWaitForCompletion, operationLog,
machineLog);
processResult.log();
final List<String> processOutput = processResult.getProcessOutput();
final String commandLine = StringUtils.join(processResult.getCommandLine(), SPACE);
String spaceOutputKb = tryParseFreeSpaceOutput(processOutput);
if (spaceOutputKb == null)
{
throw new IOException(String.format(
"Command line '%s' did not return info as expected. Response was '%s'",
commandLine, processOutput));
}
return parseKbytes(spaceOutputKb, dfCommand);
}
// NOTE sometimes the line with results breaks if the value in the column is longer then the
// header. So we cannot take the 3rd token from the second line, we have to count tokens in all
// the lines which appear
private static String tryParseFreeSpaceOutput(final List<String> outputLines)
{
int line = 1;
int seenTokens = 0;
while (line < outputLines.size())
{
final String output = outputLines.get(line);
final String[] split = StringUtils.split(output, SPACE);
if (seenTokens + split.length >= 4)
{
// The column 'avail' (3th column) interests us.
return split[3 - seenTokens];
}
seenTokens += split.length;
line++;
}
return null;
}
}
\ No newline at end of file
/*
* Copyright 2008 ETH Zuerich, CISD
*
* 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 ch.systemsx.cisd.common.highwatermark;
import static org.testng.AssertJUnit.assertNotNull;
import java.io.File;
import java.io.IOException;
import org.testng.annotations.Test;
import ch.systemsx.cisd.common.utilities.OSUtilities;
/**
* Test cases for {@link RemoteFreeSpaceProvider}.
*
* @author Christian Ribeaud
*/
public final class RemoteFreeSpaceProviderTest
{
@Test(groups = "broken")
public final void testFreeSpaceKb() throws IOException
{
final File sshExecutable = OSUtilities.findExecutable("ssh");
assertNotNull(sshExecutable);
final RemoteFreeSpaceProvider freeSpaceProvider =
new RemoteFreeSpaceProvider("sprint-ob", sshExecutable);
System.out.println(freeSpaceProvider.freeSpaceKb(new File("/")));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment