net.schmizz.sshj.xfer.FileSystemFile Java Examples

The following examples show how to use net.schmizz.sshj.xfer.FileSystemFile. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SshjHolder.java    From super-cloudops with Apache License 2.0 6 votes vote down vote up
/**
 * Transfer get file from remote host.(user sftp)
 * 
 * @param host
 * @param user
 * @param pemPrivateKey
 * @param localFile
 * @param remoteFilePath
 * @throws Exception
 */
@Override
public void scpGetFile(String host, String user, char[] pemPrivateKey, String password, File localFile, String remoteFilePath)
		throws Exception {
	notNull(localFile, "Transfer localFile must not be null.");
	hasText(remoteFilePath, "Transfer remoteDir can't empty.");
	log.debug("SSH2 transfer file from {} to {}@{}:{}", localFile.getAbsolutePath(), user, host, remoteFilePath);

	try {
		// Transfer get file.
		doScpTransfer(host, user, pemPrivateKey,password, scp -> {
			scp.download(remoteFilePath, new FileSystemFile(localFile));
		});

		log.debug("SCP get transfered: '{}' from '{}@{}:{}'", localFile.getAbsolutePath(), user, host, remoteFilePath);
	} catch (IOException e) {
		throw e;
	}

}
 
Example #2
Source File: SshjHolder.java    From super-cloudops with Apache License 2.0 6 votes vote down vote up
/**
 * Transfer put file to remote host directory.
 * 
 * @param host
 * @param user
 * @param pemPrivateKey
 * @param localFile
 * @param remoteDir
 * @throws Exception
 */
@Override
public void scpPutFile(String host, String user, char[] pemPrivateKey, String password, File localFile, String remoteDir) throws Exception {
	notNull(localFile, "Transfer localFile must not be null.");
	hasText(remoteDir, "Transfer remoteDir can't empty.");
	log.debug("SSH2 transfer file from {} to {}@{}:{}", localFile.getAbsolutePath(), user, host, remoteDir);

	try {
		// Transfer send file.
		doScpTransfer(host, user, pemPrivateKey,password, scp -> {
			// scp.upload(new FileSystemFile(localFile), remoteDir);
			scp.newSCPUploadClient().copy(new FileSystemFile(localFile), remoteDir, ScpCommandLine.EscapeMode.NoEscape);
		});

		log.debug("SCP put transfered: '{}' to '{}@{}:{}'", localFile.getAbsolutePath(), user, host, remoteDir);
	} catch (IOException e) {
		throw e;
	}

}
 
Example #3
Source File: ConfiguratorOnCreation.java    From roboconf-platform with Apache License 2.0 6 votes vote down vote up
/**
 * Updates the configuration file of an agent.
 * @param parameters
 * @param ssh
 * @param tmpDir
 * @param keyToNewValue
 * @throws IOException
 */
void updateAgentConfigurationFile(
		TargetHandlerParameters parameters,
		SSHClient ssh,
		File tmpDir,
		Map<String,String> keyToNewValue )
throws IOException {

	this.logger.fine( "Updating agent parameters on remote host..." );

	// Update the agent's configuration file
	String agentConfigDir = Utils.getValue( parameters.getTargetProperties(), SCP_AGENT_CONFIG_DIR, DEFAULT_SCP_AGENT_CONFIG_DIR );
	File localAgentConfig = new File( tmpDir, Constants.KARAF_CFG_FILE_AGENT );
	File remoteAgentConfig = new File( agentConfigDir, Constants.KARAF_CFG_FILE_AGENT );

	// Download remote agent config file...
	ssh.newSCPFileTransfer().download(remoteAgentConfig.getCanonicalPath(), new FileSystemFile(tmpDir));

	// Replace "parameters" property to point on the user data file...
	String config = Utils.readFileContent(localAgentConfig);
	config = Utils.updateProperties( config, keyToNewValue );
	Utils.writeStringInto(config, localAgentConfig);

	// Then upload agent config file back
	ssh.newSCPFileTransfer().upload(new FileSystemFile(localAgentConfig), agentConfigDir);
}
 
Example #4
Source File: SshjTool.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Override
public LocalDestFile create() throws Exception {
    sftp = acquire(sftpConnection);
    LocalDestFile localDestFile = new FileSystemFile(localFile);
    sftp.get(path, localDestFile);
    return localDestFile;
}
 
