Skip to content

Commit

Permalink
add most of hook content
Browse files Browse the repository at this point in the history
  • Loading branch information
pickledish committed Dec 12, 2023
1 parent 404d78c commit ab15afa
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ FROM ubuntu:22.04

WORKDIR /app

RUN apt-get update && apt-get install git ca-certificates -y && update-ca-certificates
RUN apt-get update && apt-get install -y git ca-certificates sshpass

RUN update-ca-certificates

COPY GeoLite2-Country.mmdb .

Expand Down
30 changes: 26 additions & 4 deletions cmd/git/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os/signal"
"strings"
"syscall"
"text/template"
"time"

"cbnr/util"
Expand Down Expand Up @@ -109,14 +110,35 @@ func RenderPostReceiveHookMiddleware(next http.Handler) http.Handler {

log.Printf("site ID found for repo %s, storage %s", repoName, storage[0])

hookValues := HookValues{
SiteId: repoName,
StorageHost: "storage.bunnycdn.com",
StorageName: storage[0].Name,
StorageToken: storage[0].Token,
}

hookPath := fmt.Sprintf("/tmp/%s/.git/hooks/post-receive", repoName)
hookData := []byte("#!/bin/sh\necho 'got it thank you'\ntouch /tmp/got\n")
err = os.WriteFile(hookPath, hookData, 0777)

tpl, err := template.New("naaaaame").Parse(HookTemplate)
if err != nil {
http.Error(out, fmt.Sprintf("Unable to render post-receive hook template: %v", err), http.StatusInternalServerError)
return
}

file, err := os.OpenFile(hookPath, os.O_RDWR|os.O_CREATE, 0777)
if err != nil {
log.Printf("THIS DID NOT WORK: %s", err)
http.Error(out, fmt.Sprintf("Could not create and open post-receive hook file: %v", err), http.StatusInternalServerError)
return
}

defer file.Close()

err = tpl.Execute(file, hookValues)
if err != nil {
http.Error(out, fmt.Sprintf("Could not render template into hook file: %v", err), http.StatusInternalServerError)
return
}

// user is allowed to push to this site ID, pass it through
next.ServeHTTP(out, req)
return
})
Expand Down
32 changes: 32 additions & 0 deletions cmd/git/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package git

type HookValues struct {
SiteId string
StorageHost string
StorageName string
StorageToken string
}

var HookTemplate = `#!/bin/bash
set -euo pipefail
echo "hi"
echo "you just pushed to {{.SiteId}}..."
echo "deleting current content"
curl -X "DELETE" -H "AccessKey: {{.StorageToken}}" "https://{{.StorageHost}}/{{.StorageName}}/"
echo "listing files..."
git checkout main
for FILENAME in $(git ls-files);
do
echo "Uploading $FILENAME of size $(stat --printf='%s' $FILENAME) bytes..."
sshpass -p {{.StorageToken}} sftp -q -o StrictHostKeyChecking=no {{.StorageName}}@{{.StorageHost}} <<< $"@put ${FILENAME}"
done
echo "all uploaded goodbye"
`
5 changes: 5 additions & 0 deletions toast.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ image: golang:1.21
command_prefix: set -euxo pipefail
location: /scratch
tasks:
sshpass:
command: |
apt-get update
apt-get install -y sshpass
deps:
input_paths:
- go.mod
Expand All @@ -12,6 +16,7 @@ tasks:
# I would like to cache this but go-build files prevent it
cache: false
dependencies:
- sshpass
- deps
environment:
GOCACHE: /scratch/go-build
Expand Down

0 comments on commit ab15afa

Please sign in to comment.