-
Notifications
You must be signed in to change notification settings - Fork 2
/
pushy.go
106 lines (92 loc) · 2.96 KB
/
pushy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/exec"
)
var config Configuration
var configPath *string = flag.String("config", "/etc/pushy.json", "path to configuration JSON file")
var logPath *string = flag.String("log", "/var/log/pushy.log", "path to log file")
func main() {
// Parse command-line options
flag.Parse()
// Load configuration file
configFile, err := os.Open(*configPath)
if err != nil {
log.Fatal(err)
}
if err := json.NewDecoder(configFile).Decode(&config); err != nil {
log.Fatal(err)
}
log.Printf("Loaded configuration: %+v", config)
if err := configFile.Close(); err != nil {
log.Fatal(err)
}
// Create a log file and redirect output there
logFile, err := os.OpenFile(*logPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
defer func() {
if err := logFile.Close(); err != nil {
log.Fatal(err)
}
}()
if err != nil {
log.Fatal(err)
}
log.SetOutput(logFile)
// Make sure we have git on this computer
if _, err := exec.LookPath("git"); err != nil {
log.Fatalf("No git binary found. error: %v", err)
}
// Drop Privileges
if err := DropPrivileges(config.User, config.Group); err != nil {
log.Fatal(err)
}
// Start serving
http.HandleFunc("/", handler)
http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil)
}
func handler(w http.ResponseWriter, r *http.Request) {
payloadData := []byte(r.FormValue("payload"))
var payload Payload
json.Unmarshal(payloadData, &payload)
if repoConfig := config.FindRepositoryConfig(payload.Repository); repoConfig != nil {
log.Printf("Repository configuration found: %+v", repoConfig)
pull(repoConfig)
} else {
log.Printf("ERROR: Repository configuration not found for: %+v", payload.Repository)
http.Error(w, "Not configured for that repository", 400)
}
}
func pull(repositoryConfig *RepositoryConfig) {
// save the present working directory; cd back to it once this method is done
log.Printf("Changing working directory to %s", repositoryConfig.Path)
if oldWorkingDirectory, err := os.Getwd(); err != nil {
log.Printf("WARNING: could not get working directory. error: %v", err)
} else {
defer os.Chdir(oldWorkingDirectory)
}
// cd to the repository directory
if err := os.Chdir(repositoryConfig.Path); err != nil {
log.Printf("WARNING: could not change directory; skipping repository update. error: %v", err)
return
}
// git fetch origin <branch>
fetch := exec.Command("git", "fetch", "origin", repositoryConfig.Branch)
log.Printf("Running fetch command: %+v", fetch.Args)
if err := fetch.Run(); err != nil {
log.Printf("ERROR: problem running fetch command. error: %+v", err)
return
}
// git checkout -f origin/<branch>
checkout := exec.Command("git", "checkout", "-f", fmt.Sprint("origin/", repositoryConfig.Branch))
log.Printf("Running checkout command: %+v", checkout.Args)
if err := checkout.Run(); err != nil {
log.Printf("ERROR: problem running checkout command. error: %+v", err)
return
}
log.Printf("Repository updated!")
}