Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Use DockerError struct for all docker errors (#303)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
jcockbain authored and hhellyer committed Jan 8, 2020
1 parent ad7fa03 commit fa14385
Show file tree
Hide file tree
Showing 12 changed files with 249 additions and 88 deletions.
3 changes: 3 additions & 0 deletions pkg/actions/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

var homeDir = desktoputils.GetHomeDir()
var dockerComposeFile = homeDir + "/.codewind/docker-compose.yaml"
var printAsJSON = false

const healthEndpoint = "/api/v1/environment"

Expand Down Expand Up @@ -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"); {
Expand Down
8 changes: 6 additions & 2 deletions pkg/actions/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -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..")

Expand All @@ -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"),
Expand Down
21 changes: 18 additions & 3 deletions pkg/actions/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@ package actions

import (
"fmt"
"os"

"github.com/eclipse/codewind-installer/pkg/utils"
"github.com/urfave/cli"
)

// 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!")
Expand All @@ -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)
}
}
}
52 changes: 39 additions & 13 deletions pkg/actions/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"`
}
Expand All @@ -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"`
Expand All @@ -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"`
Expand All @@ -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"`
Expand All @@ -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 {
Expand Down
9 changes: 7 additions & 2 deletions pkg/actions/stop-all.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
7 changes: 6 additions & 1 deletion pkg/actions/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package actions

import (
"fmt"
"os"

"github.com/eclipse/codewind-installer/pkg/utils"
"github.com/urfave/cli"
Expand All @@ -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)
}
}
29 changes: 29 additions & 0 deletions pkg/actions/utils.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
4 changes: 2 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading

0 comments on commit fa14385

Please sign in to comment.