MCPcopy
hub / github.com/kubesphere/kubekey / executor

Method executor

pkg/web/handler/executor.go:31–83  ·  view source on GitHub ↗
(playbook *kkcorev1.Playbook, client ctrlclient.Client, promise string)

Source from the content-addressed store, hash-verified

29}
30
31func (m *manager) executor(playbook *kkcorev1.Playbook, client ctrlclient.Client, promise string) error {
32 f := func() error {
33 // Build the log file path for the playbook execution
34 filename := filepath.Join(
35 _const.GetWorkdirFromConfig(playbook.Spec.Config),
36 _const.RuntimeDir,
37 kkcorev1.SchemeGroupVersion.Group,
38 kkcorev1.SchemeGroupVersion.Version,
39 "playbooks",
40 playbook.Namespace,
41 playbook.Name,
42 playbook.Name+".log",
43 )
44 // Ensure the directory for the log file exists
45 if _, err := os.Stat(filepath.Dir(filename)); err != nil {
46 if !os.IsNotExist(err) {
47 return errors.Wrapf(err, "failed to stat playbook dir %q", filepath.Dir(filename))
48 }
49 // If directory does not exist, create it
50 if err := os.MkdirAll(filepath.Dir(filename), os.ModePerm); err != nil {
51 return errors.Wrapf(err, "failed to create playbook dir %q", filepath.Dir(filename))
52 }
53 }
54 // Open the log file for writing
55 file, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, _const.PermFilePublic)
56 if err != nil {
57 return errors.Wrapf(err, "failed to open log file", "file", filename)
58 }
59 defer file.Close()
60
61 // Create a cancellable context for playbook execution
62 ctx, cancel := context.WithCancel(context.Background())
63 // Register the playbook and its cancel function in the playbookManager
64 m.addPlaybook(playbook, cancel)
65 // Execute the playbook and write output to the log file
66 if err := executor.NewPlaybookExecutor(ctx, client, playbook, file).Exec(ctx); err != nil {
67 // recode to log file
68 fmt.Fprintf(file, "%s [Playbook %s] ERROR: %v\n", time.Now().Format(time.TimeOnly+" MST"), ctrlclient.ObjectKeyFromObject(playbook), err)
69 }
70 // Remove the playbook from the playbookManager after execution
71 m.deletePlaybook(playbook)
72 return nil
73 }
74 if promise == "true" {
75 go func() {
76 if err := f(); err != nil {
77 klog.ErrorS(err, "failed to execute playbook", "playbook", ctrlclient.ObjectKeyFromObject(playbook))
78 }
79 }()
80 return nil
81 }
82 return f()
83}
84
85// addPlaybook adds a playbook and its cancel function to the manager map.
86func (m *manager) addPlaybook(playbook *kkcorev1.Playbook, cancel context.CancelFunc) {

Callers 3

PatchMethod · 0.80
PostMethod · 0.80
PostConfigMethod · 0.80

Calls 7

addPlaybookMethod · 0.95
deletePlaybookMethod · 0.95
NewPlaybookExecutorFunction · 0.92
StatMethod · 0.65
CloseMethod · 0.65
ExecMethod · 0.65
fFunction · 0.50

Tested by

no test coverage detected