|
|
@@ -0,0 +1,77 @@
|
|
|
+package nju.seec.SEECdemo.logic.service.impl;
|
|
|
+
|
|
|
+import nju.seec.SEECdemo.logic.api.k8s.ExecApi;
|
|
|
+import nju.seec.SEECdemo.logic.api.k8s.exception.K8sApiException;
|
|
|
+import nju.seec.SEECdemo.logic.service.DBService;
|
|
|
+import nju.seec.SEECdemo.logic.vo.DBDetailVO;
|
|
|
+import nju.seec.SEECdemo.util.ApplicationProperties;
|
|
|
+import nju.seec.SEECdemo.util.LoggerUtil;
|
|
|
+import nju.seec.SEECdemo.util.RandomStringUtil;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class DBServiceImpl implements DBService{
|
|
|
+
|
|
|
+ private static final Logger logger = LoggerUtil.getLogger(DBServiceImpl.class);
|
|
|
+
|
|
|
+ private final ExecApi execApi;
|
|
|
+
|
|
|
+ private final String publicMySqlPod;
|
|
|
+
|
|
|
+ private final String publicMysqlUsername;
|
|
|
+
|
|
|
+ private final String publicMysqlPassword;
|
|
|
+
|
|
|
+ private final String publicMysqlUrl;
|
|
|
+
|
|
|
+ private final String publicNamespace;
|
|
|
+ @Autowired
|
|
|
+ public DBServiceImpl(ApplicationProperties applicationProperties, ExecApi execApi) {
|
|
|
+ this.execApi = execApi;
|
|
|
+ ApplicationProperties.K8s k8s = applicationProperties.getK8s();
|
|
|
+ this.publicMySqlPod = k8s.getPublicMysqlPod();
|
|
|
+ this.publicMysqlUsername = k8s.getPublicMysqlUsername();
|
|
|
+ this.publicMysqlPassword = k8s.getPublicMysqlPassword();
|
|
|
+ this.publicMysqlUrl = k8s.getPublicMysqlUrl();
|
|
|
+ this.publicNamespace = k8s.getPublicNamespace();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public DBDetailVO createSchemaAndUser(String schema, String username, String password) {
|
|
|
+ DBDetailVO dbDetailVO = new DBDetailVO(publicMysqlUrl, schema, username, password);
|
|
|
+
|
|
|
+ StringBuilder args = new StringBuilder();
|
|
|
+ args.append("mysql -u ").append(publicMysqlUsername).append(" -p").append(publicMysqlPassword).append(" -e ")
|
|
|
+ .append("\"drop database if exists ").append(schema).append(";")
|
|
|
+ .append("create database ").append(schema).append(";")
|
|
|
+ .append("drop user if exists ").append(username).append(";")
|
|
|
+ .append("create user ").append(username).append(" identified by '").append(password).append("';")
|
|
|
+ .append("grant all privileges on ").append(schema).append(".* to ").append(username).append("@'%' identified by '").append(password).append("';")
|
|
|
+ .append("\"");
|
|
|
+ List<String> commands = new ArrayList<>();
|
|
|
+ commands.add("sh");
|
|
|
+ commands.add("-c");
|
|
|
+ commands.add(args.toString());
|
|
|
+ try {
|
|
|
+ execApi.exec(publicNamespace, publicMySqlPod, commands);
|
|
|
+ } catch (K8sApiException e) {
|
|
|
+ }
|
|
|
+ return dbDetailVO;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public DBDetailVO createSchemaAndUserForApplication(String projectId, String applicationName) {
|
|
|
+ String schemaName = projectId + "_" + applicationName + "_db";
|
|
|
+ String userName = projectId + applicationName;
|
|
|
+ String password = RandomStringUtil.getRandomString(10);
|
|
|
+ LoggerUtil.info(logger,"创建数据库信息 schema={}, userName={}, password={}",
|
|
|
+ schemaName, userName, password);
|
|
|
+ return createSchemaAndUser(schemaName, userName, password);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|