|
| 1 | +package io.jenkins.plugins.casc.auto; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; |
| 6 | +import com.flipkart.zjsonpatch.JsonDiff; |
| 7 | +import com.flipkart.zjsonpatch.JsonPatch; |
| 8 | +import hudson.init.InitMilestone; |
| 9 | +import hudson.init.Initializer; |
| 10 | +import java.io.ByteArrayInputStream; |
| 11 | +import java.io.File; |
| 12 | +import java.io.FileOutputStream; |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.InputStream; |
| 15 | +import java.io.OutputStream; |
| 16 | +import java.net.URL; |
| 17 | +import java.util.logging.Level; |
| 18 | +import java.util.logging.Logger; |
| 19 | +import javax.servlet.ServletContext; |
| 20 | +import jenkins.model.Jenkins; |
| 21 | +import org.apache.commons.io.IOUtils; |
| 22 | + |
| 23 | +/** |
| 24 | + * Apply the patch between two versions of the initial config files |
| 25 | + */ |
| 26 | +public class PatchConfig { |
| 27 | + private static final Logger LOGGER = Logger.getLogger(CasCBackup.class.getName()); |
| 28 | + |
| 29 | + final static String DEFAULT_JENKINS_YAML_PATH = "jenkins.yaml"; |
| 30 | + final static String cascFile = "/WEB-INF/" + DEFAULT_JENKINS_YAML_PATH; |
| 31 | + final static String cascDirectory = "/WEB-INF/" + DEFAULT_JENKINS_YAML_PATH + ".d/"; |
| 32 | + final static String cascUserConfigFile = "user.yaml"; |
| 33 | + |
| 34 | + @Initializer(after= InitMilestone.STARTED, fatal=false) |
| 35 | + public static void patchConfig() { |
| 36 | + LOGGER.fine("start to calculate the patch of casc"); |
| 37 | + |
| 38 | + URL newSystemConfig = findConfig("/" + DEFAULT_JENKINS_YAML_PATH); |
| 39 | + URL systemConfig = findConfig(cascFile); |
| 40 | + URL userConfig = findConfig(cascDirectory + cascUserConfigFile); |
| 41 | + URL userConfigDir = findConfig(cascDirectory); |
| 42 | + |
| 43 | + if (newSystemConfig == null || userConfigDir == null) { |
| 44 | + LOGGER.warning("no need to upgrade the configuration of Jenkins"); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + JsonNode patch = null; |
| 49 | + if (systemConfig != null && userConfig != null) { |
| 50 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 51 | + try { |
| 52 | + JsonNode source = objectMapper.readTree(yamlToJson(systemConfig.openStream())); |
| 53 | + JsonNode target = objectMapper.readTree(yamlToJson(userConfig.openStream())); |
| 54 | + |
| 55 | + patch = JsonDiff.asJson(source, target); |
| 56 | + } catch (IOException e) { |
| 57 | + LOGGER.log(Level.SEVERE, "error happen when calculate the patch", e); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + try { |
| 62 | + // give systemConfig a real path |
| 63 | + PatchConfig.copyAndDelSrc(newSystemConfig, systemConfig); |
| 64 | + } catch (IOException e) { |
| 65 | + LOGGER.log(Level.SEVERE, "error happen when copy the new system config", e); |
| 66 | + return; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if (patch != null) { |
| 71 | + File userYamlFile = new File(userConfigDir.getFile(), "user.yaml"); |
| 72 | + File userJSONFile = new File(userConfigDir.getFile(), "user.json"); |
| 73 | + |
| 74 | + try (InputStream newSystemInput = systemConfig.openStream(); |
| 75 | + OutputStream userFileOutput = new FileOutputStream(userYamlFile); |
| 76 | + OutputStream patchFileOutput = new FileOutputStream(userJSONFile)){ |
| 77 | + ObjectMapper jsonReader = new ObjectMapper(); |
| 78 | + JsonNode target = JsonPatch.apply(patch, jsonReader.readTree(yamlToJson(newSystemInput))); |
| 79 | + |
| 80 | + String userYaml = jsonToYaml(new ByteArrayInputStream(target.toString().getBytes())); |
| 81 | + |
| 82 | + userFileOutput.write(userYaml.getBytes()); |
| 83 | + patchFileOutput.write(patch.toString().getBytes()); |
| 84 | + } catch (Exception e) { |
| 85 | + LOGGER.log(Level.SEVERE, "error happen when copy the new system config", e); |
| 86 | + } |
| 87 | + } else { |
| 88 | + LOGGER.warning("there's no patch of casc"); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private static URL findConfig(String path) { |
| 93 | + final ServletContext servletContext = Jenkins.getInstance().servletContext; |
| 94 | + try { |
| 95 | + return servletContext.getResource(path); |
| 96 | + } catch (IOException e) { |
| 97 | + LOGGER.log(Level.SEVERE, String.format("error happen when finding path %s", path), e); |
| 98 | + } |
| 99 | + return null; |
| 100 | + } |
| 101 | + |
| 102 | + private static void copyAndDelSrc(URL src, URL target) throws IOException { |
| 103 | + try { |
| 104 | + PatchConfig.copy(src, target); |
| 105 | + } finally { |
| 106 | + boolean result = new File(src.getFile()).delete(); |
| 107 | + LOGGER.fine("src file delete " + result); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private static void copy(URL src, URL target) throws IOException { |
| 112 | + IOUtils.copy(src.openStream(), new FileOutputStream(target.getFile())); |
| 113 | + } |
| 114 | + |
| 115 | + private static String jsonToYaml(InputStream input) throws IOException { |
| 116 | + ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory()); |
| 117 | + ObjectMapper jsonReader = new ObjectMapper(); |
| 118 | + |
| 119 | + Object obj = jsonReader.readValue(input, Object.class); |
| 120 | + |
| 121 | + return yamlReader.writeValueAsString(obj); |
| 122 | + } |
| 123 | + |
| 124 | + private static String yamlToJson(InputStream input) throws IOException { |
| 125 | + ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory()); |
| 126 | + ObjectMapper jsonReader = new ObjectMapper(); |
| 127 | + |
| 128 | + Object obj = yamlReader.readValue(input, Object.class); |
| 129 | + |
| 130 | + return jsonReader.writeValueAsString(obj); |
| 131 | + } |
| 132 | +} |
0 commit comments