add ci pipeline and update management
This commit is contained in:
parent
cb31b19691
commit
7c304a63d7
8 changed files with 223 additions and 0 deletions
75
.gitlab-ci.yml
Normal file
75
.gitlab-ci.yml
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
.parallel:
|
||||
parallel:
|
||||
matrix:
|
||||
- GOOS: "linux"
|
||||
GOARCH:
|
||||
- amd64
|
||||
- arm64
|
||||
- GOOS: "windows"
|
||||
GOARCH:
|
||||
- amd64
|
||||
- arm64
|
||||
- GOOS: "darwin"
|
||||
GOARCH:
|
||||
- amd64
|
||||
- arm64
|
||||
|
||||
stages:
|
||||
- build
|
||||
- deploy
|
||||
|
||||
build-application-binary:
|
||||
stage: build
|
||||
needs: []
|
||||
parallel: !reference [ .parallel, parallel ]
|
||||
image: golang:1.25.4
|
||||
script:
|
||||
- CGO_ENABLED=0 go build -ldflags="-s -w -X 'main.BuildNumber=${CI_PIPELINE_IID}' -X 'main.BuildTime=$(date)'" -o shelly-smoke-alamos-${GOOS}-${GOARCH} ./
|
||||
- "if [ \"${GOOS}\" = \"windows\" ]; then
|
||||
|
||||
\ mv shelly-smoke-alamos-${GOOS}-${GOARCH} shelly-smoke-alamos-${GOOS}-${GOARCH}.exe\
|
||||
|
||||
fi\n"
|
||||
artifacts:
|
||||
paths:
|
||||
- shelly-smoke-alamos-${GOOS}-${GOARCH}
|
||||
- shelly-smoke-alamos-${GOOS}-${GOARCH}.exe
|
||||
expire_in: 1 week
|
||||
|
||||
build-update-json:
|
||||
stage: build
|
||||
needs: []
|
||||
image: alpine:latest
|
||||
script:
|
||||
- |
|
||||
echo '{
|
||||
"downloadUrl": "'${S3_HOST}'/'${S3_BUCKET}'/shelly-smoke-alamos-%GOOS%-%GOARCH%%FILEXTENSION%",
|
||||
"buildNumber": "'${CI_PIPELINE_IID}'",
|
||||
"time": "'$(date)'"
|
||||
}' > update.json
|
||||
artifacts:
|
||||
paths:
|
||||
- update.json
|
||||
expire_in: 1 week
|
||||
only:
|
||||
- master
|
||||
|
||||
upload-to-s3:
|
||||
stage: deploy
|
||||
image:
|
||||
name: quay.io/minio/mc
|
||||
entrypoint: [ '' ]
|
||||
before_script:
|
||||
- mc alias set minio $S3_HOST $S3_ACCESS_KEY_ID $S3_SECRET_ACCESS_KEY
|
||||
script:
|
||||
- mc cp shelly-smoke-alamos-* minio/$S3_BUCKET/
|
||||
- mc cp update.json minio/$S3_BUCKET/
|
||||
needs:
|
||||
- build-update-json
|
||||
- build-application-binary
|
||||
only:
|
||||
- master
|
||||
|
||||
include:
|
||||
- project: 'infra/ci'
|
||||
file: '/pipelines/docker.yml'
|
||||
1
go.mod
1
go.mod
|
|
@ -5,6 +5,7 @@ go 1.25.3
|
|||
require (
|
||||
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
|
||||
github.com/gin-gonic/gin v1.11.0
|
||||
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/swaggo/files v1.0.1
|
||||
github.com/swaggo/gin-swagger v1.6.1
|
||||
|
|
|
|||
2
go.sum
2
go.sum
|
|
@ -49,6 +49,8 @@ github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7Lk
|
|||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf h1:WfD7VjIE6z8dIvMsI4/s+1qr5EL+zoIGev1BQj1eoJ8=
|
||||
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf/go.mod h1:hyb9oH7vZsitZCiBt0ZvifOrB+qc8PS5IiilCIb87rg=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
|
||||
|
|
|
|||
21
main.go
21
main.go
|
|
@ -1,9 +1,13 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
_ "github.com/joho/godotenv/autoload"
|
||||
"jonasled.dev/jonasled/go-libs/config"
|
||||
"jonasled.dev/jonasled/go-libs/log"
|
||||
"jonasled.dev/jonasled/shelly-smoke-alamos/api"
|
||||
"jonasled.dev/jonasled/shelly-smoke-alamos/update"
|
||||
)
|
||||
|
||||
// @title Shelly Smoke 2 Alamos
|
||||
|
|
@ -11,6 +15,23 @@ import (
|
|||
// @description Protect you and your environment against fire
|
||||
func main() {
|
||||
log.Init()
|
||||
PrintVersion()
|
||||
|
||||
if config.GetOrDefault("UPDATE_CHECK", "true") != "false" {
|
||||
updateAvailable, err := update.CheckForUpdates(BuildNumber, UpdateJson)
|
||||
if err != nil {
|
||||
log.Log.Error("Failed checking for updates: ", err.Error())
|
||||
} else if updateAvailable && config.GetOrDefault("UPDATE_INSTALL", "true") == "true" {
|
||||
err := update.DoUpdate(UpdateJson)
|
||||
if err != nil {
|
||||
log.Log.Error("Failed installing update: ", err.Error())
|
||||
} else {
|
||||
log.Log.Info("Update successfully installed, please restart the application")
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
|
||||
api.Init()
|
||||
|
||||
api.Run()
|
||||
|
|
|
|||
53
update/check.go
Normal file
53
update/check.go
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package update
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"jonasled.dev/jonasled/go-libs/log"
|
||||
)
|
||||
|
||||
func getUpdateJson(updateUrl string) (*UpdateInfo, error) {
|
||||
resp, err := http.Get(updateUrl)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to fetch URL: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("unexpected HTTP status: %s", resp.Status)
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read response body: %w", err)
|
||||
}
|
||||
|
||||
var updateJson UpdateInfo
|
||||
if err := json.Unmarshal(body, &updateJson); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse JSON: %w", err)
|
||||
}
|
||||
return &updateJson, nil
|
||||
}
|
||||
|
||||
func CheckForUpdates(currentBuild string, updateUrl string) (bool, error) {
|
||||
if currentBuild == "git" { // disable update check for custom builds
|
||||
log.Log.Warn("Found custom build, update check is skipped")
|
||||
return false, nil
|
||||
}
|
||||
|
||||
updateJson, err := getUpdateJson(updateUrl)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
currentBuildNum, _ := strconv.Atoi(currentBuild)
|
||||
latestBuildNum, _ := strconv.Atoi(updateJson.BuildNumber)
|
||||
if currentBuildNum < latestBuildNum {
|
||||
log.Log.Infof("New Update available: build %s, date: %s", updateJson.BuildNumber, updateJson.RawTime)
|
||||
}
|
||||
return currentBuildNum < latestBuildNum, nil
|
||||
}
|
||||
29
update/execute.go
Normal file
29
update/execute.go
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
package update
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/inconshreveable/go-update"
|
||||
"jonasled.dev/jonasled/go-libs/log"
|
||||
)
|
||||
|
||||
func DoUpdate(updateUrl string) error {
|
||||
updateJson, err := getUpdateJson(updateUrl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Log.Info("Downloading update")
|
||||
resp, err := http.Get(updateJson.GetUpdateUrl())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
log.Log.Info("Installing update")
|
||||
err = update.Apply(resp.Body, update.Options{})
|
||||
if err != nil {
|
||||
// error handling
|
||||
}
|
||||
return err
|
||||
|
||||
}
|
||||
28
update/types.go
Normal file
28
update/types.go
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
package update
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type UpdateInfo struct {
|
||||
DownloadUrl string `json:"downloadUrl"`
|
||||
BuildNumber string `json:"buildNumber"`
|
||||
RawTime string `json:"time"`
|
||||
}
|
||||
|
||||
func (u UpdateInfo) GetUpdateUrl() string {
|
||||
goos := runtime.GOOS
|
||||
goarch := runtime.GOARCH
|
||||
fileExtension := ""
|
||||
|
||||
if goos == "windows" {
|
||||
fileExtension = ".exe"
|
||||
}
|
||||
|
||||
downloadUrl := strings.ReplaceAll(u.DownloadUrl, "%GOOS%", goos)
|
||||
downloadUrl = strings.ReplaceAll(downloadUrl, "%GOARCH%", goarch)
|
||||
downloadUrl = strings.ReplaceAll(downloadUrl, "%FILEXTENSION%", fileExtension)
|
||||
|
||||
return downloadUrl
|
||||
}
|
||||
14
version.go
Normal file
14
version.go
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
package main
|
||||
|
||||
import "jonasled.dev/jonasled/go-libs/log"
|
||||
|
||||
var (
|
||||
Version = "0.1"
|
||||
BuildNumber = "git"
|
||||
BuildTime = "unknown"
|
||||
UpdateJson = "https://dus.s3.jonasled.de/shelly-smoke-alamos/update.json"
|
||||
)
|
||||
|
||||
func PrintVersion() {
|
||||
log.Log.Infof("Shelly Smoke 2 Alamos Server Version %s-%s (build: %s)", Version, BuildNumber, BuildTime)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue