Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request #817 from caos/nginx-file-descriptors
Browse files Browse the repository at this point in the history
fix: enable more file descriptors for nginx
  • Loading branch information
eliobischof committed Aug 23, 2021
2 parents e474d2c + 802e592 commit 4d99e1f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
35 changes: 35 additions & 0 deletions internal/operator/nodeagent/dep/nginx/dep.go
Expand Up @@ -15,6 +15,9 @@ import (
"github.com/caos/orbos/mntr"
)

const LimitNoFileKey = "LimitNOFILE="
const LimitNoFile8192Entry = LimitNoFileKey + "8192 # Line added by CAOS node agent"

type Installer interface {
isNgninx()
nodeagent.Installer
Expand Down Expand Up @@ -68,6 +71,21 @@ func (s *nginxDep) Current() (pkg common.Package, err error) {
"nginx.conf": string(config),
}

unitPath, err := s.systemd.UnitPath("nginx")
if err != nil {
return pkg, err
}

svc, err := ioutil.ReadFile(unitPath)
if err != nil {
return pkg, err
}

// make pkg config different, so (*nginxDep).Ensure() is called
if !strings.Contains(string(svc), LimitNoFile8192Entry) {
pkg.Config["ensuresystemdconf"] = "yes"
}

return pkg, nil
}

Expand Down Expand Up @@ -127,9 +145,26 @@ module_hotfixes=true`, repoURL)), 0600); err != nil {
return err
}

unitPath, err := s.systemd.UnitPath("nginx")
if err != nil {
return err
}

if err := dep.ManipulateFile(unitPath, []string{LimitNoFileKey}, nil, func(line string) *string {
serviceLine := "[Service]"
if strings.HasPrefix(line, serviceLine) {
return strPtr(serviceLine + "\n" + LimitNoFile8192Entry)
}
return strPtr(line)
}); err != nil {
return err
}

if err := s.systemd.Enable("nginx"); err != nil {
return err
}

return s.systemd.Start("nginx")
}

func strPtr(str string) *string { return &str }
32 changes: 31 additions & 1 deletion internal/operator/nodeagent/dep/systemd.go
Expand Up @@ -84,7 +84,7 @@ func (s *SystemD) Enable(binary string) error {
}

if err := cmd.Run(); err != nil {
return fmt.Errorf("enabling systemd unit %s failed with stderr %s", binary, errBuf.String(), err)
return fmt.Errorf("enabling systemd unit %s failed with stderr %s: %w", binary, errBuf.String(), err)
}

if !s.Active(binary) {
Expand All @@ -101,3 +101,33 @@ func (s *SystemD) Active(binary string) bool {
}
return cmd.Run() == nil
}

func (s *SystemD) UnitPath(unit string) (string, error) {

const showProperty = "FragmentPath"
const expectOutputPrefix = showProperty + "="

errBuf := new(bytes.Buffer)
defer errBuf.Reset()
outBuf := new(bytes.Buffer)
defer outBuf.Reset()
cmd := exec.Command("systemctl", "show", "-p", showProperty, unit)
cmd.Stderr = errBuf
cmd.Stdout = outBuf
err := cmd.Run()
errStr := errBuf.String()
outStr := outBuf.String()
s.monitor.WithFields(map[string]interface{}{
"stdout": outStr,
"stderr": errStr,
}).Debug("Executed " + strings.Join(cmd.Args, " "))
if err != nil {
return "", fmt.Errorf("getting systemd unit path for %s failed with stderr %s: %w", unit, errStr, err)
}

if !strings.HasPrefix(outStr, expectOutputPrefix) {
return "", fmt.Errorf("expected prefix %s but got %s", expectOutputPrefix, outStr)
}

return strings.Trim(strings.TrimPrefix(outStr, expectOutputPrefix), "\n"), nil
}

0 comments on commit 4d99e1f

Please sign in to comment.