From 81e9a9e44d3b5e48cf53f2a23d1f2fdb1c40c0e5 Mon Sep 17 00:00:00 2001 From: Niranjan Date: Fri, 22 Mar 2024 18:24:10 +0530 Subject: [PATCH] now changing the directory where the repo is downloaded --- dev.env | 4 +++- docker-compose.yml | 3 +++ go.mod | 5 ++++- go.sum | 3 +++ main.go | 48 +++++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 58 insertions(+), 5 deletions(-) diff --git a/dev.env b/dev.env index 858b241..8689a21 100644 --- a/dev.env +++ b/dev.env @@ -3,4 +3,6 @@ LOGDIR=/var/log/eensy/autoupdt RUNDIR=/var/run/eensy/autoupdt ETCDIR=/etc/eensy/autoupdt BINDIR=/usr/bin/eensy/autoupdt -APPNAME=autoupdt \ No newline at end of file +APPNAME=AutoDeploy +REPONAME=autoupdate +DIRREPO_HOST=/root/source/github.com/eensymachines-in/patio-web \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 15e58e3..0a28233 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -15,10 +15,13 @@ services: - 8082:8082 volumes: - .:${SRCDIR} + - /var/autodeploy/repo:${DIRREPO_HOST} environment: - FLOG=0 - SILENT=0 - GINMODE=DEBUG + - REPO_NAME=${REPONAME} + - REPO_DIR=/var/autodeploy/repo container_name: ctn_gin stdin_open: true entrypoint: ["${BINDIR}/${APPNAME}"] diff --git a/go.mod b/go.mod index 51bc7dc..82c0b70 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,10 @@ module github.com/eensymachines-in/autoupdate go 1.21.0 -require github.com/gin-gonic/gin v1.9.1 +require ( + github.com/gin-gonic/gin v1.9.1 + github.com/sirupsen/logrus v1.9.3 +) require ( github.com/bytedance/sonic v1.9.1 // indirect diff --git a/go.sum b/go.sum index 1a77fa1..6f00312 100644 --- a/go.sum +++ b/go.sum @@ -45,6 +45,8 @@ github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZ github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4= 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/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= @@ -68,6 +70,7 @@ golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0 golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/main.go b/main.go index 3d1f676..7896310 100644 --- a/main.go +++ b/main.go @@ -3,8 +3,10 @@ package main import ( "fmt" "net/http" + "os" "github.com/gin-gonic/gin" + log "github.com/sirupsen/logrus" ) type ReleaseInfo struct { @@ -30,6 +32,11 @@ type WebHkRelease struct { // payload we receive from web hook notification Sender SenderInfo `json:"sender"` } +var ( + REPO_NAME = "" // expected repository name + REPO_DIR_ONHOST = "" // directory on the host where the git repo is located +) + func CORS(c *gin.Context) { // First, we add the headers with need to enable CORS // Make sure to adjust these headers to your needs @@ -47,9 +54,30 @@ func CORS(c *gin.Context) { // request using any other method than OPTIONS c.AbortWithStatus(http.StatusOK) } + +} + +func init() { + log.SetFormatter(&log.TextFormatter{DisableColors: false, FullTimestamp: false}) + log.SetReportCaller(false) + + REPO_NAME = os.Getenv("REPO_NAME") + if REPO_NAME == "" { + log.Panic("Empty environment variable: REPO_NAME") + } + REPO_DIR_ONHOST = os.Getenv("REPO_DIR") + if REPO_NAME == "" { + log.Panic("Empty environment variable: REPO_DIR_ONHOST") + } } func main() { + log.Info("=========") + log.Info("Starting the auto deploy service..") + log.Info("=========") + + defer log.Warn("Now closing the patio-web program...") + gin.SetMode(gin.DebugMode) r := gin.Default() api := r.Group("/api") @@ -58,15 +86,29 @@ func main() { res := WebHkRelease{} err := c.ShouldBind(&res) defer c.Request.Body.Close() - if err != nil { fmt.Println("Error unmarshaling the payload") fmt.Println(err) c.AbortWithStatus(http.StatusInternalServerError) return } - fmt.Println("received payload from github..") - fmt.Println(res) + if REPO_NAME != res.Repository.Name { + log.WithFields(log.Fields{ + "expected": REPO_NAME, + "got": res.Repository.Name, + }).Error("Repository name isnt as expected, did you change the repository name upstream?") + c.AbortWithStatus(http.StatusOK) //but send back 200 ok to the server, acknowledge + return + } + // changing the current directory before executing the other bash scripts + if err := os.Chdir(REPO_DIR_ONHOST); err != nil { + log.WithFields(log.Fields{ + "expected": REPO_DIR_ONHOST, + "err": err, + }).Error("Error changing to the working directory on the host") + c.AbortWithStatus(http.StatusOK) //but send back 200 ok to the server, acknowledge + return + } c.AbortWithStatus(http.StatusOK) }) r.Run(":8082")