This repository has been archived by the owner on Apr 17, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from vidar-team/dev [CI SKIP]
v0.6.2
Showing
14 changed files
with
412 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package docker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package docker | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/client" | ||
"io" | ||
) | ||
|
||
// FetchImage pull the image from the given registry. | ||
func FetchImage(registry string, repo string, name string, tag string) error { | ||
dockerCli, err := client.NewClientWithOpts(client.WithAPIVersionNegotiation()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
events, err := dockerCli.ImagePull( | ||
context.Background(), | ||
fmt.Sprintf("%s/%s/%s:%s", registry, repo, name, tag), | ||
types.ImagePullOptions{}, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
d := json.NewDecoder(events) | ||
|
||
type Event struct { | ||
Status string `json:"status"` | ||
Error string `json:"error"` | ||
Progress string `json:"progress"` | ||
ProgressDetail struct { | ||
Current int `json:"current"` | ||
Total int `json:"total"` | ||
} `json:"progressDetail"` | ||
} | ||
|
||
var event *Event | ||
for { | ||
if err := d.Decode(&event); err != nil { | ||
if err == io.EOF { | ||
break | ||
} | ||
panic(err) | ||
} | ||
fmt.Printf("EVENT: %+v\n", event) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package livelog | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"sync" | ||
) | ||
|
||
type streamer struct { | ||
sync.Mutex | ||
|
||
streams map[int64]*stream | ||
} | ||
|
||
var errStreamNotFound = errors.New("stream: not found") | ||
|
||
// New returns a new in-memory log streamer. | ||
func New() *streamer { | ||
return &streamer{ | ||
streams: make(map[int64]*stream), | ||
} | ||
} | ||
|
||
// Create adds a new log stream. | ||
func (s *streamer) Create(id int64) error { | ||
s.Lock() | ||
s.streams[id] = newStream() | ||
s.Unlock() | ||
return nil | ||
} | ||
|
||
// Delete removes a log by id. | ||
func (s *streamer) Delete(id int64) error { | ||
s.Lock() | ||
stream, ok := s.streams[id] | ||
if ok { | ||
delete(s.streams, id) | ||
} | ||
s.Unlock() | ||
if !ok { | ||
return errStreamNotFound | ||
} | ||
return stream.close() | ||
} | ||
|
||
// Write adds a new line into stream. | ||
func (s *streamer) Write(id int64, line *Line) error { | ||
s.Lock() | ||
stream, ok := s.streams[id] | ||
s.Unlock() | ||
if !ok { | ||
return errStreamNotFound | ||
} | ||
return stream.write(line) | ||
} | ||
|
||
// Tail returns the end signal. | ||
func (s *streamer) Tail(ctx context.Context, id int64) (<-chan *Line, <-chan error) { | ||
s.Lock() | ||
stream, ok := s.streams[id] | ||
s.Unlock() | ||
if !ok { | ||
return nil, nil | ||
} | ||
return stream.subscribe(ctx) | ||
} | ||
|
||
// Info returns the count of subscribers in each stream. | ||
func (s *streamer) Info() map[int64]int { | ||
s.Lock() | ||
defer s.Unlock() | ||
info := map[int64]int{} | ||
for id, stream := range s.streams { | ||
stream.Lock() | ||
info[id] = len(stream.sub) | ||
stream.Unlock() | ||
} | ||
return info | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package livelog | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
) | ||
|
||
// The max size that the content can be. | ||
const bufferSize = 5000 | ||
|
||
// Line is a single line of the log. | ||
type Line struct { | ||
Number int `json:"Position"` | ||
Message string `json:"Message"` | ||
Timestamp int64 `json:"Time"` | ||
} | ||
|
||
type stream struct { | ||
sync.Mutex | ||
|
||
content []*Line | ||
sub map[*subscriber]struct{} | ||
} | ||
|
||
func newStream() *stream { | ||
return &stream{ | ||
sub: map[*subscriber]struct{}{}, | ||
} | ||
} | ||
|
||
func (s *stream) write(line *Line) error { | ||
s.Lock() | ||
defer s.Unlock() | ||
for su := range s.sub { | ||
su.send(line) | ||
} | ||
|
||
if size := len(s.content); size >= bufferSize { | ||
s.content = s.content[size-bufferSize:] | ||
} | ||
return nil | ||
} | ||
|
||
func (s *stream) subscribe(ctx context.Context) (<-chan *Line, <-chan error) { | ||
sub := &subscriber{ | ||
handler: make(chan *Line, bufferSize), | ||
closeChannel: make(chan struct{}), | ||
} | ||
err := make(chan error) | ||
|
||
s.Lock() | ||
// Send history data. | ||
for _, line := range s.content { | ||
sub.send(line) | ||
} | ||
s.sub[sub] = struct{}{} | ||
s.Unlock() | ||
|
||
go func() { | ||
defer close(err) | ||
select { | ||
case <-sub.closeChannel: | ||
case <-ctx.Done(): | ||
sub.close() | ||
} | ||
}() | ||
return sub.handler, err | ||
} | ||
|
||
func (s *stream) close() error { | ||
s.Lock() | ||
defer s.Unlock() | ||
for sub := range s.sub { | ||
delete(s.sub, sub) | ||
sub.close() | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package livelog | ||
|
||
import "sync" | ||
|
||
type subscriber struct { | ||
sync.Mutex | ||
|
||
handler chan *Line | ||
closeChannel chan struct{} | ||
closed bool | ||
} | ||
|
||
func (s *subscriber) send(line *Line) { | ||
select { | ||
case <-s.closeChannel: | ||
case s.handler <- line: | ||
default: | ||
|
||
} | ||
} | ||
|
||
func (s *subscriber) close() { | ||
s.Lock() | ||
if !s.closed { | ||
close(s.closeChannel) | ||
s.closed = true | ||
} | ||
s.Unlock() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package main | ||
|
||
var ( | ||
VERSION = "v0.5.0" | ||
VERSION string | ||
COMMIT_SHA string | ||
BUILD_TIME string | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package main |