Skip to content

Commit cbe46b1

Browse files
authored
Check if docker is running on uninstall (dapr#334)
* Check if docker is running on uninstall * Add comment for exported method * Call DeleteRunDataFile even if docker is not running * Refactor: Move to common method and rename container to image
1 parent 6cba4a4 commit cbe46b1

File tree

3 files changed

+44
-22
lines changed

3 files changed

+44
-22
lines changed

pkg/standalone/standalone.go

+1-12
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"archive/tar"
1010
"archive/zip"
1111
"compress/gzip"
12-
"context"
1312
"errors"
1413
"fmt"
1514
"io"
@@ -25,7 +24,6 @@ import (
2524
"sync"
2625
"time"
2726

28-
"github.com/docker/docker/client"
2927
"github.com/fatih/color"
3028

3129
"github.com/briandowns/spinner"
@@ -108,7 +106,7 @@ func isInstallationRequired(installLocation, requestedVersion string) bool {
108106

109107
// Init installs Dapr on a local machine using the supplied runtimeVersion.
110108
func Init(runtimeVersion string, dockerNetwork string, installLocation string) error {
111-
dockerInstalled := isDockerInstalled()
109+
dockerInstalled := utils.IsDockerInstalled()
112110
if !dockerInstalled {
113111
return errors.New("could not connect to Docker. Docker may not be installed or running")
114112
}
@@ -168,15 +166,6 @@ func Init(runtimeVersion string, dockerNetwork string, installLocation string) e
168166
return nil
169167
}
170168

171-
func isDockerInstalled() bool {
172-
cli, err := client.NewEnvClient()
173-
if err != nil {
174-
return false
175-
}
176-
_, err = cli.Ping(context.Background())
177-
return err == nil
178-
}
179-
180169
func getDaprDir() (string, error) {
181170
p := ""
182171

pkg/standalone/uninstall.go

+31-10
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,18 @@ import (
88
"github.com/dapr/cli/utils"
99
)
1010

11-
// Uninstall deletes all installed containers
12-
func Uninstall(uninstallAll bool, dockerNetwork string) error {
13-
var errs []error
11+
func removeContainers(uninstallAll bool, dockerNetwork string) []error {
12+
var containerErrs []error
1413

1514
_, err := utils.RunCmdAndWait(
1615
"docker", "rm",
1716
"--force",
1817
utils.CreateContainerName(DaprPlacementContainerName, dockerNetwork))
1918

2019
if err != nil {
21-
errs = append(errs, fmt.Errorf("could not remove %s container: %s", DaprPlacementContainerName, err))
20+
containerErrs = append(
21+
containerErrs,
22+
fmt.Errorf("could not remove %s container: %s", DaprPlacementContainerName, err))
2223
}
2324

2425
_, err = utils.RunCmdAndWait(
@@ -27,7 +28,9 @@ func Uninstall(uninstallAll bool, dockerNetwork string) error {
2728
daprDockerImageName)
2829

2930
if err != nil {
30-
errs = append(errs, fmt.Errorf("could not remove %s container: %s", daprDockerImageName, err))
31+
containerErrs = append(
32+
containerErrs,
33+
fmt.Errorf("could not remove %s image: %s", daprDockerImageName, err))
3134
}
3235

3336
if uninstallAll {
@@ -36,21 +39,39 @@ func Uninstall(uninstallAll bool, dockerNetwork string) error {
3639
"--force",
3740
utils.CreateContainerName(DaprRedisContainerName, dockerNetwork))
3841
if err != nil {
39-
errs = append(errs, fmt.Errorf("could not remove %s container: %s", DaprRedisContainerName, err))
42+
containerErrs = append(
43+
containerErrs,
44+
fmt.Errorf("could not remove %s container: %s", DaprRedisContainerName, err))
4045
}
4146
}
4247

43-
err = rundata.DeleteRunDataFile()
48+
return containerErrs
49+
}
50+
51+
// Uninstall deletes all installed containers
52+
func Uninstall(uninstallAll bool, dockerNetwork string) error {
53+
var containerErrs []error
54+
55+
dockerInstalled := utils.IsDockerInstalled()
56+
if dockerInstalled {
57+
containerErrs = removeContainers(uninstallAll, dockerNetwork)
58+
}
59+
60+
err := rundata.DeleteRunDataFile()
4461
if err != nil {
4562
fmt.Println("WARNING: could not delete run data file")
4663
}
4764

48-
if len(errs) == 0 {
65+
err = errors.New("uninstall failed")
66+
if !dockerInstalled {
67+
return fmt.Errorf("%w \n could not connect to Docker. Docker may not be installed or running", err)
68+
}
69+
70+
if len(containerErrs) == 0 {
4971
return nil
5072
}
5173

52-
err = errors.New("uninstall failed")
53-
for _, e := range errs {
74+
for _, e := range containerErrs {
5475
err = fmt.Errorf("%w \n %s", err, e)
5576
}
5677
return err

utils/utils.go

+12
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ package utils
77

88
import (
99
"bufio"
10+
"context"
1011
"errors"
1112
"fmt"
1213
"io/ioutil"
1314
"os"
1415
"os/exec"
1516
"strings"
1617

18+
"github.com/docker/docker/client"
1719
"github.com/olekukonko/tablewriter"
1820
)
1921

@@ -106,3 +108,13 @@ func CreateDirectory(dir string) error {
106108
}
107109
return os.Mkdir(dir, 0777)
108110
}
111+
112+
// IsDockerInstalled checks whether docker is installed/running
113+
func IsDockerInstalled() bool {
114+
cli, err := client.NewEnvClient()
115+
if err != nil {
116+
return false
117+
}
118+
_, err = cli.Ping(context.Background())
119+
return err == nil
120+
}

0 commit comments

Comments
 (0)