|
| 1 | +package upgrade |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "csgclaw/internal/config" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + applyStatusEnvVar = "CSGCLAW_UPGRADE_STATUS_PATH" |
| 17 | + applyLogEnvVar = "CSGCLAW_UPGRADE_LOG_PATH" |
| 18 | + applyStatusFileName = "upgrade-helper-status.json" |
| 19 | + applyLogFileName = "upgrade-helper.log" |
| 20 | + applyLogsDirName = "logs" |
| 21 | +) |
| 22 | + |
| 23 | +type ApplyArtifacts struct { |
| 24 | + StatusPath string |
| 25 | + LogPath string |
| 26 | +} |
| 27 | + |
| 28 | +type applyFailureRecord struct { |
| 29 | + Message string `json:"message"` |
| 30 | + LogPath string `json:"log_path,omitempty"` |
| 31 | + UpdatedAt time.Time `json:"updated_at"` |
| 32 | +} |
| 33 | + |
| 34 | +func ResolveApplyArtifacts(configPath string) (ApplyArtifacts, error) { |
| 35 | + dir, err := applyArtifactsDir(configPath) |
| 36 | + if err != nil { |
| 37 | + return ApplyArtifacts{}, err |
| 38 | + } |
| 39 | + return ApplyArtifacts{ |
| 40 | + StatusPath: filepath.Join(dir, applyLogsDirName, applyStatusFileName), |
| 41 | + LogPath: filepath.Join(dir, applyLogsDirName, applyLogFileName), |
| 42 | + }, nil |
| 43 | +} |
| 44 | + |
| 45 | +func PrepareApplyArtifacts(configPath string) (ApplyArtifacts, error) { |
| 46 | + artifacts, err := ResolveApplyArtifacts(configPath) |
| 47 | + if err != nil { |
| 48 | + return ApplyArtifacts{}, err |
| 49 | + } |
| 50 | + if err := os.MkdirAll(filepath.Dir(artifacts.StatusPath), 0o755); err != nil { |
| 51 | + return ApplyArtifacts{}, fmt.Errorf("create upgrade helper state dir: %w", err) |
| 52 | + } |
| 53 | + if err := os.Remove(artifacts.StatusPath); err != nil && !errors.Is(err, os.ErrNotExist) { |
| 54 | + return ApplyArtifacts{}, fmt.Errorf("remove stale upgrade helper status: %w", err) |
| 55 | + } |
| 56 | + return artifacts, nil |
| 57 | +} |
| 58 | + |
| 59 | +func ApplyArtifactsFromEnv() ApplyArtifacts { |
| 60 | + return ApplyArtifacts{ |
| 61 | + StatusPath: strings.TrimSpace(os.Getenv(applyStatusEnvVar)), |
| 62 | + LogPath: strings.TrimSpace(os.Getenv(applyLogEnvVar)), |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func (a ApplyArtifacts) Enabled() bool { |
| 67 | + return strings.TrimSpace(a.StatusPath) != "" |
| 68 | +} |
| 69 | + |
| 70 | +func (a ApplyArtifacts) Env() []string { |
| 71 | + if !a.Enabled() { |
| 72 | + return nil |
| 73 | + } |
| 74 | + env := []string{fmt.Sprintf("%s=%s", applyStatusEnvVar, a.StatusPath)} |
| 75 | + if strings.TrimSpace(a.LogPath) != "" { |
| 76 | + env = append(env, fmt.Sprintf("%s=%s", applyLogEnvVar, a.LogPath)) |
| 77 | + } |
| 78 | + return env |
| 79 | +} |
| 80 | + |
| 81 | +func (a ApplyArtifacts) ClearStatus() error { |
| 82 | + if !a.Enabled() { |
| 83 | + return nil |
| 84 | + } |
| 85 | + if err := os.Remove(a.StatusPath); err != nil && !errors.Is(err, os.ErrNotExist) { |
| 86 | + return fmt.Errorf("remove upgrade helper status: %w", err) |
| 87 | + } |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +func (a ApplyArtifacts) RecordFailure(err error) error { |
| 92 | + if err == nil || !a.Enabled() { |
| 93 | + return nil |
| 94 | + } |
| 95 | + if mkdirErr := os.MkdirAll(filepath.Dir(a.StatusPath), 0o755); mkdirErr != nil { |
| 96 | + return fmt.Errorf("create upgrade helper state dir: %w", mkdirErr) |
| 97 | + } |
| 98 | + record := applyFailureRecord{ |
| 99 | + Message: err.Error(), |
| 100 | + LogPath: strings.TrimSpace(a.LogPath), |
| 101 | + UpdatedAt: time.Now().UTC(), |
| 102 | + } |
| 103 | + data, marshalErr := json.MarshalIndent(record, "", " ") |
| 104 | + if marshalErr != nil { |
| 105 | + return fmt.Errorf("encode upgrade helper status: %w", marshalErr) |
| 106 | + } |
| 107 | + if writeErr := os.WriteFile(a.StatusPath, append(data, '\n'), 0o600); writeErr != nil { |
| 108 | + return fmt.Errorf("write upgrade helper status: %w", writeErr) |
| 109 | + } |
| 110 | + return nil |
| 111 | +} |
| 112 | + |
| 113 | +func ConsumeApplyFailure(configPath string) (string, error) { |
| 114 | + artifacts, err := ResolveApplyArtifacts(configPath) |
| 115 | + if err != nil { |
| 116 | + return "", err |
| 117 | + } |
| 118 | + data, err := os.ReadFile(artifacts.StatusPath) |
| 119 | + if err != nil { |
| 120 | + if errors.Is(err, os.ErrNotExist) { |
| 121 | + return "", nil |
| 122 | + } |
| 123 | + return "", fmt.Errorf("read upgrade helper status: %w", err) |
| 124 | + } |
| 125 | + |
| 126 | + var record applyFailureRecord |
| 127 | + if err := json.Unmarshal(data, &record); err != nil { |
| 128 | + return "", fmt.Errorf("decode upgrade helper status: %w", err) |
| 129 | + } |
| 130 | + if err := os.Remove(artifacts.StatusPath); err != nil && !errors.Is(err, os.ErrNotExist) { |
| 131 | + return "", fmt.Errorf("remove consumed upgrade helper status: %w", err) |
| 132 | + } |
| 133 | + |
| 134 | + message := strings.TrimSpace(record.Message) |
| 135 | + if message == "" { |
| 136 | + return "", nil |
| 137 | + } |
| 138 | + if logPath := strings.TrimSpace(record.LogPath); logPath != "" { |
| 139 | + message = fmt.Sprintf("%s\nLog: %s", message, logPath) |
| 140 | + } |
| 141 | + return message, nil |
| 142 | +} |
| 143 | + |
| 144 | +func applyArtifactsDir(configPath string) (string, error) { |
| 145 | + if path := strings.TrimSpace(configPath); path != "" { |
| 146 | + return filepath.Dir(path), nil |
| 147 | + } |
| 148 | + return config.DefaultDir() |
| 149 | +} |
0 commit comments