1
0

feat: add redis support

This commit is contained in:
502647092 2018-10-31 20:30:43 +08:00
parent 0be3e70228
commit 93668341a3
3 changed files with 94 additions and 3 deletions

18
go.mod Normal file
View File

@ -0,0 +1,18 @@
module docker-image-sync
require (
github.com/cweill/gotests v1.5.2 // indirect
github.com/go-redis/redis v6.14.2+incompatible
github.com/golang/lint v0.0.0-20181026193005-c67002cb31c3 // indirect
github.com/lukehoban/go-find-references v0.0.0-20150918174914-42505ef666d1 // indirect
github.com/lukehoban/go-outline v0.0.0-20161011150102-e78556874252 // indirect
github.com/lukehoban/ident v0.0.0-20161021214352-ec6acfba39eb // indirect
github.com/newhook/go-symbols v0.0.0-20151212134530-b75dfefa0d23 // indirect
github.com/nsf/gocode v0.0.0-20180902125341-7b1d4e18cdc5 // indirect
github.com/onsi/gomega v1.4.2 // indirect
github.com/rogpeppe/godef v1.0.0 // indirect
github.com/sqs/goreturns v0.0.0-20181028201513-538ac6014518 // indirect
github.com/tpng/gopkgs v0.0.0-20180428091733-81e90e22e204 // indirect
sourcegraph.com/sqs/goreturns v0.0.0-20181028201513-538ac6014518 // indirect
yumc.pw/cloud/docker-image-sync v0.0.0-20181029103637-0be3e70228f3 // indirect
)

53
go.sum Normal file
View File

@ -0,0 +1,53 @@
9fans.net/go v0.0.0-20150709035532-65b8cf069318/go.mod h1:diCsxrliIURU9xsYtjCp5AbpQKqdhKmf0ujWDUSkfoY=
github.com/cweill/gotests v1.5.2 h1:kKqmKmS2wCV3tuLnfpbiuN8OlkosQZTpCfiqmiuNAsA=
github.com/cweill/gotests v1.5.2/go.mod h1:XZYOJkGVkCRoymaIzmp9Wyi3rUgfA3oOnkuljYrjFV8=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-redis/redis v6.14.2+incompatible h1:UE9pLhzmWf+xHNmZsoccjXosPicuiNaInPgym8nzfg0=
github.com/go-redis/redis v6.14.2+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/golang/lint v0.0.0-20181026193005-c67002cb31c3 h1:I4BOK3PBMjhWfQM2zPJKK7lOBGsrsvOB7kBELP33hiE=
github.com/golang/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:tluoj9z5200jBnyusfRPU2LqT6J+DAorxEvtC7LHB+E=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/lukehoban/go-find-references v0.0.0-20150918174914-42505ef666d1 h1:F7r7JiLNObE2d4z82V3/cUfW5alV6YY89LQtK0qcUQc=
github.com/lukehoban/go-find-references v0.0.0-20150918174914-42505ef666d1/go.mod h1:T8xMbBjUvlaQRNZdgSoPLks3kZlAemyqb6HJPHyOUek=
github.com/lukehoban/go-outline v0.0.0-20161011150102-e78556874252 h1:D2VNityLQ1srKF+MSllSGQ4NwMci20llMkvVAmU2aCk=
github.com/lukehoban/go-outline v0.0.0-20161011150102-e78556874252/go.mod h1:O9bIJ6BRFBmP3AKTW8cqESVbauSmifSrRB/n9zq6x9Q=
github.com/lukehoban/ident v0.0.0-20161021214352-ec6acfba39eb h1:hLP73jhKhyayGVSX48TiA1qjMatiBpG7uc2zIsqy7yk=
github.com/lukehoban/ident v0.0.0-20161021214352-ec6acfba39eb/go.mod h1:Znk/A72lIyE/q043nd5s5tg63q2rCEohe6Gn8Yo4VdU=
github.com/newhook/go-symbols v0.0.0-20151212134530-b75dfefa0d23 h1:bXKgb2O/gIrZd/smUeP3OJcAt1ey3UV+cPYT+mRUNzs=
github.com/newhook/go-symbols v0.0.0-20151212134530-b75dfefa0d23/go.mod h1:9RIESfVnNNAkdTKNuEYS0rEkXXM2HYfchPD4xXNx1CU=
github.com/nsf/gocode v0.0.0-20180902125341-7b1d4e18cdc5 h1:V2BSh6DxcqZrEId1t9k3V7CXJQEpu50cVko4PyrwDb4=
github.com/nsf/gocode v0.0.0-20180902125341-7b1d4e18cdc5/go.mod h1:6Q8/OMaaKAgTX7/jt2bOXVDrm1eJhoNd+iwzghR7jvs=
github.com/onsi/ginkgo v1.6.0 h1:Ix8l273rp3QzYgXSR+c8d1fTG7UPgYkOSELPhiY/YGw=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.2 h1:3mYCb7aPxS/RU7TI1y4rkEn1oKmPRjNJLNEXgw7MH2I=
github.com/onsi/gomega v1.4.2/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/rogpeppe/godef v1.0.0 h1:+3JM5juQRFS/Vifg5lMHkAtRELpcGicuZXdBmf7NIhE=
github.com/rogpeppe/godef v1.0.0/go.mod h1:FWOCnfqToTbJkUGS32JdUoCuBBjtBQ3ZawrP7InscsM=
github.com/sqs/goreturns v0.0.0-20181028201513-538ac6014518 h1:iD+PFTQwKEmbwSdwfvP5ld2WEI/g7qbdhmHJ2ASfYGs=
github.com/sqs/goreturns v0.0.0-20181028201513-538ac6014518/go.mod h1:CKI4AZ4XmGV240rTHfO0hfE83S6/a3/Q1siZJ/vXf7A=
github.com/tpng/gopkgs v0.0.0-20180428091733-81e90e22e204 h1:nQYpoEpKTbArT17ogHCrMfsM4CCGiOdtQMuZJxeu8lQ=
github.com/tpng/gopkgs v0.0.0-20180428091733-81e90e22e204/go.mod h1:MwY6Iwya1EJi2InQFe7g/J3Qr7eik70yycN5+sw03hQ=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e h1:o3PsSEY8E4eXWkXrIP9YJALUkVZqzHJT5DOasTyn8Vs=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
sourcegraph.com/sqs/goreturns v0.0.0-20181028201513-538ac6014518 h1:hK6WwmQDFDRBBDeN/+3lcp1NoDn0ptGiJhxIpvzpqCw=
sourcegraph.com/sqs/goreturns v0.0.0-20181028201513-538ac6014518/go.mod h1:Hm3i1Xmrc6guw1/fGxmO0PIGpbmXAQsJ8LhIQncGYwo=
yumc.pw/cloud/docker-image-sync v0.0.0-20181029103637-0be3e70228f3 h1:963GsCDM0C1wNTG/UBDQ4aYCPvkFRnj4YSce5pj5whw=
yumc.pw/cloud/docker-image-sync v0.0.0-20181029103637-0be3e70228f3/go.mod h1:ZY9otb70bNCEGplod222rkpZ6P5ilIDxOUs5BIPUAjg=

