Skip to content

Commit

Permalink
feat: get release from raucb (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
CorrectRoadH authored Oct 12, 2023
1 parent fdc3cb1 commit 6c0682c
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 5 deletions.
11 changes: 11 additions & 0 deletions internal/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package internal

import (
"bufio"
"bytes"
"context"
"fmt"
"net/http"
Expand Down Expand Up @@ -40,6 +41,16 @@ func GetReleaseFromLocal(releasePath string) (*codegen.Release, error) {
return &release, nil
}

func GetReleaseFromContent(content []byte) (*codegen.Release, error) {
// decode the yaml file
var release codegen.Release
decoder := yaml.NewDecoder(bytes.NewReader(content))
if err := decoder.Decode(&release); err != nil {
return nil, err
}
return &release, nil
}

func GetReleaseFrom(ctx context.Context, releaseURL string) (*codegen.Release, error) {
// download content from releaseURL
response, err := client.R().SetContext(ctx).Get(releaseURL)
Expand Down
4 changes: 3 additions & 1 deletion internal/config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ var (
)

const (
RAUC_OFFLINE_PATH = "/DATA/rauc/"
// RAUC_OFFLINE_PATH = "/DATA/rauc/"
RAUC_OFFLINE_PATH = "/var/lib/casa_data/rauc/"

RAUC_OFFLINE_RELEASE_FILENAME = "release.yaml"
OFFLINE_RAUC_TEMP_PATH = "/tmp/offline_rauc"
)
Expand Down
72 changes: 71 additions & 1 deletion service/rauc.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package service

import (
"bufio"
"bytes"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"

"github.com/IceWhaleTech/CasaOS-Installer/codegen"
"github.com/IceWhaleTech/CasaOS-Installer/internal"
Expand Down Expand Up @@ -104,7 +108,7 @@ func RAUCFilePath(release codegen.Release) (string, error) {

packageFilePath := filepath.Join(releaseDir, packageFilename)

packageFilePath = packageFilePath[:len(packageFilePath)-len(".tar")] + ".raucb"
// packageFilePath = packageFilePath[:len(packageFilePath)-len(".tar")] + ".raucb"
// to check file exist
fmt.Println("rauc verify in cache:", packageFilePath)
if _, err := os.Stat(packageFilePath); os.IsNotExist(err) {
Expand All @@ -120,3 +124,69 @@ func MarkGood() error {
func RebootSystem() {
exec.Command("reboot").Run()
}

func getFreeMemory() (uint64, error) {
file, err := os.Open("/proc/meminfo")
if err != nil {
return 0, err
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "MemAvailable:") {
parts := strings.Fields(line)
if len(parts) < 2 {
return 0, fmt.Errorf("unexpected line in /proc/meminfo: %s", line)
}
mem, err := strconv.ParseUint(parts[1], 10, 64)
if err != nil {
return 0, err
}
// /proc/meminfo中内存的单位是KB,所以需要转换成GB
return mem / 1024 / 1024, nil
}
}
if scanner.Err() != nil {
return 0, scanner.Err()
}
return 0, fmt.Errorf("did not find MemAvailable in /proc/meminfo")
}

func CheckMemory() error {
mem, err := getFreeMemory()
if mem < 2 {
return fmt.Errorf("memory is less than 2GB")
}
if err != nil {
return err
}
return nil
}

func GetDescription(raucPath string) (string, error) {
cmd := exec.Command("rauc", "info", raucPath)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return "", err
}

lines := strings.Split(out.String(), "\n")
if len(lines) < 8 {
return "", fmt.Errorf("unexpected output: less than 8 lines")
}

line := lines[2]
prefix := "Description:\t'"
if !strings.HasPrefix(line, prefix) {
return "", fmt.Errorf("unexpected line format: %s", line)
}

description := strings.TrimPrefix(line, prefix)
description = strings.TrimSuffix(description, "'")

return description, nil
}
19 changes: 17 additions & 2 deletions service/rauc_offline.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"context"
"encoding/base64"
"fmt"
"io"
"os"
Expand All @@ -23,6 +24,19 @@ func (r *RAUCOfflineService) Install(release codegen.Release, sysRoot string) er
return InstallRAUC(release, sysRoot, r.InstallRAUCHandler)
}

func (r *RAUCOfflineService) LoadReleaseFromRAUC(sysRoot string) (*codegen.Release, error) {
base64_release, err := GetDescription(filepath.Join(sysRoot, config.RAUC_OFFLINE_PATH))
if err != nil {
return nil, err
}
releaseContent, err := base64.StdEncoding.DecodeString(base64_release)
if err != nil {
return nil, err
}

return internal.GetReleaseFromContent(releaseContent)
}

func (r *RAUCOfflineService) GetRelease(ctx context.Context, tag string) (*codegen.Release, error) {

if _, err := os.Stat(filepath.Join(r.SysRoot, config.RAUC_OFFLINE_PATH, config.RAUC_OFFLINE_RAUC_FILENAME)); os.IsNotExist(err) {
Expand All @@ -31,7 +45,7 @@ func (r *RAUCOfflineService) GetRelease(ctx context.Context, tag string) (*codeg
fmt.Println("found offline install package")
}

release, err := r.LoadReleaseFromOfflineRAUC(r.SysRoot)
release, err := r.LoadReleaseFromRAUC(r.SysRoot)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -80,7 +94,8 @@ func (r *RAUCOfflineService) DownloadRelease(ctx context.Context, release codege

func (r *RAUCOfflineService) ExtractRelease(packageFilepath string, release codegen.Release) error {
// 这个offline没有变化
return ExtractRAUCRelease(packageFilepath, release)
// return ExtractRAUCRelease(packageFilepath, release)
return nil
}

func (r *RAUCOfflineService) PostInstall(release codegen.Release, sysRoot string) error {
Expand Down
7 changes: 6 additions & 1 deletion service/rauc_online.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ type RAUCService struct {
}

func (r *RAUCService) Install(release codegen.Release, sysRoot string) error {
err := CheckMemory()
if err != nil {
return err
}
return InstallRAUC(release, sysRoot, r.InstallRAUCHandler)
}

Expand All @@ -43,7 +47,8 @@ func (r *RAUCService) DownloadRelease(ctx context.Context, release codegen.Relea
}

func (r *RAUCService) ExtractRelease(packageFilepath string, release codegen.Release) error {
return ExtractRAUCRelease(packageFilepath, release)
// return ExtractRAUCRelease(packageFilepath, release)
return nil
}

func (r *RAUCService) ShouldUpgrade(release codegen.Release, sysRoot string) bool {
Expand Down

0 comments on commit 6c0682c

Please sign in to comment.