Skip to content

Commit

Permalink
fix: added error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
yyewolf committed Jun 17, 2024
1 parent 219f4ad commit c5ab1eb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
40 changes: 27 additions & 13 deletions internal/docker/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Container struct {
ID string
Labels labels.GoCDLabels
Inspect types.ContainerJSON
Error error
}

var mutex sync.Mutex
Expand Down Expand Up @@ -64,26 +65,14 @@ func UpdateContainers(token string) error {
listCopy := make([]*Container, len(list))
copy(listCopy, list)

// Prepare discord's message
message := fmt.Sprintf("Updating %d container(s):\n", len(list))
for _, c := range list {
lbl := labels.MapToGoCDLabels(c.Inspect.Config.Labels)
if lbl.Repo == "" {
message += fmt.Sprintf("- **%s**\n", c.Inspect.Name)
} else {
message += fmt.Sprintf("- **[%s](%s)**\n", c.Inspect.Name, lbl.Repo)
}
}

discord.SendMessage(message)

for _, c := range list {
logrus.Infof("Updating container %s", c.Inspect.Name)

// Pull image, delete container, create new container
out, err := cli.ImagePull(context.Background(), c.Inspect.Config.Image, types.ImagePullOptions{})
if err != nil {
logrus.Error("Failed to update container at image pull: ", err)
c.Error = err
}

if out == nil {
Expand All @@ -104,6 +93,7 @@ func UpdateContainers(token string) error {
})
if err != nil {
logrus.Error("Failed to update container at container stop: ", err)
c.Error = err
}

logrus.Debug("Removing container")
Expand All @@ -113,6 +103,7 @@ func UpdateContainers(token string) error {
if err != nil {
logrus.Error("Failed to update container at container remove: ", err)
logrus.Error("Attempting to start container anyway")
c.Error = err
}
// Get networking config from old container
nw := c.Inspect.NetworkSettings.Networks
Expand All @@ -129,6 +120,7 @@ func UpdateContainers(token string) error {
resp, err := cli.ContainerCreate(context.Background(), c.Inspect.Config, c.Inspect.HostConfig, nil, nil, c.Inspect.Name)
if err != nil {
logrus.Error("Failed to update container at container create: ", err)
c.Error = err
continue
}

Expand All @@ -138,13 +130,15 @@ func UpdateContainers(token string) error {
err = cli.NetworkConnect(context.Background(), k, resp.ID, v)
if err != nil {
logrus.Error("Failed to update container at network connect: ", err)
c.Error = err
}
}

logrus.Debug("Starting new container")
err = cli.ContainerStart(context.Background(), resp.ID, types.ContainerStartOptions{})
if err != nil {
logrus.Error("Failed to update container at container start: ", err)
c.Error = err
continue
}

Expand All @@ -159,6 +153,26 @@ func UpdateContainers(token string) error {
logrus.Debug("Finished updating container")
}
containers[token] = listCopy

// Prepare discord's message
message := fmt.Sprintf("Updated %d container(s):\n", len(list))
for _, c := range list {
lbl := labels.MapToGoCDLabels(c.Inspect.Config.Labels)
if lbl.Repo == "" {
message += fmt.Sprintf("- **%s**", c.Inspect.Name)
} else {
message += fmt.Sprintf("- **[%s](%s)**", c.Inspect.Name, lbl.Repo)
}

// Error report
if c.Error != nil {
message += fmt.Sprintf(" (%v)\n", c.Error)
} else {
message += "\n"
}
}

discord.SendMessage(message)
}()

return nil
Expand Down
16 changes: 15 additions & 1 deletion web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
)

func Routes(e *echo.Echo) {

e.GET("/", func(c echo.Context) error {
return c.String(418, "I'm a teapot")
})
Expand All @@ -26,4 +25,19 @@ func Routes(e *echo.Echo) {
"message": "ok",
})
})

e.POST("/containers", func(c echo.Context) error {
token := c.FormValue("token")

err := docker.UpdateContainers(token)
if err != nil {
return c.JSON(403, map[string]interface{}{
"message": "forbidden",
})
}

return c.JSON(200, map[string]interface{}{
"message": "ok",
})
})
}

0 comments on commit c5ab1eb

Please sign in to comment.