Skip to content

Commit 76105aa

Browse files
committed
Initial windows configurer
Signed-off-by: Kimmo Lehto <[email protected]>
1 parent d2bc2fe commit 76105aa

33 files changed

+730
-237
lines changed

configurer/interface.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package configurer
2+
3+
import (
4+
"time"
5+
6+
"github.com/k0sproject/rig/exec"
7+
"github.com/k0sproject/rig/os"
8+
)
9+
10+
// Configurer defines the per-host operations required for managing a host.
11+
type Configurer interface {
12+
Kind() string
13+
OSKind() string
14+
CheckPrivilege(os.Host) error
15+
StartService(os.Host, string) error
16+
StopService(os.Host, string) error
17+
RestartService(os.Host, string) error
18+
ServiceIsRunning(os.Host, string) bool
19+
Arch(os.Host) (string, error)
20+
K0sCmdf(string, ...interface{}) string
21+
K0sBinaryPath() string
22+
K0sConfigPath() string
23+
DataDirDefaultPath() string
24+
K0sJoinTokenPath() string
25+
WriteFile(os.Host, string, string, string) error
26+
UpdateEnvironment(os.Host, map[string]string) error
27+
DaemonReload(os.Host) error
28+
ReplaceK0sTokenPath(os.Host, string) error
29+
ServiceScriptPath(os.Host, string) (string, error)
30+
ReadFile(os.Host, string) (string, error)
31+
FileExist(os.Host, string) bool
32+
Chmod(os.Host, string, string, ...exec.Option) error
33+
Chown(os.Host, string, string, ...exec.Option) error
34+
DownloadURL(os.Host, string, string, ...exec.Option) error
35+
InstallPackage(os.Host, ...string) error
36+
FileContains(os.Host, string, string) bool
37+
MoveFile(os.Host, string, string) error
38+
MkDir(os.Host, string, ...exec.Option) error
39+
DeleteFile(os.Host, string) error
40+
CommandExist(os.Host, string) bool
41+
Hostname(os.Host) string
42+
KubectlCmdf(os.Host, string, string, ...interface{}) string
43+
KubeconfigPath(os.Host, string) string
44+
IsContainer(os.Host) bool
45+
FixContainer(os.Host) error
46+
HTTPStatus(os.Host, string) (int, error)
47+
PrivateInterface(os.Host) (string, error)
48+
PrivateAddress(os.Host, string, string) (string, error)
49+
TempDir(os.Host) (string, error)
50+
TempFile(os.Host) (string, error)
51+
UpdateServiceEnvironment(os.Host, string, map[string]string) error
52+
CleanupServiceEnvironment(os.Host, string) error
53+
Stat(os.Host, string, ...exec.Option) (*os.FileInfo, error)
54+
DeleteDir(os.Host, string, ...exec.Option) error
55+
K0sctlLockFilePath(os.Host) string
56+
UpsertFile(os.Host, string, string) error
57+
MachineID(os.Host) (string, error)
58+
SetPath(string, string)
59+
SystemTime(os.Host) (time.Time, error)
60+
Touch(os.Host, string, time.Time, ...exec.Option) error
61+
}

configurer/linux.go

Lines changed: 37 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -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
1918
type 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

3237
func (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
4556
func (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
5461
func (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
6366
func (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
7271
func (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
8176
func (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
128107
func (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
183151
func (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
198175
func (l *Linux) KubeconfigPath(h os.Host, dataDir string) string {
199176
linux := &os.Linux{}

configurer/linux/alpine.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ type Alpine struct {
2121
BaseLinux
2222
}
2323

24+
var _ configurer.Configurer = (*Alpine)(nil)
25+
2426
func init() {
2527
registry.RegisterOSModule(
2628
func(os rig.OSVersion) bool {

configurer/linux/archlinux.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ type Archlinux struct {
1313
configurer.Linux
1414
}
1515

16+
var _ configurer.Configurer = (*Archlinux)(nil)
17+
1618
func init() {
1719
registry.RegisterOSModule(
1820
func(os rig.OSVersion) bool {

configurer/linux/coreos.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"strings"
66

7+
"github.com/k0sproject/k0sctl/configurer"
78
"github.com/k0sproject/rig"
89
"github.com/k0sproject/rig/os"
910
"github.com/k0sproject/rig/os/registry"
@@ -15,6 +16,8 @@ type CoreOS struct {
1516
BaseLinux
1617
}
1718

19+
var _ configurer.Configurer = (*CoreOS)(nil)
20+
1821
func init() {
1922
registry.RegisterOSModule(
2023
func(os rig.OSVersion) bool {

configurer/linux/debian.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ type Debian struct {
1313
configurer.Linux
1414
}
1515

16+
var _ configurer.Configurer = (*Debian)(nil)
17+
1618
func init() {
1719
registry.RegisterOSModule(
1820
func(os rig.OSVersion) bool {

configurer/linux/enterpriselinux/almalinux.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ type AlmaLinux struct {
1313
configurer.Linux
1414
}
1515

16+
var _ configurer.Configurer = (*AlmaLinux)(nil)
17+
1618
func init() {
1719
registry.RegisterOSModule(
1820
func(os rig.OSVersion) bool {

configurer/linux/enterpriselinux/amazon.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ type AmazonLinux struct {
1414
configurer.Linux
1515
}
1616

17+
var _ configurer.Configurer = (*AmazonLinux)(nil)
18+
1719
// Hostname on amazon linux will return the full hostname
1820
func (l *AmazonLinux) Hostname(h os.Host) string {
1921
hostname, _ := h.ExecOutput("hostname")

configurer/linux/enterpriselinux/centos.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ type CentOS struct {
1313
configurer.Linux
1414
}
1515

16+
var _ configurer.Configurer = (*CentOS)(nil)
17+
1618
func init() {
1719
registry.RegisterOSModule(
1820
func(os rig.OSVersion) bool {

configurer/linux/enterpriselinux/fedora.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ type Fedora struct {
1515
configurer.Linux
1616
}
1717

18+
var _ configurer.Configurer = (*Fedora)(nil)
19+
1820
func init() {
1921
registry.RegisterOSModule(
2022
func(os rig.OSVersion) bool {

0 commit comments

Comments
 (0)