-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
109 lines (98 loc) · 2.65 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
package main
import (
"context"
"fmt"
"os"
"regexp"
"runtime"
"github.com/exfly/container/config"
"github.com/exfly/container/container"
"github.com/exfly/container/image"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
flag "github.com/spf13/pflag"
)
func main() {
log.SetReportCaller(true)
log.SetLevel(log.TraceLevel)
var nameRegex = regexp.MustCompile(fmt.Sprintf(`.*%v/`, "container"))
log.SetReportCaller(true)
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
return nameRegex.ReplaceAllString(f.Function, ""), fmt.Sprintf("%s:%d", nameRegex.ReplaceAllString(f.File, ""), f.Line)
},
})
/* We chroot and write to privileged directories. We need to be root */
if os.Geteuid() != 0 {
log.Fatal("You need root privileges to run this program.")
}
configHome := config.NewHome("/home/vagrant/containerd")
configHome.InitDirs()
imageConfig := image.NewImageConfig(configHome)
// init bridge
imgSrv, err := image.NewImageService(configHome)
if err != nil {
panic(err)
}
containerSrv := container.NewContainerService(
configHome,
imageConfig,
imgSrv,
)
ctx := context.TODO()
ops := opts{
configHome: configHome,
imgConf: imageConfig,
imgSrv: imgSrv,
containerSrv: containerSrv,
}
switch os.Args[1] {
case "run":
fs := flag.FlagSet{}
fs.ParseErrorsWhitelist.UnknownFlags = true
if err := fs.Parse(os.Args[2:]); err != nil {
fmt.Println("Error parsing: ", err)
}
img := fs.Args()[0]
if err := runCmd(ctx, img, fs.Args()[1:], ops); err != nil {
panic(err)
}
case "images":
case "child-mode":
log.Info("child-mode")
fs := flag.FlagSet{}
if err := fs.Parse(os.Args[2:]); err != nil {
fmt.Println("Error parsing: ", err)
}
containerID := fs.Args()[0]
if err := runChildMode(ctx, containerID, fs.Args()[1:], ops); err != nil {
panic(err)
}
}
}
type opts struct {
configHome *config.Home
imgConf *image.ImageConfig
imgSrv *image.ImageService
containerSrv *container.ContainerService
}
func runChildMode(ctx context.Context, containerID string, args []string, ops opts) error {
return ops.containerSrv.RunByID(ctx, containerID, args)
}
func runCmd(ctx context.Context, rawImg string, args []string, ops opts) error {
img, err := image.NewImage(rawImg)
if err != nil {
return err
}
pulledImg, err := ops.imgSrv.GetOrPull(ctx, img)
if err != nil {
return err
}
log.Infof("imges: %v", pulledImg)
containerInstance := container.NewContainer(pulledImg, nil)
if err = ops.containerSrv.Run(ctx, containerInstance, args); err != nil {
return errors.Wrap(err, "run container error")
}
return nil
}