begin work on a new web

This commit is contained in:
Timothy Pidashev
2024-03-06 10:11:13 -08:00
parent 0e534d670d
commit 8f57e420b5
12 changed files with 200 additions and 204 deletions

View File

@@ -1,8 +0,0 @@
# This is the list of FHCC this repositories significant contributors.
#
# This does not necessarily list everyone who has contributed code,
# especially since most of the code is written in the many packages
# this repository relies on. This list only shows the direct
# code contributions that make this project tick, not its dependencies!
Timothy Pidashev (timmypidashev) <pidashev.tim@gmail.com>

167
Makefile Normal file
View File

@@ -0,0 +1,167 @@
PROJECT_NAME := "web"
PROJECT_AUTHORS := "Timothy Pidashev (timmypidashev) <pidashev.tim@gmail.com>"
PROJECT_VERSION := "v1.0.1"
PROJECT_LICENSE := "MIT"
PROJECT_SOURCES := "https://github.com/timmypidashev/web"
PROJECT_REGISTRY := "ghcr.io/timmypidashev/web"
PROJECT_ORGANIZATION := "org.opencontainers"
CONTAINER_PROXY_NAME := "proxy"
CONTAINER_PROXY_VERSION := "v0.0.1"
CONTAINER_PROXY_LOCATION := "src/proxy"
CONTAINER_PROXY_DESCRIPTION := "A Caddy reverse proxy."
CONTAINER_LANDING_NAME := "landing"
CONTAINER_LANDING_VERSION := "v1.0.0"
CONTAINER_LANDING_LOCATION := "src/web/landing"
CONTAINER_LANDING_DESCRIPTION := "The landing page for my website."
CONTAINER_BLOG_NAME := "blog"
CONTAINER_BLOG_VERSION := "v0.0.0"
CONTAINER_BLOG_LOCATION := "src/web/blog"
CONTAINER_BLOG_DESCRIPTION := "The blog page for my website."
.DEFAULT_GOAL := help
.PHONY: run build push prune bump
.SILENT: run build push prune bump
help:
@echo "Available targets:"
@echo " run - Runs the docker compose file with the specified environment (dev or prod)"
@echo " build - Builds the specified docker image with the appropriate environment"
@echo " push - Pushes the built docker image to the registry"
@echo " prune - Removes all built and cached docker images and containers"
@echo " bump - Bumps the project and container versions"
run:
# Arguments:
# [environment]: 'dev' or 'prod'
#
# Explanation:
# * Runs the docker compose file with the specified environment(compose.dev.yml, or compose.prod.yml)
# * Passes all generated arguments to the compose file.
# Make sure we have been given proper arguments.
@if [ "$(word 2,$(MAKECMDGOALS))" = "dev" ]; then \
echo "Running in development environment"; \
elif [ "$(word 2,$(MAKECMDGOALS))" = "prod" ]; then \
echo "Running in production environment"; \
else \
echo "Invalid usage. Please use 'make run dev' or 'make run prod'"; \
exit 1; \
fi
docker compose -f compose.$(word 2,$(MAKECMDGOALS)).yml up --remove-orphans
build:
# Arguments
# [container]: Build context(which container to build ['all' to build every container defined])
# [environment]: 'dev' or 'prod'
#
# Explanation:
# * Builds the specified docker image with the appropriate environment.
# * Passes all generated arguments to docker build-kit.
# Extract container and environment inputted.
$(eval INPUT_TARGET := $(word 2,$(MAKECMDGOALS)))
$(eval INPUT_CONTAINER := $(firstword $(subst :, ,$(INPUT_TARGET))))
$(eval INPUT_ENVIRONMENT := $(lastword $(subst :, ,$(INPUT_TARGET))))
# Call function container_build either through a for loop for each container
# if all is called, or singularly to build the container.
$(if $(filter $(strip $(INPUT_CONTAINER)),all), \
$(foreach container,$(containers),$(call container_build,$(container) $(INPUT_ENVIRONMENT))), \
$(call container_build,$(INPUT_CONTAINER) $(INPUT_ENVIRONMENT)))
push:
# Arguments
# [container]: Push context(which container to push to the registry)
# [version]: Container version to push.
#
# Explanation:
# * Pushes the specified container version to the registry defined in the user configuration.
# Extract container and version inputted.
$(eval INPUT_TARGET := $(word 2,$(MAKECMDGOALS)))
$(eval INPUT_CONTAINER := $(firstword $(subst :, ,$(INPUT_TARGET))))
$(eval INPUT_VERSION := $(lastword $(subst :, ,$(INPUT_TARGET))))
# Push the specified container version to the registry.
# NOTE: docker will complain if the container tag is invalid, no need to validate here.
@docker push $(PROJECT_REGISTRY)/$(INPUT_CONTAINER):$(INPUT_VERSION)
prune:
# Removes all built and cached docker images and containers.
bump:
@echo "Future: consider adding this; for now manually bumping project and container versions is acceptable :D"
# This function generates Docker build arguments based on variables defined in the Makefile.
# It extracts variable assignments, removes whitespace, and formats them as build arguments.
# Additionally, it appends any custom shell generated arguments defined below.
define args
$(shell \
grep -E '^[[:alnum:]_]+[[:space:]]*[:?]?[[:space:]]*=' $(MAKEFILE_LIST) | \
awk 'BEGIN {FS = ":="} { \
gsub(/^[[:space:]]+|[[:space:]]+$$/, "", $$2); \
gsub(/^/, "\x27", $$2); \
gsub(/$$/, "\x27", $$2); \
gsub(/[[:space:]]+/, "", $$1); \
gsub(":", "", $$1); \
printf "--build-arg %s=%s ", $$1, $$2 \
}') \
--build-arg BUILD_DATE='"$(shell date)"' \
--build-arg GIT_COMMIT='"$(shell git rev-parse HEAD)"'
endef
# This function generates labels based on variables defined in the Makefile.
# It extracts only the selected container variables and is used to echo this information
# to the docker buildx engine in the command line.
define labels
--label $(PROJECT_ORGANIZATION).image.title=$(CONTAINER_$(1)_NAME) \
--label $(PROJECT_ORGANIZATION).image.description=$(CONTAINER_$(1)_DESCRIPTION) \
--label $(PROJECT_ORGANIZATION).image.authors=$(PROJECT_AUTHORS) \
--label $(PROJECT_ORGANIZATION).image.url=$(PROJECT_SOURCES) \
--label $(PROJECT_ORGANIZATION).image.source=$(PROJECT_SOURCES)/$(CONTAINER_$(1)_LOCATION)
endef
# This function returns a list of container names defined in the Makefile.
# In order for this function to return a container, it needs to have this format: CONTAINER_%_NAME!
define containers
$(strip $(filter-out $(_NL),$(foreach var,$(.VARIABLES),$(if $(filter CONTAINER_%_NAME,$(var)),$(strip $($(var)))))))
endef
define container_build
$(eval CONTAINER := $(word 1,$1))
$(eval ENVIRONMENT := $(word 2,$1))
$(eval ARGS := $(shell echo $(args)))
$(eval VERSION := $(strip $(call container_version,$(CONTAINER))))
$(eval TAG := $(CONTAINER):$(ENVIRONMENT))
@echo "Building container: $(CONTAINER)"
@echo "Environment: $(ENVIRONMENT)"
@echo "Version: $(VERSION)"
@if [ "$(strip $(ENVIRONMENT))" != "dev" ] && [ "$(strip $(ENVIRONMENT))" != "prod" ]; then \
echo "Invalid environment. Please specify 'dev' or 'prod'"; \
exit 1; \
fi
$(if $(filter $(strip $(ENVIRONMENT)),prod), \
$(eval TAG := $(PROJECT_REGISTRY)/$(CONTAINER):$(VERSION)), \
)
docker buildx build --load -t $(TAG) -f $(strip $(subst $(SPACE),,$(call container_location,$(CONTAINER))))/Dockerfile.$(ENVIRONMENT) ./$(strip $(subst $(SPACE),,$(call container_location,$(CONTAINER))))/. $(ARGS) $(call labels,$(shell echo $(CONTAINER_NAME) | tr '[:lower:]' '[:upper:]')) --no-cache
endef
define container_location
$(strip $(eval CONTAINER_NAME := $(shell echo $(1) | tr '[:lower:]' '[:upper:]'))) \
$(CONTAINER_$(CONTAINER_NAME)_LOCATION)
endef
define container_version
$(strip $(eval CONTAINER_NAME := $(shell echo $(1) | tr '[:lower:]' '[:upper:]'))) \
$(if $(CONTAINER_$(CONTAINER_NAME)_VERSION), \
$(shell echo $(strip $(strip $(CONTAINER_$(CONTAINER_NAME)_VERSION))) | tr -d '[:space:]'), \
$(error Version data for container $(1) not found))
endef

