Skip to content

Commit 88e04d1

Browse files
feat: if docker is not available install it (#668) (#669)
(cherry picked from commit 374dd81) Co-authored-by: Tanmoy Sarkar <[email protected]>
1 parent ff44df3 commit 88e04d1

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

main.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
"github.com/fatih/color"
56
"github.com/moby/sys/user"
67
"github.com/swiftwave-org/swiftwave/swiftwave_service/cmd"
@@ -45,9 +46,54 @@ func main() {
4546
// management node also needs docker for running postgres or registry at-least
4647
_, err = exec.LookPath("docker")
4748
if err != nil {
48-
color.Red("Docker is not installed. Aborting.")
49-
os.Exit(1)
49+
color.Red("Docker is not installed.")
50+
isDockerInstalled := false
51+
color.Blue("Run `curl -fsSL get.docker.com | bash -` to install docker.")
52+
color.Blue("Do you want to install docker now? (y/n)")
53+
fmt.Print("> ")
54+
var response string
55+
_, err := fmt.Scanln(&response)
56+
if err != nil {
57+
color.Red("Error reading response. Aborting.")
58+
os.Exit(1)
59+
}
60+
if strings.Compare(response, "y") == 0 || strings.Compare(response, "Y") == 0 {
61+
color.Blue("Installing docker...")
62+
// install docker
63+
err = runCommand(exec.Command("bash", "-c", "curl -fsSL get.docker.com | bash -"))
64+
if err != nil {
65+
color.Red("Error installing docker. Aborting.")
66+
os.Exit(1)
67+
}
68+
// enable docker service
69+
err = runCommand(exec.Command("systemctl", "enable", "docker"))
70+
if err != nil {
71+
color.Red("Error enabling docker. Aborting.")
72+
os.Exit(1)
73+
}
74+
// start docker service
75+
err = runCommand(exec.Command("systemctl", "start", "docker"))
76+
if err != nil {
77+
color.Red("Error starting docker. Aborting.")
78+
os.Exit(1)
79+
}
80+
isDockerInstalled = true
81+
}
82+
if !isDockerInstalled {
83+
color.Red("Docker is not installed. Aborting.")
84+
os.Exit(1)
85+
} else {
86+
color.Green("Docker is installed.")
87+
}
5088
}
5189
// Start the command line interface
5290
cmd.Execute()
5391
}
92+
93+
// private function
94+
func runCommand(command *exec.Cmd) error {
95+
command.Stdout = os.Stdout
96+
command.Stderr = os.Stderr
97+
command.Stdin = os.Stdin
98+
return command.Run()
99+
}

0 commit comments

Comments
 (0)