Skip to content

Commit

Permalink
✨ Added features logs
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMarble committed Jul 25, 2020
1 parent 68b4e67 commit 5db3463
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 6 deletions.
27 changes: 27 additions & 0 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"io"
"regexp"
"time"

Expand Down Expand Up @@ -70,6 +71,32 @@ func (d *Docker) inspect(containerID string) (*types.ContainerJSON, error) {
return &container, nil
}

func (d *Docker) logs(containerID string, tail string) ([]string, error) {
var (
bytes []byte = make([]byte, 3000) // Telegram message length limit
logs []string = nil
err error = nil
)
if tail != "all" && !isNumber(tail) {
tail = "10"
}
logsReader, err := d.cli.ContainerLogs(d.ctx, containerID, types.ContainerLogsOptions{Tail: tail, ShowStderr: true, ShowStdout: true})
defer logsReader.Close()

if err != nil {
log.Fatal().Str("module", "docker").Str("containerID", containerID).Err(err).Msg("error getting container logs")
return nil, err
}
for {
numBytes, err := logsReader.Read(bytes)
logs = append(logs, string(bytes[:numBytes]))
if err == io.EOF {
break
}
}
return logs, nil
}

func (d *Docker) isValidID(containerID string) bool {
re := regexp.MustCompile(`(?m)^[A-Fa-f0-9]{10,12}$`)
return re.MatchString(containerID)
Expand Down
50 changes: 47 additions & 3 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,36 @@ func (t *Telegram) handleStacks(m *tb.Message) {
t.send(m.Chat, strings.Join(resultMsg, "\n\n"))
}

func (t *Telegram) handleLogs(m *tb.Message) {
if !t.isSuperAdmin(m.Sender) {
return
}
payload := strings.Split(m.Payload, " ")
containerID := payload[0]
if containerID == "" || !docker.isValidID(containerID) {
t.reply(m, "Choose a container", makeContainerMenu(t, types.ContainerListOptions{All: true}, "logs"))
} else {
var tail string = "10"
if len(payload) > 1 {
tail = payload[1]
}
logs, err := docker.logs(containerID, tail)

if err != nil {
t.reply(m, err.Error())
return
}
for index, chunk := range logs {
if index == 0 {
t.reply(m, fmt.Sprintf("<code>%v</code>", chunk), tb.ModeHTML)
} else {
t.send(m.Chat, fmt.Sprintf("<code>%v</code>", chunk), tb.ModeHTML)
}
time.Sleep(100 * time.Millisecond)
}
}
}

func (t *Telegram) handleCallback(c *tb.Callback) {
parts := strings.Split(c.Data, ":")
instruction := parts[0]
Expand All @@ -139,11 +169,11 @@ func (t *Telegram) handleCallback(c *tb.Callback) {
case "inspect":
container, err := docker.inspect(payload)
if err != nil {
callbackResponse(t, c, err, payload, fmt.Sprintf("<code>%v</code>", payload))
callbackResponse(t, c, err, payload, "")
} else {
response, err := formatStruct(container)
if err != nil {
callbackResponse(t, c, err, payload, fmt.Sprintf("<code>%v</code>", response))
callbackResponse(t, c, err, payload, "")
return
}
for index, chunk := range chunkString(response, 300) {
Expand All @@ -155,6 +185,20 @@ func (t *Telegram) handleCallback(c *tb.Callback) {
time.Sleep(250 * time.Millisecond)
}
}

case "logs":
logs, err := docker.logs(payload, "10")
if err != nil {
callbackResponse(t, c, err, payload, "")
return
}
for index, chunk := range logs {
if index == 0 {
callbackResponse(t, c, err, payload, fmt.Sprintf("<code>%v</code>", chunk))
}
if index != 0 && chunk != "" {
t.send(c.Message.Chat, fmt.Sprintf("<code>%v</code>", chunk), tb.ModeHTML)
}
time.Sleep(100 * time.Millisecond)
}
}
}
11 changes: 8 additions & 3 deletions telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,30 @@ func (t *Telegram) registerHandlers() {
Handler: t.handleStop,
Cmd: "stop",
Aliases: []string{"down"},
Description: "Stop a running container",
Description: "Stop a running container. <ContainerID>",
},
{
Handler: t.handleStartContainer,
Cmd: "run",
Description: "Start a stopped container",
Description: "Start a stopped container. <ContainerID>",
},
{
Handler: t.handleInspect,
Cmd: "inspect",
Aliases: []string{"describe"},
Description: "Start a stopped container",
Description: "Inspect a container. <ContainerID>",
},
{
Handler: t.handleStacks,
Cmd: "stacks",
Aliases: []string{"lss", "liststacks"},
Description: "Lists all compose stacks",
},
{
Handler: t.handleLogs,
Cmd: "logs",
Description: "Shows container logs. <ContainerID> <tail>",
},
}
for _, command := range botCommands {
botCommandList = append(botCommandList, tb.Command{
Expand Down
5 changes: 5 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ func parseInt64(s string) (int64, error) {
return int64(i), nil
}

func isNumber(s string) bool {
_, err := strconv.Atoi(s)
return err == nil
}

func parseList(options types.ContainerListOptions) []string {
containers := docker.list(options)
if len(containers) == 0 {
Expand Down

0 comments on commit 5db3463

Please sign in to comment.