diff --git a/magefiles/config.go b/magefiles/config.go index bc9fdc9..233cddc 100644 --- a/magefiles/config.go +++ b/magefiles/config.go @@ -59,7 +59,6 @@ type forgejoConfig struct { type gotosocialConfig struct { Version string `json:"version"` - DockerImageDigest string `json:"dockerImageDigest"` Name string `json:"name"` LogLevel string `json:"logLevel"` LinuxUID int32 `json:"linuxUID"` diff --git a/magefiles/download_forgejo.go b/magefiles/download_forgejo.go index 384a519..b71293a 100644 --- a/magefiles/download_forgejo.go +++ b/magefiles/download_forgejo.go @@ -111,8 +111,7 @@ func downloadForgejo(version string) error { return fmt.Errorf("GPG verification failed; %w", err) } - err = os.Chdir(downloadFolder) - if err != nil { + if err := os.Chdir(downloadFolder); err != nil { return err } diff --git a/magefiles/download_gotosocial.go b/magefiles/download_gotosocial.go new file mode 100644 index 0000000..9c2f530 --- /dev/null +++ b/magefiles/download_gotosocial.go @@ -0,0 +1,109 @@ +//go:build mage + +package main + +import ( + "fmt" + "io" + "net/http" + "os" + "path/filepath" + + "github.com/magefile/mage/sh" +) + +func downloadGoToSocial(version string) error { + downloadFolder := filepath.Join(rootBuildDir, "gotosocial") + + if err := os.MkdirAll(downloadFolder, 0o750); err != nil { + return fmt.Errorf("unable to make %s; %w", downloadFolder, err) + } + + binaryTarUrl := fmt.Sprintf( + "https://github.com/superseriousbusiness/gotosocial/releases/download/v%s/gotosocial_%s_linux_amd64.tar.gz", + version, + version, + ) + binaryTarFilepath := filepath.Join(downloadFolder, fmt.Sprintf("gotosocial_%s_linux_amd64.tar.gz", version)) + + webAssetsTarUrl := fmt.Sprintf( + "https://github.com/superseriousbusiness/gotosocial/releases/download/v%s/gotosocial_%s_web-assets.tar.gz", + version, + version, + ) + webAssetsFilepath := filepath.Join(downloadFolder, fmt.Sprintf("gotosocial_%s_web-assets.tar.gz", version)) + + checksumUrl := fmt.Sprintf( + "https://github.com/superseriousbusiness/gotosocial/releases/download/v%s/checksums.txt", + version, + ) + checksumFilePath := filepath.Join(downloadFolder, fmt.Sprintf("gotosocial_%s_checksums.txt", version)) + + _, err := os.Stat(binaryTarFilepath) + if err == nil { + fmt.Printf("GoToSocial %s is already downloaded.\n", version) + return nil + } + + downloads := []struct { + url string + path string + }{ + { + url: binaryTarUrl, + path: binaryTarFilepath, + }, + { + url: webAssetsTarUrl, + path: webAssetsFilepath, + }, + { + url: checksumUrl, + path: checksumFilePath, + }, + } + + for _, v := range downloads { + if err := func() error { + download, err := os.Create(v.path) + if err != nil { + return fmt.Errorf("unable to create %s; %w", v.path, err) + } + defer download.Close() + + client := http.Client{ + CheckRedirect: func(r *http.Request, _ []*http.Request) error { + r.URL.Opaque = r.URL.Path + return nil + }, + } + + resp, err := client.Get(v.url) + if err != nil { + return err + } + defer resp.Body.Close() + + size, err := io.Copy(download, resp.Body) + if err != nil { + return err + } + + fmt.Printf("Downloaded %s with size %d.\n", v.path, size) + + return nil + }(); err != nil { + return err + } + } + + if err := os.Chdir(downloadFolder); err != nil { + return err + } + + if err := sh.Run("sha256sum", "--check", "--ignore-missing", fmt.Sprintf("gotosocial_%s_checksums.txt", version)); err != nil { + return err + } + + return nil +} diff --git a/magefiles/mage.go b/magefiles/mage.go index 4524e60..c4584e8 100644 --- a/magefiles/mage.go +++ b/magefiles/mage.go @@ -38,19 +38,24 @@ func Clean() error { return nil } -// Download downaloads the binaries for a given service. +// Download downloads the binaries for a given service. func Download(name string) error { cfg, err := newConfig(configFile) if err != nil { return fmt.Errorf("unable to load the configuration; %v", err) } - if name == "forgejo" { + switch name { + case "forgejo": if err := downloadForgejo(cfg.Forgejo.Version); err != nil { return fmt.Errorf("an error occurred whilst getting the forgejo binary; %w", err) } - } else { - return fmt.Errorf("unsupported service: %s", name) + case "gotosocial": + if err := downloadGoToSocial(cfg.GoToSocial.Version); err != nil { + return fmt.Errorf("an error occurred whilst getting the packages for GoToSocial; %w", err) + } + default: + fmt.Printf("'%s' has no files to download.\n", name) } return nil diff --git a/magefiles/render.go b/magefiles/render.go index 9b43427..92f5397 100644 --- a/magefiles/render.go +++ b/magefiles/render.go @@ -20,12 +20,6 @@ func Render(name string) error { return fmt.Errorf("unable to load the configuration; %v", err) } - if name == "forgejo" || name == "all" { - mg.Deps( - mg.F(Download, "forgejo"), - ) - } - if name == "all" { objects, err := os.ReadDir(rootTemplatesDir) if err != nil { @@ -39,6 +33,12 @@ func Render(name string) error { dirName := o.Name() + if dirName != "compose" { + mg.Deps( + mg.F(Download, dirName), + ) + } + log.Printf("Rendering templates for %s.\n", dirName) if err := render(cfg, o.Name()); err != nil { return fmt.Errorf("unable to render templates for %s; %w", dirName, err) @@ -47,6 +47,7 @@ func Render(name string) error { } else { if name != "compose" { mg.Deps( + mg.F(Download, name), mg.F(Render, "compose"), ) } diff --git a/templates/gotosocial/Dockerfile.gotmpl b/templates/gotosocial/Dockerfile.gotmpl index 9a46197..66a3414 100644 --- a/templates/gotosocial/Dockerfile.gotmpl +++ b/templates/gotosocial/Dockerfile.gotmpl @@ -1,19 +1,26 @@ -FROM superseriousbusiness/gotosocial:{{ .GoToSocial.Version }}@{{ .GoToSocial.DockerImageDigest }} +# syntax=docker/dockerfile:1 +FROM alpine:3.17 -USER 0 +WORKDIR /tmp -RUN apk update && apk upgrade && apk add bash \ +RUN --mount=type=bind,source=.,target=/packages \ + apk update --no-cache && apk add bash --no-cache \ && addgroup -S -g {{ .FlowGID }} flow \ && adduser -S -H -D -s /bin/bash -u {{ .GoToSocial.LinuxUID }} -G flow gts \ - && chown -R {{ .GoToSocial.LinuxUID }}:{{ .GoToSocial.LinuxUID }} /gotosocial \ - && mkdir -p /flow/gts/tmp /flow/gts/config && chown -R {{ .GoToSocial.LinuxUID }}:{{ .GoToSocial.LinuxUID }} /flow/gts + && mkdir -p /flow/gts/config \ + && tar xzvf /packages/gotosocial_{{ .GoToSocial.Version }}_linux_amd64.tar.gz \ + && tar xzvf /packages/gotosocial_{{ .GoToSocial.Version }}_web-assets.tar.gz \ + && mv /tmp/gotosocial /usr/local/bin/gotosocial \ + && mv /tmp/web /flow/gts \ + && chown -R {{ .GoToSocial.LinuxUID }}:{{ .FlowGID }} /flow/gts \ + && rm -rf /tmp/* -COPY --chown={{ .GoToSocial.LinuxUID }}:{{ .GoToSocial.LinuxUID }} entrypoint.sh /usr/local/bin/entrypoint.sh +COPY --chown={{ .GoToSocial.LinuxUID }}:{{ .GoToSocial.LinuxUID }} entrypoint.sh /usr/local/bin/entrypoint COPY --chown={{ .GoToSocial.LinuxUID }}:{{ .GoToSocial.LinuxUID }} config.yaml /flow/gts/config/config.yaml COPY --chown={{ .GoToSocial.LinuxUID }}:{{ .FlowGID }} traefik_gotosocial.yaml /flow/gts/tmp/traefik_gotosocial.yaml -RUN chmod a+x /usr/local/bin/entrypoint.sh +RUN chmod a+x /usr/local/bin/entrypoint USER {{ .GoToSocial.LinuxUID }}:{{ .FlowGID }} -ENTRYPOINT ["/usr/local/bin/entrypoint.sh"] +ENTRYPOINT ["entrypoint"] diff --git a/templates/gotosocial/config.yaml.gotmpl b/templates/gotosocial/config.yaml.gotmpl index efe560f..1f14d6b 100644 --- a/templates/gotosocial/config.yaml.gotmpl +++ b/templates/gotosocial/config.yaml.gotmpl @@ -290,12 +290,12 @@ cache: # String. Directory from which gotosocial will attempt to load html templates (.tmpl files). # Examples: ["/some/absolute/path/", "./relative/path/", "../../some/weird/path/"] # Default: "./web/template/" -web-template-base-dir: "./web/template/" +web-template-base-dir: "/flow/gts/web/template/" # String. Directory from which gotosocial will attempt to serve static web assets (images, scripts). # Examples: ["/some/absolute/path/", "./relative/path/", "../../some/weird/path/"] # Default: "./web/assets/" -web-asset-base-dir: "./web/assets/" +web-asset-base-dir: "/flow/gts/web/assets/" ########################### ##### INSTANCE CONFIG ##### diff --git a/templates/gotosocial/entrypoint.sh.gotmpl b/templates/gotosocial/entrypoint.sh.gotmpl index 563fe88..44e66f8 100644 --- a/templates/gotosocial/entrypoint.sh.gotmpl +++ b/templates/gotosocial/entrypoint.sh.gotmpl @@ -11,4 +11,4 @@ fi mkdir -p {{ .GoToSocial.DataContainerDirectory }}/database -exec /gotosocial/gotosocial --config-path /flow/gts/config/config.yaml server start +exec gotosocial --config-path /flow/gts/config/config.yaml server start