View File

@@ -1,13 +1,39 @@
version: '3'
version: "3.8"
services:
proxy:
container_name: proxy
build:
context: ./proxy
context: ./src/proxy
dockerfile: Dockerfile.dev
args:
- PROXY_VERSION
- BUILD_DATE
- GIT_COMMIT
ports:
- "80:80"
- "443:443"
volumes:
- "./src/proxy/Caddyfile.dev:/Caddyfile.dev:ro"
- "./src/proxy/certs:/certs:rw"
restart: always
networks:
- proxy
depends_on:
- landing
landing:
container_name: landing
build:
context: ./src/landing
dockerfile: Dockerfile.dev
ports:
- "3000:3000"
- "8000:8000"
volumes:
- "./src/landing:/landing:rw"
networks:
- proxy
networks:
# The proxy network is the only network exposed externally
proxy:
name: proxy

View File

@@ -1,18 +0,0 @@
# The following version number is incremented whenever
# a container in this repository recieves a version bump!
[project]
name = "web"
version = "1.0.1"
description = "My portfolio website!"
authors = ["Timothy Pidashev (timmypidashev) <pidashev.tim@gmail.com>"]
source = "https://github.com/timmypidashev/web"
license = "MIT"
[containers]
[proxy]
version = "0.0.0"
description = "Responsible for proxying all traffic from containers to the web."

