-
Notifications
You must be signed in to change notification settings - Fork 63
/
runu.go
130 lines (106 loc) · 3.37 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
129
130
// 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 runu
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/config"
"kraftkit.sh/internal/cli"
"kraftkit.sh/iostreams"
"kraftkit.sh/log"
"kraftkit.sh/internal/cli/runu/create"
"kraftkit.sh/internal/cli/runu/delete"
"kraftkit.sh/internal/cli/runu/kill"
"kraftkit.sh/internal/cli/runu/ps"
"kraftkit.sh/internal/cli/runu/start"
"kraftkit.sh/internal/cli/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 RunuOptions 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 NewCmd() *cobra.Command {
cmd, err := cmdfactory.New(&RunuOptions{}, cobra.Command{
Short: "Run OCI-compatible unikernels",
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
HiddenDefaultCmd: true,
},
})
if err != nil {
panic(err)
}
cmd.AddCommand(state.NewCmd())
cmd.AddCommand(create.NewCmd())
cmd.AddCommand(start.NewCmd())
cmd.AddCommand(kill.NewCmd())
cmd.AddCommand(delete.NewCmd())
cmd.AddCommand(ps.NewCmd())
return cmd
}
func (opts *RunuOptions) 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 (*RunuOptions) Run(_ context.Context, args []string) error {
return pflag.ErrHelp
}
func Main(args []string) int {
cmd := NewCmd()
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)
return 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)
}
return cmdfactory.Main(ctx, cmd)
}