databricks-cli/libs/daemon/daemon.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

108 lines
2.3 KiB
Go
Raw Normal View History

2025-02-13 18:00:13 +00:00
package daemon
import (
2025-02-17 18:23:02 +00:00
"context"
2025-02-13 18:00:13 +00:00
"fmt"
"io"
"os"
"os/exec"
"strconv"
)
2025-02-17 18:23:02 +00:00
const DatabricksCliParentPid = "DATABRICKS_CLI_PARENT_PID"
2025-02-13 18:00:13 +00:00
type Daemon struct {
2025-02-17 18:23:02 +00:00
// TODO: remove this.
ctx context.Context
2025-02-13 18:00:13 +00:00
// If provided, the child process will create a pid file at this path.
2025-02-17 18:23:02 +00:00
// TODO: Can we remove this?
2025-02-13 18:00:13 +00:00
PidFilePath string
// Environment variables to set in the child process.
Env []string
2025-02-17 18:23:02 +00:00
// Arguments to pass to the child process. The main executable is always the CLI
// binary itself.
2025-02-13 18:00:13 +00:00
Args []string
2025-02-17 18:23:02 +00:00
// Log file to write the child process's output to.
LogFile string
2025-02-13 18:00:13 +00:00
cmd *exec.Cmd
stdin io.WriteCloser
}
func (d *Daemon) Start() error {
cli, err := os.Executable()
if err != nil {
return err
}
d.cmd = exec.Command(cli, d.Args...)
2025-02-17 18:23:02 +00:00
// Set environment variable so that the child process know's it's parent's PID.
d.Env = append(d.Env, fmt.Sprintf("%s=%d", DatabricksCliParentPid, os.Getpid()))
2025-02-13 18:00:13 +00:00
d.cmd.Env = d.Env
2025-02-17 18:23:02 +00:00
2025-02-13 18:00:13 +00:00
d.cmd.SysProcAttr = sysProcAttr()
2025-02-17 18:23:02 +00:00
// By default redirect stdout and stderr to /dev/null.
// TODO: Test that by default stdout and stderr do not leak to the parent process.
d.cmd.Stdout = nil
d.cmd.Stderr = nil
// If a log file is provided, redirect stdout and stderr to the log file.
if d.LogFile != "" {
f, err := os.OpenFile(d.LogFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644)
if err != nil {
return fmt.Errorf("failed to open log file: %w", err)
}
defer f.Close()
d.cmd.Stdout = f
d.cmd.Stderr = f
}
2025-02-13 18:00:13 +00:00
d.stdin, err = d.cmd.StdinPipe()
if err != nil {
return fmt.Errorf("failed to get stdin pipe: %w", err)
}
err = d.cmd.Start()
if err != nil {
return err
}
if d.PidFilePath != "" {
err = os.WriteFile(d.PidFilePath, []byte(strconv.Itoa(d.cmd.Process.Pid)), 0o644)
if err != nil {
return fmt.Errorf("failed to write pid file: %w", err)
}
}
return nil
}
2025-02-17 18:23:02 +00:00
func (d *Daemon) WriteInput(b []byte) error {
_, err := d.stdin.Write(b)
return err
}
2025-02-13 18:00:13 +00:00
2025-02-17 18:23:02 +00:00
func (d *Daemon) Release() error {
2025-02-13 18:00:13 +00:00
if d.stdin != nil {
err := d.stdin.Close()
if err != nil {
return fmt.Errorf("failed to close stdin: %w", err)
}
}
if d.cmd == nil {
return nil
}
2025-02-17 18:23:02 +00:00
// This does not seem to be strictly necessary, but the docs recommend
// adding it if Wait is not called. Thus we add it here to be safe.
2025-02-13 18:00:13 +00:00
return d.cmd.Process.Release()
}