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

Commit

Permalink
Merge pull request #59 from vidar-team/dev [CI SKIP]
Browse files Browse the repository at this point in the history
v0.6.2
wuhan005 authored Aug 5, 2020
2 parents 58d3532 + 6ef226d commit ff8bd07
Showing 14 changed files with 412 additions and 15 deletions.
20 changes: 11 additions & 9 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
on: release
on:
release:
types: [published]
name: Build
jobs:
release-linux-386:
@@ -7,7 +9,7 @@ jobs:
steps:
- uses: actions/checkout@master
- name: compile and release
uses: wuhan005/cardinal_release_action@v0.0.6
uses: Cardinal-Platform/release_action@v0.0.7
env:
GITHUB_TOKEN: ${{ secrets.TOKEN }}
GOARCH: "386"
@@ -18,7 +20,7 @@ jobs:
steps:
- uses: actions/checkout@master
- name: compile and release
uses: wuhan005/cardinal_release_action@v0.0.6
uses: Cardinal-Platform/release_action@v0.0.7
env:
GITHUB_TOKEN: ${{ secrets.TOKEN }}
GOARCH: amd64
@@ -29,7 +31,7 @@ jobs:
steps:
- uses: actions/checkout@master
- name: compile and release
uses: wuhan005/cardinal_release_action@v0.0.6
uses: Cardinal-Platform/release_action@v0.0.7
env:
GITHUB_TOKEN: ${{ secrets.TOKEN }}
GOARCH: arm
@@ -40,7 +42,7 @@ jobs:
steps:
- uses: actions/checkout@master
- name: compile and release
uses: wuhan005/cardinal_release_action@v0.0.6
uses: Cardinal-Platform/release_action@v0.0.7
env:
GITHUB_TOKEN: ${{ secrets.TOKEN }}
GOARCH: arm64
@@ -51,7 +53,7 @@ jobs:
steps:
- uses: actions/checkout@master
- name: compile and release
uses: wuhan005/cardinal_release_action@v0.0.6
uses: Cardinal-Platform/release_action@v0.0.7
env:
GITHUB_TOKEN: ${{ secrets.TOKEN }}
GOARCH: "386"
@@ -62,7 +64,7 @@ jobs:
steps:
- uses: actions/checkout@master
- name: compile and release
uses: wuhan005/cardinal_release_action@v0.0.6
uses: Cardinal-Platform/release_action@v0.0.7
env:
GITHUB_TOKEN: ${{ secrets.TOKEN }}
GOARCH: amd64
@@ -73,7 +75,7 @@ jobs:
steps:
- uses: actions/checkout@master
- name: compile and release
uses: wuhan005/cardinal_release_action@v0.0.6
uses: Cardinal-Platform/release_action@v0.0.7
env:
GITHUB_TOKEN: ${{ secrets.TOKEN }}
GOARCH: "386"
@@ -84,7 +86,7 @@ jobs:
steps:
- uses: actions/checkout@master
- name: compile and release
uses: wuhan005/cardinal_release_action@v0.0.6
uses: Cardinal-Platform/release_action@v0.0.7
env:
GITHUB_TOKEN: ${{ secrets.TOKEN }}
GOARCH: amd64
13 changes: 12 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -4,24 +4,33 @@ go 1.13