View File

@@ -1,2 +0,0 @@
Dockerfile
README

View File

View File

View File

@@ -1,54 +0,0 @@
# Imported args
ARG PROXY_VERSION
ARG BUILD_DATE
ARG GIT_COMMIT
# Caddy base image
ARG ALPINE_VERSION=3.18
ARG GO_VERSION=1.21.3
ARG CADDY_VERSION=v2.7.5
FROM golang:${GO_VERSION}-alpine${PROXY_VERSION} AS builder
RUN apk add -q --progress --update --no-cache git ca-certificates tzdata
RUN mkdir -p /caddydir/data && \
chmod -R 700 /caddydir
ENV GO111MODULE=on \
CGO_ENABLED=0
RUN go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
WORKDIR /caddy
ARG PLUGINS=
RUN for plugin in $(echo $PLUGINS | tr "," " "); do withFlags="$withFlags --with $plugin"; done && \
xcaddy build ${CADDY_VERSION} ${withFlags}
# Proxy base image
FROM scratch
LABEL \
com.timmypidashev.image.authors="pidashev.tim@gmail.com" \
com.timmypidashev.license="MIT" \
com.timmypidashev.image.url="https://github.com/timmypidashev/web" \
com.timmpidashev.image.source="https://github.com/timmypidashev/web" \
com.timmpidashev.image.title="proxy" \
com.timmpidashev.image.description="Caddy reverse proxy" \
com.timmypidashev.image.source="https://github.com/timmypidashev/web"
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
EXPOSE 8080 8443 2015
ENV HOME=/caddydir \
CADDYPATH=/caddydir/data \
TZ=America/Los_Angeles
COPY --from=builder --chown=1000 /caddydir /caddydir
VOLUME ["/caddydir"]
ENTRYPOINT ["/caddy"]
USER 1000
# see https://caddyserver.com/docs/cli
COPY --chown=1000 Caddyfile.dev /caddydir/Caddyfile.dev
COPY --from=builder --chown=1000 /caddy/caddy /caddy
CMD ["run","--config","/caddydir/Caddyfile.dev"]

