MCPcopy Index your code
hub / github.com/devspace-sh/devspace / ExecuteRemotely

Method ExecuteRemotely

pkg/devspace/hook/remote_command.go:32–90  ·  view source on GitHub ↗
(ctx devspacecontext.Context, hook *latest.HookConfig, podContainer *selector.SelectedPodContainer)

Source from the content-addressed store, hash-verified

30}
31
32func (r *remoteCommandHook) ExecuteRemotely(ctx devspacecontext.Context, hook *latest.HookConfig, podContainer *selector.SelectedPodContainer) error {
33 hookCommand, hookArgs, err := ResolveCommand(ctx.Context(), hook.Command, hook.Args, ctx.WorkingDir(), ctx.Config(), ctx.Dependencies())
34 if err != nil {
35 return err
36 }
37
38 cmd := []string{hookCommand}
39 if hook.Args == nil {
40 cmd = []string{"sh", "-c", hookCommand}
41 } else {
42 cmd = append(cmd, hookArgs...)
43 }
44
45 once := hook.Container.Once != nil && *hook.Container.Once
46 if once {
47 // check whether hook has previously executed
48 hookExecuted, err := hasHookExecuted(ctx.Context(), hookCommand, hookArgs, podContainer, ctx.KubeClient())
49 if err != nil {
50 return errors.Errorf("error checking whether hook has executed '%s/%s/%s': %v", podContainer.Pod.Namespace, podContainer.Pod.Name, podContainer.Container.Name, err)
51 }
52
53 if hookExecuted {
54 ctx.Log().Infof("Skip hook '%s' because it is configured to run once", ansi.Color(hookName(hook), "white+b"))
55 return nil
56 }
57 }
58
59 // if args are nil we execute the command in a shell
60 stdout := &bytes.Buffer{}
61 stderr := &bytes.Buffer{}
62 defer func() {
63 if hook.Name != "" {
64 ctx.Config().SetRuntimeVariable("hooks."+hook.Name+".stdout", strings.TrimSpace(stdout.String()))
65 ctx.Config().SetRuntimeVariable("hooks."+hook.Name+".stderr", strings.TrimSpace(stderr.String()))
66 }
67 }()
68
69 ctx.Log().Infof("Execute hook '%s' in container '%s/%s/%s'", ansi.Color(hookName(hook), "white+b"), podContainer.Pod.Namespace, podContainer.Pod.Name, podContainer.Container.Name)
70 err = ctx.KubeClient().ExecStream(ctx.Context(), &kubectl.ExecStreamOptions{
71 Pod: podContainer.Pod,
72 Container: podContainer.Container.Name,
73 Command: cmd,
74 Stdout: io.MultiWriter(r.Stdout, stdout),
75 Stderr: io.MultiWriter(r.Stderr, stderr),
76 })
77 if err != nil {
78 return errors.Errorf("error in container '%s/%s/%s': %v", podContainer.Pod.Namespace, podContainer.Pod.Name, podContainer.Container.Name, err)
79 }
80
81 if once {
82 // record hook execution
83 err := recordHookExecuted(ctx.Context(), hookCommand, hookArgs, podContainer, ctx.KubeClient())
84 if err != nil {
85 return errors.Errorf("error recording hook execution %s in container '%s/%s/%s': %v", ansi.Color(hookName(hook), "white+b"), podContainer.Pod.Namespace, podContainer.Pod.Name, podContainer.Container.Name, err)
86 }
87 }
88
89 return nil

Callers

nothing calls this directly

Calls 15

StringMethod · 0.95
ResolveCommandFunction · 0.85
hasHookExecutedFunction · 0.85
hookNameFunction · 0.85
recordHookExecutedFunction · 0.85
ContextMethod · 0.65
WorkingDirMethod · 0.65
ConfigMethod · 0.65
DependenciesMethod · 0.65
KubeClientMethod · 0.65
LogMethod · 0.65
SetRuntimeVariableMethod · 0.65

Tested by

no test coverage detected