-
Notifications
You must be signed in to change notification settings - Fork 748
Expand file tree
/
Copy pathCasCBackup.java
More file actions
86 lines (75 loc) · 3.21 KB
/
CasCBackup.java
File metadata and controls
86 lines (75 loc) · 3.21 KB
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
package io.jenkins.plugins.casc.auto;
import hudson.Extension;
import hudson.XmlFile;
import hudson.model.Saveable;
import hudson.model.listeners.SaveableListener;
import io.jenkins.plugins.casc.ConfigurationAsCode;
import io.jenkins.plugins.casc.ConfigurationContext;
import io.jenkins.plugins.casc.impl.DefaultConfiguratorRegistry;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.inject.Inject;
import javax.servlet.ServletContext;
import jenkins.model.GlobalConfiguration;
import jenkins.model.Jenkins;
@Extension(ordinal = 100)
public class CasCBackup extends SaveableListener {
private static final Logger LOGGER = Logger.getLogger(CasCBackup.class.getName());
private static final String DEFAULT_JENKINS_YAML_PATH = "jenkins.yaml";
private static final String cascDirectory = "/WEB-INF/" + DEFAULT_JENKINS_YAML_PATH + ".d/";
@Inject
private DefaultConfiguratorRegistry registry;
@Override
public void onChange(Saveable o, XmlFile file) {
ConfigurationContext context = new ConfigurationContext(registry);
if (!context.isEnableBackup()) {
return;
}
// only take care of the configuration which controlled by casc
if (!(o instanceof GlobalConfiguration)) {
return;
}
ByteArrayOutputStream buf = new ByteArrayOutputStream();
try {
ConfigurationAsCode.get().export(buf);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "error happen when exporting the whole config into a YAML", e);
return;
}
final ServletContext servletContext = Jenkins.getInstance().servletContext;
try {
URL bundled = servletContext.getResource(cascDirectory);
if (bundled != null) {
File cascDir = new File(bundled.getFile());
boolean hasDir = false;
if(!cascDir.exists()) {
hasDir = cascDir.mkdirs();
} else if (cascDir.isFile()) {
LOGGER.severe(String.format("%s is a regular file", cascDir));
} else {
hasDir = true;
}
if(hasDir) {
File backupFile = new File(cascDir, "user.yaml");
try (OutputStream writer = new FileOutputStream(backupFile)) {
writer.write(buf.toByteArray());
LOGGER.fine(String.format("backup file was saved, %s", backupFile.getAbsolutePath()));
} catch (IOException e) {
LOGGER.log(Level.WARNING, String.format("error happen when saving %s", backupFile.getAbsolutePath()), e);
}
} else {
LOGGER.severe(String.format("cannot create casc backup directory %s", cascDir));
}
}
} catch (MalformedURLException e) {
LOGGER.log(Level.WARNING, String.format("error happen when finding %s", cascDirectory), e);
}
}
}