Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions agent/app/dto/request/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ type Volume struct {
}

type ExposedPort struct {
HostPort int `json:"hostPort"`
ContainerPort int `json:"containerPort"`
HostPort int `json:"hostPort"`
ContainerPort int `json:"containerPort"`
HostIP string `json:"hostIP"`
}

type RuntimeDelete struct {
Expand Down
2 changes: 1 addition & 1 deletion agent/app/dto/response/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type RuntimeDTO struct {
CreatedAt time.Time `json:"createdAt"`
CodeDir string `json:"codeDir"`
AppParams []AppParam `json:"appParams"`
Port int `json:"port"`
Port string `json:"port"`
Path string `json:"path"`
ExposedPorts []request.ExposedPort `json:"exposedPorts"`
Environments []request.Environment `json:"environments"`
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 @@ -19,7 +19,7 @@ type Runtime struct {
Type string `gorm:"not null" json:"type"`
Status string `gorm:"not null" json:"status"`
Resource string `gorm:"not null" json:"resource"`
Port int `json:"port"`
Port string `json:"port"`
Message string `json:"message"`
CodeDir string `json:"codeDir"`
ContainerName string `json:"containerName"`
Expand Down
6 changes: 6 additions & 0 deletions agent/app/repo/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ func WithByID(id uint) DBOption {
}
}

func WithByNOTID(id uint) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("id != ?", id)
}
}

func WithByIDs(ids []uint) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("id in (?)", ids)
Expand Down
5 changes: 4 additions & 1 deletion agent/app/repo/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package repo

import (
"context"
"fmt"

"github.com/1Panel-dev/1Panel/agent/app/model"
"github.com/1Panel-dev/1Panel/agent/global"
Expand Down Expand Up @@ -55,7 +56,9 @@ func (r *RuntimeRepo) WithNotId(id uint) DBOption {

func (r *RuntimeRepo) WithPort(port int) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("port = ?", port)
portStr := fmt.Sprintf("%d", port)
regexPattern := fmt.Sprintf("(^|,)%s(,|$)", portStr)
return g.Where("port REGEXP ?", regexPattern)
}
}

Expand Down
72 changes: 29 additions & 43 deletions agent/app/service/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (r *RuntimeService) Create(create request.RuntimeCreate) (*model.Runtime, e
return nil, err
}
}

