/
commands.go
165 lines (144 loc) · 4.55 KB
/
commands.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
//command line interface
package main
import (
"github.com/codegangsta/cli"
"github.com/eeayiaia/scmt/daemon"
"github.com/eeayiaia/scmt/invoker"
log "github.com/Sirupsen/logrus"
"bytes"
"fmt"
"strings"
)
/* add plugin, remove plugin, enable/disable plugin, list nodes (status), list plugins (status), status of
specific node, ..?
*/
type ActionFunction func(*cli.Context)
var commands []cli.Command = []cli.Command{
{
Name: "install-plugin",
Aliases: []string{""},
Usage: "scmt install-plugin <plugin name>",
UsageText: "Installs the plugin listed in plugins.d",
Description: "",
ArgsUsage: "<plugin name> as first argument followed by nodes to install on, if no nodes are listed the plugin is installed on every node",
Category: "Plugin Control",
Action: func(c *cli.Context) { installPlugin(c) },
},
{
Name: "uninstall-plugin",
Aliases: []string{""},
Usage: "scmt uninstall-plugin <plugin name>",
UsageText: "Uninstalls the plugin listed in plugins.d",
Description: "",
ArgsUsage: "<plugin name> as first argument followed by nodes to uninstall from, if no nodes are listed the plugin is uninstalled on every node",
Category: "Plugin Control",
Action: func(c *cli.Context) { uninstallPlugin(c) },
},
{
Name: "node-status",
Aliases: []string{""},
Usage: "scmt node-status <node name | node ip>",
UsageText: "Presents status information on node",
Description: "",
ArgsUsage: "A list of <node name | node ip> , if left blank, status on all nodes are presented",
Category: "Cluster information",
Action: func(c *cli.Context) { printNodeInfo(c) },
},
{
Name: "register-device",
Aliases: []string{""},
Usage: "scmt register-device <node mac> <node ip>",
UsageText: "",
Description: "",
ArgsUsage: "Is used to add and register a new device",
Category: "Cluster information",
Action: registerDevice,
},
{
Name: "stop-daemon",
Aliases: []string{""},
Usage: "scmt stop-daemon",
UsageText: "Stops the daemon if it is running in the background",
Description: "",
ArgsUsage: "",
Category: "Daemon Control",
Action: stopDaemon,
},
{
Name: "start-daemon",
Aliases: []string{""},
Usage: "scmt start-daemon",
UsageText: "Starts the daemon if it is not running in the background",
Description: "",
ArgsUsage: "",
Category: "Daemon Control",
Action: startDaemon,
},
{
Name: "remove-device",
Aliases: []string{""},
Usage: "scmt remove-device <node mac>",
UsageText: "",
Description: "",
ArgsUsage: "Is used to remove a device",
Category: "Cluster information",
Action: removeDevice,
},
}
func getCommands() []cli.Command {
return commands
}
func AddCommandShort(name string, af ActionFunction) {
AddCommand(name, []string{""}, "No usage set", "No usage text set", "No args usage description", "", af)
}
func AddCommand(name string, aliases []string, usage string, usageText string, argsUsage string, cat string, af ActionFunction) {
commands = append(commands, cli.Command{
Name: name,
Aliases: aliases,
Usage: usage,
UsageText: usageText,
ArgsUsage: argsUsage,
Category: cat,
Action: af,
})
}
func installPlugin(c *cli.Context) {
//TODO: handle installation of plugins on specific nodes only, examine
buffer := bytes.NewBufferString(c.Args().First())
buffer.WriteString(" ")
invoker.SendPacket(invoker.TYPE_INSTALL_PLUGIN, *buffer)
}
func uninstallPlugin(c *cli.Context) {
fmt.Println("uninstalling plugin " + c.Args().First() + " :Not implemented")
}
func printNodeInfo(c *cli.Context) {
fmt.Println("Not implemented: print node info")
}
func stopDaemon(c *cli.Context) {
if !daemon.IsDaemonized() {
log.Error("No daemon is running!")
return
}
invoker.SendPacket(invoker.TYPE_STOP_DAEMON, *bytes.NewBufferString(""))
log.Info("Stopping the daemon ..")
}
func startDaemon(c *cli.Context) {
if daemon.IsDaemonized() {
log.Error("Daemon is already running!")
return
}
daemon.Daemonize(background, termHandler)
}
func registerDevice(c *cli.Context) {
// TODO: add validation
mac := strings.Trim(c.Args().First(), " ")
ip := strings.Trim(c.Args().Get(1), " ")
buffer := bytes.NewBufferString(mac + " " + ip)
invoker.SendPacket(invoker.TYPE_NEW_DEVICE, *buffer)
}
func removeDevice(c *cli.Context) {
// TODO: add validation
mac := strings.Trim(c.Args().First(), " ")
buffer := bytes.NewBufferString(mac)
invoker.SendPacket(invoker.TYPE_REMOVE_DEVICE, *buffer)
}