View File

115
web
View File

@@ -1,115 +0,0 @@
#!/bin/bash
# Commandline args
action="$1"
environment="$2"
# local args
config="project.toml"
# pull project info from project.toml
project_info() {
local name=$(awk -F'[" ]+' '/^\[project\]/{getline; print $3}' "$config")
# project_version
local description=$(awk -F'[" ]+' '/description =/{print $3}' "$config")
local authors=$(awk -F'[" ]+' '/authors =/{gsub(/[\[\]]/, ""); getline; print $0}' "$config")
local src=$(awk -F'[" ]+' '/source =/{print $3}' "$config")
local license=$(awk -F'[" ]+' '/license =/{print $3}' "$config")
echo "$name $description $authors $src $license"
}
# Pulls container version from version.toml
container_version() {
local container="$1"
local version=$(awk -F'[" ]+' -v container="$container" '/^\[containers\]/{block=$1} block == "[containers]" && $1 == container {getline; print $3}' "$config")
echo "v$version"
}
# Pull project info
INFO=$(project_info)
echo $INFO
read -r name description authors src license <<< "$result"
echo $project_version
BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
GIT_COMMIT=$(git rev-parse --short HEAD)
PROJECT_VERSION=$description
case $action in
help)
echo "timmypidashev.com | deploy script"
echo "Environments:"
echo " * dev"
echo " - development environment, builds include local https, debug mode, and hot reloading"
echo ""
echo " * prod"
echo " - production environment, builds include optimizations and are ready to roll"
echo ""
echo "Actions:"
echo " * build"
echo " - builds the entire stack without cache"
echo " - example: '{action} {environment}' or '{action} {environment}"
echo ""
echo " * run"
echo " - runs the entire stack with caching enabled"
echo " - example: '{action} {environment}'"
echo " - Note: individual containers cannot run on their own"
echo ""
echo " * bump"
echo " - bumps a containers version and the project version"
echo " - example: '{action} {container}'"
echo ""
echo " * push"
echo " - pushes an image to ghcr.io"
echo " - example: '{action} {container}"
echo " - Note: only production images are pushed"
exit 1
;;
build)
if [ "$environment" == "dev" ];
then
docker compose --file compose.dev.yml build \
--build-arg BUILD_DATE=$BUILD_DATE \
--build-arg GIT_COMMIT=$GIT_COMMIT \
--build-arg PROJECT_VERSION="$PROJECT_VERSION" \
--build-arg DNS_VERSION=$(container_version "dns") \
--build-arg PROXY_VERSION=$(container_version "proxy") \
elif [ "$environment" == "prod" ];
then
echo "build prod"
else
echo "Invalid environment. Use 'dev' or 'prod'!"
exit 1
fi
;;
run)
if [ "$environment" == "dev" ];
then
echo "run dev"
elif [ "$environment" == "prod" ];
then
echo "run prod"
else
echo "Invalid environment. Use 'dev' or 'prod'!"
exit 1
fi
;;
bump)
"bump pkg"
;;
push)
"push pkg"
;;
*)
echo "Invalid action. Use 'build', 'run', or 'push'!"
;;
esac

View File

View File