-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.go
154 lines (128 loc) · 4.4 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/weaveworks/common/server"
"github.com/weaveworks/launcher/pkg/text"
)
const (
defaultAgentManifest = "./static/agent.yaml"
installScriptFile = "./static/install.sh"
s3Bucket = "https://weaveworks-launcher.s3.amazonaws.com"
)
type templateData struct {
Scheme string
LauncherHostname string
WCHostname string
CRIEndpoint string
ReadOnly bool
}
var (
bootstrapVersion = flag.String("bootstrap-version", "", "Bootstrap version used for S3 binaries (commit hash)")
launcherHostname = flag.String("hostname", "get.weave.works", "Hostname for external launcher service")
wcHostname = flag.String("wcHostname", "cloud.weave.works", "Hostname for WC agents and users API")
scheme = flag.String("scheme", "https", "URL scheme for external launcher service")
bootstrapBaseURL = flag.String("bootstrap.base-url", s3Bucket, "Base URL the bootstrap binary should be fetched from")
agentManifest = flag.String("agent-manifest", defaultAgentManifest, "File used to load agent k8s")
serverCfg = server.Config{
MetricsNamespace: "service",
RegisterInstrumentation: true, // This gets ignored by the parsing below
}
)
func main() {
serverCfg.RegisterFlags(flag.CommandLine)
flag.Parse()
if *bootstrapVersion == "" {
log.Fatal("a bootstrap version is required")
}
if serverCfg.RegisterInstrumentation {
log.Printf("WARNING: RegisterInstrumentation is set. Do not run with this in production.")
}
// Load install.sh and agent.yaml into memory
data := &templateData{
Scheme: *scheme,
LauncherHostname: *launcherHostname,
WCHostname: *wcHostname,
CRIEndpoint: "",
}
installScriptData, err := loadData(installScriptFile, data)
if err != nil {
log.Fatal("error reading installScriptFile:", err)
}
handlers := &Handlers{
bootstrapVersion: *bootstrapVersion,
installScriptData: installScriptData,
templateData: data,
}
server, err := server.New(serverCfg)
if err != nil {
log.Fatal("error initialising server:", err)
}
defer server.Shutdown()
server.HTTP.HandleFunc("/", handlers.install).Methods("GET").Name("install")
server.HTTP.HandleFunc("/bootstrap", handlers.bootstrap).Methods("GET").Name("bootstrap")
server.HTTP.HandleFunc("/k8s/agent.yaml", handlers.agentYAML).Methods("GET").Name("agentYAML")
server.HTTP.Handle("/metrics", promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{
EnableOpenMetrics: true,
}))
server.Run()
}
func loadData(filename string, ctx *templateData) ([]byte, error) {
tmplData, err := ioutil.ReadFile(filename)
if err != nil {
return []byte{}, err
}
data, err := text.ResolveString(string(tmplData), ctx)
if err != nil {
return []byte{}, err
}
return []byte(data), nil
}
// Handlers contains the configuration for serving launcher related binaries
type Handlers struct {
bootstrapVersion string
installScriptData []byte
templateData *templateData
}
func (h *Handlers) install(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Disposition", "attachment; filename=\"install-weave-cloud.sh\"")
http.ServeContent(w, r, "install.sh", time.Time{}, bytes.NewReader(h.installScriptData))
}
func (h *Handlers) bootstrap(w http.ResponseWriter, r *http.Request) {
dist := r.URL.Query().Get("dist")
var filename string
switch dist {
case "darwin":
filename = "bootstrap_darwin_amd64"
case "linux":
filename = "bootstrap_linux_amd64"
default:
http.Error(w, "Invalid dist query parameter", http.StatusBadRequest)
return
}
http.Redirect(w, r, fmt.Sprintf("%s/bootstrap/%s/%s", *bootstrapBaseURL, h.bootstrapVersion, filename), 301)
}
func boolFromParam(r *http.Request, param string) bool {
v := r.URL.Query().Get(param)
if v == "true" {
return true
}
return false
}
func (h *Handlers) agentYAML(w http.ResponseWriter, r *http.Request) {
h.templateData.CRIEndpoint = r.URL.Query().Get("cri-endpoint")
h.templateData.ReadOnly = boolFromParam(r, "read-only")
agentManifestData, err := loadData(*agentManifest, h.templateData)
if err != nil {
log.Fatal("error reading agentYAMLFile:", err)
}
w.Header().Set("Content-Disposition", "attachment")
http.ServeContent(w, r, "agent.yaml", time.Time{}, bytes.NewReader(agentManifestData))
}