diff --git a/lib/tbot/config/systemd/systemd.tmpl b/lib/tbot/config/systemd/systemd.tmpl index 95ea44848d254..200e5b8fcdeff 100644 --- a/lib/tbot/config/systemd/systemd.tmpl +++ b/lib/tbot/config/systemd/systemd.tmpl @@ -10,9 +10,9 @@ Group={{ .Group }} Restart=always RestartSec=5 Environment="TELEPORT_ANONYMOUS_TELEMETRY={{ if .AnonymousTelemetry }}1{{ else }}0{{ end }}" -ExecStart={{ .TBotPath }} start -c {{ .ConfigPath }}{{ with .DiagSocketForUpdater }} --diag-socket-for-updater={{ . }}{{ end }} --pid-file=/run/{{ .UnitName }}.pid +ExecStart={{ .TBotPath }} start -c {{ .ConfigPath }}{{ with .DiagSocketForUpdater }} --diag-socket-for-updater={{ . }}{{ end }} --pid-file={{ if .PIDFile }}{{ .PIDFile }}{{ else }}/run/{{ .UnitName }}.pid{{ end }} ExecReload=/bin/kill -HUP $MAINPID -PIDFile=/run/{{ .UnitName }}.pid +PIDFile={{ if .PIDFile }}{{ .PIDFile }}{{ else }}/run/{{ .UnitName }}.pid{{ end }} LimitNOFILE=524288 [Install] diff --git a/lib/tbot/config/systemd/template.go b/lib/tbot/config/systemd/template.go index 1c4af6fb57c25..da454901337e3 100644 --- a/lib/tbot/config/systemd/template.go +++ b/lib/tbot/config/systemd/template.go @@ -47,4 +47,6 @@ type TemplateParams struct { TBotPath string // DiagSocketForUpdater is the path to the diag socket for the updater. DiagSocketForUpdater string + // PIDFile is the path to the PID file. + PIDFile string } diff --git a/tool/tbot/systemd.go b/tool/tbot/systemd.go index babf75c4be1bc..1d28f6d51d582 100644 --- a/tool/tbot/systemd.go +++ b/tool/tbot/systemd.go @@ -55,6 +55,7 @@ func setupInstallSystemdCmd(rootCmd *kingpin.Application) ( write := installSystemdCmd.Flag("write", "Write the systemd unit file. If not specified, this command runs in a dry-run mode that outputs the generated content to stdout.").Bool() systemdDirectory := installSystemdCmd.Flag("systemd-directory", "Path to the directory that the systemd unit file should be written. Defaults to '/etc/systemd/system'.").Default("/etc/systemd/system").String() anonymousTelemetry := installSystemdCmd.Flag("anonymous-telemetry", "Enable anonymous telemetry.").Bool() + pidFile := installSystemdCmd.Flag("pid-file", "Overrides the PID file path that should be set in the systemd unit files.").String() f := onInstallSystemdCmdFunc(func( ctx context.Context, @@ -76,6 +77,7 @@ func setupInstallSystemdCmd(rootCmd *kingpin.Application) ( configPath, getExecutablePath, stdout, + *pidFile, ) }) @@ -95,6 +97,7 @@ func onInstallSystemdCmd( configPath string, getExecutablePath func() (string, error), stdout io.Writer, + pidFile string, ) error { switch { case configPath == "": @@ -123,6 +126,7 @@ func onInstallSystemdCmd( AnonymousTelemetry: anonymousTelemetry, ConfigPath: configPath, TBotPath: tbotPath, + PIDFile: pidFile, }) if err != nil { return trace.Wrap(err) diff --git a/tool/tbot/systemd_test.go b/tool/tbot/systemd_test.go index 248a696636a8f..0ad43b098f645 100644 --- a/tool/tbot/systemd_test.go +++ b/tool/tbot/systemd_test.go @@ -74,6 +74,22 @@ func TestInstallSystemdCmd(t *testing.T) { }, wantUnitName: "my-farm-bot", }, + { + name: "success - override only pid file", + params: []string{ + "--write", + "--pid-file", "/tmp/tbot-fake.pid", + }, + }, + { + name: "success - override pid and unit name", + params: []string{ + "--write", + "--pid-file", "/tmp/tbot-fake.pid", + "--name", "my-special-bot", + }, + wantUnitName: "my-special-bot", + }, { name: "fails prexisting", params: []string{ diff --git a/tool/tbot/testdata/TestInstallSystemdCmd/success_-_override_only_pid_file.golden b/tool/tbot/testdata/TestInstallSystemdCmd/success_-_override_only_pid_file.golden new file mode 100644 index 0000000000000..12edc48c3d273 --- /dev/null +++ b/tool/tbot/testdata/TestInstallSystemdCmd/success_-_override_only_pid_file.golden @@ -0,0 +1,19 @@ +# Generated by `tbot` +[Unit] +Description=tbot - Teleport Machine & Workload Identity Service +After=network.target + +[Service] +Type=simple +User=teleport +Group=teleport +Restart=always +RestartSec=5 +Environment="TELEPORT_ANONYMOUS_TELEMETRY=0" +ExecStart=/usr/local/bin/tbot start -c /etc/tbot.yaml --pid-file=/tmp/tbot-fake.pid +ExecReload=/bin/kill -HUP $MAINPID +PIDFile=/tmp/tbot-fake.pid +LimitNOFILE=524288 + +[Install] +WantedBy=multi-user.target diff --git a/tool/tbot/testdata/TestInstallSystemdCmd/success_-_override_pid_and_unit_name.golden b/tool/tbot/testdata/TestInstallSystemdCmd/success_-_override_pid_and_unit_name.golden new file mode 100644 index 0000000000000..4af18742eb96d --- /dev/null +++ b/tool/tbot/testdata/TestInstallSystemdCmd/success_-_override_pid_and_unit_name.golden @@ -0,0 +1,19 @@ +# Generated by `tbot` +[Unit] +Description=my-special-bot - Teleport Machine & Workload Identity Service +After=network.target + +[Service] +Type=simple +User=teleport +Group=teleport +Restart=always +RestartSec=5 +Environment="TELEPORT_ANONYMOUS_TELEMETRY=0" +ExecStart=/usr/local/bin/tbot start -c /etc/tbot.yaml --pid-file=/tmp/tbot-fake.pid +ExecReload=/bin/kill -HUP $MAINPID +PIDFile=/tmp/tbot-fake.pid +LimitNOFILE=524288 + +[Install] +WantedBy=multi-user.target