-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmain.go
More file actions
102 lines (86 loc) · 2.63 KB
/
main.go
File metadata and controls
102 lines (86 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2025 Edgeless Systems GmbH
// SPDX-License-Identifier: BUSL-1.1
package main
import (
"context"
"errors"
"fmt"
"log/slog"
"net"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/containerd/ttrpc"
"github.com/edgelesssys/contrast/imagestore/internal/service"
"github.com/edgelesssys/contrast/internal/katacomponents"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
)
var version = "0.0.0-dev"
func main() {
if err := newRootCmd().Execute(); err != nil {
os.Exit(1)
}
}
func newRootCmd() *cobra.Command {
return &cobra.Command{
Use: "imagestore",
Short: "securely mount a block device",
Version: version,
SilenceUsage: true,
RunE: run,
}
}
func run(cmd *cobra.Command, _ []string) error {
log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: slog.LevelDebug,
}))
ctx, cancel := signal.NotifyContext(cmd.Context(), syscall.SIGTERM, syscall.SIGINT)
defer cancel()
fmt.Fprintf(os.Stderr, "Contrast imagestore %s\n", version)
fmt.Fprintln(os.Stderr, "Report issues at https://github.com/edgelesssys/contrast/issues")
if err := os.MkdirAll(filepath.Dir(katacomponents.SecuremountSocket), os.ModePerm); err != nil {
return fmt.Errorf("creating directory for socket: %w", err)
}
if err := os.Remove(katacomponents.SecuremountSocket); err != nil && !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("removing existing socket: %w", err)
}
l, err := (&net.ListenConfig{}).Listen(ctx, "unix", katacomponents.SecuremountSocket)
if err != nil {
return fmt.Errorf("listening on socket: %w", err)
}
defer l.Close()
defer os.RemoveAll(katacomponents.SecuremountSocket)
s, err := ttrpc.NewServer()
if err != nil {
return fmt.Errorf("creating ttRPC server: %w", err)
}
defer s.Close()
katacomponents.RegisterSecureMountServiceService(s, &service.SecureImageStoreService{Logger: log})
eg, ctxEg := errgroup.WithContext(ctx)
eg.Go(func() error {
log.Info("Started imagestore", "socket", katacomponents.SecuremountSocket)
log.Info("Waiting for imagestore request...")
if err := s.Serve(ctxEg, l); err != nil {
return fmt.Errorf("starting the ttRPC server: %w", err)
}
return nil
})
eg.Go(func() error {
<-ctxEg.Done()
if ctx.Err() != nil {
log.Info("Received signal, shutting down.")
} else {
log.Info("Unexpected shutdown", "err", ctxEg.Err())
}
ctxCleanup, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
if err := s.Shutdown(ctxCleanup); err != nil { //nolint:contextcheck
return fmt.Errorf("shutting down the ttRPC server: %w", err)
}
return nil
})
return eg.Wait()
}