Skip to content

Commit

Permalink
Add rollback snapshot purging functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
arvchristos committed Oct 6, 2020
1 parent f6b14fc commit 22466c3
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 5 deletions.
3 changes: 3 additions & 0 deletions cerndb-sw-zkpolicy.spec
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ rm -rf /var/log/zkpolicy
%postun -p /bin/sh

%changelog
* Tue Oct 6 2020 Christos Arvanitis <[email protected]> 1.0.1-18
- Add rollback snapshot purge feature

* Fri Jul 18 2020 Christos Arvanitis <[email protected]> 1.0.1-8
- Add MIT License

Expand Down
2 changes: 1 addition & 1 deletion zkPolicy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ or submit itself to any jurisdiction.
<modelVersion>4.0.0</modelVersion>
<groupId>ch.cern</groupId>
<artifactId>cerndb-sw-zkpolicy</artifactId>
<version>1.0.1-17</version>
<version>1.0.1-18</version>
<name>${project.groupId}:${project.artifactId}</name>
<description>zkpolicy - ZooKeeper Policy Auditing Tool</description>
<url>https://github.com/cerndb/zkpolicy</url>
Expand Down
2 changes: 1 addition & 1 deletion zkPolicy/src/main/java/ch/cern/ZKPolicyCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

@Command(name = "zkpolicy", description = ZkPolicy.DESCRIPTION, versionProvider = ZKPolicyCli.PropertiesVersionProvider.class, subcommands = {
ZKQueryCli.class, ZKExportCli.class, ZKTreeCli.class, ZKEnforceCli.class, ZKAuditCli.class, ZKCheckCli.class,
ZKRollbackCli.class, HelpCommand.class }, mixinStandardHelpOptions = true)
ZKRollbackCli.class, ZKPurgeRollbackCli.class, HelpCommand.class }, mixinStandardHelpOptions = true)
/**
* Class that handles CLI arguments for the tool.
*/
Expand Down
7 changes: 7 additions & 0 deletions zkPolicy/src/main/java/ch/cern/ZKPolicyDefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ static class Rollback {
static final String DESCRIPTION = "Rollback ACLs to pre enforce state";
static final String INPUT_STATE_DESCRIPTION = "File with ZooKeeper tree state before enforcing";
}

static class PurgeRollback {
static final String DESCRIPTION = "Purge rollback snapshots, retaining a user defined number";
static final String RETAIN_CNT_DESCRIPTION = "Number of rollback snapshots to retain after purging";
static final String ROLLBACK_DIR_DESCRIPTION = "Rollback snapshots directory";
static final String ROLLBACK_DIR_DEFAULT = "/opt/zkpolicy/rollback";
}
}

static class Queries {
Expand Down
71 changes: 71 additions & 0 deletions zkPolicy/src/main/java/ch/cern/ZKPurgeRollback.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright © 2020, CERN
* This software is distributed under the terms of the MIT Licence,
* copied verbatim in the file 'LICENSE'. In applying this licence,
* CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/
package ch.cern;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Arrays;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.AccessLevel;


/**
* Purge rollback snapshots.
*/
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PROTECTED)
public class ZKPurgeRollback {
private static Logger logger = LogManager.getLogger(ZKPurgeRollback.class);

private File rollbackDir;
/**
* Purge rollback snapshots retaining retainCount number of snapshots.
*
* @param retainCount Number of snapshots to retain
* @throws IOException
*/
public void purgeRollback(Integer retainCount) throws IOException {
// Sort files lexicographically
final File[] files = rollbackDir.listFiles(new RollbackFilenameFilter());

if (files != null) {
Arrays.sort(files);
Integer totalSnapshotNum = files.length;
Integer cnt = 0;
for (File rollbackFile : files) {
if (totalSnapshotNum - cnt > retainCount) {
logger.info("Purging: " + rollbackFile.getPath());
if (!rollbackFile.delete()) {
logger.error("Failed to delete " + rollbackFile.getPath());
throw new IOException("Failed to delete " + rollbackFile.getPath());
}
} else {
break;
}
cnt = cnt + 1;
}
}
}

static class RollbackFilenameFilter implements FilenameFilter {
@Override
public boolean accept(final File dir, final String name) {
return name.matches("^ROLLBACK_STATE.*\\.yml");
}
}

}
41 changes: 41 additions & 0 deletions zkPolicy/src/main/java/ch/cern/ZKPurgeRollbackCli.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright © 2020, CERN
* This software is distributed under the terms of the MIT Licence,
* copied verbatim in the file 'LICENSE'. In applying this licence,
* CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/
package ch.cern;

import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import java.io.File;
import java.io.IOException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import ch.cern.ZKPolicyDefs.Cli.PurgeRollback;

@Command(name = "purge-rollback", aliases = {
"p" }, description = PurgeRollback.DESCRIPTION, helpCommand = true, mixinStandardHelpOptions = true)
public class ZKPurgeRollbackCli implements Runnable {
private static Logger logger = LogManager.getLogger(ZKPurgeRollbackCli.class);

@Option(names = { "-r", "--retain-count" }, required = true, description = PurgeRollback.RETAIN_CNT_DESCRIPTION)
Integer retainCount;

@Option(names = { "-d",
"--rollback-dir" }, required = false, defaultValue = PurgeRollback.ROLLBACK_DIR_DEFAULT, description = PurgeRollback.ROLLBACK_DIR_DESCRIPTION)
File rollbackDir;

@Override
public void run() {
ZKPurgeRollback zkPurge = new ZKPurgeRollback(rollbackDir);
try {
zkPurge.purgeRollback(this.retainCount);
} catch (IOException e) {
System.out.println(e.toString());
logger.error("Exception occurred!", e);
}
}
}
1 change: 0 additions & 1 deletion zkPolicy/src/test/java/ch/cern/ZKPolicyCliTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.io.ByteArrayOutputStream;
import java.io.File;
Expand Down
2 changes: 0 additions & 2 deletions zkPolicy/src/test/java/ch/cern/ZKQueryCliTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.curator.test.InstanceSpec;
import org.apache.curator.test.TestingServer;
Expand Down

0 comments on commit 22466c3

Please sign in to comment.