-
Notifications
You must be signed in to change notification settings - Fork 63
/
runu.go
128 lines (105 loc) · 3.21 KB
/
runu.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
120
121
122
123
124
125
126
127
128
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2023, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/config"
"kraftkit.sh/internal/cli"
"kraftkit.sh/iostreams"
"kraftkit.sh/log"
"kraftkit.sh/cmd/runu/create"
"kraftkit.sh/cmd/runu/delete"
"kraftkit.sh/cmd/runu/kill"
"kraftkit.sh/cmd/runu/ps"
"kraftkit.sh/cmd/runu/start"
"kraftkit.sh/cmd/runu/state"
)
// [OCI runtime] for unikernels. Implements the runc [command-line interface].
//
// [OCI runtime]: https://github.com/opencontainers/runtime-spec/blob/v1.1.0/runtime.md
// [command-line interface]: https://github.com/opencontainers/runtime-tools/blob/v0.9.0/docs/command-line-interface.md
type Runu struct {
Root string `long:"root" usage:"Root directory for storage of unikernel state" default:"/run/runu"`
Log string `long:"log" usage:"Set the log file path where internal debug information is written" default:"/run/runu/runu.log"`
LogFormat string `long:"log-format" usage:"set the format used by logs" default:"text"`
SystemdCgroup bool `long:"systemd-cgroup" usage:"enable systemd cgroup support"`
}
func New() *cobra.Command {
cmd, err := cmdfactory.New(&Runu{}, cobra.Command{
Short: "Run OCI-compatible unikernels",
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
HiddenDefaultCmd: true,
},
})
if err != nil {
panic(err)
}
cmd.AddCommand(state.New())
cmd.AddCommand(create.New())
cmd.AddCommand(start.New())
cmd.AddCommand(kill.New())
cmd.AddCommand(delete.New())
cmd.AddCommand(ps.New())
return cmd
}
func (opts *Runu) PersistentPre(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
if opts.Root == "" {
return fmt.Errorf("--root cannot be empty")
}
if opts.Log != "" {
f, err := os.OpenFile(opts.Log, os.O_CREATE|os.O_WRONLY|os.O_APPEND|os.O_SYNC, 0o644)
if err != nil {
return err
}
log.G(ctx).SetOutput(f)
}
if opts.LogFormat == "json" {
log.G(ctx).SetFormatter(new(logrus.JSONFormatter))
}
return nil
}
func (*Runu) Run(cmd *cobra.Command, args []string) error {
return cmd.Help()
}
func main() {
cmd := New()
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
copts := &cli.CliOptions{}
for _, o := range []cli.CliOption{
cli.WithDefaultConfigManager(cmd),
cli.WithDefaultIOStreams(),
cli.WithDefaultPluginManager(),
cli.WithDefaultLogger(),
cli.WithDefaultHTTPClient(),
} {
if err := o(copts); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// Set up the config manager in the context if it is available
if copts.ConfigManager != nil {
ctx = config.WithConfigManager(ctx, copts.ConfigManager)
}
// Set up the logger in the context if it is available
if copts.Logger != nil {
ctx = log.WithLogger(ctx, copts.Logger)
}
// Set up the iostreams in the context if it is available
if copts.IOStreams != nil {
ctx = iostreams.WithIOStreams(ctx, copts.IOStreams)
}
cmdfactory.Main(ctx, cmd)
}