-
Notifications
You must be signed in to change notification settings - Fork 925
/
main.go
229 lines (180 loc) · 6.43 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"bufio"
"fmt"
"os"
"github.com/canonical/go-dqlite"
"github.com/spf13/cobra"
"github.com/canonical/lxd/lxd/daemon"
"github.com/canonical/lxd/lxd/events"
"github.com/canonical/lxd/lxd/operations"
"github.com/canonical/lxd/lxd/response"
"github.com/canonical/lxd/lxd/rsync"
cli "github.com/canonical/lxd/shared/cmd"
"github.com/canonical/lxd/shared/logger"
"github.com/canonical/lxd/shared/version"
)
type cmdGlobal struct {
cmd *cobra.Command
asker cli.Asker
flagHelp bool
flagVersion bool
flagLogFile string
flagLogDebug bool
flagLogSyslog bool
flagLogTrace []string
flagLogVerbose bool
}
func (c *cmdGlobal) Run(cmd *cobra.Command, args []string) error {
// Configure dqlite to *not* disable internal SQLite locking, since we
// use SQLite both through dqlite and through go-dqlite, potentially
// from different threads at the same time. We need to call this
// function as early as possible since this is a global setting in
// SQLite, which can't be changed afterwise.
err := dqlite.ConfigMultiThread()
if err != nil {
return err
}
// Set logging global variables
daemon.Debug = c.flagLogDebug
rsync.Debug = c.flagLogDebug
daemon.Verbose = c.flagLogVerbose
// Set debug for the operations package
operations.Init(daemon.Debug)
// Set debug for the response package
response.Init(daemon.Debug)
// Setup logger
syslog := ""
if c.flagLogSyslog {
syslog = "lxd"
}
err = logger.InitLogger(c.flagLogFile, syslog, c.flagLogVerbose, c.flagLogDebug, events.NewEventHandler())
if err != nil {
return err
}
return nil
}
// rawArgs returns the raw unprocessed arguments from os.Args after the command name arg is found.
func (c *cmdGlobal) rawArgs(cmd *cobra.Command) []string {
for i, arg := range os.Args {
if arg == cmd.Name() && len(os.Args)-1 > i {
return os.Args[i+1:]
}
}
return []string{}
}
func main() {
// daemon command (main)
daemonCmd := cmdDaemon{}
app := daemonCmd.Command()
app.SilenceUsage = true
app.CompletionOptions = cobra.CompletionOptions{DisableDefaultCmd: true}
// Workaround for main command
app.Args = cobra.ArbitraryArgs
// Global flags
globalCmd := cmdGlobal{cmd: app, asker: cli.NewAsker(bufio.NewReader(os.Stdin))}
daemonCmd.global = &globalCmd
app.PersistentPreRunE = globalCmd.Run
app.PersistentFlags().BoolVar(&globalCmd.flagVersion, "version", false, "Print version number")
app.PersistentFlags().BoolVarP(&globalCmd.flagHelp, "help", "h", false, "Print help")
app.PersistentFlags().StringVar(&globalCmd.flagLogFile, "logfile", "", "Path to the log file"+"``")
app.PersistentFlags().BoolVar(&globalCmd.flagLogSyslog, "syslog", false, "Log to syslog")
app.PersistentFlags().StringArrayVar(&globalCmd.flagLogTrace, "trace", []string{}, "Log tracing targets"+"``")
app.PersistentFlags().BoolVarP(&globalCmd.flagLogDebug, "debug", "d", false, "Show all debug messages")
app.PersistentFlags().BoolVarP(&globalCmd.flagLogVerbose, "verbose", "v", false, "Show all information messages")
// Version handling
app.SetVersionTemplate("{{.Version}}\n")
app.Version = version.Version
if version.IsLTSVersion {
app.Version = fmt.Sprintf("%s LTS", version.Version)
}
// activateifneeded sub-command
activateifneededCmd := cmdActivateifneeded{global: &globalCmd}
app.AddCommand(activateifneededCmd.Command())
// callhook sub-command
callhookCmd := cmdCallhook{global: &globalCmd}
app.AddCommand(callhookCmd.Command())
// forkconsole sub-command
forkconsoleCmd := cmdForkconsole{global: &globalCmd}
app.AddCommand(forkconsoleCmd.Command())
// forkdns sub-command
forkDNSCmd := cmdForkDNS{global: &globalCmd}
app.AddCommand(forkDNSCmd.Command())
// forkexec sub-command
forkexecCmd := cmdForkexec{global: &globalCmd}
app.AddCommand(forkexecCmd.Command())
// forkfile sub-command
forkfileCmd := cmdForkfile{global: &globalCmd}
app.AddCommand(forkfileCmd.Command())
// forklimits sub-command
forklimitsCmd := cmdForklimits{global: &globalCmd}
app.AddCommand(forklimitsCmd.Command())
// forkmigrate sub-command
forkmigrateCmd := cmdForkmigrate{global: &globalCmd}
app.AddCommand(forkmigrateCmd.Command())
// forksyscall sub-command
forksyscallCmd := cmdForksyscall{global: &globalCmd}
app.AddCommand(forksyscallCmd.Command())
// forksyscallgo sub-command
forksyscallgoCmd := cmdForksyscallgo{global: &globalCmd}
app.AddCommand(forksyscallgoCmd.Command())
// forkcoresched sub-command
forkcoreschedCmd := cmdForkcoresched{global: &globalCmd}
app.AddCommand(forkcoreschedCmd.Command())
// forkmount sub-command
forkmountCmd := cmdForkmount{global: &globalCmd}
app.AddCommand(forkmountCmd.Command())
// forknet sub-command
forknetCmd := cmdForknet{global: &globalCmd}
app.AddCommand(forknetCmd.Command())
// forkproxy sub-command
forkproxyCmd := cmdForkproxy{global: &globalCmd}
app.AddCommand(forkproxyCmd.Command())
// forkstart sub-command
forkstartCmd := cmdForkstart{global: &globalCmd}
app.AddCommand(forkstartCmd.Command())
// forkuevent sub-command
forkueventCmd := cmdForkuevent{global: &globalCmd}
app.AddCommand(forkueventCmd.Command())
// forkzfs sub-command
forkzfsCmd := cmdForkZFS{global: &globalCmd}
app.AddCommand(forkzfsCmd.Command())
// import sub-command
importCmd := cmdImport{global: &globalCmd}
app.AddCommand(importCmd.Command())
// init sub-command
initCmd := cmdInit{global: &globalCmd}
app.AddCommand(initCmd.Command())
// manpage sub-command
manpageCmd := cmdManpage{global: &globalCmd}
app.AddCommand(manpageCmd.Command())
// migratedumpsuccess sub-command
migratedumpsuccessCmd := cmdMigratedumpsuccess{global: &globalCmd}
app.AddCommand(migratedumpsuccessCmd.Command())
// netcat sub-command
netcatCmd := cmdNetcat{global: &globalCmd}
app.AddCommand(netcatCmd.Command())
// recover sub-command
recoverCmd := cmdRecover{global: &globalCmd}
app.AddCommand(recoverCmd.Command())
// shutdown sub-command
shutdownCmd := cmdShutdown{global: &globalCmd}
app.AddCommand(shutdownCmd.Command())
// sql sub-command
sqlCmd := cmdSql{global: &globalCmd}
app.AddCommand(sqlCmd.Command())
// version sub-command
versionCmd := cmdVersion{global: &globalCmd}
app.AddCommand(versionCmd.Command())
// waitready sub-command
waitreadyCmd := cmdWaitready{global: &globalCmd}
app.AddCommand(waitreadyCmd.Command())
// cluster sub-command
clusterCmd := cmdCluster{global: &globalCmd}
app.AddCommand(clusterCmd.Command())
// Run the main command and handle errors
err := app.Execute()
if err != nil {
os.Exit(1)
}
}