-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
158 lines (132 loc) · 3.78 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
155
156
157
158
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package main
import (
"context"
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"sync"
"time"
"github.com/gorilla/mux"
"google.golang.org/protobuf/encoding/protojson"
"namespacelabs.dev/foundation/internal/reverseproxy"
"namespacelabs.dev/foundation/std/development/controller/admin"
"namespacelabs.dev/foundation/std/development/filesync"
)
var (
serializedConf = flag.String("configuration", "", "Serialized configuration in JSON.")
)
func main() {
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
flag.Parse()
configuration := &admin.Configuration{}
if err := protojson.Unmarshal([]byte(*serializedConf), configuration); err != nil {
log.Fatal(err)
}
if configuration.PackageBase == "" {
log.Fatal("package_base is missing")
}
for _, backend := range configuration.Backend {
if len(backend.GetExecution().GetArgs()) == 0 {
log.Fatal("backend.execution.args can't be empty")
}
}
ctx := context.Background()
running := &running{configuration: configuration, processes: map[int]runningProcess{}}
r := mux.NewRouter()
for index, backend := range configuration.Backend {
running.restartBackend(index)
if httpPass := backend.HttpPass; httpPass != nil {
if configuration.RevproxyPort == 0 {
log.Fatal("http_pass without revproxy_port configuration")
}
r.PathPrefix(httpPass.UrlPrefix).
Handler(reverseproxy.Make(httpPass.Backend, reverseproxy.DefaultLocalProxy()))
}
}
if configuration.FilesyncPort > 0 {
go func() {
filesync.StartFileSyncServer(&filesync.FileSyncConfiguration{
RootDir: configuration.PackageBase,
Port: configuration.FilesyncPort,
})
}()
}
if configuration.RevproxyPort > 0 {
httpHost := fmt.Sprintf("0.0.0.0:%d", configuration.RevproxyPort)
srv := &http.Server{
Handler: r,
Addr: httpHost,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
BaseContext: func(l net.Listener) context.Context { return ctx },
}
log.Printf("ReverseProxy listening on %s", httpHost)
if err := srv.ListenAndServe(); err != nil {
log.Fatal(err)
}
} else {
select {}
}
}
func makeExecution(ctx context.Context, configuration *admin.Configuration, packageName string, execution *admin.Execution) *exec.Cmd {
cmd := exec.CommandContext(ctx, execution.Args[0], execution.Args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = filepath.Join(configuration.PackageBase, packageName)
cmd.Env = append(os.Environ(), execution.AdditionalEnv...)
return cmd
}
type running struct {
configuration *admin.Configuration
mu sync.Mutex
processes map[int]runningProcess
}
type runningProcess struct {
cmd *exec.Cmd
cancel func()
doneSignal chan struct{}
}
func (r *running) restartBackend(index int) {
r.mu.Lock()
existing, ok := r.processes[index]
r.mu.Unlock()
if ok {
existing.cancel()
for range existing.doneSignal {
// Wait until Wait() returns.
}
}
r.mu.Lock()
defer r.mu.Unlock()
if _, ok := r.processes[index]; ok {
// Someone beat us and already started it.
return
}
backend := r.configuration.Backend[index]
ctx, done := context.WithCancel(context.Background())
cmd := makeExecution(ctx, r.configuration, backend.PackageName, backend.Execution)
if err := cmd.Start(); err != nil {
log.Fatal("cmd.Start failed", index, err)
}
log.Printf("Started backend index %d.", index)
ch := make(chan struct{})
go func() {
defer close(ch)
if err := cmd.Wait(); err != nil {
log.Fatal("cmd.Wait failed", index, err)
}
}()
r.processes[index] = runningProcess{
cmd: cmd,
cancel: done,
doneSignal: ch,
}
}