blob: 289efe718715f3862ceb7a8e399ac1f39939fc2b [file] [log] [blame]
Lorenz Brunc88c82d2020-05-08 14:35:04 +02001// Copyright 2020 The Monogon Project Authors.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17package containerd
18
19import (
20 "context"
Lorenz Brun878f5f92020-05-12 16:15:39 +020021 "fmt"
Lorenz Brun6acfc322020-05-13 17:01:26 +020022 "io"
Lorenz Brunc88c82d2020-05-08 14:35:04 +020023 "os"
24 "os/exec"
Lorenz Brun6acfc322020-05-13 17:01:26 +020025 "time"
26
Serge Bazanskic2c7ad92020-07-13 17:20:09 +020027 "git.monogon.dev/source/nexantic.git/core/internal/localstorage"
Lorenz Brunc88c82d2020-05-08 14:35:04 +020028 "git.monogon.dev/source/nexantic.git/core/pkg/logbuffer"
Lorenz Brunc88c82d2020-05-08 14:35:04 +020029)
30
Lorenz Brun878f5f92020-05-12 16:15:39 +020031type Service struct {
Serge Bazanskic2c7ad92020-07-13 17:20:09 +020032 EphemeralVolume *localstorage.EphemeralContainerdDirectory
33
Lorenz Brun6acfc322020-05-13 17:01:26 +020034 Log *logbuffer.LogBuffer
35 RunscLog *logbuffer.LogBuffer
Lorenz Brun878f5f92020-05-12 16:15:39 +020036}
Lorenz Brunc88c82d2020-05-08 14:35:04 +020037
Serge Bazanskic2c7ad92020-07-13 17:20:09 +020038func (s *Service) Run(ctx context.Context) error {
39 if s.Log == nil {
40 s.Log = logbuffer.New(5000, 16384)
41 }
42 if s.RunscLog == nil {
43 s.RunscLog = logbuffer.New(5000, 16384)
44 }
Lorenz Brunc88c82d2020-05-08 14:35:04 +020045
Serge Bazanskic2c7ad92020-07-13 17:20:09 +020046 cmd := exec.CommandContext(ctx, "/containerd/bin/containerd", "--config", "/containerd/conf/config.toml")
47 cmd.Stdout = s.Log
48 cmd.Stderr = s.Log
49 cmd.Env = []string{"PATH=/containerd/bin", "TMPDIR=" + s.EphemeralVolume.Tmp.FullPath()}
Lorenz Brun878f5f92020-05-12 16:15:39 +020050
Serge Bazanskic2c7ad92020-07-13 17:20:09 +020051 runscFifo, err := os.OpenFile(s.EphemeralVolume.RunSCLogsFIFO.FullPath(), os.O_CREATE|os.O_RDONLY, os.ModeNamedPipe|0777)
52 if err != nil {
Lorenz Brun878f5f92020-05-12 16:15:39 +020053 return err
Lorenz Brunc88c82d2020-05-08 14:35:04 +020054 }
Serge Bazanskic2c7ad92020-07-13 17:20:09 +020055 go func() {
56 for {
57 n, err := io.Copy(s.RunscLog, runscFifo)
58 if n == 0 && err == nil {
59 // Hack because pipes/FIFOs can return zero reads when nobody is writing. To avoid busy-looping,
60 // sleep a bit before retrying. This does not loose data since the FIFO internal buffer will
61 // stall writes when it becomes full. 10ms maximum stall in a non-latency critical process (reading
62 // debug logs) is not an issue for us.
63 time.Sleep(10 * time.Millisecond)
64 } else if err != nil {
65 // TODO: Use supervisor.Logger() and Error() before exiting. Should never happen.
66 fmt.Println(err)
67 return // It's likely that this will busy-loop printing errors if it encounters one, so bail
68 }
69 }
70 }()
71
72 // TODO(lorenz): Healthcheck against CRI RuntimeService.Status() and SignalHealthy
73
74 err = cmd.Run()
75 fmt.Fprintf(s.Log, "containerd stopped: %v\n", err)
76 return err
Lorenz Brunc88c82d2020-05-08 14:35:04 +020077}