From 6f3ec2375b8f1969dc3faec9b6bcc9d962011520 Mon Sep 17 00:00:00 2001 From: Preston Baxter Date: Sun, 19 Nov 2023 09:37:45 -0600 Subject: [PATCH] B: adding deployment things --- .gitignore | 8 +++++--- 2 | 38 ++++++++++++++++++++++++++++++++++++ Makefile | 32 +++++++++++++++++++++++++++--- docker/service.dockerfile | 23 ++++++++++++++++++++++ docker/ui.dockerfile | 40 ++++++++++++++++++++++++++++++++++++++ infra/Makefile | 6 ++++++ infra/main.tf | 34 ++++++++++++++++++++------------ service/Dockerfile | 19 ------------------ service/main.go | 29 ++++++++++++++------------- service/vendors/pco/pco.go | 1 - ui/Dockerfile | 19 ------------------ ui/Makefile | 3 +-- ui/main.go | 15 ++++++++++---- versions.json | 4 ++++ 14 files changed, 195 insertions(+), 76 deletions(-) create mode 100644 2 create mode 100644 docker/service.dockerfile create mode 100644 docker/ui.dockerfile delete mode 100644 service/Dockerfile delete mode 100644 ui/Dockerfile create mode 100644 versions.json diff --git a/.gitignore b/.gitignore index 548c0fc..88b7a10 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ infra/main.*.tfvars -# Canned terraform ignores# +# Canned terraform ignores# infra/Local .terraform directories **/.terraform/* @@ -13,8 +13,8 @@ infra/crash.log infra/crash.*.log # Exclude all .tfvars files, which are likely to contain sensitive data, such as -# password, private keys, and other secrets. These should not be part of version -# control as they are data points which are potentially sensitive and subject +# password, private keys, and other secrets. These should not be part of version +# control as they are data points which are potentially sensitive and subject # to change depending on the environment. infra/*.tfvars infra/*.tfvars.json @@ -35,3 +35,5 @@ infra/*_override.tf.json # Ignore CLI configuration files infra/.terraformrc infra/terraform.rc + +secrets/* diff --git a/2 b/2 new file mode 100644 index 0000000..95e2678 --- /dev/null +++ b/2 @@ -0,0 +1,38 @@ +BASE_URL=us-central1-docker.pkg.dev/pbaxter-infra/capstone-repo +FRONTEND_VERSION=$(shell jq -rc ".frontend_version" versions.json) +WEBHOOK_VERSION=$(shell jq -rc ".webhook_version" versions.json) + +deploy: deploy-tf deploy-ui deploy-service + +deploy-tf: + cd infra; make deploy + +deploy-ui: build-ui + docker push $(BASE_URL)/frontend-service:latest + +deploy-service: build-service + docker push $(BASE_URL)/webhook-service:latest + +build: build-ui build-service + +build-ui: + docker build -f ./docker/ui.dockerfile . -t frontend-service:latest + docker build -f ./docker/ui.dockerfile . -t frontend-service:$(FRONTEND_VERSION) + docker tag frontend-service:latest $(BASE_URL)/frontend-service:latest + docker tag frontend-service:$(FRONTEND_VERSION) $(BASE_URL)/frontend-service:$(FRONTEND_VERSION) + +build-service: + docker build -f ./docker/service.dockerfile . -t webhook-service:latest + docker build -f ./docker/service.dockerfile . -t webhook-service:$(WEBHOOK_VERSION) + docker tag webhook-service:latest $(BASE_URL)/webhook-service:latest + docker tag webhook-service:$(FRONTEND_VERSION) $(BASE_URL)/webhook-service:$(WEBHOOK_VERSION) + +image: SHELL := /bin/bash +image: + [[ -d "/tmp/capstone" ]] || mkdir /tmp/capstone + cp -R infra/ /tmp/capstone/ + cp -R service/ /tmp/capstone/ + cp -R ui/ /tmp/capstone/ + rm -rf /tmp/capstone/ui/templates/*_templ.go + codevis -i /tmp/capstone --whitelist-extension go,hcl,tf,templ -o ./out.png + rm -rf /tmp/capstone/* diff --git a/Makefile b/Makefile index 120543d..e3a78fa 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,34 @@ -deploy: - cd service; make deploy - cd ui; make deploy +BASE_URL=us-central1-docker.pkg.dev/pbaxter-infra/capstone-repo +FRONTEND_VERSION=$(shell jq -rc ".frontend_version" versions.json) +WEBHOOK_VERSION=$(shell jq -rc ".webhook_version" versions.json) + +deploy: deploy-ui deploy-service deploy-tf + +deploy-tf: cd infra; make deploy +deploy-ui: build-ui + docker push $(BASE_URL)/frontend-service:latest + docker push $(BASE_URL)/frontend-service:$(FRONTEND_VERSION) + +deploy-service: build-service + docker push $(BASE_URL)/webhook-service:latest + docker push $(BASE_URL)/webhook-service:$(WEBHOOK_VERSION) + +build: build-ui build-service + +build-ui: + docker build -f ./docker/ui.dockerfile . -t frontend-service:latest + docker build -f ./docker/ui.dockerfile . -t frontend-service:$(FRONTEND_VERSION) + docker tag frontend-service:latest $(BASE_URL)/frontend-service:latest + docker tag frontend-service:$(FRONTEND_VERSION) $(BASE_URL)/frontend-service:$(FRONTEND_VERSION) + +build-service: + docker build -f ./docker/service.dockerfile . -t webhook-service:latest + docker build -f ./docker/service.dockerfile . -t webhook-service:$(WEBHOOK_VERSION) + docker tag webhook-service:latest $(BASE_URL)/webhook-service:latest + docker tag webhook-service:$(WEBHOOK_VERSION) $(BASE_URL)/webhook-service:$(WEBHOOK_VERSION) + image: SHELL := /bin/bash image: [[ -d "/tmp/capstone" ]] || mkdir /tmp/capstone diff --git a/docker/service.dockerfile b/docker/service.dockerfile new file mode 100644 index 0000000..af0edc3 --- /dev/null +++ b/docker/service.dockerfile @@ -0,0 +1,23 @@ +from golang:1.21-alpine as builder + +WORKDIR /build/service + +COPY service/go.mod service/go.sum ./ +COPY ./ui ../ui +COPY ./libs ../libs + +RUN go mod download + +COPY ./service . + +RUN go build -o main + +FROM alpine:latest + +COPY --from=builder /build/service/main /bin/main +RUN mkdir -p /etc/capstone +COPY secrets/config.yaml /etc/capstone + +EXPOSE "8080" + +ENTRYPOINT ["/bin/main"] diff --git a/docker/ui.dockerfile b/docker/ui.dockerfile new file mode 100644 index 0000000..fe6346d --- /dev/null +++ b/docker/ui.dockerfile @@ -0,0 +1,40 @@ +#Build Go stuff +FROM golang:1.21-alpine AS builder + +RUN go install github.com/a-h/templ/cmd/templ@latest + +WORKDIR /build/ui + +#Setup libs +COPY ui/go.mod ui/go.sum ./ +COPY ./service ../service +COPY ./libs ../libs + +RUN go mod download + +COPY ui/ . + +RUN rm **/*_templ.go; templ generate -path ./templates +RUN go build -o main + +#Build NPM stuff +FROM node:18-alpine AS node-builder + +WORKDIR /build + +COPY ui/ . + +RUN npm install +RUN npx tailwindcss -i static/index.css -o dist/output.css + +#Final Contianer +FROM alpine:latest + +COPY --from=node-builder /build/dist /bin/dist +COPY --from=builder /build/ui/main /bin/main +RUN mkdir -p /etc/capstone +COPY secrets/config.yaml /etc/capstone + +EXPOSE "8080" + +ENTRYPOINT ["/bin/main"] diff --git a/infra/Makefile b/infra/Makefile index 35c72bb..0719ebb 100644 --- a/infra/Makefile +++ b/infra/Makefile @@ -1,2 +1,8 @@ +FRONTEND_VERSION=$(shell jq -rc ".frontend_version" ../versions.json) +WEBHOOK_VERSION=$(shell jq -rc ".webhook_version" ../versions.json) + +deploy: SHELL := /bin/bash deploy: + sed -i -Ee "s/(webhook_service_tag = \").*(\")/\1$(WEBHOOK_VERSION)\2/g" terraform.tfvars + sed -i -Ee "s/(frontend_service_tag = \").*(\")/\1$(FRONTEND_VERSION)\2/g" terraform.tfvars tofu apply diff --git a/infra/main.tf b/infra/main.tf index 43f388e..2ae78e6 100644 --- a/infra/main.tf +++ b/infra/main.tf @@ -16,6 +16,16 @@ variable "project_region" { type = string } +variable "webhook_service_tag" { + description = "Tag for the webhook service collector image" + type = string +} + +variable "frontend_service_tag" { + description = "Tag for the frontend service collector image" + type = string +} + provider "google" { project = var.project_id } @@ -27,13 +37,13 @@ resource "google_project_service" "run_api" { } resource "google_project_service" "artifact_api" { - service = "artifactregistry.googleapis.com" + service = "artifactregistry.googleapis.com" disable_on_destroy = true } resource "google_project_service" "serverless_vpc_api" { - service = "vpcaccess.googleapis.com" + service = "vpcaccess.googleapis.com" disable_on_destroy = true } @@ -43,7 +53,7 @@ resource "google_artifact_registry_repository" "capstone_repo" { repository_id = "capstone-repo" description = "Images for capstone project" format = "DOCKER" - + docker_config { immutable_tags = false } @@ -52,13 +62,13 @@ resource "google_artifact_registry_repository" "capstone_repo" { } resource "google_cloud_run_v2_service" "webhook_service_cr" { - name = "webhook-service-cr" + name = "webhook-service-cr" location = var.project_region launch_stage = "BETA" - + template { containers { - image = "${var.project_region}-docker.pkg.dev/${var.project_id}/${google_artifact_registry_repository.capstone_repo.name}/webhook-service:latest" + image = "${var.project_region}-docker.pkg.dev/${var.project_id}/${google_artifact_registry_repository.capstone_repo.name}/webhook-service:${var.webhook_service_tag}" } vpc_access{ network_interfaces { @@ -81,13 +91,13 @@ resource "google_cloud_run_v2_service_iam_member" "webhook_service_run_all_users } resource "google_cloud_run_v2_service" "frontend_service_cr" { - name = "frontend-service-cr" + name = "frontend-service-cr" location = var.project_region launch_stage = "BETA" - + template { containers { - image = "${var.project_region}-docker.pkg.dev/${var.project_id}/${google_artifact_registry_repository.capstone_repo.name}/frontend-service:latest" + image = "${var.project_region}-docker.pkg.dev/${var.project_id}/${google_artifact_registry_repository.capstone_repo.name}/frontend-service:${var.frontend_service_tag}" } vpc_access{ network_interfaces { @@ -143,7 +153,7 @@ resource "google_cloud_run_domain_mapping" "frontend_cname_mapping" { name = trimsuffix("frontend.${data.google_dns_managed_zone.preston_baxter_zone.dns_name}", ".") metadata { - namespace = var.project_id + namespace = var.project_id } spec { @@ -156,9 +166,9 @@ resource "google_cloud_run_domain_mapping" "webhook_cname_mapping" { name = trimsuffix("webhook.${data.google_dns_managed_zone.preston_baxter_zone.dns_name}", ".") metadata { - namespace = var.project_id + namespace = var.project_id } - + spec { route_name = google_cloud_run_v2_service.webhook_service_cr.name diff --git a/service/Dockerfile b/service/Dockerfile deleted file mode 100644 index 9bd187e..0000000 --- a/service/Dockerfile +++ /dev/null @@ -1,19 +0,0 @@ -from golang:1.21-alpine as builder - -WORKDIR /build - -COPY go.mod go.sum ./ - -RUN go mod download - -COPY . . - -RUN go build -o main - -FROM alpine:latest - -COPY --from=builder /build/main /bin/main - -EXPOSE "8080" - -ENTRYPOINT ["/bin/main"] diff --git a/service/main.go b/service/main.go index 73264ed..a967fa1 100644 --- a/service/main.go +++ b/service/main.go @@ -2,25 +2,28 @@ package main import ( "fmt" - "io" - "net/http" + "os" + "git.preston-baxter.com/Preston_PLB/capstone/frontend-service/config" + "git.preston-baxter.com/Preston_PLB/capstone/webhook-service/controllers" "github.com/gin-gonic/gin" ) func main() { + config.Init() r := gin.Default() - r.POST("/webhook", func(c *gin.Context) { - body, err := io.ReadAll(c.Request.Body) - if err != nil { - panic(err) - } - fmt.Printf("captured: %s\n", string(body)) + controllers.BuildRouter(r) - c.JSON(http.StatusOK, gin.H{ - "message": "pong", - }) - }) - r.Run("0.0.0.0:8081") // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080") + var addr string + if port := os.Getenv("PORT"); port != "" { + addr = fmt.Sprintf("0.0.0.0:%s", port) + } else { + addr = "0.0.0.0:8008" + } + + err := r.Run(addr) + if err != nil { + panic(err) + } } diff --git a/service/vendors/pco/pco.go b/service/vendors/pco/pco.go index acbf929..080cedf 100644 --- a/service/vendors/pco/pco.go +++ b/service/vendors/pco/pco.go @@ -5,7 +5,6 @@ import ( "net/http" "net/url" - "git.preston-baxter.com/Preston_PLB/capstone/frontend-service/db/models" "golang.org/x/oauth2" ) diff --git a/ui/Dockerfile b/ui/Dockerfile deleted file mode 100644 index 9bd187e..0000000 --- a/ui/Dockerfile +++ /dev/null @@ -1,19 +0,0 @@ -from golang:1.21-alpine as builder - -WORKDIR /build - -COPY go.mod go.sum ./ - -RUN go mod download - -COPY . . - -RUN go build -o main - -FROM alpine:latest - -COPY --from=builder /build/main /bin/main - -EXPOSE "8080" - -ENTRYPOINT ["/bin/main"] diff --git a/ui/Makefile b/ui/Makefile index b9d239b..a3bbe1a 100644 --- a/ui/Makefile +++ b/ui/Makefile @@ -8,7 +8,6 @@ local-build: local-run: local-build ./tmp/main - infra-clean: cd docker; docker compose down @@ -17,7 +16,7 @@ infra: build: - docker build . -t frontend-service:latest + docker build -f ../docker/ui.dockerfile . -t frontend-service:latest docker tag frontend-service:latest $(BASE_URL)/frontend-service:latest deploy: build diff --git a/ui/main.go b/ui/main.go index 7412cf6..ef9b515 100644 --- a/ui/main.go +++ b/ui/main.go @@ -1,8 +1,8 @@ package main import ( - "log" - "net/http" + "fmt" + "os" "git.preston-baxter.com/Preston_PLB/capstone/frontend-service/config" "git.preston-baxter.com/Preston_PLB/capstone/frontend-service/controllers" @@ -15,8 +15,15 @@ func main() { controllers.BuildRouter(r) - err := http.ListenAndServeTLS(":8080", "tls.crt", "tls.key", r) + var addr string + if port := os.Getenv("PORT"); port != "" { + addr = fmt.Sprintf("0.0.0.0:%s", port) + } else { + addr = "0.0.0.0:8008" + } + + err := r.Run(addr) if err != nil { - log.Fatal(err) + panic(err) } } diff --git a/versions.json b/versions.json new file mode 100644 index 0000000..8988127 --- /dev/null +++ b/versions.json @@ -0,0 +1,4 @@ +{ + "webhook_version": "0.0.3", + "frontend_version": "0.0.3" +}