go-gluon-publisher/release.go

113 lines
2.5 KiB
Go
Raw Permalink Normal View History

2023-05-29 20:27:28 +00:00
package main
import (
2023-06-02 17:04:31 +00:00
"encoding/json"
2023-06-01 20:30:57 +00:00
"fmt"
2023-06-02 17:04:31 +00:00
"io/ioutil"
2023-05-29 20:27:28 +00:00
"log"
"os"
2023-06-01 20:30:57 +00:00
"path/filepath"
"strings"
2023-05-29 20:27:28 +00:00
)
2023-06-01 20:30:57 +00:00
func release_branch(b string, sDir string, tDir string, dryrun bool, v string) {
check_sDir, _ := os.ReadDir(sDir)
check_tDir, _ := os.ReadDir(tDir)
2023-05-29 20:27:28 +00:00
var newImages bool = false
2023-06-01 20:30:57 +00:00
if len(check_sDir) > 0 {
2023-05-29 20:27:28 +00:00
newImages = true
}
2023-06-01 20:30:57 +00:00
sVersion := GetGluonVersion(sDir)
tVersion := GetGluonVersion(tDir)
2023-08-20 11:37:04 +00:00
fmt.Println("Target Version:", b, tVersion)
fmt.Println("Source Version:", b, sVersion)
fmt.Println("Wanted Version:", b, v)
2023-06-01 20:30:57 +00:00
//check wanted version in target
if tVersion == v {
2023-08-20 11:37:04 +00:00
log.Println(b, "wanted version is already published!")
os.Exit(0)
}
2023-08-20 11:37:04 +00:00
log.Println(b, "published version is outdated, try to update from source folder")
if sVersion != v {
2023-08-20 11:37:04 +00:00
log.Println(b, "wanted Version is not availible in source folder")
os.Exit(1)
2023-06-01 20:30:57 +00:00
}
2023-05-29 20:27:28 +00:00
if newImages {
// delete old firmware files in public folder
2023-07-30 18:49:32 +00:00
// if we are in stable branch, move images to archive
if b == "stable" {
2023-08-20 11:37:04 +00:00
err := os.Rename(tDir+"/*", tDir+"/archive/"+v)
2023-07-30 18:49:32 +00:00
if err != nil {
log.Fatalln(err)
}
} else {
for _, file := range check_tDir {
if !(dryrun) {
2023-08-20 11:37:04 +00:00
err := os.RemoveAll(tDir + "/" + file.Name())
2023-07-30 18:49:32 +00:00
if err != nil {
log.Println("error deleting file: ", err)
} else {
log.Println("removed file: ", file.Name())
}
2023-05-30 17:57:42 +00:00
} else {
2023-07-30 18:49:32 +00:00
log.Println("DRYRUN: delete folder :", file.Name())
2023-05-30 17:57:42 +00:00
}
2023-05-29 20:27:28 +00:00
}
}
//move new files to folder
2023-06-01 20:30:57 +00:00
for _, file := range check_sDir {
2023-05-30 17:57:42 +00:00
if !(dryrun) {
2023-06-01 20:30:57 +00:00
os.Rename(sDir+file.Name(), tDir+file.Name())
2023-05-30 17:57:42 +00:00
log.Println("moved file: ", file.Name())
} else {
log.Println("DRYRUN: move folder: ", file.Name())
}
2023-05-29 20:27:28 +00:00
}
}
}
2023-06-01 20:30:57 +00:00
func GetGluonVersion(path string) string {
2023-08-20 11:37:04 +00:00
image_folder := path + "/sysupgrade/"
2023-06-01 20:30:57 +00:00
files, err := filepath.Glob(filepath.Join(image_folder, "gluon-tdf-*"))
if err != nil {
fmt.Println("Error reading directory: ", err)
os.Exit(1)
}
if len(files) > 0 {
fileName := filepath.Base(files[0])
parts := strings.Split(fileName, "-")
if len(parts) >= 4 {
version := parts[2]
return version
}
}
return ""
}
2023-06-02 17:04:31 +00:00
func readVersions(branch string) string {
2023-07-30 18:28:57 +00:00
filePath := "/srv/fwuploads-tmp/bin/release_state.json"
2023-06-02 17:04:31 +00:00
v, err := ioutil.ReadFile(filePath)
if err != nil {
log.Fatalln("Error reading version JSON file!")
}
2023-06-01 20:30:57 +00:00
2023-06-02 17:04:31 +00:00
var data Versions
err = json.Unmarshal(v, &data)
if err != nil {
log.Fatalln("Error in JSON data!")
}
switch branch {
case "stable":
return data.Stable
case "beta":
return data.Beta
case "experimental":
return data.Experimental
}
return ""
2023-06-01 20:30:57 +00:00
}