Skip to content

Commit

Permalink
增加日志查看,优化
Browse files Browse the repository at this point in the history
  • Loading branch information
name committed Jun 2, 2021
1 parent 096b1e2 commit 1771b40
Show file tree
Hide file tree
Showing 11 changed files with 288 additions and 30 deletions.
11 changes: 2 additions & 9 deletions App.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,25 @@ func PrintParamInfo() {

fmt.Println("Parameter description:")
fmt.Println("\tDockerServer\trequired\thttp server address,like:http://127.0.0.1:8080/dockerApi")
fmt.Println("\tDockerWsServer\trequired\twebsocket server address,like:http://127.0.0.1:8080/dockerApi/ws")
fmt.Println("\tToken\t\tOptional\tThe http and websocket header authorization for dockerserver auth")
}

func main() {
PrintParamInfo()

flag.StringVar(&conf.DockerServer, "DockerServer", "http://127.0.0.1:8080/dockerApi", "DockerServer服务地址,如:http://127.0.0.1:8080/dockerApi")
flag.StringVar(&conf.DockerWsServer, "DockerWsServer", "", "DockerWsServer服务地址,如:http://127.0.0.1:8068/dockerApi/ws")
flag.StringVar(&conf.Token, "Token", "", "The http and websocket header authorization for dockerserver auth")
flag.Parse()

log.Println("Start docker agent, AppId:", conf.AppId)
log.Println("conf.DockerServer:", conf.DockerServer)
log.Println("conf.DockerWsServer:", conf.DockerWsServer)

if conf.DockerServer == "" {
log.Panic("DockerServer must be set. like: -DockerServer http://127.0.0.1:8080/dockerApi")
}

if conf.DockerWsServer != "" {
log.Println("Start connect to DockerWsServer :", conf.DockerWsServer)
agent.StartWs()
} else {
log.Println("DockerWsServer not set, that websocket client not start, if you want websocket, you can add param,like : -DockerWsServer http://127.0.0.1:8080/dockerApi/ws ")
}
log.Println("Start connect to DockerWsServer :", conf.GetDockerWsUrl())
agent.StartWs()

for true {
go work()
Expand Down
11 changes: 5 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@ docker agent ,which is an agent post docker info、container list、container st

### Quick start:
```
docker run -it --rm -v /var/run/docker.sock:/var/run/docker.sock -e DockerServer="http://192.168.1.200:8080/dockerApi" -e DockerWsServer="ws://192.168.1.200:8080/dockerApi/ws/" -e Token="12345678" xiaojun207/docker-agent:latest
docker run -it --rm -v /var/run/docker.sock:/var/run/docker.sock -e DockerServer="http://192.168.1.200:8080/dockerApi" -e Token="12345678" xiaojun207/docker-agent:latest
```

or
```
/app/App -DockerServer $DockerServer -DockerWsServer $DockerWsServer -Token $Token
/app/App -DockerServer $DockerServer -Token $Token
```


### Env:
- DockerServer: The http server accept the agent post docker info;
- DockerWsServer: The websocket server accept the agent post docker info, and push the task to agent, like create and run a new container;
- Token: The http and websocket header authorization for dockerserver auth;

### DockerServer, application/json, must support api:
- POST /reg, recive agent post docker info data;
- POST /containers, recive agent post all container list data;
- POST {DockerServer}/reg, recive agent post docker info data;
- POST {DockerServer}/containers, recive agent post all container list data;
- WS {DockerServer}/ws, websocket path, The websocket server accept the agent post docker info, and push the task to agent, like create and run a new container;

### DockerWsServer, the web socket server:
the message like this:
```
{
Expand Down
49 changes: 49 additions & 0 deletions service/agent/Container.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package agent

import (
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"github.com/docker/docker/api/types"
Expand Down Expand Up @@ -47,6 +48,9 @@ func ContainerCreate(imageName, containerName string, ports map[string]string, e
RestartPolicy: container.RestartPolicy{
Name: "always",
},
//Resources: container.Resources{
// Memory: 1024 * 1024 * 512, // 512M
//},
Binds: volumes, // []string{"/tmp:/tmp"}
}

Expand Down Expand Up @@ -247,6 +251,51 @@ func ContainerLogs(containerId string, tail, since string) (string, error) {
return res, nil
}

func ContainerLogFollow(containerId string, out func(timestamps int64, line string) bool) {
i, err := cli.ContainerLogs(ctx, containerId, types.ContainerLogsOptions{
ShowStderr: true,
ShowStdout: true,
Timestamps: true,
Follow: true,
Tail: "40",
})
if err != nil {
log.Fatal(err)
}
header := make([]byte, 8)
Follow := true

format_layout := "2006-01-02T15:04:05.000000000Z"
timeLen := 30 // len(format_layout)

for Follow {
_, err := i.Read(header)
if err != nil {
log.Fatal(err)
}
//var w io.Writer
//switch header[0] {
//case 1:
// w = os.Stdout
//default:
// w = os.Stderr
//}
count := binary.BigEndian.Uint32(header[4:])
dat := make([]byte, count)
_, err = i.Read(dat)
//fmt.Fprint(w, string(dat))
//log.Print(string(dat))

line := string(dat)

tmp := SubString(line, 0, timeLen)
line = SubString(line, timeLen+1, len(line)-timeLen)
t2, _ := time.Parse(format_layout, tmp)
timestamps := t2.UnixNano() / 1e6 // 毫秒级时间戳
Follow = out(timestamps, line)
}
}

func SubString(str string, begin, length int) (substr string) {
// 将字符串的转换成[]rune
rs := []rune(str)
Expand Down
55 changes: 49 additions & 6 deletions service/agent/Container_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,61 @@
package agent

import (
"context"
"encoding/binary"
"fmt"
"github.com/docker/docker/api/types"
"log"
"strconv"
"testing"
"time"
)

func TestContainerLogs(t *testing.T) {
log.Println(time.Now().String())
containerId := "302c6c35f81699fd8d3c02e903cea24b4979b51080abd8dcbbb06bf3de45016b"
logs, err := ContainerLogs(containerId, "100", "2021-05-30T04:07:11Z")
log.Println(err)
fmt.Println(logs)
containerId := "drone-runner"
//logs, err := ContainerLogs(containerId, "100", "2021-05-30T04:07:11Z")
//log.Println(err)
//fmt.Println(logs)

i, err := cli.ContainerLogs(context.Background(), containerId, types.ContainerLogsOptions{
ShowStderr: true,
ShowStdout: true,
Timestamps: true,
Follow: true,
Tail: "40",
Details: true,
})
if err != nil {
log.Fatal(err)
}
header := make([]byte, 8)
Follow := true
for Follow {
_, err := i.Read(header)
if err != nil {
log.Fatal(err)
}
//var w io.Writer
//switch header[0] {
//case 1:
// w = os.Stdout
//default:
// w = os.Stderr
//}
count := binary.BigEndian.Uint32(header[4:])
dat := make([]byte, count)
_, err = i.Read(dat)
//fmt.Fprint(w, string(dat))
fmt.Print(string(dat))
}
}

func TestContainerLogs2(t *testing.T) {
containerId := "drone-runner"
ContainerLogFollow(containerId, func(timestamps int64, line string) bool {
fmt.Println("timestamps:" + strconv.FormatInt(timestamps, 10))
fmt.Println("line:" + line)
return true
})
}

func TestContainerCreate(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions service/agent/DockerAgentWs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type DockerAgentWs struct {
var wsConn *utils.WsConn

func StartWs() {
endpoint := conf.DockerWsServer
endpoint := conf.GetDockerWsUrl()
wsConn = utils.NewWsBuilder().
WsUrl(endpoint).
AutoReconnect().
Expand Down Expand Up @@ -45,13 +45,13 @@ func exitHandler(c *utils.WsConn) {
}
}

func SendWsMsg(ch string, data interface{}) {
func SendWsMsg(ch string, data interface{}) error {
msg := map[string]interface{}{
"ch": ch,
"ts": time.Now().UnixNano() / 1e6,
"d": data,
}
wsConn.SendJsonMessage(msg)
return wsConn.SendJsonMessage(msg)
}

func wsMsgHandle(msg []byte) error {
Expand Down
18 changes: 17 additions & 1 deletion service/agent/DockerAgent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,24 @@ import (
"time"
)

func TestTimeName(t *testing.T) {
s := "2021-06-02T10:51:12.725599800Z"
fmt.Println(time.Now().UnixNano() / 1e6)
fmt.Println(time.Now().Format("Mon Jan 2 15:04:05 -0700 MST 2006"))
fmt.Println(time.Now().Format("Mon Jan 2 15:04:05 -0700 MST 2006"))
t1, err := time.Parse("Mon Jan 2 15:04:05 -0700 MST 2006", "Mon Jan 2 15:04:05 -0700 MST 2006")
fmt.Println("t1:", t1)
t2, err := time.Parse("2006-01-02T15:04:05.000000000Z", s)
fmt.Println("length:", len("2006-01-02T15:04:05.000000000Z"))
if err != nil {
log.Println(err)
return
}
fmt.Println(t2)
fmt.Println(s)
}

func TestFindContainer(t *testing.T) {
log.Println(time.Now().UnixNano() / 1e6)

ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
Expand Down
38 changes: 38 additions & 0 deletions service/agent/WsMsgHandle.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package agent

import (
"docker-agent/service/conf"
"docker-agent/utils"
"log"
)
Expand Down Expand Up @@ -69,6 +70,43 @@ func MsgHandle(ch string, data map[string]interface{}) (error, map[string]interf
stats, err := ContainerStats(containerId)
log.Println("ws: " + ch + " containerId:" + containerId)
return err, map[string]interface{}{"containerId": containerId, "stats": stats}
case "docker.container.logs":
containerId := data["containerId"].(string)
tail := data["tail"].(string)
since := data["since"].(string)
logs, err := ContainerLogs(containerId, tail, since)
log.Println("ws: " + ch + " containerId:" + containerId)
return err, map[string]interface{}{"containerId": containerId, "logs": logs}
case "docker.container.log.follow.close":
containerId := data["containerId"].(string)
conf.LogsFollow.Delete(containerId)
return nil, map[string]interface{}{"containerId": containerId}
case "docker.container.log.follow":
containerId := data["containerId"].(string)
isFollow, _ := conf.LogsFollow.LoadBool(containerId)
if isFollow {
log.Println("ws: " + ch + " containerId:" + containerId + ", log is follow")
return nil, map[string]interface{}{"containerId": containerId}
}

conf.LogsFollow.Store(containerId, true)

go ContainerLogFollow(containerId, func(timestamps int64, line string) bool {
d := map[string]interface{}{
"cId": containerId,
"ts": timestamps,
"line": line,
}
err := SendWsMsg("docker.container.log.line", d)
if err != nil {
conf.LogsFollow.Delete(containerId)
return false
}
follow, _ := conf.LogsFollow.LoadBool(containerId)
return follow
})
log.Println("ws: " + ch + " containerId:" + containerId)
return nil, map[string]interface{}{"containerId": containerId}
default:
log.Println("unknown message "+ch, data)
return nil, nil
Expand Down
9 changes: 8 additions & 1 deletion service/conf/Agent.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package conf

import "github.com/docker/docker/api/types"
import (
"docker-agent/service/model"
"github.com/docker/docker/api/types"
)

var DockerInfo types.Info

var (
LogsFollow model.SyncMap
)
17 changes: 14 additions & 3 deletions service/conf/Server.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
package conf

import "github.com/go-basic/uuid"
import (
"github.com/go-basic/uuid"
"strings"
)

var DockerServer = "" //eg.: http://localhost:8080/docker/
var DockerWsServer = "" //eg.: ws://localhost:8080/docker/ws
var DockerServer = "" //eg.: http://localhost:8080/docker/
var Token = "12345678"
var AppId = uuid.New()

func GetDockerWsUrl() string {
//var DockerWsServer = "" //eg.: ws://localhost:8080/docker/ws
if strings.HasPrefix(DockerServer, "http://") {
return "ws://" + strings.TrimLeft(DockerServer, "http://") + "/ws"
} else {
return "wss://" + strings.TrimLeft(DockerServer, "https://") + "/ws"
}
}
Loading

0 comments on commit 1771b40

Please sign in to comment.