Skip to content

Commit 6a0d1cb

Browse files
committed
yaaay
1 parent d7da927 commit 6a0d1cb

File tree

3 files changed

+84
-456
lines changed

3 files changed

+84
-456
lines changed

Makefile

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,17 @@ site/node_modules/.installed: site/package.json
399399
cd site/
400400
../scripts/pnpm_install.sh
401401

402-
site/out/index.html: site/node_modules/.installed $(shell find ./site $(FIND_EXCLUSIONS) -type f \( -name '*.ts' -o -name '*.tsx' \))
402+
SITE_GEN_FILES := \
403+
site/src/api/typesGenerated.ts \
404+
site/src/api/rbacresourcesGenerated.ts \
405+
site/src/api/countriesGenerated.ts \
406+
site/src/theme/icons.json
407+
408+
site/out/index.html: \
409+
site/node_modules/.installed \
410+
site/static/install.sh \
411+
$(SITE_GEN_FILES) \
412+
$(shell find ./site $(FIND_EXCLUSIONS) -type f \( -name '*.ts' -o -name '*.tsx' \))
403413
cd site/
404414
# prevents this directory from getting to big, and causing "too much data" errors
405415
rm -rf out/assets/
@@ -542,22 +552,20 @@ GEN_FILES := \
542552
vpn/vpn.pb.go \
543553
coderd/database/dump.sql \
544554
$(DB_GEN_FILES) \
545-
site/src/api/typesGenerated.ts \
555+
$(SITE_GEN_FILES) \
546556
coderd/rbac/object_gen.go \
547557
codersdk/rbacresources_gen.go \
548-
site/src/api/rbacresourcesGenerated.ts \
549-
site/src/api/countriesGenerated.ts \
550558
docs/admin/integrations/prometheus.md \
551559
docs/reference/cli/index.md \
552560
docs/admin/security/audit-logs.md \
553561
coderd/apidoc/swagger.json \
554562
provisioner/terraform/testdata/version \
555563
site/e2e/provisionerGenerated.ts \
556-
site/src/theme/icons.json \
557564
examples/examples.gen.json \
558565
$(TAILNETTEST_MOCKS) \
559566
coderd/database/pubsub/psmock/psmock.go
560567

568+
561569
# all gen targets should be added here and to gen/mark-fresh
562570
gen: $(GEN_FILES)
563571
.PHONY: gen

site/site.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ func New(opts *Options) *Handler {
160160
handler.buildInfoJSON = html.EscapeString(string(buildInfoResponse))
161161
handler.handler = mux.ServeHTTP
162162

163+
handler.installScript, err = parseInstallScript(opts.SiteFS, opts.BuildInfo)
164+
if err != nil {
165+
panic("failed to parse install script")
166+
}
167+
163168
return handler
164169
}
165170

@@ -169,8 +174,8 @@ type Handler struct {
169174
secureHeaders *secure.Secure
170175
handler http.HandlerFunc
171176
htmlTemplates *template.Template
172-
173177
buildInfoJSON string
178+
installScript []byte
174179

175180
// RegionsFetcher will attempt to fetch the more detailed WorkspaceProxy data, but will fall back to the
176181
// regions if the user does not have the correct permissions.
@@ -217,6 +222,11 @@ func (h *Handler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
217222
// If the asset does not exist, this will return a 404.
218223
h.handler.ServeHTTP(rw, r)
219224
return
225+
// If requesting the install.sh script, fill in the template with the
226+
// appropriate hostname and version info.
227+
case reqFile == "install.sh":
228+
h.serveInstallScript(rw, r)
229+
return
220230
// If the original file path exists we serve it.
221231
case h.exists(reqFile):
222232
if ShouldCacheFile(reqFile) {
@@ -306,6 +316,11 @@ func ShouldCacheFile(reqFile string) bool {
306316
return true
307317
}
308318

319+
func (h *Handler) serveInstallScript(rw http.ResponseWriter, r *http.Request) {
320+
rw.Header().Add("content-type", "text/plain; charset=utf-8")
321+
http.ServeContent(rw, r, "install.sh", time.Time{}, bytes.NewReader(h.installScript))
322+
}
323+
309324
func (h *Handler) serveHTML(resp http.ResponseWriter, request *http.Request, reqPath string, state htmlState) bool {
310325
if data, err := h.renderHTMLWithState(request, reqPath, state); err == nil {
311326
if reqPath == "" {
@@ -533,6 +548,32 @@ func findAndParseHTMLFiles(files fs.FS) (*template.Template, error) {
533548
return root, nil
534549
}
535550

551+
type installScriptState struct {
552+
Origin string
553+
Version string
554+
}
555+
556+
func parseInstallScript(files fs.FS, buildInfo codersdk.BuildInfoResponse) ([]byte, error) {
557+
scriptFile, err := fs.ReadFile(files, "install.sh")
558+
if err != nil {
559+
return []byte{}, err
560+
}
561+
562+
script, err := template.New("install.sh").Parse(string(scriptFile))
563+
if err != nil {
564+
return []byte{}, err
565+
}
566+
567+
var buf bytes.Buffer
568+
state := installScriptState{Origin: buildInfo.DashboardURL, Version: buildInfo.Version}
569+
err = script.Execute(&buf, state)
570+
if err != nil {
571+
return []byte{}, err
572+
}
573+
574+
return buf.Bytes(), err
575+
}
576+
536577
// ExtractOrReadBinFS checks the provided fs for compressed coder binaries and
537578
// extracts them into dest/bin if found. As a fallback, the provided FS is
538579
// checked for a /bin directory, if it is non-empty it is returned. Finally

0 commit comments

Comments
 (0)