diff --git a/Dockerfile b/Dockerfile index e69de29..e37a714 100644 --- a/Dockerfile +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM alpine + +COPY ukiyo . +COPY dbs . +RUN chmod 777 ./ukiyo + +EXPOSE 8080 + +CMD ["./ukiyo"] diff --git a/README.md b/README.md index 224ed36..a4ef2b8 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ - Container manager - Push manager (webhooks configuration) +- Scheduler - OPTIONAL - Pull based update implementation ## Execution modes @@ -28,3 +29,101 @@ Two modes of execution ### Go style guide - https://github.com/rajikaimal/go-styleguide + +## Dev Setup Guide + +Setup docker +```sh +$ go mod init ukiyo +$ go mod tidy +``` + +Create docker binary file +```sh +$ set GOARCH=amd64 +$ set GOOS=linux +$ go build -ldflags="-s -w" -o ukiyo main.go +``` + +Docker build command +```sh +$ docker build -f Dockerfile -t agentukiyo/ukiyo:tag . +$ docker push agentukiyo/ukiyo:tag +``` + +Run ukiyo agent +```sh +$ docker run -p 8080:8080 \ + -- name ukiyo \ + -v /var/run/docker.sock:/var/run/docker.sock \ + -v /home/reinventor/dbs:/dbs \ + agentukiyo/ukiyo:01 +``` + +Add webhook to your dockerhub repository +``` +http:{serverIP}:8080/ukiyo-web-hook +``` + +Add your own docker registy details +``` +http:{serverIP}:8080/save-container-access-keys +{ + "username":"docker registry name" + "desc":"docker registry description" + "accessToken":"docker registry accesstoken" + "email":"your email" + "serverAddress":"http://docker.io/v1" +} + +http:{serverIP}:8080/edit-container-access-keys +{ + "username":"docker registry name" + "desc":"edited description" + "accessToken":"new docker registry accesstoken" + "email":"new email" + "serverAddress":"http://docker.io/v1" +} + +http://{serverIP}:8080/delete-container-access-keys/{registryname} +``` + +Add your deployment details +``` +http://{serverIP}:8080/save-repository-scheduled-time +{ + "name": "repository name", + "bindingPort": + [{ + "exportPort": "8180", + "internalPort": "80" + }, + { + "exportPort": "443", + "internalPort": "443" + }], + "scheduledAt": "1555438658", + "scheduledDowntime": false +} + +http://{serverIP}:8080/edit-repository-scheduled-time +{ + "name": "repository name", + "bindingPort": + [{ + "exportPort": "8180", + "internalPort": "80" + }], + "scheduledAt": "1555438658", + "scheduledDowntime": false +} + +http://{serverIP}:8080/remove-repository-scheduled-time/{repositoryname} +``` + +List your deployment details +``` +http://{serverIP}:8080/get-container-history/1 + +http://{serverIP}:8080/get-container-history-by-name/{repositoryname} +``` diff --git a/api/historyapilayer/historyapilayer.go b/api/historyapilayer/historyapilayer.go new file mode 100644 index 0000000..7a14e95 --- /dev/null +++ b/api/historyapilayer/historyapilayer.go @@ -0,0 +1,26 @@ +package historyapilayer + +import ( + "github.com/gin-gonic/gin" + "log" + "net/http" + "ukiyo/internal/containerhistory" +) + +func GetAllContainerHistory(r *gin.Engine) { + r.GET("/get-container-history/:pageNo", func(c *gin.Context) { + pageNo := c.Param("pageNo") + response := containerhistory.QueryAllHistoryRecodeInDB(pageNo) + log.Println("Container History details by pageNo=" + pageNo) + c.JSON(http.StatusOK, response) + }) +} + +func GetAllContainerHistoryByName(r *gin.Engine) { + r.GET("/get-container-history-by-name/:name", func(c *gin.Context) { + var response containerhistory.HistoryResponse + name := c.Param("name") + log.Println("Container History details by name=" + name) + c.JSON(http.StatusOK, response) + }) +} diff --git a/api/registryapilayer/registryapilayer.go b/api/registryapilayer/registryapilayer.go new file mode 100644 index 0000000..d58a720 --- /dev/null +++ b/api/registryapilayer/registryapilayer.go @@ -0,0 +1,61 @@ +package registryapilayer + +import ( + "github.com/gin-gonic/gin" + "log" + "net/http" + "ukiyo/internal/containeraccess" + "ukiyo/pkg/jencoder" +) + +func SaveContainerAccessKeys(r *gin.Engine) { + r.POST("/save-container-access-keys", func(c *gin.Context) { + var containerKey containeraccess.ContainerKeys + var responseObj containeraccess.ResponseObj + c.ShouldBindJSON(&containerKey) + log.Println("Container keys save details" + jencoder.PrintJson(containerKey)) + + if len(containerKey.UserName) > 0 && len(containerKey.AccessToken) > 0 && len(containerKey.ServerAddress) > 0 { + res := containeraccess.InsertDockerRegData(containerKey) + responseObj = res + } else { + responseObj.ResponseCode = 1 + responseObj.ResponseDesc = "Invalid Parameter" + } + c.JSON(http.StatusOK, responseObj) + }) +} + +func EditContainerAccessKeys(r *gin.Engine) { + r.POST("/edit-container-access-keys", func(c *gin.Context) { + var containerKey containeraccess.ContainerKeys + var responseObj containeraccess.ResponseObj + c.ShouldBindJSON(&containerKey) + log.Println("Container keys Update details" + jencoder.PrintJson(containerKey)) + + if len(containerKey.UserName) > 0 && len(containerKey.AccessToken) > 0 { + res := containeraccess.UpdateDockerRegData(containerKey) + responseObj = res + } else { + responseObj.ResponseCode = 1 + responseObj.ResponseDesc = "Invalid Parameter" + } + c.JSON(http.StatusOK, responseObj) + }) +} + +func DeleteContainerAccessKeys(r *gin.Engine) { + r.DELETE("/delete-container-access-keys/:userName", func(c *gin.Context) { + var responseObj containeraccess.ResponseObj + name := c.Param("userName") + log.Println("Container keys Delete details userName=" + name) + if len(name) > 0 { + res := containeraccess.DeleteDockerRegData(name) + responseObj = res + } else { + responseObj.ResponseCode = 1 + responseObj.ResponseDesc = "Invalid Parameter" + } + c.JSON(http.StatusOK, responseObj) + }) +} diff --git a/api/schedulerapilayer/schedulerapilayer.go b/api/schedulerapilayer/schedulerapilayer.go new file mode 100644 index 0000000..93c5392 --- /dev/null +++ b/api/schedulerapilayer/schedulerapilayer.go @@ -0,0 +1,62 @@ +package schedulerapilayer + +import ( + "github.com/gin-gonic/gin" + "log" + "net/http" + "ukiyo/internal/containerscheduler" + "ukiyo/internal/util" + "ukiyo/pkg/jencoder" +) + +func SaveRepositoryScheduledTime(r *gin.Engine) { + r.POST("/save-repository-scheduled-time", func(c *gin.Context) { + var podsDetails containerscheduler.PodsDetails + var responseObj containerscheduler.ResponseObj + c.ShouldBindJSON(&podsDetails) + log.Println("Pods save details" + jencoder.PrintJson(podsDetails)) + + if len(podsDetails.Name) > 0 && util.BindPortValidator(podsDetails.BindingPort) { + res := containerscheduler.InsertPodsData(podsDetails) + responseObj = res + } else { + responseObj.ResponseCode = 1 + responseObj.ResponseDesc = "Invalid Parameter" + } + c.JSON(http.StatusOK, responseObj) + }) +} + +func EditRepositoryScheduledTime(r *gin.Engine) { + r.POST("/edit-repository-scheduled-time", func(c *gin.Context) { + var podsDetails containerscheduler.PodsDetails + var responseObj containerscheduler.ResponseObj + c.ShouldBindJSON(&podsDetails) + log.Println("Pods Update details" + jencoder.PrintJson(podsDetails)) + + if len(podsDetails.Name) > 0 && util.BindPortValidator(podsDetails.BindingPort) { + res := containerscheduler.UpdatePodsData(podsDetails) + responseObj = res + } else { + responseObj.ResponseCode = 1 + responseObj.ResponseDesc = "Invalid Parameter" + } + c.JSON(http.StatusOK, responseObj) + }) +} + +func DeleteRepositoryScheduledTime(r *gin.Engine) { + r.DELETE("/remove-repository-scheduled-time/:name", func(c *gin.Context) { + var responseObj containerscheduler.ResponseObj + name := c.Param("name") + log.Println("Pod Delete details name=" + name) + if len(name) > 0 { + res := containerscheduler.DeleteDockerRegData(name) + responseObj = res + } else { + responseObj.ResponseCode = 1 + responseObj.ResponseDesc = "Invalid Parameter" + } + c.JSON(http.StatusOK, responseObj) + }) +} diff --git a/cmd/ukiyo/main b/cmd/ukiyo/main new file mode 100644 index 0000000..4bcdceb Binary files /dev/null and b/cmd/ukiyo/main differ diff --git a/docker-compose.yml b/dbs/app.properties similarity index 100% rename from docker-compose.yml rename to dbs/app.properties diff --git a/go.mod b/go.mod index 82ba0af..73cd011 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module ukiyo go 1.14 require ( + github.com/fsouza/go-dockerclient v1.6.5 github.com/gin-gonic/gin v1.6.3 - github.com/stretchr/testify v1.4.0 + github.com/sonyarouje/simdb v0.0.0-20181202125413-c2488dfc374a ) diff --git a/go.sum b/go.sum index fd20c56..b8a9462 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,46 @@ +bazil.org/fuse v0.0.0-20160811212531-371fbbdaa898/go.mod h1:Xbm+BRKSBEpa4q4hTSxohYNQpsxXPbPry4JJWOB3LB8= +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 h1:w+iIsaOQNcT7OZ575w+acHgRric5iCyQh+xv+KJ4HB8= +github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/Microsoft/go-winio v0.4.15-0.20190919025122-fc70bd9a86b5/go.mod h1:tTuCMEN+UleMWgg9dVx4Hu52b1bJo+59jBh3ajtinzw= +github.com/Microsoft/go-winio v0.4.15-0.20200113171025-3fe6c5262873 h1:93nQ7k53GjoMQ07HVP8g6Zj1fQZDDj7Xy2VkNNtvX8o= +github.com/Microsoft/go-winio v0.4.15-0.20200113171025-3fe6c5262873/go.mod h1:tTuCMEN+UleMWgg9dVx4Hu52b1bJo+59jBh3ajtinzw= +github.com/Microsoft/hcsshim v0.8.7 h1:ptnOoufxGSzauVTsdE+wMYnCWA301PdoN4xg5oRdZpg= +github.com/Microsoft/hcsshim v0.8.7/go.mod h1:OHd7sQqRFrYd3RmSgbgji+ctCwkbq2wbEYNSzOYtcBQ= +github.com/blang/semver v3.1.0+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk= +github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/containerd/cgroups v0.0.0-20190919134610-bf292b21730f/go.mod h1:OApqhQ4XNSNC13gXIwDjhOQxjWa/NxkwZXJ1EvqT0ko= +github.com/containerd/console v0.0.0-20180822173158-c12b1e7919c1/go.mod h1:Tj/on1eG8kiEhd0+fhSDzsPAFESxzBBvdyEgyryXffw= +github.com/containerd/containerd v1.3.0-beta.2.0.20190828155532-0293cbd26c69/go.mod h1:bC6axHOhabU15QhwfG7w5PipXdVtMXFTttgp+kVtyUA= +github.com/containerd/containerd v1.3.0 h1:xjvXQWABwS2uiv3TWgQt5Uth60Gu86LTGZXMJkjc7rY= +github.com/containerd/containerd v1.3.0/go.mod h1:bC6axHOhabU15QhwfG7w5PipXdVtMXFTttgp+kVtyUA= +github.com/containerd/continuity v0.0.0-20190426062206-aaeac12a7ffc/go.mod h1:GL3xCUCBDV3CZiTSEKksMWbLE66hEyuu9qyDOOqM47Y= +github.com/containerd/continuity v0.0.0-20200228182428-0f16d7a0959c h1:8ahmSVELW1wghbjerVAyuEYD5+Dio66RYvSS0iGfL1M= +github.com/containerd/continuity v0.0.0-20200228182428-0f16d7a0959c/go.mod h1:Dq467ZllaHgAtVp4p1xUQWBrFXR9s/wyoTpG8zOJGkY= +github.com/containerd/fifo v0.0.0-20190226154929-a9fb20d87448/go.mod h1:ODA38xgv3Kuk8dQz2ZQXpnv/UZZUHUCL7pnLehbXgQI= +github.com/containerd/go-runc v0.0.0-20180907222934-5a6d9f37cfa3/go.mod h1:IV7qH3hrUgRmyYrtgEeGWJfWbgcHL9CSRruz2Vqcph0= +github.com/containerd/ttrpc v0.0.0-20190828154514-0e0f228740de/go.mod h1:PvCDdDGpgqzQIzDW1TphrGLssLDZp2GuS+X5DkEJB8o= +github.com/containerd/typeurl v0.0.0-20180627222232-a93fcdb778cd/go.mod h1:Cm3kwCdlkCfMSHURc+r6fwoGH6/F1hH3S4sg0rLFWPc= +github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/docker/distribution v2.7.1+incompatible h1:a5mlkVzth6W5A4fOsS3D2EO5BUmsJpcB+cRlLU7cSug= +github.com/docker/distribution v2.7.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= +github.com/docker/docker v1.4.2-0.20191101170500-ac7306503d23 h1:oqgGT9O61YAYvI41EBsLePOr+LE6roB0xY4gpkZuFSE= +github.com/docker/docker v1.4.2-0.20191101170500-ac7306503d23/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= +github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ= +github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= +github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw= +github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= +github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= +github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/fsouza/go-dockerclient v1.6.5 h1:vuFDnPcds3LvTWGYb9h0Rty14FLgkjHZdwLDROCdgsw= +github.com/fsouza/go-dockerclient v1.6.5/go.mod h1:GOdftxWLWIbIWKbIMDroKFJzPdg6Iw7r+jX1DDZdVsA= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14= @@ -13,11 +53,36 @@ github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD87 github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY= github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI= +github.com/godbus/dbus v0.0.0-20190422162347-ade71ed3457e/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4= +github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= +github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls= +github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I= github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/hashicorp/errwrap v0.0.0-20141028054710-7554cd9344ce/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/go-multierror v0.0.0-20161216184304-ed905158d874/go.mod h1:JMRHfdO9jKNzS/+BTlxCjKNQHg/jZAft8U7LloJvN7I= +github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= +github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= @@ -26,22 +91,119 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OH github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A= +github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= +github.com/opencontainers/go-digest v0.0.0-20180430190053-c9281466c8b2/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s= +github.com/opencontainers/go-digest v1.0.0-rc1 h1:WzifXhOVOEOuFYOJAW6aQqW0TooG2iki3E3Ii+WN7gQ= +github.com/opencontainers/go-digest v1.0.0-rc1/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s= +github.com/opencontainers/image-spec v1.0.1 h1:JMemWkRwHx4Zj+fVxWoMCFm/8sYGGrUVojFA6h/TRcI= +github.com/opencontainers/image-spec v1.0.1/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0= +github.com/opencontainers/runc v0.0.0-20190115041553-12f6a991201f/go.mod h1:qT5XzbpPznkRYVz/mWwUaVBUv2rmF59PVA73FjuZG0U= +github.com/opencontainers/runc v0.1.1 h1:GlxAyO6x8rfZYN9Tt0Kti5a/cP41iuiO2yYT0IJGY8Y= +github.com/opencontainers/runc v0.1.1/go.mod h1:qT5XzbpPznkRYVz/mWwUaVBUv2rmF59PVA73FjuZG0U= +github.com/opencontainers/runtime-spec v0.1.2-0.20190507144316-5b71a03e2700/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0= +github.com/opencontainers/runtime-tools v0.0.0-20181011054405-1d69bd0f9c39/go.mod h1:r3f7wjNzSs2extwzU3Y+6pKfobzPh+kKFJ3ofN+3nfs= +github.com/pkg/errors v0.8.1-0.20171018195549-f15c970de5b7/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/procfs v0.0.5/go.mod h1:4A/X28fw3Fc593LaREMrKMqOKvUAntwMDaekg4FpcdQ= +github.com/sirupsen/logrus v1.0.4-0.20170822132746-89742aefa4b2/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc= +github.com/sirupsen/logrus v1.4.1 h1:GL2rEmy6nsikmW0r8opw9JIRScdMF5hA8cOYLH7In1k= +github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= +github.com/sonyarouje/simdb v0.0.0-20181202125413-c2488dfc374a h1:NTTRw3/6k1FROs2k/rmSszsF6gqW7g9YZRwoD0yRftA= +github.com/sonyarouje/simdb v0.0.0-20181202125413-c2488dfc374a/go.mod h1:EOV7qxVlldkek340xzqOyHCAJSkSTsxyL83e8HF4Enk= +github.com/spf13/cobra v0.0.2-0.20171109065643-2da4a54c5cee/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= +github.com/spf13/pflag v1.0.1-0.20171106142849-4c012f6dcd95/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/syndtr/gocapability v0.0.0-20170704070218-db04d3cc01c8/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww= github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= +github.com/urfave/cli v0.0.0-20171014202726-7bc6a0acffa5/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= +github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= +github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ= +github.com/xeipuuv/gojsonschema v0.0.0-20180618132009-1d523034197f/go.mod h1:5yf86TLmAcydyeJq5YvxkGPE2fm/u4myDekKRoLuqhs= +go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= +golang.org/x/crypto v0.0.0-20171113213409-9f005a07e0d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200220183623-bac4c82f6975 h1:/Tl7pH94bvbAAHBdZJT947M/+gp0+CqQXDtMRC0fseo= +golang.org/x/crypto v0.0.0-20200220183623-bac4c82f6975/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09 h1:KaQtG+aDELoNmXYas3TVkGNYRuq8JQ1aa7LJt8EXVyo= +golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190514135907-3a4b5fb9f71f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55 h1:gSJIx1SDwno+2ElGhA4+qG2zF97qiUzTM+rQ0klBOcE= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= +google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.27.1 h1:zvIju4sqAGvwKspUQOhwnpcqSbzi7/H6QomNNjTL4sk= +google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +gopkg.in/airbrake/gobrake.v2 v2.0.9/go.mod h1:/h5ZAUhDkGaJfjzjKLSjv6zCL6O0LLBxU4K+aSYdM/U= 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/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2/go.mod h1:Xk6kEKp8OKb+X14hQBKWaSkCsqBpgog8nAV2xsGOxlo= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= +gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +k8s.io/kubernetes v1.13.0/go.mod h1:ocZa8+6APFNC2tX1DZASIbocyYT5jHzqFVsY5aoB7Jk= diff --git a/internal/containeraccess/containeraccess.go b/internal/containeraccess/containeraccess.go new file mode 100644 index 0000000..5b14054 --- /dev/null +++ b/internal/containeraccess/containeraccess.go @@ -0,0 +1,165 @@ +package containeraccess + +import ( + "github.com/fsouza/go-dockerclient" + "log" + "strings" + "ukiyo/internal/dbconfig" +) + +type ContainerKeys struct { + UserName string `json:"userName"` + Desc string `json:"desc"` + AccessToken string `json:"accessToken"` + Email string `json:"email"` + ServerAddress string `json:"serverAddress"` +} + +type ResponseObj struct { + ResponseCode int + ResponseDesc string +} + +const ( + _dockerRegPk = "userName" +) + +func (c ContainerKeys) ID() (jsonField string, value interface{}) { + value = c.UserName + jsonField = _dockerRegPk + return +} + +var val int + +func RequestLoginKeys(userName string) docker.AuthConfiguration { + var registries = QueryRecodeInDB(userName) + + if len(registries.UserName) == 0 { + panic("User's Registry Credentials is not exist. : " + userName) + } + + return docker.AuthConfiguration{ + Username: registries.UserName, + Password: registries.AccessToken, + Email: registries.Email, + ServerAddress: registries.ServerAddress, + } +} + +func InsertDockerRegData(containerKeys ContainerKeys) ResponseObj { + var responseObj ResponseObj + var registries = QueryListRecodeInDB(containerKeys.UserName) + if len(registries) > 0 { + for _, registry := range registries { + if strings.EqualFold(registry.UserName, containerKeys.UserName) { + val++ + responseObj.ResponseCode = 1 + responseObj.ResponseDesc = "Already Exist" + log.Println("Already Exist The Registry : " + registry.UserName) + } + } + } + log.Println(val) + if val == 0 { + responseObj = InsertDb(containerKeys) + } + return responseObj +} + +func UpdateDockerRegData(containerKeys ContainerKeys) ResponseObj { + var responseObj ResponseObj + + var registries = QueryRecodeInDB(containerKeys.UserName) + + log.Println(registries) + + if len(registries.UserName) != 0 { + registries.Desc = containerKeys.Desc + registries.AccessToken = containerKeys.AccessToken + registries.Email = containerKeys.Email + registries.ServerAddress = containerKeys.ServerAddress + responseObj = UpdateDb(registries) + } else { + responseObj.ResponseCode = 1 + responseObj.ResponseDesc = "Update Failed." + } + + return responseObj +} + +func DeleteDockerRegData(userName string) ResponseObj { + var responseObj ResponseObj + + var registries = QueryRecodeInDB(userName) + + if len(registries.UserName) != 0 { + responseObj = DeleteDb(registries) + } else { + responseObj.ResponseCode = 1 + responseObj.ResponseDesc = "Delete Failed." + } + return responseObj +} + +func QueryListRecodeInDB(Username string) []ContainerKeys { + var registries []ContainerKeys + err := dbconfig.DbConfig().Open(ContainerKeys{}).Where(_dockerRegPk, "=", Username).Get().AsEntity(®istries) + if err != nil { + log.Println(err) + return registries + } + return registries +} + +func QueryRecodeInDB(Username string) ContainerKeys { + var registries ContainerKeys + err := dbconfig.DbConfig().Open(ContainerKeys{}).Where(_dockerRegPk, "=", Username).First().AsEntity(®istries) + if err != nil { + log.Println(err) + return registries + } + return registries +} + +func InsertDb(containerKeys ContainerKeys) ResponseObj { + var responseObj ResponseObj + err := dbconfig.DbConfig().Insert(containerKeys) + if err != nil { + responseObj.ResponseCode = 1 + responseObj.ResponseDesc = "Insert Failed" + log.Println(err) + } else { + responseObj.ResponseCode = 0 + responseObj.ResponseDesc = "Successfully Added" + } + return responseObj +} + +func UpdateDb(containerKeys ContainerKeys) ResponseObj { + var responseObj ResponseObj + err := dbconfig.DbConfig().Update(containerKeys) + if err != nil { + responseObj.ResponseCode = 1 + responseObj.ResponseDesc = "Update Failed" + log.Println(err) + } else { + responseObj.ResponseCode = 0 + responseObj.ResponseDesc = "Successfully Updated" + } + return responseObj +} + +func DeleteDb(containerKeys ContainerKeys) ResponseObj { + var responseObj ResponseObj + err := dbconfig.DbConfig().Delete(containerKeys) + if err != nil { + responseObj.ResponseCode = 1 + responseObj.ResponseDesc = "Delete Failed" + log.Println(err) + } else { + responseObj.ResponseCode = 0 + responseObj.ResponseDesc = "Successfully Deleted" + } + return responseObj +} diff --git a/internal/containerhistory/containerhistory.go b/internal/containerhistory/containerhistory.go new file mode 100644 index 0000000..8fb6bfe --- /dev/null +++ b/internal/containerhistory/containerhistory.go @@ -0,0 +1,131 @@ +package containerhistory + +import ( + "log" + "ukiyo/internal/containeraccess" + "ukiyo/internal/containerscheduler" + "ukiyo/internal/dbconfig" +) + +type HistoryDetails struct { + Name string `json:"name"` + Data []Data `json:"data"` +} + +type Data struct { + Id int `json:"id"` + EventType string `json:"eventType"` + EventObject EventObject `json:"eventObject"` + EventData EventData `json:"eventData"` +} + +type EventObject struct { + EventCode int `json:"EventCode"` + EventDesc string `json:"eventDesc"` + EventAt int `json:"eventAt"` +} + +type EventData struct { + ContainerKeys containeraccess.ContainerKeys `json:"containerKeys"` + PodsDetails containerscheduler.PodsDetails `json:"podsDetails"` + ImagePuller ImagePuller `json:"imagePuller"` + DockerCreator DockerCreator `json:"dockerCreator"` + DockerRunner DockerRunner `json:"dockerRunner"` +} + +type DockerCreator struct { + Name string `json:"name"` + ContainerId string `json:"containerId"` +} + +type ImagePuller struct { + Name string `json:"name"` + ImageName string `json:"imageName"` +} + +type DockerRunner struct { + Name string `json:"name"` +} + +type HistoryResponse struct { + ResponseCode int + ResponseDesc string + PageNumbers int + HistoryDetails HistoryDetails +} + +const ( + _podsPk = "name" + _eventTypeContainerKeys = "Save Container Keys Event" + _eventTypePodsDetails = "Save Pod Details for Deployment Event" + _eventTypeImagePuller = "Pull Image Event" + _eventTypeDockerCreator = "Create Docker Event" + _eventTypeDockerRunner = "Run Image Event" +) + +func (c HistoryDetails) ID() (jsonField string, value interface{}) { + value = c.Name + jsonField = _podsPk + return +} + +func UpdateContainerHistory(name string, data Data) { + var history = QueryHistoryRecodeInDB(name) + if len(history.Name) > 0 { + data.Id = len(history.Data) + 1 + history.Data = append(history.Data, data) + UpdateDb(history) + } else { + var historyDetails HistoryDetails + historyDetails.Name = name + data.Id = 1 + historyDetails.Data = append(historyDetails.Data, data) + InsertDb(historyDetails) + } +} + +func QueryAllHistoryRecodeInDB(pageNo string) HistoryResponse { + var historyRes HistoryResponse + var historyDetails HistoryDetails + err := dbconfig.DbConfig().Open(HistoryDetails{}).First().AsEntity(&historyDetails) + if err != nil { + log.Println(err) + historyRes.ResponseCode = 1 + historyRes.ResponseDesc = "Failed" + return historyRes + } else { + historyRes.ResponseCode = 0 + historyRes.ResponseDesc = "Success" + historyRes.HistoryDetails = historyDetails + historyRes.PageNumbers = 1 + } + return historyRes +} + +func QueryHistoryRecodeInDB(Name string) HistoryDetails { + var history HistoryDetails + err := dbconfig.DbConfig().Open(HistoryDetails{}).Where(_podsPk, "=", Name).First().AsEntity(&history) + if err != nil { + log.Println(err) + return history + } + return history +} + +func InsertDb(historyDetails HistoryDetails) { + err := dbconfig.DbConfig().Insert(historyDetails) + if err != nil { + log.Println("Fail History Recode Insert") + } else { + log.Println("Successfully Installed History Recode") + } +} + +func UpdateDb(historyDetails HistoryDetails) { + err := dbconfig.DbConfig().Update(historyDetails) + if err != nil { + log.Println("Fail History Recode Update") + } else { + log.Println("Successfully Updated History Recode") + } +} diff --git a/internal/containerscheduler/containerscheduler.go b/internal/containerscheduler/containerscheduler.go new file mode 100644 index 0000000..5f9a9f7 --- /dev/null +++ b/internal/containerscheduler/containerscheduler.go @@ -0,0 +1,152 @@ +package containerscheduler + +import ( + "log" + "strings" + "ukiyo/internal/dbconfig" +) + +type PodsDetails struct { + Name string `json:"name"` + BindingPort []BindingPort `json:"bindingPort"` + ScheduledAt int `json:"scheduledAt"` + ScheduledDowntime bool `json:"scheduledDowntime"` +} + +type BindingPort struct { + ExportPort string `json:"exportPort"` + InternalPort string `json:"internalPort"` +} + +type ResponseObj struct { + ResponseCode int + ResponseDesc string +} + +const ( + _podsPk = "name" +) + +var val int + +func (c PodsDetails) ID() (jsonField string, value interface{}) { + value = c.Name + jsonField = _podsPk + return +} + +func GetBindingPortsForContainerCreate(name string) []string { + return []string{"8180:80", "443:443"} +} + +func InsertPodsData(podsDetails PodsDetails) ResponseObj { + var responseObj ResponseObj + var pods = QueryListRecodeInDB() + if len(pods) > 0 { + for _, pod := range pods { + if strings.EqualFold(pod.Name, podsDetails.Name) { + val++ + responseObj.ResponseCode = 1 + responseObj.ResponseDesc = "Already Exist Pod details" + log.Println("Already Exist The Pods : " + pod.Name) + } + } + } + if val == 0 { + responseObj = InsertDb(podsDetails) + } + return responseObj +} + +func UpdatePodsData(podsDetails PodsDetails) ResponseObj { + var responseObj ResponseObj + + var pod = QueryRecodeInDB(podsDetails.Name) + + if len(pod.Name) != 0 { + pod.BindingPort = podsDetails.BindingPort + pod.ScheduledAt = podsDetails.ScheduledAt + responseObj = UpdateDb(pod) + } else { + responseObj.ResponseCode = 1 + responseObj.ResponseDesc = "Update Failed pod." + } + + return responseObj +} + +func DeleteDockerRegData(name string) ResponseObj { + var responseObj ResponseObj + + var pod = QueryRecodeInDB(name) + + if len(pod.Name) != 0 { + responseObj = DeleteDb(pod) + } else { + responseObj.ResponseCode = 1 + responseObj.ResponseDesc = "Delete Failed pod." + } + return responseObj +} + +func QueryListRecodeInDB() []PodsDetails { + var pods []PodsDetails + err := dbconfig.DbConfig().Open(PodsDetails{}).Get().AsEntity(&pods) + if err != nil { + log.Println(err) + return pods + } + return pods +} + +func QueryRecodeInDB(Name string) PodsDetails { + var pod PodsDetails + err := dbconfig.DbConfig().Open(PodsDetails{}).Where(_podsPk, "=", Name).First().AsEntity(&pod) + if err != nil { + log.Println(err) + return pod + } + return pod +} + +func InsertDb(pods PodsDetails) ResponseObj { + var responseObj ResponseObj + err := dbconfig.DbConfig().Insert(pods) + if err != nil { + responseObj.ResponseCode = 1 + responseObj.ResponseDesc = "Insert Failed pod details" + log.Println(err) + } else { + responseObj.ResponseCode = 0 + responseObj.ResponseDesc = "Successfully Added pod details" + } + return responseObj +} + +func UpdateDb(pod PodsDetails) ResponseObj { + var responseObj ResponseObj + err := dbconfig.DbConfig().Update(pod) + if err != nil { + responseObj.ResponseCode = 1 + responseObj.ResponseDesc = "Update Failed pod details" + log.Println(err) + } else { + responseObj.ResponseCode = 0 + responseObj.ResponseDesc = "Successfully Updated pod details" + } + return responseObj +} + +func DeleteDb(pod PodsDetails) ResponseObj { + var responseObj ResponseObj + err := dbconfig.DbConfig().Delete(pod) + if err != nil { + responseObj.ResponseCode = 1 + responseObj.ResponseDesc = "Delete Failed pod details" + log.Println(err) + } else { + responseObj.ResponseCode = 0 + responseObj.ResponseDesc = "Successfully Deleted pod details" + } + return responseObj +} diff --git a/internal/dbconfig/dbconfig.go b/internal/dbconfig/dbconfig.go new file mode 100644 index 0000000..105a15a --- /dev/null +++ b/internal/dbconfig/dbconfig.go @@ -0,0 +1,13 @@ +package dbconfig + +import ( + "github.com/sonyarouje/simdb/db" +) + +func DbConfig() *db.Driver { + driver, err := db.New("dbs") + if err != nil { + panic(err) + } + return driver +} diff --git a/internal/util/util.go b/internal/util/util.go new file mode 100644 index 0000000..4ece6ed --- /dev/null +++ b/internal/util/util.go @@ -0,0 +1,17 @@ +package util + +import ( + "ukiyo/internal/containerscheduler" +) + +func BindPortValidator(bindingPort []containerscheduler.BindingPort) bool { + if len(bindingPort) > 0 { + for _, ports := range bindingPort { + if len(ports.InternalPort) == 0 && len(ports.ExportPort) == 0 { + return false + } + } + return true + } + return false +} diff --git a/internal/validator/validator.go b/internal/validator/validator.go deleted file mode 100644 index e69de29..0000000 diff --git a/internal/validator/validator_test.go b/internal/validator/validator_test.go deleted file mode 100644 index e69de29..0000000 diff --git a/main.go b/main.go index 63936ed..2430c27 100644 --- a/main.go +++ b/main.go @@ -1,14 +1,26 @@ package main import ( - "ukiyo/pkg/webhooks" - "github.com/gin-gonic/gin" + "ukiyo/api/historyapilayer" + "ukiyo/api/registryapilayer" + "ukiyo/api/schedulerapilayer" + "ukiyo/pkg/scheduler" + "ukiyo/pkg/webhook-listener" ) func main() { r := gin.Default() - webhooks.HooksListener(r) - webhooks.HealthCheck(r) + webhook_listener.HealthCheck(r) + webhook_listener.HooksListener(r) + registryapilayer.SaveContainerAccessKeys(r) + registryapilayer.EditContainerAccessKeys(r) + registryapilayer.DeleteContainerAccessKeys(r) + schedulerapilayer.SaveRepositoryScheduledTime(r) + schedulerapilayer.EditRepositoryScheduledTime(r) + schedulerapilayer.DeleteRepositoryScheduledTime(r) + historyapilayer.GetAllContainerHistory(r) + historyapilayer.GetAllContainerHistoryByName(r) + scheduler.ScheduledRunner() r.Run(":8080") } diff --git a/pkg/container/container.go b/pkg/container/container.go deleted file mode 100644 index e69de29..0000000 diff --git a/pkg/container/container_test.go b/pkg/container/container_test.go deleted file mode 100644 index e69de29..0000000 diff --git a/pkg/jencoder/jencoder.go b/pkg/jencoder/jencoder.go new file mode 100644 index 0000000..3b260df --- /dev/null +++ b/pkg/jencoder/jencoder.go @@ -0,0 +1,15 @@ +package jencoder + +import ( + "encoding/json" + "log" +) + +func PrintJson(obj interface{}) string { + b, err := json.Marshal(obj) + if err != nil { + log.Println(err) + return "json marshal error" + } + return string(b) +} diff --git a/pkg/manager/dockercreater/dockercreater.go b/pkg/manager/dockercreater/dockercreater.go new file mode 100644 index 0000000..cd05c6c --- /dev/null +++ b/pkg/manager/dockercreater/dockercreater.go @@ -0,0 +1,66 @@ +package dockercreater + +import ( + docker "github.com/fsouza/go-dockerclient" + "log" + "strings" + "ukiyo/internal/containerscheduler" +) + +type ResponseObj struct { + Name string + ContainerId string + ResponseCode int + ResponseDesc string +} + +func ContainerCreate(name string, imageName string) (ResponseObj, error) { + var responseObj ResponseObj + client, err := docker.NewClientFromEnv() + if err != nil { + responseObj.ResponseCode = 1 + responseObj.ResponseDesc = "Error Creating Docker Client Env for : " + name + return responseObj, err + } + + ports := containerscheduler.GetBindingPortsForContainerCreate(name) + portBindings := make(map[docker.Port][]docker.PortBinding) + exposedPorts := map[docker.Port]struct{}{} + + if len(ports) > 0 { + for _, p := range ports { + s := strings.Split(p, ":") + extPort, intPort := s[0], s[1] + log.Println("extPort : " + extPort + " intPort : " + intPort) + dockerPort := docker.Port(intPort + "/tcp") + portBindings[dockerPort] = []docker.PortBinding{{HostIP: "0.0.0.0", HostPort: extPort}} + exposedPorts[dockerPort] = struct{}{} + } + } else { + responseObj.ResponseCode = 1 + responseObj.ResponseDesc = "No valid Binding Ports for :" + name + return responseObj, err + } + + container, err := client.CreateContainer(docker.CreateContainerOptions{ + Name: name, + Config: &docker.Config{ + Image: imageName, + ExposedPorts: exposedPorts, + }, + HostConfig: &docker.HostConfig{ + PortBindings: portBindings, + }, + }) + if err != nil { + responseObj.ResponseCode = 1 + responseObj.ResponseDesc = "Error Creating Container for :" + name + return responseObj, err + } + log.Println(container) + + responseObj.ResponseCode = 0 + responseObj.ContainerId = container.ID + responseObj.ResponseDesc = "Successfully Create The Container :" + name + return responseObj, err +} diff --git a/pkg/manager/dockerpull/dockerpull.go b/pkg/manager/dockerpull/dockerpull.go new file mode 100644 index 0000000..ef89fbe --- /dev/null +++ b/pkg/manager/dockerpull/dockerpull.go @@ -0,0 +1,82 @@ +package dockerpull + +import ( + docker "github.com/fsouza/go-dockerclient" + "log" + "strings" + "ukiyo/internal/containeraccess" + "ukiyo/internal/containerhistory" + "ukiyo/pkg/jencoder" + "ukiyo/pkg/webhook" +) + +type ResponseObj struct { + Name string + ImageName string + ResponseCode int + ResponseDesc string +} + +const ( + _eventTypeImagePuller = "Pull Image Event" +) + +var val int +var ImageName string + +func PullToDocker(str webhook.Response) (ResponseObj, error) { + var responseObj ResponseObj + log.Println("docker pull by repo name : " + jencoder.PrintJson(str)) + ImageName = str.RepoName + ":" + str.Tag + + client, err := docker.NewClientFromEnv() + if err != nil { + responseObj.ResponseCode = 1 + responseObj.ResponseDesc = "Error Creating Docker Client Env" + return responseObj, err + } + + err = client.PullImage(webhook.DockerPullImage(str), containeraccess.RequestLoginKeys(str.Namespace)) + if err != nil { + responseObj.ResponseCode = 1 + responseObj.ResponseDesc = "Error Pulling Images" + return responseObj, err + } + + imgs, err := client.ListImages(docker.ListImagesOptions{All: false}) + if err != nil { + responseObj.ResponseCode = 1 + responseObj.ResponseDesc = "Error List images" + return responseObj, err + } + + for _, img := range imgs { + for _, repo := range img.RepoTags { + if strings.EqualFold(repo, ImageName) { + val++ + log.Println("Successfully pull the images : " + repo) + } + } + } + + if val > 0 { + responseObj.ResponseCode = 0 + responseObj.Name = str.Name + responseObj.ImageName = ImageName + responseObj.ResponseDesc = "Successfully pull the images" + } else { + responseObj.ResponseCode = 0 + responseObj.ResponseDesc = "Failed pull the images" + } + + var data containerhistory.Data + data.EventType = _eventTypeImagePuller + data.EventObject.EventCode = responseObj.ResponseCode + data.EventObject.EventDesc = responseObj.ResponseDesc + data.EventObject.EventAt = 11111111 + data.EventData.ImagePuller.Name = str.Name + data.EventData.ImagePuller.ImageName = ImageName + containerhistory.UpdateContainerHistory(str.Name, data) + + return responseObj, err +} diff --git a/pkg/manager/dockerremove/dockerremove.go b/pkg/manager/dockerremove/dockerremove.go new file mode 100644 index 0000000..06a24f3 --- /dev/null +++ b/pkg/manager/dockerremove/dockerremove.go @@ -0,0 +1,60 @@ +package dockerremove + +import ( + docker "github.com/fsouza/go-dockerclient" + "log" + "strings" + "ukiyo/pkg/jencoder" +) + +type ResponseObj struct { + ResponseCode int + ResponseDesc string +} + +func RemoveRunningContainer(name string) (ResponseObj, error) { + var responseObj ResponseObj + log.Println("Starting Remove Running pods: " + name) + + client, err := docker.NewClientFromEnv() + if err != nil { + responseObj.ResponseCode = 1 + responseObj.ResponseDesc = "Error Creating Docker Client Env" + return responseObj, err + } + + imgs, err := client.ListContainers(docker.ListContainersOptions{All: false}) + if err != nil { + responseObj.ResponseCode = 1 + responseObj.ResponseDesc = "Error List Running Docker Services" + return responseObj, err + } + + if len(imgs) > 0 { + log.Println("List Running container ...." + jencoder.PrintJson(imgs)) + for _, img := range imgs { + if len(img.Names) > 0 { + for _, cName := range img.Names { + if strings.EqualFold(cName[1:len(cName)], name) { + err = client.StopContainer(img.ID, 0) + if err != nil { + responseObj.ResponseCode = 0 + responseObj.ResponseDesc = "Error getting Remove Running Container" + return responseObj, err + } else { + client.RemoveContainer(docker.RemoveContainerOptions{ID: img.ID}) + responseObj.ResponseCode = 0 + responseObj.ResponseDesc = "Successfully Remove Running Container" + return responseObj, err + } + } + } + } + } + } else { + responseObj.ResponseCode = 0 + responseObj.ResponseDesc = "No Running Container for Remove" + return responseObj, err + } + return responseObj, err +} diff --git a/pkg/manager/dockerrunner/dockerrunner.go b/pkg/manager/dockerrunner/dockerrunner.go new file mode 100644 index 0000000..9b82245 --- /dev/null +++ b/pkg/manager/dockerrunner/dockerrunner.go @@ -0,0 +1,36 @@ +package dockerrunner + +import ( + docker "github.com/fsouza/go-dockerclient" + "log" +) + +type ResponseObj struct { + ResponseCode int + ResponseDesc string +} + +func ContainerRunner(containerId string) (ResponseObj, error) { + var responseObj ResponseObj + log.Println("Starting container Runner : containerId: " + containerId) + client, err := docker.NewClientFromEnv() + if err != nil { + responseObj.ResponseCode = 1 + responseObj.ResponseDesc = "Error Running Docker Container Id: " + containerId + return responseObj, err + } + + err = client.StartContainer(containerId, &docker.HostConfig{ + PublishAllPorts: true, + }) + if err != nil { + log.Println(err) + responseObj.ResponseCode = 1 + responseObj.ResponseDesc = "Container Run failure :" + containerId + } else { + responseObj.ResponseCode = 0 + responseObj.ResponseDesc = "Successfully Run The Container :" + containerId + } + + return responseObj, err +} diff --git a/pkg/pullmanager/pullmanager.go b/pkg/pullmanager/pullmanager.go deleted file mode 100644 index dbb5f6d..0000000 --- a/pkg/pullmanager/pullmanager.go +++ /dev/null @@ -1,16 +0,0 @@ -package pullmanager - -import ( - "encoding/json" - "log" - "ukiyo/pkg/util" -) - -func PullToDocker(str util.PullObj) { - b, err := json.Marshal(str) - if err != nil { - log.Println(err) - return - } - log.Println("pull images : " + string(b)) -} diff --git a/pkg/pullmanager/pullmanager_test.go b/pkg/pullmanager/pullmanager_test.go deleted file mode 100644 index ee92fa8..0000000 --- a/pkg/pullmanager/pullmanager_test.go +++ /dev/null @@ -1 +0,0 @@ -package pullmanager diff --git a/pkg/pushmanager/pushmanager.go b/pkg/pushmanager/pushmanager.go deleted file mode 100644 index bbee683..0000000 --- a/pkg/pushmanager/pushmanager.go +++ /dev/null @@ -1 +0,0 @@ -package pushmanager diff --git a/pkg/pushmanager/pushmanager_test.go b/pkg/pushmanager/pushmanager_test.go deleted file mode 100644 index bbee683..0000000 --- a/pkg/pushmanager/pushmanager_test.go +++ /dev/null @@ -1 +0,0 @@ -package pushmanager diff --git a/pkg/scheduler/scheduler.go b/pkg/scheduler/scheduler.go index 6990da0..e597c6b 100644 --- a/pkg/scheduler/scheduler.go +++ b/pkg/scheduler/scheduler.go @@ -1 +1,49 @@ package scheduler + +import ( + "log" + "time" + "ukiyo/internal/containerscheduler" + "ukiyo/pkg/jencoder" + "ukiyo/pkg/manager/dockercreater" + "ukiyo/pkg/manager/dockerremove" + "ukiyo/pkg/manager/dockerrunner" +) + +func ScheduledRunner() { + pods := containerscheduler.QueryListRecodeInDB() + log.Println("ScheduledRunner trigger ...." + jencoder.PrintJson(pods)) + time.AfterFunc(1*time.Minute, func() { + Foo() + }) +} + +func WebHookScheduler(name string, imageName string) { + pod := containerscheduler.QueryRecodeInDB(name) + if len(pod.Name) > 0 && !pod.ScheduledDowntime { + + log.Println("Starting Container Remove - WebHookScheduler") + removeObj, _ := dockerremove.RemoveRunningContainer(name) + log.Println("Ending Container Remove - WebHookScheduler ...." + jencoder.PrintJson(removeObj)) + + if removeObj.ResponseCode == 0 { + + log.Println("WebHookScheduler trigger ContainerCreate...." + jencoder.PrintJson(pod)) + res, _ := dockercreater.ContainerCreate(name, imageName) + log.Println("WebHookScheduler trigger ContainerCreate...." + jencoder.PrintJson(res)) + + log.Println("Starting Container runner - WebHookScheduler") + resObj, _ := dockerrunner.ContainerRunner(res.ContainerId) + log.Println("Ending Container runner - WebHookScheduler ...." + jencoder.PrintJson(resObj)) + + } else { + log.Println("Stop Container runner - WebHookScheduler - Failed Running Container remove process") + } + } else { + log.Println("No saved pod details to schedule images") + } +} + +func Foo() { + log.Println("Foo run for more than a minute.") +} \ No newline at end of file diff --git a/pkg/scheduler/scheduler_test.go b/pkg/scheduler/scheduler_test.go deleted file mode 100644 index 6990da0..0000000 --- a/pkg/scheduler/scheduler_test.go +++ /dev/null @@ -1 +0,0 @@ -package scheduler diff --git a/pkg/util/util.go b/pkg/util/util.go deleted file mode 100644 index d53ec87..0000000 --- a/pkg/util/util.go +++ /dev/null @@ -1,8 +0,0 @@ -package util - -type PullObj struct { - RepoName string - Namespace string - Tag string - PushedDate int -} diff --git a/pkg/webhook-listener/webhook-listener.go b/pkg/webhook-listener/webhook-listener.go new file mode 100644 index 0000000..f4cbe88 --- /dev/null +++ b/pkg/webhook-listener/webhook-listener.go @@ -0,0 +1,84 @@ +package webhook_listener + +import ( + "log" + "net/http" + "ukiyo/pkg/jencoder" + "ukiyo/pkg/manager/dockerpull" + "ukiyo/pkg/scheduler" + "ukiyo/pkg/webhook" + + "github.com/gin-gonic/gin" +) + +type PushData struct { + PushedAt int `json:"pushed_at"` + Images []string `json:"images"` + Tag string `json:"tag"` + Pusher string `json:"pusher"` +} + +type Repository struct { + Status string `json:"status"` + Description string `json:"description"` + IsTrusted bool `json:"is_trusted"` + FullDescription string `json:"full_description"` + RepoUrl string `json:"repo_url"` + Owner string `json:"owner"` + IsOfficial bool `json:"is_official"` + IsPrivate bool `json:"is_private"` + Name string `json:"name"` + Namespace string `json:"namespace"` + StarCount int `json:"star_count"` + CommentCount int `json:"comment_count"` + DateCreated int `json:"date_created"` + RepoName string `json:"repo_name"` +} + +type DockerWebHook struct { + PushData PushData `json:"push_data"` + CallbackUrl string `json:"callback_url"` + Repository Repository `json:"repository"` +} + +func HooksListener(r *gin.Engine) { + r.POST("/ukiyo-web-hook", func(c *gin.Context) { + log.Println("Starting web hook trigger .... ############################") + var dockerWebHook DockerWebHook + c.ShouldBindJSON(&dockerWebHook) + log.Println("web-hook trigger" + jencoder.PrintJson(dockerWebHook)) + + res := SetWebHookResponse(dockerWebHook) + log.Println("Ending web hook trigger ...." + jencoder.PrintJson(res)) + + log.Println("Starting Image Pull .... ############################") + resObj, err := dockerpull.PullToDocker(res) + log.Println(err) + log.Println("Ending Image Pull ...." + jencoder.PrintJson(resObj)) + + if len(resObj.Name) > 0 { + log.Println("Trigger for check the Scheduler ....") + scheduler.WebHookScheduler(resObj.Name, resObj.ImageName) + } else { + log.Println("Error Creating Image ....") + } + + c.String(http.StatusOK, "OK") + }) +} + +func HealthCheck(r *gin.Engine) { + r.GET("/ping", func(c *gin.Context) { + c.String(http.StatusOK, "pong") + }) +} + +func SetWebHookResponse(dockerWebHook DockerWebHook) webhook.Response { + var response webhook.Response + response.Namespace = dockerWebHook.Repository.Namespace + response.RepoName = dockerWebHook.Repository.RepoName + response.Name = dockerWebHook.Repository.Name + response.Tag = dockerWebHook.PushData.Tag + response.PushedAt = dockerWebHook.PushData.PushedAt + return response +} diff --git a/pkg/webhook/webhook.go b/pkg/webhook/webhook.go new file mode 100644 index 0000000..59b2ca8 --- /dev/null +++ b/pkg/webhook/webhook.go @@ -0,0 +1,24 @@ +package webhook + +import docker "github.com/fsouza/go-dockerclient" + +type Response struct { + RepoName string + Namespace string + Name string + Tag string + PushedAt int +} + +func DockerPullImage(str Response) docker.PullImageOptions { + return docker.PullImageOptions{ + Repository: str.RepoName, + Tag: str.Tag, + Platform: "", + Registry: "", + OutputStream: nil, + RawJSONStream: false, + InactivityTimeout: 0, + Context: nil, + } +} diff --git a/pkg/webhooks/webhooks.go b/pkg/webhooks/webhooks.go deleted file mode 100644 index 86fa89e..0000000 --- a/pkg/webhooks/webhooks.go +++ /dev/null @@ -1,71 +0,0 @@ -package webhooks - -import ( - "encoding/json" - "log" - "net/http" - "ukiyo/pkg/pullmanager" - "ukiyo/pkg/util" - - "github.com/gin-gonic/gin" -) - -type PushData struct { - Pushed_at int `json:"pushed_at"` - images []string `json:"images"` - Tag string `json:"tag"` - Pusher string `json:"pusher"` -} - -type Repository struct { - Status string `json:"status"` - Description string `json:"description"` - IsTrusted bool `json:"is_trusted"` - FullDescription string `json:"full_description"` - RepoUrl string `json:"repo_url"` - Owner string `json:"owner"` - IsOfficial bool `json:"is_official"` - IsPrivate bool `json:"is_private"` - Name string `json:"name"` - Namespace string `json:"namespace"` - StarCount int `json:"star_count"` - CommentCount int `json:"comment_count"` - DateCreated int `json:"date_created"` - RepoName string `json:"repo_name"` -} - -type DockerWebHook struct { - PushData PushData `json:"push_data"` - CallbackUrl string `json:"callback_url"` - Repository Repository `json:"repository"` -} - -func HooksListener(r *gin.Engine) { - r.POST("/ukiyo-web-hook", func(c *gin.Context) { - var dockerWebHook DockerWebHook - c.ShouldBindJSON(&dockerWebHook) - - b, err := json.Marshal(dockerWebHook) - if err != nil { - log.Println(err) - return - } - - var pullObj util.PullObj - pullObj.Namespace = dockerWebHook.Repository.Namespace - pullObj.RepoName = dockerWebHook.Repository.RepoName - pullObj.Tag = dockerWebHook.PushData.Tag - pullObj.PushedDate = dockerWebHook.PushData.Pushed_at - - pullmanager.PullToDocker(pullObj) - - log.Println(string(b)) - c.String(http.StatusOK, "OK") - }) -} - -func HealthCheck(r *gin.Engine) { - r.GET("/ping", func(c *gin.Context) { - c.String(http.StatusOK, "pong") - }) -} diff --git a/pkg/webhooks/webhooks_test.go b/pkg/webhooks/webhooks_test.go deleted file mode 100644 index 66de495..0000000 --- a/pkg/webhooks/webhooks_test.go +++ /dev/null @@ -1,60 +0,0 @@ -package webhooks - -import ( - "net/http" - "net/http/httptest" - "strings" - "testing" - - "github.com/gin-gonic/gin" - "github.com/stretchr/testify/assert" -) - -func TestPingRoute(t *testing.T) { - r := gin.Default() - HealthCheck(r) - - w := httptest.NewRecorder() - req, _ := http.NewRequest("GET", "/ping", nil) - r.ServeHTTP(w, req) - - assert.Equal(t, http.StatusOK, w.Code) - assert.Equal(t, "pong", w.Body.String()) -} - -func TestHookRoute(t *testing.T) { - r := gin.Default() - HooksListener(r) - - pushJson := `{ - "push_data": { - "pushed_at": 1591175168, - "images": [], - "tag": "02", - "pusher": "dabare" - }, - "callback_url": "https://registry.hub.docker.com/u/dabare/testing/hook/22fbde3h00hi54a3jdf42j5cf44bebjf1/", - "repository": { - "status": "Active", - "description": "", - "is_trusted": false, - "full_description": "", - "repo_url": "https://hub.docker.com/r/dabare/testing", - "owner": "dabare", - "is_official": false, - "is_private": false, - "name": "testing", - "namespace": "dabare", - "star_count": 0, - "comment_count": 0, - "date_created": 1555438658, - "repo_name": "dabare/testing" - } - }` - - w := httptest.NewRecorder() - req, _ := http.NewRequest("POST", "/ukiyo-web-hook", strings.NewReader(pushJson)) - r.ServeHTTP(w, req) - assert.Equal(t, http.StatusOK, w.Code) - assert.Equal(t, "OK", w.Body.String()) -}