Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #83 from remyLemeunier/rework-error-handling
Browse files Browse the repository at this point in the history
Rework error handling
  • Loading branch information
remyLemeunier authored Dec 19, 2017
2 parents 970bbf4 + 98ba9e8 commit 2b1ef9b
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 46 deletions.
6 changes: 3 additions & 3 deletions commands/command_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func init() {

type CckCommand interface {
fill(context *context.Context, service string, env string)
execute()
execute() error
}

func fill(cck CckCommand, config *utils.Config, service string, env string) error {
Expand Down Expand Up @@ -69,6 +69,6 @@ func fill(cck CckCommand, config *utils.Config, service string, env string) erro
return nil
}

func execute(cck CckCommand) {
cck.execute()
func execute(cck CckCommand) error {
return cck.execute()
}
2 changes: 0 additions & 2 deletions commands/contact_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ func Execute() {
cfg, err := utils.LoadConfig()
if err != nil {
log.Fatalln(fmt.Sprintf("Failed load config: %q", err))
return
}

services, err := cfg.DiscoverServices()
if err != nil {
log.Fatalln(fmt.Sprintf("Failed to find services: %q", err))
return
}

rootCmd := &cobra.Command{
Expand Down
36 changes: 14 additions & 22 deletions commands/deploy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -40,7 +41,7 @@ type Deploy struct {
Writer io.Writer
}

func (d *Deploy) execute() {
func (d *Deploy) execute() error {
timer := prometheus.NewTimer(prometheus.ObserverFunc(
deployDuration.With(prometheus.Labels{"env": d.Env, "project": d.Service}).Set))
d.Context.Metrics.Add(deployDuration)
Expand All @@ -62,26 +63,23 @@ func (d *Deploy) execute() {
}

if err := utils.CheckIfIsLaunchedInAScreen(); err != nil && d.Context.ScreenMandatory == true {
log.Errorln(fmt.Sprintf("Screen error raised: %q", err))
return
return errors.New(fmt.Sprintf("Screen error raised: %q", err))
}

// The lock system is not mandatory
if d.Context.LockSystem != nil {
log.Println(fmt.Sprintf("Trying to lock the lock command for service %q and env %q", d.Service, d.Env))
canLock, err := d.Context.LockSystem.Lock(d.Env, d.Service)
if err != nil {
log.Fatalln(fmt.Sprintf("Failed to lock, error raised: %q", err))
return
return errors.New(fmt.Sprintf("Failed to lock, error raised: %q", err))
}

if canLock == false {
log.Fatalln("Another command is currently running")
return
return errors.New("Another command is currently running")
}

defer func(d *Deploy) {
d.Context.LockSystem.Unlock(d.Env, d.Service)
err = d.Context.LockSystem.Unlock(d.Env, d.Service)
if err != nil {
log.Errorln(fmt.Sprintf("Failed to unlock, error raised: %q", err))
}
Expand All @@ -91,26 +89,22 @@ func (d *Deploy) execute() {
// If the branch is null it will use the default one.
sha1ToDeploy, err := d.Context.Vcs.RetrieveSha1ForProject(branch)
if err != nil {
log.Fatalln(fmt.Sprintf("Failed to retrieve source changes for %q : %q", d.Service, err))
return
return errors.New(fmt.Sprintf("Failed to retrieve source changes for %q : %q", d.Service, err))
}

podVersion, err := d.Context.Binaries.RetrievePodVersion(sha1ToDeploy)
if err != nil {
log.Fatalln(fmt.Sprintf("Failed to retrieve pod version: %q", err))
return
return errors.New(fmt.Sprintf("Failed to retrieve pod version: %q", err))
}

deployedVersions, err := d.Context.Deployer.ListVcsVersions(d.Env)
if err != nil {
log.Fatalln(fmt.Sprintf("Failed to retrieve DEPLOYED version from service(%q) in env %q: %q", d.Service, d.Env, err))
return
return errors.New(fmt.Sprintf("Failed to retrieve DEPLOYED version from service(%q) in env %q: %q", d.Service, d.Env, err))
}

if podVersion == "" {
log.Fatalln(fmt.Sprintf("We have not found the pod version with the the sha1 %q \n"+
return errors.New(fmt.Sprintf("We have not found the pod version with the the sha1 %q \n"+
"The pod has not been created.", sha1ToDeploy))
return
}

needToDeploy := force
Expand All @@ -125,16 +119,14 @@ func (d *Deploy) execute() {
}

if needToDeploy == false {
log.Fatalln(fmt.Sprintf("Version %q is already deployed.", sha1ToDeploy))
return
return errors.New(fmt.Sprintf("Version %q is already deployed.", sha1ToDeploy))
}

log.Println(fmt.Sprintf("Going to deploy pod version %q \n", podVersion))
for _, hook := range d.Context.Hooks {
err = hook.PreDeployment(userName, d.Env, d.Service, podVersion)
if hook.StopOnError() == true && err != nil {
log.Fatalln(fmt.Sprintf("Predeployment failed: %q", err))
return
return errors.New(fmt.Sprintf("Predeployment failed: %q", err))
} else if err != nil {
log.Debugln(fmt.Sprintf("Predeployment failed: %q", err))
}
Expand All @@ -150,8 +142,7 @@ func (d *Deploy) execute() {
}()
err = d.Context.Deployer.Deploy(d.Env, podVersion, stateStream)
if err != nil {
log.Fatalf("Deployment failed: %q", err)
return
return errors.New(fmt.Sprintf("Deployment failed: %q", err))
}

for _, hook := range d.Context.Hooks {
Expand All @@ -168,6 +159,7 @@ func (d *Deploy) execute() {
}

log.Println(d.Writer, "Deployment has successfully ended")
return nil
}

func (d *Deploy) fill(context *context.Context, service string, env string) {
Expand Down
20 changes: 9 additions & 11 deletions commands/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"os"

"errors"

"github.com/olekukonko/tablewriter"
"github.com/remyLemeunier/contactkey/context"
log "github.com/sirupsen/logrus"
Expand All @@ -26,27 +28,23 @@ type Diff struct {
TableWriter *tablewriter.Table
}

func (d Diff) execute() {
func (d Diff) execute() error {
// If the branch is null it will use the default one.
sha1, err := d.Context.Vcs.RetrieveSha1ForProject(branch)
if err != nil {
log.Fatal(fmt.Sprintf("Failed to retrieve sha1: %q \n", err))
return
return errors.New(fmt.Sprintf("Failed to retrieve sha1: %q \n", err))
}
if sha1 == "" {
log.Fatal(fmt.Sprintf("No sha1 found for service %q \n", d.Service))
return
return errors.New(fmt.Sprintf("No sha1 found for service %q \n", d.Service))
}

versions, err := d.Context.Deployer.ListVcsVersions(d.Env)
if err != nil {
log.Fatal(fmt.Sprintf("Failed to list versions with error %q \n", err))
return
return errors.New(fmt.Sprintf("Failed to list versions with error %q \n", err))
}

if len(versions) == 0 {
log.Fatal(fmt.Sprintf("No service (%q) versions found for the Env: %q \n", d.Service, d.Env))
return
return errors.New(fmt.Sprintf("No service (%q) versions found for the Env: %q \n", d.Service, d.Env))
}

// Retrieve only unique versions
Expand All @@ -62,8 +60,7 @@ func (d Diff) execute() {
for _, uniqueVersion := range uniqueVersions {
changes, err := d.Context.Vcs.Diff(uniqueVersion, sha1)
if err != nil {
log.Fatal(fmt.Sprintf("Failed to retrieve sha1: %q \n", err))
return
return errors.New(fmt.Sprintf("Failed to retrieve sha1: %q \n", err))
}

log.Println(fmt.Sprintf("Diff between %q(deployed) and %q(branch) \n", uniqueVersion, sha1))
Expand All @@ -73,6 +70,7 @@ func (d Diff) execute() {
}
d.TableWriter.Render()
}
return nil
}

func (d *Diff) fill(context *context.Context, service string, env string) {
Expand Down
3 changes: 1 addition & 2 deletions commands/env_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ func addServiceNameToCommand(cmd *cobra.Command, cfg *utils.Config, services []s
if err != nil {
return err
}
execute(cckCommand)

return nil
return execute(cckCommand)
},
}
cmd.AddCommand(serviceCmd)
Expand Down
9 changes: 4 additions & 5 deletions commands/list.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package commands

import (
"errors"
"fmt"
"io"
"os"

"github.com/olekukonko/tablewriter"
"github.com/remyLemeunier/contactkey/context"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -24,19 +24,18 @@ type List struct {
Writer io.Writer
}

func (l List) execute() {
func (l List) execute() error {
instances, err := l.Context.Deployer.ListInstances(l.Env)
if err != nil {
log.Fatalln(fmt.Sprintf("Failed to list versions : %q", err))
return
return errors.New(fmt.Sprintf("Failed to list versions : %q", err))
}

l.TableWriter.SetHeader([]string{"instance", "state", "version"})
for _, instance := range instances {
l.TableWriter.Append([]string{instance.Name, instance.State, instance.Version})
}
l.TableWriter.Render()

return nil
}

func (l *List) fill(context *context.Context, service string, env string) {
Expand Down
3 changes: 2 additions & 1 deletion commands/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ type Rollback struct {
Context *context.Context
}

func (r *Rollback) execute() {
func (r *Rollback) execute() error {
return nil
}

func (r *Rollback) fill(context *context.Context, service string, env string) {
Expand Down

0 comments on commit 2b1ef9b

Please sign in to comment.