This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathmain.go
119 lines (96 loc) · 2.74 KB
/
main.go
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2017 HyperHQ Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"sync"
"time"
"github.com/moby/moby/pkg/term"
"github.com/sirupsen/logrus"
)
const (
shimName = "kata-shim"
exitFailure = 1
)
// version is the shim version. This variable is populated at build time.
var version = "unknown"
var shimLog = logrus.WithFields(logrus.Fields{
"name": shimName,
"pid": os.Getpid(),
})
func initLogger(logLevel string) error {
shimLog.Logger.Formatter = &logrus.TextFormatter{TimestampFormat: time.RFC3339Nano}
level, err := logrus.ParseLevel(logLevel)
if err != nil {
return err
}
logrus.SetLevel(level)
shimLog.WithField("version", version).Info()
return nil
}
func main() {
var (
logLevel string
agentAddr string
container string
execId string
proxyExitCode bool
showVersion bool
)
flag.BoolVar(&showVersion, "version", false, "display program version and exit")
flag.StringVar(&logLevel, "log", "warn", "set shim log level: debug, info, warn, error, fatal or panic")
flag.StringVar(&agentAddr, "agent", "", "agent gRPC socket endpoint")
flag.StringVar(&container, "container", "", "container id for the shim")
flag.StringVar(&execId, "exec-id", "", "process id for the shim")
flag.BoolVar(&proxyExitCode, "proxy-exit-code", true, "proxy exit code of the process")
flag.Parse()
if showVersion {
fmt.Printf("%v version %v\n", shimName, version)
os.Exit(0)
}
if agentAddr == "" || container == "" || execId == "" {
shimLog.WithField("agentAddr", agentAddr).WithField("container", container).WithField("exec-id", execId).Error("container ID, exec ID and agent socket endpoint must be set")
os.Exit(exitFailure)
}
err := initLogger(logLevel)
if err != nil {
shimLog.WithError(err).WithField("loglevel", logLevel).Error("invalid log level")
os.Exit(exitFailure)
}
shim, err := newShim(agentAddr, container, execId)
if err != nil {
shimLog.WithError(err).Error("failed to create new shim")
os.Exit(exitFailure)
}
// stdio
wg := &sync.WaitGroup{}
shim.proxyStdio(wg)
defer wg.Wait()
// winsize
s, err := term.SetRawTerminal(os.Stdin.Fd())
if err != nil {
shimLog.WithError(err).Error("failed to set raw terminal")
os.Exit(exitFailure)
}
defer term.RestoreTerminal(os.Stdin.Fd(), s)
shim.monitorTtySize(os.Stdin)
// signals
sigc := shim.forwardAllSignals()
defer signal.Stop(sigc)
// wait until exit
exitcode, err := shim.wait()
if err != nil {
shimLog.WithError(err).WithField("exec-id", execId).Error("failed waiting for process")
os.Exit(exitFailure)
} else if proxyExitCode {
shimLog.WithField("exitcode", exitcode).Info("using shim to proxy exit code")
if exitcode != 0 {
os.Exit(int(exitcode))
}
}
}