Example #5
Source File: ConfiguratorOnCreation.java    From roboconf-platform with Apache License 2.0 5 votes vote down vote up
Map<String,String> prepareConfiguration( TargetHandlerParameters parameters, SSHClient ssh, File tmpDir )
throws IOException {

	// Generate local user-data file
	String userData = UserDataHelpers.writeUserDataAsString(
			parameters.getMessagingProperties(),
			parameters.getDomain(),
			parameters.getApplicationName(),
			parameters.getScopedInstancePath());

	File userdataFile = new File(tmpDir, USER_DATA_FILE);
	Utils.writeStringInto(userData, userdataFile);

	// Then upload it
	this.logger.fine( "Uploading user data as a file..." );
	String agentConfigDir = Utils.getValue( parameters.getTargetProperties(), SCP_AGENT_CONFIG_DIR, DEFAULT_SCP_AGENT_CONFIG_DIR );
	ssh.newSCPFileTransfer().upload( new FileSystemFile(userdataFile), agentConfigDir);

	// Update the agent's configuration file
	Map<String,String> keyToNewValue = new HashMap<> ();
	File remoteAgentConfig = new File(agentConfigDir, userdataFile.getName());

	// Reset all the fields
	keyToNewValue.put( AGENT_APPLICATION_NAME, "" );
	keyToNewValue.put( AGENT_SCOPED_INSTANCE_PATH, "" );
	keyToNewValue.put( AGENT_DOMAIN, "" );

	// The location of the parameters must be an URL
	keyToNewValue.put( AGENT_PARAMETERS, remoteAgentConfig.toURI().toURL().toString());

	return keyToNewValue;
}
 
Example #6
Source File: ConfiguratorOnCreationTest.java    From roboconf-platform with Apache License 2.0 4 votes vote down vote up
@Test
public void testUpdateAgentConfigurationFile() throws Exception {

	// Prepare the mocks
	File tmpDir = this.folder.newFolder();
	File agentConfigurationFile = new File( tmpDir, Constants.KARAF_CFG_FILE_AGENT );

	Properties props = new Properties();
	props.setProperty( "a0", "c0" );
	props.setProperty( "a1", "c213" );
	props.setProperty( "a2", "c2" );
	props.setProperty( "a3", "c3" );
	props.setProperty( "a4", "c4" );
	props.setProperty( "a5", "c5" );
	Utils.writePropertiesFile( props, agentConfigurationFile );

	TargetHandlerParameters parameters = new TargetHandlerParameters()
			.targetProperties( new HashMap<String,String>( 0 ));

	final Map<String,String> keyToNewValue = new HashMap<> ();
	keyToNewValue.put( "a1", "b1" );
	keyToNewValue.put( "a2", "b2" );
	keyToNewValue.put( "a3", "b3" );

	SSHClient ssh = Mockito.mock( SSHClient.class );
	SCPFileTransfer scp = Mockito.mock( SCPFileTransfer.class );
	Mockito.when( ssh.newSCPFileTransfer()).thenReturn( scp );

	// Invoke the method
	EmbeddedHandler embedded = new EmbeddedHandler();
	embedded.karafData = this.folder.newFolder().getAbsolutePath();
	ConfiguratorOnCreation configurator = new ConfiguratorOnCreation( parameters, "ip", "machineId", embedded );
	configurator.updateAgentConfigurationFile( parameters, ssh, tmpDir, keyToNewValue );

	// Verify
	ArgumentCaptor<String> remotePathCaptor = ArgumentCaptor.forClass( String.class );
	ArgumentCaptor<FileSystemFile> fileCaptor = ArgumentCaptor.forClass( FileSystemFile.class );
	Mockito.verify( scp ).download( remotePathCaptor.capture(), fileCaptor.capture());

	Assert.assertEquals( tmpDir, fileCaptor.getValue().getFile());
	Assert.assertEquals(
			new File( DEFAULT_SCP_AGENT_CONFIG_DIR, Constants.KARAF_CFG_FILE_AGENT ).getAbsolutePath(),
			remotePathCaptor.getValue());

	// 1st: we upload the user data
	// 2nd: we reupload the same file than the one we downloaded
	ArgumentCaptor<String> remotePathCaptor2 = ArgumentCaptor.forClass( String.class );
	ArgumentCaptor<FileSystemFile> fileCaptor2 = ArgumentCaptor.forClass( FileSystemFile.class );
	Mockito.verify( scp ).upload( fileCaptor2.capture(), remotePathCaptor2.capture());

	Assert.assertEquals( agentConfigurationFile.getAbsolutePath(), fileCaptor2.getValue().getFile().getAbsolutePath());
	Assert.assertEquals( DEFAULT_SCP_AGENT_CONFIG_DIR, remotePathCaptor2.getValue());

	// And no additional call
	Mockito.verifyNoMoreInteractions( scp );
	Mockito.verify( ssh, Mockito.times( 2 )).newSCPFileTransfer();
	Mockito.verifyNoMoreInteractions( ssh );

	// Verify the properties were correctly updated in the file
	Properties readProps = Utils.readPropertiesFile( agentConfigurationFile );
	Assert.assertEquals( "c0", readProps.get( "a0" ));
	Assert.assertEquals( "b1", readProps.get( "a1" ));
	Assert.assertEquals( "b2", readProps.get( "a2" ));
	Assert.assertEquals( "b3", readProps.get( "a3" ));
	Assert.assertEquals( "c4", readProps.get( "a4" ));
	Assert.assertEquals( "c5", readProps.get( "a5" ));
	Assert.assertEquals( props.size(), readProps.size());

	// Prevent a compilation warning about leaks
	configurator.close();
}