From 42fa39f3c00264b1e5771cc61773e90295f7ce67 Mon Sep 17 00:00:00 2001 From: "Willem@105.pve1.lan" Date: Fri, 18 Feb 2022 08:33:19 +0200 Subject: [PATCH 1/3] moved .gitignore(s) to seperate branch --- .gitignore | 10 ++++++++++ docker/.gitignore | 2 ++ docker/client/examples/home-assistant/.gitignore | 7 +++++++ 3 files changed, 19 insertions(+) create mode 100644 .gitignore create mode 100644 docker/.gitignore create mode 100644 docker/client/examples/home-assistant/.gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b388db8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +# Logo +/logo.png + +# Build executables +/build +/cmd/boringproxy/boringproxy* +boringproxy_*.tar.gz + +# Boringproxy database, created if exec is run in boringproxy folder +boringproxy_db.json \ No newline at end of file diff --git a/docker/.gitignore b/docker/.gitignore new file mode 100644 index 0000000..9425d7b --- /dev/null +++ b/docker/.gitignore @@ -0,0 +1,2 @@ +# Docker-compose file with local dev config +dev.yml \ No newline at end of file diff --git a/docker/client/examples/home-assistant/.gitignore b/docker/client/examples/home-assistant/.gitignore new file mode 100644 index 0000000..7333a75 --- /dev/null +++ b/docker/client/examples/home-assistant/.gitignore @@ -0,0 +1,7 @@ +# Ignore everything in config +config/* +# But not these files... +!config/configuration.yaml + +# Ignore everything in development config +dev-config/* \ No newline at end of file From 9a4863c4f419ecd5acee2df146ebed75816e84e3 Mon Sep 17 00:00:00 2001 From: "Willem@105.pve1.lan" Date: Fri, 18 Feb 2022 14:30:12 +0200 Subject: [PATCH 2/3] build files using docker --- .gitignore | 11 +- Dockerfile | 19 +- default_logo.png | Bin 0 -> 913 bytes docker/.gitignore | 2 - .../client/examples/home-assistant/.gitignore | 7 - scripts/build_docker.sh | 220 ++++++++++++++++++ scripts/upload_docker_github.sh | 32 +++ 7 files changed, 278 insertions(+), 13 deletions(-) create mode 100644 default_logo.png delete mode 100644 docker/.gitignore delete mode 100644 docker/client/examples/home-assistant/.gitignore create mode 100755 scripts/build_docker.sh create mode 100755 scripts/upload_docker_github.sh diff --git a/.gitignore b/.gitignore index b388db8..5e94504 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,13 @@ boringproxy_*.tar.gz # Boringproxy database, created if exec is run in boringproxy folder -boringproxy_db.json \ No newline at end of file +boringproxy_db.json + +# Development files +dev.yml + +# Docker client examples +# Ignore everything in config +docker/client/examples/home-assistant/config/* +# But not these files... +!docker/client/examples/home-assistant/config/configuration.yaml \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 9522d51..945640f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,19 +1,32 @@ FROM golang:1.17-alpine3.15 as builder +LABEL boringproxy=builder + +ARG VERSION +ARG GOOS="linux" +ARG GOARCH="amd64" +ARG BRANCH="master" +ARG REPO="https://github.com/boringproxy/boringproxy.git" +ARG ORIGIN='local' WORKDIR /build RUN apk add git +RUN if [[ "ORIGIN" == 'remote' ]] ; then git clone --depth 1 --branch "${BRANCH}" ${REPO}; fi + COPY go.* ./ RUN go mod download COPY . . +RUN export VERSION='2' -RUN cd cmd/boringproxy && CGO_ENABLED=0 go build -o boringproxy +RUN cd cmd/boringproxy && CGO_ENABLED=0 GOOS=${GOOS} GOARCH=${GOARCH} \ + go build -ldflags "-X main.Version=${VERSION}" \ + -o boringproxy -FROM scratch +FROM scratch EXPOSE 80 443 COPY --from=builder /build/cmd/boringproxy/boringproxy / ENTRYPOINT ["/boringproxy"] -CMD ["server"] +CMD ["version"] \ No newline at end of file diff --git a/default_logo.png b/default_logo.png new file mode 100644 index 0000000000000000000000000000000000000000..09e6a58e5f8778a07ba8a155c3c518d279778317 GIT binary patch literal 913 zcmV;C18)3@P);b6~!9Vaj%5%y(kYd1KRhW7&IS+IwT% zd}H2xWZ->c;eBM`ePrT&WaE8qxqfBher4l-X5)Wn;HH<~rkLNUoZhLN->RM8uA|+5J3bHM5+fNRQ(J;Vy7;LpS3v%#1d@$= zSLyMpCzb_AmWisiBG5kd1o|2ocUXAcP$AK*%)@M2K@hi1m#FLfkwM z(=#L@9z~T*CJ{LlS;Yy6c;A1Mq=!`=pb#NQXICaV^+F**HoT$%YqnwqE`M*+0zee~fa$EcO2Km-_KG zc59Me{^rBYCbEwGq-i3j`|*dBbUG38o7vB|vpSHmE>cq6>e}O5M@unw^|eHkwX?+y zi{9pLuyLW7vGSz5P3`V0hTQ0HxEDJH3L7U7M~Aug-lhBm)FEiGr+y~yoIipc=Z5M= zw2i$4;K6jNv)QR8w(r1iBxK(j)0p~X_{)NyVM-224vX1rb4X_oDX41?1638fz(E8N nL=Zs)5k$BnP=yF0{~Y-SffN5Q4;P&~00000NkvXXu0mjfL>tW? literal 0 HcmV?d00001 diff --git a/docker/.gitignore b/docker/.gitignore deleted file mode 100644 index 9425d7b..0000000 --- a/docker/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -# Docker-compose file with local dev config -dev.yml \ No newline at end of file diff --git a/docker/client/examples/home-assistant/.gitignore b/docker/client/examples/home-assistant/.gitignore deleted file mode 100644 index 7333a75..0000000 --- a/docker/client/examples/home-assistant/.gitignore +++ /dev/null @@ -1,7 +0,0 @@ -# Ignore everything in config -config/* -# But not these files... -!config/configuration.yaml - -# Ignore everything in development config -dev-config/* \ No newline at end of file diff --git a/scripts/build_docker.sh b/scripts/build_docker.sh new file mode 100755 index 0000000..1c46398 --- /dev/null +++ b/scripts/build_docker.sh @@ -0,0 +1,220 @@ +#!/bin/bash + +# Run from root boringproxy folder and call with ./scripts/build_docker.sh + +############################################################ +# Help # +############################################################ +Help() +{ + # Display Help + echo "Script to buid BoringProxy executables using docker" + echo "Syntax: build_docker.sh [h|help|local|remote]" + echo + echo "h & help: Display help documetation" + echo + echo "local: Build executables from local repo (current folder)" + echo "options:" + echo " a|arch Architecture to build for build (amd64,arm,arm64)" + echo " os Operating System to build for (linux,freebsd,openbsd,windows,darwin)" + echo " o|output Output format (image,exec)" + echo "example: " + echo " build_docker.sh local -a=amd -s=linux -o=image" + echo + echo "local: Build executables remote repo (Github fork)" + echo "options:" + echo " a|arch Architecture to build for build (amd64,arm,arm64)" + echo " os Operating System to build for (linux,freebsd,openbsd,windows,darwin)" + echo " u|user Github user" + echo " b|branch Branch/Tree" + echo " o|output Output format (image,exec)" + echo "example: " + echo " generate_docker.sh remote -a=amd -s=linux -u=wgrobler -b=dev -o=exec" + echo +} + +############################################################ +############################################################ +# Main program # +############################################################ +############################################################ + +# Check if file was run from correct working directory, if correct script file will exists +FILE=./scripts/build_docker.sh +if [ ! -f "$FILE" ]; then + echo "Script needs to be run from root boringproxy folder, call with ./scripts/build_docker.sh" + exit; +fi + +if [ -z "$1" ]; +then + echo "No input variabled supplied" + echo "Here is the script help documentation:" + echo + Help + exit; +else + if [ "$1" == "help" ] || [ "$1" == "h" ]; + then + Help + exit; + fi + if [ "$1" == "local" ]; + then + CMD='local' + GOARCH='amd64'; + GOOS='linux'; + OUTPUT_FORMAT='image'; + # Get the options + for i in "$@"; do + case $i in + -a=*|--arch=*) + GOARCH="${i#*=}"; + shift; + ;; + -os=*) + GOOS="${i#*=}"; + shift; + ;; + -o=*|--output=*) + OUTPUT_FORMAT="${i#*=}"; + shift; + ;; + -*|--*) + echo "Unknown option $i" + exit 1 + ;; + *) + ;; + esac + done + fi + if [ "$1" == "remote" ]; + then + CMD='remote' + GOARCH='amd64'; + GOOS='linux'; + BRANCH='master'; + GITHUB_USER="boringproxy" + OUTPUT_FORMAT='image'; + # Get the options + for i in "$@"; do + case $i in + -a=*|--arch=*) + GOARCH="${i#*=}"; + shift; + ;; + -os=*) + GOOS="${i#*=}"; + shift; + ;; + -b=*|--branch=*) + BRANCH="${i#*=}"; + shift; + ;; + -u=*|--user=*) + GITHUB_USER="${i#*=}"; + shift; + ;; + -o=*|--output=*) + OUTPUT_FORMAT="${i#*=}"; + shift; + ;; + -*|--*) + echo "Unknown option $i" + exit 1 + ;; + *) + ;; + esac + done + fi +fi + +# Get current timestamp and set at TAG +timestamp=$(date +%s) + +# Make build folder if not already exists +mkdir -p ./build + +# Check if logo.png exists, if not create +FILE=./logo.png +if [ -f "$FILE" ]; +then + echo "$FILE exists. Using file in build"; +else + echo "$FILE does not exist. Creating file"; + cp ./default_logo.png ./logo.png; +fi + +if [ "$CMD" == "local" ]; +then + echo "Building from local git repo" + + # Get current version from git tags + version=$(git describe --tags) + + # Set docker image name + if [ "$OUTPUT_FORMAT" == "image" ]; + then DockerImage="boringproxy-$GOOS-$GOARCH"; + else DockerImage="boringproxy-$GOOS-$GOARCH:$timestamp"; + fi + + # Build docker image(s) + docker build -t $DockerImage . \ + --build-arg VERSION=$(git describe --tags) \ + --build-arg GOARCH=$GOARCH \ + --build-arg GOOS=$GOOS; +fi + +if [ "$CMD" == "remote" ]; +then + echo "Building from remote git repo" + + # Set docker image name + if [ "$OUTPUT_FORMAT" == "image" ]; + then DockerImage="$GITHUB_USER.$BRANCH.boringproxy-$GOOS-$GOARCH"; + else DockerImage="$GITHUB_USER.$BRANCH.boringproxy-$GOOS-$GOARCH:$timestamp"; + fi + + # Build docker image(s) + REPO="https://github.com/$GITHUB_USER/boringproxy.git" + docker build -t $DockerImage . \ + --build-arg VERSION="$GITHUB_USER:$BRANCH" \ + --build-arg GOARCH=$GOARCH \ + --build-arg GOOS=$GOOS \ + --build-arg BRANCH=$BRANCH \ + --build-arg REPO=$REPO; +fi + +# if DockerImage is set, continue +if [ -n "$DockerImage" ]; +then + if [ "$OUTPUT_FORMAT" == "image" ]; + then + # Prune intermediate images + docker image prune -f --filter label=boringproxy=builder + + echo + echo "Docker file created with filename: $DockerImage" + echo "Use $DockerImage as image name when uploading" + else + # Prune intermediate images + docker image prune -f --filter label=boringproxy=builder + + # Set filename for exec + if [ "$CMD" == "local" ]; + then FILENAME="boringproxy-$GOOS-$GOARCH"; + else FILENAME="$GITHUB_USER.$BRANCH.boringproxy-$GOOS-$GOARCH"; + fi + + # Copy exec from image + docker cp $(docker create $DockerImage):/boringproxy ./build/$FILENAME; + + # Remove temp container + docker rm $(docker container ls -n 1 | awk '{ print $1 }' | grep -v CONTAINER) + + # Remove image + docker rmi $DockerImage; + fi +fi \ No newline at end of file diff --git a/scripts/upload_docker_github.sh b/scripts/upload_docker_github.sh new file mode 100755 index 0000000..832c8dd --- /dev/null +++ b/scripts/upload_docker_github.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +# This file is used to upload a build docker image to GitHub. +# Run build_docker.sh first to create new image +# Run from root boringproxy folder and call with ./scripts/upload_docker_image.sh github-username +# github-username must be lowercase + +# https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry + +if [ -z "$1" ]; +then { + echo "Container name required"; + exit; +} +fi + +if [ -z "$2" ]; +then echo "GitHub username required"; +else { + if [ -z "$3" ]; + then { + echo "No TAG set, using latest"; + tag='latest'; + } + else tag=$3; + fi + docker image tag $1 ghcr.io/$2/$1:$tag + CR_PAT=`cat ~/.auth_tokens/github` + echo $CR_PAT | docker login ghcr.io -u $2 --password-stdin + docker push ghcr.io/$2/$1:$tag +} fi + From 6f089e2d758e1f737acd01abd0c787e88b4e44f9 Mon Sep 17 00:00:00 2001 From: Willem Grobler <40760504+WGrobler@users.noreply.github.com> Date: Fri, 18 Feb 2022 14:43:17 +0200 Subject: [PATCH 3/3] Delete .gitignore --- .gitignore | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 5e94504..0000000 --- a/.gitignore +++ /dev/null @@ -1,19 +0,0 @@ -# Logo -/logo.png - -# Build executables -/build -/cmd/boringproxy/boringproxy* -boringproxy_*.tar.gz - -# Boringproxy database, created if exec is run in boringproxy folder -boringproxy_db.json - -# Development files -dev.yml - -# Docker client examples -# Ignore everything in config -docker/client/examples/home-assistant/config/* -# But not these files... -!docker/client/examples/home-assistant/config/configuration.yaml \ No newline at end of file