mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2026-09-03 07:24:52 +08:00
refactor(nginx): share the systemd defaults through settings getters
The /bin/systemctl and nginx.service fallbacks were copied into every host command builder. Expose them as GetHostSystemctlPath and GetHostSystemdUnitName next to the launchd getters so a default change lands in one place. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
@@ -359,15 +359,7 @@ func hostReloadCommand(n *settings.Nginx) (string, []string) {
|
||||
if n.GetHostServiceManager() == settings.HostServiceManagerLaunchd {
|
||||
return n.GetHostSbinPath(), []string{"-s", "reload"}
|
||||
}
|
||||
systemctl := n.HostSystemctlPath
|
||||
if systemctl == "" {
|
||||
systemctl = "/bin/systemctl"
|
||||
}
|
||||
unit := n.HostSystemdUnitName
|
||||
if unit == "" {
|
||||
unit = "nginx.service"
|
||||
}
|
||||
return systemctl, []string{"reload", unit}
|
||||
return n.GetHostSystemctlPath(), []string{"reload", n.GetHostSystemdUnitName()}
|
||||
}
|
||||
|
||||
func hostRestartCommand(runner Runner, n *settings.Nginx) (string, []string, error) {
|
||||
@@ -378,15 +370,7 @@ func hostRestartCommand(runner Runner, n *settings.Nginx) (string, []string, err
|
||||
}
|
||||
return n.GetHostLaunchctlPath(), []string{"kickstart", "-k", target}, nil
|
||||
}
|
||||
systemctl := n.HostSystemctlPath
|
||||
if systemctl == "" {
|
||||
systemctl = "/bin/systemctl"
|
||||
}
|
||||
unit := n.HostSystemdUnitName
|
||||
if unit == "" {
|
||||
unit = "nginx.service"
|
||||
}
|
||||
return systemctl, []string{"restart", unit}, nil
|
||||
return n.GetHostSystemctlPath(), []string{"restart", n.GetHostSystemdUnitName()}, nil
|
||||
}
|
||||
|
||||
func launchdTarget(runner Runner, service string) (string, error) {
|
||||
@@ -428,15 +412,7 @@ func isRunningViaHostService() bool {
|
||||
}
|
||||
|
||||
func hostSystemdStatusCommand(n *settings.Nginx) (string, []string) {
|
||||
systemctl := n.HostSystemctlPath
|
||||
if systemctl == "" {
|
||||
systemctl = "/bin/systemctl"
|
||||
}
|
||||
unit := n.HostSystemdUnitName
|
||||
if unit == "" {
|
||||
unit = "nginx.service"
|
||||
}
|
||||
return systemctl, []string{"is-active", unit}
|
||||
return n.GetHostSystemctlPath(), []string{"is-active", n.GetHostSystemdUnitName()}
|
||||
}
|
||||
|
||||
func isRemotePIDRunning(runner Runner, pidPath string) bool {
|
||||
|
||||
@@ -74,10 +74,6 @@ func buildSSHOptions() hostssh.ClientOptions {
|
||||
_ = n.HostPasswordRef // suppress unused-field lint until decryption lands
|
||||
|
||||
sudo := n.GetHostSudoPrefix()
|
||||
systemctl := n.HostSystemctlPath
|
||||
if systemctl == "" {
|
||||
systemctl = "/bin/systemctl"
|
||||
}
|
||||
|
||||
return hostssh.ClientOptions{
|
||||
Address: n.HostAddress,
|
||||
@@ -88,7 +84,7 @@ func buildSSHOptions() hostssh.ClientOptions {
|
||||
KnownHosts: kh,
|
||||
Config: hostssh.Config{
|
||||
SudoPrefix: sudo,
|
||||
SystemctlPath: systemctl,
|
||||
SystemctlPath: n.GetHostSystemctlPath(),
|
||||
// The resolved path must match what -t/-T is invoked with, or the
|
||||
// sudo whitelist check in needsSudo never fires.
|
||||
NginxSbinPath: n.GetHostSbinPath(),
|
||||
|
||||
@@ -23,6 +23,11 @@ const (
|
||||
// same paths when SbinPath is left empty.
|
||||
DefaultHostSbinPathSystemd = "/usr/sbin/nginx"
|
||||
DefaultHostSbinPathLaunchd = "/opt/homebrew/opt/nginx/bin/nginx"
|
||||
|
||||
// Default systemd control paths on an SSH host, used whenever the
|
||||
// corresponding setting is left empty.
|
||||
DefaultHostSystemctlPath = "/bin/systemctl"
|
||||
DefaultHostSystemdUnitName = "nginx.service"
|
||||
)
|
||||
|
||||
const DefaultMaintenanceDir = "/etc/nginx/maintenance"
|
||||
@@ -141,6 +146,23 @@ func (n *Nginx) GetHostSudoPrefix() string {
|
||||
return n.HostSudoPrefix
|
||||
}
|
||||
|
||||
// GetHostSystemctlPath returns the systemctl binary to run on the SSH host.
|
||||
func (n *Nginx) GetHostSystemctlPath() string {
|
||||
if n.HostSystemctlPath == "" {
|
||||
return DefaultHostSystemctlPath
|
||||
}
|
||||
return n.HostSystemctlPath
|
||||
}
|
||||
|
||||
// GetHostSystemdUnitName returns the systemd unit that manages nginx on the
|
||||
// SSH host.
|
||||
func (n *Nginx) GetHostSystemdUnitName() string {
|
||||
if n.HostSystemdUnitName == "" {
|
||||
return DefaultHostSystemdUnitName
|
||||
}
|
||||
return n.HostSystemdUnitName
|
||||
}
|
||||
|
||||
func (n *Nginx) GetHostLaunchdService() string {
|
||||
if n.HostLaunchdService == "" {
|
||||
return "homebrew.mxcl.nginx"
|
||||
|
||||
@@ -62,3 +62,27 @@ func TestNginx_GetHostSbinPath(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNginx_GetHostSystemdDefaults(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
nginx Nginx
|
||||
wantSystemctl string
|
||||
wantUnit string
|
||||
}{
|
||||
{"defaults", Nginx{}, DefaultHostSystemctlPath, DefaultHostSystemdUnitName},
|
||||
{"configured systemctl", Nginx{HostSystemctlPath: "/usr/bin/systemctl"}, "/usr/bin/systemctl", DefaultHostSystemdUnitName},
|
||||
{"configured unit", Nginx{HostSystemdUnitName: "openresty.service"}, DefaultHostSystemctlPath, "openresty.service"},
|
||||
{"both configured", Nginx{HostSystemctlPath: "/usr/bin/systemctl", HostSystemdUnitName: "openresty.service"}, "/usr/bin/systemctl", "openresty.service"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := tt.nginx.GetHostSystemctlPath(); got != tt.wantSystemctl {
|
||||
t.Errorf("GetHostSystemctlPath() = %q, want %q", got, tt.wantSystemctl)
|
||||
}
|
||||
if got := tt.nginx.GetHostSystemdUnitName(); got != tt.wantUnit {
|
||||
t.Errorf("GetHostSystemdUnitName() = %q, want %q", got, tt.wantUnit)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user