From b568e349e73ffcba7bf702dc25ab34d461b79ed1 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Fri, 30 Mar 2018 14:55:46 -0700 Subject: Redirect go code to github.com --- tools/govanity/Makefile | 6 ++++++ tools/govanity/README.md | 5 +++++ tools/govanity/app.yaml | 6 ++++++ tools/govanity/main.go | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+) create mode 100644 tools/govanity/Makefile create mode 100644 tools/govanity/README.md create mode 100644 tools/govanity/app.yaml create mode 100644 tools/govanity/main.go (limited to 'tools') diff --git a/tools/govanity/Makefile b/tools/govanity/Makefile new file mode 100644 index 0000000..939497d --- /dev/null +++ b/tools/govanity/Makefile @@ -0,0 +1,6 @@ +.PHONY: deploy +deploy: + gcloud -q app deploy --project=fcuny-govanity + +.PHONY: all +all: deploy diff --git a/tools/govanity/README.md b/tools/govanity/README.md new file mode 100644 index 0000000..0c1f061 --- /dev/null +++ b/tools/govanity/README.md @@ -0,0 +1,5 @@ +A service to manage vanity URLs for go packages. + +## Deployment + +To update the application with the most recent code, run `make all`. diff --git a/tools/govanity/app.yaml b/tools/govanity/app.yaml new file mode 100644 index 0000000..40f1c9f --- /dev/null +++ b/tools/govanity/app.yaml @@ -0,0 +1,6 @@ +runtime: go +api_version: go1 + +handlers: + - url: /.* + script: _go_app diff --git a/tools/govanity/main.go b/tools/govanity/main.go new file mode 100644 index 0000000..4afe8d1 --- /dev/null +++ b/tools/govanity/main.go @@ -0,0 +1,35 @@ +package repo + +import ( + "html/template" + "net/http" +) + +type repository struct { + Name string +} + +var vanityTemplate = template.Must(template.New("code").Parse(` + + + + + + + + + + +`)) + +func init() { + http.HandleFunc("/", handleTransactions) +} + +func handleTransactions(w http.ResponseWriter, r *http.Request) { + repoName := r.URL.Path + p := &repository{Name: repoName} + if err := vanityTemplate.Execute(w, p); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } +} -- cgit 1.4.1 From f0bee440b2fcc3d85b24320d1e91903993000442 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Sun, 8 Apr 2018 17:26:07 -0700 Subject: Add a new target to the Makefile. Add a target to open the console. --- tools/govanity/Makefile | 3 +++ 1 file changed, 3 insertions(+) (limited to 'tools') diff --git a/tools/govanity/Makefile b/tools/govanity/Makefile index 939497d..e9ffe64 100644 --- a/tools/govanity/Makefile +++ b/tools/govanity/Makefile @@ -2,5 +2,8 @@ deploy: gcloud -q app deploy --project=fcuny-govanity +.PHONY: console + gcloud -q app open-console --project=fcuny-govanity + .PHONY: all all: deploy -- cgit 1.4.1 From 5e17207e418dc19c3b4502ee43f38d008b827586 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Thu, 12 Aug 2021 09:03:17 -0700 Subject: build: convert to go module --- tools/govanity/go.mod | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 tools/govanity/go.mod (limited to 'tools') diff --git a/tools/govanity/go.mod b/tools/govanity/go.mod new file mode 100644 index 0000000..9d1ea60 --- /dev/null +++ b/tools/govanity/go.mod @@ -0,0 +1,3 @@ +module golang.fcuny.net/vanity + +go 1.16 -- cgit 1.4.1 From 02734a5b4ffc555a47c5029422ae4015b59c138f Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Thu, 12 Aug 2021 13:10:25 -0700 Subject: vanity: rewrite with templates and external config Rewrite the program to use external templates and load the configuration from an external source. We don't want to blindly send all the requests to some SCM since we want to support only some modules / program that way. There's two templates, one for requests coming from a browser, which list all the supported modules, and one for the `go get` command. For requests coming from `go get`, we generate a template that indicates where the repository is. The external configuration is in YAML, and list all the supported repositories, and the URL of the repository. It assumes a default VCS for all the modules (git in our case). --- tools/govanity/go.mod | 2 + tools/govanity/go.sum | 3 + tools/govanity/main.go | 130 +++++++++++++++++++++++++------ tools/govanity/templates/index.html.tpl | 14 ++++ tools/govanity/templates/module.html.tpl | 12 +++ tools/govanity/vanity.yaml | 5 ++ 6 files changed, 144 insertions(+), 22 deletions(-) create mode 100644 tools/govanity/go.sum create mode 100644 tools/govanity/templates/index.html.tpl create mode 100644 tools/govanity/templates/module.html.tpl create mode 100644 tools/govanity/vanity.yaml (limited to 'tools') diff --git a/tools/govanity/go.mod b/tools/govanity/go.mod index 9d1ea60..d37f146 100644 --- a/tools/govanity/go.mod +++ b/tools/govanity/go.mod @@ -1,3 +1,5 @@ module golang.fcuny.net/vanity go 1.16 + +require gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b diff --git a/tools/govanity/go.sum b/tools/govanity/go.sum new file mode 100644 index 0000000..97f8991 --- /dev/null +++ b/tools/govanity/go.sum @@ -0,0 +1,3 @@ +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/tools/govanity/main.go b/tools/govanity/main.go index 4afe8d1..dd6d653 100644 --- a/tools/govanity/main.go +++ b/tools/govanity/main.go @@ -1,35 +1,121 @@ -package repo +package main import ( + "bytes" + "embed" + "flag" "html/template" + "io/ioutil" + "log" "net/http" + "strings" + + "gopkg.in/yaml.v3" ) +//go:embed templates +var tpls embed.FS + type repository struct { - Name string + Name string `yaml:"name"` + Repo string `yaml:"repo"` +} + +type config struct { + BaseUrl string `yaml:"baseUrl"` + VCS string `yaml:"vcs"` + Repositories []repository `yaml:"repositories"` +} + +type moduleTmpl struct { + Name string + Repo string + VCS string + BaseUrl string +} + +func main() { + flag.Parse() + buf, err := ioutil.ReadFile("vanity.yaml") + if err != nil { + log.Fatalf("failed to read the configuration: %+v", err) + } + + cfg := &config{} + err = yaml.Unmarshal(buf, cfg) + if err != nil { + log.Fatalf("failed to parse the YAML configuration: %+v", err) + } + + http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(200) + w.Write([]byte("ok")) + }) + + http.HandleFunc("/", goGet(cfg)) + + log.Printf("starting web server on :8080") + log.Fatal(http.ListenAndServe(":8080", nil)) } -var vanityTemplate = template.Must(template.New("code").Parse(` - - - - - - - - - - -`)) - -func init() { - http.HandleFunc("/", handleTransactions) +func goGet(cfg *config) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + status := http.StatusMethodNotAllowed + http.Error(w, http.StatusText(status), status) + return + } + + if r.FormValue("go-get") == "1" { + pathParts := strings.Split(r.URL.Path, "/") + for _, m := range cfg.Repositories { + if pathParts[1] == m.Name { + goGetModule(w, r, m, cfg) + return + } + } + status := http.StatusNotFound + http.Error(w, http.StatusText(status), status) + return + } + browserURL(w, r, cfg) + } } -func handleTransactions(w http.ResponseWriter, r *http.Request) { - repoName := r.URL.Path - p := &repository{Name: repoName} - if err := vanityTemplate.Execute(w, p); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) +func goGetModule(w http.ResponseWriter, r *http.Request, m repository, cfg *config) { + tmpl, err := template.ParseFS(tpls, "templates/module.html.tpl") + if err != nil { + log.Fatal(err) + } + mod := moduleTmpl{ + VCS: cfg.VCS, + BaseUrl: cfg.BaseUrl, + Name: m.Name, + Repo: m.Repo, + } + var buf bytes.Buffer + if err := tmpl.Execute(&buf, mod); err != nil { + log.Printf("error: %+v", err) + status := http.StatusInternalServerError + http.Error(w, http.StatusText(status), status) + } else { + w.Header().Set("Cache-Control", "no-store") + w.Write(buf.Bytes()) + } +} + +func browserURL(w http.ResponseWriter, r *http.Request, cfg *config) { + tmpl, err := template.ParseFS(tpls, "templates/index.html.tpl") + if err != nil { + log.Fatal(err) + } + var buf bytes.Buffer + if err := tmpl.Execute(&buf, cfg); err != nil { + log.Printf("error: %+v", err) + status := http.StatusInternalServerError + http.Error(w, http.StatusText(status), status) + } else { + w.Header().Set("Cache-Control", "no-store") + w.Write(buf.Bytes()) } } diff --git a/tools/govanity/templates/index.html.tpl b/tools/govanity/templates/index.html.tpl new file mode 100644 index 0000000..fd8fc56 --- /dev/null +++ b/tools/govanity/templates/index.html.tpl @@ -0,0 +1,14 @@ + + + + golang repo + + + + + + diff --git a/tools/govanity/templates/module.html.tpl b/tools/govanity/templates/module.html.tpl new file mode 100644 index 0000000..fab3414 --- /dev/null +++ b/tools/govanity/templates/module.html.tpl @@ -0,0 +1,12 @@ + + + + {{.Name}}: golang repo + + + + + + go get {{.BaseUrl}}/{{.Name}} + + diff --git a/tools/govanity/vanity.yaml b/tools/govanity/vanity.yaml new file mode 100644 index 0000000..a757729 --- /dev/null +++ b/tools/govanity/vanity.yaml @@ -0,0 +1,5 @@ +baseUrl: golang.fcuny.net +vcs: git +repositories: + - name: homelab + repo: https://git.fcuny.net/fcuny/homelab -- cgit 1.4.1 From 63d639ba8fcdde69a64aba07c5e1b16169593165 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Thu, 12 Aug 2021 13:37:05 -0700 Subject: config: replace homelab with tools The repository for `homelab` is not ready yet. --- tools/govanity/vanity.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tools') diff --git a/tools/govanity/vanity.yaml b/tools/govanity/vanity.yaml index a757729..13e0adf 100644 --- a/tools/govanity/vanity.yaml +++ b/tools/govanity/vanity.yaml @@ -1,5 +1,5 @@ baseUrl: golang.fcuny.net vcs: git repositories: - - name: homelab - repo: https://git.fcuny.net/fcuny/homelab + - name: tools + repo: https://git.fcuny.net/fcuny/tools -- cgit 1.4.1 From aa80df2b26c7455007d3db96f01bfde66c6779df Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Thu, 12 Aug 2021 13:53:33 -0700 Subject: build: deploy to fly Don't deploy to Google App anymore, but use fly.io instead. Add steps to the Makefile to build a docker image, and to deploy the application to fly.io. --- tools/govanity/Dockerfile | 13 +++++++++++++ tools/govanity/Makefile | 31 +++++++++++++++++++++++++------ tools/govanity/README.md | 5 ----- tools/govanity/README.org | 11 +++++++++++ tools/govanity/app.yaml | 6 ------ tools/govanity/fly.toml | 34 ++++++++++++++++++++++++++++++++++ 6 files changed, 83 insertions(+), 17 deletions(-) create mode 100644 tools/govanity/Dockerfile delete mode 100644 tools/govanity/README.md create mode 100644 tools/govanity/README.org delete mode 100644 tools/govanity/app.yaml create mode 100644 tools/govanity/fly.toml (limited to 'tools') diff --git a/tools/govanity/Dockerfile b/tools/govanity/Dockerfile new file mode 100644 index 0000000..caca19a --- /dev/null +++ b/tools/govanity/Dockerfile @@ -0,0 +1,13 @@ +FROM golang:1.16-alpine + +WORKDIR /src + +ADD go.mod /src +ADD go.sum /src +RUN go mod download + +ADD . /src + +RUN go build -o app . + +ENTRYPOINT ["/src/app"] diff --git a/tools/govanity/Makefile b/tools/govanity/Makefile index e9ffe64..a826abe 100644 --- a/tools/govanity/Makefile +++ b/tools/govanity/Makefile @@ -1,9 +1,28 @@ -.PHONY: deploy +DOCKER := docker +DOCKER_BUILD_ARGS := +DOCKER_IMAGE := fcuny/golang.fcuny.net +DOCKER_IMAGE_REF := $(shell git rev-parse HEAD) +DOCKERFILE := Dockerfile +PROJECT_DIR := $(realpath $(CURDIR)) + +.PHONY: deploy docker-build docker-run + +server: + @echo "Running server ..." + go run . + deploy: - gcloud -q app deploy --project=fcuny-govanity + @echo "Deploying to fly ..." + flyctl deploy --build-arg IMAGE_REF=$(DOCKER_IMAGE_REF) -.PHONY: console - gcloud -q app open-console --project=fcuny-govanity +docker-build: + @echo "Building Docker image ..." + $(DOCKER) build $(DOCKER_BUILD_ARGS) \ + --tag "${DOCKER_IMAGE}:${DOCKER_IMAGE_REF}" \ + --build-arg IMAGE_REF=$(DOCKER_IMAGE_REF) \ + --file "$(DOCKERFILE)" \ + "$(PROJECT_DIR)" -.PHONY: all -all: deploy +docker-run: docker-build + @echo "Running Docker image ..." + $(DOCKER) run -ti --rm -p 8080:8080 $(DOCKER_IMAGE):$(DOCKER_IMAGE_REF) diff --git a/tools/govanity/README.md b/tools/govanity/README.md deleted file mode 100644 index 0c1f061..0000000 --- a/tools/govanity/README.md +++ /dev/null @@ -1,5 +0,0 @@ -A service to manage vanity URLs for go packages. - -## Deployment - -To update the application with the most recent code, run `make all`. diff --git a/tools/govanity/README.org b/tools/govanity/README.org new file mode 100644 index 0000000..e2da852 --- /dev/null +++ b/tools/govanity/README.org @@ -0,0 +1,11 @@ +A service to manage vanity URLs for go packages. + +It makes it possible to install tools like this: +#+begin_src sh +GOPRIVATE=1 go install -v golang.fcuny.net/tools/cmd/music-organizer@latest +#+end_src + +* Deployment +To update the application with the most recent code, run =make deploy=. +* Configuration +Add repositories to the [[file+sys:vanity.yaml][configuration file]]. diff --git a/tools/govanity/app.yaml b/tools/govanity/app.yaml deleted file mode 100644 index 40f1c9f..0000000 --- a/tools/govanity/app.yaml +++ /dev/null @@ -1,6 +0,0 @@ -runtime: go -api_version: go1 - -handlers: - - url: /.* - script: _go_app diff --git a/tools/govanity/fly.toml b/tools/govanity/fly.toml new file mode 100644 index 0000000..286cd1e --- /dev/null +++ b/tools/govanity/fly.toml @@ -0,0 +1,34 @@ +app = "golang-fcuny-net" + +kill_signal = "SIGINT" +kill_timeout = 5 + +[env] + +[experimental] + allowed_public_ports = [] + auto_rollback = true + +[[services]] + internal_port = 8080 + protocol = "tcp" + script_checks = [] + + [services.concurrency] + hard_limit = 25 + soft_limit = 20 + type = "connections" + + [[services.ports]] + handlers = ["http"] + port = 80 + + [[services.ports]] + handlers = ["tls", "http"] + port = 443 + + [[services.tcp_checks]] + grace_period = "1s" + interval = "15s" + restart_limit = 6 + timeout = "2s" -- cgit 1.4.1 From 19b3dccc1ff31534df3636d4d7d6a28c0ae1e189 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Thu, 12 Aug 2021 15:46:51 -0700 Subject: config: add vanity --- tools/govanity/vanity.yaml | 2 ++ 1 file changed, 2 insertions(+) (limited to 'tools') diff --git a/tools/govanity/vanity.yaml b/tools/govanity/vanity.yaml index 13e0adf..57c18a1 100644 --- a/tools/govanity/vanity.yaml +++ b/tools/govanity/vanity.yaml @@ -1,5 +1,7 @@ baseUrl: golang.fcuny.net vcs: git repositories: + - name: vanity + repo: https://git.fcuny.net/fcuny/govanity - name: tools repo: https://git.fcuny.net/fcuny/tools -- cgit 1.4.1 From 714b3309ec5c8d7d4881ba2965be84b3a49b76b8 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Sat, 14 Aug 2021 14:52:36 -0700 Subject: build: trim the size of the Docker container Prior to this change, the size of the image was about 300MB: ``` ; docker images|grep fcuny/golang|grep days fcuny/golang.fcuny.net d07add42f21a69e2c057eae8492bbd599dc50082 9fd284c5d8ee 2 days ago 313MB fcuny/golang.fcuny.net ca3dd083f8d6642821781ce03829713524322bbe 8104afadfd1f 2 days ago 313MB ``` With this change, we're reducing the image to less than 10MB: ``` fcuny/golang.fcuny.net 19b3dccc1ff31534df3636d4d7d6a28c0ae1e189 c9ae35574f8e 24 seconds ago 9.09MB ``` Since there's storage and bandwidth involved in this process, there's no point in not going for a smaller image. --- tools/govanity/Dockerfile | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'tools') diff --git a/tools/govanity/Dockerfile b/tools/govanity/Dockerfile index caca19a..db471c8 100644 --- a/tools/govanity/Dockerfile +++ b/tools/govanity/Dockerfile @@ -1,13 +1,30 @@ -FROM golang:1.16-alpine +FROM golang:1.16 AS builder WORKDIR /src +ENV USER=app +RUN adduser \ + --disabled-password \ + --gecos "" \ + --home "/src" \ + --shell "/sbin/nologin" \ + --uid "10001" \ + "${USER}" + ADD go.mod /src ADD go.sum /src RUN go mod download ADD . /src -RUN go build -o app . +RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -a -installsuffix cgo -ldflags '-extldflags "-static"' -o app . + +FROM scratch +COPY --from=builder /src/app /govanity +COPY --from=builder /src/vanity.yaml /vanity.yaml +COPY --from=builder /etc/passwd /etc/passwd +COPY --from=builder /etc/group /etc/group + +USER app:app -ENTRYPOINT ["/src/app"] +ENTRYPOINT ["/govanity"] -- cgit 1.4.1 From ad1f44c2fc3d280013735756561c36edfb3487f9 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Sat, 14 Aug 2021 15:27:30 -0700 Subject: build: build the image before deploying --- tools/govanity/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tools') diff --git a/tools/govanity/Makefile b/tools/govanity/Makefile index a826abe..48eb9b4 100644 --- a/tools/govanity/Makefile +++ b/tools/govanity/Makefile @@ -11,7 +11,7 @@ server: @echo "Running server ..." go run . -deploy: +deploy: docker-build @echo "Deploying to fly ..." flyctl deploy --build-arg IMAGE_REF=$(DOCKER_IMAGE_REF) -- cgit 1.4.1 From 80c9c7e4ac76e4c0f7b245da79508f87ac9171cb Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Sat, 14 Aug 2021 15:29:38 -0700 Subject: build: create user before setting workdir --- tools/govanity/Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tools') diff --git a/tools/govanity/Dockerfile b/tools/govanity/Dockerfile index db471c8..eb6b8db 100644 --- a/tools/govanity/Dockerfile +++ b/tools/govanity/Dockerfile @@ -1,7 +1,5 @@ FROM golang:1.16 AS builder -WORKDIR /src - ENV USER=app RUN adduser \ --disabled-password \ @@ -11,6 +9,8 @@ RUN adduser \ --uid "10001" \ "${USER}" +WORKDIR /src + ADD go.mod /src ADD go.sum /src RUN go mod download -- cgit 1.4.1 From 3161b0a05d07dfaada45dffd83b37672231edb9f Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Thu, 9 Sep 2021 18:13:05 -0700 Subject: build: tag correctly the release The version was evaluated before the deploy was done, we need to get that information after running `flyctl deploy`. Annotate the tag since we're doing a release. Remove the argument `IMAGE_REF` since it's being ignored. --- tools/govanity/Makefile | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'tools') diff --git a/tools/govanity/Makefile b/tools/govanity/Makefile index 48eb9b4..9632386 100644 --- a/tools/govanity/Makefile +++ b/tools/govanity/Makefile @@ -5,21 +5,25 @@ DOCKER_IMAGE_REF := $(shell git rev-parse HEAD) DOCKERFILE := Dockerfile PROJECT_DIR := $(realpath $(CURDIR)) -.PHONY: deploy docker-build docker-run +.PHONY: deploy docker-build docker-run worktree-clean server server: @echo "Running server ..." go run . -deploy: docker-build +worktree-clean: + git diff --exit-code + git diff --staged --exit-code + +deploy: worktree-clean docker-build @echo "Deploying to fly ..." - flyctl deploy --build-arg IMAGE_REF=$(DOCKER_IMAGE_REF) + flyctl deploy + git tag -a --message $$(flyctl info -j |jq -r '.App | "\(.Name)/v\(.Version)"') $$(flyctl info -j |jq -r '.App | "\(.Name)/v\(.Version)"') docker-build: @echo "Building Docker image ..." $(DOCKER) build $(DOCKER_BUILD_ARGS) \ --tag "${DOCKER_IMAGE}:${DOCKER_IMAGE_REF}" \ - --build-arg IMAGE_REF=$(DOCKER_IMAGE_REF) \ --file "$(DOCKERFILE)" \ "$(PROJECT_DIR)" -- cgit 1.4.1 From 67c5eff3dfe11d3741d42e70fcd5f960038f4011 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Sun, 10 Oct 2021 18:03:22 -0700 Subject: config: add mpd-stats --- tools/govanity/vanity.yaml | 2 ++ 1 file changed, 2 insertions(+) (limited to 'tools') diff --git a/tools/govanity/vanity.yaml b/tools/govanity/vanity.yaml index 57c18a1..cfe5b03 100644 --- a/tools/govanity/vanity.yaml +++ b/tools/govanity/vanity.yaml @@ -5,3 +5,5 @@ repositories: repo: https://git.fcuny.net/fcuny/govanity - name: tools repo: https://git.fcuny.net/fcuny/tools + - name: mpd-stats + repo: https://git.fcuny.net/fcuny/mpd-stats -- cgit 1.4.1 From 97185faedaf8efbff15e2b06eecf02e40292348c Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Wed, 11 May 2022 18:17:35 -0700 Subject: add a flake.nix to the repository --- tools/govanity/.envrc | 1 + tools/govanity/.gitignore | 1 + tools/govanity/flake.lock | 90 +++++++++++++++++++++++++++++++++++++++++++++++ tools/govanity/flake.nix | 27 ++++++++++++++ 4 files changed, 119 insertions(+) create mode 100644 tools/govanity/.envrc create mode 100644 tools/govanity/.gitignore create mode 100644 tools/govanity/flake.lock create mode 100644 tools/govanity/flake.nix (limited to 'tools') diff --git a/tools/govanity/.envrc b/tools/govanity/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/tools/govanity/.envrc @@ -0,0 +1 @@ +use flake diff --git a/tools/govanity/.gitignore b/tools/govanity/.gitignore new file mode 100644 index 0000000..c4a847d --- /dev/null +++ b/tools/govanity/.gitignore @@ -0,0 +1 @@ +/result diff --git a/tools/govanity/flake.lock b/tools/govanity/flake.lock new file mode 100644 index 0000000..a334e5c --- /dev/null +++ b/tools/govanity/flake.lock @@ -0,0 +1,90 @@ +{ + "nodes": { + "flake-utils": { + "locked": { + "lastModified": 1644229661, + "narHash": "sha256-1YdnJAsNy69bpcjuoKdOYQX0YxZBiCYZo4Twxerqv7k=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "3cecb5b042f7f209c56ffd8371b2711a290ec797", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1652298859, + "narHash": "sha256-hcwRboK+NxMWUJh0fQ3VsocDcAHrYJ95FmPEHlddV0Y=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "051448e41537c3463ae776d46115d01afb6c498d", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "release-21.11", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1645655918, + "narHash": "sha256-ZfbEFRW7o237+A1P7eTKhXje435FCAoe0blj2n20Was=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "77a7a4197740213879b9a1d2e1788c6c8ade4274", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "type": "indirect" + } + }, + "pre-commit-hooks": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs_2" + }, + "locked": { + "lastModified": 1649054408, + "narHash": "sha256-wz8AH7orqUE4Xog29WMTqOYBs0DMj2wFM8ulrTRVgz0=", + "path": "/nix/store/2bw70fnmml6w0vjkcj2n7jvzcxpgprrm-source", + "rev": "e5e7b3b542e7f4f96967966a943d7e1c07558042", + "type": "path" + }, + "original": { + "id": "pre-commit-hooks", + "type": "indirect" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs", + "pre-commit-hooks": "pre-commit-hooks", + "utils": "utils" + } + }, + "utils": { + "locked": { + "lastModified": 1649676176, + "narHash": "sha256-OWKJratjt2RW151VUlJPRALb7OU2S5s+f0vLj4o1bHM=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "a4b154ebbdc88c8498a5c7b01589addc9e9cb678", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/tools/govanity/flake.nix b/tools/govanity/flake.nix new file mode 100644 index 0000000..15b13d0 --- /dev/null +++ b/tools/govanity/flake.nix @@ -0,0 +1,27 @@ +{ + description = "govanity"; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/release-21.11"; + utils.url = "github:numtide/flake-utils"; + }; + + outputs = { self, utils, nixpkgs, pre-commit-hooks }: + utils.lib.eachDefaultSystem (system: + let pkgs = nixpkgs.legacyPackages.${system}; + in { + + defaultPackage = self.packages.${system}.vanity; + + packages.vanity = pkgs.buildGoModule { + pname = "vanity"; + version = "0.0.1"; + src = ./.; + vendorSha256 = "sha256-iu2QE+vvenFWpOOz1NHVQHudiWkvkKqZvD4ZX4Xa1sY="; + nativeBuildInputs = with pkgs; [ go ]; + }; + + devShell = with pkgs; + mkShell { nativeBuildInputs = [ git go gopls golangci-lint bash ]; }; + }); +} -- cgit 1.4.1 From 369c37f9d8e7ec6bb206c095e16e6b10609f387d Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Wed, 11 May 2022 18:18:30 -0700 Subject: README: update documentation --- tools/govanity/README.org | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'tools') diff --git a/tools/govanity/README.org b/tools/govanity/README.org index e2da852..7955ead 100644 --- a/tools/govanity/README.org +++ b/tools/govanity/README.org @@ -5,6 +5,10 @@ It makes it possible to install tools like this: GOPRIVATE=1 go install -v golang.fcuny.net/tools/cmd/music-organizer@latest #+end_src +* Build +Running =nix build= in the repository will create the binary. +* Run +Running =nix run= in the repository will start the server. * Deployment To update the application with the most recent code, run =make deploy=. * Configuration -- cgit 1.4.1 From 722389e918af9c44fba6ec658f576b5838661179 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Wed, 11 May 2022 18:18:39 -0700 Subject: Dockerfile: update steps --- tools/govanity/Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tools') diff --git a/tools/govanity/Dockerfile b/tools/govanity/Dockerfile index eb6b8db..20df29f 100644 --- a/tools/govanity/Dockerfile +++ b/tools/govanity/Dockerfile @@ -20,11 +20,11 @@ ADD . /src RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -a -installsuffix cgo -ldflags '-extldflags "-static"' -o app . FROM scratch -COPY --from=builder /src/app /govanity +COPY --from=builder /src/app /vanity COPY --from=builder /src/vanity.yaml /vanity.yaml COPY --from=builder /etc/passwd /etc/passwd COPY --from=builder /etc/group /etc/group USER app:app -ENTRYPOINT ["/govanity"] +ENTRYPOINT ["/vanity"] -- cgit 1.4.1 From ec02496f673820244fabb3fadc903d1315218497 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Wed, 11 May 2022 18:45:05 -0700 Subject: scripts: add a script to deploy Remove the target from the Makefile, add a target to the flake configuration. --- tools/govanity/Makefile | 7 +------ tools/govanity/flake.nix | 10 ++++++++++ tools/govanity/scripts/deploy.sh | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) create mode 100755 tools/govanity/scripts/deploy.sh (limited to 'tools') diff --git a/tools/govanity/Makefile b/tools/govanity/Makefile index 9632386..6392551 100644 --- a/tools/govanity/Makefile +++ b/tools/govanity/Makefile @@ -5,7 +5,7 @@ DOCKER_IMAGE_REF := $(shell git rev-parse HEAD) DOCKERFILE := Dockerfile PROJECT_DIR := $(realpath $(CURDIR)) -.PHONY: deploy docker-build docker-run worktree-clean server +.PHONY: docker-build docker-run worktree-clean server server: @echo "Running server ..." @@ -15,11 +15,6 @@ worktree-clean: git diff --exit-code git diff --staged --exit-code -deploy: worktree-clean docker-build - @echo "Deploying to fly ..." - flyctl deploy - git tag -a --message $$(flyctl info -j |jq -r '.App | "\(.Name)/v\(.Version)"') $$(flyctl info -j |jq -r '.App | "\(.Name)/v\(.Version)"') - docker-build: @echo "Building Docker image ..." $(DOCKER) build $(DOCKER_BUILD_ARGS) \ diff --git a/tools/govanity/flake.nix b/tools/govanity/flake.nix index 15b13d0..d2c2254 100644 --- a/tools/govanity/flake.nix +++ b/tools/govanity/flake.nix @@ -21,6 +21,16 @@ nativeBuildInputs = with pkgs; [ go ]; }; + apps = { + deploy = pkgs.pkgs.writeShellScriptBin "run-deploy" '' + set -euxo pipefail + export PATH=${ + pkgs.lib.makeBinPath [ pkgs.go pkgs.git pkgs.jq pkgs.flyctl ] + }:$PATH + bash ./scripts/deploy.sh + ''; + }; + devShell = with pkgs; mkShell { nativeBuildInputs = [ git go gopls golangci-lint bash ]; }; }); diff --git a/tools/govanity/scripts/deploy.sh b/tools/govanity/scripts/deploy.sh new file mode 100755 index 0000000..4f46b68 --- /dev/null +++ b/tools/govanity/scripts/deploy.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +git diff --exit-code +git diff --staged --exit-code + +flyctl deploy + +VERSION=$(flyctl info -j |jq -r '.App | "\(.Name)/v\(.Version)"') + +git tag -a --message ${VERSION} ${VERSION} +git push origin --all +git push origin --tags + +flyctl agent stop -- cgit 1.4.1 From 1440a7c5f648cb6a5797f98db18c00f37a5b7f74 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Wed, 11 May 2022 18:47:07 -0700 Subject: drone: add a configuration --- tools/govanity/.drone.yml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tools/govanity/.drone.yml (limited to 'tools') diff --git a/tools/govanity/.drone.yml b/tools/govanity/.drone.yml new file mode 100644 index 0000000..ee50017 --- /dev/null +++ b/tools/govanity/.drone.yml @@ -0,0 +1,18 @@ +--- +kind: pipeline +type: exec +name: default + +trigger: + event: + - push + branch: + - main + +steps: + - name: deploy + environment: + FLY_API_TOKEN: + from_secret: FLY_API_TOKEN + commands: + - nix run .#deploy -- cgit 1.4.1 From 8dd0ed8f2d7bc6d0cc845b6e5dbd53588ede1ad6 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Fri, 13 May 2022 19:56:57 -0700 Subject: remove unused Makefile --- tools/govanity/Makefile | 27 --------------------------- 1 file changed, 27 deletions(-) delete mode 100644 tools/govanity/Makefile (limited to 'tools') diff --git a/tools/govanity/Makefile b/tools/govanity/Makefile deleted file mode 100644 index 6392551..0000000 --- a/tools/govanity/Makefile +++ /dev/null @@ -1,27 +0,0 @@ -DOCKER := docker -DOCKER_BUILD_ARGS := -DOCKER_IMAGE := fcuny/golang.fcuny.net -DOCKER_IMAGE_REF := $(shell git rev-parse HEAD) -DOCKERFILE := Dockerfile -PROJECT_DIR := $(realpath $(CURDIR)) - -.PHONY: docker-build docker-run worktree-clean server - -server: - @echo "Running server ..." - go run . - -worktree-clean: - git diff --exit-code - git diff --staged --exit-code - -docker-build: - @echo "Building Docker image ..." - $(DOCKER) build $(DOCKER_BUILD_ARGS) \ - --tag "${DOCKER_IMAGE}:${DOCKER_IMAGE_REF}" \ - --file "$(DOCKERFILE)" \ - "$(PROJECT_DIR)" - -docker-run: docker-build - @echo "Running Docker image ..." - $(DOCKER) run -ti --rm -p 8080:8080 $(DOCKER_IMAGE):$(DOCKER_IMAGE_REF) -- cgit 1.4.1