-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathmain.go
129 lines (112 loc) · 3.46 KB
/
main.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Contributor: Julien Vehent [email protected] [:ulfr]
package main
//go:generate ./version.sh
import (
"fmt"
"log"
"net/http"
"os/exec"
"path/filepath"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/elasticbeanstalk"
"github.com/gorilla/mux"
)
type deployer struct {
}
func main() {
var dplr deployer
// register routes
r := mux.NewRouter()
r.HandleFunc("/dockerhub", dplr.postWebHook).Methods("POST")
r.HandleFunc("/__heartbeat__", getHeartbeat).Methods("GET")
r.HandleFunc("/__version__", getVersion).Methods("GET")
// all set, start the http handler
log.Fatal(http.ListenAndServe(":8080", r))
}
func (dplr *deployer) postWebHook(w http.ResponseWriter, r *http.Request) {
log.Println("Received webhook notification")
hookData, err := NewDockerHubWebhookDataFromRequest(r)
if err != nil {
httpError(w, http.StatusInternalServerError, "Failed to initialize DockerHub Webhook parser")
return
}
log.Printf("%+v", hookData)
// This application only accepts containers placed under the
// `securingdevops` dockerhub organization. If this wasn't an
// example application, we would make the namespacing configurable
if hookData.Repository.Namespace != `securingdevops` {
httpError(w, http.StatusUnauthorized, "Invalid namespace")
return
}
err = hookData.Callback(NewSuccessCallbackData())
if err != nil {
httpError(w, http.StatusUnauthorized, "Request could not be validated")
return
}
log.Println("Verified notification authenticity")
go testAndDeploy()
w.Write([]byte("OK"))
}
func testAndDeploy() {
testFiles, err := filepath.Glob("/app/deploymentTests/*")
if err != nil {
panic(err)
}
var do_deploy = true
for _, testFile := range testFiles {
log.Println("Executing test", testFile)
out, err := exec.Command(testFile).Output()
if err != nil {
log.Printf("Test %s failed:\n%s\n%s", testFile, err, out)
do_deploy = false
} else {
log.Printf("Test %s succeeded: %s", testFile, out)
}
}
if do_deploy {
deploy()
}
}
func deploy() {
svc := elasticbeanstalk.New(
session.New(),
&aws.Config{Region: aws.String("us-east-1")},
)
params := &elasticbeanstalk.UpdateEnvironmentInput{
ApplicationName: aws.String("invoicer201707071231"),
EnvironmentId: aws.String("e-y8ubep55hp"),
VersionLabel: aws.String("invoicer-api"),
}
resp, err := svc.UpdateEnvironment(params)
if err != nil {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
log.Println(err)
return
}
// Pretty-print the response data.
log.Println("Deploying EBS application:", params)
log.Println(resp)
}
func getHeartbeat(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("I am alive"))
}
// handleVersion returns the current version of the API
func getVersion(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(fmt.Sprintf(`{
"source": "https://github.com/Securing-DevOps/deployer",
"version": "%s",
"commit": "%s",
"build": "https://circleci.com/gh/Securing-DevOps/deployer/"
}`, version, commit)))
}
func httpError(w http.ResponseWriter, errorCode int, errorMessage string, args ...interface{}) {
log.Printf("%d: %s", errorCode, fmt.Sprintf(errorMessage, args...))
http.Error(w, fmt.Sprintf(errorMessage, args...), errorCode)
return
}