Skip to content

Commit 4836b5a

Browse files
committed
补充修改启动脚本中端口的逻辑
1 parent d4aa137 commit 4836b5a

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

internal/deploy/service/deploy_service.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,12 @@ func (f *floyDeployService) processPackageWithPort(packagePath, serviceName stri
12701270
return "", nil, fmt.Errorf("修改配置文件失败: %v", err)
12711271
}
12721272

1273+
// 3.1 修改start.sh脚本中的端口逻辑
1274+
err = f.modifyStartScript(tempDir, port)
1275+
if err != nil {
1276+
return "", nil, fmt.Errorf("修改启动脚本失败: %v", err)
1277+
}
1278+
12731279
// 4. 重新打包到临时目录
12741280
tempPackagePath := filepath.Join(tempDir, "modified-"+filepath.Base(packagePath))
12751281
err = f.createTarGz(tempDir, tempPackagePath)
@@ -1396,6 +1402,54 @@ func (f *floyDeployService) modifyConfigPort(tempDir, serviceName string, port i
13961402
return nil
13971403
}
13981404

1405+
// modifyStartScript 修改启动脚本中的端口
1406+
func (f *floyDeployService) modifyStartScript(tempDir string, port int) error {
1407+
startScriptPath := filepath.Join(tempDir, "start.sh")
1408+
1409+
// 检查start.sh文件是否存在
1410+
if _, err := os.Stat(startScriptPath); os.IsNotExist(err) {
1411+
// 如果start.sh不存在,说明这个包可能没有start.sh脚本,跳过
1412+
return nil
1413+
}
1414+
1415+
// 读取start.sh文件内容
1416+
content, err := os.ReadFile(startScriptPath)
1417+
if err != nil {
1418+
return fmt.Errorf("读取启动脚本失败: %v", err)
1419+
}
1420+
1421+
// 检查是否包含环境变量SERVICE_PORT的设置
1422+
contentStr := string(content)
1423+
if !strings.Contains(contentStr, "SERVICE_PORT") {
1424+
// 如果start.sh不包含SERVICE_PORT,说明已经是新版本的脚本,跳过
1425+
return nil
1426+
}
1427+
1428+
// 替换环境变量设置,改为从配置文件读取端口
1429+
oldPattern := `export SERVICE_PORT="${SERVICE_PORT:-8080}"`
1430+
newPattern := fmt.Sprintf(`# 从配置文件读取端口
1431+
if [ -f "config.yaml" ]; then
1432+
SERVICE_PORT=$(grep -E "^\s*port:\s*" config.yaml | sed 's/.*port:\s*\([0-9]*\).*/\1/')
1433+
if [ -z "$SERVICE_PORT" ]; then
1434+
SERVICE_PORT="%d"
1435+
fi
1436+
else
1437+
SERVICE_PORT="%d"
1438+
fi`, port, port)
1439+
1440+
// 执行替换
1441+
newContent := strings.Replace(contentStr, oldPattern, newPattern, 1)
1442+
1443+
// 写回文件
1444+
err = os.WriteFile(startScriptPath, []byte(newContent), 0755)
1445+
if err != nil {
1446+
return fmt.Errorf("写入启动脚本失败: %v", err)
1447+
}
1448+
1449+
fmt.Printf("修改启动脚本端口逻辑,端口: %d\n", port)
1450+
return nil
1451+
}
1452+
13991453
// createTarGz 创建tar.gz文件
14001454
func (f *floyDeployService) createTarGz(src, dest string) error {
14011455
file, err := os.Create(dest)

0 commit comments

Comments
 (0)