Skip to content

Commit

Permalink
now changing the directory where the repo is downloaded
Browse files Browse the repository at this point in the history
  • Loading branch information
kneerunjun committed Mar 22, 2024
1 parent 9e95d35 commit 81e9a9e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 5 deletions.
4 changes: 3 additions & 1 deletion dev.env
Original file line number Diff line number Diff line change
Expand Up @@ -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
APPNAME=AutoDeploy
REPONAME=autoupdate
DIRREPO_HOST=/root/source/github.com/eensymachines-in/patio-web
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}"]
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down
48 changes: 45 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package main
import (
"fmt"
"net/http"
"os"

"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)

type ReleaseInfo struct {
Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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")
Expand Down

0 comments on commit 81e9a9e

Please sign in to comment.