From 068aae82c340fab5c1391f4a221f2fc5b2dfde7b Mon Sep 17 00:00:00 2001 From: MassiveBox Date: Fri, 23 Jun 2023 13:43:03 +0200 Subject: [PATCH 01/26] Fix NaN*10^(-9223372036854775808) glitch --- src/tools/tools.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/tools.go b/src/tools/tools.go index 47ab613..9d819c1 100644 --- a/src/tools/tools.go +++ b/src/tools/tools.go @@ -18,7 +18,7 @@ func TemplateDivide(num1, num2 float32) template.HTML { division := float64(num1 / num2) powerOfTen := int(math.Floor(math.Log10(division))) - if powerOfTen >= -2 && powerOfTen <= 2 { + if (powerOfTen >= -2 && powerOfTen <= 2) || division == 0 { // #nosec G203 // We're only printing floats return template.HTML(strconv.FormatFloat(math.Round(division*100)/100, 'f', -1, 64)) } From d51f42ecb1d2d847187acb0703f71d7cb5252c32 Mon Sep 17 00:00:00 2001 From: MassiveBox Date: Fri, 23 Jun 2023 16:51:43 +0200 Subject: [PATCH 02/26] Fix linter --- .golangci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.golangci.yml b/.golangci.yml index c128eff..1bd5888 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -234,6 +234,8 @@ linters: - wrapcheck - nonamedreturns - gomnd + - gosmopolitan + - depguard enable-all: true fast: false From b720bf4ac06edabfe1453026281f57f055d615fb Mon Sep 17 00:00:00 2001 From: MassiveBox Date: Fri, 23 Jun 2023 20:39:37 +0200 Subject: [PATCH 03/26] Squash bugs --- src/ecodash/database.go | 2 +- src/ecodash/http.go | 2 +- src/tools/tools.go | 6 +++++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/ecodash/database.go b/src/ecodash/database.go index 0c44e35..f580b15 100644 --- a/src/ecodash/database.go +++ b/src/ecodash/database.go @@ -75,7 +75,7 @@ func (config *Config) refreshCacheFromPast(pastTime time.Time) error { } defer stmtIgnore.Close() - for key, day := range greenEnergyPercentage { + for key, day := range historyPolledSmartEnergySummation { var stmt *sql.Stmt if greenEnergyPercentage[key].Value != 0 && historyPolledSmartEnergySummation[key].Value != 0 { stmt = stmtReplace diff --git a/src/ecodash/http.go b/src/ecodash/http.go index 7c32f2a..7fbd1b3 100644 --- a/src/ecodash/http.go +++ b/src/ecodash/http.go @@ -155,7 +155,7 @@ func (config *Config) saveAdminForm(c *fiber.Ctx) error { } func averageExcludingCurrentDay(data []float32) float32 { - if len(data) == 0 { + if len(data) <= 1 { return 0 } data = data[:len(data)-1] diff --git a/src/tools/tools.go b/src/tools/tools.go index 9d819c1..3bac1b1 100644 --- a/src/tools/tools.go +++ b/src/tools/tools.go @@ -17,8 +17,12 @@ func Hash(toHash string) string { func TemplateDivide(num1, num2 float32) template.HTML { division := float64(num1 / num2) + if math.IsNaN(division) || division == 0 { + return "0" + } + powerOfTen := int(math.Floor(math.Log10(division))) - if (powerOfTen >= -2 && powerOfTen <= 2) || division == 0 { + if powerOfTen >= -2 && powerOfTen <= 2 { // #nosec G203 // We're only printing floats return template.HTML(strconv.FormatFloat(math.Round(division*100)/100, 'f', -1, 64)) } From 002fab4786ed4bccd9ed1df1a1f725b383a41a2b Mon Sep 17 00:00:00 2001 From: MassiveBox Date: Sat, 24 Jun 2023 09:00:20 +0200 Subject: [PATCH 04/26] Optimize CI --- .woodpecker.yml | 45 -------------------- .woodpecker/Dockerfile-woodpecker | 11 +++++ .woodpecker/setup.sh | 19 +++++++++ .woodpecker/woodpecker.yml | 69 +++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 45 deletions(-) delete mode 100644 .woodpecker.yml create mode 100644 .woodpecker/Dockerfile-woodpecker create mode 100644 .woodpecker/setup.sh create mode 100644 .woodpecker/woodpecker.yml diff --git a/.woodpecker.yml b/.woodpecker.yml deleted file mode 100644 index d431220..0000000 --- a/.woodpecker.yml +++ /dev/null @@ -1,45 +0,0 @@ -pipeline: - - docker: - image: woodpeckerci/plugin-docker-buildx - settings: - registry: git.massivebox.net - repo: git.massivebox.net/ecodash/ecodash - platforms: linux/amd64,linux/arm64 - auto_tag: true - username: massivebox - password: - from_secret: auth_token - when: - event: tag - - build: - image: golang - commands: - - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin - - go mod tidy - - golangci-lint run - - go build -o ecodash-x86 src/main/main.go - - env GOOS=linux GOARCH=arm go build -o ecodash-arm src/main/main.go - - prepare-gitea-release: - image: alpine - commands: - - apk update; apk add zip - - mv ecodash-x86 ecodash; zip -r ecodash-x86.zip templates ecodash - - mv ecodash-arm ecodash; zip -r ecodash-arm.zip templates ecodash - when: - event: tag - - gitea-publish: - image: plugins/gitea-release - settings: - base_url: https://git.massivebox.net - files: - - ecodash-x86.zip - - ecodash-arm.zip - api_key: - from_secret: auth_token - title: ${CI_COMMIT_TAG} - when: - event: tag diff --git a/.woodpecker/Dockerfile-woodpecker b/.woodpecker/Dockerfile-woodpecker new file mode 100644 index 0000000..773f0f3 --- /dev/null +++ b/.woodpecker/Dockerfile-woodpecker @@ -0,0 +1,11 @@ +FROM debian:latest + +WORKDIR /app +COPY ./setup.sh ./setup.sh + +RUN apt-get update; apt-get upgrade -y; apt-get install zip curl -y && \ + curl https://cloud.massivebox.net/api/public/dl/fLgOAQNc -o templates.zip && unzip templates.zip && rm templates.zip && \ + chmod +x setup.sh && ./setup.sh && rm setup.sh && \ + chmod +x app + +CMD ["./app"] \ No newline at end of file diff --git a/.woodpecker/setup.sh b/.woodpecker/setup.sh new file mode 100644 index 0000000..f9d0741 --- /dev/null +++ b/.woodpecker/setup.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +ARCH=$(arch) + +# This is a workaround to Woodpecker's inability to give files it has just built to Docker BuildX to build new images. +# After compiling the binaries in the "build-and-format" step of woodpecker.yml, we upload them to the cloud and fetch them from here. + +if [ "$ARCH" = "x86_64" ]; then + echo "detected amd64" + curl https://cloud.massivebox.net/api/public/dl/uZaDQXAa -o app +elif [ "$ARCH" = "aarch64" ]; then + echo "deteched arm" + curl https://cloud.massivebox.net/api/public/dl/EhM62nhf -o app +else + echo "unsupported architecture" + return 1 +fi + +return 0 \ No newline at end of file diff --git a/.woodpecker/woodpecker.yml b/.woodpecker/woodpecker.yml new file mode 100644 index 0000000..7a670bb --- /dev/null +++ b/.woodpecker/woodpecker.yml @@ -0,0 +1,69 @@ +pipeline: + + build-and-format: + image: golang + commands: + - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin + - go mod tidy + - golangci-lint run + - env GOOS=linux GOARCH=amd64 go build -o ecodash-x86 src/main/main.go + - env GOOS=linux GOARCH=arm go build -o ecodash-arm src/main/main.go + - apt-get update; apt-get upgrade -y; apt-get install zip -y + - mv ecodash-x86 ecodash; zip -r ecodash-x86.zip templates ecodash; mv ecodash ecodash-x86 + - mv ecodash-arm ecodash; zip -r ecodash-arm.zip templates ecodash; mv ecodash ecodash-arm + - zip templates.zip -r templates + + upload-debug: + image: vividboarder/drone-webdav + settings: + file: { ecodash-x86,ecodash-arm,templates.zip } + destination: + from_secret: webdav_destination + username: + from_secret: webdav_username + password: + from_secret: webdav_password + attempts: 5 + + gitea-publish: + image: plugins/gitea-release + settings: + base_url: https://git.massivebox.net + files: + - ecodash-x86.zip + - ecodash-arm.zip + api_key: + from_secret: auth_token + title: ${CI_COMMIT_TAG} + when: + event: tag + + docker-unstable: + image: woodpeckerci/plugin-docker-buildx + settings: + registry: git.massivebox.net + repo: git.massivebox.net/ecodash/ecodash + platforms: linux/amd64,linux/arm64 + tag: unstable + username: massivebox + password: + from_secret: auth_token + context: .woodpecker + dockerfile: .woodpecker/Dockerfile-woodpecker + when: + event: [ push, pull_request, deployment ] + + docker-tag: + image: woodpeckerci/plugin-docker-buildx + settings: + registry: git.massivebox.net + repo: git.massivebox.net/ecodash/ecodash + platforms: linux/amd64,linux/arm64 + auto_tag: true + username: massivebox + password: + from_secret: auth_token + context: .woodpecker + dockerfile: .woodpecker/Dockerfile-woodpecker + when: + event: tag From 82114b8c76a7123a1ab4b1e01ec4689256c82d20 Mon Sep 17 00:00:00 2001 From: MassiveBox Date: Sat, 24 Jun 2023 09:00:20 +0200 Subject: [PATCH 05/26] Optimize CI --- .woodpecker.yml | 45 -------------------- .woodpecker/.woodpecker.yml | 69 +++++++++++++++++++++++++++++++ .woodpecker/Dockerfile-woodpecker | 11 +++++ .woodpecker/setup.sh | 19 +++++++++ 4 files changed, 99 insertions(+), 45 deletions(-) delete mode 100644 .woodpecker.yml create mode 100644 .woodpecker/.woodpecker.yml create mode 100644 .woodpecker/Dockerfile-woodpecker create mode 100644 .woodpecker/setup.sh diff --git a/.woodpecker.yml b/.woodpecker.yml deleted file mode 100644 index d431220..0000000 --- a/.woodpecker.yml +++ /dev/null @@ -1,45 +0,0 @@ -pipeline: - - docker: - image: woodpeckerci/plugin-docker-buildx - settings: - registry: git.massivebox.net - repo: git.massivebox.net/ecodash/ecodash - platforms: linux/amd64,linux/arm64 - auto_tag: true - username: massivebox - password: - from_secret: auth_token - when: - event: tag - - build: - image: golang - commands: - - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin - - go mod tidy - - golangci-lint run - - go build -o ecodash-x86 src/main/main.go - - env GOOS=linux GOARCH=arm go build -o ecodash-arm src/main/main.go - - prepare-gitea-release: - image: alpine - commands: - - apk update; apk add zip - - mv ecodash-x86 ecodash; zip -r ecodash-x86.zip templates ecodash - - mv ecodash-arm ecodash; zip -r ecodash-arm.zip templates ecodash - when: - event: tag - - gitea-publish: - image: plugins/gitea-release - settings: - base_url: https://git.massivebox.net - files: - - ecodash-x86.zip - - ecodash-arm.zip - api_key: - from_secret: auth_token - title: ${CI_COMMIT_TAG} - when: - event: tag diff --git a/.woodpecker/.woodpecker.yml b/.woodpecker/.woodpecker.yml new file mode 100644 index 0000000..7a670bb --- /dev/null +++ b/.woodpecker/.woodpecker.yml @@ -0,0 +1,69 @@ +pipeline: + + build-and-format: + image: golang + commands: + - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin + - go mod tidy + - golangci-lint run + - env GOOS=linux GOARCH=amd64 go build -o ecodash-x86 src/main/main.go + - env GOOS=linux GOARCH=arm go build -o ecodash-arm src/main/main.go + - apt-get update; apt-get upgrade -y; apt-get install zip -y + - mv ecodash-x86 ecodash; zip -r ecodash-x86.zip templates ecodash; mv ecodash ecodash-x86 + - mv ecodash-arm ecodash; zip -r ecodash-arm.zip templates ecodash; mv ecodash ecodash-arm + - zip templates.zip -r templates + + upload-debug: + image: vividboarder/drone-webdav + settings: + file: { ecodash-x86,ecodash-arm,templates.zip } + destination: + from_secret: webdav_destination + username: + from_secret: webdav_username + password: + from_secret: webdav_password + attempts: 5 + + gitea-publish: + image: plugins/gitea-release + settings: + base_url: https://git.massivebox.net + files: + - ecodash-x86.zip + - ecodash-arm.zip + api_key: + from_secret: auth_token + title: ${CI_COMMIT_TAG} + when: + event: tag + + docker-unstable: + image: woodpeckerci/plugin-docker-buildx + settings: + registry: git.massivebox.net + repo: git.massivebox.net/ecodash/ecodash + platforms: linux/amd64,linux/arm64 + tag: unstable + username: massivebox + password: + from_secret: auth_token + context: .woodpecker + dockerfile: .woodpecker/Dockerfile-woodpecker + when: + event: [ push, pull_request, deployment ] + + docker-tag: + image: woodpeckerci/plugin-docker-buildx + settings: + registry: git.massivebox.net + repo: git.massivebox.net/ecodash/ecodash + platforms: linux/amd64,linux/arm64 + auto_tag: true + username: massivebox + password: + from_secret: auth_token + context: .woodpecker + dockerfile: .woodpecker/Dockerfile-woodpecker + when: + event: tag diff --git a/.woodpecker/Dockerfile-woodpecker b/.woodpecker/Dockerfile-woodpecker new file mode 100644 index 0000000..773f0f3 --- /dev/null +++ b/.woodpecker/Dockerfile-woodpecker @@ -0,0 +1,11 @@ +FROM debian:latest + +WORKDIR /app +COPY ./setup.sh ./setup.sh + +RUN apt-get update; apt-get upgrade -y; apt-get install zip curl -y && \ + curl https://cloud.massivebox.net/api/public/dl/fLgOAQNc -o templates.zip && unzip templates.zip && rm templates.zip && \ + chmod +x setup.sh && ./setup.sh && rm setup.sh && \ + chmod +x app + +CMD ["./app"] \ No newline at end of file diff --git a/.woodpecker/setup.sh b/.woodpecker/setup.sh new file mode 100644 index 0000000..f9d0741 --- /dev/null +++ b/.woodpecker/setup.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +ARCH=$(arch) + +# This is a workaround to Woodpecker's inability to give files it has just built to Docker BuildX to build new images. +# After compiling the binaries in the "build-and-format" step of woodpecker.yml, we upload them to the cloud and fetch them from here. + +if [ "$ARCH" = "x86_64" ]; then + echo "detected amd64" + curl https://cloud.massivebox.net/api/public/dl/uZaDQXAa -o app +elif [ "$ARCH" = "aarch64" ]; then + echo "deteched arm" + curl https://cloud.massivebox.net/api/public/dl/EhM62nhf -o app +else + echo "unsupported architecture" + return 1 +fi + +return 0 \ No newline at end of file From 33f09c93bd26bd2e93ca419990a03733ea32873d Mon Sep 17 00:00:00 2001 From: MassiveBox Date: Fri, 21 Jul 2023 09:43:36 +0200 Subject: [PATCH 06/26] Attempt at adding Jenkins CI --- .woodpecker/.woodpecker.yml | 69 --------------------------- .woodpecker/Dockerfile-woodpecker | 11 ----- .woodpecker/setup.sh | 19 -------- .woodpecker/woodpecker.yml | 69 --------------------------- Jenkinsfile | 77 +++++++++++++++++++++++++++++++ jenkins/Dockerfile | 0 jenkins/Jenkinsfile | 76 ++++++++++++++++++++++++++++++ 7 files changed, 153 insertions(+), 168 deletions(-) delete mode 100644 .woodpecker/.woodpecker.yml delete mode 100644 .woodpecker/Dockerfile-woodpecker delete mode 100644 .woodpecker/setup.sh delete mode 100644 .woodpecker/woodpecker.yml create mode 100644 Jenkinsfile create mode 100644 jenkins/Dockerfile create mode 100644 jenkins/Jenkinsfile diff --git a/.woodpecker/.woodpecker.yml b/.woodpecker/.woodpecker.yml deleted file mode 100644 index 7a670bb..0000000 --- a/.woodpecker/.woodpecker.yml +++ /dev/null @@ -1,69 +0,0 @@ -pipeline: - - build-and-format: - image: golang - commands: - - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin - - go mod tidy - - golangci-lint run - - env GOOS=linux GOARCH=amd64 go build -o ecodash-x86 src/main/main.go - - env GOOS=linux GOARCH=arm go build -o ecodash-arm src/main/main.go - - apt-get update; apt-get upgrade -y; apt-get install zip -y - - mv ecodash-x86 ecodash; zip -r ecodash-x86.zip templates ecodash; mv ecodash ecodash-x86 - - mv ecodash-arm ecodash; zip -r ecodash-arm.zip templates ecodash; mv ecodash ecodash-arm - - zip templates.zip -r templates - - upload-debug: - image: vividboarder/drone-webdav - settings: - file: { ecodash-x86,ecodash-arm,templates.zip } - destination: - from_secret: webdav_destination - username: - from_secret: webdav_username - password: - from_secret: webdav_password - attempts: 5 - - gitea-publish: - image: plugins/gitea-release - settings: - base_url: https://git.massivebox.net - files: - - ecodash-x86.zip - - ecodash-arm.zip - api_key: - from_secret: auth_token - title: ${CI_COMMIT_TAG} - when: - event: tag - - docker-unstable: - image: woodpeckerci/plugin-docker-buildx - settings: - registry: git.massivebox.net - repo: git.massivebox.net/ecodash/ecodash - platforms: linux/amd64,linux/arm64 - tag: unstable - username: massivebox - password: - from_secret: auth_token - context: .woodpecker - dockerfile: .woodpecker/Dockerfile-woodpecker - when: - event: [ push, pull_request, deployment ] - - docker-tag: - image: woodpeckerci/plugin-docker-buildx - settings: - registry: git.massivebox.net - repo: git.massivebox.net/ecodash/ecodash - platforms: linux/amd64,linux/arm64 - auto_tag: true - username: massivebox - password: - from_secret: auth_token - context: .woodpecker - dockerfile: .woodpecker/Dockerfile-woodpecker - when: - event: tag diff --git a/.woodpecker/Dockerfile-woodpecker b/.woodpecker/Dockerfile-woodpecker deleted file mode 100644 index 773f0f3..0000000 --- a/.woodpecker/Dockerfile-woodpecker +++ /dev/null @@ -1,11 +0,0 @@ -FROM debian:latest - -WORKDIR /app -COPY ./setup.sh ./setup.sh - -RUN apt-get update; apt-get upgrade -y; apt-get install zip curl -y && \ - curl https://cloud.massivebox.net/api/public/dl/fLgOAQNc -o templates.zip && unzip templates.zip && rm templates.zip && \ - chmod +x setup.sh && ./setup.sh && rm setup.sh && \ - chmod +x app - -CMD ["./app"] \ No newline at end of file diff --git a/.woodpecker/setup.sh b/.woodpecker/setup.sh deleted file mode 100644 index f9d0741..0000000 --- a/.woodpecker/setup.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/sh - -ARCH=$(arch) - -# This is a workaround to Woodpecker's inability to give files it has just built to Docker BuildX to build new images. -# After compiling the binaries in the "build-and-format" step of woodpecker.yml, we upload them to the cloud and fetch them from here. - -if [ "$ARCH" = "x86_64" ]; then - echo "detected amd64" - curl https://cloud.massivebox.net/api/public/dl/uZaDQXAa -o app -elif [ "$ARCH" = "aarch64" ]; then - echo "deteched arm" - curl https://cloud.massivebox.net/api/public/dl/EhM62nhf -o app -else - echo "unsupported architecture" - return 1 -fi - -return 0 \ No newline at end of file diff --git a/.woodpecker/woodpecker.yml b/.woodpecker/woodpecker.yml deleted file mode 100644 index 7a670bb..0000000 --- a/.woodpecker/woodpecker.yml +++ /dev/null @@ -1,69 +0,0 @@ -pipeline: - - build-and-format: - image: golang - commands: - - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin - - go mod tidy - - golangci-lint run - - env GOOS=linux GOARCH=amd64 go build -o ecodash-x86 src/main/main.go - - env GOOS=linux GOARCH=arm go build -o ecodash-arm src/main/main.go - - apt-get update; apt-get upgrade -y; apt-get install zip -y - - mv ecodash-x86 ecodash; zip -r ecodash-x86.zip templates ecodash; mv ecodash ecodash-x86 - - mv ecodash-arm ecodash; zip -r ecodash-arm.zip templates ecodash; mv ecodash ecodash-arm - - zip templates.zip -r templates - - upload-debug: - image: vividboarder/drone-webdav - settings: - file: { ecodash-x86,ecodash-arm,templates.zip } - destination: - from_secret: webdav_destination - username: - from_secret: webdav_username - password: - from_secret: webdav_password - attempts: 5 - - gitea-publish: - image: plugins/gitea-release - settings: - base_url: https://git.massivebox.net - files: - - ecodash-x86.zip - - ecodash-arm.zip - api_key: - from_secret: auth_token - title: ${CI_COMMIT_TAG} - when: - event: tag - - docker-unstable: - image: woodpeckerci/plugin-docker-buildx - settings: - registry: git.massivebox.net - repo: git.massivebox.net/ecodash/ecodash - platforms: linux/amd64,linux/arm64 - tag: unstable - username: massivebox - password: - from_secret: auth_token - context: .woodpecker - dockerfile: .woodpecker/Dockerfile-woodpecker - when: - event: [ push, pull_request, deployment ] - - docker-tag: - image: woodpeckerci/plugin-docker-buildx - settings: - registry: git.massivebox.net - repo: git.massivebox.net/ecodash/ecodash - platforms: linux/amd64,linux/arm64 - auto_tag: true - username: massivebox - password: - from_secret: auth_token - context: .woodpecker - dockerfile: .woodpecker/Dockerfile-woodpecker - when: - event: tag diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..5d19417 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,77 @@ +pipeline { + + agent any + + environment { + USER='placeholder' + PASSWORD='placeholder' + DOCKER_REGISTRY='git.massivebox.net' + BUILDER_NAME='mbuilder' + SERVICE='ecodash/ecodash' + TAG='latest' + } + + stages { + + stage('Run linter and build') { + agent { docker { image 'golang' } } + steps { + git url: 'https://git.massivebox.net/ecodash/ecodash' + sh 'curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin' + sh 'go mod tidy' + sh 'golangci-lint run' + sh 'env GOOS=linux GOARCH=amd64 go build -o app src/main/main.go' + stash includes: 'app', name: 'ecodash-x86' + sh 'env GOOS=linux GOARCH=arm go build -o app src/main/main.go' + stash includes: 'app', name: 'ecodash-arm' + stash includes: 'jenkins/Dockerfile', name: 'dockerfile' + stash includes: 'templates/**', name: 'templates' + } + } + + + stage('Build x86 container') { + steps { + unstash 'dockerfile' + unstash 'ecodash-x86' + unstash 'templates' + sh 'docker build -t $DOCKER_REGISTRY/$SERVICE:$TAG .' + } + } + + stage('Prepare buildx') { + steps { + sh """ + docker run --privileged --rm tonistiigi/binfmt --install all + + docker context create $BUILDER_NAME + docker context use $BUILDER_NAME + docker buildx create $BUILDER_NAME + docker buildx use $BUILDER_NAME + docker buildx inspect --bootstrap + """ + } + } + + stage('Build arm container') { + steps { + unstash 'dockerfile' + unstash 'ecodash-arm' + unstash 'templates' + sh 'docker buildx build --platform linux/arm64 -t $DOCKER_REGISTRY/$SERVICE:$TAG .' + } + } + + } + + post { + always { + // cleanup + sh """ + docker context rm -f $BUILDER_NAME + docker buildx use default + """ + } + } + +} diff --git a/jenkins/Dockerfile b/jenkins/Dockerfile new file mode 100644 index 0000000..e69de29 diff --git a/jenkins/Jenkinsfile b/jenkins/Jenkinsfile new file mode 100644 index 0000000..c7d6982 --- /dev/null +++ b/jenkins/Jenkinsfile @@ -0,0 +1,76 @@ +pipeline { + + agent any + + environment { + USER='user' + PASSWORD='password' + DOCKER_REGISTRY='git.massivebox.net' + BUILDER_NAME='mbuilder' + SERVICE='ecodash/ecodash' + TAG='latest' + } + + stages { + + stage('Run linter and build') { + agent { docker { image 'golang' } } + steps { + git url: 'https://git.massivebox.net/ecodash/ecodash' + //sh 'curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin' + sh 'go mod tidy' + //sh 'golangci-lint run' + sh 'env GOOS=linux GOARCH=amd64 go build -o app src/main/main.go' + stash includes: 'app', name: 'ecodash-x86' + sh 'env GOOS=linux GOARCH=arm go build -o app src/main/main.go' + stash includes: 'app', name: 'ecodash-arm' + stash includes: 'templates/**', name: 'templates' + } + } + + + stage('Build x86 container') { + steps { + sh 'curl -L -o Dockerfile https://pasty.nikko.cf/UYU39i/raw' + unstash 'ecodash-x86' + unstash 'templates' + sh 'docker build -t $DOCKER_REGISTRY/$SERVICE:$TAG .' + } + } + + stage('Prepare buildx') { + steps { + sh """ + docker run --privileged --rm tonistiigi/binfmt --install all + + docker context create $BUILDER_NAME + docker context use $BUILDER_NAME + docker buildx create $BUILDER_NAME + docker buildx use $BUILDER_NAME + docker buildx inspect --bootstrap + """ + } + } + + stage('Build arm container') { + steps { + sh 'curl -L -o Dockerfile https://pasty.nikko.cf/UYU39i/raw' + unstash 'ecodash-arm' + unstash 'templates' + sh 'docker buildx build --platform linux/arm64 -t $DOCKER_REGISTRY/$SERVICE:$TAG .' + } + } + + } + + post { + always { + // cleanup + sh """ + docker context rm -f $BUILDER_NAME + docker buildx use default + """ + } + } + +} From 90e83eaf624412f4a05f1136560e1ba248d067dd Mon Sep 17 00:00:00 2001 From: MassiveBox Date: Fri, 21 Jul 2023 17:01:53 +0200 Subject: [PATCH 07/26] Pipeline improvements --- Jenkinsfile | 77 --------------------------------------------- README.md | 2 +- jenkins/Jenkinsfile | 32 +++++++++++++------ 3 files changed, 23 insertions(+), 88 deletions(-) delete mode 100644 Jenkinsfile diff --git a/Jenkinsfile b/Jenkinsfile deleted file mode 100644 index 5d19417..0000000 --- a/Jenkinsfile +++ /dev/null @@ -1,77 +0,0 @@ -pipeline { - - agent any - - environment { - USER='placeholder' - PASSWORD='placeholder' - DOCKER_REGISTRY='git.massivebox.net' - BUILDER_NAME='mbuilder' - SERVICE='ecodash/ecodash' - TAG='latest' - } - - stages { - - stage('Run linter and build') { - agent { docker { image 'golang' } } - steps { - git url: 'https://git.massivebox.net/ecodash/ecodash' - sh 'curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin' - sh 'go mod tidy' - sh 'golangci-lint run' - sh 'env GOOS=linux GOARCH=amd64 go build -o app src/main/main.go' - stash includes: 'app', name: 'ecodash-x86' - sh 'env GOOS=linux GOARCH=arm go build -o app src/main/main.go' - stash includes: 'app', name: 'ecodash-arm' - stash includes: 'jenkins/Dockerfile', name: 'dockerfile' - stash includes: 'templates/**', name: 'templates' - } - } - - - stage('Build x86 container') { - steps { - unstash 'dockerfile' - unstash 'ecodash-x86' - unstash 'templates' - sh 'docker build -t $DOCKER_REGISTRY/$SERVICE:$TAG .' - } - } - - stage('Prepare buildx') { - steps { - sh """ - docker run --privileged --rm tonistiigi/binfmt --install all - - docker context create $BUILDER_NAME - docker context use $BUILDER_NAME - docker buildx create $BUILDER_NAME - docker buildx use $BUILDER_NAME - docker buildx inspect --bootstrap - """ - } - } - - stage('Build arm container') { - steps { - unstash 'dockerfile' - unstash 'ecodash-arm' - unstash 'templates' - sh 'docker buildx build --platform linux/arm64 -t $DOCKER_REGISTRY/$SERVICE:$TAG .' - } - } - - } - - post { - always { - // cleanup - sh """ - docker context rm -f $BUILDER_NAME - docker buildx use default - """ - } - } - -} diff --git a/README.md b/README.md index f3c9f66..0469ba7 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # 🌿 EcoDash -[![status-badge](https://woodpecker.massivebox.net/api/badges/ecodash/ecodash/status.svg)](https://woodpecker.massivebox.net/ecodash/ecodash) [![Visit our website](https://cloud.massivebox.net/api/public/dl/yEzoZyW8?inline=true)](https://ecodash.xyz) [![Support the project](https://cloud.massivebox.net/api/public/dl/dthbBylL?inline=true)](https://ecodash.xyz/contribute) +[![status-badge](https://woodpecker.massivebox.net/api/badges/ecodash/ecodash/status.svg)](https://woodpecker.massivebox.net/ecodash/ecohttpsdash) [![Visit our website](https://cloud.massivebox.net/api/public/dl/yEzoZyW8?inline=true)](https://ecodash.xyz) [![Support the project](https://cloud.massivebox.net/api/public/dl/dthbBylL?inline=true)](https://ecodash.xyz/contribute) EcoDash is a simple way to show your users how much your server consumes. It's intended as a medium of transparency, that gives your users an idea about the consumption of your machine. It's not meant to be 100% accurate. diff --git a/jenkins/Jenkinsfile b/jenkins/Jenkinsfile index c7d6982..7833d6c 100644 --- a/jenkins/Jenkinsfile +++ b/jenkins/Jenkinsfile @@ -3,12 +3,9 @@ pipeline { agent any environment { - USER='user' - PASSWORD='password' DOCKER_REGISTRY='git.massivebox.net' BUILDER_NAME='mbuilder' SERVICE='ecodash/ecodash' - TAG='latest' } stages { @@ -16,14 +13,15 @@ pipeline { stage('Run linter and build') { agent { docker { image 'golang' } } steps { - git url: 'https://git.massivebox.net/ecodash/ecodash' - //sh 'curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin' + checkout scm + sh 'curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin' sh 'go mod tidy' - //sh 'golangci-lint run' + sh 'golangci-lint run' sh 'env GOOS=linux GOARCH=amd64 go build -o app src/main/main.go' stash includes: 'app', name: 'ecodash-x86' sh 'env GOOS=linux GOARCH=arm go build -o app src/main/main.go' stash includes: 'app', name: 'ecodash-arm' + stash includes: 'jenkins/Dockerfile', name: 'dockerfile' stash includes: 'templates/**', name: 'templates' } } @@ -31,10 +29,10 @@ pipeline { stage('Build x86 container') { steps { - sh 'curl -L -o Dockerfile https://pasty.nikko.cf/UYU39i/raw' + unstash 'dockerfile' unstash 'ecodash-x86' unstash 'templates' - sh 'docker build -t $DOCKER_REGISTRY/$SERVICE:$TAG .' + sh 'docker build -t ecodash .' } } @@ -54,10 +52,23 @@ pipeline { stage('Build arm container') { steps { - sh 'curl -L -o Dockerfile https://pasty.nikko.cf/UYU39i/raw' + unstash 'dockerfile' unstash 'ecodash-arm' unstash 'templates' - sh 'docker buildx build --platform linux/arm64 -t $DOCKER_REGISTRY/$SERVICE:$TAG .' + sh 'docker buildx build --platform linux/arm64 -t ecodash .' + } + } + + stage('Publish container on tag latest') { + when { branch 'master' } + steps { + withCredentials([usernamePassword(credentialsId: 'gitea-credentials', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) { + sh 'docker login -u $USER -p $PASSWORD $DOCKER_REGISTRY' + } + sh """ + docker image tag ecodash $DOCKER_REGISTRY/$SERVICE:$TAG + docker push $DOCKER_REGISTRY/$SERVICE:$TAG + """ } } @@ -74,3 +85,4 @@ pipeline { } } + From 394091d885af53edb1067d53961a40799c6f02c1 Mon Sep 17 00:00:00 2001 From: MassiveBox Date: Fri, 21 Jul 2023 18:10:35 +0200 Subject: [PATCH 08/26] Fix pipeline --- jenkins/Jenkinsfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jenkins/Jenkinsfile b/jenkins/Jenkinsfile index 7833d6c..b58cdb1 100644 --- a/jenkins/Jenkinsfile +++ b/jenkins/Jenkinsfile @@ -32,7 +32,7 @@ pipeline { unstash 'dockerfile' unstash 'ecodash-x86' unstash 'templates' - sh 'docker build -t ecodash .' + sh 'cp jenkins/Dockerfile ./Dockerfile; docker build -t ecodash .' } } @@ -55,7 +55,7 @@ pipeline { unstash 'dockerfile' unstash 'ecodash-arm' unstash 'templates' - sh 'docker buildx build --platform linux/arm64 -t ecodash .' + sh 'cp jenkins/Dockerfile ./Dockerfile; docker buildx build --platform linux/arm64 -t ecodash .' } } From d0f8950c3cc4cd018229d06ce9b029e0ba7a6381 Mon Sep 17 00:00:00 2001 From: MassiveBox Date: Fri, 21 Jul 2023 18:30:25 +0200 Subject: [PATCH 09/26] Add jenkins'Dockerfile whoops I forgot --- jenkins/Dockerfile | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/jenkins/Dockerfile b/jenkins/Dockerfile index e69de29..1cfaa85 100644 --- a/jenkins/Dockerfile +++ b/jenkins/Dockerfile @@ -0,0 +1,7 @@ +FROM debian:latest + +WORKDIR /app +COPY app app +COPY templates templates + +CMD ["./app"] From 97994ab47abc45a3af4fa9b5fb52cb4e3f690c69 Mon Sep 17 00:00:00 2001 From: MassiveBox Date: Fri, 21 Jul 2023 18:46:31 +0200 Subject: [PATCH 10/26] Minor inconvenience --- jenkins/Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jenkins/Jenkinsfile b/jenkins/Jenkinsfile index b58cdb1..a2e64e7 100644 --- a/jenkins/Jenkinsfile +++ b/jenkins/Jenkinsfile @@ -63,7 +63,7 @@ pipeline { when { branch 'master' } steps { withCredentials([usernamePassword(credentialsId: 'gitea-credentials', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) { - sh 'docker login -u $USER -p $PASSWORD $DOCKER_REGISTRY' + sh 'docker login -u $USERNAME -p $PASSWORD $DOCKER_REGISTRY' } sh """ docker image tag ecodash $DOCKER_REGISTRY/$SERVICE:$TAG From 6bfe31de5696092424a3242eb7c4e852f4e9973f Mon Sep 17 00:00:00 2001 From: MassiveBox Date: Fri, 21 Jul 2023 18:59:59 +0200 Subject: [PATCH 11/26] Whoops --- jenkins/Jenkinsfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jenkins/Jenkinsfile b/jenkins/Jenkinsfile index a2e64e7..e94582b 100644 --- a/jenkins/Jenkinsfile +++ b/jenkins/Jenkinsfile @@ -66,8 +66,8 @@ pipeline { sh 'docker login -u $USERNAME -p $PASSWORD $DOCKER_REGISTRY' } sh """ - docker image tag ecodash $DOCKER_REGISTRY/$SERVICE:$TAG - docker push $DOCKER_REGISTRY/$SERVICE:$TAG + docker image tag ecodash $DOCKER_REGISTRY/$SERVICE:latest + docker push $DOCKER_REGISTRY/$SERVICE:latest """ } } From c650a1fae1b2c8f114af55aad03d3ffe34072c63 Mon Sep 17 00:00:00 2001 From: MassiveBox Date: Sat, 22 Jul 2023 10:24:01 +0200 Subject: [PATCH 12/26] Fix multi-arch container build --- jenkins/Dockerfile | 5 ++++- jenkins/Jenkinsfile | 34 +++++++++++++++++----------------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/jenkins/Dockerfile b/jenkins/Dockerfile index 1cfaa85..96d5a1d 100644 --- a/jenkins/Dockerfile +++ b/jenkins/Dockerfile @@ -1,7 +1,10 @@ FROM debian:latest WORKDIR /app -COPY app app +COPY ecodash_arm ecodash_arm +COPY ecodash_x86 ecodash_x86 COPY templates templates +RUN if [ "$(uname -m)" = "aarch64" ]; then mv ecodash_arm app; rm ecodash_x86; else mv ecodash_x86 app; rm ecodash_arm fi + CMD ["./app"] diff --git a/jenkins/Jenkinsfile b/jenkins/Jenkinsfile index e94582b..d97746e 100644 --- a/jenkins/Jenkinsfile +++ b/jenkins/Jenkinsfile @@ -17,25 +17,15 @@ pipeline { sh 'curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin' sh 'go mod tidy' sh 'golangci-lint run' - sh 'env GOOS=linux GOARCH=amd64 go build -o app src/main/main.go' - stash includes: 'app', name: 'ecodash-x86' - sh 'env GOOS=linux GOARCH=arm go build -o app src/main/main.go' - stash includes: 'app', name: 'ecodash-arm' + sh 'env GOOS=linux GOARCH=amd64 go build -o ecodash_x86 src/main/main.go' + stash includes: 'ecodash_x86', name: 'ecodash_x86' + sh 'env GOOS=linux GOARCH=arm go build -o ecodash_arm src/main/main.go' + stash includes: 'ecodash_arm', name: 'ecodash_arm' stash includes: 'jenkins/Dockerfile', name: 'dockerfile' stash includes: 'templates/**', name: 'templates' } } - - stage('Build x86 container') { - steps { - unstash 'dockerfile' - unstash 'ecodash-x86' - unstash 'templates' - sh 'cp jenkins/Dockerfile ./Dockerfile; docker build -t ecodash .' - } - } - stage('Prepare buildx') { steps { sh """ @@ -50,12 +40,22 @@ pipeline { } } - stage('Build arm container') { + stage('Build multi-arch container') { steps { unstash 'dockerfile' - unstash 'ecodash-arm' + unstash 'ecodash_x86' + unstash 'ecodash_arm' unstash 'templates' - sh 'cp jenkins/Dockerfile ./Dockerfile; docker buildx build --platform linux/arm64 -t ecodash .' + sh 'cp jenkins/Dockerfile ./Dockerfile; docker buildx build --platform linux/amd64,linux/arm64 -t ecodash .' + } + } + + stage('Publish built files') { + steps { + sh 'mv ecodash_x86 ecodash' + archiveArtifacts artifacts: ['templates/**', 'ecodash'] name: 'ecodash-x86' + sh 'mv ecodash_arm ecodash' + archiveArtifacts artifacts: ['templates/**', 'ecodash'] name: 'ecodash-arm' } } From fa28b77c5214236ed514d759bb7f7036cee0cd11 Mon Sep 17 00:00:00 2001 From: MassiveBox Date: Sat, 22 Jul 2023 10:26:14 +0200 Subject: [PATCH 13/26] Add missing commas --- jenkins/Jenkinsfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jenkins/Jenkinsfile b/jenkins/Jenkinsfile index d97746e..85dc69f 100644 --- a/jenkins/Jenkinsfile +++ b/jenkins/Jenkinsfile @@ -53,9 +53,9 @@ pipeline { stage('Publish built files') { steps { sh 'mv ecodash_x86 ecodash' - archiveArtifacts artifacts: ['templates/**', 'ecodash'] name: 'ecodash-x86' + archiveArtifacts artifacts: ['templates/**', 'ecodash'], name: 'ecodash-x86' sh 'mv ecodash_arm ecodash' - archiveArtifacts artifacts: ['templates/**', 'ecodash'] name: 'ecodash-arm' + archiveArtifacts artifacts: ['templates/**', 'ecodash'], name: 'ecodash-arm' } } From ad89006cc47213f610be76611673cc965c674b33 Mon Sep 17 00:00:00 2001 From: MassiveBox Date: Sat, 22 Jul 2023 10:34:21 +0200 Subject: [PATCH 14/26] Fix published artifacts --- jenkins/Jenkinsfile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/jenkins/Jenkinsfile b/jenkins/Jenkinsfile index 85dc69f..955f4e7 100644 --- a/jenkins/Jenkinsfile +++ b/jenkins/Jenkinsfile @@ -52,10 +52,10 @@ pipeline { stage('Publish built files') { steps { - sh 'mv ecodash_x86 ecodash' - archiveArtifacts artifacts: ['templates/**', 'ecodash'], name: 'ecodash-x86' - sh 'mv ecodash_arm ecodash' - archiveArtifacts artifacts: ['templates/**', 'ecodash'], name: 'ecodash-arm' + sh 'mv ecodash_x86 ecodash; zip -r ecodash-x86.zip templates ecodash' + archiveArtifacts artifacts: "ecodash-x86.zip" + sh 'mv ecodash_arm ecodash; zip -r ecodash-arm.zip templates ecodash' + archiveArtifacts artifacts: "ecodash-arm.zip" } } From 8db3f56ca45d831f751f2f322e07bbf3144763c8 Mon Sep 17 00:00:00 2001 From: MassiveBox Date: Sat, 22 Jul 2023 11:14:54 +0200 Subject: [PATCH 15/26] Hopefully fix docker complaining about nonsense --- jenkins/Jenkinsfile | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/jenkins/Jenkinsfile b/jenkins/Jenkinsfile index 955f4e7..1874a41 100644 --- a/jenkins/Jenkinsfile +++ b/jenkins/Jenkinsfile @@ -29,11 +29,7 @@ pipeline { stage('Prepare buildx') { steps { sh """ - docker run --privileged --rm tonistiigi/binfmt --install all - - docker context create $BUILDER_NAME - docker context use $BUILDER_NAME - docker buildx create $BUILDER_NAME + docker buildx create --name $BUILDER_NAME docker buildx use $BUILDER_NAME docker buildx inspect --bootstrap """ @@ -77,10 +73,7 @@ pipeline { post { always { // cleanup - sh """ - docker context rm -f $BUILDER_NAME - docker buildx use default - """ + sh 'docker buildx rm $BUILDER_NAME' } } From 07b9571ffa285dbbed7f2d98d9aa8332648b19a0 Mon Sep 17 00:00:00 2001 From: MassiveBox Date: Sat, 22 Jul 2023 11:34:01 +0200 Subject: [PATCH 16/26] Fix --- jenkins/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jenkins/Dockerfile b/jenkins/Dockerfile index 96d5a1d..82c8879 100644 --- a/jenkins/Dockerfile +++ b/jenkins/Dockerfile @@ -5,6 +5,6 @@ COPY ecodash_arm ecodash_arm COPY ecodash_x86 ecodash_x86 COPY templates templates -RUN if [ "$(uname -m)" = "aarch64" ]; then mv ecodash_arm app; rm ecodash_x86; else mv ecodash_x86 app; rm ecodash_arm fi +RUN if [ "$(uname -m)" = "aarch64" ]; then mv ecodash_arm app; rm ecodash_x86; else mv ecodash_x86 app; rm ecodash_arm; fi CMD ["./app"] From 153b507a69c3d3708d464a53494f99058f9c29ca Mon Sep 17 00:00:00 2001 From: MassiveBox Date: Sat, 22 Jul 2023 12:27:56 +0200 Subject: [PATCH 17/26] Load image into docker --- jenkins/Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jenkins/Jenkinsfile b/jenkins/Jenkinsfile index 1874a41..5c2ee18 100644 --- a/jenkins/Jenkinsfile +++ b/jenkins/Jenkinsfile @@ -42,7 +42,7 @@ pipeline { unstash 'ecodash_x86' unstash 'ecodash_arm' unstash 'templates' - sh 'cp jenkins/Dockerfile ./Dockerfile; docker buildx build --platform linux/amd64,linux/arm64 -t ecodash .' + sh 'cp jenkins/Dockerfile ./Dockerfile; docker buildx build --platform linux/amd64,linux/arm64 --load -t ecodash .' } } From d5d6aa4d087e3a272325cc7a18baa046a5add262 Mon Sep 17 00:00:00 2001 From: MassiveBox Date: Sat, 22 Jul 2023 15:37:59 +0200 Subject: [PATCH 18/26] Work around docker's BS --- jenkins/Jenkinsfile | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/jenkins/Jenkinsfile b/jenkins/Jenkinsfile index 5c2ee18..f22a44b 100644 --- a/jenkins/Jenkinsfile +++ b/jenkins/Jenkinsfile @@ -26,23 +26,41 @@ pipeline { } } - stage('Prepare buildx') { + stage('Prepare container build') { steps { sh """ docker buildx create --name $BUILDER_NAME docker buildx use $BUILDER_NAME docker buildx inspect --bootstrap + cp jenkins/Dockerfile ./Dockerfile """ - } - } - - stage('Build multi-arch container') { - steps { + withCredentials([usernamePassword(credentialsId: 'gitea-credentials', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) { + sh 'docker login -u $USERNAME -p $PASSWORD $DOCKER_REGISTRY' + } unstash 'dockerfile' unstash 'ecodash_x86' unstash 'ecodash_arm' unstash 'templates' - sh 'cp jenkins/Dockerfile ./Dockerfile; docker buildx build --platform linux/amd64,linux/arm64 --load -t ecodash .' + } + } + + stage('Build and push container on push to master') { + when { + anyOf { + branch 'master' + buildingTag() + } + } + steps { + sh 'docker buildx build --platform linux/amd64,linux/arm64 --push -t $DOCKER_REGISTRY/$SERVICE:latest .' + } + } + + stage('Build and push container on tag') { + when { buildingTag() } + steps { + def formattedTag = env.TAG_NAME.replaceFirst(/^v/, '') + sh 'docker buildx build --platform linux/amd64,linux/arm64 --push -t $DOCKER_REGISTRY/$SERVICE:$formattedTag .' } } @@ -55,19 +73,6 @@ pipeline { } } - stage('Publish container on tag latest') { - when { branch 'master' } - steps { - withCredentials([usernamePassword(credentialsId: 'gitea-credentials', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) { - sh 'docker login -u $USERNAME -p $PASSWORD $DOCKER_REGISTRY' - } - sh """ - docker image tag ecodash $DOCKER_REGISTRY/$SERVICE:latest - docker push $DOCKER_REGISTRY/$SERVICE:latest - """ - } - } - } post { From 75423645ff459e5d1d7861d539538cea22e08796 Mon Sep 17 00:00:00 2001 From: MassiveBox Date: Sat, 22 Jul 2023 15:40:00 +0200 Subject: [PATCH 19/26] Work around Jenkins'BS --- jenkins/Jenkinsfile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/jenkins/Jenkinsfile b/jenkins/Jenkinsfile index f22a44b..0b94e58 100644 --- a/jenkins/Jenkinsfile +++ b/jenkins/Jenkinsfile @@ -59,8 +59,10 @@ pipeline { stage('Build and push container on tag') { when { buildingTag() } steps { - def formattedTag = env.TAG_NAME.replaceFirst(/^v/, '') - sh 'docker buildx build --platform linux/amd64,linux/arm64 --push -t $DOCKER_REGISTRY/$SERVICE:$formattedTag .' + script { + def formattedTag = env.TAG_NAME.replaceFirst(/^v/, '') + sh 'docker buildx build --platform linux/amd64,linux/arm64 --push -t $DOCKER_REGISTRY/$SERVICE:$formattedTag .' + } } } From 4bf1455ba49b2adc3b9aa9d549d88540571172ea Mon Sep 17 00:00:00 2001 From: MassiveBox Date: Sun, 29 Oct 2023 18:32:35 +0100 Subject: [PATCH 20/26] Remove need for restart on settings change --- BUILD.md | 6 ++---- README.md | 2 +- jenkins/Jenkinsfile | 3 ++- src/ecodash/config.go | 16 +++++++-------- src/ecodash/http.go | 9 +++++---- src/main/main.go | 36 ++++++++++------------------------ templates/default/restart.html | 6 ------ 7 files changed, 28 insertions(+), 50 deletions(-) delete mode 100644 templates/default/restart.html diff --git a/BUILD.md b/BUILD.md index b9d6e3f..260e9c9 100644 --- a/BUILD.md +++ b/BUILD.md @@ -12,7 +12,7 @@ If you really have to build it yourself, we recommend you Docker over binaries. 1. Download the Go Compiler from https://go.dev/dl/ or from your repository's package manager (it's usually called `go` or `golang`) 2. Download the Git SCM from https://git-scm.com/download/linux or from your package manager (it's always called `git`) 3. Download `golangci-lint` from https://golangci-lint.run/ -4. Clone the repository by running `git clone https://gitea.massivebox.net/ecodash/ecodash.git ` inside a command prompt +4. Clone the repository by running `git clone https://git.massivebox.net/ecodash/ecodash.git ` inside a command prompt 5. Switch to the project directory with `cd ecodash` 6. Run `golangci-lint run` to lint all project files 7. Build with `go build src/main/main.go -o ecodash`. This will generate an executable, `ecodash`, in the same directory. @@ -22,9 +22,7 @@ If you really have to build it yourself, we recommend you Docker over binaries. 1. Install the latest release of the Go Compiler for Windows from https://go.dev/dl/ 2. Install the Git SCM from https://git-scm.com/download/win. The "Standalone installer" is recommended. All the default settings will work fine. 3. Download `golangci-lint` from https://golangci-lint.run/ -4. Clone the repository by running `git clone https://gitea.massivebox.net/ecodash/ecodash.git ` inside a command prompt +4. Clone the repository by running `git clone https://git.massivebox.net/ecodash/ecodash.git ` inside a command prompt 5. Switch to the project directory with `cd ecodash` 6. Run `golangci-lint run` to lint all project files 7. Build with `go build src/main/main.go -o ecodash`. This will generate an executable, `ecodash.exe`, in the same directory. - -## Docker \ No newline at end of file diff --git a/README.md b/README.md index 0469ba7..ea9e6c6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # 🌿 EcoDash -[![status-badge](https://woodpecker.massivebox.net/api/badges/ecodash/ecodash/status.svg)](https://woodpecker.massivebox.net/ecodash/ecohttpsdash) [![Visit our website](https://cloud.massivebox.net/api/public/dl/yEzoZyW8?inline=true)](https://ecodash.xyz) [![Support the project](https://cloud.massivebox.net/api/public/dl/dthbBylL?inline=true)](https://ecodash.xyz/contribute) +[![status-badge](https://jenkins.massivebox.net/job/ecodash/job/master/badge/icon)](https://jenkins.massivebox.net/job/ecodash/) [![Visit our website](https://cloud.massivebox.net/s/DP3ZSaDS84wQ6jp/download?path=%2FEcoDash&files=visit-website.svg)](https://ecodash.xyz) [![Support the project](https://cloud.massivebox.net/s/DP3ZSaDS84wQ6jp/download?path=%2FEcoDash&files=support-the%20project.svg)](https://ecodash.xyz/contribute) EcoDash is a simple way to show your users how much your server consumes. It's intended as a medium of transparency, that gives your users an idea about the consumption of your machine. It's not meant to be 100% accurate. diff --git a/jenkins/Jenkinsfile b/jenkins/Jenkinsfile index 0b94e58..dac4bc1 100644 --- a/jenkins/Jenkinsfile +++ b/jenkins/Jenkinsfile @@ -66,7 +66,8 @@ pipeline { } } - stage('Publish built files') { + stage('Publish build artifacts on tag') { + when { buildingTag() } steps { sh 'mv ecodash_x86 ecodash; zip -r ecodash-x86.zip templates ecodash' archiveArtifacts artifacts: "ecodash-x86.zip" diff --git a/src/ecodash/config.go b/src/ecodash/config.go index 91b7541..2856159 100644 --- a/src/ecodash/config.go +++ b/src/ecodash/config.go @@ -61,10 +61,10 @@ func formatURL(url string) (string, error) { return url, nil } -func LoadConfig() (config *Config, isFirstRun bool, err error) { +func LoadConfig() (config *Config, err error) { db, err := sql.Open("sqlite", "./database.db") if err != nil { - return &Config{}, false, err + return &Config{}, err } _, err = db.Exec(`CREATE TABLE IF NOT EXISTS "cache" ( @@ -74,7 +74,7 @@ func LoadConfig() (config *Config, isFirstRun bool, err error) { PRIMARY KEY("time") );`) if err != nil { - return &Config{}, false, err + return &Config{}, err } defaultConfig := &Config{} @@ -95,24 +95,24 @@ func LoadConfig() (config *Config, isFirstRun bool, err error) { if err != nil { // if the data file doesn't exist, we consider it a first run if os.IsNotExist(err) { - return defaultConfig, true, nil + return defaultConfig, nil } - return &Config{}, false, err + return &Config{}, err } // if the data file is empty, we consider it as a first run if len(data) == 0 { - return defaultConfig, true, nil + return defaultConfig, nil } conf := &Config{} err = json.Unmarshal(data, &conf) if err != nil { - return &Config{}, false, err + return &Config{}, err } conf.db = db - return conf, false, nil + return conf, nil } func (config *Config) IsAuthorized(c *fiber.Ctx) bool { diff --git a/src/ecodash/http.go b/src/ecodash/http.go index 7fbd1b3..bd17c71 100644 --- a/src/ecodash/http.go +++ b/src/ecodash/http.go @@ -53,9 +53,9 @@ func (config *Config) AdminEndpoint(c *fiber.Ctx) error { }) } return config.RenderAdminPanel(c, Warning{ - Header: "Restart needed", - Body: "In order to apply changes, please restart EcoDash.
" + - "If you're running via Docker, click here to restart automatically.", + Header: "Settings applied", + Body: "Your settings have been tested and applied successfully.
" + + "You can continue using EcoDash on the Home.", IsSuccess: true, }) } @@ -117,7 +117,7 @@ func (config *Config) saveAdminForm(c *fiber.Ctx) error { return err } - form := &Config{ + form := Config{ HomeAssistant: HomeAssistant{ /*BaseURL to be filled later*/ APIKey: c.FormValue("api_key"), InstallationDate: dayStart(parsedTime)}, Sensors: Sensors{PolledSmartEnergySummation: c.FormValue("polled_smart_energy_summation"), FossilPercentage: c.FormValue("fossil_percentage")}, Administrator: Administrator{Username: c.FormValue("username") /*PasswordHash to be filled later*/}, @@ -151,6 +151,7 @@ func (config *Config) saveAdminForm(c *fiber.Ctx) error { return err } + *config = form return os.WriteFile("config.json", js, 0o600) } diff --git a/src/main/main.go b/src/main/main.go index 4fcba7a..4b4c4aa 100644 --- a/src/main/main.go +++ b/src/main/main.go @@ -1,33 +1,28 @@ package main import ( - "log" - "net/http" - "os" - "time" - "git.massivebox.net/ecodash/ecodash/src/ecodash" "git.massivebox.net/ecodash/ecodash/src/tools" "github.com/gofiber/fiber/v2" "github.com/gofiber/template/html" "github.com/robfig/cron/v3" + "log" + "os" ) func main() { - config, isFirstRun, err := ecodash.LoadConfig() + config, err := ecodash.LoadConfig() if err != nil { log.Fatal(err) } - if !isFirstRun { - cr := cron.New() - _, err = cr.AddFunc("@hourly", config.UpdateHistory) - if err != nil { - log.Fatal(err) - } - cr.Start() - config.UpdateHistory() + cr := cron.New() + _, err = cr.AddFunc("@hourly", config.UpdateHistory) + if err != nil { + log.Fatal(err) } + cr.Start() + config.UpdateHistory() engine := html.New("./templates/"+config.Dashboard.Theme, ".html") engine.AddFunc("divide", tools.TemplateDivide) @@ -40,7 +35,7 @@ func main() { app.Static("/assets", "./templates/"+config.Dashboard.Theme+"/assets") app.Get("/", func(c *fiber.Ctx) error { - if isFirstRun { + if config.Administrator.Username == "" || config.Administrator.PasswordHash == "" { c.Cookie(&fiber.Cookie{Name: "admin_username", Value: ""}) c.Cookie(&fiber.Cookie{Name: "admin_password_hash", Value: tools.Hash("")}) return config.RenderAdminPanel(c) @@ -54,17 +49,6 @@ func main() { app.All("/admin", config.AdminEndpoint) - app.Get("/restart", func(c *fiber.Ctx) error { - if config.IsAuthorized(c) { - go func() { - time.Sleep(time.Second) - os.Exit(1) - }() - return c.Render("restart", config.TemplateDefaultsMap(), "base") - } - return c.Redirect("./", http.StatusTemporaryRedirect) - }) - port := os.Getenv("PORT") if port == "" { port = "80" diff --git a/templates/default/restart.html b/templates/default/restart.html deleted file mode 100644 index 2a9ca60..0000000 --- a/templates/default/restart.html +++ /dev/null @@ -1,6 +0,0 @@ -

Restarting...

-

- You should be able to continue using EcoDash soon by clicking here.
- If you get an error like "Address Unreachable", make sure you've allowed your container to restart automatically.
- Check the error logs if the error persists. -

\ No newline at end of file From 8b81c41bd79019b9cafc43abc6edd0762606742e Mon Sep 17 00:00:00 2001 From: MassiveBox Date: Tue, 31 Oct 2023 23:59:09 +0100 Subject: [PATCH 21/26] Add admin-settable MOTD, rewrite existing warning system to use it --- src/ecodash/config.go | 16 ++++++++--- src/ecodash/http.go | 44 ++++++++++++++--------------- src/main/main.go | 2 +- templates/default/admin.html | 28 ++++++++++++------ templates/default/assets/custom.css | 10 +++++++ templates/default/index.html | 12 +++++++- 6 files changed, 75 insertions(+), 37 deletions(-) diff --git a/src/ecodash/config.go b/src/ecodash/config.go index 2856159..efa5841 100644 --- a/src/ecodash/config.go +++ b/src/ecodash/config.go @@ -4,6 +4,7 @@ import ( "database/sql" "encoding/json" "errors" + "html/template" "os" "reflect" "regexp" @@ -37,10 +38,17 @@ type Administrator struct { PasswordHash string `json:"password_hash"` } type Dashboard struct { - Name string `json:"name"` - Theme string `json:"theme"` - FooterLinks []Link `json:"footer_links"` - HeaderLinks []Link `json:"header_links"` + Name string `json:"name"` + Theme string `json:"theme"` + FooterLinks []Link `json:"footer_links"` + HeaderLinks []Link `json:"header_links"` + MOTD *MessageCard `json:"motd"` +} + +type MessageCard struct { + Title string `json:"title"` + Content template.HTML `json:"content"` + Style string `json:"style"` } var errBadHAFormat = errors.New("HomeAssistant base URL is badly formatted") diff --git a/src/ecodash/http.go b/src/ecodash/http.go index bd17c71..8cce565 100644 --- a/src/ecodash/http.go +++ b/src/ecodash/http.go @@ -47,16 +47,17 @@ func (config *Config) AdminEndpoint(c *fiber.Ctx) error { if config.IsAuthorized(c) { // here the user is submitting the form to change configuration err := config.saveAdminForm(c) if err != nil { - return config.RenderAdminPanel(c, Warning{ - Header: "An error occurred!", - Body: html.EscapeString(err.Error()), + return config.RenderAdminPanel(c, &MessageCard{ + Title: "An error occurred!", + Content: template.HTML(html.EscapeString(err.Error())), + Style: "error", }) } - return config.RenderAdminPanel(c, Warning{ - Header: "Settings applied", - Body: "Your settings have been tested and applied successfully.
" + + return config.RenderAdminPanel(c, &MessageCard{ + Title: "Settings applied", + Content: "Your settings have been tested and applied successfully.
" + "You can continue using EcoDash on the Home.", - IsSuccess: true, + Style: "success", }) } @@ -64,38 +65,28 @@ func (config *Config) AdminEndpoint(c *fiber.Ctx) error { if c.FormValue("username") == config.Administrator.Username && tools.Hash(c.FormValue("password")) == config.Administrator.PasswordHash { c.Cookie(&fiber.Cookie{Name: "admin_username", Value: c.FormValue("username")}) c.Cookie(&fiber.Cookie{Name: "admin_password_hash", Value: tools.Hash(c.FormValue("password"))}) - return config.RenderAdminPanel(c) + return config.RenderAdminPanel(c, nil) } return c.Render("login", fiber.Map{"Defaults": config.getTemplateDefaults(), "Failed": true}, "base") } if config.IsAuthorized(c) { - return config.RenderAdminPanel(c) + return config.RenderAdminPanel(c, nil) } return c.Render("login", config.TemplateDefaultsMap(), "base") } -func (config *Config) RenderAdminPanel(c *fiber.Ctx, warning ...Warning) error { +func (config *Config) RenderAdminPanel(c *fiber.Ctx, message *MessageCard) error { dirs, err := os.ReadDir("./templates") if err != nil { return err } - if len(warning) > 0 { - // #nosec // TODO this is dangerous, even if we're escaping the only place where we're passing a non-literal - warning[0].BodyHTML = template.HTML(warning[0].Body) - return c.Render("admin", fiber.Map{ - "Defaults": config.getTemplateDefaults(), - "Themes": dirs, - "Config": config, - "Warning": warning[0], - }, "base") - } - return c.Render("admin", fiber.Map{ "Defaults": config.getTemplateDefaults(), "Themes": dirs, "Config": config, + "Message": message, }, "base") } @@ -121,7 +112,7 @@ func (config *Config) saveAdminForm(c *fiber.Ctx) error { HomeAssistant: HomeAssistant{ /*BaseURL to be filled later*/ APIKey: c.FormValue("api_key"), InstallationDate: dayStart(parsedTime)}, Sensors: Sensors{PolledSmartEnergySummation: c.FormValue("polled_smart_energy_summation"), FossilPercentage: c.FormValue("fossil_percentage")}, Administrator: Administrator{Username: c.FormValue("username") /*PasswordHash to be filled later*/}, - Dashboard: Dashboard{Theme: c.FormValue("theme"), Name: c.FormValue("name"), HeaderLinks: config.Dashboard.HeaderLinks, FooterLinks: config.Dashboard.FooterLinks}, + Dashboard: Dashboard{Theme: c.FormValue("theme"), Name: c.FormValue("name"), HeaderLinks: config.Dashboard.HeaderLinks, FooterLinks: config.Dashboard.FooterLinks /*MessageCard to be filled later*/}, } if c.FormValue("keep_old_password") == "" { @@ -130,6 +121,14 @@ func (config *Config) saveAdminForm(c *fiber.Ctx) error { form.Administrator.PasswordHash = config.Administrator.PasswordHash } + if c.FormValue("motd_title") != "" || c.FormValue("motd_content") != "" { + form.Dashboard.MOTD = &MessageCard{ + Title: c.FormValue("motd_title"), + Content: template.HTML(c.FormValue("motd_content")), + Style: c.FormValue("motd_style"), + } + } + fmtURL, err := formatURL(c.FormValue("base_url")) if err != nil { return err @@ -202,5 +201,6 @@ func (config *Config) RenderIndex(c *fiber.Ctx) error { "EnergyConsumptions": energyConsumptions, "GreenEnergyPercent": averageExcludingCurrentDay(greenEnergyPercents), "PerDayUsage": perDayUsage, + "MOTD": config.Dashboard.MOTD, }, "base") } diff --git a/src/main/main.go b/src/main/main.go index 4b4c4aa..29ae004 100644 --- a/src/main/main.go +++ b/src/main/main.go @@ -38,7 +38,7 @@ func main() { if config.Administrator.Username == "" || config.Administrator.PasswordHash == "" { c.Cookie(&fiber.Cookie{Name: "admin_username", Value: ""}) c.Cookie(&fiber.Cookie{Name: "admin_password_hash", Value: tools.Hash("")}) - return config.RenderAdminPanel(c) + return config.RenderAdminPanel(c, nil) } return config.RenderIndex(c) }) diff --git a/templates/default/admin.html b/templates/default/admin.html index 14654a2..d1e889f 100644 --- a/templates/default/admin.html +++ b/templates/default/admin.html @@ -4,15 +4,15 @@ Documentation

-{{if .Warning}} -
-
-

{{.Warning.Header}}

-
-
-

{{.Warning.BodyHTML}}

-
-
+{{if .Message}} +
+
+

{{.Message.Title}}

+
+
+

{{.Message.Content}}

+
+
{{end}}
@@ -45,6 +45,16 @@ + + +
diff --git a/templates/default/assets/custom.css b/templates/default/assets/custom.css index 1184003..c80367c 100644 --- a/templates/default/assets/custom.css +++ b/templates/default/assets/custom.css @@ -46,4 +46,14 @@ svg, footer img { width: 100% } fill: white; } +} + +.success { + background-color: #008000; color: white +} +.warning { + background-color: #807a00; color: white +} +.error { + background-color: #800000; color: white } \ No newline at end of file diff --git a/templates/default/index.html b/templates/default/index.html index 5115c01..cafaa3e 100644 --- a/templates/default/index.html +++ b/templates/default/index.html @@ -1,5 +1,16 @@ +{{if .MOTD}} +
+
+

{{.MOTD.Title}}

+
+
+

{{.MOTD.Content}}

+
+
+{{end}} +

Green report

@@ -7,7 +18,6 @@ This server's energy statistics for the last eight days (current day included)

-
From 99acf1fd18d4db63acd2e58dc2efcdb0e85b516f Mon Sep 17 00:00:00 2001 From: MassiveBox Date: Fri, 3 Nov 2023 22:53:54 +0100 Subject: [PATCH 22/26] Fix linter errors --- .golangci.yml | 1 - src/ecodash/config.go | 2 +- src/ecodash/http.go | 2 ++ src/main/main.go | 5 +++-- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 1bd5888..13ae91d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -234,7 +234,6 @@ linters: - wrapcheck - nonamedreturns - gomnd - - gosmopolitan - depguard enable-all: true diff --git a/src/ecodash/config.go b/src/ecodash/config.go index efa5841..c598902 100644 --- a/src/ecodash/config.go +++ b/src/ecodash/config.go @@ -38,11 +38,11 @@ type Administrator struct { PasswordHash string `json:"password_hash"` } type Dashboard struct { + MOTD *MessageCard `json:"motd"` Name string `json:"name"` Theme string `json:"theme"` FooterLinks []Link `json:"footer_links"` HeaderLinks []Link `json:"header_links"` - MOTD *MessageCard `json:"motd"` } type MessageCard struct { diff --git a/src/ecodash/http.go b/src/ecodash/http.go index 8cce565..46b9030 100644 --- a/src/ecodash/http.go +++ b/src/ecodash/http.go @@ -47,6 +47,7 @@ func (config *Config) AdminEndpoint(c *fiber.Ctx) error { if config.IsAuthorized(c) { // here the user is submitting the form to change configuration err := config.saveAdminForm(c) if err != nil { + // #nosec the input is admin-defined, and the admin is assumed to be trusted. return config.RenderAdminPanel(c, &MessageCard{ Title: "An error occurred!", Content: template.HTML(html.EscapeString(err.Error())), @@ -122,6 +123,7 @@ func (config *Config) saveAdminForm(c *fiber.Ctx) error { } if c.FormValue("motd_title") != "" || c.FormValue("motd_content") != "" { + // #nosec the input is admin-defined, and the admin is assumed to be trusted. form.Dashboard.MOTD = &MessageCard{ Title: c.FormValue("motd_title"), Content: template.HTML(c.FormValue("motd_content")), diff --git a/src/main/main.go b/src/main/main.go index 29ae004..52e0854 100644 --- a/src/main/main.go +++ b/src/main/main.go @@ -1,13 +1,14 @@ package main import ( + "log" + "os" + "git.massivebox.net/ecodash/ecodash/src/ecodash" "git.massivebox.net/ecodash/ecodash/src/tools" "github.com/gofiber/fiber/v2" "github.com/gofiber/template/html" "github.com/robfig/cron/v3" - "log" - "os" ) func main() { From 1f9827505bbc8236d635c272b3b6b38d16a3dff6 Mon Sep 17 00:00:00 2001 From: MassiveBox Date: Fri, 3 Nov 2023 23:10:07 +0100 Subject: [PATCH 23/26] Disable again gosmopolitan, which for some reason figures as non-existent on my machine, but breaks builds on the CI if it's not disabled. --- .golangci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.golangci.yml b/.golangci.yml index 13ae91d..edcb24b 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -235,6 +235,7 @@ linters: - nonamedreturns - gomnd - depguard + - gosmopolitan enable-all: true fast: false From 519796d3d107ae189d2490b8b74c5e65686e2fab Mon Sep 17 00:00:00 2001 From: MassiveBox Date: Fri, 12 Apr 2024 21:40:22 +0200 Subject: [PATCH 24/26] Improve data location selection --- Dockerfile | 14 ++++++++------ src/ecodash/config.go | 12 ++++++++++-- src/ecodash/http.go | 7 ++++++- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/Dockerfile b/Dockerfile index 47f94d0..f6f7e04 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,10 +9,9 @@ COPY src /app/src COPY go.mod /app/ COPY .golangci.yml /app/ -RUN go mod tidy -RUN golangci-lint run -RUN go test ./src/... - +RUN go mod tidy; \ + golangci-lint run; \ + go test ./src/... RUN CGO_ENABLED=1 go build -o app src/main/main.go FROM alpine:latest @@ -21,6 +20,9 @@ WORKDIR /app COPY --from=1 /app/app . COPY ./templates /app/templates -RUN touch config.json database.db -CMD ["./app"] \ No newline at end of file +RUN mkdir data +ENV DATABASE_PATH=./data/database.db +ENV CONFIG_PATH=./data/config.json + +CMD "./app" \ No newline at end of file diff --git a/src/ecodash/config.go b/src/ecodash/config.go index c598902..b183666 100644 --- a/src/ecodash/config.go +++ b/src/ecodash/config.go @@ -70,7 +70,11 @@ func formatURL(url string) (string, error) { } func LoadConfig() (config *Config, err error) { - db, err := sql.Open("sqlite", "./database.db") + var dbPath string + if dbPath = os.Getenv("DATABASE_PATH"); dbPath == "" { + dbPath = "./database.db" + } + db, err := sql.Open("sqlite", dbPath) if err != nil { return &Config{}, err } @@ -99,7 +103,11 @@ func LoadConfig() (config *Config, err error) { }) defaultConfig.db = db - data, err := os.ReadFile("config.json") + var confPath string + if confPath = os.Getenv("CONFIG_PATH"); confPath == "" { + confPath = "./config.json" + } + data, err := os.ReadFile(confPath) if err != nil { // if the data file doesn't exist, we consider it a first run if os.IsNotExist(err) { diff --git a/src/ecodash/http.go b/src/ecodash/http.go index 46b9030..02560ae 100644 --- a/src/ecodash/http.go +++ b/src/ecodash/http.go @@ -153,7 +153,12 @@ func (config *Config) saveAdminForm(c *fiber.Ctx) error { } *config = form - return os.WriteFile("config.json", js, 0o600) + + var confPath string + if confPath = os.Getenv("CONFIG_PATH"); confPath == "" { + confPath = "./config.json" + } + return os.WriteFile(confPath, js, 0o600) } func averageExcludingCurrentDay(data []float32) float32 { From 924b96e0dbe35ee8e27e557570c19a74d9d1dc2a Mon Sep 17 00:00:00 2001 From: MassiveBox Date: Thu, 18 Apr 2024 12:14:36 +0200 Subject: [PATCH 25/26] Move to Forgejo CI --- {jenkins => .forgejo/workflows}/Dockerfile | 0 .forgejo/workflows/build.yaml | 123 +++++++++++++++++++++ jenkins/Jenkinsfile | 89 --------------- 3 files changed, 123 insertions(+), 89 deletions(-) rename {jenkins => .forgejo/workflows}/Dockerfile (100%) create mode 100644 .forgejo/workflows/build.yaml delete mode 100644 jenkins/Jenkinsfile diff --git a/jenkins/Dockerfile b/.forgejo/workflows/Dockerfile similarity index 100% rename from jenkins/Dockerfile rename to .forgejo/workflows/Dockerfile diff --git a/.forgejo/workflows/build.yaml b/.forgejo/workflows/build.yaml new file mode 100644 index 0000000..8f62b00 --- /dev/null +++ b/.forgejo/workflows/build.yaml @@ -0,0 +1,123 @@ +name: CI Pipeline + +on: + push: + branches: + - master + tags: + - 'v*' + +jobs: + + build: + runs-on: ubuntu-22.04 + steps: + + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: '1.22' + + - name: Checkout code + uses: actions/checkout@v3 + + - name: Build + run: | + go mod tidy + echo Building for linux/amd64... + GOOS=linux GOARCH=amd64 go build -o ecodash_x86 src/main/main.go + echo Building for linux/arm... + GOOS=linux GOARCH=arm go build -o ecodash_arm src/main/main.go + + - name: Stash artifacts + uses: actions/upload-artifact@v3 + with: + path: | + ecodash_x86 + ecodash_arm + + build-and-push-docker: + runs-on: ubuntu-22.04 + needs: build + steps: + + - name: Checkout + uses: actions/checkout@v4 + + - name: Docker meta + id: meta + uses: https://github.com/docker/metadata-action@v5 + with: + images: git.massivebox.net/massivebox/ecodash + tags: | + type=ref,event=branch + type=semver,pattern={{version}} + type=semver,pattern=v{{major}} + type=semver,pattern={{major}}.{{minor}} + + - name: Install QEMU + run: sudo apt-get update && sudo apt-get install -y qemu-user-static + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + registry: git.massivebox.net + username: ${{ github.actor }} + password: ${{ secrets.FORGE_TOKEN }} + + - name: Download artifacts + uses: actions/download-artifact@v3 + with: + name: artifact + - name: Move dockerfile + run: mv .forgejo/workflows/Dockerfile Dockerfile + + - name: Build and push + uses: docker/build-push-action@v5 + with: + context: . + platforms: linux/amd64,linux/arm64 + push: true + tags: ${{ steps.meta.outputs.tags }} + + publish-executables: + runs-on: ubuntu-22.04 + needs: build + steps: + + - name: Checkout code + uses: actions/checkout@v3 + + - name: Download artifacts + uses: actions/download-artifact@v3 + with: + name: artifact + + - name: Prepare build artifacts + run: | + mkdir release + mv ecodash_x86 ecodash + zip -r release/ecodash-x86.zip templates ecodash + mv ecodash_arm ecodash + zip -r release/ecodash-arm.zip templates ecodash + + - name: Upload artifacts to CI + uses: actions/upload-artifact@v3 + with: + path: | + ecodash_x86 + ecodash_arm + templates + overwrite: true + + - name: Create release + if: ${{ startsWith(github.ref, 'refs/tags/v') }} + uses: actions/forgejo-release@v1 + with: + direction: upload + url: https://git.massivebox.net + repo: massivebox/ecodash + release-dir: release + tag: ${{ github.ref_name }} + token: ${{ secrets.FORGE_TOKEN }} diff --git a/jenkins/Jenkinsfile b/jenkins/Jenkinsfile deleted file mode 100644 index dac4bc1..0000000 --- a/jenkins/Jenkinsfile +++ /dev/null @@ -1,89 +0,0 @@ -pipeline { - - agent any - - environment { - DOCKER_REGISTRY='git.massivebox.net' - BUILDER_NAME='mbuilder' - SERVICE='ecodash/ecodash' - } - - stages { - - stage('Run linter and build') { - agent { docker { image 'golang' } } - steps { - checkout scm - sh 'curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin' - sh 'go mod tidy' - sh 'golangci-lint run' - sh 'env GOOS=linux GOARCH=amd64 go build -o ecodash_x86 src/main/main.go' - stash includes: 'ecodash_x86', name: 'ecodash_x86' - sh 'env GOOS=linux GOARCH=arm go build -o ecodash_arm src/main/main.go' - stash includes: 'ecodash_arm', name: 'ecodash_arm' - stash includes: 'jenkins/Dockerfile', name: 'dockerfile' - stash includes: 'templates/**', name: 'templates' - } - } - - stage('Prepare container build') { - steps { - sh """ - docker buildx create --name $BUILDER_NAME - docker buildx use $BUILDER_NAME - docker buildx inspect --bootstrap - cp jenkins/Dockerfile ./Dockerfile - """ - withCredentials([usernamePassword(credentialsId: 'gitea-credentials', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) { - sh 'docker login -u $USERNAME -p $PASSWORD $DOCKER_REGISTRY' - } - unstash 'dockerfile' - unstash 'ecodash_x86' - unstash 'ecodash_arm' - unstash 'templates' - } - } - - stage('Build and push container on push to master') { - when { - anyOf { - branch 'master' - buildingTag() - } - } - steps { - sh 'docker buildx build --platform linux/amd64,linux/arm64 --push -t $DOCKER_REGISTRY/$SERVICE:latest .' - } - } - - stage('Build and push container on tag') { - when { buildingTag() } - steps { - script { - def formattedTag = env.TAG_NAME.replaceFirst(/^v/, '') - sh 'docker buildx build --platform linux/amd64,linux/arm64 --push -t $DOCKER_REGISTRY/$SERVICE:$formattedTag .' - } - } - } - - stage('Publish build artifacts on tag') { - when { buildingTag() } - steps { - sh 'mv ecodash_x86 ecodash; zip -r ecodash-x86.zip templates ecodash' - archiveArtifacts artifacts: "ecodash-x86.zip" - sh 'mv ecodash_arm ecodash; zip -r ecodash-arm.zip templates ecodash' - archiveArtifacts artifacts: "ecodash-arm.zip" - } - } - - } - - post { - always { - // cleanup - sh 'docker buildx rm $BUILDER_NAME' - } - } - -} - From 79c7dde23082af97669f1bb87daaa8a02ffcd207 Mon Sep 17 00:00:00 2001 From: MassiveBox Date: Fri, 19 Apr 2024 18:30:11 +0200 Subject: [PATCH 26/26] Reorganize docs --- BUILD.md | 28 ---------------------------- README.md | 14 ++++++-------- 2 files changed, 6 insertions(+), 36 deletions(-) delete mode 100644 BUILD.md diff --git a/BUILD.md b/BUILD.md deleted file mode 100644 index 260e9c9..0000000 --- a/BUILD.md +++ /dev/null @@ -1,28 +0,0 @@ -# 👷 Building EcoDash - -Here's how to build EcoDash in both binaries and as a Docker container. This is not necessary for most cases - we provide both pre-built binaries and containers for Linux ARM and x86_64 - however in devices with unsupported architectures it's necessary. - -You're encouraged to first check the installation instructions to see if a pre-built container or binary is already available. -If you really have to build it yourself, we recommend you Docker over binaries. - -## Binaries - -### Linux - -1. Download the Go Compiler from https://go.dev/dl/ or from your repository's package manager (it's usually called `go` or `golang`) -2. Download the Git SCM from https://git-scm.com/download/linux or from your package manager (it's always called `git`) -3. Download `golangci-lint` from https://golangci-lint.run/ -4. Clone the repository by running `git clone https://git.massivebox.net/ecodash/ecodash.git ` inside a command prompt -5. Switch to the project directory with `cd ecodash` -6. Run `golangci-lint run` to lint all project files -7. Build with `go build src/main/main.go -o ecodash`. This will generate an executable, `ecodash`, in the same directory. - -### Windows - -1. Install the latest release of the Go Compiler for Windows from https://go.dev/dl/ -2. Install the Git SCM from https://git-scm.com/download/win. The "Standalone installer" is recommended. All the default settings will work fine. -3. Download `golangci-lint` from https://golangci-lint.run/ -4. Clone the repository by running `git clone https://git.massivebox.net/ecodash/ecodash.git ` inside a command prompt -5. Switch to the project directory with `cd ecodash` -6. Run `golangci-lint run` to lint all project files -7. Build with `go build src/main/main.go -o ecodash`. This will generate an executable, `ecodash.exe`, in the same directory. diff --git a/README.md b/README.md index ea9e6c6..40c1bc7 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,19 @@ # 🌿 EcoDash -[![status-badge](https://jenkins.massivebox.net/job/ecodash/job/master/badge/icon)](https://jenkins.massivebox.net/job/ecodash/) [![Visit our website](https://cloud.massivebox.net/s/DP3ZSaDS84wQ6jp/download?path=%2FEcoDash&files=visit-website.svg)](https://ecodash.xyz) [![Support the project](https://cloud.massivebox.net/s/DP3ZSaDS84wQ6jp/download?path=%2FEcoDash&files=support-the%20project.svg)](https://ecodash.xyz/contribute) +[![Support the project](https://cloud.massivebox.net/index.php/s/DcxB6KwkDZALbXw/download?path=%2FEcoDash&files=support-the-project.svg)](https://massivebox.net/pages/donate.html) EcoDash is a simple way to show your users how much your server consumes. It's intended as a medium of transparency, that gives your users an idea about the consumption of your machine. It's not meant to be 100% accurate. -You can see it in action here: https://demo.ecodash.xyz +You can see it in action here: https://ecodash.massivebox.net ## Get started -Check out the documentation in our [website](https://ecodash.xyz) to get started with EcoDash. +Check out the documentation in our [wiki](https://git.massivebox.net/massivebox/ecodash/wiki) to get started with EcoDash. -- [📖 Introduction](https://ecodash.xyz/docs) -- [🛣 Roadmap](https://ecodash.xyz/docs/roadmap) -- [⬇️ Install](https://ecodash.xyz/docs/install) -- [⚙️ Setup](https://ecodash.xyz/docs/setup) -- [🆘 Support](https://ecodash.xyz/docs/support) +- [📖 Introduction](https://git.massivebox.net/massivebox/ecodash/wiki) +- [⬇️ Installation](https://git.massivebox.net/massivebox/ecodash/wiki/install) +- [⚙️ Configuration](https://git.massivebox.net/massivebox/ecodash/wiki/config) ## License