Skip to content

Commit

Permalink
Merge pull request #2867 from subutai-io/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Abdysamat Mamutov authored Jun 9, 2019
2 parents a47a5dc + 8bb498c commit 7419315
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
public class BackupStateHandler extends StateHandler
{
private final String path = "/rest/v1/environments/%s/peers/%s/backups";
private final String DEFAULT_PARTITION = "all";
static final String DEFAULT_PARTITION = "all";

private ExecutorService executor = Executors.newFixedThreadPool( 5 );

Expand Down Expand Up @@ -72,8 +72,8 @@ private void processBackupCommands( BackupCommandsDto backupCommandsDto, String
}
catch ( ResourceHostException | PeerException e )
{
throw new ResourceHostException(
"Failed to create snapshot to take backup from: " + e.getMessage() );
throw new ResourceHostException( "Failed to create snapshot to take backup from: " + e.getMessage(),
e );
}

// do backup
Expand All @@ -86,7 +86,7 @@ private void processBackupCommands( BackupCommandsDto backupCommandsDto, String
}
catch ( ResourceHostException e )
{
throw new ResourceHostException( "Failed to export container file system: " + e.getMessage() );
throw new ResourceHostException( "Failed to export container file system: " + e.getMessage(), e );
}

// encrypt backup file
Expand All @@ -99,7 +99,7 @@ private void processBackupCommands( BackupCommandsDto backupCommandsDto, String
}
catch ( ResourceHostException e )
{
throw new ResourceHostException( "Failed to encrypt backup file: " + e.getMessage() );
throw new ResourceHostException( "Failed to encrypt backup file: " + e.getMessage(), e );
}

// upload backup to CDN
Expand All @@ -112,7 +112,7 @@ private void processBackupCommands( BackupCommandsDto backupCommandsDto, String
}
catch ( ResourceHostException e )
{
throw new ResourceHostException( "Failed to upload backup to CDN: " + e.getMessage() );
throw new ResourceHostException( "Failed to upload backup to CDN: " + e.getMessage(), e );
}

// cleanup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,16 @@ protected Object doHandle( final EnvironmentPeerDto peerDto ) throws BazaarManag
{
removeFileAsync( rh, filePath );
}

log.debug( "9. Deleting snapshots by restored container..." );
for ( final String filePath : backupFilesByContainers.get( restoreCmd.getContainerHostname() ) )
{
String label = extractSnapshotLabel( filePath, restoreCmd.getContainerOldName() );
if ( StringUtils.isNotBlank( label ) )
{
removeSnapshotAsync( containerHost, label );
}
}
}
catch ( ResourceHostException | PeerException e )
{
Expand Down Expand Up @@ -359,6 +369,47 @@ public void run()
}


private String extractSnapshotLabel( String backupFilePath, String oldContainerName )
{
String label = null;
try
{
String s = StringUtils.substringBetween( backupFilePath, oldContainerName, ".tar.gz" );
label = StringUtils.substringAfterLast( s, "_" );
}
catch ( Exception e )
{
log.error( "Couldn't extract snapshot label from file '{}' and container '{}': {}", backupFilePath,
oldContainerName, e.getMessage() );
}

return label;
}


private void removeSnapshotAsync( final ContainerHost containerHost, final String snapshotLabel )
{
executor.execute( new Runnable()
{
@Override
public void run()
{
log.debug( "Removing snapshot {} of container {}", snapshotLabel, containerHost.getContainerName() );
try
{
ctx.localPeer.removeContainerSnapshot( containerHost.getContainerId(),
BackupStateHandler.DEFAULT_PARTITION, snapshotLabel );
}
catch ( PeerException e )
{
log.error( "Failed to remove snapshot {} by container {}", snapshotLabel,
containerHost.getContainerName(), e );
}
}
} );
}


/**
* Copy of {@link io.subutai.common.environment.CloneContainerTask#generateContainerName()}
*/
Expand Down

0 comments on commit 7419315

Please sign in to comment.