talos

Форк
0
96 строк · 2.2 Кб
1
// This Source Code Form is subject to the terms of the Mozilla Public
2
// License, v. 2.0. If a copy of the MPL was not distributed with this
3
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4

5
package config
6

7
import (
8
	"errors"
9
	"os"
10
	"path/filepath"
11

12
	"github.com/siderolabs/talos/pkg/machinery/constants"
13
)
14

15
// Path represents a path to a configuration file.
16
type Path struct {
17
	// Path is the filesystem path of the config.
18
	Path string
19
	// WriteAllowed is true if the path is allowed to be written.
20
	WriteAllowed bool
21
}
22

23
// GetTalosDirectory returns path to Talos directory (~/.talos).
24
func GetTalosDirectory() (string, error) {
25
	home, err := os.UserHomeDir()
26
	if err != nil {
27
		return "", err
28
	}
29

30
	return filepath.Join(home, constants.TalosDir), nil
31
}
32

33
// GetDefaultPaths returns the list of config file paths in order of priority.
34
func GetDefaultPaths() ([]Path, error) {
35
	talosDir, err := GetTalosDirectory()
36
	if err != nil {
37
		return nil, err
38
	}
39

40
	result := make([]Path, 0, 3)
41

42
	if path, ok := os.LookupEnv(constants.TalosConfigEnvVar); ok {
43
		result = append(result, Path{
44
			Path:         path,
45
			WriteAllowed: true,
46
		})
47
	}
48

49
	result = append(
50
		result,
51
		Path{
52
			Path:         filepath.Join(talosDir, constants.TalosconfigFilename),
53
			WriteAllowed: true,
54
		},
55
		Path{
56
			Path:         filepath.Join(constants.ServiceAccountMountPath, constants.TalosconfigFilename),
57
			WriteAllowed: false,
58
		},
59
	)
60

61
	return result, nil
62
}
63

64
// firstValidPath iterates over the default paths and returns the first one that exists and readable.
65
// If no path is found, it will ensure that the first path that allows writes is created and returned.
66
// If no path is found that is writable, an error is returned.
67
func firstValidPath() (Path, error) {
68
	paths, err := GetDefaultPaths()
69
	if err != nil {
70
		return Path{}, err
71
	}
72

73
	var firstWriteAllowed Path
74

75
	for _, path := range paths {
76
		_, err = os.Stat(path.Path)
77
		if err == nil {
78
			return path, nil
79
		}
80

81
		if firstWriteAllowed.Path == "" && path.WriteAllowed {
82
			firstWriteAllowed = path
83
		}
84
	}
85

86
	if firstWriteAllowed.Path == "" {
87
		return Path{}, errors.New("no valid config paths found")
88
	}
89

90
	err = ensure(firstWriteAllowed.Path)
91
	if err != nil {
92
		return Path{}, err
93
	}
94

95
	return firstWriteAllowed, nil
96
}
97

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.