var hostPorts []string
switch create.Type {
case constant.RuntimePHP:
if create.Resource == constant.ResourceLocal {
Expand All @@ -116,17 +116,12 @@ func (r *RuntimeService) Create(create request.RuntimeCreate) (*model.Runtime, e
}
create.Install = true
for _, export := range create.ExposedPorts {
hostPorts = append(hostPorts, strconv.Itoa(export.HostPort))
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 @@ -159,7 +154,7 @@ func (r *RuntimeService) Create(create request.RuntimeCreate) (*model.Runtime, e
Resource: create.Resource,
Version: create.Version,
ContainerName: containerName.(string),
Port: int(portValue.(float64)),
Port: strings.Join(hostPorts, ","),
}

switch create.Type {
Expand Down Expand Up @@ -352,34 +347,30 @@ func (r *RuntimeService) Get(id uint) (*response.RuntimeDTO, error) {
return nil, err
}
for k, v := range envs {
switch k {
case "APP_PORT", "PANEL_APP_PORT_HTTP":
port, err := strconv.Atoi(v)
if err != nil {
return nil, err
}
res.Params[k] = port
default:
if strings.Contains(k, "CONTAINER_PORT") || strings.Contains(k, "HOST_PORT") {
if strings.Contains(k, "CONTAINER_PORT") {
r := regexp.MustCompile(`_(\d+)$`)
matches := r.FindStringSubmatch(k)
containerPort, err := strconv.Atoi(v)
if err != nil {
return nil, err
}
hostPort, err := strconv.Atoi(envs[fmt.Sprintf("HOST_PORT_%s", matches[1])])
if err != nil {
return nil, err
}
res.ExposedPorts = append(res.ExposedPorts, request.ExposedPort{
ContainerPort: containerPort,
HostPort: hostPort,
})
if strings.Contains(k, "CONTAINER_PORT") || strings.Contains(k, "HOST_PORT") {
if strings.Contains(k, "CONTAINER_PORT") {
r := regexp.MustCompile(`_(\d+)$`)
matches := r.FindStringSubmatch(k)
containerPort, err := strconv.Atoi(v)
if err != nil {
return nil, err
}
} else {
res.Params[k] = v
hostPort, err := strconv.Atoi(envs[fmt.Sprintf("HOST_PORT_%s", matches[1])])
if err != nil {
return nil, err
}
hostIP := envs[fmt.Sprintf("HOST_IP_%s", matches[1])]
if hostIP == "" {
hostIP = "0.0.0.0"
}
res.ExposedPorts = append(res.ExposedPorts, request.ExposedPort{
ContainerPort: containerPort,
HostPort: hostPort,
HostIP: hostIP,
})
}
} else {
res.Params[k] = v
}
}
if v, ok := envs["CONTAINER_PACKAGE_URL"]; ok {
Expand Down Expand Up @@ -437,22 +428,17 @@ func (r *RuntimeService) Update(req request.RuntimeUpdate) error {
}
oldImage := runtime.Image
oldEnv := runtime.Env
port := int(req.Params["PANEL_APP_PORT_HTTP"].(float64))
var hostPorts []string
switch runtime.Type {
case constant.RuntimePHP:
exist, _ := runtimeRepo.GetFirst(runtimeRepo.WithImage(req.Name), runtimeRepo.WithNotId(req.ID))
if exist != nil {
return buserr.New("ErrImageExist")
}
case constant.RuntimeNode, constant.RuntimeJava, constant.RuntimeGo, constant.RuntimePython, constant.RuntimeDotNet:
if runtime.Port != port {
if err = checkPortExist(port); err != nil {
return err
}
runtime.Port = port
}
for _, export := range req.ExposedPorts {
if err = checkPortExist(export.HostPort); err != nil {
hostPorts = append(hostPorts, strconv.Itoa(export.HostPort))
if err = checkRuntimePortExist(export.HostPort, false, runtime.ID); err != nil {
return err
}
}
Expand Down Expand Up @@ -522,7 +508,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 = port
runtime.Port = strings.Join(hostPorts, ",")
runtime.Status = constant.RuntimeReCreating
_ = runtimeRepo.Save(runtime)
go reCreateRuntime(runtime)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no regularity in the provided code, any issues or optimization suggestions found need to be evaluated individually and specific context or requirements will dictate what adjustments might be necessary.

In general though:

  • No errors were thrown, so you don't have an issue with your code at runtime.
  • You can improve readability/organization/maintainability using some Go best practices: use package main in first line of every file, remove unused imports. Also consider comments that explain functions' logic for maintenance convenience. Use type hints everywhere if possible - this helps IDE understand types easily.
  • For performance/caching purposes it is beneficial to cache frequently used string values.

This code does not show irregularities per se but these points could enhance its clarity/security/maintenance.

To optimize further see if there's a more concise way of writing some parts like checking ports etc. Remember however most of the time the quality metric is about maintainability and understanding instead of optimizing just for efficiency.

The code doesn't contain any syntax errors I checked.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seem to be no errors in the provided code snippet. No changes have been made since its previous version (not shown or mentioned earlier). Therefore, there's nothing to check for discrepancies between versions.

The current function appears to handle runtime creation and updates without encountering issues that need addressing at this time given it is currently functioning with the latest knowledge based on September 1st of last year.

However, if you have a specific problem related to the use of ports being checked against already existing ones in checkPortExist or a different part of the code, please specify those points so I could assist further.

Expand Down
44 changes: 39 additions & 5 deletions agent/app/service/runtime_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/1Panel-dev/1Panel/agent/i18n"
"github.com/1Panel-dev/1Panel/agent/utils/common"
"io"
"os"
"os/exec"
Expand Down Expand Up @@ -319,7 +321,7 @@ func handleParams(create request.RuntimeCreate, projectDir string) (composeConte
return
}
for k := range env {
if strings.HasPrefix(k, "CONTAINER_PORT_") || strings.HasPrefix(k, "HOST_PORT_") {
if strings.HasPrefix(k, "CONTAINER_PORT_") || strings.HasPrefix(k, "HOST_PORT_") || strings.HasPrefix(k, "HOST_IP_") || strings.Contains(k, "APP_PORT") {
delete(env, k)
}
}
Expand Down Expand Up @@ -431,18 +433,19 @@ func handleCompose(env gotenv.Env, composeContent []byte, create request.Runtime
for name, service := range services {
serviceName = name
serviceValue = service.(map[string]interface{})
_, ok := serviceValue["ports"].([]interface{})
if ok {
delete(serviceValue, "ports")
if create.ExposedPorts != nil && len(create.ExposedPorts) > 0 {
var ports []interface{}
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)
existMap[containerPortStr] = struct{}{}
existMap[hostPortStr] = struct{}{}
ports = append(ports, fmt.Sprintf("${HOST_IP}:${%s}:${%s}", hostPortStr, containerPortStr))
hostIPStr := fmt.Sprintf("HOST_IP_%d", i)
ports = append(ports, fmt.Sprintf("${%s}:${%s}:${%s}", hostIPStr, hostPortStr, containerPortStr))
create.Params[containerPortStr] = port.ContainerPort
create.Params[hostPortStr] = port.HostPort
create.Params[hostIPStr] = port.HostIP
}
serviceValue["ports"] = ports
}
Expand Down Expand Up @@ -667,3 +670,34 @@ func getDockerComposeVolumes(yml []byte) ([]request.Volume, error) {
}
return res, nil
}

func checkRuntimePortExist(port int, scanPort bool, runtimeID uint) error {
errMap := make(map[string]interface{})
errMap["port"] = port
appInstall, _ := appInstallRepo.GetFirst(appInstallRepo.WithPort(port))
if appInstall.ID > 0 {
errMap["type"] = i18n.GetMsgByKey("TYPE_APP")
errMap["name"] = appInstall.Name
return buserr.WithMap("ErrPortExist", errMap, nil)
}
opts := []repo.DBOption{runtimeRepo.WithPort(port)}
if runtimeID > 0 {
opts = append(opts, repo.WithByNOTID(runtimeID))
}
runtime, _ := runtimeRepo.GetFirst(opts...)
if runtime != nil {
errMap["type"] = i18n.GetMsgByKey("TYPE_RUNTIME")
errMap["name"] = runtime.Name
return buserr.WithMap("ErrPortExist", errMap, nil)
}
domain, _ := websiteDomainRepo.GetFirst(websiteDomainRepo.WithPort(port))
if domain.ID > 0 {
errMap["type"] = i18n.GetMsgByKey("TYPE_DOMAIN")
errMap["name"] = domain.Domain
return buserr.WithMap("ErrPortExist", errMap, nil)
}
if scanPort && common.ScanPort(port) {
return buserr.WithDetail("ErrPortInUsed", port, nil)
}
return nil
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code snippet is not related to your question and doesn't contain relevant information. Please provide more context if you want me to analyze code variations within this particular context.

To give a concise opinion for such an analysis, I'd need a clear set of source code which contains some variation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code appears to be part of a larger implementation related to Docker Compose handling in an application development environment. The context here is not clear; thus, no obvious differences can be detected between current and previous versions based on the information you've shared about knowledge cutoff or current date.

If you need assistance with specific issues or discrepancies, please clarify what particular parts or features you are interested in comparing between different versions.

2 changes: 1 addition & 1 deletion agent/cmd/server/conf/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ base:
install_dir: /opt
mode: dev

remote_rul:
remote_url:
repo_url: https://resource.fit2cloud.com/1panel/package
resource_url: https://resource.fit2cloud.com/1panel/resource
app_repo: https://apps-assets.fit2cloud.com
Expand Down
1 change: 1 addition & 0 deletions frontend/src/api/interface/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export namespace Runtime {
export interface ExposedPort {
hostPort: number;
containerPort: number;
hostIP: string;
}

export interface Environment {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/drawer-pro/index.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<el-drawer v-model="localOpenPage" :destroy-on-close="true" :close-on-click-modal="false" :size="size">
<el-drawer v-model="localOpenPage" :destroy-on-close="true" :size="size">
<template #header>
<el-page-header @back="handleBack">
<template #content>
Expand Down
3 changes: 0 additions & 3 deletions frontend/src/views/app-store/setting/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,8 @@ import { FormRules } from 'element-plus';
import CustomSetting from '@/xpack/views/appstore/index.vue';
import DefaultDomain from './default-domain/index.vue';
import { GlobalStore } from '@/store';
import { storeToRefs } from 'pinia';

const globalStore = GlobalStore();
const { isProductPro } = storeToRefs(globalStore);

const rules = ref<FormRules>({
defaultDomain: [Rules.domainOrIP],
});
Expand Down
8 changes: 6 additions & 2 deletions frontend/src/views/website/runtime/dotnet/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@
<el-table-column :label="$t('runtime.version')" prop="version"></el-table-column>
<el-table-column :label="$t('runtime.externalPort')" prop="port" min-width="110px">
<template #default="{ row }">
{{ row.port }}
<el-button link :icon="Promotion" @click="goDashboard(row.port, 'http')"></el-button>
<span v-for="(port, index) in row.port.split(',')" :key="index">
<el-button link @click="goDashboard(port, 'http')">
{{ port }}
<el-icon class="el-icon--right"><Promotion /></el-icon>
</el-button>
</span>
</template>
</el-table-column>
<el-table-column :label="$t('commons.table.status')" prop="status">
Expand Down
2 changes: 0 additions & 2 deletions frontend/src/views/website/runtime/dotnet/operate/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ const submit = async (formEl: FormInstance | undefined) => {
if (runtime.exposedPorts && runtime.exposedPorts.length > 0) {
const containerPortMap = new Map();
const hostPortMap = new Map();
containerPortMap[runtime.params['APP_PORT']] = true;
hostPortMap[runtime.port] = true;
for (const port of runtime.exposedPorts) {
if (containerPortMap[port.containerPort]) {
MsgError(i18n.global.t('runtime.portError'));
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/views/website/runtime/environment/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
</el-col>
<el-col :span="4">
<el-form-item>
<el-button type="primary" @click="removeEnv(index)" link>
<el-button type="primary" @click="removeEnv(index)" link class="mt-1">
{{ $t('commons.button.delete') }}
</el-button>
</el-form-item>
</el-col>
</el-row>
<el-row :gutter="20">
<el-col :span="4">
<el-button @click="addEnv">{{ $t('commons.button.add') }}{{ $t('runtime.environment') }}</el-button>
<el-button @click="addEnv">{{ $t('commons.button.add') }}</el-button>
</el-col>
</el-row>
</div>
Expand Down
8 changes: 6 additions & 2 deletions frontend/src/views/website/runtime/go/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@
<el-table-column :label="$t('runtime.version')" prop="version"></el-table-column>
<el-table-column :label="$t('runtime.externalPort')" prop="port" min-width="110px">
<template #default="{ row }">
{{ row.port }}
<el-button link :icon="Promotion" @click="goDashboard(row.port, 'http')"></el-button>
<span v-for="(port, index) in row.port.split(',')" :key="index">
<el-button link @click="goDashboard(port, 'http')">
{{ port }}
<el-icon class="el-icon--right"><Promotion /></el-icon>
</el-button>
</span>
</template>
</el-table-column>
<el-table-column :label="$t('commons.table.status')" prop="status">
Expand Down
2 changes: 0 additions & 2 deletions frontend/src/views/website/runtime/go/operate/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@ const submit = async (formEl: FormInstance | undefined) => {
if (runtime.exposedPorts && runtime.exposedPorts.length > 0) {
const containerPortMap = new Map();
const hostPortMap = new Map();
containerPortMap[runtime.params['APP_PORT']] = true;
hostPortMap[runtime.port] = true;
for (const port of runtime.exposedPorts) {
if (containerPortMap[port.containerPort]) {
MsgError(i18n.global.t('runtime.portError'));
Expand Down
8 changes: 6 additions & 2 deletions frontend/src/views/website/runtime/java/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@
<el-table-column :label="$t('runtime.version')" prop="version"></el-table-column>
<el-table-column :label="$t('runtime.externalPort')" prop="port" min-width="110px">
<template #default="{ row }">
{{ row.port }}
<el-button link :icon="Promotion" @click="goDashboard(row.port, 'http')"></el-button>
<span v-for="(port, index) in row.port.split(',')" :key="index">
<el-button link @click="goDashboard(port, 'http')">
{{ port }}
<el-icon class="el-icon--right"><Promotion /></el-icon>
</el-button>
</span>
</template>
</el-table-column>
<el-table-column :label="$t('commons.table.status')" prop="status">
Expand Down
2 changes: 0 additions & 2 deletions frontend/src/views/website/runtime/java/operate/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ const submit = async (formEl: FormInstance | undefined) => {
if (runtime.exposedPorts && runtime.exposedPorts.length > 0) {
const containerPortMap = new Map();
const hostPortMap = new Map();
containerPortMap[runtime.params['APP_PORT']] = true;
hostPortMap[runtime.port] = true;
for (const port of runtime.exposedPorts) {
if (containerPortMap[port.containerPort]) {
MsgError(i18n.global.t('runtime.portError'));
Expand Down
Loading
Loading