Check if docker is running on uninstall (#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
This commit is contained in:
Sashi Kumar 2020-05-15 22:26:07 +02:00 committed by GitHub
parent 6cba4a47f3
commit cbe46b1eb1
3 changed files with 44 additions and 22 deletions

View File

@ -9,7 +9,6 @@ import (
"archive/tar"
"archive/zip"
"compress/gzip"
"context"
"errors"
"fmt"
"io"
@ -25,7 +24,6 @@ import (
"sync"
"time"
"github.com/docker/docker/client"
"github.com/fatih/color"
"github.com/briandowns/spinner"
@ -108,7 +106,7 @@ func isInstallationRequired(installLocation, requestedVersion string) bool {
// Init installs Dapr on a local machine using the supplied runtimeVersion.
func Init(runtimeVersion string, dockerNetwork string, installLocation string) error {
dockerInstalled := isDockerInstalled()
dockerInstalled := utils.IsDockerInstalled()
if !dockerInstalled {
return errors.New("could not connect to Docker. Docker may not be installed or running")
}
@ -168,15 +166,6 @@ func Init(runtimeVersion string, dockerNetwork string, installLocation string) e
return nil
}
func isDockerInstalled() bool {
cli, err := client.NewEnvClient()
if err != nil {
return false
}
_, err = cli.Ping(context.Background())
return err == nil
}
func getDaprDir() (string, error) {
p := ""

View File

@ -8,9 +8,8 @@ import (
"github.com/dapr/cli/utils"
)
// Uninstall deletes all installed containers
func Uninstall(uninstallAll bool, dockerNetwork string) error {
var errs []error
func removeContainers(uninstallAll bool, dockerNetwork string) []error {
var containerErrs []error
_, err := utils.RunCmdAndWait(
"docker", "rm",
@ -18,7 +17,9 @@ func Uninstall(uninstallAll bool, dockerNetwork string) error {
utils.CreateContainerName(DaprPlacementContainerName, dockerNetwork))
if err != nil {
errs = append(errs, fmt.Errorf("could not remove %s container: %s", DaprPlacementContainerName, err))
containerErrs = append(
containerErrs,
fmt.Errorf("could not remove %s container: %s", DaprPlacementContainerName, err))
}
_, err = utils.RunCmdAndWait(
@ -27,7 +28,9 @@ func Uninstall(uninstallAll bool, dockerNetwork string) error {
daprDockerImageName)
if err != nil {
errs = append(errs, fmt.Errorf("could not remove %s container: %s", daprDockerImageName, err))
containerErrs = append(
containerErrs,
fmt.Errorf("could not remove %s image: %s", daprDockerImageName, err))
}
if uninstallAll {
@ -36,21 +39,39 @@ func Uninstall(uninstallAll bool, dockerNetwork string) error {
"--force",
utils.CreateContainerName(DaprRedisContainerName, dockerNetwork))
if err != nil {
errs = append(errs, fmt.Errorf("could not remove %s container: %s", DaprRedisContainerName, err))
containerErrs = append(
containerErrs,
fmt.Errorf("could not remove %s container: %s", DaprRedisContainerName, err))
}
}
err = rundata.DeleteRunDataFile()
return containerErrs
}
// Uninstall deletes all installed containers
func Uninstall(uninstallAll bool, dockerNetwork string) error {
var containerErrs []error
dockerInstalled := utils.IsDockerInstalled()
if dockerInstalled {
containerErrs = removeContainers(uninstallAll, dockerNetwork)
}
err := rundata.DeleteRunDataFile()
if err != nil {
fmt.Println("WARNING: could not delete run data file")
}
if len(errs) == 0 {
err = errors.New("uninstall failed")
if !dockerInstalled {
return fmt.Errorf("%w \n could not connect to Docker. Docker may not be installed or running", err)
}
if len(containerErrs) == 0 {
return nil
}
err = errors.New("uninstall failed")
for _, e := range errs {
for _, e := range containerErrs {
err = fmt.Errorf("%w \n %s", err, e)
}
return err

View File

@ -7,6 +7,7 @@ package utils
import (
"bufio"
"context"
"errors"
"fmt"
"io/ioutil"
@ -14,6 +15,7 @@ import (
"os/exec"
"strings"
"github.com/docker/docker/client"
"github.com/olekukonko/tablewriter"
)
@ -106,3 +108,13 @@ func CreateDirectory(dir string) error {
}
return os.Mkdir(dir, 0777)
}
// IsDockerInstalled checks whether docker is installed/running
func IsDockerInstalled() bool {
cli, err := client.NewEnvClient()
if err != nil {
return false
}
_, err = cli.Ping(context.Background())
return err == nil
}