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

Commit

Permalink
Choose default user depending on auto-update, use root when enabled, …
Browse files Browse the repository at this point in the history
…and postgres otherwise.
  • Loading branch information
lesovsky committed Mar 31, 2021
1 parent 69ce414 commit 5e7eb6e
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
5 changes: 3 additions & 2 deletions doc/setup-pgscv-targz.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ wget https://github.com/weaponry/pgscv/releases/download/v0.4.17/pgscv_0.4.17_li
tar xvzf pgscv_0.4.17_linux_amd64.tar.gz
```

Specify all necessary environment variables and run pgSCV with `--bootstrap` flag:
Specify all necessary environment variables and run pgSCV with `--bootstrap` flag.
**NOTE**: these settings enable auto-update and run service under `root` user. This is necessary because auto-update restarts service using systemd and root privileges required. You can use `sudo` and allow to restart the service to unprivileged user.
```
sudo -E PGSCV_RUN_AS_USER=postgres \
sudo -E PGSCV_RUN_AS_USER=root \
PGSCV_SEND_METRICS_URL="https://push.weaponry.io" \
PGSCV_AUTOUPDATE=stable \
PGSCV_API_KEY=12345678-0000-1111-2222-1234567890ab \
Expand Down
2 changes: 1 addition & 1 deletion internal/packaging/autoupdate/autoupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func runUpdate(c *Config) error {
// Explicit cleanup, because after restart execution of the code will interrupted.
doCleanup(workDir)

log.Infof("auto-update from '%s' to '%s' has been successful", c.BinaryVersion, distVersion)
log.Infof("auto-update executable from '%s' to '%s' has been successful", c.BinaryVersion, distVersion)

// Restart the service.
err = restartSystemdService()
Expand Down
4 changes: 3 additions & 1 deletion internal/packaging/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ type Config struct {
func (c *Config) Validate() error {
log.Infoln("Validate bootstrap configuration")

if c.RunAsUser == "" {
if c.RunAsUser == "" && c.AutoUpdate != "" {
c.RunAsUser = "root"
} else if c.RunAsUser == "" {
c.RunAsUser = "postgres"
}

Expand Down
2 changes: 1 addition & 1 deletion internal/pgscv/pgscv.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func (s *sendClient) sendMetrics(buf []byte) error {
// addDelay increments passed delay to random value between 1 and 10 seconds.
func addDelay(d time.Duration) time.Duration {
sec := int(math.Max(float64(d/time.Second), 1))
sec = int(math.Min(float64(sec+rand.New(rand.NewSource(time.Now().Unix())).Intn(9))+1, 60)) // #nosec G404
sec = int(math.Min(float64(sec+rand.Intn(9))+1, 60)) // #nosec G404

return time.Duration(sec) * time.Second
}

0 comments on commit 5e7eb6e

Please sign in to comment.