Skip to content

Commit e423f9a

Browse files
authored
feat: support to install docker via dnf (#337)
Co-authored-by: rick <[email protected]>
1 parent f823723 commit e423f9a

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed

pkg/os/dnf/docker.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package dnf
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"time"
7+
8+
"github.com/linuxsuren/http-downloader/pkg/exec"
9+
)
10+
11+
// dockerInstallerInFedora is the installer of Docker in Fedora
12+
type dockerInstallerInFedora struct {
13+
Execer exec.Execer
14+
count int
15+
}
16+
17+
// Available check if support current platform
18+
func (d *dockerInstallerInFedora) Available() (ok bool) {
19+
if d.Execer.OS() == "linux" {
20+
_, err := d.Execer.LookPath("dnf")
21+
ok = err == nil
22+
}
23+
return
24+
}
25+
26+
// Install installs the Docker
27+
func (d *dockerInstallerInFedora) Install() (err error) {
28+
if err = d.Execer.RunCommand("dnf", "-y", "install", "dnf-plugins-core"); err != nil {
29+
return
30+
}
31+
32+
if err = d.Execer.RunCommand("dnf", "config-manager",
33+
"--add-repo", "https://download.docker.com/linux/fedora/docker-ce.repo"); err != nil {
34+
return
35+
}
36+
37+
err = d.Execer.RunCommand("dnf", "install", "docker-ce", "docker-ce-cli", "containerd.io", "docker-compose-plugin")
38+
return
39+
}
40+
41+
// Uninstall uninstalls the Docker
42+
func (d *dockerInstallerInFedora) Uninstall() (err error) {
43+
err = d.Execer.RunCommand("dnf", "remove", "docker",
44+
"docker-client", "docker-client-latest", "docker-common", "docker-latest",
45+
"docker-latest-logrotate",
46+
"docker-logrotate",
47+
"docker-selinux",
48+
"docker-engine-selinux",
49+
"docker-engine")
50+
return
51+
}
52+
53+
// WaitForStart waits for the service be started
54+
func (d *dockerInstallerInFedora) WaitForStart() (ok bool, err error) {
55+
var result string
56+
if result, err = d.Execer.RunCommandAndReturn("systemctl", "", "status", "docker"); err != nil {
57+
return
58+
} else if strings.Contains(result, "Unit docker.service could not be found") {
59+
err = fmt.Errorf("unit docker.service could not be found")
60+
} else if strings.Contains(result, "Active: active") {
61+
ok = true
62+
} else {
63+
if d.count > 0 {
64+
fmt.Println("waiting for Docker service start")
65+
} else if d.count > 9 {
66+
return
67+
}
68+
69+
d.count++
70+
time.Sleep(time.Second * 1)
71+
return d.WaitForStart()
72+
}
73+
return
74+
}
75+
76+
// Start starts the Docker service
77+
func (d *dockerInstallerInFedora) Start() (err error) {
78+
err = d.Execer.RunCommand("systemctl", "start", "docker")
79+
return
80+
}
81+
82+
// Stop stops the Docker service
83+
func (d *dockerInstallerInFedora) Stop() (err error) {
84+
err = d.Execer.RunCommand("systemctl", "stop", "docker")
85+
return
86+
}

pkg/os/dnf/init.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package dnf
2+
3+
import (
4+
"github.com/linuxsuren/http-downloader/pkg/exec"
5+
"github.com/linuxsuren/http-downloader/pkg/os/core"
6+
)
7+
8+
// SetInstallerRegistry sets the installer of registry
9+
func SetInstallerRegistry(registry core.InstallerRegistry, defaultExecer exec.Execer) {
10+
registry.Registry("docker", &dockerInstallerInFedora{Execer: defaultExecer})
11+
}

pkg/os/dnf/init_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package dnf
2+
3+
import (
4+
"errors"
5+
"testing"
6+
7+
"github.com/linuxsuren/http-downloader/pkg/exec"
8+
"github.com/linuxsuren/http-downloader/pkg/os/core"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestCommonCase(t *testing.T) {
13+
registry := &core.FakeRegistry{}
14+
SetInstallerRegistry(registry, exec.FakeExecer{
15+
ExpectOS: "linux",
16+
})
17+
18+
registry.Walk(func(s string, i core.Installer) {
19+
t.Run(s, func(t *testing.T) {
20+
assert.True(t, i.Available())
21+
assert.Nil(t, i.Uninstall())
22+
assert.Nil(t, i.Install())
23+
assert.Nil(t, i.Start())
24+
assert.Nil(t, i.Stop())
25+
})
26+
})
27+
28+
errRegistry := &core.FakeRegistry{}
29+
SetInstallerRegistry(errRegistry, exec.FakeExecer{
30+
ExpectLookPathError: errors.New("error"),
31+
ExpectError: errors.New("error"),
32+
ExpectOS: "linux",
33+
})
34+
errRegistry.Walk(func(s string, i core.Installer) {
35+
t.Run(s, func(t *testing.T) {
36+
assert.False(t, i.Available())
37+
assert.NotNil(t, i.Uninstall())
38+
if s != "docker" {
39+
assert.NotNil(t, i.Install())
40+
assert.Nil(t, i.Start())
41+
assert.Nil(t, i.Stop())
42+
ok, err := i.WaitForStart()
43+
assert.True(t, ok)
44+
assert.Nil(t, err)
45+
}
46+
})
47+
})
48+
}

pkg/os/installer.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/linuxsuren/http-downloader/pkg/exec"
99
"github.com/linuxsuren/http-downloader/pkg/os/apt"
1010
"github.com/linuxsuren/http-downloader/pkg/os/core"
11+
"github.com/linuxsuren/http-downloader/pkg/os/dnf"
1112
"github.com/linuxsuren/http-downloader/pkg/os/docker"
1213
"github.com/linuxsuren/http-downloader/pkg/os/yum"
1314
"github.com/mitchellh/go-homedir"
@@ -28,6 +29,7 @@ func init() {
2829
yum.SetInstallerRegistry(defaultInstallerRegistry, defaultExecer)
2930
apt.SetInstallerRegistry(defaultInstallerRegistry, defaultExecer)
3031
docker.SetInstallerRegistry(defaultInstallerRegistry, defaultExecer)
32+
dnf.SetInstallerRegistry(defaultInstallerRegistry, defaultExecer)
3133

3234
var userHome string
3335
var err error

0 commit comments

Comments
 (0)