|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "syscall" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/fiatjaf/pyramid/global" |
| 14 | +) |
| 15 | + |
| 16 | +// this is set at build time to something else based on git |
| 17 | +var currentVersion string = "dev" |
| 18 | + |
| 19 | +type releaseVersion struct { |
| 20 | + name string |
| 21 | + binaryURL string |
| 22 | +} |
| 23 | + |
| 24 | +var latestVersion releaseVersion |
| 25 | + |
| 26 | +func fetchLatestVersion() { |
| 27 | + client := &http.Client{Timeout: 10 * time.Second} |
| 28 | + resp, err := client.Get("https://api.github.com/repos/fiatjaf/pyramid/releases/latest") |
| 29 | + if err != nil { |
| 30 | + log.Error().Err(err).Msg("failed to fetch latest release from github") |
| 31 | + return |
| 32 | + } |
| 33 | + defer resp.Body.Close() |
| 34 | + |
| 35 | + if resp.StatusCode != http.StatusOK { |
| 36 | + log.Error().Int("status", resp.StatusCode).Msg("github api returned non-200 status") |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + body, err := io.ReadAll(resp.Body) |
| 41 | + if err != nil { |
| 42 | + log.Error().Err(err).Msg("failed to read github api response") |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + var release struct { |
| 47 | + TagName string `json:"tag_name"` |
| 48 | + Name string `json:"name"` |
| 49 | + Assets []struct { |
| 50 | + Name string `json:"name"` |
| 51 | + URL string `json:"browser_download_url"` |
| 52 | + } `json:"assets"` |
| 53 | + } |
| 54 | + if err := json.Unmarshal(body, &release); err != nil { |
| 55 | + log.Error().Err(err).Msg("failed to parse github api response") |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + // find the pyramid-exe asset |
| 60 | + var binaryURL string |
| 61 | + for _, asset := range release.Assets { |
| 62 | + if asset.Name == "pyramid-exe" { |
| 63 | + binaryURL = asset.URL |
| 64 | + break |
| 65 | + } |
| 66 | + } |
| 67 | + if binaryURL == "" { |
| 68 | + log.Error().Msg("pyramid-exe asset not found in latest release") |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + latestVersion = releaseVersion{ |
| 73 | + name: release.TagName, |
| 74 | + binaryURL: binaryURL, |
| 75 | + } |
| 76 | + log.Info().Str("version", latestVersion.name).Msg("fetched latest version from github") |
| 77 | +} |
| 78 | + |
| 79 | +func performUpdateInPlace() error { |
| 80 | + log.Info().Str("version", latestVersion.name).Msg("performing in-place update") |
| 81 | + if latestVersion.binaryURL == "" { |
| 82 | + return fmt.Errorf("no update available") |
| 83 | + } |
| 84 | + |
| 85 | + currentBinary, err := os.Executable() |
| 86 | + if err != nil { |
| 87 | + return fmt.Errorf("failed to get executable path: %w", err) |
| 88 | + } |
| 89 | + |
| 90 | + // download the new binary |
| 91 | + log.Info().Str("url", latestVersion.binaryURL).Msg("downloading version for update") |
| 92 | + client := &http.Client{Timeout: 30 * time.Second} |
| 93 | + resp, err := client.Get(latestVersion.binaryURL) |
| 94 | + if err != nil { |
| 95 | + return fmt.Errorf("failed to download binary: %w", err) |
| 96 | + } |
| 97 | + defer resp.Body.Close() |
| 98 | + if resp.StatusCode != 200 { |
| 99 | + b, _ := io.ReadAll(resp.Body) |
| 100 | + body := string(b) |
| 101 | + if len(body) > 200 { |
| 102 | + body = body[0:199] + "…" |
| 103 | + } |
| 104 | + log.Warn().Str("body", body).Int("status", resp.StatusCode).Msg("github failed to serve us the binary again") |
| 105 | + return fmt.Errorf("downloading the new binary from github failed with status %d", resp.StatusCode) |
| 106 | + } |
| 107 | + // save the new binary to a stable path (overwrite it if it exists) |
| 108 | + tempPath := fmt.Sprintf("pyramid-update-%s", latestVersion.name) |
| 109 | + tempFile, err := os.Create(tempPath) |
| 110 | + if err != nil { |
| 111 | + return fmt.Errorf("failed to create temp file: %w", err) |
| 112 | + } |
| 113 | + defer os.Remove(tempPath) |
| 114 | + if _, err := io.Copy(tempFile, resp.Body); err != nil { |
| 115 | + tempFile.Close() |
| 116 | + return fmt.Errorf("failed to write binary: %w", err) |
| 117 | + } |
| 118 | + tempFile.Close() |
| 119 | + |
| 120 | + // use rename for atomic replacement |
| 121 | + log.Info().Msg("replacing binary with new version") |
| 122 | + if err := os.Rename(currentBinary, "pyramid-old-binary"); err != nil { |
| 123 | + return fmt.Errorf("replace failed: %w", err) |
| 124 | + } |
| 125 | + if err := os.Rename(tempPath, currentBinary); err != nil { |
| 126 | + return fmt.Errorf("replace failed: %w", err) |
| 127 | + } |
| 128 | + // ensure executable permissions on the final binary |
| 129 | + if err := os.Chmod(currentBinary, 0755); err != nil { |
| 130 | + return fmt.Errorf("chmod failed: %w", err) |
| 131 | + } |
| 132 | + |
| 133 | + log.Info().Msg("restarting process with new binary...") |
| 134 | + // get the absolute path (syscall.Exec requires absolute path) |
| 135 | + absPath, err := filepath.Abs(currentBinary) |
| 136 | + if err != nil { |
| 137 | + return fmt.Errorf("failed to get absolute path: %w", err) |
| 138 | + } |
| 139 | + |
| 140 | + // execute the new binary, replacing current process |
| 141 | + // this call does not return if successful, therefore we must perform a graceful deinitialization of all things |
| 142 | + cancelStartContext(updating) |
| 143 | + global.End() |
| 144 | + err = syscall.Exec(absPath, append([]string{absPath}, os.Args[1:]...), os.Environ()) |
| 145 | + |
| 146 | + // if we reach here, exec failed |
| 147 | + return fmt.Errorf("exec failed: %w", err) |
| 148 | +} |
0 commit comments