From 7b7b40a5c1b9ff4a04c5a3af6f19d79067c2ae1f Mon Sep 17 00:00:00 2001 From: konrad Date: Sat, 29 Apr 2017 15:16:46 +0200 Subject: [PATCH] Enabled script trigger on post-request --- form.htm | 7 +++ src/Simple-Git-Deploy/config.ini | 4 +- src/Simple-Git-Deploy/deploy.sh | 20 ++++---- src/Simple-Git-Deploy/main.go | 83 ++++++++++++++++++++++++------- src/github.com/go-playground/form | 1 + 5 files changed, 87 insertions(+), 28 deletions(-) create mode 100644 form.htm create mode 160000 src/github.com/go-playground/form diff --git a/form.htm b/form.htm new file mode 100644 index 0000000..dc05c7d --- /dev/null +++ b/form.htm @@ -0,0 +1,7 @@ +
+ + +
\ No newline at end of file diff --git a/src/Simple-Git-Deploy/config.ini b/src/Simple-Git-Deploy/config.ini index 7bfd125..f3d9cf8 100644 --- a/src/Simple-Git-Deploy/config.ini +++ b/src/Simple-Git-Deploy/config.ini @@ -2,4 +2,6 @@ ssh_key=/home/konrad/Schreibtisch/life-wob/life-deploy git_url=ssh://git@git.mowie.cc:9022/Websites/Life-Homepage.git deploy_dir=/home/konrad/Schreibtisch/life-wob/html container_name= -secret= \ No newline at end of file +secret=Baum +interface=":8080" +branch_name=refs/heads/master \ No newline at end of file diff --git a/src/Simple-Git-Deploy/deploy.sh b/src/Simple-Git-Deploy/deploy.sh index 11fc27a..8967a9b 100755 --- a/src/Simple-Git-Deploy/deploy.sh +++ b/src/Simple-Git-Deploy/deploy.sh @@ -10,13 +10,13 @@ while getopts ':k:g:d:c:' OPTION ; do esac done -echo "-----------------------------" -echo "Started at:" -date -echo "Key: ${KEY}" -echo "Git-Url: ${GIT_URL}" -echo "Deploy-Dir: ${DEPLOY_DIR}" -echo "----------------------------" +#echo "-----------------------------" +#echo "Started at:" +#date +#echo "Key: ${KEY}" +#echo "Git-Url: ${GIT_URL}" +#echo "Deploy-Dir: ${DEPLOY_DIR}" +#echo "----------------------------" #Check for Key if [ -z "$KEY" ] @@ -54,6 +54,6 @@ if [ ! -z "$DCONTAINER_NAME" ] docker restart $DCONTAINER_NAME fi -echo "Finished at:" -date -echo "-----------------------------" \ No newline at end of file +#echo "Finished at:" +#date +#echo "-----------------------------" \ No newline at end of file diff --git a/src/Simple-Git-Deploy/main.go b/src/Simple-Git-Deploy/main.go index 39bbe36..f4a085d 100644 --- a/src/Simple-Git-Deploy/main.go +++ b/src/Simple-Git-Deploy/main.go @@ -2,37 +2,86 @@ package main import ( "fmt" - "os/exec" + //"os/exec" "github.com/go-ini/ini" "log" + "net/http" + //"encoding/json" + //"os/exec" + //"github.com/go-playground/form" + //"encoding/json" + "github.com/go-playground/form" + "io/ioutil" + "encoding/json" + "net/url" + "os/exec" ) type Config struct { - SSH_Key string - Git_url string - Deploy_dir string + SSH_Key string + Git_url string + Deploy_dir string Container_name string - Secret string + Secret string + Interface string +} + +func config(key string) string { + cfg, _ := ini.Load("config.ini") + return cfg.Section("").Key(key).String() +} + +type Payload struct { + Secret string `json:"secret"` + Ref string `json:"ref"` } func main() { - //Init Config - cfg, _ := ini.Load("config.ini") - var Conf Config - Conf.SSH_Key = cfg.Section("").Key("ssh_key").String() - Conf.Deploy_dir = cfg.Section("").Key("deploy_dir").String() - Conf.Git_url = cfg.Section("").Key("git_url").String() - Conf.Container_name = cfg.Section("").Key("container_name").String() - Conf.Secret = cfg.Section("").Key("secret").String() + + if config("git_url") != "" && config("deploy_dir") != "" && config("ssh_key") != "" { - if Conf.Git_url != "" && Conf.Deploy_dir != "" && Conf.SSH_Key != "" { - cmd, err := exec.Command("/bin/bash", "deploy.sh", "-k", Conf.SSH_Key, "-g", Conf.Git_url, "-d", Conf.Deploy_dir, "-c", Conf.Container_name).Output() + // Server + http.HandleFunc("/", handleHook) + err := http.ListenAndServe(config("interface"), nil) // setting listening port if err != nil { - log.Fatal(err) + log.Fatal("ListenAndServe: ", err) } - fmt.Printf("%s\n", cmd) } else { fmt.Println("You must provide at least a Git-Url, Deploy-Dir and SSH-Key!") } } + +var decoder *form.Decoder + +func handleHook(w http.ResponseWriter, r *http.Request) { + if r.Method == "POST" { + body, err := ioutil.ReadAll(r.Body) + if err != nil { + log.Printf("error reading response: %v", err) + } + + encoded, _ := url.QueryUnescape(string(body[8:])) + //log.Println(encoded) + + var hook_data Payload + err = json.Unmarshal([]byte(encoded), &hook_data) + if err != nil { + log.Printf("error decoding response: %v", err) + } + log.Println("Recived payload, Secret:", hook_data.Secret, ", Ref:", hook_data.Ref) + + if hook_data.Secret == config("secret") { + if hook_data.Ref == config("branch_name") || hook_data.Ref == "refs/heads/" + config("secret") { + log.Println("Recived corresponding secret: ", hook_data.Secret) + log.Println("Starting update...") + cmd, err := exec.Command("/bin/bash", "deploy.sh", "-k", config("ssh_key"), "-g", config("git_url"), "-d", config("deploy_dir"), "-c", config("container_name")).Output() + if err != nil { + log.Fatal(err) + } + log.Printf("%s\n", cmd) + log.Println("Finished update.") + } + } + } +} diff --git a/src/github.com/go-playground/form b/src/github.com/go-playground/form new file mode 160000 index 0000000..20d0406 --- /dev/null +++ b/src/github.com/go-playground/form @@ -0,0 +1 @@ +Subproject commit 20d04065dbc6f79aac03bbfd49b6a9e0658f5842