Skip to content

Commit

Permalink
feat(runtime): Optimize Runtime Module Components (#7548)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengkunwang223 authored Dec 24, 2024
1 parent 18321d6 commit c6346b7
Show file tree
Hide file tree
Showing 17 changed files with 358 additions and 839 deletions.
1 change: 0 additions & 1 deletion agent/app/dto/request/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ type RuntimeCreate struct {
type NodeConfig struct {
Install bool `json:"install"`
Clean bool `json:"clean"`
Port int `json:"port"`
ExposedPorts []ExposedPort `json:"exposedPorts"`
Environments []Environment `json:"environments"`
Volumes []Volume `json:"volumes"`
Expand Down
2 changes: 1 addition & 1 deletion agent/app/model/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
type Runtime struct {
BaseModel
Name string `gorm:"not null" json:"name"`
AppDetailID uint `json:"appDetailId"`
AppDetailID uint `json:"appDetailID"`
Image string `json:"image"`
WorkDir string `json:"workDir"`
DockerCompose string `json:"dockerCompose"`
Expand Down
33 changes: 14 additions & 19 deletions agent/app/service/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,26 +109,23 @@ func (r *RuntimeService) Create(create request.RuntimeCreate) (*model.Runtime, e
if exist != nil {
return nil, buserr.New(constant.ErrImageExist)
}
portValue, _ := create.Params["PANEL_APP_PORT_HTTP"]
if portValue != nil {
if err := checkPortExist(int(portValue.(float64))); err != nil {
return nil, err
}
}
case constant.RuntimeNode, constant.RuntimeJava, constant.RuntimeGo, constant.RuntimePython, constant.RuntimeDotNet:
if !fileOp.Stat(create.CodeDir) {
return nil, buserr.New(constant.ErrPathNotFound)
}
create.Install = true
if err := checkPortExist(create.Port); err != nil {
return nil, err
}
for _, export := range create.ExposedPorts {
if err := checkPortExist(export.HostPort); err != nil {
return nil, err
}
}
}
portValue, _ := create.Params["PANEL_APP_PORT_HTTP"]
if portValue != nil {
if err := checkPortExist(int(portValue.(float64))); err != nil {
return nil, err
}
}
containerName, ok := create.Params["CONTAINER_NAME"]
if !ok {
return nil, buserr.New("ErrContainerNameIsNull")
Expand Down Expand Up @@ -161,17 +158,16 @@ func (r *RuntimeService) Create(create request.RuntimeCreate) (*model.Runtime, e
Resource: create.Resource,
Version: create.Version,
ContainerName: containerName.(string),
Port: int(portValue.(float64)),
}

switch create.Type {
case constant.RuntimePHP:
runtime.Port = int(create.Params["PANEL_APP_PORT_HTTP"].(float64))
if err = handlePHP(create, runtime, fileOp, appVersionDir); err != nil {
return nil, err
}
case constant.RuntimeNode, constant.RuntimeJava, constant.RuntimeGo, constant.RuntimePython, constant.RuntimeDotNet:
runtime.Port = int(create.Params["port"].(float64))
if err = handleNodeAndJava(create, runtime, fileOp, appVersionDir); err != nil {
if err = handleRuntime(create, runtime, fileOp, appVersionDir); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -356,7 +352,7 @@ func (r *RuntimeService) Get(id uint) (*response.RuntimeDTO, error) {
}
for k, v := range envs {
switch k {
case "NODE_APP_PORT", "PANEL_APP_PORT_HTTP", "JAVA_APP_PORT", "GO_APP_PORT", "APP_PORT", "port":
case "APP_PORT", "PANEL_APP_PORT_HTTP":
port, err := strconv.Atoi(v)
if err != nil {
return nil, err
Expand Down Expand Up @@ -440,19 +436,19 @@ func (r *RuntimeService) Update(req request.RuntimeUpdate) error {
}
oldImage := runtime.Image
oldEnv := runtime.Env
req.Port = int(req.Params["port"].(float64))
port := int(req.Params["PANEL_APP_PORT_HTTP"].(float64))
switch runtime.Type {
case constant.RuntimePHP:
exist, _ := runtimeRepo.GetFirst(runtimeRepo.WithImage(req.Name), runtimeRepo.WithNotId(req.ID))
if exist != nil {
return buserr.New(constant.ErrImageExist)
}
case constant.RuntimeNode, constant.RuntimeJava, constant.RuntimeGo, constant.RuntimePython, constant.RuntimeDotNet:
if runtime.Port != req.Port {
if err = checkPortExist(req.Port); err != nil {
if runtime.Port != port {
if err = checkPortExist(port); err != nil {
return err
}
runtime.Port = req.Port
runtime.Port = port
}
for _, export := range req.ExposedPorts {
if err = checkPortExist(export.HostPort); err != nil {
Expand Down Expand Up @@ -494,7 +490,6 @@ func (r *RuntimeService) Update(req request.RuntimeUpdate) error {
CodeDir: req.CodeDir,
Version: req.Version,
NodeConfig: request.NodeConfig{
Port: req.Port,
Install: true,
ExposedPorts: req.ExposedPorts,
Environments: req.Environments,
Expand Down Expand Up @@ -526,7 +521,7 @@ func (r *RuntimeService) Update(req request.RuntimeUpdate) error {
case constant.RuntimeNode, constant.RuntimeJava, constant.RuntimeGo, constant.RuntimePython, constant.RuntimeDotNet:
runtime.Version = req.Version
runtime.CodeDir = req.CodeDir
runtime.Port = req.Port
runtime.Port = port
runtime.Status = constant.RuntimeReCreating
_ = runtimeRepo.Save(runtime)
go reCreateRuntime(runtime)
Expand Down
22 changes: 2 additions & 20 deletions agent/app/service/runtime_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"time"
)

func handleNodeAndJava(create request.RuntimeCreate, runtime *model.Runtime, fileOp files.FileOp, appVersionDir string) (err error) {
func handleRuntime(create request.RuntimeCreate, runtime *model.Runtime, fileOp files.FileOp, appVersionDir string) (err error) {
runtimeDir := path.Join(constant.RuntimeDir, create.Type)
if err = fileOp.CopyDir(appVersionDir, runtimeDir); err != nil {
return
Expand Down Expand Up @@ -356,48 +356,40 @@ func handleParams(create request.RuntimeCreate, projectDir string) (composeConte
case constant.RuntimeNode:
create.Params["CODE_DIR"] = create.CodeDir
create.Params["NODE_VERSION"] = create.Version
create.Params["PANEL_APP_PORT_HTTP"] = create.Port
if create.NodeConfig.Install {
create.Params["RUN_INSTALL"] = "1"
} else {
create.Params["RUN_INSTALL"] = "0"
}
create.Params["CONTAINER_PACKAGE_URL"] = create.Source
create.Params["NODE_APP_PORT"] = create.Params["APP_PORT"]
composeContent, err = handleCompose(env, composeContent, create, projectDir)
if err != nil {
return
}
case constant.RuntimeJava:
create.Params["CODE_DIR"] = create.CodeDir
create.Params["JAVA_VERSION"] = create.Version
create.Params["PANEL_APP_PORT_HTTP"] = create.Port
create.Params["JAVA_APP_PORT"] = create.Params["APP_PORT"]
composeContent, err = handleCompose(env, composeContent, create, projectDir)
if err != nil {
return
}
case constant.RuntimeGo:
create.Params["CODE_DIR"] = create.CodeDir
create.Params["GO_VERSION"] = create.Version
create.Params["PANEL_APP_PORT_HTTP"] = create.Port
create.Params["GO_APP_PORT"] = create.Params["APP_PORT"]
composeContent, err = handleCompose(env, composeContent, create, projectDir)
if err != nil {
return
}
case constant.RuntimePython:
create.Params["CODE_DIR"] = create.CodeDir
create.Params["PYTHON_VERSION"] = create.Version
create.Params["PANEL_APP_PORT_HTTP"] = create.Port
composeContent, err = handleCompose(env, composeContent, create, projectDir)
if err != nil {
return
}
case constant.RuntimeDotNet:
create.Params["CODE_DIR"] = create.CodeDir
create.Params["DOTNET_VERSION"] = create.Version
create.Params["PANEL_APP_PORT_HTTP"] = create.Port
composeContent, err = handleCompose(env, composeContent, create, projectDir)
if err != nil {
return
Expand Down Expand Up @@ -440,17 +432,7 @@ func handleCompose(env gotenv.Env, composeContent []byte, create request.Runtime
_, ok := serviceValue["ports"].([]interface{})
if ok {
var ports []interface{}
switch create.Type {
case constant.RuntimeNode:
ports = append(ports, "${HOST_IP}:${PANEL_APP_PORT_HTTP}:${NODE_APP_PORT}")
case constant.RuntimeJava:
ports = append(ports, "${HOST_IP}:${PANEL_APP_PORT_HTTP}:${JAVA_APP_PORT}")
case constant.RuntimeGo:
ports = append(ports, "${HOST_IP}:${PANEL_APP_PORT_HTTP}:${GO_APP_PORT}")
case constant.RuntimePython, constant.RuntimeDotNet:
ports = append(ports, "${HOST_IP}:${PANEL_APP_PORT_HTTP}:${APP_PORT}")

}
ports = append(ports, "${HOST_IP}:${PANEL_APP_PORT_HTTP}:${APP_PORT}")
for i, port := range create.ExposedPorts {
containerPortStr := fmt.Sprintf("CONTAINER_PORT_%d", i)
hostPortStr := fmt.Sprintf("HOST_PORT_%d", i)
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/lang/modules/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2374,7 +2374,7 @@ const message = {
appPort: '应用端口',
externalPort: '外部映射端口',
packageManager: '包管理器',
codeDir: '码目录',
codeDir: '目目录',
appPortHelper: '应用端口是指容器内部的端口',
externalPortHelper: '外部映射端口是指容器对外暴露的端口',
runScript: '启动命令',
Expand Down
119 changes: 119 additions & 0 deletions frontend/src/views/website/runtime/app/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<template>
<el-form-item :label="$t('runtime.app')" prop="appID">
<el-row :gutter="20">
<el-col :span="12">
<el-select
v-model="runtime.appID"
:disabled="mode === 'edit'"
@change="changeApp(runtime.appID)"
class="p-w-200"
>
<el-option v-for="(app, index) in apps" :key="index" :label="app.name" :value="app.id"></el-option>
</el-select>
</el-col>
<el-col :span="12">
<el-select
v-model="runtime.version"
:disabled="mode === 'edit'"
@change="changeVersion()"
class="p-w-200"
>
<el-option
v-for="(version, index) in appVersions"
:key="index"
:label="version"
:value="version"
></el-option>
</el-select>
</el-col>
</el-row>
</el-form-item>
</template>

<script setup lang="ts">
import { App } from '@/api/interface/app';
import { GetApp, GetAppDetail, SearchApp } from '@/api/modules/app';
import { useVModel } from '@vueuse/core';
import { defineProps } from 'vue';
const props = defineProps({
mode: {
type: String,
required: true,
},
appKey: {
type: String,
required: true,
},
modelValue: {
type: Object,
required: true,
},
});
const apps = ref<App.App[]>([]);
const appVersions = ref<string[]>([]);
const emit = defineEmits(['update:modelValue']);
const runtime = useVModel(props, 'modelValue', emit);
const appReq = reactive({
type: props.appKey,
page: 1,
pageSize: 20,
resource: 'remote',
});
const changeApp = (appID: number) => {
for (const app of apps.value) {
if (app.id === appID) {
getApp(app.key, props.mode);
break;
}
}
};
const changeVersion = async () => {
try {
const res = await GetAppDetail(runtime.value.appID, runtime.value.version, 'runtime');
runtime.value.appDetailID = res.data.id;
} catch (error) {}
};
const getApp = async (appkey: string, mode: string) => {
try {
const res = await GetApp(appkey);
appVersions.value = res.data.versions || [];
if (res.data.versions.length > 0) {
if (mode === 'create') {
runtime.value.version = res.data.versions[0];
changeVersion();
}
}
} catch (error) {}
};
const searchApp = async (appID: number) => {
try {
const res = await SearchApp(appReq);
apps.value = res.data.items || [];
if (res.data && res.data.items && res.data.items.length > 0) {
if (appID == null) {
runtime.value.appID = res.data.items[0].id;
getApp(res.data.items[0].key, props.mode);
} else {
res.data.items.forEach((item) => {
if (item.id === appID) {
getApp(item.key, props.mode);
}
});
}
}
} catch (error) {}
};
onMounted(() => {
if (props.mode === 'create') {
searchApp(null);
} else {
searchApp(runtime.value.appID);
}
});
</script>
Loading

0 comments on commit c6346b7

Please sign in to comment.