-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
97 lines (78 loc) · 2.45 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
package main
import "flag"
import (
"fmt"
"os"
"net/http"
log "github.com/Sirupsen/logrus"
)
type deviceFilesFlag []string
var attachArg bool
var configFile string
var detachArg bool
var deviceFiles deviceFilesFlag
var hostname string
var serverMode bool
var vmName string
const (
configFileDescription = "Path to config file"
serverModeDescription = "Run as a server"
)
func (deviceFiles *deviceFilesFlag) String() string {
return fmt.Sprint(*deviceFiles)
}
func (deviceFiles *deviceFilesFlag) Set(deviceFile string) error {
*deviceFiles = append(*deviceFiles, deviceFile)
return nil
}
func init() {
log.SetLevel(log.DebugLevel)
flag.BoolVar(&serverMode, "s", false, serverModeDescription)
flag.BoolVar(&serverMode, "server", false, serverModeDescription)
flag.StringVar(&configFile, "c", "", configFileDescription)
flag.StringVar(&configFile, "config", "", configFileDescription)
flag.Var(&deviceFiles, "f", "Path to device file")
flag.Var(&deviceFiles, "device-file", "Path to device file")
flag.StringVar(&vmName, "n", "", "Name of the VM to manage")
flag.StringVar(&vmName, "name", "", "Name of the VM to manage")
flag.StringVar(&hostname, "h", "127.0.0.1:7654", "Hostname of the service to connect to")
flag.StringVar(&hostname, "hostname", "127.0.0.1:7654", "Hostname of the service to connect to")
flag.BoolVar(&attachArg, "a", false, "Attach the devices")
flag.BoolVar(&attachArg, "attach", false, "Attach the devices")
flag.BoolVar(&detachArg, "d", false, "Detach the devices")
flag.BoolVar(&detachArg, "detach", false, "Detach the devices")
}
func main() {
flag.Parse()
// Two run methods:
// 1) As server daemon that listens for commands via REST api
// 2) As client that sends command based on CLI args
if serverMode {
// If config file is not empty, use that instead of CLI arguments
if configFile != "" {
config, err := parseConfig(configFile)
if err != nil {
log.Panic(err)
}
vmName = config.Name
deviceFiles = config.DeviceFiles
}
if vmName == "" {
log.Error("VM name must be provided when starting in server mode")
os.Exit(1)
}
log.Info("Starting in server mode...")
startServer()
} else {
if !attachArg && !detachArg {
log.Error("Must specify whether to attach or detach the devices")
os.Exit(1)
}
if attachArg {
http.Get(fmt.Sprintf("http://%s/virsh-device-daemon/attach", hostname))
}
if detachArg {
http.Get(fmt.Sprintf("http://%s/virsh-device-daemon/detach", hostname))
}
}
}