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

Kai Reinhard
31.45.2019 e24dcccdda9ae085534e0f16814486eb155aaa59
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package de.micromata.borgbutler;
 
import de.micromata.borgbutler.config.BorgRepoConfig;
import de.micromata.borgbutler.config.ConfigurationHandler;
import de.micromata.borgbutler.data.Archive;
import de.micromata.borgbutler.demo.DemoRepos;
import de.micromata.borgbutler.jobs.AbstractCommandLineJob;
import de.micromata.borgbutler.jobs.JobResult;
import de.micromata.borgbutler.json.JsonUtils;
import de.micromata.borgbutler.json.borg.ProgressInfo;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.environment.EnvironmentUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.IOException;
import java.util.Map;
 
/**
 * A queue is important because Borg doesn't support parallel calls for one repository.
 * For each repository one single queue is allocated.
 */
public class BorgJob<T> extends AbstractCommandLineJob implements Cloneable {
    private Logger log = LoggerFactory.getLogger(BorgJob.class);
    private BorgCommand command;
    /**
     * Some jobs may store here the result of the command (e. g. {@link BorgCommands#listArchiveContent(BorgRepoConfig, Archive)}).
     */
    protected T payload;
 
    private ProgressInfo progressInfo;
 
    public BorgJob(BorgCommand command) {
        this.command = command;
        setWorkingDirectory(command.getWorkingDir());
        setDescription(command.getDescription());
    }
 
    private BorgJob() {
    }
 
    @Override
    protected CommandLine buildCommandLine() {
        if (command == null) {
            return null;
        }
        String borgCommand = ConfigurationHandler.getConfiguration().getBorgCommand();
        if (StringUtils.isBlank(borgCommand)) {
            log.error("Can't run empty borg command.");
            return null;
        }
        CommandLine commandLine = new CommandLine(borgCommand);
        commandLine.addArgument(command.getCommand());
        if (command.getParams() != null) {
            for (String param : command.getParams()) {
                if (param != null)
                    commandLine.addArgument(param);
            }
        }
        if (command.getRepoArchive() != null) {
            commandLine.addArgument(command.getRepoArchive());
        }
        if (command.getArgs() != null) {
            for (String arg : command.getArgs()) {
                if (arg != null)
                    commandLine.addArgument(arg);
            }
        }
        return commandLine;
    }
 
    public void processStdErrLine(String line, int level) {
        if (StringUtils.startsWith(line, "{\"message")) {
            ProgressInfo message = JsonUtils.fromJson(ProgressInfo.class, line);
            if (message != null) {
                progressInfo = message;
                return;
            }
        }
        super.processStdErrLine(line, level);
    }
 
    @Override
    protected Map<String, String> getEnvironment() throws IOException {
        BorgRepoConfig repoConfig = command.getRepoConfig();
        if (repoConfig == null) {
            return null;
        }
        Map<String, String> env = EnvironmentUtils.getProcEnvironment();
        String[] variables = repoConfig.getEnvironmentVariables(true);
        for (String variable : variables) {
            // For MacOS BORG_PASSCOMMAND="security find-generic-password -a $USER -s borg-passphrase -w"
            String environmentVariable = variable.replace("$USER", System.getProperty("user.name"));
            addEnvironmentVariable(env, environmentVariable);
        }
        return env;
    }
 
    @Override
    public JobResult<String> execute() {
        if (command.getRepoConfig() != null && DemoRepos.isDemo(command.getRepoConfig().getRepo())) {
            return DemoRepos.execute(this);
        }
        return super.execute();
    }
 
    @Override
    public BorgJob<?> clone() {
        BorgJob<?> clone = new BorgJob<>();
        if (command != null) {
            // Needed for getting environment variables: JsonJob of borgbutler-server.
            clone.command = new BorgCommand().setRepoConfig(command.getRepoConfig());
        }
        clone.setUniqueJobNumber(getUniqueJobNumber());
        clone.setTitle(getTitle());
        clone.setExecuteStarted(isExecuteStarted());
        clone.setCommandLineAsString(getCommandLineAsString());
        clone.setCancellationRequested(isCancellationRequested());
        clone.setStatus(getStatus());
        clone.setWorkingDirectory(getWorkingDirectory());
        clone.setDescription(getDescription());
        if (progressInfo != null) {
            clone.setProgressInfo(progressInfo.clone());
        }
        clone.setCreateTime(getCreateTime());
        clone.setStartTime(getStartTime());
        clone.setStopTime(getStopTime());
        return clone;
    }
 
    @Override
    public void cleanUp() {
        super.cleanUp();
        payload = null;
    }
 
    public BorgCommand getCommand() {
        return this.command;
    }
 
    public T getPayload() {
        return this.payload;
    }
 
    public ProgressInfo getProgressInfo() {
        return this.progressInfo;
    }
 
    protected BorgJob<T> setProgressInfo(ProgressInfo progressInfo) {
        this.progressInfo = progressInfo;
        return this;
    }
}