From 9f46c2723227baae46b0d0d326d05e93a7dda9e7 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Tue, 22 Feb 2022 10:11:18 +0100 Subject: [PATCH 1/3] Very basic CLI to update repos --- cli.v | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 cli.v diff --git a/cli.v b/cli.v new file mode 100644 index 0000000..256b856 --- /dev/null +++ b/cli.v @@ -0,0 +1,84 @@ +import os +import toml +import net.http + +struct Config { + address string [required] + api_key string [required] +} + +fn list(conf Config) ? { + mut req := http.new_request(http.Method.get, '$conf.address/api/repos', '') ? + req.add_custom_header('X-API-Key', conf.api_key) ? + + res := req.do() ? + + println(res.text) +} + +fn add(conf Config, args []string) ? { + if args.len < 2 { + eprintln('Not enough arguments.') + exit(1) + } + + if args.len > 2 { + eprintln('Too many arguments.') + exit(1) + } + + mut req := http.new_request(http.Method.post, '$conf.address/api/repos?url=${args[0]}&branch=${args[1]}', '') ? + req.add_custom_header('X-API-Key', conf.api_key) ? + + res := req.do() ? + + println(res.text) +} + +fn remove(conf Config, args []string) ? { + if args.len < 2 { + eprintln('Not enough arguments.') + exit(1) + } + + if args.len > 2 { + eprintln('Too many arguments.') + exit(1) + } + + mut req := http.new_request(http.Method.delete, '$conf.address/api/repos?url=${args[0]}&branch=${args[1]}', '') ? + req.add_custom_header('X-API-Key', conf.api_key) ? + + res := req.do() ? + + println(res.text) +} + +fn main() { + conf_path := os.expand_tilde_to_home('~/.vieterrc') + + if !os.is_file(conf_path) { + exit(1) + } + + conf := toml.parse_file(conf_path) ?.reflect() + + args := os.args[1..] + + if args.len == 0 { + eprintln('No action provided.') + exit(1) + } + + action := args[0] + + match action { + 'list' { list(conf) ? } + 'add' { add(conf, args[1..]) ? } + 'remove' { remove(conf, args[1..]) ? } + else { + eprintln("Invalid action '$action'.") + exit(1) + } + } +} From f6b0e29552d3a1a21c90864bcf9d6b64b4ed0cb5 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Tue, 22 Feb 2022 18:04:54 +0100 Subject: [PATCH 2/3] Merged dockerfiles --- .gitignore | 2 ++ .woodpecker/.docker.yml | 2 -- Dockerfile | 49 +++++++++++++++++++++++++++++++++++----- Dockerfile.ci | 50 ----------------------------------------- Makefile | 11 +++++++++ 5 files changed, 56 insertions(+), 58 deletions(-) delete mode 100644 Dockerfile.ci diff --git a/.gitignore b/.gitignore index 3a6b11b..7847b3f 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,8 @@ data/ vieter dvieter pvieter +dvieterctl +vieterctl vieter.c # Ignore testing files diff --git a/.woodpecker/.docker.yml b/.woodpecker/.docker.yml index 6202fe6..b2f08ca 100644 --- a/.woodpecker/.docker.yml +++ b/.woodpecker/.docker.yml @@ -9,7 +9,6 @@ pipeline: secrets: [ docker_username, docker_password ] settings: repo: chewingbever/vieter - dockerfile: Dockerfile.ci tag: dev platforms: [ linux/arm/v7, linux/arm64/v8, linux/amd64 ] build_args_from_env: @@ -23,7 +22,6 @@ pipeline: secrets: [ docker_username, docker_password ] settings: repo: chewingbever/vieter - dockerfile: Dockerfile.ci auto_tag: true platforms: [ linux/arm/v7, linux/arm64/v8, linux/amd64 ] build_args_from_env: diff --git a/Dockerfile b/Dockerfile index 5564e34..8b62521 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,19 +1,56 @@ FROM chewingbever/vlang:latest AS builder +ARG TARGETPLATFORM +ARG CI_COMMIT_SHA +ARG DI_VER=1.2.5 + WORKDIR /app +# Build dumb-init +RUN curl -Lo - "https://github.com/Yelp/dumb-init/archive/refs/tags/v${DI_VER}.tar.gz" | tar -xzf - && \ + cd "dumb-init-${DI_VER}" && \ + make SHELL=/bin/sh && \ + mv dumb-init .. && \ + cd .. + # Copy over source code & build production binary COPY src ./src COPY Makefile ./ -ENV LDFLAGS='-lz -lbz2 -llzma -lexpat -lzstd -llz4 -static' -RUN v -o pvieter -cflags "-O3" src +RUN if [ -n "${CI_COMMIT_SHA}" ]; then \ + curl --fail \ + -o vieter \ + "https://s3.rustybever.be/vieter/commits/${CI_COMMIT_SHA}/vieter-$(echo "${TARGETPLATFORM}" | sed 's:/:-:g')" && \ + chmod +x vieter ; \ + else \ + LDFLAGS='-lz -lbz2 -llzma -lexpat -lzstd -llz4 -static' make prod && \ + mv pvieter vieter ; \ + fi -FROM alpine:3.15 +FROM busybox:1.35.0 -ENV REPO_DIR=/data +ENV PATH=/bin \ + VIETER_REPO_DIR=/data/repo \ + VIETER_PKG_DIR=/data/pkgs \ + VIETER_DOWNLOAD_DIR=/data/downloads \ + VIETER_REPOS_FILE=/data/repos.json -COPY --from=builder /app/pvieter /usr/local/bin/vieter +COPY --from=builder /app/dumb-init /app/vieter /bin/ -ENTRYPOINT [ "/usr/local/bin/vieter" ] +HEALTHCHECK --interval=30s \ + --timeout=3s \ + --start-period=5s \ + CMD /bin/wget --spider http://localhost:8000/health || exit 1 + +RUN mkdir /data && \ + chown -R www-data:www-data /data && \ + mkdir -p '/var/spool/cron/crontabs' && \ + echo '0 3 * * * /bin/vieter build' | crontab - + +WORKDIR /data + +USER www-data:www-data + +ENTRYPOINT ["/bin/dumb-init", "--"] +CMD ["/bin/vieter", "server"] diff --git a/Dockerfile.ci b/Dockerfile.ci deleted file mode 100644 index a062d95..0000000 --- a/Dockerfile.ci +++ /dev/null @@ -1,50 +0,0 @@ -# vim: ft=dockerfile -# This image just has the required tools to download the binaries -FROM chewingbever/vlang:latest AS builder - -ARG TARGETPLATFORM -ARG CI_COMMIT_SHA -ARG DI_VER=1.2.5 - -WORKDIR /app - -# Build dumb-init -RUN curl -Lo - "https://github.com/Yelp/dumb-init/archive/refs/tags/v${DI_VER}.tar.gz" | tar -xzf - && \ - cd "dumb-init-${DI_VER}" && \ - make SHELL=/bin/sh && \ - mv dumb-init .. && \ - cd .. - -RUN curl --fail \ - -o vieter \ - "https://s3.rustybever.be/vieter/commits/${CI_COMMIT_SHA}/vieter-$(echo "${TARGETPLATFORM}" | sed 's:/:-:g')" && \ - chmod +x vieter - - -FROM busybox:1.35.0 - -ENV PATH=/bin \ - VIETER_REPO_DIR=/data/repo \ - VIETER_PKG_DIR=/data/pkgs \ - VIETER_DOWNLOAD_DIR=/data/downloads \ - VIETER_REPOS_FILE=/data/repos.json - -COPY --from=builder /app/dumb-init /app/vieter /bin/ - -HEALTHCHECK --interval=30s \ - --timeout=3s \ - --start-period=5s \ - CMD /bin/wget --spider http://localhost:8000/health || exit 1 - -RUN mkdir /data && \ - chown -R www-data:www-data /data && \ - mkdir -p '/var/spool/cron/crontabs' && \ - echo '0 3 * * * /bin/vieter build' >> /var/spool/cron/crontabs/www-data && \ - chown www-data:www-data /var/spool/cron/crontabs/www-data - -WORKDIR /data - -USER www-data:www-data - -ENTRYPOINT ["/bin/dumb-init", "--"] -CMD ["/bin/vieter", "server"] diff --git a/Makefile b/Makefile index 062a47f..bd629c9 100644 --- a/Makefile +++ b/Makefile @@ -42,6 +42,17 @@ pvieter: $(SOURCES) c: $(V) -o vieter.c $(SRC_DIR) +# Build the CLI tool +.PHONY: cli +cli: dvieterctl +dvieterctl: cli.v + $(V_PATH) -showcc -o dvieterctl cli.v + +.PHONY: cli-prod +cli-prod: vieterctl +vieterctl: cli.v +cli-prod: + $(V_PATH) -showcc -o vieterctl -prod cli.v # =====EXECUTION===== # Run the server in the default 'data' directory From 098f89909d3d80f7a1bfdf0f0277565ecce21a64 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Tue, 22 Feb 2022 18:42:09 +0100 Subject: [PATCH 3/3] Added cli to CI builds --- .woodpecker/.build.yml | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/.woodpecker/.build.yml b/.woodpecker/.build.yml index 66caa8c..5bfe429 100644 --- a/.woodpecker/.build.yml +++ b/.woodpecker/.build.yml @@ -36,18 +36,49 @@ pipeline: when: event: push + cli: + image: 'chewingbever/vlang:latest' + environment: + - LDFLAGS=-static + commands: + - make cli-prod + # Make sure the binary is actually statically built + - readelf -d vieterctl + - du -h vieterctl + - '[ "$(readelf -d vieterctl | grep NEEDED | wc -l)" = 0 ]' + # This removes so much, it's amazing + - strip -s vieterctl + - du -h vieterctl + when: + event: push + upload: image: 'chewingbever/vlang:latest' secrets: [ s3_username, s3_password ] commands: # https://gist.github.com/JustinTimperio/7c7115f87b775618637d67ac911e595f - export URL=s3.rustybever.be - - export OBJ_PATH="/vieter/commits/$CI_COMMIT_SHA/vieter-$(echo '${PLATFORM}' | sed 's:/:-:g')" - export DATE="$(date -R --utc)" - export CONTENT_TYPE='application/zstd' + + - export OBJ_PATH="/vieter/commits/$CI_COMMIT_SHA/vieter-$(echo '${PLATFORM}' | sed 's:/:-:g')" - export SIG_STRING="PUT\n\n$CONTENT_TYPE\n$DATE\n$OBJ_PATH" - export SIGNATURE=`echo -en $SIG_STRING | openssl sha1 -hmac $S3_PASSWORD -binary | base64` + - > + curl + --silent + -XPUT + -T pvieter + -H "Host: $URL" + -H "Date: $DATE" + -H "Content-Type: $CONTENT_TYPE" + -H "Authorization: AWS $S3_USERNAME:$SIGNATURE" + https://$URL$OBJ_PATH + # Also update the CLI tool + - export OBJ_PATH="/vieter/commits/$CI_COMMIT_SHA/vieterctl-$(echo '${PLATFORM}' | sed 's:/:-:g')" + - export SIG_STRING="PUT\n\n$CONTENT_TYPE\n$DATE\n$OBJ_PATH" + - export SIGNATURE=`echo -en $SIG_STRING | openssl sha1 -hmac $S3_PASSWORD -binary | base64` - > curl --silent