Skip to content

Commit

Permalink
logging: change Fatal* to Error*, and ensure that return err always e…
Browse files Browse the repository at this point in the history
…xecutes directly after a log.Error. Exception: utils.go:ExtractGzip()
  • Loading branch information
randomshinichi committed Dec 22, 2020
1 parent 63dc5d3 commit bc6a3f6
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 86 deletions.
12 changes: 6 additions & 6 deletions cmd/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,19 @@ func setupEvent(cmd *cobra.Command, args []string) (err error) {
fmt.Println("Here we go!!")
err = lctrld.CreateEvent(settings, evt)
if err != nil {
log.Fatal("There was an error, run the command with --debug for more info:", err)
log.Error("There was an error, run the command with --debug for more info:", err)
return err
}

dmc := lctrld.NewDockerMachineConfig(settings, evt.ID())
err = lctrld.Provision(settings, evt, lctrld.RunCommand, dmc)
if err != nil {
log.Fatal("There was an error, run the command with --debug for more info:", err)
log.Error("There was an error, run the command with --debug for more info:", err)
return err
}
err = lctrld.StoreEvent(settings, evt)
if err != nil {
log.Fatal("There was a problem saving the updated Event", err)
log.Error("There was a problem saving the updated Event", err)
return err
}
fmt.Println("Operation completed in", time.Since(start))
Expand All @@ -113,12 +113,12 @@ func tearDownEvent(cmd *cobra.Command, args []string) (err error) {
start := time.Now()
evt, err := lctrld.LoadEvent(settings, args[0])
if err != nil {
log.Fatal("There was an error shutting down the event: ", err)
log.Error("There was an error shutting down the event: ", err)
return err
}
err = lctrld.DestroyEvent(settings, evt, lctrld.RunCommand)
if err != nil {
log.Fatal("There was an error shutting down the event: ", err)
log.Error("There was an error shutting down the event: ", err)
return err
}
fmt.Println("Operation completed in", time.Since(start))
Expand Down Expand Up @@ -177,7 +177,7 @@ func retryEvent(cmd *cobra.Command, args []string) (err error) {
log.Infof("Updated info for %s: %#v\n", v, evt2.State[v])
}
if err != nil {
log.Fatal("There was a problem saving the updated Event", err)
log.Error("There was a problem saving the updated Event", err)
return
}
return
Expand Down
4 changes: 2 additions & 2 deletions cmd/serve.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cmd

import (
"log"
log "github.com/sirupsen/logrus"

"github.com/apeunit/LaunchControlD/pkg/server"
"github.com/spf13/cobra"
Expand All @@ -20,6 +20,6 @@ func init() {

func serve(cmd *cobra.Command, args []string) {
if err := server.ServeHTTP(settings); err != nil {
log.Fatal(err)
log.Error(err)
}
}
2 changes: 1 addition & 1 deletion pkg/lctrld/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func RunCommand(bin string, args, envVars []string) (out string, err error) {
// execute the command
o, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("%s %s failed with %s, %s\n", bin, args, err, string(o))
log.Errorf("%s %s failed with %s, %s\n", bin, args, err, string(o))
return
}
out = strings.TrimSpace(string(o))
Expand Down
58 changes: 30 additions & 28 deletions pkg/lctrld/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func InspectEvent(settings config.Schema, evt *model.Event, cmdRunner CommandRun
path, err := evts(settings, evt.ID())
log.Debugln("InspectEvent event", evt.ID(), "home:", path)
if err != nil {
log.Fatal("Inspect failed:", err)
log.Error("Inspect failed:", err)
return
}
dmBin := dmBin(settings)
Expand Down Expand Up @@ -156,8 +156,8 @@ func RereadDockerMachineInfo(settings config.Schema, evt *model.Event, dmc Docke
for i, v := range validatorAccounts {
mc, err := dmc.ReadConfig(fmt.Sprint(i))
if err != nil {
log.Fatal("Provision read machine config error:", err)
break
log.Error("Provision read machine config error:", err)
return nil, err
}
evt.State[v.Name] = mc
}
Expand All @@ -173,49 +173,49 @@ func DeployPayload(settings config.Schema, evt *model.Event, cmdRunner CommandRu
for name, state := range evt.State {
envVars, err := dockerMachineEnv(settings, evt)
if err != nil {
log.Fatalf("dockerMachineEnv() failed while generating envVars: %s", err)
break
log.Errorf("dockerMachineEnv() failed while generating envVars: %s", err)
return err
}

// docker-machine ssh mkdir -p /home/docker/nodeconfig
args := []string{"ssh", state.ID(), "mkdir", "-p", "/home/docker/nodeconfig"}
_, err = cmdRunner(dmBin, args, envVars)
if err != nil {
log.Fatalf("docker-machine %s failed with %s", args, err)
break
log.Errorf("docker-machine %s failed with %s", args, err)
return err
}

// docker-machine scp -r pathDaemon evtx-d97517a3673688070aef-0:/home/docker/nodeconfig
args = []string{"scp", "-r", evt.Accounts[name].ConfigLocation.DaemonConfigDir, fmt.Sprintf("%s:/home/docker/nodeconfig", state.ID())}
_, err = cmdRunner(dmBin, args, envVars)
if err != nil {
log.Fatalf("docker-machine %s failed with %s", args, err)
break
log.Errorf("docker-machine %s failed with %s", args, err)
return err
}

// docker-machine scp -r pathCLI evtx-d97517a3673688070aef-0:/home/docker/nodeconfig
args = []string{"scp", "-r", evt.Accounts[name].ConfigLocation.CLIConfigDir, fmt.Sprintf("%s:/home/docker/nodeconfig", state.ID())}
_, err = cmdRunner(dmBin, args, envVars)
if err != nil {
log.Fatalf("docker-machine %s failed with %s", args, err)
break
log.Errorf("docker-machine %s failed with %s", args, err)
return err
}

// docker-machine chmod -R 777 /home/docker/nodeconfig
args = []string{"ssh", state.ID(), "chmod", "-R", "777", "/home/docker/nodeconfig"}
_, err = cmdRunner(dmBin, args, envVars)
if err != nil {
log.Fatalf("docker-machine %s failed with %s", args, err)
break
log.Errorf("docker-machine %s failed with %s", args, err)
return err
}
}

log.Infof("Running docker pull %s on each provisioned machine", evt.Payload.DockerImage)
for email, state := range evt.State {
envVars, err := dockerMachineEnv(settings, evt)
if err != nil {
log.Fatalf("dockerMachineEnv() failed while generating envVars: %s", err)
break
log.Errorf("dockerMachineEnv() failed while generating envVars: %s", err)
return err
}

// Build the output of docker-machine -s /tmp/workspace/evts/evtx-d97517a3673688070aef/.docker/machine/ env evtx-d97517a3673688070aef-1
Expand All @@ -226,17 +226,17 @@ func DeployPayload(settings config.Schema, evt *model.Event, cmdRunner CommandRu
log.Debugf("Running docker %s for validator %s machine; envVars %s\n", args, email, envVars)
_, err = cmdRunner("docker", args, envVars)
if err != nil {
log.Fatalf("docker %s failed with %s", args, err)
break
log.Errorf("docker %s failed with %s", args, err)
return err
}
}

log.Infoln("Running the dockerized Cosmos daemons on the provisioned machines")
for email, state := range evt.State {
envVars, err := dockerMachineEnv(settings, evt)
if err != nil {
log.Fatalf("dockerMachineEnv() failed while generating envVars: %s", err)
break
log.Errorf("dockerMachineEnv() failed while generating envVars: %s", err)
return err
}

// Build the output of docker-machine -s /tmp/workspace/evts/evtx-d97517a3673688070aef/.docker/machine/ env evtx-d97517a3673688070aef-1
Expand All @@ -247,8 +247,8 @@ func DeployPayload(settings config.Schema, evt *model.Event, cmdRunner CommandRu
log.Debugf("Running docker %s for validator %s machine; envVars %s\n", args, email, envVars)
_, err = cmdRunner("docker", args, envVars)
if err != nil {
log.Fatalf("docker %s failed with %s", args, err)
break
log.Errorf("docker %s failed with %s", args, err)
return err
}
}

Expand All @@ -268,15 +268,15 @@ func DeployPayload(settings config.Schema, evt *model.Event, cmdRunner CommandRu
log.Debugf("Running docker-machine %s on validator %s machine; envVars %s\n", args, firstNode, envVars)
_, err = cmdRunner(dmBin, args, envVars)
if err != nil {
log.Fatal(err)
log.Error(err)
return
}

// args = []string{"ssh", evt.State[firstNode].ID(), "/home/docker/launchpayloadcli", "rest-server", "--laddr", "tcp://0.0.0.0:1317", "--node", fmt.Sprintf("tcp://%s:26657", evt.State[firstNode].Instance.IPAddress), "--unsafe-cors", "--chain-id", evt.ID(), "--home", "/home/docker/nodeconfig/cli", "&"}
// o, err = cmdRunner(dmBin, args, envVars)
// log.Infoln(o)
// if err != nil {
// log.Fatal(err)
// log.Error(err)
// return
// }

Expand All @@ -286,25 +286,27 @@ func DeployPayload(settings config.Schema, evt *model.Event, cmdRunner CommandRu
args = []string{"scp", "-r", faucetAccount.ConfigLocation.CLIConfigDir, fmt.Sprintf("%s:/home/docker/nodeconfig/faucet_account", evt.State[v[0]].ID())}
_, err = cmdRunner("docker-machine", args, envVars)
if err != nil {
log.Fatalf("docker-machine %s failed with %s", args, err)
log.Errorf("docker-machine %s failed with %s", args, err)
return
}
// docker-machine chmod -R 777 /home/docker/nodeconfig AGAIN - what a mess!
args = []string{"ssh", evt.State[v[0]].ID(), "chmod", "-R", "777", "/home/docker/nodeconfig"}
_, err = cmdRunner(dmBin, args, envVars)
if err != nil {
log.Fatalf("docker-machine %s failed with %s", args, err)
log.Errorf("docker-machine %s failed with %s", args, err)
return
}

evtDir, err := evts(settings, evt.ID())
if err != nil {
log.Fatal(err)
log.Error(err)
return
}

args = []string{"scp", "-r", filepath.Join(evtDir, "faucetconfig.yml"), fmt.Sprintf("%s:/home/docker/nodeconfig/", evt.State[v[0]].ID())}
_, err = cmdRunner("docker-machine", args, envVars)
if err != nil {
log.Fatalf("docker-machine %s failed with %s", args, err)
log.Errorf("docker-machine %s failed with %s", args, err)
return
}

Expand All @@ -315,7 +317,7 @@ func DeployPayload(settings config.Schema, evt *model.Event, cmdRunner CommandRu
log.Debugf("Running docker %s on %s; envVars %s\n", args, firstValidator.ID(), envVars)
_, err = cmdRunner("docker", args, envVars)
if err != nil {
log.Fatalf("docker %s failed with %s", args, err)
log.Errorf("docker %s failed with %s", args, err)
return
}
return
Expand Down
26 changes: 13 additions & 13 deletions pkg/lctrld/lctrld.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func SetupWorkspace(settings config.Schema) (err error) {
log.Debugln("Folder ", settings.Workspace, "does not exists, creating")
err = os.MkdirAll(settings.Workspace, 0700)
if err != nil {
log.Fatal("SetupWorkspace: ", err)
log.Error("SetupWorkspace: ", err)
return
}
}
Expand All @@ -29,7 +29,7 @@ func SetupWorkspace(settings config.Schema) (err error) {
log.Debugln("Folder", dir, "does not exists, creating")
err = os.MkdirAll(dir, 0700)
if err != nil {
log.Fatal("SetupWorkspace: ", err)
log.Error("SetupWorkspace: ", err)
return
}
}
Expand All @@ -53,20 +53,20 @@ func InstallDockerMachine(settings config.Schema) (err error) {
// generate a temp dir
td, err := tmp(settings)
if err != nil {
log.Fatal("InstallDockerMachine: ", err)
log.Error("InstallDockerMachine: ", err)
return
}
log.Debug("InstallDockerMachine: file will be download in ", td)
dwnFile, err := utils.DownloadFile(td, downloadURL)
if err != nil {
log.Fatal("InstallDockerMachine: ", err)
log.Error("InstallDockerMachine: ", err)
return
}
dwnFilePath := _path(td, dwnFile)
log.Debug("InstallDockerMachine: download complete ", dwnFilePath)
ct, err := utils.DetectContentType(dwnFilePath)
if err != nil {
log.Fatal("InstallDockerMachine: ", err)
log.Error("InstallDockerMachine: ", err)
return
}
log.Debug("InstallDockerMachine: downloaded file ", dwnFilePath, " has content-type ", ct)
Expand All @@ -75,41 +75,41 @@ func InstallDockerMachine(settings config.Schema) (err error) {
log.Debugln("InstallDockerMachine: downloaded file is binary, moving to the destination path")
err = os.Rename(dwnFilePath, targetPath)
if err != nil {
log.Fatal("InstallDockerMachine: ", err)
log.Error("InstallDockerMachine: ", err)
return
}
case "application/zip":
err = fmt.Errorf("InstallDockerMachine: unsupported file type %s", ct)
case "application/x-gzip":
err = utils.ExtractGzip(dwnFilePath, td)
if err != nil {
log.Fatal("InstallDockerMachine: ", err)
log.Error("InstallDockerMachine: ", err)
return
}
err = utils.SearchAndMove(td, file, targetPath)
if err != nil {
log.Fatal("InstallDockerMachine: ", err)
log.Error("InstallDockerMachine: ", err)
return
}
default:
err = fmt.Errorf("InstallDockerMachine: unsupported file type %s", ct)
}
if err != nil {
log.Fatal("InstallDockerMachine: ", err)
log.Error("InstallDockerMachine: ", err)
return
}
// make it executable
os.Chmod(targetPath, 0700)
if err != nil {
log.Fatal("InstallDockerMachine: ", err)
log.Error("InstallDockerMachine: ", err)
}
return
}

// check if the system has been setup already
err = dine(settings.DockerMachine.Binary, settings.DockerMachine.BinaryURL)
if err != nil {
log.Fatal("InstallDockerMachine: ", err)
log.Error("InstallDockerMachine: ", err)
return
}
for dName, driver := range settings.DockerMachine.Drivers {
Expand All @@ -120,7 +120,7 @@ func InstallDockerMachine(settings config.Schema) (err error) {
}
err = dine(driver.Binary, driver.BinaryURL)
if err != nil {
log.Fatal("InstallDockerMachine: ", err)
log.Error("InstallDockerMachine: ", err)
return
}
}
Expand Down Expand Up @@ -161,7 +161,7 @@ func ListEvents(settings config.Schema) (events []model.Event, err error) {
log.Debugln("Event found", info.Name())
evt, err := model.LoadEvent(subPath)
if err != nil {
log.Fatal("ListEvents failed:", err)
log.Error("ListEvents failed:", err)
return err
}
events = append(events, *evt)
Expand Down
Loading

0 comments on commit bc6a3f6

Please sign in to comment.