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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
| package com.server.service;
import com.server.entity.Server; import com.server.entity.ServerConfig; import com.server.repository.ServerRepository; import com.server.repository.ServerConfigRepository; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;
import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.time.LocalDateTime; import java.util.List; import java.util.Optional;
@Slf4j @Service public class ConfigManagementService { @Autowired private ServerRepository serverRepository; @Autowired private ServerConfigRepository configRepository; @Value("${server-config.config-path:/opt/server-configs}") private String configPath; @Value("${server-config.backup-path:/opt/config-backups}") private String backupPath;
@Transactional public ServerConfig createConfig(Long serverId, String configName, String configContent, String configType) { try { log.info("创建服务器配置: serverId={}, configName={}", serverId, configName); Optional<Server> serverOpt = serverRepository.findById(serverId); if (!serverOpt.isPresent()) { throw new RuntimeException("服务器不存在"); } Server server = serverOpt.get(); ServerConfig config = ServerConfig.builder() .server(server) .configName(configName) .configContent(configContent) .configType(configType) .version(1) .status("ACTIVE") .createTime(LocalDateTime.now()) .updateTime(LocalDateTime.now()) .build(); config = configRepository.save(config); createConfigFile(server, config); log.info("服务器配置创建成功: configId={}", config.getId()); return config; } catch (Exception e) { log.error("创建服务器配置失败", e); throw new RuntimeException("创建服务器配置失败", e); } }
@Transactional public ServerConfig updateConfig(Long configId, String configContent) { try { log.info("更新服务器配置: configId={}", configId); Optional<ServerConfig> configOpt = configRepository.findById(configId); if (!configOpt.isPresent()) { throw new RuntimeException("配置不存在"); } ServerConfig config = configOpt.get(); backupConfig(config); config.setConfigContent(configContent); config.setVersion(config.getVersion() + 1); config.setUpdateTime(LocalDateTime.now()); config = configRepository.save(config); updateConfigFile(config); log.info("服务器配置更新成功: configId={}, version={}", configId, config.getVersion()); return config; } catch (Exception e) { log.error("更新服务器配置失败", e); throw new RuntimeException("更新服务器配置失败", e); } }
@Transactional public boolean deployConfig(Long configId) { try { log.info("部署配置到服务器: configId={}", configId); Optional<ServerConfig> configOpt = configRepository.findById(configId); if (!configOpt.isPresent()) { throw new RuntimeException("配置不存在"); } ServerConfig config = configOpt.get(); Server server = config.getServer(); if (!"ONLINE".equals(server.getStatus())) { throw new RuntimeException("服务器状态异常,无法部署"); } boolean success = deployConfigToServer(server, config); if (success) { config.setLastDeployTime(LocalDateTime.now()); config.setDeployStatus("SUCCESS"); configRepository.save(config); log.info("配置部署成功: configId={}", configId); } else { config.setDeployStatus("FAILED"); configRepository.save(config); log.error("配置部署失败: configId={}", configId); } return success; } catch (Exception e) { log.error("部署配置失败", e); throw new RuntimeException("部署配置失败", e); } }
@Transactional public boolean rollbackConfig(Long configId, Integer targetVersion) { try { log.info("回滚配置: configId={}, targetVersion={}", configId, targetVersion); Optional<ServerConfig> configOpt = configRepository.findById(configId); if (!configOpt.isPresent()) { throw new RuntimeException("配置不存在"); } ServerConfig config = configOpt.get(); Optional<ServerConfig> targetConfigOpt = configRepository .findByServerAndConfigNameAndVersion( config.getServer(), config.getConfigName(), targetVersion); if (!targetConfigOpt.isPresent()) { throw new RuntimeException("目标版本配置不存在"); } ServerConfig targetConfig = targetConfigOpt.get(); config.setConfigContent(targetConfig.getConfigContent()); config.setVersion(config.getVersion() + 1); config.setUpdateTime(LocalDateTime.now()); config.setDeployStatus("ROLLBACK"); config = configRepository.save(config); boolean success = deployConfigToServer(config.getServer(), config); if (success) { log.info("配置回滚成功: configId={}, version={}", configId, config.getVersion()); } else { log.error("配置回滚失败: configId={}", configId); } return success; } catch (Exception e) { log.error("回滚配置失败", e); throw new RuntimeException("回滚配置失败", e); } }
public List<ServerConfig> getServerConfigs(Long serverId) { try { log.info("获取服务器配置列表: serverId={}", serverId); Optional<Server> serverOpt = serverRepository.findById(serverId); if (!serverOpt.isPresent()) { throw new RuntimeException("服务器不存在"); } List<ServerConfig> configs = configRepository.findByServerOrderByCreateTimeDesc(serverOpt.get()); log.info("获取服务器配置列表成功,数量: {}", configs.size()); return configs; } catch (Exception e) { log.error("获取服务器配置列表失败", e); throw new RuntimeException("获取服务器配置列表失败", e); } }
private void createConfigFile(Server server, ServerConfig config) throws IOException { String fileName = String.format("%s_%s_%d.%s", server.getHost(), config.getConfigName(), config.getVersion(), getFileExtension(config.getConfigType())); Path filePath = Paths.get(configPath, server.getEnvironment(), fileName); Files.createDirectories(filePath.getParent()); Files.write(filePath, config.getConfigContent().getBytes()); log.info("配置文件创建成功: {}", filePath); }
private void updateConfigFile(ServerConfig config) throws IOException { Server server = config.getServer(); String fileName = String.format("%s_%s_%d.%s", server.getHost(), config.getConfigName(), config.getVersion(), getFileExtension(config.getConfigType())); Path filePath = Paths.get(configPath, server.getEnvironment(), fileName); Files.write(filePath, config.getConfigContent().getBytes()); log.info("配置文件更新成功: {}", filePath); }
private void backupConfig(ServerConfig config) throws IOException { Server server = config.getServer(); String fileName = String.format("%s_%s_%d_%s.%s", server.getHost(), config.getConfigName(), config.getVersion(), LocalDateTime.now().toString().replace(":", "-"), getFileExtension(config.getConfigType())); Path backupFilePath = Paths.get(backupPath, server.getEnvironment(), fileName); Files.createDirectories(backupFilePath.getParent()); Files.write(backupFilePath, config.getConfigContent().getBytes()); log.info("配置文件备份成功: {}", backupFilePath); }
private boolean deployConfigToServer(Server server, ServerConfig config) { try { log.info("部署配置到服务器: host={}, configName={}", server.getHost(), config.getConfigName()); Thread.sleep(1000); log.info("配置部署到服务器成功: host={}", server.getHost()); return true; } catch (Exception e) { log.error("部署配置到服务器失败", e); return false; } }
private String getFileExtension(String configType) { switch (configType.toUpperCase()) { case "YAML": case "YML": return "yml"; case "JSON": return "json"; case "XML": return "xml"; case "PROPERTIES": return "properties"; default: return "txt"; } } }
|