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/tar"
"archive/zip" "archive/zip"
"compress/gzip" "compress/gzip"
"context"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -25,7 +24,6 @@ import (
"sync" "sync"
"time" "time"
"github.com/docker/docker/client"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/briandowns/spinner" "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. // Init installs Dapr on a local machine using the supplied runtimeVersion.
func Init(runtimeVersion string, dockerNetwork string, installLocation string) error { func Init(runtimeVersion string, dockerNetwork string, installLocation string) error {
dockerInstalled := isDockerInstalled() dockerInstalled := utils.IsDockerInstalled()
if !dockerInstalled { if !dockerInstalled {
return errors.New("could not connect to Docker. Docker may not be installed or running") 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 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) { func getDaprDir() (string, error) {
p := "" p := ""

View File

@ -8,9 +8,8 @@ import (
"github.com/dapr/cli/utils" "github.com/dapr/cli/utils"
) )
// Uninstall deletes all installed containers func removeContainers(uninstallAll bool, dockerNetwork string) []error {
func Uninstall(uninstallAll bool, dockerNetwork string) error { var containerErrs []error
var errs []error
_, err := utils.RunCmdAndWait( _, err := utils.RunCmdAndWait(
"docker", "rm", "docker", "rm",
@ -18,7 +17,9 @@ func Uninstall(uninstallAll bool, dockerNetwork string) error {
utils.CreateContainerName(DaprPlacementContainerName, dockerNetwork)) utils.CreateContainerName(DaprPlacementContainerName, dockerNetwork))
if err != nil { 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( _, err = utils.RunCmdAndWait(
@ -27,7 +28,9 @@ func Uninstall(uninstallAll bool, dockerNetwork string) error {
daprDockerImageName) daprDockerImageName)
if err != nil { 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 { if uninstallAll {
@ -36,21 +39,39 @@ func Uninstall(uninstallAll bool, dockerNetwork string) error {
"--force", "--force",
utils.CreateContainerName(DaprRedisContainerName, dockerNetwork)) utils.CreateContainerName(DaprRedisContainerName, dockerNetwork))
if err != nil { 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 { if err != nil {
fmt.Println("WARNING: could not delete run data file") 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 return nil
} }
err = errors.New("uninstall failed") for _, e := range containerErrs {
for _, e := range errs {
err = fmt.Errorf("%w \n %s", err, e) err = fmt.Errorf("%w \n %s", err, e)
} }
return err return err

View File

@ -7,6 +7,7 @@ package utils
import ( import (
"bufio" "bufio"
"context"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
@ -14,6 +15,7 @@ import (
"os/exec" "os/exec"
"strings" "strings"
"github.com/docker/docker/client"
"github.com/olekukonko/tablewriter" "github.com/olekukonko/tablewriter"
) )
@ -106,3 +108,13 @@ func CreateDirectory(dir string) error {
} }
return os.Mkdir(dir, 0777) 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
}