| 1 | // Command bluegit-runner draait BlueGit CI-jobs op een eigen machine. |
| 2 | // Hij maakt alleen uitgaande HTTPS-verbindingen (long-poll), er hoeft |
| 3 | // geen poort open. |
| 4 | package main |
| 5 | |
| 6 | import ( |
| 7 | "bytes" |
| 8 | "context" |
| 9 | "encoding/json" |
| 10 | "errors" |
| 11 | "flag" |
| 12 | "fmt" |
| 13 | "io" |
| 14 | "log/slog" |
| 15 | "net/http" |
| 16 | "os" |
| 17 | "os/exec" |
| 18 | "os/signal" |
| 19 | "os/user" |
| 20 | "path/filepath" |
| 21 | "strconv" |
| 22 | "strings" |
| 23 | "sync" |
| 24 | "syscall" |
| 25 | "time" |
| 26 | |
| 27 | "gopkg.in/yaml.v3" |
| 28 | |
| 29 | "gitlab.com/Timfa/bluegit/internal/runner" |
| 30 | ) |
| 31 | |
| 32 | var version = "dev" |
| 33 | |
| 34 | const defaultConfig = "/etc/bluegit-runner/config.yml" |
| 35 | |
| 36 | // Config staat in /etc/bluegit-runner/config.yml. |
| 37 | type Config struct { |
| 38 | URL string `yaml:"url"` |
| 39 | Token string `yaml:"token"` |
| 40 | Name string `yaml:"name"` |
| 41 | Docker string `yaml:"docker"` |
| 42 | Git string `yaml:"git"` |
| 43 | WorkDir string `yaml:"work_dir"` |
| 44 | Network string `yaml:"network"` |
| 45 | Concurrency int `yaml:"concurrency"` |
| 46 | } |
| 47 | |
| 48 | func (c *Config) defaults() { |
| 49 | if c.Docker == "" { |
| 50 | c.Docker = "docker" |
| 51 | } |
| 52 | if c.Git == "" { |
| 53 | c.Git = "git" |
| 54 | } |
| 55 | if c.WorkDir == "" { |
| 56 | c.WorkDir = "/var/lib/bluegit-runner" |
| 57 | } |
| 58 | if c.Network == "" { |
| 59 | c.Network = "bridge" |
| 60 | } |
| 61 | if c.Concurrency < 1 { |
| 62 | c.Concurrency = 1 |
| 63 | } |
| 64 | c.URL = strings.TrimRight(c.URL, "/") |
| 65 | } |
| 66 | |
| 67 | func main() { |
| 68 | if len(os.Args) < 2 { |
| 69 | usage() |
| 70 | os.Exit(2) |
| 71 | } |
| 72 | var err error |
| 73 | switch os.Args[1] { |
| 74 | case "register": |
| 75 | err = cmdRegister(os.Args[2:]) |
| 76 | case "run": |
| 77 | err = cmdRun(os.Args[2:]) |
| 78 | case "version", "--version": |
| 79 | fmt.Println(version) |
| 80 | default: |
| 81 | usage() |
| 82 | os.Exit(2) |
| 83 | } |
| 84 | if err != nil { |
| 85 | fmt.Fprintln(os.Stderr, "bluegit-runner:", err) |
| 86 | os.Exit(1) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func usage() { |
| 91 | fmt.Fprintln(os.Stderr, `usage: |
| 92 | bluegit-runner register --url https://git.example.com --token bgrr_... [--name NAME] [--config PATH] |
| 93 | bluegit-runner run [--config PATH] |
| 94 | bluegit-runner version`) |
| 95 | } |
| 96 | |
| 97 | func loadConfig(path string) (Config, error) { |
| 98 | var c Config |
| 99 | b, err := os.ReadFile(path) |
| 100 | if err != nil { |
| 101 | return c, err |
| 102 | } |
| 103 | if err := yaml.Unmarshal(b, &c); err != nil { |
| 104 | return c, err |
| 105 | } |
| 106 | c.defaults() |
| 107 | if c.URL == "" || c.Token == "" { |
| 108 | return c, fmt.Errorf("%s: url and token are required; run bluegit-runner register first", path) |
| 109 | } |
| 110 | return c, nil |
| 111 | } |
| 112 | |
| 113 | func cmdRegister(args []string) error { |
| 114 | fs := flag.NewFlagSet("register", flag.ExitOnError) |
| 115 | url := fs.String("url", "", "BlueGit base URL") |
| 116 | token := fs.String("token", "", "registration token from the BlueGit UI") |
| 117 | name := fs.String("name", "", "runner name (default: hostname)") |
| 118 | cfgPath := fs.String("config", defaultConfig, "config file") |
| 119 | _ = fs.Parse(args) |
| 120 | if *token == "" { |
| 121 | *token = os.Getenv("BLUEGIT_RUNNER_REGISTRATION_TOKEN") |
| 122 | } |
| 123 | if *url == "" || *token == "" { |
| 124 | return errors.New("--url and --token are required") |
| 125 | } |
| 126 | if *name == "" { |
| 127 | *name, _ = os.Hostname() |
| 128 | } |
| 129 | var existing Config |
| 130 | if b, err := os.ReadFile(*cfgPath); err == nil { |
| 131 | _ = yaml.Unmarshal(b, &existing) |
| 132 | } |
| 133 | body, _ := json.Marshal(map[string]string{"token": *token, "name": *name, "version": version}) |
| 134 | resp, err := http.Post(strings.TrimRight(*url, "/")+"/api/v1/runners/register", "application/json", bytes.NewReader(body)) |
| 135 | if err != nil { |
| 136 | return err |
| 137 | } |
| 138 | defer resp.Body.Close() |
| 139 | var out struct { |
| 140 | ID int64 `json:"id"` |
| 141 | Name string `json:"name"` |
| 142 | Token string `json:"token"` |
| 143 | Message string `json:"message"` |
| 144 | } |
| 145 | _ = json.NewDecoder(resp.Body).Decode(&out) |
| 146 | if resp.StatusCode != http.StatusCreated || out.Token == "" { |
| 147 | return fmt.Errorf("registration failed (%d): %s", resp.StatusCode, out.Message) |
| 148 | } |
| 149 | existing.URL, existing.Token, existing.Name = strings.TrimRight(*url, "/"), out.Token, out.Name |
| 150 | b, err := yaml.Marshal(existing) |
| 151 | if err != nil { |
| 152 | return err |
| 153 | } |
| 154 | if err := os.MkdirAll(filepath.Dir(*cfgPath), 0o755); err != nil { |
| 155 | return err |
| 156 | } |
| 157 | if err := os.WriteFile(*cfgPath, b, 0o600); err != nil { |
| 158 | return err |
| 159 | } |
| 160 | if os.Geteuid() == 0 { |
| 161 | if u, err := user.Lookup("bluegit-runner"); err == nil { |
| 162 | uid, _ := strconv.Atoi(u.Uid) |
| 163 | gid, _ := strconv.Atoi(u.Gid) |
| 164 | _ = os.Chown(*cfgPath, uid, gid) |
| 165 | } |
| 166 | } |
| 167 | fmt.Printf("registered runner %q (id %d); config written to %s\n", out.Name, out.ID, *cfgPath) |
| 168 | // Met het AUR-pakket: de service is al enabled en wacht op de config. |
| 169 | if os.Geteuid() == 0 && *cfgPath == defaultConfig { |
| 170 | if _, err := os.Stat("/usr/lib/systemd/system/bluegit-runner.service"); err == nil { |
| 171 | if err := exec.Command("systemctl", "enable", "--now", "bluegit-runner.service").Run(); err == nil { |
| 172 | _ = exec.Command("systemctl", "restart", "bluegit-runner.service").Run() |
| 173 | fmt.Println("bluegit-runner.service started") |
| 174 | return nil |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | fmt.Println("start it with: sudo systemctl enable --now bluegit-runner") |
| 179 | return nil |
| 180 | } |
| 181 | |
| 182 | type client struct { |
| 183 | cfg Config |
| 184 | http *http.Client |
| 185 | log *slog.Logger |
| 186 | } |
| 187 | |
| 188 | func (c *client) do(ctx context.Context, method, path string, body io.Reader, ctype string) (*http.Response, error) { |
| 189 | req, err := http.NewRequestWithContext(ctx, method, c.cfg.URL+path, body) |
| 190 | if err != nil { |
| 191 | return nil, err |
| 192 | } |
| 193 | req.Header.Set("Authorization", "Bearer "+c.cfg.Token) |
| 194 | req.Header.Set("X-Runner-Version", version) |
| 195 | if ctype != "" { |
| 196 | req.Header.Set("Content-Type", ctype) |
| 197 | } |
| 198 | return c.http.Do(req) |
| 199 | } |
| 200 | |
| 201 | func (c *client) postJSON(ctx context.Context, path string, in, out any) error { |
| 202 | b, _ := json.Marshal(in) |
| 203 | resp, err := c.do(ctx, http.MethodPost, path, bytes.NewReader(b), "application/json") |
| 204 | if err != nil { |
| 205 | return err |
| 206 | } |
| 207 | defer resp.Body.Close() |
| 208 | if resp.StatusCode >= 300 { |
| 209 | msg, _ := io.ReadAll(io.LimitReader(resp.Body, 4096)) |
| 210 | return fmt.Errorf("%s: %d %s", path, resp.StatusCode, strings.TrimSpace(string(msg))) |
| 211 | } |
| 212 | if out != nil { |
| 213 | return json.NewDecoder(resp.Body).Decode(out) |
| 214 | } |
| 215 | return nil |
| 216 | } |
| 217 | |
| 218 | func cmdRun(args []string) error { |
| 219 | fs := flag.NewFlagSet("run", flag.ExitOnError) |
| 220 | cfgPath := fs.String("config", defaultConfig, "config file") |
| 221 | once := fs.Bool("once", false, "run at most one job, then exit (for tests)") |
| 222 | _ = fs.Parse(args) |
| 223 | cfg, err := loadConfig(*cfgPath) |
| 224 | if err != nil { |
| 225 | return err |
| 226 | } |
| 227 | log := slog.New(slog.NewTextHandler(os.Stderr, nil)) |
| 228 | ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) |
| 229 | defer stop() |
| 230 | c := &client{cfg: cfg, http: &http.Client{Timeout: 0}, log: log} |
| 231 | if err := c.postJSON(ctx, "/api/v1/runner/heartbeat", map[string]string{}, nil); err != nil { |
| 232 | return fmt.Errorf("cannot reach BlueGit: %w", err) |
| 233 | } |
| 234 | log.Info("bluegit-runner started", "url", cfg.URL, "name", cfg.Name, "version", version, "concurrency", cfg.Concurrency) |
| 235 | go func() { |
| 236 | t := time.NewTicker(30 * time.Second) |
| 237 | defer t.Stop() |
| 238 | for { |
| 239 | select { |
| 240 | case <-ctx.Done(): |
| 241 | return |
| 242 | case <-t.C: |
| 243 | if err := c.postJSON(ctx, "/api/v1/runner/heartbeat", map[string]string{}, nil); err != nil && ctx.Err() == nil { |
| 244 | log.Warn("heartbeat", "err", err) |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | }() |
| 249 | var wg sync.WaitGroup |
| 250 | for i := 0; i < cfg.Concurrency; i++ { |
| 251 | wg.Add(1) |
| 252 | go func() { |
| 253 | defer wg.Done() |
| 254 | for ctx.Err() == nil { |
| 255 | ran, err := c.poll(ctx) |
| 256 | if err != nil && ctx.Err() == nil { |
| 257 | log.Warn("poll", "err", err) |
| 258 | select { |
| 259 | case <-ctx.Done(): |
| 260 | case <-time.After(5 * time.Second): |
| 261 | } |
| 262 | } |
| 263 | if ran && *once { |
| 264 | stop() |
| 265 | } |
| 266 | } |
| 267 | }() |
| 268 | } |
| 269 | wg.Wait() |
| 270 | return nil |
| 271 | } |
| 272 | |
| 273 | func (c *client) poll(ctx context.Context) (bool, error) { |
| 274 | pctx, cancel := context.WithTimeout(ctx, 60*time.Second) |
| 275 | defer cancel() |
| 276 | resp, err := c.do(pctx, http.MethodPost, "/api/v1/runner/jobs/request", strings.NewReader("{}"), "application/json") |
| 277 | if err != nil { |
| 278 | return false, err |
| 279 | } |
| 280 | defer resp.Body.Close() |
| 281 | if resp.StatusCode == http.StatusNoContent { |
| 282 | return false, nil |
| 283 | } |
| 284 | if resp.StatusCode != http.StatusOK { |
| 285 | msg, _ := io.ReadAll(io.LimitReader(resp.Body, 4096)) |
| 286 | return false, fmt.Errorf("request: %d %s", resp.StatusCode, strings.TrimSpace(string(msg))) |
| 287 | } |
| 288 | var pl runner.RemotePayload |
| 289 | if err := json.NewDecoder(resp.Body).Decode(&pl); err != nil { |
| 290 | return false, err |
| 291 | } |
| 292 | c.runJob(ctx, pl) |
| 293 | return true, nil |
| 294 | } |
| 295 | |
| 296 | // logBuffer verzamelt logregels en stuurt ze elke seconde door. |
| 297 | type logBuffer struct { |
| 298 | mu sync.Mutex |
| 299 | buf strings.Builder |
| 300 | } |
| 301 | |
| 302 | func (l *logBuffer) add(s string) { |
| 303 | l.mu.Lock() |
| 304 | l.buf.WriteString(s) |
| 305 | l.mu.Unlock() |
| 306 | } |
| 307 | |
| 308 | func (l *logBuffer) take() string { |
| 309 | l.mu.Lock() |
| 310 | defer l.mu.Unlock() |
| 311 | s := l.buf.String() |
| 312 | l.buf.Reset() |
| 313 | return s |
| 314 | } |
| 315 | |
| 316 | func (c *client) runJob(parent context.Context, pl runner.RemotePayload) { |
| 317 | c.log.Info("job", "id", pl.JobID, "name", pl.Job.Name) |
| 318 | base := fmt.Sprintf("/api/v1/runner/jobs/%d", pl.JobID) |
| 319 | ctx, cancel := context.WithCancel(parent) |
| 320 | defer cancel() |
| 321 | runCtx := ctx |
| 322 | if pl.Timeout > 0 { |
| 323 | var tc context.CancelFunc |
| 324 | runCtx, tc = context.WithTimeout(ctx, time.Duration(pl.Timeout)*time.Second) |
| 325 | defer tc() |
| 326 | } |
| 327 | var canceled bool |
| 328 | var cmu sync.Mutex |
| 329 | logs := &logBuffer{} |
| 330 | send := func() { |
| 331 | chunk := logs.take() |
| 332 | var out struct { |
| 333 | Cancel bool `json:"cancel"` |
| 334 | } |
| 335 | sctx, sc := context.WithTimeout(context.Background(), 30*time.Second) |
| 336 | defer sc() |
| 337 | err := func() error { |
| 338 | resp, err := c.do(sctx, http.MethodPost, base+"/log", strings.NewReader(chunk), "text/plain") |
| 339 | if err != nil { |
| 340 | return err |
| 341 | } |
| 342 | defer resp.Body.Close() |
| 343 | if resp.StatusCode != http.StatusOK { |
| 344 | return fmt.Errorf("log: %d", resp.StatusCode) |
| 345 | } |
| 346 | return json.NewDecoder(resp.Body).Decode(&out) |
| 347 | }() |
| 348 | if err != nil { |
| 349 | // Bij een fout gaat de chunk terug in de buffer voor de volgende poging. |
| 350 | if chunk != "" { |
| 351 | logs.mu.Lock() |
| 352 | rest := logs.buf.String() |
| 353 | logs.buf.Reset() |
| 354 | logs.buf.WriteString(chunk + rest) |
| 355 | logs.mu.Unlock() |
| 356 | } |
| 357 | return |
| 358 | } |
| 359 | if out.Cancel { |
| 360 | cmu.Lock() |
| 361 | canceled = true |
| 362 | cmu.Unlock() |
| 363 | cancel() |
| 364 | } |
| 365 | } |
| 366 | done := make(chan struct{}) |
| 367 | flushed := make(chan struct{}) |
| 368 | go func() { |
| 369 | defer close(flushed) |
| 370 | t := time.NewTicker(time.Second) |
| 371 | defer t.Stop() |
| 372 | for { |
| 373 | select { |
| 374 | case <-done: |
| 375 | return |
| 376 | case <-t.C: |
| 377 | send() |
| 378 | } |
| 379 | } |
| 380 | }() |
| 381 | r := &runner.Runner{GitBin: c.cfg.Git, Docker: c.cfg.Docker, Storage: c.cfg.WorkDir, Network: c.cfg.Network, Name: c.cfg.Name} |
| 382 | work := filepath.Join(c.cfg.WorkDir, "work", strconv.FormatInt(pl.JobID, 10)) |
| 383 | defer os.RemoveAll(work) |
| 384 | clone := func(ctx context.Context, dir string) error { |
| 385 | cmd := exec.CommandContext(ctx, c.cfg.Git, "clone", "--quiet", pl.CloneURL, dir) |
| 386 | // Het runner-token via de omgeving, niet in de proceslijst. |
| 387 | cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0", "GIT_CONFIG_COUNT=1", |
| 388 | "GIT_CONFIG_KEY_0=http.extraHeader", "GIT_CONFIG_VALUE_0=Authorization: Bearer "+c.cfg.Token) |
| 389 | out, err := cmd.CombinedOutput() |
| 390 | if err != nil { |
| 391 | return fmt.Errorf("%s", strings.ReplaceAll(strings.TrimSpace(string(out)), c.cfg.Token, "[MASKED]")) |
| 392 | } |
| 393 | return nil |
| 394 | } |
| 395 | inputs := func(ctx context.Context, dir string) error { |
| 396 | resp, err := c.do(ctx, http.MethodGet, base+"/inputs", nil, "") |
| 397 | if err != nil { |
| 398 | return err |
| 399 | } |
| 400 | defer resp.Body.Close() |
| 401 | if resp.StatusCode != http.StatusOK { |
| 402 | return fmt.Errorf("inputs: %d", resp.StatusCode) |
| 403 | } |
| 404 | return runner.ReadTar(resp.Body, dir, nil, 0) |
| 405 | } |
| 406 | err := r.RunRemote(runCtx, pl, work, logs.add, clone, inputs) |
| 407 | if err == nil && len(pl.Job.ArtifactPaths) > 0 { |
| 408 | err = c.upload(runCtx, base, work, pl.Job.ArtifactPaths) |
| 409 | } |
| 410 | close(done) |
| 411 | <-flushed |
| 412 | send() |
| 413 | status, msg := "success", "" |
| 414 | cmu.Lock() |
| 415 | wasCanceled := canceled |
| 416 | cmu.Unlock() |
| 417 | switch { |
| 418 | case err == nil: |
| 419 | case wasCanceled: |
| 420 | status = "canceled" |
| 421 | case errors.Is(runCtx.Err(), context.DeadlineExceeded): |
| 422 | status, msg = "failed", "job_execution_timeout: "+err.Error() |
| 423 | default: |
| 424 | status, msg = "failed", err.Error() |
| 425 | } |
| 426 | fctx, fc := context.WithTimeout(context.Background(), time.Minute) |
| 427 | defer fc() |
| 428 | if ferr := c.postJSON(fctx, base+"/finish", map[string]string{"status": status, "message": msg}, nil); ferr != nil { |
| 429 | c.log.Error("finish", "job", pl.JobID, "err", ferr) |
| 430 | } |
| 431 | c.log.Info("job done", "id", pl.JobID, "status", status) |
| 432 | } |
| 433 | |
| 434 | func (c *client) upload(ctx context.Context, base, work string, paths []string) error { |
| 435 | pr, pw := io.Pipe() |
| 436 | go func() { pw.CloseWithError(runner.WriteTar(pw, work, paths)) }() |
| 437 | resp, err := c.do(ctx, http.MethodPut, base+"/artifacts", pr, "application/gzip") |
| 438 | if err != nil { |
| 439 | return fmt.Errorf("artifacts upload: %w", err) |
| 440 | } |
| 441 | defer resp.Body.Close() |
| 442 | if resp.StatusCode != http.StatusOK { |
| 443 | msg, _ := io.ReadAll(io.LimitReader(resp.Body, 4096)) |
| 444 | return fmt.Errorf("artifacts upload: %d %s", resp.StatusCode, strings.TrimSpace(string(msg))) |
| 445 | } |
| 446 | return nil |
| 447 | } |