Skip to content
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

Open
wants to merge 6 commits into
base: builder-stack
Choose a base branch
from
Open
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
23 changes: 22 additions & 1 deletion cnb-builder-shim/cmd/dev-launcher/launch/supervisorctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ redirect_stderr = true
var reloadScript = fmt.Sprintf(`#!/bin/bash

socket_file="%[1]s/supervisor.sock"
# 检查supervisor的socket文件是否存在
# 检查 supervisor 的 socket 文件是否存在
if [ -S "$socket_file" ]; then
echo "supervisord is already running. update and restart processes..."
supervisorctl -c %[2]s reload
Expand All @@ -70,6 +70,15 @@ else
fi
`, supervisorDir, confFilePath)

var statusScript = fmt.Sprintf(`#!/bin/bash

socket_file="%[1]s/supervisor.sock"
# 检查 supervisor 的 socket 文件是否存在
if [ -S "$socket_file" ]; then
supervisorctl -c %[2]s status
fi
`, supervisorDir, confFilePath)

// ProcessConf is a process config
type ProcessConf struct {
Process
Expand Down Expand Up @@ -161,6 +170,18 @@ func (ctl *SupervisorCtl) reload() error {
return cmd.Run()
}

// Status get the status of all processes by running 'supervisorctl status'.
func (ctl *SupervisorCtl) Status() error {
cmd := exec.Command("bash")

cmd.Env = os.Environ()
cmd.Stdin = bytes.NewBufferString(statusScript)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout

return cmd.Run()
}

// validateEnvironment validates the environment variables for supervisor conf.
//
// see detail environment conf in http://supervisord.org/configuration.html
Expand Down
34 changes: 2 additions & 32 deletions cnb-builder-shim/cmd/dev-launcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,9 @@
package main

import (
"os"
"path/filepath"

"github.com/BurntSushi/toml"
"github.com/buildpacks/lifecycle/launch"

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/TencentBlueking/bkpaas/cnb-builder-shim/cmd/dev-launcher/subcmd"
)

// DefaultAppDir default app dir
var DefaultAppDir = utils.EnvOrDefault("CNB_APP_DIR", "/app")

func main() {
logger := logging.Default()

var md launch.Metadata

if _, err := toml.DecodeFile(launch.GetMetadataFilePath("/layers"), &md); err != nil {
logger.Error(err, "read metadata")
os.Exit(1)
}

appDesc, err := appdesc.UnmarshalToAppDesc(filepath.Join(DefaultAppDir, "app_desc.yaml"))
if err != nil {
logger.Error(err, "parse invalid app_desc.yaml")
os.Exit(1)
}

if err = devlaunch.Run(md.Processes, appDesc); err != nil {
logger.Error(err, "hot launch")
os.Exit(1)
}
subcmd.Execute()
}
66 changes: 66 additions & 0 deletions cnb-builder-shim/cmd/dev-launcher/subcmd/launch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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 subcmd

import (
"path/filepath"

"github.com/BurntSushi/toml"
"github.com/buildpacks/lifecycle/launch"
"github.com/spf13/cobra"

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"
)

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, "failed to hot launch")
return err
}
return nil
},
}

func init() {
rootCmd.AddCommand(reloadCmd)
}
41 changes: 41 additions & 0 deletions cnb-builder-shim/cmd/dev-launcher/subcmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 subcmd

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.`,
Run: func(cmd *cobra.Command, args []string) {
Copy link
Collaborator

@jamesgetx jamesgetx Nov 7, 2024

Choose a reason for hiding this comment

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

这段提示怎么理解有误导,跑了还是没跑?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

reload、status、stop 等操作应该是一个级别,一般 root 就是不放东西的
可以参考 bkpaas-cli :

console.Info("Hello %s, welcome to use bkpaas-cli, use `bkpaas-cli -h` for help", cliConf.G.Username)

Copy link
Collaborator

Choose a reason for hiding this comment

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

没跑的话,就不要用 run 这个词,确实容易有误导

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

没跑的话,就不要用 run 这个词,确实容易有误导

已修改

fmt.Println("dev-launcher manages processes defined by app_desc")
},
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
38 changes: 38 additions & 0 deletions cnb-builder-shim/cmd/dev-launcher/subcmd/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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 subcmd

import (
"github.com/spf13/cobra"

"github.com/TencentBlueking/bkpaas/cnb-builder-shim/cmd/dev-launcher/launch"
)

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)
}
2 changes: 2 additions & 0 deletions cnb-builder-shim/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ require (
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
github.com/iand/logfmtr v0.2.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.1 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
Expand All @@ -62,6 +63,7 @@ require (
github.com/pierrec/lz4/v4 v4.1.17 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/cobra v1.8.1 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
Expand Down
6 changes: 6 additions & 0 deletions cnb-builder-shim/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
github.com/containerd/stargz-snapshotter/estargz v0.14.3 h1:OqlDCK3ZVUO6C3B/5FSkDwbkEETK84kQgEeFwDC+62k=
github.com/containerd/stargz-snapshotter/estargz v0.14.3/go.mod h1:KY//uOCIkSuNAHhJogcZtrNHdKrA99/FCCRjE3HD36o=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down Expand Up @@ -107,6 +108,8 @@ github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/iand/logfmtr v0.2.1 h1:y677w6gmOdGXaPbIGjQhGZdmezpCo0AyxXC74scyDk0=
github.com/iand/logfmtr v0.2.1/go.mod h1:J1K526bF1o/E0Xq01J0vk7pLEfHkgGs8vmdbxH4wqwU=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
Expand Down Expand Up @@ -174,12 +177,15 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sclevine/spec v1.4.0 h1:z/Q9idDcay5m5irkZ28M7PtQM4aOISzOpj4bUPkDee8=
github.com/sclevine/spec v1.4.0/go.mod h1:LvpgJaFyvQzRvc1kaDs0bulYwzC70PbiYjC4QnFHkOM=
github.com/shabbywu/logfmtr v0.2.3 h1:RFkAyvcY93lIAl0tkjWz3ludHODZMRJCwihSgVn3b6A=
github.com/shabbywu/logfmtr v0.2.3/go.mod h1:cGpwyHM894YO5s/XwNvcm+bUuXTSoWg9jAaOV00qMMI=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
4 changes: 2 additions & 2 deletions cnb-builder-shim/internal/devsandbox/phase/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
const DefaultDevLauncherPath = "/cnb/devsandbox/bin/dev-launcher"

// MakeLauncherCmd make launcher cmd
func MakeLauncherCmd() *exec.Cmd {
cmd := exec.Command(DefaultDevLauncherPath)
func MakeLauncherCmd(subCommand string) *exec.Cmd {
cmd := exec.Command(DefaultDevLauncherPath, subCommand)
cmd.Env = os.Environ()
return cmd
}
4 changes: 3 additions & 1 deletion cnb-builder-shim/internal/devsandbox/reload.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ var (
ReloadDir = "/cnb/devsandbox/reload"
// ReloadLogDir used to store reload log
ReloadLogDir = path.Join(ReloadDir, "log")
// reload sub command of dev-launcher
reloadSubCommand = "reload"
)

// ReloadStatus is the status of a reload operation.
Expand Down Expand Up @@ -74,7 +76,7 @@ func (m HotReloadManager) Rebuild(reloadID string) error {

// Relaunch ...
func (m HotReloadManager) Relaunch(reloadID string) error {
cmd := phase.MakeLauncherCmd()
cmd := phase.MakeLauncherCmd(reloadSubCommand)
return m.runCmd(reloadID, cmd)
}

Expand Down
Loading