From fa14385754888d4cec51622740f3521e9b3a6063 Mon Sep 17 00:00:00 2001 From: James Cockbain Date: Wed, 8 Jan 2020 15:31:07 +0000 Subject: [PATCH] Use DockerError struct for all docker errors (#303) * add docker error structs in docker.go * change logr.Println to logr.Error * use helper function to handle docker errors Signed-off-by: James Cockbain --- pkg/actions/commands.go | 3 + pkg/actions/remove.go | 8 +- pkg/actions/start.go | 21 ++++- pkg/actions/status.go | 52 +++++++++--- pkg/actions/stop-all.go | 9 +- pkg/actions/stop.go | 7 +- pkg/actions/utils.go | 29 +++++++ pkg/config/config.go | 4 +- pkg/utils/docker.go | 167 +++++++++++++++++++++++++------------- pkg/utils/docker_error.go | 18 +++- pkg/utils/filesystem.go | 9 +- pkg/utils/utils_test.go | 10 ++- 12 files changed, 249 insertions(+), 88 deletions(-) create mode 100644 pkg/actions/utils.go diff --git a/pkg/actions/commands.go b/pkg/actions/commands.go index 057346b3..cc06858e 100755 --- a/pkg/actions/commands.go +++ b/pkg/actions/commands.go @@ -25,6 +25,7 @@ import ( var homeDir = desktoputils.GetHomeDir() var dockerComposeFile = homeDir + "/.codewind/docker-compose.yaml" +var printAsJSON = false const healthEndpoint = "/api/v1/environment" @@ -822,6 +823,8 @@ func Commands() { http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} } + printAsJSON = c.GlobalBool("json") + // Handle Global log level flag switch loglevel := c.GlobalString("loglevel"); { diff --git a/pkg/actions/remove.go b/pkg/actions/remove.go index 1bd293ab..4b76cb99 100644 --- a/pkg/actions/remove.go +++ b/pkg/actions/remove.go @@ -32,7 +32,12 @@ func RemoveCommand(c *cli.Context, dockerComposeFile string) { "cw-", } - images := utils.GetImageList() + images, err := utils.GetImageList() + + if err != nil { + HandleDockerError(err) + os.Exit(1) + } fmt.Println("Removing Codewind docker images..") @@ -56,7 +61,6 @@ func RemoveCommand(c *cli.Context, dockerComposeFile string) { // DoRemoteRemove : Delete a remote Codewind deployment func DoRemoteRemove(c *cli.Context) { - printAsJSON := c.GlobalBool("json") removeOptions := remote.RemoveDeploymentOptions{ Namespace: c.String("namespace"), WorkspaceID: c.String("workspace"), diff --git a/pkg/actions/start.go b/pkg/actions/start.go index 8c8ed993..8e2f209b 100644 --- a/pkg/actions/start.go +++ b/pkg/actions/start.go @@ -13,6 +13,7 @@ package actions import ( "fmt" + "os" "github.com/eclipse/codewind-installer/pkg/utils" "github.com/urfave/cli" @@ -20,7 +21,11 @@ import ( // StartCommand : start the codewind containers func StartCommand(c *cli.Context, dockerComposeFile string, healthEndpoint string) { - status := utils.CheckContainerStatus() + status, err := utils.CheckContainerStatus() + if err != nil { + HandleDockerError(err) + os.Exit(1) + } if status { fmt.Println("Codewind is already running!") @@ -31,7 +36,17 @@ func StartCommand(c *cli.Context, dockerComposeFile string, healthEndpoint strin utils.CreateTempFile(dockerComposeFile) utils.WriteToComposeFile(dockerComposeFile, debug) - utils.DockerCompose(dockerComposeFile, tag) - utils.PingHealth(healthEndpoint) + + err := utils.DockerCompose(dockerComposeFile, tag) + if err != nil { + HandleDockerError(err) + os.Exit(1) + } + + _, pingHealthErr := utils.PingHealth(healthEndpoint) + if pingHealthErr != nil { + HandleDockerError(pingHealthErr) + os.Exit(1) + } } } diff --git a/pkg/actions/status.go b/pkg/actions/status.go index ac90c28f..35d59453 100644 --- a/pkg/actions/status.go +++ b/pkg/actions/status.go @@ -36,7 +36,6 @@ func StatusCommand(c *cli.Context) { // StatusCommandRemoteConnection : Output remote connection details func StatusCommandRemoteConnection(c *cli.Context) { - jsonOutput := c.Bool("json") || c.GlobalBool("json") conID := c.String("conid") connection, conErr := connections.GetConnectionByID(conID) if conErr != nil { @@ -46,7 +45,7 @@ func StatusCommandRemoteConnection(c *cli.Context) { PFEReady, err := apiroutes.IsPFEReady(http.DefaultClient, connection.URL) if err != nil || PFEReady == false { - if jsonOutput { + if printAsJSON { type status struct { Status string `json:"status"` } @@ -67,7 +66,7 @@ func StatusCommandRemoteConnection(c *cli.Context) { } // Codewind responded - if jsonOutput { + if printAsJSON { type status struct { Status string `json:"status"` URL string `json:"url"` @@ -87,14 +86,31 @@ func StatusCommandRemoteConnection(c *cli.Context) { // StatusCommandLocalConnection : Output local connection details func StatusCommandLocalConnection(c *cli.Context) { - jsonOutput := c.Bool("json") || c.GlobalBool("json") - if utils.CheckContainerStatus() { + containersAreRunning, err := utils.CheckContainerStatus() + if err != nil { + HandleDockerError(err) + os.Exit(1) + } + + if containersAreRunning { // Started - hostname, port := utils.GetPFEHostAndPort() - if jsonOutput { + hostname, port, err := utils.GetPFEHostAndPort() + if err != nil { + HandleDockerError(err) + os.Exit(1) + } + if printAsJSON { + imageTagArr, err := utils.GetImageTags() + if err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } - imageTagArr := utils.GetImageTags() - containerTagArr := utils.GetContainerTags() + containerTagArr, err := utils.GetContainerTags() + if err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } type status struct { Status string `json:"status"` @@ -118,11 +134,21 @@ func StatusCommandLocalConnection(c *cli.Context) { os.Exit(0) } - if utils.CheckImageStatus() { + imagesAreInstalled, err := utils.CheckImageStatus() + if err != nil { + HandleDockerError(err) + os.Exit(1) + } + + if imagesAreInstalled { // Installed but not started - if jsonOutput { + if printAsJSON { - imageTagArr := utils.GetImageTags() + imageTagArr, err := utils.GetImageTags() + if err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } type status struct { Status string `json:"status"` @@ -142,7 +168,7 @@ func StatusCommandLocalConnection(c *cli.Context) { os.Exit(0) } else { // Not installed - if jsonOutput { + if printAsJSON { output, _ := json.Marshal(map[string]string{"status": "uninstalled"}) fmt.Println(string(output)) } else { diff --git a/pkg/actions/stop-all.go b/pkg/actions/stop-all.go index a394d8ba..a743864e 100644 --- a/pkg/actions/stop-all.go +++ b/pkg/actions/stop-all.go @@ -13,15 +13,20 @@ package actions import ( "fmt" + "os" "github.com/eclipse/codewind-installer/pkg/utils" "github.com/urfave/cli" ) -//StopAllCommand to stop codewind and project containers +// StopAllCommand to stop codewind and project containers func StopAllCommand(c *cli.Context, dockerComposeFile string) { tag := c.String("tag") - containers := utils.GetContainerList() + containers, err := utils.GetContainerList() + if err != nil { + HandleDockerError(err) + os.Exit(1) + } utils.DockerComposeStop(tag, dockerComposeFile) fmt.Println("Stopping Project containers") diff --git a/pkg/actions/stop.go b/pkg/actions/stop.go index 47253b08..df55b00b 100644 --- a/pkg/actions/stop.go +++ b/pkg/actions/stop.go @@ -13,6 +13,7 @@ package actions import ( "fmt" + "os" "github.com/eclipse/codewind-installer/pkg/utils" "github.com/urfave/cli" @@ -22,5 +23,9 @@ import ( func StopCommand(c *cli.Context, dockerComposeFile string) { tag := c.String("tag") fmt.Println("Only stopping Codewind containers. To stop project containers, please use 'stop-all'") - utils.DockerComposeStop(tag, dockerComposeFile) + err := utils.DockerComposeStop(tag, dockerComposeFile) + if err != nil { + HandleDockerError(err) + os.Exit(1) + } } diff --git a/pkg/actions/utils.go b/pkg/actions/utils.go new file mode 100644 index 00000000..c59c76b6 --- /dev/null +++ b/pkg/actions/utils.go @@ -0,0 +1,29 @@ +/******************************************************************************* + * Copyright (c) 2019 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ + +package actions + +import ( + "fmt" + + "github.com/eclipse/codewind-installer/pkg/utils" + logr "github.com/sirupsen/logrus" +) + +// HandleDockerError prints a Docker error, in JSON format if the global flag is set, and as a string if not +func HandleDockerError(err *utils.DockerError) { + // printAsJSON is a global variable, set in commands.go + if printAsJSON { + fmt.Println(err.Error()) + } else { + logr.Error(err.Desc) + } +} diff --git a/pkg/config/config.go b/pkg/config/config.go index b1e9d077..d7ffe163 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -41,8 +41,8 @@ func PFEOriginFromConnection(connection *connections.Connection) (string, *Confi } func getLocalHostnameAndPort() (string, *ConfigError) { - hostname, port := utils.GetPFEHostAndPort() - if hostname == "" || port == "" { + hostname, port, err := utils.GetPFEHostAndPort() + if err != nil || hostname == "" || port == "" { return "", &ConfigError{errOpConfPFEHostnamePortNotFound, nil, "Hostname or port for PFE not found"} } val, ok := os.LookupEnv("CHE_API_EXTERNAL") diff --git a/pkg/utils/docker.go b/pkg/utils/docker.go index da22dbae..4ca7996e 100644 --- a/pkg/utils/docker.go +++ b/pkg/utils/docker.go @@ -30,7 +30,6 @@ import ( "github.com/docker/docker/client" "github.com/docker/docker/pkg/jsonmessage" "github.com/docker/docker/pkg/term" - "github.com/eclipse/codewind-installer/pkg/errors" logr "github.com/sirupsen/logrus" ) @@ -114,7 +113,7 @@ const ( ) // DockerCompose to set up the Codewind environment -func DockerCompose(dockerComposeFile string, tag string) { +func DockerCompose(dockerComposeFile string, tag string) *DockerError { setupDockerComposeEnvs(tag, "") cmd := exec.Command("docker-compose", "-f", dockerComposeFile, "up", "-d", "--force-recreate") output := new(bytes.Buffer) @@ -122,12 +121,12 @@ func DockerCompose(dockerComposeFile string, tag string) { cmd.Stderr = output if err := cmd.Start(); err != nil { // after 'Start' the program is continued and script is executing in background DeleteTempFile(dockerComposeFile) - errors.CheckErr(err, 101, "Is docker-compose installed?") + return &DockerError{errOpDockerComposeStart, err, err.Error()} } fmt.Printf("Please wait while containers initialize... %s \n", output.String()) if err := cmd.Wait(); err != nil { DeleteTempFile(dockerComposeFile) - errors.CheckErr(err, 101, "docker-compose command failed to execute correctly") // TODO - replace with new docker error + return &DockerError{errOpDockerComposeStart, err, err.Error()} } fmt.Printf(output.String()) // Wait to finish execution, so we can read all output @@ -140,48 +139,54 @@ func DockerCompose(dockerComposeFile string, tag string) { DeleteTempFile(dockerComposeFile) os.Exit(1) } + return nil } // DockerComposeStop to stop Codewind containers -func DockerComposeStop(tag, dockerComposeFile string) { +func DockerComposeStop(tag, dockerComposeFile string) *DockerError { setupDockerComposeEnvs(tag, "stop") cmd := exec.Command("docker-compose", "-f", dockerComposeFile, "rm", "--stop", "-f") output := new(bytes.Buffer) cmd.Stdout = output cmd.Stderr = output if err := cmd.Start(); err != nil { // after 'Start' the program is continued and script is executing in background - errors.CheckErr(err, 101, "") + return &DockerError{errOpDockerComposeStop, err, err.Error()} } fmt.Printf("Please wait while containers shutdown... %s \n", output.String()) if err := cmd.Wait(); err != nil { - errors.CheckErr(err, 101, "docker-compose stop command failed to execute correctly") //TODO - replace with new docker error + return &DockerError{errOpDockerComposeStop, err, err.Error()} } fmt.Printf(output.String()) // Wait to finish execution, so we can read all output if strings.Contains(output.String(), "ERROR") || strings.Contains(output.String(), "error") { os.Exit(1) } + return nil } // DockerComposeRemove to remove Codewind images -func DockerComposeRemove(dockerComposeFile, tag string) { +func DockerComposeRemove(dockerComposeFile, tag string) *DockerError { setupDockerComposeEnvs(tag, "remove") cmd := exec.Command("docker-compose", "-f", dockerComposeFile, "down", "--rmi", "all") output := new(bytes.Buffer) cmd.Stdout = output cmd.Stderr = output - if err := cmd.Start(); err != nil { // after 'Start' the program is continued and script is executing in background - errors.CheckErr(err, 101, "") + // after 'Start' the program is continued and script is executing in background + err := cmd.Start() + if err != nil { + return &DockerError{errOpDockerComposeRemove, err, err.Error()} } - fmt.Printf("Please wait while images are removed... %s \n", output.String()) - if err := cmd.Wait(); err != nil { - errors.CheckErr(err, 101, "docker-compose remove command failed to execute correctly") //TODO - replace with new docker error + fmt.Printf("Please wait whilst images are removed... %s \n", output.String()) + err = cmd.Wait() + if err != nil { + return &DockerError{errOpImageRemove, err, err.Error()} } fmt.Printf(output.String()) // Wait to finish execution, so we can read all output if strings.Contains(output.String(), "ERROR") || strings.Contains(output.String(), "error") { os.Exit(1) } + return nil } // setupDockerComposeEnvs for docker-compose to use @@ -227,16 +232,21 @@ func setupDockerComposeEnvs(tag, command string) { } // PullImage - pull pfe/performance images from dockerhub -func PullImage(image string, jsonOutput bool) { +func PullImage(image string, jsonOutput bool) *DockerError { ctx := context.Background() cli, err := client.NewClientWithOpts(client.WithVersion("1.30")) - errors.CheckErr(err, 200, "") + if err != nil { + return &DockerError{errOpImageNotFound, err, err.Error()} + } var codewindOut io.ReadCloser codewindOut, err = cli.ImagePull(ctx, image, types.ImagePullOptions{}) - errors.CheckErr(err, 100, "") + if err != nil { + return &DockerError{errOpImagePull, err, err.Error()} + } + if jsonOutput == true { defer codewindOut.Close() io.Copy(os.Stdout, codewindOut) @@ -245,6 +255,7 @@ func PullImage(image string, jsonOutput bool) { termFd, isTerm := term.GetFdInfo(os.Stderr) jsonmessage.DisplayJSONMessagesStream(codewindOut, os.Stderr, termFd, isTerm, nil) } + return nil } // ValidateImageDigest - will ensure the image digest matches that of the one in dockerhub @@ -253,7 +264,9 @@ func ValidateImageDigest(image string) (string, *DockerError) { ctx := context.Background() cli, err := client.NewClientWithOpts(client.WithVersion("1.30")) - errors.CheckErr(err, 200, "") + if err != nil { + return "", &DockerError{errOpImageDigest, err, err.Error()} + } // call docker api for image digest queryDigest, err := cli.DistributionInspect(ctx, image, "") if err != nil { @@ -265,7 +278,11 @@ func ValidateImageDigest(image string) (string, *DockerError) { logr.Traceln("Query image digest is.. ", queryDigest.Descriptor.Digest) // get local image digest - imageList := GetImageList() + imageList, dockerError := GetImageList() + if err != nil { + return "", dockerError + } + imageName := strings.TrimPrefix(image, "docker.io/") imageArr := []string{ imageName, @@ -292,12 +309,15 @@ func ValidateImageDigest(image string) (string, *DockerError) { } // TagImage - locally retag the downloaded images -func TagImage(source, tag string) { +func TagImage(source, tag string) *DockerError { out, err := exec.Command("docker", "tag", source, tag).Output() - errors.CheckErr(err, 102, "Image Tagging Failed") + if err != nil { + return &DockerError{errOpImageTag, err, err.Error()} + } output := string(out[:]) fmt.Println(output) + return nil } // GetContainersToRemove returns a list of containers ([]types.Container) matching "/cw" @@ -319,10 +339,13 @@ func GetContainersToRemove(containerList []types.Container) []types.Container { } // CheckContainerStatus of Codewind running/stopped -func CheckContainerStatus() bool { +func CheckContainerStatus() (bool, *DockerError) { var containerStatus = false containerArr := baseImageNameArr - containers := GetContainerList() + containers, err := GetContainerList() + if err != nil { + return false, err + } containerCount := 0 for _, container := range containers { @@ -337,14 +360,17 @@ func CheckContainerStatus() bool { } else { containerStatus = false } - return containerStatus + return containerStatus, nil } // CheckImageStatus of Codewind installed/uninstalled -func CheckImageStatus() bool { +func CheckImageStatus() (bool, *DockerError) { var imageStatus = false imageArr := baseImageNameArr - images := GetImageList() + images, err := GetImageList() + if err != nil { + return false, err + } imageCount := 0 for _, image := range images { @@ -358,66 +384,80 @@ func CheckImageStatus() bool { if imageCount >= 2 { imageStatus = true } - return imageStatus + return imageStatus, nil } // RemoveImage of Codewind and project -func RemoveImage(imageID string) { +func RemoveImage(imageID string) *DockerError { cmd := exec.Command("docker", "rmi", imageID, "-f") cmd.Stdin = strings.NewReader("some input") var out bytes.Buffer cmd.Stdout = &out err := cmd.Run() - errors.CheckErr(err, 105, "Failed to remove image - Please make sure all containers are stopped") + if err != nil { + return &DockerError{errOpImageRemove, err, err.Error()} + } + return nil } // GetContainerList from docker -func GetContainerList() []types.Container { +func GetContainerList() ([]types.Container, *DockerError) { ctx := context.Background() cli, err := client.NewClientWithOpts(client.WithVersion("1.30")) - errors.CheckErr(err, 200, "") + if err != nil { + return nil, &DockerError{errOpContainerList, err, err.Error()} + } containers, err := cli.ContainerList(ctx, types.ContainerListOptions{}) - errors.CheckErr(err, 107, "") - - return containers + if err != nil { + return nil, &DockerError{errOpContainerList, err, err.Error()} + } + return containers, nil } // GetImageList from docker -func GetImageList() []types.ImageSummary { +func GetImageList() ([]types.ImageSummary, *DockerError) { ctx := context.Background() cli, err := client.NewClientWithOpts(client.WithVersion("1.30")) - errors.CheckErr(err, 200, "") + if err != nil { + return nil, &DockerError{errOpImageList, err, err.Error()} + } images, err := cli.ImageList(ctx, types.ImageListOptions{}) - errors.CheckErr(err, 109, "") - - return images + if err != nil { + return nil, &DockerError{errOpImageList, err, err.Error()} + } + return images, nil } // StopContainer will stop only codewind containers -func StopContainer(container types.Container) { +func StopContainer(container types.Container) *DockerError { ctx := context.Background() cli, err := client.NewClientWithOpts(client.WithVersion("1.30")) - errors.CheckErr(err, 200, "") + if err != nil { + return &DockerError{errOpStopContainer, err, err.Error()} + } // Check if the container will remove after it is stopped isAutoRemoved, isAutoRemovedErr := getContainerAutoRemovePolicy(container.ID) if isAutoRemovedErr != nil { - errors.CheckErr(err, 108, "") + return &DockerError{errOpStopContainer, err, err.Error()} } // Stop the running container - if err := cli.ContainerStop(ctx, container.ID, nil); err != nil { - errors.CheckErr(err, 108, "") + err = cli.ContainerStop(ctx, container.ID, nil) + if err != nil { + return &DockerError{errOpStopContainer, err, err.Error()} } if !isAutoRemoved { // Remove the container so it isnt lingering in the background - if err := cli.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{}); err != nil { - errors.CheckErr(err, 108, "") + err = cli.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{}) + if err != nil { + return &DockerError{errOpStopContainer, err, err.Error()} } } + return nil } // getContainerAutoRemovePolicy will get the auto remove policy of a given container @@ -438,31 +478,41 @@ func getContainerAutoRemovePolicy(containerID string) (bool, *DockerError) { } // GetPFEHostAndPort will return the current hostname and port that PFE is running on -func GetPFEHostAndPort() (string, string) { +func GetPFEHostAndPort() (string, string, *DockerError) { + containerIsRunning, err := CheckContainerStatus() + if err != nil { + return "", "", err + } // on Che, can assume PFE is always on localhost:9090 if os.Getenv("CHE_API_EXTERNAL") != "" { - return "localhost", "9090" - } else if CheckContainerStatus() { - containerList := GetContainerList() + return "localhost", "9090", nil + } else if containerIsRunning { + containerList, err := GetContainerList() + if err != nil { + return "", "", err + } for _, container := range containerList { if strings.HasPrefix(container.Image, pfeImageName) { for _, port := range container.Ports { if port.PrivatePort == internalPFEPort { - return port.IP, strconv.Itoa(int(port.PublicPort)) + return port.IP, strconv.Itoa(int(port.PublicPort)), nil } } } } } - return "", "" + return "", "", nil } // GetImageTags of Codewind images -func GetImageTags() []string { +func GetImageTags() ([]string, *DockerError) { imageArr := baseImageNameArr tagArr := []string{} - images := GetImageList() + images, err := GetImageList() + if err != nil { + return nil, err + } for _, image := range images { imageRepo := strings.Join(image.RepoDigests, " ") @@ -482,7 +532,7 @@ func GetImageTags() []string { } tagArr = RemoveDuplicateEntries(tagArr) - return tagArr + return tagArr, err } // IsTCPPortAvailable checks to find the next available port and returns it @@ -509,11 +559,14 @@ func DetermineDebugPortForPFE() (pfeDebugPort string) { } // GetContainerTags of the Codewind version(s) currently running -func GetContainerTags() []string { +func GetContainerTags() ([]string, *DockerError) { containerArr := baseImageNameArr tagArr := []string{} - containers := GetContainerList() + containers, err := GetContainerList() + if err != nil { + return nil, err + } for _, container := range containers { for _, key := range containerArr { @@ -525,5 +578,5 @@ func GetContainerTags() []string { } tagArr = RemoveDuplicateEntries(tagArr) - return tagArr + return tagArr, nil } diff --git a/pkg/utils/docker_error.go b/pkg/utils/docker_error.go index 402e551b..b44c4b29 100644 --- a/pkg/utils/docker_error.go +++ b/pkg/utils/docker_error.go @@ -21,9 +21,21 @@ type DockerError struct { } const ( - errOpValidate = "docker_validate" // validate docker images - errOpClientCreate = "CLIENT_CREATE_ERROR" - errOpContainerInspect = "CONTAINER_INSPECT_ERROR" + errOpValidate = "DOCKER_VALIDATE" + errOpClientCreate = "CLIENT_CREATE_ERROR" + errOpContainerInspect = "CONTAINER_INSPECT_ERROR" + errOpContainerError = "CONTAINER_ERROR" + errOpStopContainer = "CONTAINER_STOP_ERROR" + errOpDockerComposeStart = "DOCKER_COMPOSE_START_ERROR" + errOpDockerComposeStop = "DOCKER_COMPOSE_STOP_ERROR" + errOpDockerComposeRemove = "DOCKER_COMPOSE_REMOVE" + errOpImageNotFound = "IMAGE_NOT_FOUND" + errOpImagePull = "IMAGE_PULL_ERROR" + errOpImageTag = "IMAGE_TAG_ERROR" + errOpImageRemove = "IMAGE_REMOVE_ERROR" + errOpImageDigest = "IMAGE_DIGEST_ERROR" + errOpContainerList = "CONTAINER_LIST_ERROR" + errOpImageList = "IMAGE_LIST_ERROR" ) const ( diff --git a/pkg/utils/filesystem.go b/pkg/utils/filesystem.go index eb4c3598..0169417c 100644 --- a/pkg/utils/filesystem.go +++ b/pkg/utils/filesystem.go @@ -96,10 +96,13 @@ func DeleteTempFile(filePath string) (bool, error) { } // PingHealth - pings environment api every 15 seconds to check if containers started -func PingHealth(healthEndpoint string) bool { +func PingHealth(healthEndpoint string) (bool, *DockerError) { var started = false fmt.Println("Waiting for Codewind to start") - hostname, port := GetPFEHostAndPort() + hostname, port, err := GetPFEHostAndPort() + if err != nil { + return false, err + } for i := 0; i < 120; i++ { resp, err := http.Get("http://" + hostname + ":" + port + healthEndpoint) if err != nil { @@ -118,7 +121,7 @@ func PingHealth(healthEndpoint string) bool { if started != true { log.Fatal("Codewind containers are taking a while to start. Please check the container logs and/or restart Codewind") } - return started + return started, nil } // GetZipURL from github api /repos/:owner/:repo/:archive_format/:ref diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index 5a2a509e..06f954e2 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -40,14 +40,20 @@ func TestRemoveImage(t *testing.T) { func TestCheckImageStatusFalse(t *testing.T) { // Test checks that image list can be searched // False return as no images have been installed for this test - result := CheckImageStatus() + result, err := CheckImageStatus() + if err != nil { + t.Fail() + } assert.Equal(t, result, false, "should return false: no images are installed") } func TestCheckContainerStatusFalse(t *testing.T) { // Test checks that container list can be searched // False return as no containers have been started for this test - result := CheckContainerStatus() + result, err := CheckContainerStatus() + if err != nil { + t.Fail() + } assert.Equal(t, result, false, "should return false: no containers are started") }