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

hook脚本支持环境变量 #3

Merged
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
4 changes: 2 additions & 2 deletions cli/cmd/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func pullAppFiles(bscp *client.Client, tempDir string, biz uint32, app string, o
}
// 2. execute pre hook
if preHook != nil {
if err := util.ExecuteHook(appDir, preHook, table.PreHook); err != nil {
if err := util.ExecuteHook(preHook, table.PreHook, tempDir, biz, app); err != nil {
return err
}
}
Expand All @@ -97,7 +97,7 @@ func pullAppFiles(bscp *client.Client, tempDir string, biz uint32, app string, o
}
// 4. execute post hook
if postHook != nil {
if err := util.ExecuteHook(appDir, postHook, table.PostHook); err != nil {
if err := util.ExecuteHook(postHook, table.PostHook, tempDir, biz, app); err != nil {
return err
}
}
Expand Down
7 changes: 5 additions & 2 deletions cli/cmd/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func Watch(cmd *cobra.Command, args []string) {
tempDir = conf.TempDir
}
handler := &WatchHandler{
Biz: conf.Biz,
App: subscriber.Name,
Labels: subscriber.Labels,
UID: subscriber.UID,
Expand All @@ -89,6 +90,8 @@ func Watch(cmd *cobra.Command, args []string) {

// WatchHandler watch handler
type WatchHandler struct {
// Biz BSCP biz id
Biz uint32
// App BSCP app name
App string
// Labels instance labels
Expand Down Expand Up @@ -118,7 +121,7 @@ func (w *WatchHandler) watchCallback(releaseID uint32, files []*types.ConfigItem

// 1. execute pre hook
if preHook != nil {
if err := util.ExecuteHook(w.TempDir, preHook, table.PreHook); err != nil {
if err := util.ExecuteHook(preHook, table.PreHook, w.TempDir, w.Biz, w.App); err != nil {
logs.Errorf(err.Error())
return err
}
Expand All @@ -136,7 +139,7 @@ func (w *WatchHandler) watchCallback(releaseID uint32, files []*types.ConfigItem
}
// 5. execute post hook
if postHook != nil {
if err := util.ExecuteHook(w.TempDir, postHook, table.PostHook); err != nil {
if err := util.ExecuteHook(postHook, table.PostHook, w.TempDir, w.Biz, w.App); err != nil {
logs.Errorf(err.Error())
return err
}
Expand Down
61 changes: 29 additions & 32 deletions cli/util/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,48 +29,45 @@ const (
executeShellCmd = "bash"
// executePythonCmd python script executor
executePythonCmd = "python3"

// !important: promise of compatibility
// EnvAppTempDir bscp app temp dir env
EnvAppTempDir = "bk_bscp_app_temp_dir"
// EnvTempDir bscp temp dir env
EnvTempDir = "bk_bscp_temp_dir"
// EnvApp bscp biz id env
EnvBiz = "bk_bscp_biz"
// EnvApp bscp app name env
EnvApp = "bk_bscp_app"
)

// ExecuteHook executes the hook.
func ExecuteHook(workspace string, hook *pbhook.HookSpec, hookType table.HookType) error {
func ExecuteHook(hook *pbhook.HookSpec, hookType table.HookType,
tempDir string, biz uint32, app string) error {
appTempDir := path.Join(tempDir, fmt.Sprintf("%d/%s", biz, app))
hookPath, err := saveContentToFile(appTempDir, hook, hookType)
if err != nil {
logs.Errorf("save hook content to file error: %s", err.Error())
return err
}
var command string
switch hook.Type {
case "shell":
return ExecuteShellHook(workspace, hook, hookType)
command = executeShellCmd
case "python":
return ExecutePythonHook(workspace, hook, hookType)
command = executePythonCmd
default:
return fmt.Errorf("invalid hook type: %s", hook.Type)
}
}

// ExecuteShellHook executes the shell hook.
func ExecuteShellHook(workspace string, hook *pbhook.HookSpec, hookType table.HookType) error {
hookPath, err := saveContentToFile(workspace, hook, hookType)
if err != nil {
logs.Errorf("save hook content to file error: %s", err.Error())
return err
}
args := []string{hookPath}
cmd := exec.Command(executeShellCmd, args...)
cmd.Dir = workspace
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("exec %s error: %s, output: %s", hookType.String(), err.Error(), string(out))
}
logs.Infof("exec %s success, output: \n%s", hookType.String(), string(out))
return nil
}

// ExecutePythonHook executes the python hook.
func ExecutePythonHook(workspace string, hook *pbhook.HookSpec, hookType table.HookType) error {
hookPath, err := saveContentToFile(workspace, hook, hookType)
if err != nil {
logs.Errorf("save hook content to file error: %s", err.Error())
return err
}
args := []string{hookPath}
cmd := exec.Command(executePythonCmd, args...)
cmd.Dir = workspace
cmd := exec.Command(command, args...)
cmd.Dir = appTempDir
cmd.Env = append(os.Environ(),
fmt.Sprintf("%s=%s", EnvAppTempDir, appTempDir),
fmt.Sprintf("%s=%s", EnvTempDir, tempDir),
fmt.Sprintf("%s=%d", EnvBiz, biz),
fmt.Sprintf("%s=%s", EnvApp, app),
)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("exec %s error: %s, output: %s", hookType.String(), err.Error(), string(out))
Expand Down
30 changes: 20 additions & 10 deletions cli/util/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package util_test
import (
"os"
"path/filepath"
"strconv"
"testing"

"bscp.io/pkg/dal/table"
Expand All @@ -24,11 +25,15 @@ import (
)

func TestExecuteShellHook(t *testing.T) {
workspace, err := os.MkdirTemp("", "hook-test")
tempDir, err := os.MkdirTemp("", "hook-test")
if err != nil {
t.Fatalf("create workspace error: %s", err.Error())
}
defer os.RemoveAll(workspace)
defer os.RemoveAll(tempDir)

biz := 2
app := "bscp-test"
appTempDir := filepath.Join(tempDir, strconv.Itoa(biz), app)

hookContent := "#!/bin/sh\nmkdir -p test"
hookSpec := &pbhook.HookSpec{
Expand All @@ -37,27 +42,32 @@ func TestExecuteShellHook(t *testing.T) {
Content: hookContent,
}

err = util.ExecuteHook(workspace, hookSpec, table.PreHook)
err = util.ExecuteHook(hookSpec, table.PreHook,tempDir, 2, "bscp-test")
if err != nil {
t.Fatalf("execute hook error: %s", err.Error())
}

hookPath := filepath.Join(workspace, "hooks", table.PreHook.String()+".sh")
hookPath := filepath.Join(appTempDir, "hooks", table.PreHook.String()+".sh")
if _, err := os.Stat(hookPath); os.IsNotExist(err) {
t.Fatalf("hook file not exist: %s", hookPath)
}

if _, err := os.Stat(filepath.Join(workspace, "test")); os.IsNotExist(err) {
if _, err := os.Stat(filepath.Join(appTempDir, "test")); os.IsNotExist(err) {
t.Fatal("hook exec failed")
}
}

func TestExecutePythonHook(t *testing.T) {
workspace, err := os.MkdirTemp("", "hook-test")
tempDir, err := os.MkdirTemp("", "hook-test")
if err != nil {
t.Fatalf("create workspace error: %s", err.Error())
}
defer os.RemoveAll(workspace)
defer os.RemoveAll(tempDir)

biz := 2
app := "bscp-test"
appTempDir := filepath.Join(tempDir, strconv.Itoa(biz), app)


hookContent := "import os\nos.makedirs('test')"
hookSpec := &pbhook.HookSpec{
Expand All @@ -66,17 +76,17 @@ func TestExecutePythonHook(t *testing.T) {
Content: hookContent,
}

err = util.ExecuteHook(workspace, hookSpec, table.PostHook)
err = util.ExecuteHook(hookSpec, table.PostHook, tempDir, 2, "bscp-test")
if err != nil {
t.Fatalf("execute hook error: %s", err.Error())
}

hookPath := filepath.Join(workspace, "hooks", table.PostHook.String()+".py")
hookPath := filepath.Join(appTempDir, "hooks", table.PostHook.String()+".py")
if _, err := os.Stat(hookPath); os.IsNotExist(err) {
t.Fatalf("hook file not exist: %s", hookPath)
}

if _, err := os.Stat(filepath.Join(workspace, "test")); os.IsNotExist(err) {
if _, err := os.Stat(filepath.Join(appTempDir, "test")); os.IsNotExist(err) {
t.Fatal("hook exec failed")
}
}
Loading