From f9318c737573c0a85a35f2426ab678bd19d0281b Mon Sep 17 00:00:00 2001 From: dijiachen Date: Tue, 5 Nov 2024 16:50:09 +0800 Subject: [PATCH 1/6] =?UTF-8?q?feat:=E8=8E=B7=E5=8F=96=E8=BF=9B=E7=A8=8B?= =?UTF-8?q?=E5=88=97=E8=A1=A8=EF=BC=8C=E8=8E=B7=E5=8F=96=E8=BF=9B=E7=A8=8B?= =?UTF-8?q?=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cmd/dev-launcher/cmd/launch.go | 65 ++++++++++++++ cnb-builder-shim/cmd/dev-launcher/cmd/root.go | 42 +++++++++ .../cmd/dev-launcher/cmd/status.go | 37 ++++++++ .../cmd/dev-launcher/launch/supervisorctl.go | 21 +++++ cnb-builder-shim/cmd/dev-launcher/main.go | 34 +------ cnb-builder-shim/go.mod | 2 + cnb-builder-shim/go.sum | 6 ++ .../internal/devsandbox/phase/launcher.go | 4 +- .../internal/devsandbox/reload.go | 4 +- .../internal/devsandbox/status.go | 89 +++++++++++++++++++ .../internal/devsandbox/webserver/server.go | 29 ++++++ 11 files changed, 298 insertions(+), 35 deletions(-) create mode 100644 cnb-builder-shim/cmd/dev-launcher/cmd/launch.go create mode 100644 cnb-builder-shim/cmd/dev-launcher/cmd/root.go create mode 100644 cnb-builder-shim/cmd/dev-launcher/cmd/status.go create mode 100644 cnb-builder-shim/internal/devsandbox/status.go diff --git a/cnb-builder-shim/cmd/dev-launcher/cmd/launch.go b/cnb-builder-shim/cmd/dev-launcher/cmd/launch.go new file mode 100644 index 0000000000..53455ec6e1 --- /dev/null +++ b/cnb-builder-shim/cmd/dev-launcher/cmd/launch.go @@ -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" +) + +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") + return err + } + return nil + }, +} + +func init() { + rootCmd.AddCommand(reloadCmd) +} diff --git a/cnb-builder-shim/cmd/dev-launcher/cmd/root.go b/cnb-builder-shim/cmd/dev-launcher/cmd/root.go new file mode 100644 index 0000000000..f3a13c5b7b --- /dev/null +++ b/cnb-builder-shim/cmd/dev-launcher/cmd/root.go @@ -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.`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("run dev-launcher...") + }, +} + +func Execute() { + if err := rootCmd.Execute(); err != nil { + os.Exit(1) + } +} diff --git a/cnb-builder-shim/cmd/dev-launcher/cmd/status.go b/cnb-builder-shim/cmd/dev-launcher/cmd/status.go new file mode 100644 index 0000000000..952d192831 --- /dev/null +++ b/cnb-builder-shim/cmd/dev-launcher/cmd/status.go @@ -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" +) + +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) +} diff --git a/cnb-builder-shim/cmd/dev-launcher/launch/supervisorctl.go b/cnb-builder-shim/cmd/dev-launcher/launch/supervisorctl.go index 0fd9664231..cb93306e63 100644 --- a/cnb-builder-shim/cmd/dev-launcher/launch/supervisorctl.go +++ b/cnb-builder-shim/cmd/dev-launcher/launch/supervisorctl.go @@ -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 @@ -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 diff --git a/cnb-builder-shim/cmd/dev-launcher/main.go b/cnb-builder-shim/cmd/dev-launcher/main.go index f94db4b64e..2524c76218 100644 --- a/cnb-builder-shim/cmd/dev-launcher/main.go +++ b/cnb-builder-shim/cmd/dev-launcher/main.go @@ -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/cmd" ) -// 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) - } + cmd.Execute() } diff --git a/cnb-builder-shim/go.mod b/cnb-builder-shim/go.mod index bcb3b121fe..ee5fe1bb2c 100644 --- a/cnb-builder-shim/go.mod +++ b/cnb-builder-shim/go.mod @@ -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 @@ -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 diff --git a/cnb-builder-shim/go.sum b/cnb-builder-shim/go.sum index 7a920de8f6..783bb37904 100644 --- a/cnb-builder-shim/go.sum +++ b/cnb-builder-shim/go.sum @@ -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= @@ -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= @@ -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= diff --git a/cnb-builder-shim/internal/devsandbox/phase/launcher.go b/cnb-builder-shim/internal/devsandbox/phase/launcher.go index 1a8efc66d6..de0f6f598d 100644 --- a/cnb-builder-shim/internal/devsandbox/phase/launcher.go +++ b/cnb-builder-shim/internal/devsandbox/phase/launcher.go @@ -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 } diff --git a/cnb-builder-shim/internal/devsandbox/reload.go b/cnb-builder-shim/internal/devsandbox/reload.go index e4f801e823..3d2883dfa0 100644 --- a/cnb-builder-shim/internal/devsandbox/reload.go +++ b/cnb-builder-shim/internal/devsandbox/reload.go @@ -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 = "rebuild" ) // ReloadStatus is the status of a reload operation. @@ -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) } diff --git a/cnb-builder-shim/internal/devsandbox/status.go b/cnb-builder-shim/internal/devsandbox/status.go new file mode 100644 index 0000000000..7b90e1d769 --- /dev/null +++ b/cnb-builder-shim/internal/devsandbox/status.go @@ -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 + + 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: +// +// +// 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 { + result := make(map[string]string) + + // 按行分割输出 + lines := strings.Split(output, "\n") + for _, line := range lines { + // 移除空白符并检查是否为空行 + line = strings.TrimSpace(line) + if line == "" { + continue + } + + // 按空格分割,格式为 " ..." + parts := strings.Fields(line) + if len(parts) < 2 { + continue // 如果格式不符合预期,跳过该行 + } + // 提取进程名称和状态 + processName := parts[0] + processState := parts[1] + + result[processName] = processState + } + + return result +} diff --git a/cnb-builder-shim/internal/devsandbox/webserver/server.go b/cnb-builder-shim/internal/devsandbox/webserver/server.go index 96e7519659..acf7fb76ac 100644 --- a/cnb-builder-shim/internal/devsandbox/webserver/server.go +++ b/cnb-builder-shim/internal/devsandbox/webserver/server.go @@ -20,6 +20,7 @@ package webserver import ( "fmt" + "github.com/TencentBlueking/bkpaas/cnb-builder-shim/pkg/appdesc" "net/http" "os" "path" @@ -87,6 +88,8 @@ func New(lg *logr.Logger) (*WebServer, error) { r.POST("/deploys", DeployHandler(s, mgr)) r.GET("/deploys/:deployID/results", ResultHandler(mgr)) r.GET("/app_logs", AppLogHandler()) + r.GET("/processes/status", ProcessStatusHandler()) + r.GET("/processes/list", ProcessListHandler()) return s, nil } @@ -230,4 +233,30 @@ func AppLogHandler() gin.HandlerFunc { } } +// ProcessStatusHandler ... +func ProcessStatusHandler() gin.HandlerFunc { + return func(c *gin.Context) { + status, err := devsandbox.Status() + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"message": fmt.Sprintf("get status error: %s", err.Error())}) + return + } + c.JSON(http.StatusOK, gin.H{"status": status}) + } +} + +// ProcessListHandler ... +func ProcessListHandler() gin.HandlerFunc { + return func(c *gin.Context) { + appDescFilePath := path.Join(config.G.SourceCode.Workspace, "app_desc.yaml") + appDesc, err := appdesc.UnmarshalToAppDesc(appDescFilePath) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"message": fmt.Sprintf("list process error: %s", err.Error())}) + return + } + + c.JSON(http.StatusOK, gin.H{"processes": appDesc.Module.Processes}) + } +} + var _ devsandbox.DevWatchServer = (*WebServer)(nil) From 814e9836049636b157a2f8759ac8da5acd007ebd Mon Sep 17 00:00:00 2001 From: dijiachen Date: Wed, 6 Nov 2024 10:47:51 +0800 Subject: [PATCH 2/6] =?UTF-8?q?feat:=E8=8E=B7=E5=8F=96=E8=BF=9B=E7=A8=8B?= =?UTF-8?q?=E5=88=97=E8=A1=A8=EF=BC=8C=E8=8E=B7=E5=8F=96=E8=BF=9B=E7=A8=8B?= =?UTF-8?q?=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cnb-builder-shim/internal/devsandbox/reload.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cnb-builder-shim/internal/devsandbox/reload.go b/cnb-builder-shim/internal/devsandbox/reload.go index 3d2883dfa0..9e9d19d7a8 100644 --- a/cnb-builder-shim/internal/devsandbox/reload.go +++ b/cnb-builder-shim/internal/devsandbox/reload.go @@ -35,7 +35,7 @@ var ( // ReloadLogDir used to store reload log ReloadLogDir = path.Join(ReloadDir, "log") // reload sub command of dev-launcher - reloadSubCommand = "rebuild" + reloadSubCommand = "reload" ) // ReloadStatus is the status of a reload operation. From da1332016121fb8d0aa1a0b5816d2ca17c2f492c Mon Sep 17 00:00:00 2001 From: dijiachen Date: Wed, 6 Nov 2024 17:44:29 +0800 Subject: [PATCH 3/6] =?UTF-8?q?fix=EF=BC=9A=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cnb-builder-shim/cmd/dev-launcher/cmd/launch.go | 5 +++-- cnb-builder-shim/cmd/dev-launcher/cmd/status.go | 3 ++- cnb-builder-shim/cmd/dev-launcher/launch/supervisorctl.go | 4 ++-- cnb-builder-shim/internal/devsandbox/webserver/server.go | 2 +- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/cnb-builder-shim/cmd/dev-launcher/cmd/launch.go b/cnb-builder-shim/cmd/dev-launcher/cmd/launch.go index 53455ec6e1..71f9de33bc 100644 --- a/cnb-builder-shim/cmd/dev-launcher/cmd/launch.go +++ b/cnb-builder-shim/cmd/dev-launcher/cmd/launch.go @@ -22,12 +22,13 @@ 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" - "github.com/buildpacks/lifecycle/launch" - "github.com/spf13/cobra" ) var DefaultAppDir = utils.EnvOrDefault("CNB_APP_DIR", "/app") diff --git a/cnb-builder-shim/cmd/dev-launcher/cmd/status.go b/cnb-builder-shim/cmd/dev-launcher/cmd/status.go index 952d192831..3294d87dd5 100644 --- a/cnb-builder-shim/cmd/dev-launcher/cmd/status.go +++ b/cnb-builder-shim/cmd/dev-launcher/cmd/status.go @@ -19,8 +19,9 @@ package cmd import ( - "github.com/TencentBlueking/bkpaas/cnb-builder-shim/cmd/dev-launcher/launch" "github.com/spf13/cobra" + + "github.com/TencentBlueking/bkpaas/cnb-builder-shim/cmd/dev-launcher/launch" ) var statusCmd = &cobra.Command{ diff --git a/cnb-builder-shim/cmd/dev-launcher/launch/supervisorctl.go b/cnb-builder-shim/cmd/dev-launcher/launch/supervisorctl.go index cb93306e63..272b673bfd 100644 --- a/cnb-builder-shim/cmd/dev-launcher/launch/supervisorctl.go +++ b/cnb-builder-shim/cmd/dev-launcher/launch/supervisorctl.go @@ -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 @@ -73,7 +73,7 @@ fi var statusScript = fmt.Sprintf(`#!/bin/bash socket_file="%[1]s/supervisor.sock" -# 检查supervisor的socket文件是否存在 +# 检查 supervisor 的 socket 文件是否存在 if [ -S "$socket_file" ]; then supervisorctl -c %[2]s status fi diff --git a/cnb-builder-shim/internal/devsandbox/webserver/server.go b/cnb-builder-shim/internal/devsandbox/webserver/server.go index acf7fb76ac..36e0fcf9ca 100644 --- a/cnb-builder-shim/internal/devsandbox/webserver/server.go +++ b/cnb-builder-shim/internal/devsandbox/webserver/server.go @@ -20,7 +20,6 @@ package webserver import ( "fmt" - "github.com/TencentBlueking/bkpaas/cnb-builder-shim/pkg/appdesc" "net/http" "os" "path" @@ -37,6 +36,7 @@ import ( "github.com/TencentBlueking/bkpaas/cnb-builder-shim/internal/devsandbox" "github.com/TencentBlueking/bkpaas/cnb-builder-shim/internal/devsandbox/config" "github.com/TencentBlueking/bkpaas/cnb-builder-shim/internal/devsandbox/webserver/service" + "github.com/TencentBlueking/bkpaas/cnb-builder-shim/pkg/appdesc" "github.com/TencentBlueking/bkpaas/cnb-builder-shim/pkg/utils" ) From 584b65338e04a6cec65a908be778f25d6ca9aaad Mon Sep 17 00:00:00 2001 From: dijiachen Date: Wed, 6 Nov 2024 17:55:48 +0800 Subject: [PATCH 4/6] =?UTF-8?q?fix=EF=BC=9A=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cnb-builder-shim/cmd/dev-launcher/cmd/launch.go | 2 +- cnb-builder-shim/cmd/dev-launcher/cmd/root.go | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/cnb-builder-shim/cmd/dev-launcher/cmd/launch.go b/cnb-builder-shim/cmd/dev-launcher/cmd/launch.go index 71f9de33bc..4d8e602124 100644 --- a/cnb-builder-shim/cmd/dev-launcher/cmd/launch.go +++ b/cnb-builder-shim/cmd/dev-launcher/cmd/launch.go @@ -54,7 +54,7 @@ var reloadCmd = &cobra.Command{ } if err = devlaunch.Run(md.Processes, appDesc); err != nil { - logger.Error(err, "hot launch") + logger.Error(err, "failed to hot launch") return err } return nil diff --git a/cnb-builder-shim/cmd/dev-launcher/cmd/root.go b/cnb-builder-shim/cmd/dev-launcher/cmd/root.go index f3a13c5b7b..b121b5c231 100644 --- a/cnb-builder-shim/cmd/dev-launcher/cmd/root.go +++ b/cnb-builder-shim/cmd/dev-launcher/cmd/root.go @@ -28,8 +28,7 @@ import ( var rootCmd = &cobra.Command{ Use: "dev-launcher", Short: "dev-launcher cli", - Long: `Manage processes defined by app_desc, including -reload, getting status, stopping.`, + Long: `Manage processes defined by app_desc, including reload, getting status.`, Run: func(cmd *cobra.Command, args []string) { fmt.Println("run dev-launcher...") }, From 8829b31d371e5791e4928d95079029ade9073e98 Mon Sep 17 00:00:00 2001 From: dijiachen Date: Wed, 6 Nov 2024 22:04:36 +0800 Subject: [PATCH 5/6] =?UTF-8?q?feat:=E6=B2=99=E7=AE=B1=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E8=BF=9B=E7=A8=8B=E6=A0=B9=E6=8D=AEapp=5Fdesc.yaml=20=E6=94=B9?= =?UTF-8?q?=E5=8F=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cmd/dev-launcher/launch/supervisorctl.go | 30 +++++++++++++++++-- cnb-builder-shim/go.mod | 3 +- cnb-builder-shim/go.sum | 5 ++++ .../devsandbox/webserver/service/deploy.go | 20 ++++++++++--- 4 files changed, 50 insertions(+), 8 deletions(-) diff --git a/cnb-builder-shim/cmd/dev-launcher/launch/supervisorctl.go b/cnb-builder-shim/cmd/dev-launcher/launch/supervisorctl.go index 272b673bfd..d38e4f72c3 100644 --- a/cnb-builder-shim/cmd/dev-launcher/launch/supervisorctl.go +++ b/cnb-builder-shim/cmd/dev-launcher/launch/supervisorctl.go @@ -21,13 +21,12 @@ package launch import ( "bytes" "fmt" + "github.com/TencentBlueking/bkpaas/cnb-builder-shim/pkg/appdesc" "os" "os/exec" "path/filepath" "strings" "text/template" - - "github.com/TencentBlueking/bkpaas/cnb-builder-shim/pkg/appdesc" ) var supervisorDir = "/cnb/devsandbox/supervisor" @@ -79,6 +78,16 @@ if [ -S "$socket_file" ]; then fi `, supervisorDir, confFilePath) +var stopScript = fmt.Sprintf(`#!/bin/bash + +socket_file="%[1]s/supervisor.sock" +# 检查 supervisor 的 socket 文件是否存在 +if [ -S "$socket_file" ]; then + echo "stop all processes..." + supervisorctl -c %[2]s stop all +fi +`, supervisorDir, confFilePath) + // ProcessConf is a process config type ProcessConf struct { Process @@ -131,11 +140,14 @@ type SupervisorCtl struct { } // Reload start or update/restart the processes +// TODO: 现在 reload 还是会导致子进程堆积僵尸进程 func (ctl *SupervisorCtl) Reload(conf *SupervisorConf) error { if err := os.MkdirAll(filepath.Join(ctl.RootDir, "log"), 0o755); err != nil { return err } - + if err := ctl.stop(); err != nil { + return err + } if err := ctl.refreshConf(conf); err != nil { return err } @@ -182,6 +194,18 @@ func (ctl *SupervisorCtl) Status() error { return cmd.Run() } +// stop all processes by running 'supervisorctl stop all'. +func (ctl *SupervisorCtl) stop() error { + cmd := exec.Command("bash") + + cmd.Env = os.Environ() + cmd.Stdin = bytes.NewBufferString(stopScript) + 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 diff --git a/cnb-builder-shim/go.mod b/cnb-builder-shim/go.mod index ee5fe1bb2c..27b58b6013 100644 --- a/cnb-builder-shim/go.mod +++ b/cnb-builder-shim/go.mod @@ -74,10 +74,11 @@ require ( go.opentelemetry.io/otel/trace v1.31.0 // indirect golang.org/x/arch v0.7.0 // indirect golang.org/x/crypto v0.28.0 // indirect + golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c // indirect golang.org/x/net v0.30.0 // indirect golang.org/x/sync v0.8.0 // indirect golang.org/x/sys v0.26.0 // indirect golang.org/x/text v0.19.0 // indirect - golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect + golang.org/x/tools v0.26.0 // indirect google.golang.org/protobuf v1.35.1 // indirect ) diff --git a/cnb-builder-shim/go.sum b/cnb-builder-shim/go.sum index 783bb37904..52b2a5098d 100644 --- a/cnb-builder-shim/go.sum +++ b/cnb-builder-shim/go.sum @@ -232,8 +232,11 @@ golang.org/x/arch v0.7.0 h1:pskyeJh/3AmoQ8CPE95vxHLqp1G1GfGNXTmcl9NEKTc= golang.org/x/arch v0.7.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw= golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U= +golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c h1:7dEasQXItcW1xKJ2+gg5VOiBnqWrJc+rq0DPKyvvdbY= +golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c/go.mod h1:NQtJDoLvd6faHhE7m4T/1IY708gDefGGjR/iUW8yQQ8= golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA= golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0= golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4= golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU= golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= @@ -249,6 +252,8 @@ golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg= golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= +golang.org/x/tools v0.26.0 h1:v/60pFQmzmT9ExmjDv2gGIfi3OqfKoEP6I5+umXlbnQ= +golang.org/x/tools v0.26.0/go.mod h1:TPVVj70c7JJ3WCazhD8OdXcZg/og+b9+tH/KxylGwH0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA= google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= diff --git a/cnb-builder-shim/internal/devsandbox/webserver/service/deploy.go b/cnb-builder-shim/internal/devsandbox/webserver/service/deploy.go index b9d82c7574..6f96ec23b5 100644 --- a/cnb-builder-shim/internal/devsandbox/webserver/service/deploy.go +++ b/cnb-builder-shim/internal/devsandbox/webserver/service/deploy.go @@ -25,6 +25,7 @@ import ( "strings" "github.com/google/uuid" + "golang.org/x/exp/slices" "github.com/TencentBlueking/bkpaas/cnb-builder-shim/internal/devsandbox" "github.com/TencentBlueking/bkpaas/cnb-builder-shim/pkg/appdesc" @@ -135,8 +136,12 @@ func (m DeployManager) newDeployID() string { } func (m *DeployManager) analyzeAndSyncToAppDir(srcFilePath, appDir string) error { + var err error // 1. 通过对比新旧文件的变化, 确定哪些步骤需要执行 - m.stepOpts = parseDeployStepOpts(appDir, srcFilePath) + m.stepOpts, err = parseDeployStepOpts(appDir, srcFilePath) + if err != nil { + return err + } // 2. 将 tmpAppDir 中的文件拷贝到 appDir if err := m.syncFiles(srcFilePath, appDir); err != nil { @@ -180,8 +185,9 @@ func (m DeployManager) generateProcfile(appDir string) error { // // oldDir string - the directory where old build dependent files are located // newDir string - the directory where new build dependent files are located -func parseDeployStepOpts(oldDir, newDir string) *deployStepOpts { - buildDependentFiles := []string{"requirements.txt", "Aptfile", "runtime.txt", "Procfile"} +func parseDeployStepOpts(oldDir, newDir string) (*deployStepOpts, error) { + buildDependentFiles := []string{"requirements.txt", "Aptfile", "runtime.txt", "Procfile", "app_desc.yaml"} + metaDataRebuildFiles := []string{"Procfile", "app_desc.yaml"} rebuild := false for _, fileName := range buildDependentFiles { @@ -199,6 +205,12 @@ func parseDeployStepOpts(oldDir, newDir string) *deployStepOpts { eq, err := utils.SortedCompareFile(oldFilePath, newFilePath) if err != nil || !eq { rebuild = true + // metaDataRebuildFiles 中的文件被修改时, 需要删除 metadata.toml, 以便生成最新的 metadata.toml + if slices.Contains(metaDataRebuildFiles, fileName) { + if err := os.Remove(path.Join(devsandbox.DefaultLayersDir, "config", "metadata.toml")); err != nil { + return nil, err + } + } break } @@ -210,7 +222,7 @@ func parseDeployStepOpts(oldDir, newDir string) *deployStepOpts { rebuild = true } - return &deployStepOpts{Rebuild: rebuild, Relaunch: true} + return &deployStepOpts{Rebuild: rebuild, Relaunch: true}, nil } var _ DeployServiceHandler = (*DeployManager)(nil) From d9c4f726faedd459da1fc763b307c070e952757a Mon Sep 17 00:00:00 2001 From: dijiachen Date: Wed, 6 Nov 2024 22:29:30 +0800 Subject: [PATCH 6/6] =?UTF-8?q?feat:=E6=B2=99=E7=AE=B1=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E8=BF=9B=E7=A8=8B=E6=A0=B9=E6=8D=AEapp=5Fdesc.yaml=20=E6=94=B9?= =?UTF-8?q?=E5=8F=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cnb-builder-shim/cmd/dev-launcher/launch/supervisorctl.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cnb-builder-shim/cmd/dev-launcher/launch/supervisorctl.go b/cnb-builder-shim/cmd/dev-launcher/launch/supervisorctl.go index d38e4f72c3..af6deeb373 100644 --- a/cnb-builder-shim/cmd/dev-launcher/launch/supervisorctl.go +++ b/cnb-builder-shim/cmd/dev-launcher/launch/supervisorctl.go @@ -21,12 +21,13 @@ package launch import ( "bytes" "fmt" - "github.com/TencentBlueking/bkpaas/cnb-builder-shim/pkg/appdesc" "os" "os/exec" "path/filepath" "strings" "text/template" + + "github.com/TencentBlueking/bkpaas/cnb-builder-shim/pkg/appdesc" ) var supervisorDir = "/cnb/devsandbox/supervisor"