B: adding deployment things

This commit is contained in:
Preston Baxter 2023-11-19 09:37:45 -06:00
parent 4c5f29c0b9
commit 6f3ec2375b
14 changed files with 195 additions and 76 deletions

8
.gitignore vendored
View File

@ -1,6 +1,6 @@
infra/main.*.tfvars infra/main.*.tfvars
# Canned terraform ignores# # Canned terraform ignores#
infra/Local .terraform directories infra/Local .terraform directories
**/.terraform/* **/.terraform/*
@ -13,8 +13,8 @@ infra/crash.log
infra/crash.*.log infra/crash.*.log
# Exclude all .tfvars files, which are likely to contain sensitive data, such as # 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 # 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 # control as they are data points which are potentially sensitive and subject
# to change depending on the environment. # to change depending on the environment.
infra/*.tfvars infra/*.tfvars
infra/*.tfvars.json infra/*.tfvars.json
@ -35,3 +35,5 @@ infra/*_override.tf.json
# Ignore CLI configuration files # Ignore CLI configuration files
infra/.terraformrc infra/.terraformrc
infra/terraform.rc infra/terraform.rc
secrets/*

38
2 Normal file
View File

@ -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/*

View File

@ -1,8 +1,34 @@
deploy: BASE_URL=us-central1-docker.pkg.dev/pbaxter-infra/capstone-repo
cd service; make deploy FRONTEND_VERSION=$(shell jq -rc ".frontend_version" versions.json)
cd ui; make deploy WEBHOOK_VERSION=$(shell jq -rc ".webhook_version" versions.json)
deploy: deploy-ui deploy-service deploy-tf
deploy-tf:
cd infra; make deploy 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: SHELL := /bin/bash
image: image:
[[ -d "/tmp/capstone" ]] || mkdir /tmp/capstone [[ -d "/tmp/capstone" ]] || mkdir /tmp/capstone

23
docker/service.dockerfile Normal file
View File

@ -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"]

40
docker/ui.dockerfile Normal file
View File

@ -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"]

View File

@ -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: 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 tofu apply

View File

@ -16,6 +16,16 @@ variable "project_region" {
type = string 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" { provider "google" {
project = var.project_id project = var.project_id
} }
@ -27,13 +37,13 @@ resource "google_project_service" "run_api" {
} }
resource "google_project_service" "artifact_api" { resource "google_project_service" "artifact_api" {
service = "artifactregistry.googleapis.com" service = "artifactregistry.googleapis.com"
disable_on_destroy = true disable_on_destroy = true
} }
resource "google_project_service" "serverless_vpc_api" { resource "google_project_service" "serverless_vpc_api" {
service = "vpcaccess.googleapis.com" service = "vpcaccess.googleapis.com"
disable_on_destroy = true disable_on_destroy = true
} }
@ -43,7 +53,7 @@ resource "google_artifact_registry_repository" "capstone_repo" {
repository_id = "capstone-repo" repository_id = "capstone-repo"
description = "Images for capstone project" description = "Images for capstone project"
format = "DOCKER" format = "DOCKER"
docker_config { docker_config {
immutable_tags = false immutable_tags = false
} }
@ -52,13 +62,13 @@ resource "google_artifact_registry_repository" "capstone_repo" {
} }
resource "google_cloud_run_v2_service" "webhook_service_cr" { resource "google_cloud_run_v2_service" "webhook_service_cr" {
name = "webhook-service-cr" name = "webhook-service-cr"
location = var.project_region location = var.project_region
launch_stage = "BETA" launch_stage = "BETA"
template { template {
containers { 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{ vpc_access{
network_interfaces { 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" { resource "google_cloud_run_v2_service" "frontend_service_cr" {
name = "frontend-service-cr" name = "frontend-service-cr"
location = var.project_region location = var.project_region
launch_stage = "BETA" launch_stage = "BETA"
template { template {
containers { 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{ vpc_access{
network_interfaces { 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}", ".") name = trimsuffix("frontend.${data.google_dns_managed_zone.preston_baxter_zone.dns_name}", ".")
metadata { metadata {
namespace = var.project_id namespace = var.project_id
} }
spec { 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}", ".") name = trimsuffix("webhook.${data.google_dns_managed_zone.preston_baxter_zone.dns_name}", ".")
metadata { metadata {
namespace = var.project_id namespace = var.project_id
} }
spec { spec {
route_name = google_cloud_run_v2_service.webhook_service_cr.name route_name = google_cloud_run_v2_service.webhook_service_cr.name

View File

@ -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"]

View File

@ -2,25 +2,28 @@ package main
import ( import (
"fmt" "fmt"
"io" "os"
"net/http"
"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" "github.com/gin-gonic/gin"
) )
func main() { func main() {
config.Init()
r := gin.Default() 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{ var addr string
"message": "pong", if port := os.Getenv("PORT"); port != "" {
}) addr = fmt.Sprintf("0.0.0.0:%s", port)
}) } else {
r.Run("0.0.0.0:8081") // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080") addr = "0.0.0.0:8008"
}
err := r.Run(addr)
if err != nil {
panic(err)
}
} }

View File

@ -5,7 +5,6 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"git.preston-baxter.com/Preston_PLB/capstone/frontend-service/db/models"
"golang.org/x/oauth2" "golang.org/x/oauth2"
) )

View File

@ -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"]

View File

@ -8,7 +8,6 @@ local-build:
local-run: local-build local-run: local-build
./tmp/main ./tmp/main
infra-clean: infra-clean:
cd docker; docker compose down cd docker; docker compose down
@ -17,7 +16,7 @@ infra:
build: 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 docker tag frontend-service:latest $(BASE_URL)/frontend-service:latest
deploy: build deploy: build

View File

@ -1,8 +1,8 @@
package main package main
import ( import (
"log" "fmt"
"net/http" "os"
"git.preston-baxter.com/Preston_PLB/capstone/frontend-service/config" "git.preston-baxter.com/Preston_PLB/capstone/frontend-service/config"
"git.preston-baxter.com/Preston_PLB/capstone/frontend-service/controllers" "git.preston-baxter.com/Preston_PLB/capstone/frontend-service/controllers"
@ -15,8 +15,15 @@ func main() {
controllers.BuildRouter(r) 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 { if err != nil {
log.Fatal(err) panic(err)
} }
} }

4
versions.json Normal file
View File

@ -0,0 +1,4 @@
{
"webhook_version": "0.0.3",
"frontend_version": "0.0.3"
}