Lorenz Brun | c88c82d | 2020-05-08 14:35:04 +0200 | [diff] [blame] | 1 | // 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 | |
| 17 | package containerd |
| 18 | |
| 19 | import ( |
| 20 | "context" |
Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 21 | "fmt" |
Lorenz Brun | 6acfc32 | 2020-05-13 17:01:26 +0200 | [diff] [blame] | 22 | "io" |
Lorenz Brun | c88c82d | 2020-05-08 14:35:04 +0200 | [diff] [blame] | 23 | "os" |
| 24 | "os/exec" |
Lorenz Brun | 6acfc32 | 2020-05-13 17:01:26 +0200 | [diff] [blame] | 25 | "time" |
| 26 | |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 27 | "git.monogon.dev/source/nexantic.git/core/internal/localstorage" |
Lorenz Brun | c88c82d | 2020-05-08 14:35:04 +0200 | [diff] [blame] | 28 | "git.monogon.dev/source/nexantic.git/core/pkg/logbuffer" |
Lorenz Brun | c88c82d | 2020-05-08 14:35:04 +0200 | [diff] [blame] | 29 | ) |
| 30 | |
Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 31 | type Service struct { |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 32 | EphemeralVolume *localstorage.EphemeralContainerdDirectory |
| 33 | |
Lorenz Brun | 6acfc32 | 2020-05-13 17:01:26 +0200 | [diff] [blame] | 34 | Log *logbuffer.LogBuffer |
| 35 | RunscLog *logbuffer.LogBuffer |
Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 36 | } |
Lorenz Brun | c88c82d | 2020-05-08 14:35:04 +0200 | [diff] [blame] | 37 | |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 38 | func (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 Brun | c88c82d | 2020-05-08 14:35:04 +0200 | [diff] [blame] | 45 | |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 46 | 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 Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 50 | |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 51 | runscFifo, err := os.OpenFile(s.EphemeralVolume.RunSCLogsFIFO.FullPath(), os.O_CREATE|os.O_RDONLY, os.ModeNamedPipe|0777) |
| 52 | if err != nil { |
Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 53 | return err |
Lorenz Brun | c88c82d | 2020-05-08 14:35:04 +0200 | [diff] [blame] | 54 | } |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 55 | 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 Brun | c88c82d | 2020-05-08 14:35:04 +0200 | [diff] [blame] | 77 | } |