fix(ota): disable elf check and fix self-update binary replacement

This commit is contained in:
2026-05-09 14:16:27 +08:00
parent cde3572f79
commit 04d6a667fe
2 changed files with 40 additions and 14 deletions

View File

@@ -33,6 +33,7 @@ func (o *OTAAgent) Run(ctx context.Context) {
ticker := time.NewTicker(10 * time.Minute) ticker := time.NewTicker(10 * time.Minute)
defer ticker.Stop() defer ticker.Stop()
log.Printf("ota: current hour=%d, maintenance window check=%v", time.Now().Hour(), inMaintenanceWindow())
// Check once on startup if in maintenance window // Check once on startup if in maintenance window
if inMaintenanceWindow() { if inMaintenanceWindow() {
o.check(client) o.check(client)
@@ -56,8 +57,14 @@ func inMaintenanceWindow() bool {
} }
func (o *OTAAgent) check(client *http.Client) { func (o *OTAAgent) check(client *http.Client) {
log.Printf("ota: checking for updates (window=%v, current_version=%s)", inMaintenanceWindow(), o.cfg.Version)
otaURL := o.cfg.OTAUrl
if otaURL == "" {
otaURL = o.cfg.CloudURL
}
url := fmt.Sprintf("%s/api/v1/edge/update/manifest?edge_id=%s&current_version=%s", url := fmt.Sprintf("%s/api/v1/edge/update/manifest?edge_id=%s&current_version=%s",
o.cfg.CloudURL, o.cfg.EdgeID, o.cfg.Version) otaURL, o.cfg.EdgeID, o.cfg.Version)
log.Printf("ota: requesting %s", url)
req, _ := http.NewRequest(http.MethodGet, url, nil) req, _ := http.NewRequest(http.MethodGet, url, nil)
req.Header.Set("Authorization", "Bearer "+o.cfg.EdgeToken) req.Header.Set("Authorization", "Bearer "+o.cfg.EdgeToken)
resp, err := client.Do(req) resp, err := client.Do(req)
@@ -66,14 +73,17 @@ func (o *OTAAgent) check(client *http.Client) {
return return
} }
defer resp.Body.Close() defer resp.Body.Close()
log.Printf("ota manifest: status=%d", resp.StatusCode)
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
return return
} }
var mf map[string]string var mf map[string]any
if err := json.NewDecoder(resp.Body).Decode(&mf); err != nil { if err := json.NewDecoder(resp.Body).Decode(&mf); err != nil {
log.Printf("ota manifest: decode error: %v", err)
return return
} }
target := mf["target_version"] log.Printf("ota manifest: target_version=%v", mf["target_version"])
target, _ := mf["target_version"].(string)
if target == "" || target == o.cfg.Version { if target == "" || target == o.cfg.Version {
return return
} }
@@ -90,9 +100,10 @@ func (o *OTAAgent) check(client *http.Client) {
o.report(client, target, "installed") o.report(client, target, "installed")
} }
func (o *OTAAgent) downloadAndVerify(client *http.Client, mf map[string]string) error { func (o *OTAAgent) downloadAndVerify(client *http.Client, mf map[string]any) error {
url, target := mf["package_url"], mf["target_version"] url, _ := mf["package_url"].(string)
expected := mf["sha256"] target, _ := mf["target_version"].(string)
expected, _ := mf["sha256"].(string)
if url == "" || expected == "" { if url == "" || expected == "" {
return fmt.Errorf("invalid manifest") return fmt.Errorf("invalid manifest")
} }
@@ -163,20 +174,31 @@ func (o *OTAAgent) install(target string) error {
magic := make([]byte, 4) magic := make([]byte, 4)
header.Read(magic) header.Read(magic)
header.Close() header.Close()
// Temporarily disabled ELF check to allow script-based upgrade package for testing
/*
if !bytes.Equal(magic, []byte{0x7f, 0x45, 0x4c, 0x46}) { // ELF magic if !bytes.Equal(magic, []byte{0x7f, 0x45, 0x4c, 0x46}) { // ELF magic
return fmt.Errorf("new binary is not a valid ELF executable") return fmt.Errorf("new binary is not a valid ELF executable")
} }
*/
// Backup current binary // Replace binary - use mv to handle "text file busy" on running executable
backupPath := "/opt/tianyan-edge/edge-agent.bak" backupPath := "/opt/tianyan-edge/edge-agent.bak"
if err := copyFile("/opt/tianyan-edge/edge-agent", backupPath); err != nil { // 1. Move new binary to a staging path
log.Printf("ota: backup failed (non-fatal): %v", err) newPath := "/opt/tianyan-edge/edge-agent.new"
if err := copyFile(newBinary, newPath); err != nil {
return fmt.Errorf("stage new binary: %w", err)
} }
// Replace binary // 2. Rename current to backup
if err := copyFile(newBinary, "/opt/tianyan-edge/edge-agent"); err != nil { if err := os.Rename("/opt/tianyan-edge/edge-agent", backupPath); err != nil {
return fmt.Errorf("replace binary: %w", err) return fmt.Errorf("rename current to bak: %w", err)
} }
// 3. Rename new to current
if err := os.Rename(newPath, "/opt/tianyan-edge/edge-agent"); err != nil {
return fmt.Errorf("rename new to current: %w", err)
}
if err := os.Chmod("/opt/tianyan-edge/edge-agent", 0755); err != nil { if err := os.Chmod("/opt/tianyan-edge/edge-agent", 0755); err != nil {
return fmt.Errorf("chmod: %w", err) return fmt.Errorf("chmod: %w", err)
} }
@@ -253,10 +275,14 @@ func copyFile(src, dst string) error {
} }
func (o *OTAAgent) report(client *http.Client, version, status string) { func (o *OTAAgent) report(client *http.Client, version, status string) {
otaURL := o.cfg.OTAUrl
if otaURL == "" {
otaURL = o.cfg.CloudURL
}
payload := map[string]any{"edge_id": o.cfg.EdgeID, "version": version, payload := map[string]any{"edge_id": o.cfg.EdgeID, "version": version,
"status": status, "ts": float64(time.Now().UnixMilli()) / 1000.0} "status": status, "ts": float64(time.Now().UnixMilli()) / 1000.0}
body, _ := json.Marshal(payload) body, _ := json.Marshal(payload)
url := fmt.Sprintf("%s/api/v1/edge/update/report", o.cfg.CloudURL) url := fmt.Sprintf("%s/api/v1/edge/update/report", otaURL)
req, _ := http.NewRequest(http.MethodPost, url, bytes.NewReader(body)) req, _ := http.NewRequest(http.MethodPost, url, bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+o.cfg.EdgeToken) req.Header.Set("Authorization", "Bearer "+o.cfg.EdgeToken)

BIN
python/.infer_server.py.swp Normal file

Binary file not shown.