/
setup.go
229 lines (189 loc) · 5.29 KB
/
setup.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package infra
import (
"context"
"errors"
"fmt"
"io"
"strings"
"google.golang.org/api/compute/v1"
"google.golang.org/api/dns/v1"
"github.com/orijtech/frontender"
"github.com/odeke-em/go-uuid"
)
// Goals:
// 1. Lookup already created domain with zone
// a) Zone Name
// b) ZoneID
//
// 2. If proxy server address is provided, well and good
// otherwise create a single machine instance:
// a) Machine Type, CPU cores
// b) Get IPv4 address
//
// 3. Make record sets
// a) A Record
// + DNS Name: --> Name, IPV4 Address
// b) CNAMES ... --> Map to A Record
//
// 4. Deploy frontender server with:
// Domains --> Record set DNS names.
type Setup struct {
Project string `json:"project"`
Zone string `json:"zone"`
ProjectDescription string `json:"project_description"`
MachineName string `json:"machine_name"`
DomainName string `json:"domainname"`
IPV4Addresses []string `json:"ipv4_addresses"`
Aliases []string `json:"aliases"`
ProxyAddress string `json:"proxy_address"`
Environ []string `json:"environ"`
TargetGOOS string `json:"target_goos"`
}
var (
errEmptyDomainName = errors.New("expecting a non-empty domain name")
)
func (req *Setup) Validate() error {
if req == nil || strings.TrimSpace(req.Project) == "" {
return errEmptyProject
}
if strings.TrimSpace(req.Zone) == "" {
return errEmptyZone
}
if req.DomainName == "" {
return errEmptyDomainName
}
return nil
}
func (c *Client) generateMachineAndIPV4Addresses(ctx context.Context, req *Setup) ([]string, error) {
instance, err := c.generateMachine(ctx, req)
if err != nil {
return nil, err
}
if len(instance.NetworkInterfaces) == 0 {
// Now fetch them directly
instance, err = c.FindInstance(ctx, &InstanceRequest{
Project: req.Project,
Zone: req.Zone,
Name: req.MachineName,
})
if err != nil {
// TODO: Rollback the instance?
}
}
return ipv4AddressesFromInstance(instance), nil
}
func ipv4AddressesFromInstance(instance *compute.Instance) []string {
var ipv4Addresses []string
for _, netInterface := range instance.NetworkInterfaces {
ipv4Addresses = append(ipv4Addresses, netInterface.NetworkIP)
}
return ipv4Addresses
}
func (c *Client) generateMachine(ctx context.Context, req *Setup) (*compute.Instance, error) {
return c.CreateInstance(ctx, &InstanceRequest{
Description: req.ProjectDescription,
Project: req.Project,
Zone: req.Zone,
Name: req.MachineName,
NetworkInterface: BasicExternalNATNetworkInterface,
})
}
func (c *Client) generateRecordSets(ctx context.Context, req *Setup, ipv4Addresses ...string) (*dns.Change, error) {
ireq := &UpdateRequest{
Project: req.Project,
Zone: req.Zone,
Records: []*Record{
{
Type: AName, DNSName: req.DomainName,
IPV4Addresses: ipv4Addresses[:],
},
},
}
for _, alias := range req.Aliases {
ireq.Records = append(ireq.Records, &Record{
Type: CName,
DNSName: alias,
CanonicalName: req.DomainName,
})
}
return c.AddRecordSets(ctx, ireq)
}
func stripTrailingDot(s string) string { return strings.TrimSuffix(s, ".") }
func recordSetsToDomainNames(recordSets []*dns.ResourceRecordSet, fn func(string) string) []string {
var domainNames []string
for _, rset := range recordSets {
stripped := stripTrailingDot(rset.Name)
if fn != nil {
stripped = fn(stripped)
}
domainNames = append(domainNames, stripped)
}
return domainNames
}
func httpsify(s string) string {
if strings.HasPrefix(s, "https://") {
return s
}
s = strings.TrimPrefix(s, "http://")
return "https://" + s
}
func (c *Client) FullSetup(ctx context.Context, req *Setup) (*SetupResponse, error) {
if err := req.Validate(); err != nil {
return nil, err
}
ipv4Addresses := req.IPV4Addresses
if len(ipv4Addresses) == 0 {
// Time to generate that server
var err error
ipv4Addresses, err = c.generateMachineAndIPV4Addresses(ctx, req)
if err != nil {
return nil, err
}
}
// Now create that DNS mapping:
dnsChange, err := c.generateRecordSets(ctx, req, ipv4Addresses...)
if err != nil {
return nil, err
}
// Now convert the DNS change additions to https based domains
httpsDomains := recordSetsToDomainNames(dnsChange.Additions, httpsify)
nonHTTPSRedirectURL := httpsify(req.DomainName)
// Now generate the binary
rc, err := frontender.GenerateBinary(&frontender.DeployInfo{
FrontendConfig: &frontender.Request{
Domains: httpsDomains,
Environ: req.Environ[:],
TargetGOOS: req.TargetGOOS,
ProxyAddresses: []string{req.ProxyAddress},
NonHTTPSRedirectURL: nonHTTPSRedirectURL,
},
})
if err != nil {
return nil, err
}
// Now upload the binary
obj, err := c.UploadWithParams(ctx, &UploadParams{
Project: req.Project,
Public: true,
Bucket: "frontender-binaries",
Name: fmt.Sprintf("generated-binary-%s", uuid.NewRandom()),
Reader: func() io.Reader { return rc },
})
_ = rc.Close()
if err != nil {
return nil, err
}
resp := &SetupResponse{
BinaryURL: ObjectURL(obj),
DNSAdditions: dnsChange.Additions,
Domains: httpsDomains,
NonHTTPSRedirectURL: nonHTTPSRedirectURL,
}
return resp, nil
}
type SetupResponse struct {
BinaryURL string `json:"binary_url"`
Domains []string `json:"domains"`
DNSAdditions []*dns.ResourceRecordSet `json:"dns_additions"`
NonHTTPSRedirectURL string `json:"non_https_redirect_url"`
}