Skip to content

Commit

Permalink
PMM-11658 refactor TmpDir to be relative of cwd
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Tymchuk committed Jul 7, 2023
1 parent 75d83dd commit 96c146c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 30 deletions.
7 changes: 1 addition & 6 deletions .github/workflows/agent.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,7 @@ jobs:
run: make env-up

- name: Run tests
run: |
umask 000
ls -la /usr/local
whoami
sudo mkdir -p /usr/local/percona/pmm2
make test-cover
run: make test-cover

- name: Upload coverage results
uses: codecov/codecov-action@v3
Expand Down
34 changes: 21 additions & 13 deletions agent/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
)

const pathBaseDefault = "/usr/local/percona/pmm2"
const agentTmpPath = "agent-tmp" // temporary directory for agent config files, relative to workdir

// Server represents PMM Server configuration.
type Server struct {
Expand Down Expand Up @@ -239,21 +240,21 @@ func get(args []string, cfg *Config, l *logrus.Entry) (configFileF string, err e
}

if cfg.Paths.TempDir == "" {
cfg.Paths.TempDir = filepath.Join(cfg.Paths.PathsBase, "tmp")
l.Infof("TempDir is undefined, will create one at %q", cfg.Paths.TempDir)
err := os.MkdirAll(cfg.Paths.TempDir, 0o700)
if err != nil {
l.WithError(err).Panicf("unable to create the temporary directory %q", cfg.Paths.TempDir)
wd, err := os.Getwd()
if err == nil {
cfg.Paths.TempDir = filepath.Join(wd, agentTmpPath)
} else {
// As a last resort, if unable to get the workdir, use system temp directory.
cfg.Paths.TempDir = filepath.Join(os.TempDir(), agentTmpPath)
}
l.Infof("Temporary directory is undefined, will create one at %q", cfg.Paths.TempDir)
createTempDir(cfg.Paths.TempDir, l)
} else {
l.Infof("TempDir is defined as %q", cfg.Paths.TempDir)
err := IsWritable(cfg.Paths.TempDir)
if err != nil {
l.WithError(err).Infof("temporary directory %q is not writable, will attempt to re-create it", cfg.Paths.TempDir)
err := os.MkdirAll(cfg.Paths.TempDir, 0o700)
if err != nil {
l.WithError(err).Panicf("unable to create the temporary directory %q", cfg.Paths.TempDir)
}
l.Infof("Temporary directory is defined as %q", cfg.Paths.TempDir)
_, err := os.Stat(cfg.Paths.TempDir)
if err != nil && errors.Is(err, fs.ErrNotExist) {
l.WithError(err).Infof("Temporary directory %q does not exist, will attempt to create it", cfg.Paths.TempDir)
createTempDir(cfg.Paths.TempDir, l)
}
}

Expand Down Expand Up @@ -538,3 +539,10 @@ func IsWritable(path string) error {
}
return unix.Access(path, unix.W_OK)
}

func createTempDir(path string, l *logrus.Entry) {
err := os.MkdirAll(path, 0o700)
if err != nil {
l.WithError(err).Panicf("Unable to create the temporary directory %q", path)
}
}
50 changes: 40 additions & 10 deletions agent/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ func removeConfig(t *testing.T, name string) {
require.NoError(t, os.Remove(name))
}

func generateTempDir(t *testing.T, path string) string {
t.Helper()
wd, err := os.Getwd()
require.NoError(t, err)
t.Logf("Working directory: %s, tmpdir: %s", wd, filepath.Join(wd, path))
return filepath.Join(wd, path)
}

func removeTempDir(t *testing.T, path string) {
t.Helper()
require.NoError(t, os.RemoveAll(path))
}

func TestLoadFromFile(t *testing.T) {
t.Run("Normal", func(t *testing.T) {
name := writeConfig(t, &Config{ID: "agent-id"})
Expand Down Expand Up @@ -83,10 +96,6 @@ func TestLoadFromFile(t *testing.T) {
}

func TestGet(t *testing.T) {
const dir = "/usr/local/percona/pmm2"
err := os.MkdirAll(dir, os.ModePerm)
assert.NoError(t, err)

t.Run("OnlyFlags", func(t *testing.T) {
var actual Config
configFilepath, err := get([]string{
Expand All @@ -96,6 +105,9 @@ func TestGet(t *testing.T) {
}, &actual, logrus.WithField("test", t.Name()))
require.NoError(t, err)

tmpDir := generateTempDir(t, agentTmpPath)
defer removeTempDir(t, tmpDir)

expected := Config{
ID: "agent-id",
ListenAddress: "127.0.0.1",
Expand All @@ -114,7 +126,7 @@ func TestGet(t *testing.T) {
RDSExporter: "/usr/local/percona/pmm2/exporters/rds_exporter",
AzureExporter: "/usr/local/percona/pmm2/exporters/azure_exporter",
VMAgent: "/usr/local/percona/pmm2/exporters/vmagent",
TempDir: "/usr/local/percona/pmm2/tmp",
TempDir: tmpDir,
PTSummary: "/usr/local/percona/pmm2/tools/pt-summary",
PTPGSummary: "/usr/local/percona/pmm2/tools/pt-pg-summary",
PTMySQLSummary: "/usr/local/percona/pmm2/tools/pt-mysql-summary",
Expand Down Expand Up @@ -147,6 +159,9 @@ func TestGet(t *testing.T) {
}, &actual, logrus.WithField("test", t.Name()))
require.NoError(t, err)

tmpDir := generateTempDir(t, agentTmpPath)
defer removeTempDir(t, tmpDir)

expected := Config{
ID: "agent-id",
ListenAddress: "0.0.0.0",
Expand All @@ -165,7 +180,7 @@ func TestGet(t *testing.T) {
RDSExporter: "/usr/local/percona/pmm2/exporters/rds_exporter",
AzureExporter: "/usr/local/percona/pmm2/exporters/azure_exporter",
VMAgent: "/usr/local/percona/pmm2/exporters/vmagent",
TempDir: "/usr/local/percona/pmm2/tmp",
TempDir: tmpDir,
PTSummary: "/usr/local/percona/pmm2/tools/pt-summary",
PTPGSummary: "/usr/local/percona/pmm2/tools/pt-pg-summary",
PTMongoDBSummary: "/usr/local/percona/pmm2/tools/pt-mongodb-summary",
Expand Down Expand Up @@ -200,6 +215,9 @@ func TestGet(t *testing.T) {
}, &actual, logrus.WithField("test", t.Name()))
require.NoError(t, err)

tmpDir := generateTempDir(t, agentTmpPath)
defer removeTempDir(t, tmpDir)

expected := Config{
ID: "flag-id",
ListenAddress: "127.0.0.1",
Expand All @@ -218,7 +236,7 @@ func TestGet(t *testing.T) {
RDSExporter: "/usr/local/percona/pmm2/exporters/rds_exporter",
AzureExporter: "/usr/local/percona/pmm2/exporters/azure_exporter",
VMAgent: "/usr/local/percona/pmm2/exporters/vmagent",
TempDir: "/usr/local/percona/pmm2/tmp",
TempDir: tmpDir,
PTSummary: "/usr/local/percona/pmm2/tools/pt-summary",
PTPGSummary: "/usr/local/percona/pmm2/tools/pt-pg-summary",
PTMySQLSummary: "/usr/local/percona/pmm2/tools/pt-mysql-summary",
Expand Down Expand Up @@ -261,6 +279,9 @@ func TestGet(t *testing.T) {
}, &actual, logrus.WithField("test", t.Name()))
require.NoError(t, err)

tmpDir := generateTempDir(t, agentTmpPath)
defer removeTempDir(t, tmpDir)

expected := Config{
ID: "flag-id",
ListenAddress: "127.0.0.1",
Expand All @@ -279,7 +300,7 @@ func TestGet(t *testing.T) {
RDSExporter: "/base/rds_exporter", // default value
AzureExporter: "/base/azure_exporter", // default value
VMAgent: "/base/vmagent", // default value
TempDir: "/usr/local/percona/pmm2/tmp",
TempDir: tmpDir,
PTSummary: "/usr/local/percona/pmm2/tools/pt-summary",
PTPGSummary: "/usr/local/percona/pmm2/tools/pt-pg-summary",
PTMongoDBSummary: "/usr/local/percona/pmm2/tools/pt-mongodb-summary",
Expand Down Expand Up @@ -322,6 +343,8 @@ func TestGet(t *testing.T) {
}, &actual, logrus.WithField("test", t.Name()))
require.NoError(t, err)

defer removeTempDir(t, "/foo/tmp")

expected := Config{
ID: "flag-id",
ListenAddress: "127.0.0.1",
Expand Down Expand Up @@ -379,6 +402,9 @@ func TestGet(t *testing.T) {
}, &actual, logrus.WithField("test", t.Name()))
require.NoError(t, err)

tmpDir := generateTempDir(t, agentTmpPath)
defer removeTempDir(t, tmpDir)

expected := Config{
ID: "flag-id",
ListenAddress: "127.0.0.1",
Expand All @@ -397,7 +423,7 @@ func TestGet(t *testing.T) {
RDSExporter: "/foo/exporters/rds_exporter", // default value
AzureExporter: "/foo/exporters/azure_exporter", // default value
VMAgent: "/foo/exporters/vmagent", // default value
TempDir: "/base/tmp",
TempDir: tmpDir,
PTSummary: "/base/tools/pt-summary",
PTPGSummary: "/base/tools/pt-pg-summary",
PTMongoDBSummary: "/base/tools/pt-mongodb-summary",
Expand Down Expand Up @@ -426,6 +452,10 @@ func TestGet(t *testing.T) {
"--id=flag-id",
"--debug",
}, &actual, logrus.WithField("test", t.Name()))

tmpDir := generateTempDir(t, agentTmpPath)
defer removeTempDir(t, tmpDir)

expected := Config{
ID: "flag-id",
ListenAddress: "127.0.0.1",
Expand All @@ -441,7 +471,7 @@ func TestGet(t *testing.T) {
RDSExporter: "/usr/local/percona/pmm2/exporters/rds_exporter",
AzureExporter: "/usr/local/percona/pmm2/exporters/azure_exporter",
VMAgent: "/usr/local/percona/pmm2/exporters/vmagent",
TempDir: "/usr/local/percona/pmm2/tmp",
TempDir: tmpDir,
PTSummary: "/usr/local/percona/pmm2/tools/pt-summary",
PTPGSummary: "/usr/local/percona/pmm2/tools/pt-pg-summary",
PTMongoDBSummary: "/usr/local/percona/pmm2/tools/pt-mongodb-summary",
Expand Down
2 changes: 1 addition & 1 deletion managed/cmd/pmm-managed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ func setup(ctx context.Context, deps *setupDeps) bool {
return false
}
ssoDetails, err := models.GetPerconaSSODetails(ctx, db.Querier)
if err != nil {
if err != nil && !errors.Is(err, models.ErrNotConnectedToPortal) {
deps.l.Warnf("Failed to get Percona SSO Details: %s.", err)
}
if err = deps.supervisord.UpdateConfiguration(settings, ssoDetails); err != nil {
Expand Down

0 comments on commit 96c146c

Please sign in to comment.