[ Server ] [ APP ] - accomplish rke and dashboard

This commit is contained in:
zeaslity
2023-11-20 17:35:08 +08:00
parent c47e6b5ed1
commit eb778bf60b
15 changed files with 277 additions and 83 deletions

View File

@@ -34,6 +34,59 @@ func BasicCommandExistsBatch(commandNameList []string) (bool, string) {
return true, "" return true, ""
} }
// BasicCommandExistByPath 根据路径判定 命令是否存在
func BasicCommandExistByPath(commandName string) bool {
if BasicFileExistInFolder(commandName, "/usr/local/sbin", "/usr/local/bin", "/usr/sbin", "/usr/bin", "/sbin", "bin", "/snap/bin") {
return true
}
return false
}
// BasicFileExistInFolder 查询fileName是否存在于目录folderList中如果存在返回true不存在返回false
func BasicFileExistInFolder(fileName string, folderList ...string) bool {
for _, folder := range folderList {
if BasicFolderExists(folder) {
// is folder
ok, _ := PipelineCommandExecutor([][]string{
{
"ls",
folder,
},
{
"grep",
"-c",
fileName,
},
})
if ok {
return true
}
} else {
// it's a file
ok, _ := PipelineCommandExecutor([][]string{
{
"echo",
folder,
},
{
"grep",
"-c",
fileName,
},
})
if ok {
return true
}
}
}
return false
}
// BasicReplace 基础替换命令 // BasicReplace 基础替换命令
func BasicReplace(filename string, origin string, replace string) bool { func BasicReplace(filename string, origin string, replace string) bool {
@@ -78,6 +131,18 @@ func BasicFileExistAndNotNull(filename string) bool {
} }
} }
func BasicFolderExists(folderName string) bool {
cmd := exec.Command("test", "-d", folderName)
err := cmd.Run()
if err != nil {
log.DebugF("目录 %s 不存在!", folderName)
return false
} else {
return true
}
}
func BasicCreateFolder(folderName string) bool { func BasicCreateFolder(folderName string) bool {
cmd := exec.Command("test", "-d", folderName) cmd := exec.Command("test", "-d", folderName)
err := cmd.Run() err := cmd.Run()
@@ -115,30 +180,9 @@ func BasicSystemdShutdown(serviceName string) (ok bool, resultLog []string) {
serviceName = serviceName + ".service" serviceName = serviceName + ".service"
} }
systemdFilePath := []string{
"/lib/systemd/system/",
"/etc/systemd/system",
}
// 检查是否存在 // 检查是否存在
var existService bool existService := BasicFileExistInFolder(serviceName, "/lib/systemd/system/",
"/etc/systemd/system")
for _, s := range systemdFilePath {
resultOk, _ := PipelineCommandExecutor(
[][]string{
{
"ls",
s,
},
{
"grep",
serviceName,
},
})
if resultOk {
existService = true
}
}
if !existService { if !existService {
return true, []string{ return true, []string{
serviceName, serviceName,
@@ -175,30 +219,9 @@ func BasicSystemdUp(serviceName string) (ok bool, resultLog []string) {
if !strings.HasSuffix(serviceName, ".service") { if !strings.HasSuffix(serviceName, ".service") {
serviceName = serviceName + ".service" serviceName = serviceName + ".service"
} }
systemdFilePath := []string{
"/lib/systemd/system/",
"/etc/systemd/system",
}
// 检查是否存在 // 检查是否存在
var existService bool existService := BasicFileExistInFolder(serviceName, "/lib/systemd/system/",
"/etc/systemd/system")
for _, s := range systemdFilePath {
resultOk, _ := PipelineCommandExecutor(
[][]string{
{"ls",
s,
},
{
"grep",
serviceName,
},
})
if resultOk {
existService = true
}
}
if !existService { if !existService {
return true, []string{ return true, []string{
serviceName, serviceName,
@@ -269,7 +292,7 @@ func BasicDownloadFile(downloadUrl, desFile string) (downloadOk bool, resultLog
// wget or curl download // wget or curl download
var ok bool var ok bool
if BasicCommandExists("wget") { if BasicCommandExistByPath("wget") {
ok, resultLog = AllCommandExecutor([]string{ ok, resultLog = AllCommandExecutor([]string{
"wget", "wget",
@@ -280,7 +303,7 @@ func BasicDownloadFile(downloadUrl, desFile string) (downloadOk bool, resultLog
desFile, desFile,
}) })
} else if BasicCommandExists("curl") { } else if BasicCommandExistByPath("curl") {
ok, resultLog = AllCommandExecutor([]string{ ok, resultLog = AllCommandExecutor([]string{
"curl", "curl",
"-o", "-o",

View File

@@ -30,7 +30,7 @@ func Execute(em *ExecutionMessage) (bool, []string) {
var resultLog []string var resultLog []string
var err error var err error
ok := true ok := true
executionContent := em.ExecutionType + "==" + strings.Join(em.FuncContent, " - ") executionContent := em.ExecutionType + " == " + strings.Join(em.FuncContent, " - ")
log.DebugF("em message is => %#v", em) log.DebugF("em message is => %#v", em)

View File

@@ -1,12 +1,21 @@
package io.wdd.func.auto.beans; package io.wdd.func.auto.beans;
/**
* 参数固定顺序为 A1C2IP SUPREME N1C2IP A1C1IP A1C1JS M2D2IP KIMMY
*/
public enum AppFunctionEnum { public enum AppFunctionEnum {
DEPLOY_RKE( DEPLOY_RKE(
"DEPLOY_RKE", "DEPLOY_RKE",
"部署rke的组件准备环境" "部署rke的组件准备环境"
), ),
DEPLOY_K8S_NAMESPACE(
"DEPLOY_K8S_NAMESPACE",
"部署k8s的业务应用的namespace, 参数A1C2IP, SUPREME"
),
DEPLOY_K8S_DASHBOARD( DEPLOY_K8S_DASHBOARD(
"DEPLOY_K8S_DASHBOARD", "DEPLOY_K8S_DASHBOARD",
"部署kubernetes dashboard, 参数A1C2IP" "部署kubernetes dashboard, 参数A1C2IP"
@@ -19,7 +28,7 @@ public enum AppFunctionEnum {
DEPLOY_NFS( DEPLOY_NFS(
"DEPLOY_NFS", "DEPLOY_NFS",
"部署nfs+storage_class, 参数为N1C2IP" "部署nfs+storage_class, 参数为A1C2IP, SUPREME, N1C2IP"
), ),
@@ -30,12 +39,12 @@ public enum AppFunctionEnum {
DEPLOY_K8S_PVC( DEPLOY_K8S_PVC(
"DEPLOY_K8S_PVC", "DEPLOY_K8S_PVC",
"部署所需要的pvc后端存储参数 SUPREME" "部署所需要的pvc后端存储参数 A1C2IP, SUPREME"
), ),
DEPLOY_K8S_MYSQL( DEPLOY_K8S_MYSQL(
"DEPLOY_K8S_MYSQL", "DEPLOY_K8S_MYSQL",
"部署k8s mysql, 参数 SUPREME A1C2IP" "部署k8s mysql, 参数 A1C2IP, SUPREME"
), ),
LOAD_MYSQL_INIT_SCRIPT( LOAD_MYSQL_INIT_SCRIPT(
@@ -45,17 +54,17 @@ public enum AppFunctionEnum {
DEPLOY_K8S_REDIS( DEPLOY_K8S_REDIS(
"DEPLOY_K8S_REDIS", "DEPLOY_K8S_REDIS",
"部署k8s redis, 参数 SUPREME A1C2IP " "部署k8s redis, 参数 A1C2IP, SUPREME "
), ),
DEPLOY_K8S_MIDDLEWARES( DEPLOY_K8S_MIDDLEWARES(
"DEPLOY_K8S_MIDDLEWARES", "DEPLOY_K8S_MIDDLEWARES",
"部署k8s mongo nacos emqxs, 参数 SUPREME A1C2IP " "部署k8s mongo nacos emqxs, 参数 A1C2IP, SUPREME "
), ),
INIT_MINIO( INIT_MINIO(
"INIT_MINIO", "INIT_MINIO",
"初始化MINIO c参数SUPREME A1C2IP M2D2IP" "初始化MINIO 参数 A1C2IP, SUPREME, M2D2IP"
), ),
DEPLOY_K8S_SRS( DEPLOY_K8S_SRS(

View File

@@ -17,24 +17,23 @@ import java.util.List;
@ApiModel("项目实施阶段的上下文实体类") @ApiModel("项目实施阶段的上下文实体类")
public class ProjectDeployContext { public class ProjectDeployContext {
Long projectId; // harbor
ServerInfoPO masterNode;
List<ServerInfoPO> agentNodeList;
boolean masterBaseProcedureAccomplished;
boolean agentBaseProcedureAccomplished;
public static final ArrayList<String> PROJECT_NEED_TO_BY_SYNCHRONIZED = new ArrayList<>( public static final ArrayList<String> PROJECT_NEED_TO_BY_SYNCHRONIZED = new ArrayList<>(
List.of( List.of(
"rancher", "rancher",
"cmii" "cmii"
) )
); );
Long projectId;
ServerInfoPO masterNode;
List<ServerInfoPO> agentNodeList;
// base
boolean masterBaseProcedureAccomplished;
boolean agentBaseProcedureAccomplished;
boolean rancherHarborSynchronized; boolean rancherHarborSynchronized;
boolean cmiiHarborSynchronized; boolean cmiiHarborSynchronized;
String currentSynchronizingProject; String currentSynchronizingProject;
// app
} }

View File

@@ -0,0 +1,140 @@
package io.wdd.func.auto.service;
import io.wdd.func.auto.beans.AppFunctionEnum;
import io.wdd.func.auto.beans.ProjectDeployContext;
import io.wdd.server.beans.po.ProjectInfoPO;
import io.wdd.server.coreService.CoreProjectService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
@Slf4j
public class AppFuncScheduler {
@Resource
FuncService funcService;
@Resource
CoreProjectService coreProjectService;
/**
* 参数固定顺序为 A1C2IP SUPREME N1C2IP A1C1IP A1C1JS M2D2IP KIMMY
*
* @param projectDeployContext
* @param projectInfoPO
* @return
*/
private static ArrayList<String> buildAppFuncArgs(ProjectDeployContext projectDeployContext, ProjectInfoPO projectInfoPO) {
ArrayList<String> appFuncArgs = new ArrayList<>();
appFuncArgs.add(projectDeployContext
.getMasterNode()
.getServerIpInV4());
appFuncArgs.add(projectInfoPO.getProjectNamespace());
appFuncArgs.add(projectDeployContext
.getMasterNode()
.getServerIpInV4());
appFuncArgs.add(projectDeployContext
.getMasterNode()
.getServerIpPbV4());
appFuncArgs.add(projectInfoPO.getProjectWebServicePort());
appFuncArgs.add("M2D2IP");
appFuncArgs.add(projectInfoPO.getProjectVersion());
return appFuncArgs;
}
public boolean runProcedure(ProjectDeployContext projectDeployContext) {
// before run
// check base requirement
beforeRunProcedure(projectDeployContext);
// during run
doRunProcedure(projectDeployContext);
// after run
afterRunProcedure(projectDeployContext);
return true;
}
private void afterRunProcedure(ProjectDeployContext projectDeployContext) {
// 检查是否安装完成, 对安装环境进行判定
log.debug("afterRunProcedure complete!");
}
private void doRunProcedure(ProjectDeployContext projectDeployContext) {
// 执行 基础功能施工的内容
String masterTopicName = projectDeployContext
.getMasterNode()
.getTopicName();
ProjectInfoPO projectInfoPO = coreProjectService.projectGetOne(projectDeployContext.getProjectId());
if (ObjectUtils.isEmpty(projectInfoPO)) {
log.error(
"project id of [{}] wrong ! cant find project !",
projectDeployContext.getProjectId()
);
}
ArrayList<String> appFuncArgs = buildAppFuncArgs(
projectDeployContext,
projectInfoPO
);
List<AppFunctionEnum> appFunctionEnumList = List.of(
AppFunctionEnum.DEPLOY_RKE,
AppFunctionEnum.DEPLOY_K8S_DASHBOARD
);
for (AppFunctionEnum appFunctionEnum : appFunctionEnumList) {
// rebuild
appFuncArgs.add(
0,
appFunctionEnum.getAppOpname()
);
boolean appFuncJudge = funcService.callAppFuncAndJudge(
masterTopicName,
appFunctionEnum,
appFuncArgs
);
if (!appFuncJudge) {
log.error(
"app func of {} execute error !",
appFunctionEnum.getAppOpname()
);
return;
}
}
}
private void beforeRunProcedure(ProjectDeployContext projectDeployContext) {
// 检查是否符合规定
// 检查每一个Agent是否能够连通,调用命令返回结果
// 打印施工信息
log.debug("beforeRunProcedure complete!");
}
}

View File

@@ -93,6 +93,7 @@ public class BaseFuncScheduler {
// BaseFunctionEnum.INSTALL_DEFAULT_SSH_KEY, // BaseFunctionEnum.INSTALL_DEFAULT_SSH_KEY,
// BaseFunctionEnum.INSTALL_HARBOR, // BaseFunctionEnum.INSTALL_HARBOR,
// BaseFunctionEnum.INSTALL_ZSH // BaseFunctionEnum.INSTALL_ZSH
); );

View File

@@ -1,6 +1,7 @@
package io.wdd.func.auto.service; package io.wdd.func.auto.service;
import io.wdd.func.auto.beans.AppFunctionEnum;
import io.wdd.func.auto.beans.BaseFunctionEnum; import io.wdd.func.auto.beans.BaseFunctionEnum;
import io.wdd.func.auto.beans.HarborFunctionEnum; import io.wdd.func.auto.beans.HarborFunctionEnum;
@@ -25,5 +26,7 @@ public interface FuncService {
*/ */
boolean callBaseFuncAndJudge(String agentTopicName, BaseFunctionEnum baseFunctionEnum, List<String> funcArgs, boolean durationTask); boolean callBaseFuncAndJudge(String agentTopicName, BaseFunctionEnum baseFunctionEnum, List<String> funcArgs, boolean durationTask);
boolean callAppFuncAndJudge(String agentTopicName, AppFunctionEnum appFunctionEnum, List<String> funcArgs);
boolean callHarborFuncAndJudge(String agentTopicName, HarborFunctionEnum harborFunctionEnum, ArrayList<String> funcArgs); boolean callHarborFuncAndJudge(String agentTopicName, HarborFunctionEnum harborFunctionEnum, ArrayList<String> funcArgs);
} }

View File

@@ -1,5 +1,6 @@
package io.wdd.func.auto.service; package io.wdd.func.auto.service;
import io.wdd.func.auto.beans.AppFunctionEnum;
import io.wdd.func.auto.beans.BaseFunctionEnum; import io.wdd.func.auto.beans.BaseFunctionEnum;
import io.wdd.func.auto.beans.HarborFunctionEnum; import io.wdd.func.auto.beans.HarborFunctionEnum;
import io.wdd.rpc.execute.ExecutionMessageType; import io.wdd.rpc.execute.ExecutionMessageType;
@@ -68,6 +69,20 @@ public class FuncServiceImpl implements FuncService {
return JudgeSyncBaseCommandResult(syncResultLog); return JudgeSyncBaseCommandResult(syncResultLog);
} }
@Override
public boolean callAppFuncAndJudge(String agentTopicName, AppFunctionEnum appFunctionEnum, List<String> funcArgs) {
List<String> syncResultLog = syncCallFunction(
agentTopicName,
ExecutionMessageType.APP,
funcArgs,
true
);
boolean appFuncResult = JudgeSyncBaseCommandResult(syncResultLog);
return appFuncResult;
}
@Override @Override
public boolean callHarborFuncAndJudge(String agentTopicName, HarborFunctionEnum harborFunctionEnum, ArrayList<String> funcArgs) { public boolean callHarborFuncAndJudge(String agentTopicName, HarborFunctionEnum harborFunctionEnum, ArrayList<String> funcArgs) {

View File

@@ -1,12 +1,10 @@
package io.wdd.server.beans.po; package io.wdd.server.beans.po;
import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import java.io.Serializable; import java.io.Serializable;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@@ -16,11 +14,7 @@ import java.time.LocalDateTime;
*/ */
@TableName(value = "project_info") @TableName(value = "project_info")
@Data @Data
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder(toBuilder = true)
public class ProjectInfoPO implements Serializable { public class ProjectInfoPO implements Serializable {
@TableField(exist = false) @TableField(exist = false)
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/** /**
@@ -39,10 +33,15 @@ public class ProjectInfoPO implements Serializable {
@TableField(value = "project_province") @TableField(value = "project_province")
private String projectProvince; private String projectProvince;
/** /**
* * 部署的版本
*/ */
@TableField(value = "project_version") @TableField(value = "project_version")
private String projectVersion; private String projectVersion;
/**
* 部署的公网端口
*/
@TableField(value = "project_web_service_port")
private String projectWebServicePort;
/** /**
* *
*/ */

View File

@@ -6,14 +6,12 @@ import io.wdd.server.beans.po.ServerInfoPO;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import java.util.List; import java.util.List;
@Data @Data
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@SuperBuilder(toBuilder = true)
@ApiModel("Project-Server绑定关系实体类") @ApiModel("Project-Server绑定关系实体类")
public class ProjectServerVO extends ProjectInfoPO { public class ProjectServerVO extends ProjectInfoPO {

View File

@@ -6,7 +6,7 @@ import io.wdd.server.beans.po.ProjectInfoPO;
/** /**
* @author wdd * @author wdd
* @description 针对表【project_info】的数据库操作Mapper * @description 针对表【project_info】的数据库操作Mapper
* @createDate 2023-10-09 11:12:02 * @createDate 2023-11-20 16:47:25
* @Entity io.wdd.server.beans.po.ProjectInfoPO * @Entity io.wdd.server.beans.po.ProjectInfoPO
*/ */
public interface ProjectInfoMapper extends BaseMapper<ProjectInfoPO> { public interface ProjectInfoMapper extends BaseMapper<ProjectInfoPO> {

View File

@@ -6,7 +6,7 @@ import io.wdd.server.beans.po.ProjectInfoPO;
/** /**
* @author wdd * @author wdd
* @description 针对表【project_info】的数据库操作Service * @description 针对表【project_info】的数据库操作Service
* @createDate 2023-10-09 11:12:02 * @createDate 2023-11-20 16:47:25
*/ */
public interface ProjectInfoService extends IService<ProjectInfoPO> { public interface ProjectInfoService extends IService<ProjectInfoPO> {

View File

@@ -9,7 +9,7 @@ import org.springframework.stereotype.Service;
/** /**
* @author wdd * @author wdd
* @description 针对表【project_info】的数据库操作Service实现 * @description 针对表【project_info】的数据库操作Service实现
* @createDate 2023-10-09 11:12:02 * @createDate 2023-11-20 16:47:25
*/ */
@Service @Service
public class ProjectInfoServiceImpl extends ServiceImpl<ProjectInfoMapper, ProjectInfoPO> public class ProjectInfoServiceImpl extends ServiceImpl<ProjectInfoMapper, ProjectInfoPO>

View File

@@ -9,6 +9,7 @@
<result property="projectName" column="project_name" jdbcType="VARCHAR"/> <result property="projectName" column="project_name" jdbcType="VARCHAR"/>
<result property="projectProvince" column="project_province" jdbcType="VARCHAR"/> <result property="projectProvince" column="project_province" jdbcType="VARCHAR"/>
<result property="projectVersion" column="project_version" jdbcType="VARCHAR"/> <result property="projectVersion" column="project_version" jdbcType="VARCHAR"/>
<result property="projectWebServicePort" column="project_web_service_port" jdbcType="VARCHAR"/>
<result property="projectNamespace" column="project_namespace" jdbcType="VARCHAR"/> <result property="projectNamespace" column="project_namespace" jdbcType="VARCHAR"/>
<result property="deployNetEnv" column="deploy_net_env" jdbcType="TINYINT"/> <result property="deployNetEnv" column="deploy_net_env" jdbcType="TINYINT"/>
<result property="deployHardwareEnv" column="deploy_hardware_env" jdbcType="VARCHAR"/> <result property="deployHardwareEnv" column="deploy_hardware_env" jdbcType="VARCHAR"/>
@@ -24,9 +25,9 @@
<sql id="Base_Column_List"> <sql id="Base_Column_List">
project_id project_id
,project_name,project_province, ,project_name,project_province,
project_version,project_namespace,deploy_net_env, project_version,project_web_service_port,project_namespace,
deploy_hardware_env,create_time,update_time, deploy_net_env,deploy_hardware_env,create_time,
expired_time,trade_person_name,pre_sale_person_name, update_time,expired_time,trade_person_name,
is_offline_map,is_ai_deploy pre_sale_person_name,is_offline_map,is_ai_deploy
</sql> </sql>
</mapper> </mapper>

View File

@@ -1,6 +1,7 @@
package io.wdd.server.func; package io.wdd.server.func;
import io.wdd.func.auto.beans.ProjectDeployContext; import io.wdd.func.auto.beans.ProjectDeployContext;
import io.wdd.func.auto.service.AppFuncScheduler;
import io.wdd.func.auto.service.BaseFuncScheduler; import io.wdd.func.auto.service.BaseFuncScheduler;
import io.wdd.func.auto.service.HarborFuncScheduler; import io.wdd.func.auto.service.HarborFuncScheduler;
import io.wdd.server.beans.po.ServerInfoPO; import io.wdd.server.beans.po.ServerInfoPO;
@@ -20,6 +21,9 @@ public class TestBaseFuncScheduler {
@Resource @Resource
HarborFuncScheduler harborFuncScheduler; HarborFuncScheduler harborFuncScheduler;
@Resource
AppFuncScheduler appFuncScheduler;
@Resource @Resource
CoreServerService serverService; CoreServerService serverService;
@@ -51,7 +55,9 @@ public class TestBaseFuncScheduler {
// baseFuncScheduler.runProcedure(projectDeployContext); // baseFuncScheduler.runProcedure(projectDeployContext);
harborFuncScheduler.runProcedure(projectDeployContext); // harborFuncScheduler.runProcedure(projectDeployContext);
appFuncScheduler.runProcedure(projectDeployContext);
} }
} }