-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: dev-sandbox 获取进程列表,获取进程状态 #1701
base: builder-stack
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* TencentBlueKing is pleased to support the open source community by making | ||
* 蓝鲸智云 - PaaS 平台 (BlueKing - PaaS System) available. | ||
* Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. | ||
* Licensed under the MIT License (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://opensource.org/licenses/MIT | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied. See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* We undertake not to change the open source license (MIT license) applicable | ||
* to the current version of the project delivered to anyone in the future. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"github.com/BurntSushi/toml" | ||
devlaunch "github.com/TencentBlueking/bkpaas/cnb-builder-shim/cmd/dev-launcher/launch" | ||
"github.com/TencentBlueking/bkpaas/cnb-builder-shim/pkg/appdesc" | ||
"github.com/TencentBlueking/bkpaas/cnb-builder-shim/pkg/logging" | ||
"github.com/TencentBlueking/bkpaas/cnb-builder-shim/pkg/utils" | ||
"github.com/buildpacks/lifecycle/launch" | ||
"github.com/spf13/cobra" | ||
SheepSheepChen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
var DefaultAppDir = utils.EnvOrDefault("CNB_APP_DIR", "/app") | ||
|
||
var reloadCmd = &cobra.Command{ | ||
Use: "reload", | ||
Short: "reload processes.", | ||
Long: "reload the given launch.Process list.", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
logger := logging.Default() | ||
|
||
var md launch.Metadata | ||
|
||
if _, err := toml.DecodeFile(launch.GetMetadataFilePath("/layers"), &md); err != nil { | ||
logger.Error(err, "read metadata") | ||
return err | ||
} | ||
|
||
appDesc, err := appdesc.UnmarshalToAppDesc(filepath.Join(DefaultAppDir, "app_desc.yaml")) | ||
if err != nil { | ||
logger.Error(err, "parse invalid app_desc.yaml") | ||
return err | ||
} | ||
|
||
if err = devlaunch.Run(md.Processes, appDesc); err != nil { | ||
logger.Error(err, "hot launch") | ||
SheepSheepChen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return err | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(reloadCmd) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* TencentBlueKing is pleased to support the open source community by making | ||
* 蓝鲸智云 - PaaS 平台 (BlueKing - PaaS System) available. | ||
* Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. | ||
* Licensed under the MIT License (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://opensource.org/licenses/MIT | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied. See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* We undertake not to change the open source license (MIT license) applicable | ||
* to the current version of the project delivered to anyone in the future. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "dev-launcher", | ||
Short: "dev-launcher cli", | ||
Long: `Manage processes defined by app_desc, including | ||
reload, getting status, stopping.`, | ||
SheepSheepChen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Println("run dev-launcher...") | ||
}, | ||
} | ||
|
||
func Execute() { | ||
if err := rootCmd.Execute(); err != nil { | ||
os.Exit(1) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* TencentBlueKing is pleased to support the open source community by making | ||
* 蓝鲸智云 - PaaS 平台 (BlueKing - PaaS System) available. | ||
* Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. | ||
* Licensed under the MIT License (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://opensource.org/licenses/MIT | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied. See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* We undertake not to change the open source license (MIT license) applicable | ||
* to the current version of the project delivered to anyone in the future. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/TencentBlueking/bkpaas/cnb-builder-shim/cmd/dev-launcher/launch" | ||
"github.com/spf13/cobra" | ||
SheepSheepChen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
var statusCmd = &cobra.Command{ | ||
Use: "status", | ||
Short: "process status.", | ||
Long: "Get status of all processes.", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return launch.NewSupervisorCtl().Status() | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(statusCmd) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* TencentBlueKing is pleased to support the open source community by making | ||
* 蓝鲸智云 - PaaS 平台 (BlueKing - PaaS System) available. | ||
* Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. | ||
* Licensed under the MIT License (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://opensource.org/licenses/MIT | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied. See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* We undertake not to change the open source license (MIT license) applicable | ||
* to the current version of the project delivered to anyone in the future. | ||
*/ | ||
|
||
package devsandbox | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"strings" | ||
|
||
"github.com/TencentBlueking/bkpaas/cnb-builder-shim/internal/devsandbox/phase" | ||
) | ||
|
||
var statusSubCommand = "status" | ||
|
||
// Status returns the status of all processes. | ||
func Status() (map[string]string, error) { | ||
statusOutput, err := status() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return parseStatusOutput(statusOutput), nil | ||
} | ||
|
||
func status() (string, error) { | ||
var outBuffer bytes.Buffer | ||
|
||
cmd := phase.MakeLauncherCmd(statusSubCommand) | ||
cmd.Stdin = os.Stdin | ||
cmd.Stdout = &outBuffer | ||
cmd.Stderr = os.Stderr | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stderr 要输出么,这里是直接吞掉,没有暴露给用户? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 通过下面的 err 处理可能更清晰一点? |
||
|
||
err := cmd.Run() | ||
if err != nil { | ||
return "", err | ||
} | ||
return outBuffer.String(), nil | ||
} | ||
|
||
// parses the output from the `supervisorctl status` command. | ||
// The expected format of each line in the output is: | ||
// <process_name> <process_state> <description> | ||
// | ||
// Parameters: | ||
// - output: A string containing the entire output from the `supervisorctl status` command. | ||
// | ||
// Returns: | ||
// - A map with process names as keys and their states as values. | ||
func parseStatusOutput(output string) map[string]string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 看到这里的解析命令,想到考虑后续通过 http 或者 xmlrpc 完成与 supervisor 的通信。这样甚至去除dev-launcher 这个二进制,直接集成到 dev-entrypoint 中 |
||
result := make(map[string]string) | ||
|
||
// 按行分割输出 | ||
lines := strings.Split(output, "\n") | ||
for _, line := range lines { | ||
// 移除空白符并检查是否为空行 | ||
line = strings.TrimSpace(line) | ||
if line == "" { | ||
continue | ||
} | ||
|
||
// 按空格分割,格式为 "<process_name> <process_state> ..." | ||
parts := strings.Fields(line) | ||
if len(parts) < 2 { | ||
continue // 如果格式不符合预期,跳过该行 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不要写行内注释 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} | ||
// 提取进程名称和状态 | ||
processName := parts[0] | ||
processState := parts[1] | ||
|
||
result[processName] = processState | ||
} | ||
|
||
return result | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cmd 里面再来一个 cmd,这个包层级可以优化下吗,并且这个 cmd 和 luanch 又是在同一级。如果你考虑 sub command 方式,建议按子命令把层级调整清晰一些。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dev-launcher 本身是一个拥有 main.go 的程序,所以他的包下面有 cmd 文件夹是不是也可以?
'cmd 和 luanch 又是在同一级' 这个问题,那把 luanch 转移到 pkg 里面吗?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
将目录 dev-launch.cmd 改为 dev-launch.subcmd
reload 操作还是放在了 reload 子命令,感觉修改为子命令形式后,不适合用 root 做 reload 操作