-
Notifications
You must be signed in to change notification settings - Fork 748
Expand file tree
/
Copy pathPatchConfig.java
More file actions
135 lines (113 loc) · 5.2 KB
/
PatchConfig.java
File metadata and controls
135 lines (113 loc) · 5.2 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
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
package io.jenkins.plugins.casc.auto;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.flipkart.zjsonpatch.JsonDiff;
import com.flipkart.zjsonpatch.JsonPatch;
import hudson.init.InitMilestone;
import hudson.init.Initializer;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import jenkins.model.Jenkins;
import org.apache.commons.io.IOUtils;
/**
* Apply the patch between two versions of the initial config files
*/
public class PatchConfig {
private static final Logger LOGGER = Logger.getLogger(CasCBackup.class.getName());
final static String DEFAULT_JENKINS_YAML_PATH = "jenkins.yaml";
final static String cascFile = "/WEB-INF/" + DEFAULT_JENKINS_YAML_PATH;
final static String cascDirectory = "/WEB-INF/" + DEFAULT_JENKINS_YAML_PATH + ".d/";
final static String cascUserConfigFile = "user.yaml";
@Initializer(after= InitMilestone.STARTED, fatal=false)
public static void patchConfig() {
LOGGER.fine("start to calculate the patch of casc");
URL newSystemConfig = findConfig("/" + DEFAULT_JENKINS_YAML_PATH);
URL systemConfig = findConfig(cascFile);
URL userConfig = findConfig(cascDirectory + cascUserConfigFile);
URL userConfigDir = findConfig(cascDirectory);
if (newSystemConfig == null || userConfigDir == null) {
LOGGER.warning("no need to upgrade the configuration of Jenkins");
return;
}
JsonNode patch = null;
if (systemConfig != null && userConfig != null) {
ObjectMapper objectMapper = new ObjectMapper();
try {
JsonNode source = objectMapper.readTree(yamlToJson(systemConfig.openStream()));
JsonNode target = objectMapper.readTree(yamlToJson(userConfig.openStream()));
patch = JsonDiff.asJson(source, target);
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "error happen when calculate the patch", e);
return;
}
try {
// give systemConfig a real path
PatchConfig.copyAndDelSrc(newSystemConfig, systemConfig);
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "error happen when copy the new system config", e);
return;
}
}
if (patch != null) {
File userYamlFile = new File(userConfigDir.getFile(), "user.yaml");
File userJSONFile = new File(userConfigDir.getFile(), "user.json");
try (InputStream newSystemInput = systemConfig.openStream();
OutputStream userFileOutput = new FileOutputStream(userYamlFile);
OutputStream patchFileOutput = new FileOutputStream(userJSONFile)){
ObjectMapper jsonReader = new ObjectMapper();
JsonNode target = JsonPatch.apply(patch, jsonReader.readTree(yamlToJson(newSystemInput)));
String userYaml = jsonToYaml(new ByteArrayInputStream(target.toString().getBytes()));
userFileOutput.write(userYaml.getBytes());
patchFileOutput.write(patch.toString().getBytes());
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "error happen when copy the new system config", e);
}
} else {
LOGGER.warning("there's no patch of casc");
}
}
private static URL findConfig(String path) {
final ServletContext servletContext = Jenkins.getInstance().servletContext;
try {
return servletContext.getResource(path);
} catch (IOException e) {
LOGGER.log(Level.SEVERE, String.format("error happen when finding path %s", path), e);
}
return null;
}
private static void copyAndDelSrc(URL src, URL target) throws IOException {
try {
PatchConfig.copy(src, target);
} finally {
boolean result = new File(src.getFile()).delete();
LOGGER.fine("src file delete " + result);
}
}
private static void copy(URL src, URL target) throws IOException {
try (InputStream input = src.openStream();
OutputStream output = new FileOutputStream(target.getFile())) {
IOUtils.copy(input, output);
}
}
private static String jsonToYaml(InputStream input) throws IOException {
ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());
ObjectMapper jsonReader = new ObjectMapper();
Object obj = jsonReader.readValue(input, Object.class);
return yamlReader.writeValueAsString(obj);
}
private static String yamlToJson(InputStream input) throws IOException {
ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());
ObjectMapper jsonReader = new ObjectMapper();
Object obj = yamlReader.readValue(input, Object.class);
return jsonReader.writeValueAsString(obj);
}
}