Enabled script trigger on post-request

This commit is contained in:
konrad 2017-04-29 15:16:46 +02:00 committed by konrad
parent 0f448136c9
commit 7b7b40a5c1
5 changed files with 87 additions and 28 deletions

7
form.htm Normal file
View File

@ -0,0 +1,7 @@
<form action="http://localhost:8080/" method="post">
<input type="hidden" name="payload" value='{
"secret":"Baum",
"ref":"feature"
}'/>
<input type="submit"/>
</form>

View File

@ -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=
secret=Baum
interface=":8080"
branch_name=refs/heads/master

View File

@ -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 "-----------------------------"
#echo "Finished at:"
#date
#echo "-----------------------------"

View File

@ -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.")
}
}
}
}

@ -0,0 +1 @@
Subproject commit 20d04065dbc6f79aac03bbfd49b6a9e0658f5842