mirror of https://github.com/micromata/borgbackup-butler.git

Kai Reinhard
15.07.2019 eeb5db743dfed0254074da61d70199add8ecaff9
Auto-install borg...
3 files modified
53 ■■■■ changed files
borgbutler-core/src/main/java/de/micromata/borgbutler/config/Configuration.java 7 ●●●● patch | view | raw | blame | history
borgbutler-server/src/main/java/de/micromata/borgbutler/server/BorgInstallation.java 39 ●●●● patch | view | raw | blame | history
borgbutler-server/src/test/java/de/micromata/borgbutler/server/BorgInstallationTest.java 7 ●●●●● patch | view | raw | blame | history
borgbutler-core/src/main/java/de/micromata/borgbutler/config/Configuration.java
@@ -22,7 +22,9 @@
    private static final String RESTORE_DIRNAME = "restore";
    @Getter
    private String binariesDownloadUrl = "https://github.com/borgbackup/borg/releases/download/1.1.8/";
    private String binariesDownloadVersion = "1.1.8";
    @Getter
    private String binariesDownloadUrl = "https://github.com/borgbackup/borg/releases/download/" + binariesDownloadVersion + "/";
    @Getter
    private String[][] borgBinaries = {
            {"freebsd64", "FreeBSD 64"},
@@ -30,6 +32,9 @@
            {"linux64", "Linux 64"},
            {"macosx64", "MacOS X 64"}};
    @Getter
    private String minimumRequiredBorgVersion = "1.1.8";
    @JsonIgnore
    @Setter(AccessLevel.PACKAGE)
    private File workingDir;
borgbutler-server/src/main/java/de/micromata/borgbutler/server/BorgInstallation.java
@@ -27,13 +27,28 @@
    public void initialize() {
        Configuration configuration = ConfigurationHandler.getConfiguration();
        String version = BorgCommands.version();
        if (version != null) {
            log.info("Using borg '" + configuration.getBorgCommand() + "', version: " + version);
        if (BorgCommands.version() != null) {
            return;
        }
        String[] binary = getBinary(RunningMode.getOSType());
        download(binary);
        File file = download(binary);
        configuration.setBorgCommand(file.getAbsolutePath());
        if (BorgCommands.version() != null) {
            return;
        }
        log.warn("No working borg version found. Please configure a borg version with minimal version '" + configuration.getMinimumRequiredBorgVersion() + "'.");
    }
    private String version(Configuration configuration) {
        String version = BorgCommands.version();
        if (version != null) {
            if (version.compareTo(configuration.getMinimumRequiredBorgVersion()) < 0) {
                log.info("Found borg version '" + version + "' is less than minimum required version '" + configuration.getMinimumRequiredBorgVersion() + "'.");
                return null;
            }
            log.info("Using borg '" + configuration.getBorgCommand() + "', version: " + version);
        }
        return version;
    }
    private String[] getBinary(RunningMode.OSType osType) {
@@ -70,7 +85,12 @@
    }
    private File download(String[] binary) {
        String url = ConfigurationHandler.getConfiguration().getBinariesDownloadUrl() + "borg-" + binary[0];
        File file = getBinaryFile(binary);
        if (file.exists()) {
            // File already downloaded, nothing to do.
            return file;
        }
        String url = ConfigurationHandler.getConfiguration().getBinariesDownloadUrl() + getDownloadFilename(binary);
        log.info("Trying to download borg binary '" + binary[0] + "' (" + binary[1] + ") from url: " + url + "...");
        HttpClientBuilder builder = HttpClients.custom()
                .setDefaultRequestConfig(RequestConfig.custom()
@@ -84,7 +104,6 @@
                throw new RuntimeException("Failed : HTTP error code : "
                        + response.getStatusLine().getStatusCode());
            }
            File file = new File(getBinaryDir(), "borg-" + binary[0]);
            FileUtils.copyInputStreamToFile(response.getEntity().getContent(), file);
            log.info("Downloaded: " + file.getAbsolutePath());
            file.setExecutable(true, false);
@@ -95,13 +114,17 @@
        }
    }
    private File getBinaryDir() {
    private File getBinaryFile(String[] binary) {
        File dir = new File(ConfigurationHandler.getInstance().getWorkingDir(), "bin");
        if (!dir.exists()) {
            log.info("Creating binary directory: " + dir.getAbsolutePath());
            dir.mkdirs();
        }
        return dir;
        return new File(dir, getDownloadFilename(binary) + "-" + ConfigurationHandler.getConfiguration().getBinariesDownloadVersion());
    }
    private String getDownloadFilename(String[] binary) {
        return "borg-" + binary[0];
    }
    private BorgInstallation() {
borgbutler-server/src/test/java/de/micromata/borgbutler/server/BorgInstallationTest.java
@@ -21,9 +21,10 @@
    @Test
    void downloadTest() {
        checkDownload(RunningMode.OSType.LINUX, "borg-linux64");
        checkDownload(RunningMode.OSType.MAC_OS, "borg-macosx64");
        checkDownload(RunningMode.OSType.FREEBSD, "borg-freebsd64");
        String version = ConfigurationHandler.getConfiguration().getBinariesDownloadVersion();
        checkDownload(RunningMode.OSType.LINUX, "borg-linux64-" + version);
        checkDownload(RunningMode.OSType.MAC_OS, "borg-macosx64-" + version);
        checkDownload(RunningMode.OSType.FREEBSD, "borg-freebsd64-" + version);
        assertNull(BorgInstallation.getInstance().download(RunningMode.OSType.WINDOWS));
    }