Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create systemd dir when use systemd user mode #2373

Merged
merged 4 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion embed/templates/systemd/system.service.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ LimitCORE={{.LimitCORE}}
LimitNOFILE=1000000
LimitSTACK=10485760

{{- if .GrantCapNetRaw}}
{{- if and .GrantCapNetRaw (eq .SystemdMode "system")}}
AmbientCapabilities=CAP_NET_RAW
{{- end}}
{{- if eq .SystemdMode "system"}}
Expand Down
8 changes: 5 additions & 3 deletions pkg/cluster/manager/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ func buildScaleOutTask(
dirs = append(dirs, spec.Abs(globalOptions.User, dirname))
}
}

if systemdMode == spec.UserMode {
dirs = append(dirs, spec.Abs(globalOptions.User, ".config/systemd/user"))
}
t := task.NewBuilder(m.logger).
RootSSH(
instance.GetManageHost(),
Expand Down Expand Up @@ -434,7 +436,7 @@ type hostInfo struct {
func buildMonitoredDeployTask(
m *Manager,
uniqueHosts map[string]hostInfo, // host -> ssh-port, os, arch
noAgentHosts set.StringSet, // hosts that do not deploy monitor agents
noAgentHosts set.StringSet, // hosts that do not deploy monitor agents
globalOptions *spec.GlobalOptions,
monitoredOptions *spec.MonitoredOptions,
gOpt operator.Options,
Expand Down Expand Up @@ -508,7 +510,7 @@ func buildMonitoredCertificateTasks(
m *Manager,
name string,
uniqueHosts map[string]hostInfo, // host -> ssh-port, os, arch
noAgentHosts set.StringSet, // hosts that do not deploy monitor agents
noAgentHosts set.StringSet, // hosts that do not deploy monitor agents
globalOptions *spec.GlobalOptions,
monitoredOptions *spec.MonitoredOptions,
gOpt operator.Options,
Expand Down
4 changes: 3 additions & 1 deletion pkg/cluster/manager/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ func (m *Manager) Deploy(
if strings.HasPrefix(globalOptions.DataDir, "/") {
dirs = append(dirs, globalOptions.DataDir)
}

if systemdMode == spec.UserMode {
dirs = append(dirs, spec.Abs(globalOptions.User, ".config/systemd/user"))
}
t := task.NewBuilder(m.logger).
RootSSH(
host,
Expand Down
58 changes: 58 additions & 0 deletions pkg/cluster/operation/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ func Destroy(
}
}

// Delete systemd directory when use systemd user mode
if gOpts.SystemdMode == spec.UserMode {
for host := range instCount {
if err := DeleteSystemdDir(ctx, host, gOpts); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't delete it, user may have some custom service

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get

return err
}
}
}

// after all things done, try to remove SSH public key
for host := range instCount {
if err := DeletePublicKey(ctx, host); err != nil {
Expand Down Expand Up @@ -150,6 +159,15 @@ func StopAndDestroyInstance(
logger.Warnf("failed to destroy %s: %v", "monitor", err)
}
}
// Delete systemd directory when use systemd user mode
if cluster.BaseTopo().GlobalOptions.SystemdMode == spec.UserMode {
if err := DeleteSystemdDir(ctx, instance.GetManageHost(), cluster.BaseTopo().GlobalOptions); err != nil {
if !ignoreErr {
return perrs.Annotatef(err, "failed to delete systemd directory")
}
logger.Warnf("failed to delete systemd directory: %v", err)
}
}

if err := DeletePublicKey(ctx, instance.GetManageHost()); err != nil {
if !ignoreErr {
Expand Down Expand Up @@ -202,6 +220,46 @@ func DeleteGlobalDirs(ctx context.Context, host string, options *spec.GlobalOpti
return nil
}

// DeleteSystemdDir deletes systemd directory of user mode when destroy node
func DeleteSystemdDir(ctx context.Context, host string, options *spec.GlobalOptions) error {
if options == nil {
return nil
}

e := ctxt.GetInner(ctx).Get(host)
logger := ctx.Value(logprinter.ContextKeyLogger).(*logprinter.Logger)
logger.Infof("Clean systemd directory %s", host)
if options.SystemdMode != spec.UserMode {
return nil
}

dir := spec.Abs(options.User, ".config/systemd/user")

logger.Infof("\tClean directory %s on instance %s", dir, host)

c := module.ShellModuleConfig{
Command: fmt.Sprintf("rmdir %s > /dev/null 2>&1 || true", dir),
Chdir: "",
UseShell: false,
}
shell := module.NewShellModule(c)
stdout, stderr, err := shell.Execute(ctx, e)

if len(stdout) > 0 {
fmt.Println(string(stdout))
}
if len(stderr) > 0 {
logger.Errorf(string(stderr))
}

if err != nil {
return perrs.Annotatef(err, "failed to clean directory %s on: %s", dir, host)
}

logger.Infof("Clean systemd directory %s success", host)
return nil
}

// DeletePublicKey deletes the SSH public key from host
func DeletePublicKey(ctx context.Context, host string) error {
e := ctxt.GetInner(ctx).Get(host)
Expand Down
3 changes: 0 additions & 3 deletions pkg/cluster/spec/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,6 @@ func (i *BaseInstance) InitConfig(ctx context.Context, e ctxt.Executor, opt Glob
sudo := true
if opt.SystemdMode == UserMode {
systemdDir = "~/.config/systemd/user/"
if err := os.MkdirAll(systemdDir, os.ModePerm); err != nil {
return err
}
sudo = false
}
cmd := fmt.Sprintf("mv %s %s%s-%d.service", tgt, systemdDir, comp, port)
Expand Down
Loading