require (
github.com/BurntSushi/toml v0.3.1
github.com/Microsoft/go-winio v0.4.14 // indirect
github.com/andybalholm/cascadia v1.1.0 // indirect
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496 // indirect
github.com/aws/aws-sdk-go v1.30.20 // indirect
github.com/containerd/containerd v1.3.6 // indirect
github.com/disintegration/imaging v1.6.2 // indirect
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v17.12.0-ce-rc1.0.20200728121027-0f41a77c6993+incompatible
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/dustin/go-humanize v1.0.0
github.com/elazarl/goproxy v0.0.0-20200426045556-49ad98f6dac1 // indirect
github.com/fatih/color v1.9.0 // indirect
github.com/getsentry/sentry-go v0.7.0
github.com/gin-contrib/cors v1.3.0
github.com/gin-contrib/static v0.0.0-20191128031702-f81c604d8ac2
github.com/gin-gonic/gin v1.5.0
github.com/gogo/protobuf v1.3.1 // indirect
github.com/gorilla/context v1.1.1 // indirect
github.com/gorilla/sessions v1.2.0 // indirect
github.com/gorilla/websocket v1.4.2
github.com/gosimple/slug v1.9.0 // indirect
github.com/jinzhu/configor v1.2.0 // indirect
github.com/jinzhu/gorm v1.9.12
github.com/microcosm-cc/bluemonday v1.0.2 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/parnurzeal/gorequest v0.2.16
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/qor/admin v0.0.0-20200229145930-e279f96c8c05 // indirect
@@ -38,6 +47,7 @@ require (
github.com/qor/session v0.0.0-20170907035918-8206b0adab70 // indirect
github.com/qor/validations v0.0.0-20171228122639-f364bca61b46 // indirect
github.com/satori/go.uuid v1.2.0
github.com/sirupsen/logrus v1.6.0 // indirect
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/stretchr/testify v1.5.1
github.com/thanhpk/randstr v1.0.4
@@ -49,7 +59,8 @@ require (
github.com/yosssi/gohtml v0.0.0-20200424144038-a48de20dd9dd // indirect
golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd
golang.org/x/text v0.3.0
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135
google.golang.org/grpc v1.30.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v2 v2.2.4 // indirect
moul.io/http2curl v1.0.0 // indirect
68 changes: 68 additions & 0 deletions go.sum

Large diffs are not rendered by default.

60 changes: 60 additions & 0 deletions src/docker.go
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"github.com/parnurzeal/gorequest"
"github.com/vidar-team/Cardinal/src/utils"
"regexp"
@@ -61,6 +62,65 @@ func (s *Service) getImageData(c *gin.Context) (int, interface{}) {
}

return utils.MakeSuccessJSON(gin.H{
"Image": fmt.Sprintf("%s/%s:%s", form.User, form.Image, form.Tag),
"Name": form.Image,
"Ports": ports,
})
}

func (s *Service) deployFromDocker(c *gin.Context) (int, interface{}) {
type port struct {
In uint `binding:"required"`
Out uint `binding:"required"`
}

type inputForm struct {
Image string `binding:"required"`
Challenge uint `binding:"required"`
IP string `binding:"required"`
ServicePort uint `binding:"required"`
SSHPort uint `binding:"required"`
RootSSHName string `binding:"required"`
UserSSHName string `binding:"required"`
Description string `binding:"required"`
Ports []port `binding:"required"`
}

var form inputForm
err := c.BindJSON(&form)
if err != nil {
return utils.MakeErrJSON(400, 40042, "payload error")
}

// Pre-check

// challenge exist
var challenge Challenge
s.Mysql.Model(&Challenge{}).Where(&Challenge{Model: gorm.Model{ID: form.Challenge}}).Find(&challenge)
if challenge.ID == 0 {
return utils.MakeErrJSON(404, 40406, "payload error")
}
// port check
if form.ServicePort == 0 || form.ServicePort > 65536 || form.SSHPort == 0 || form.SSHPort > 65536 {
return utils.MakeErrJSON(400, 40043, "error port")
}
for i1, p1 := range form.Ports {
if p1.In == 0 || p1.In > 65536 || p1.Out == 0 || p1.Out > 65536 {
return utils.MakeErrJSON(400, 40043, "error port")
}
for i2, p2 := range form.Ports {
if i1 != i2 && (p1.In == p2.In || p1.Out == p2.Out) {
return utils.MakeErrJSON(400, 40044, "error port")
}
}
}
// check name
if form.RootSSHName == form.UserSSHName {
return utils.MakeErrJSON(400, 40045, "name repeat")
}

// get the docker image
// TODO

return utils.MakeSuccessJSON("")
}
1 change: 1 addition & 0 deletions src/docker/container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package docker
50 changes: 50 additions & 0 deletions src/docker/image.go
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
}
22 changes: 19 additions & 3 deletions src/install.go
Original file line number Diff line number Diff line change
@@ -18,6 +18,8 @@ import (
"time"
)

const DOCKER_ENV = "CARDINAL_DOCKER"

const configTemplate = `
[base]
Title="{{ .Title }}"
@@ -184,10 +186,24 @@ func (s *Service) initManager() {
var managerCount int
s.Mysql.Model(&Manager{}).Count(&managerCount)
if managerCount == 0 {
// Create manager account if managers table is empty.
var managerName, managerPassword string
utils.InputString(&managerName, string(locales.I18n.T(conf.Get().SystemLanguage, "install.manager_name")))
utils.InputString(&managerPassword, string(locales.I18n.T(conf.Get().SystemLanguage, "install.manager_password")))

// Check if it is built by docker-compose.
if os.Getenv(DOCKER_ENV) != "" {
managerName = "admin_" + randstr.Hex(3)
managerPassword = randstr.String(16)

// Print out the account info.
fmt.Println("\n\n=======================================")
fmt.Printf("Manager Name: %s\n", managerName)
fmt.Printf("Manager Password: %s\n", managerPassword)
fmt.Printf("=======================================\n\n\n")
} else {
utils.InputString(&managerName, string(locales.I18n.T(conf.Get().SystemLanguage, "install.manager_name")))
utils.InputString(&managerPassword, string(locales.I18n.T(conf.Get().SystemLanguage, "install.manager_password")))
}

// Create manager account if managers table is empty.
s.Mysql.Create(&Manager{
Name: managerName,
Password: utils.AddSalt(managerPassword),
79 changes: 79 additions & 0 deletions src/livelog/livelog.go
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
}
78 changes: 78 additions & 0 deletions src/livelog/stream.go
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
}
29 changes: 29 additions & 0 deletions src/livelog/subscriber.go
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()
}
2 changes: 1 addition & 1 deletion src/main.go
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
)
2 changes: 2 additions & 0 deletions src/router.go
Original file line number Diff line number Diff line change
@@ -13,6 +13,8 @@ import (
)

func (s *Service) initRouter() *gin.Engine {
gin.SetMode(gin.ReleaseMode)

r := gin.Default()
r.Use(cors.New(cors.Config{
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD"},
2 changes: 1 addition & 1 deletion src/sentry.go
Original file line number Diff line number Diff line change
@@ -27,5 +27,5 @@ func (s *Service) initSentry() {
}

// greeting
sentry.CaptureMessage("Hello")
sentry.CaptureMessage("Hello " + VERSION)
}
1 change: 1 addition & 0 deletions src/task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package main

0 comments on commit ff8bd07

Please sign in to comment.