26
main.go
View File

@ -9,12 +9,16 @@ import (
"os/exec" "os/exec"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/go-redis/redis"
) )
var ( var (
cache = map[string]string{} cache = map[string]string{}
target string target string
client = &http.Client{} client = &http.Client{}
rc *redis.Client
) )
const ( const (
@ -26,6 +30,7 @@ func main() {
t := flag.String("t", "registry.cn-hangzhou.aliyuncs.com/miaowoo", "Target Repo") t := flag.String("t", "registry.cn-hangzhou.aliyuncs.com/miaowoo", "Target Repo")
u := flag.String("u", "", "Target Repo UserName") u := flag.String("u", "", "Target Repo UserName")
p := flag.String("p", "", "Target Repo Password") p := flag.String("p", "", "Target Repo Password")
r := flag.String("r", "127.0.0.1:6379", "Redis Server Host")
flag.Parse() flag.Parse()
target = *t target = *t
@ -36,14 +41,23 @@ func main() {
} }
loginResult, err := exec.Command(docker, "login", *t, "-u", *u, "-p", *p).Output() loginResult, err := exec.Command(docker, "login", *t, "-u", *u, "-p", *p).Output()
if err != nil { if err != nil {
fmt.Printf("Error Can't Login To %s Username %s Password %s ...\nError: %s", *t, *u, *p, err.Error()) fmt.Printf("Error Can't Login To %s Username %s Password %s ...\nError: %s", *t, *u, *p, err.Error())
return return
} }
fmt.Printf(string(loginResult)) fmt.Printf(string(loginResult))
rc = redis.NewClient(&redis.Options{
Addr: *r,
})
_, err = rc.Ping().Result()
if err != nil {
fmt.Printf("Redis Connect Error: %s\n", err.Error())
return
}
fmt.Printf("Redis Server %s Connect Success\n", *r)
startServer(*b) startServer(*b)
} }
@ -115,7 +129,7 @@ func handleNormal(resp http.ResponseWriter, req *http.Request) {
out := `<html><body>` out := `<html><body>`
out += `<h3>Image ` + origin + ` Is Tag to ` + image + ` And Push Success</h3>` out += `<h3>Image ` + origin + ` Is Tag to ` + image + ` And Push Success</h3>`
if checkMirror(name) { if checkMirrorFromRedis(name) || checkMirror(name) {
out += "Alread Transfer" out += "Alread Transfer"
} else { } else {
cache[origin] = "0" cache[origin] = "0"
@ -125,6 +139,7 @@ func handleNormal(resp http.ResponseWriter, req *http.Request) {
</pre>` </pre>`
delete(cache, origin) delete(cache, origin)
} }
rc.Set(name, time.Now().Unix(), 0)
out += `<h3>You Can Use below Command To Pull...</h3> out += `<h3>You Can Use below Command To Pull...</h3>
<pre> <pre>
docker pull ` + image + ` docker pull ` + image + `
@ -135,6 +150,11 @@ docker rmi ` + image + `
resp.Write([]byte(out)) resp.Write([]byte(out))
} }
func checkMirrorFromRedis(image string) bool {
_, err := rc.Get(image).Result()
return err != redis.Nil
}
func transferImage(origin string, image string) string { func transferImage(origin string, image string) string {
out := "" out := ""
cache[origin] = "10" cache[origin] = "10"