Skip to content

Commit beeb035

Browse files
authored
Merge pull request #74 from thin-edge/feat-config-data-dir
feat(config): add data_dir config setting
2 parents db22760 + d0bc390 commit beeb035

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

cli/container_group/install.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ func (c *InstallCommand) RunE(cmd *cobra.Command, args []string) error {
6161
// Run docker compose down before up
6262
// TODO: Move to settings file
6363
downFirst := false
64-
baseDir := "/var/tedge-container-plugin/compose"
65-
workingDir := filepath.Join(baseDir, projectName)
64+
workingDir := filepath.Join(c.CommandContext.PersistentDir(true), "compose", projectName)
6665

6766
// Stop project
6867
if downFirst && utils.PathExists(workingDir) {

packaging/config.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ log_level = "info"
44
# Service name used to show the status of the service
55
service_name = "tedge-container-plugin"
66

7+
# Data directory used to store state such as docker compose project files
8+
# The first writable directory will be used
9+
data_dir = ["/var/tedge-container-plugin", "/data/tedge-container-plugin"]
10+
711
# Remove the legacy tedge-container-monitor service
812
delete_legacy = true
913

pkg/cli/cli.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"log/slog"
66
"os"
7+
"path/filepath"
78
"sort"
89
"strings"
910
"time"
@@ -27,6 +28,7 @@ func (c *Cli) OnInit() {
2728
// Set shared config
2829
viper.SetDefault("container.network", "tedge")
2930
viper.SetDefault("delete_legacy", true)
31+
viper.SetDefault("data_dir", []string{"/var/tedge-container-plugin", "/data/tedge-container-plugin"})
3032

3133
if c.ConfigFile != "" && utils.PathExists(c.ConfigFile) {
3234
// Use config file from the flag.
@@ -165,6 +167,25 @@ func (c *Cli) GetDeviceTarget() tedge.Target {
165167
}
166168
}
167169

170+
func (c *Cli) PersistentDir(check_writable bool) string {
171+
paths := viper.GetStringSlice("data_dir")
172+
defaultDir := filepath.Join(os.TempDir(), c.GetServiceName())
173+
174+
if !check_writable {
175+
if len(paths) > 0 {
176+
return paths[0]
177+
}
178+
return defaultDir
179+
}
180+
181+
for _, p := range paths {
182+
if ok, _ := utils.IsDirWritable(p, 0755); ok {
183+
return p
184+
}
185+
}
186+
return defaultDir
187+
}
188+
168189
func getExpandedStringSlice(key string) []string {
169190
v := viper.GetStringSlice(key)
170191
out := make([]string, 0, len(v))

pkg/utils/utils.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package utils
22

33
import (
44
"errors"
5+
"io/fs"
56
"os"
67
"os/exec"
78
)
@@ -26,3 +27,20 @@ func CommandExists(cmd string) bool {
2627
_, err := exec.LookPath(cmd)
2728
return err == nil
2829
}
30+
31+
// Check if a directory is writable.
32+
// It tries to create a dummy file in the directory to verify
33+
func IsDirWritable(d string, perm fs.FileMode) (bool, error) {
34+
dirErr := os.MkdirAll(d, 0755)
35+
if dirErr != nil {
36+
return false, dirErr
37+
}
38+
39+
file, err := os.CreateTemp(d, ".write-test")
40+
if err != nil {
41+
return false, err
42+
}
43+
defer os.Remove(file.Name())
44+
defer file.Close()
45+
return true, nil
46+
}

0 commit comments

Comments
 (0)