@@ -12,13 +12,18 @@ import (
1212 "al.essio.dev/pkg/shellescape"
1313 "github.com/k0sproject/rig/exec"
1414 "github.com/k0sproject/rig/os"
15- "github.com/k0sproject/version"
1615)
1716
1817// Linux is a base module for various linux OS support packages
1918type Linux struct {
20- paths map [string ]string
21- pathMu sync.Mutex
19+ paths map [string ]string
20+ pathMu sync.RWMutex
21+ pathOnce sync.Once
22+ }
23+
24+ // OSKind returns the identifier for Linux hosts
25+ func (l * Linux ) OSKind () string {
26+ return "linux"
2227}
2328
2429// NOTE The Linux struct does not embed rig/os.Linux because it will confuse
@@ -30,59 +35,48 @@ type Linux struct {
3035// path as a parameter.
3136
3237func (l * Linux ) initPaths () {
33- if l .paths != nil {
34- return
35- }
36- l .paths = map [string ]string {
37- "K0sBinaryPath" : "/usr/local/bin/k0s" ,
38- "K0sConfigPath" : "/etc/k0s/k0s.yaml" ,
39- "K0sJoinTokenPath" : "/etc/k0s/k0stoken" ,
40- "DataDirDefaultPath" : "/var/lib/k0s" ,
41- }
38+ l .pathOnce .Do (func () {
39+ l .paths = map [string ]string {
40+ "K0sBinaryPath" : "/usr/local/bin/k0s" ,
41+ "K0sConfigPath" : "/etc/k0s/k0s.yaml" ,
42+ "K0sJoinTokenPath" : "/etc/k0s/k0stoken" ,
43+ "DataDirDefaultPath" : "/var/lib/k0s" ,
44+ }
45+ })
46+ }
47+
48+ func (l * Linux ) path (key string ) string {
49+ l .initPaths ()
50+ l .pathMu .RLock ()
51+ defer l .pathMu .RUnlock ()
52+ return l .paths [key ]
4253}
4354
4455// K0sBinaryPath returns the path to the k0s binary on the host
4556func (l * Linux ) K0sBinaryPath () string {
46- l .pathMu .Lock ()
47- defer l .pathMu .Unlock ()
48-
49- l .initPaths ()
50- return l .paths ["K0sBinaryPath" ]
57+ return l .path ("K0sBinaryPath" )
5158}
5259
5360// K0sConfigPath returns the path to the k0s config file on the host
5461func (l * Linux ) K0sConfigPath () string {
55- l .pathMu .Lock ()
56- defer l .pathMu .Unlock ()
57-
58- l .initPaths ()
59- return l .paths ["K0sConfigPath" ]
62+ return l .path ("K0sConfigPath" )
6063}
6164
6265// K0sJoinTokenPath returns the path to the k0s join token file on the host
6366func (l * Linux ) K0sJoinTokenPath () string {
64- l .pathMu .Lock ()
65- defer l .pathMu .Unlock ()
66-
67- l .initPaths ()
68- return l .paths ["K0sJoinTokenPath" ]
67+ return l .path ("K0sJoinTokenPath" )
6968}
7069
7170// DataDirDefaultPath returns the path to the k0s data dir on the host
7271func (l * Linux ) DataDirDefaultPath () string {
73- l .pathMu .Lock ()
74- defer l .pathMu .Unlock ()
75-
76- l .initPaths ()
77- return l .paths ["DataDirDefaultPath" ]
72+ return l .path ("DataDirDefaultPath" )
7873}
7974
8075// SetPath sets a path for a key
8176func (l * Linux ) SetPath (key , value string ) {
77+ l .initPaths ()
8278 l .pathMu .Lock ()
8379 defer l .pathMu .Unlock ()
84-
85- l .initPaths ()
8680 l .paths [key ] = value
8781}
8882
@@ -109,21 +103,6 @@ func (l *Linux) K0sCmdf(template string, args ...interface{}) string {
109103 return fmt .Sprintf ("%s %s" , l .K0sBinaryPath (), fmt .Sprintf (template , args ... ))
110104}
111105
112- func (l * Linux ) K0sBinaryVersion (h os.Host ) (* version.Version , error ) {
113- k0sVersionCmd := l .K0sCmdf ("version" )
114- output , err := h .ExecOutput (k0sVersionCmd , exec .Sudo (h ))
115- if err != nil {
116- return nil , err
117- }
118-
119- version , err := version .NewVersion (output )
120- if err != nil {
121- return nil , err
122- }
123-
124- return version , nil
125- }
126-
127106// K0sctlLockFilePath returns a path to a lock file
128107func (l * Linux ) K0sctlLockFilePath (h os.Host ) string {
129108 if h .Exec ("test -d /run/lock" , exec .Sudo (h )) == nil {
@@ -168,17 +147,6 @@ func (l *Linux) DownloadURL(h os.Host, url, destination string, opts ...exec.Opt
168147 return nil
169148}
170149
171- // DownloadK0s performs k0s binary download from github on the host
172- func (l * Linux ) DownloadK0s (h os.Host , path string , version * version.Version , arch string , opts ... exec.Option ) error {
173- v := strings .ReplaceAll (strings .TrimPrefix (version .String (), "v" ), "+" , "%2B" )
174- url := fmt .Sprintf ("https://github.com/k0sproject/k0s/releases/download/v%[1]s/k0s-v%[1]s-%[2]s" , v , arch )
175- if err := l .DownloadURL (h , url , path , opts ... ); err != nil {
176- return fmt .Errorf ("failed to download k0s - check connectivity and k0s version validity: %w" , err )
177- }
178-
179- return nil
180- }
181-
182150// ReplaceK0sTokenPath replaces the config path in the service stub
183151func (l * Linux ) ReplaceK0sTokenPath (h os.Host , spath string ) error {
184152 return h .Exec (fmt .Sprintf ("sed -i 's^REPLACEME^%s^g' %s" , l .K0sJoinTokenPath (), spath ))
@@ -194,6 +162,15 @@ func (l *Linux) MoveFile(h os.Host, src, dst string) error {
194162 return h .Execf (`mv "%s" "%s"` , src , dst , exec .Sudo (h ))
195163}
196164
165+ // Chown sets owner for a file or directory
166+ func (l * Linux ) Chown (h os.Host , path , owner string , opts ... exec.Option ) error {
167+ if len (opts ) == 0 {
168+ opts = []exec.Option {exec .Sudo (h )}
169+ }
170+ cmd := fmt .Sprintf (`chown %s %s` , shellescape .Quote (owner ), shellescape .Quote (path ))
171+ return h .Exec (cmd , opts ... )
172+ }
173+
197174// KubeconfigPath returns the path to a kubeconfig on the host
198175func (l * Linux ) KubeconfigPath (h os.Host , dataDir string ) string {
199176 linux := & os.Linux {}
0 commit comments