forked from tfukushima/mm-ctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.go
142 lines (114 loc) · 3.72 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
// Copyright 2015 Midokura SARL
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"flag"
"fmt"
"os"
"github.com/mitchellh/cli"
ini "gopkg.in/ini.v1"
)
// const HostUuid = "b53b82f3-ebe5-4e8d-9088-8b3ae4ade76e"w
var hostUuid = flag.String("host_uuid", "", "The UUID of the host (required).")
// the name of the command.
const CommandName = "mm-ctl"
func loadConfig() error {
midolmanConfigPath := flag.String("config", "/etc/midolman/midolman.conf",
"The location of the Midolamn config file")
hostConfigPath := flag.String("host-config", "/etc/midolman/host_uuid.properties",
"The location of the host UUID config file")
flag.Parse()
cfg, err := ini.Load(*midolmanConfigPath, *hostConfigPath)
if err != nil {
return err
}
zkSection, err := cfg.GetSection("zookeeper")
if err != nil {
return err
}
*rootPath = zkSection.Key("root_key").String()
if *rootPath == "" {
*rootPath = zkSection.Key("midolman_root_key").String()
}
*zookeeperAddresses = zkSection.Key("zookeeper_hosts").String()
*hostUuid = cfg.Section("").Key("host_uuid").String()
return nil
}
// The "binding" command.
type Binding struct{}
func (b *Binding) Help() string {
return fmt.Sprintf(`Usage: %s bind c5951b28-0d72-41fa-9f29-650bbeae2ed3 8a1c0540-veth"
binding takes the Neutron port UUID and the interface name and binds the port to
the interface.
`, CommandName)
}
func (b *Binding) Run(args []string) int {
bindCmdFlag := flag.NewFlagSet("bind", flag.ContinueOnError)
if *hostUuid == "" {
bindCmdFlag.StringVar(hostUuid, "host_uuid", "", "The UUID of the host (required).")
}
bindCmdFlag.Parse(args)
if bindCmdFlag.NArg() != 2 {
fmt.Fprintf(os.Stderr, "bind takes exactly 2 args but %d args are given.\n", bindCmdFlag.NArg())
return 1
}
portUuid := bindCmdFlag.Arg(0)
fmt.Printf("host_uuid: %s\n", *hostUuid)
if *hostUuid == "" {
fmt.Fprintf(os.Stderr, "Host UUID is required.\n")
return 1
}
interfaceName := bindCmdFlag.Arg(1)
if err := binding(portUuid, *hostUuid, interfaceName); err != nil {
fmt.Fprintf(os.Stderr, "Error on binding the port: %s\n", err.Error())
return 1
}
return 0
}
func (b *Binding) Synopsis() string {
return "bind <port-uuid> <interface-name>"
}
// The "unbinding" command.
type Unbinding struct{}
func (u *Unbinding) Help() string {
return fmt.Sprintf(`Usage: %s unbind c5951b28-0d72-41fa-9f29-650bbeae2ed3
unbinding takes the Neutron port UUID and unbinds the port.
`, CommandName)
}
func (u *Unbinding) Run(args []string) int {
unbindCmdFlag := flag.NewFlagSet("unbind", flag.ContinueOnError)
if *hostUuid == "" {
unbindCmdFlag.StringVar(hostUuid, "host_uuid", "", "The UUID of the host (required).")
}
unbindCmdFlag.Parse(args)
if unbindCmdFlag.NArg() != 1 {
fmt.Fprintf(os.Stderr, "unbind takes exactly 1 arg but %d args are given.\n", unbindCmdFlag.NArg())
return 1
}
portUuid := unbindCmdFlag.Arg(0)
if err := unbinding(portUuid, *hostUuid); err != nil {
fmt.Fprintf(os.Stderr, "Error on unbinding the port: %s\n", err.Error())
return 1
}
return 0
}
func (u *Unbinding) Synopsis() string {
return "unbind <port-uuid>"
}
func bindingCommand() (cli.Command, error) {
return &Binding{}, nil
}
func unbindingCommand() (cli.Command, error) {
return &Unbinding{}, nil
}