/
main.go
216 lines (188 loc) · 7.73 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
// simplectl is a debugging/administration utility. It allows access to most of
// the backend functionality of simple from the command line.
package main
import (
"os"
"fmt"
"gopkg.in/alecthomas/kingpin.v2"
"github.com/wrouesnel/go.log"
"github.com/wrouesnel/docker-simple-disk/volumequery"
//"github.com/wrouesnel/docker-simple-disk/volumelabel"
"github.com/wrouesnel/docker-simple-disk/config"
"github.com/coreos/go-systemd/util"
"github.com/Songmu/prompter"
"github.com/wrouesnel/docker-simple-disk/volumesetup"
"encoding/json"
"io/ioutil"
"sort"
)
type dumpDeviceRulesCmd struct {
targetDevice string
}
type listDevicePartitionsCmd struct {
targetDevice string
}
type forceInitCmd struct {
targetDevice string
inputQueryString volumequery.VolumeQuery
force bool
hostname string
machineid string
}
type checkVolumeQueryCmd struct {
targetDevice string
inputQueryString volumequery.VolumeQuery
}
// Get the hostname
func hostname() string {
h, err := os.Hostname()
if err != nil {
return ""
}
return h
}
// Get the machine ID
func machineid() string {
m, err := util.GetMachineID()
if err != nil {
return ""
}
return m
}
func main() {
app := kingpin.New("simplectl", "Test utility which runs simple volume queries against udev")
cmdlineSelectionRule := volumequery.NewDeviceSelectionRule()
config.DefaultAppFlags(app, &cmdlineSelectionRule)
rawQueryFromStdin := app.Command("raw-udev-query", "Read a JSON selection rules from stdin and run a udev query")
dumpDeviceRules := app.Command("dump-device-rules", "JSON print the parameter of a device as selection rules")
dumpDeviceRulesCmd := dumpDeviceRulesCmd{}
dumpDeviceRules.Arg("disk path", "disk or device to reverse engineer selection rules for").StringVar(&dumpDeviceRulesCmd.targetDevice)
listDevicePartitions := app.Command("list-partitions", "list all partitions from a device")
listDevicePartitionsCmd := listDevicePartitionsCmd{}
listDevicePartitions.Arg("disk path", "disk or device to reverse engineer selection rules for").StringVar(&listDevicePartitionsCmd.targetDevice)
listRawCandidates := app.Command("list-raw-candidates", "list devices which form the initial pool")
listInitializedCandidates := app.Command("list-initialized-candidates", "list initialized simple devices")
listUninitializedCandidates := app.Command("list-uninitialized-candidates", "list uninitialized devices simple would initialize on request")
listRejectedCandidates := app.Command("list-rejected-candidates", "list candidate devices which were rejected for one reason or another")
forceInitDisk := app.Command("initialize-disk", "manually write an initialization value to a given block device")
forceInitCmdData := forceInitCmd{}
forceInitDisk.Flag("force", "don't prompt for confirmation").BoolVar(&forceInitCmdData.force)
forceInitDisk.Flag("hostname", "override hostname for disk").Default(hostname()).StringVar(&forceInitCmdData.hostname)
forceInitDisk.Flag("machine-id", "override machine-id for disk").Default(machineid()).StringVar(&forceInitCmdData.machineid)
forceInitDisk.Arg("block device","block device to partition and initialize").StringVar(&forceInitCmdData.targetDevice)
volumequery.VolumeQueryVar(forceInitDisk.Arg("initializing query string", "query string used to initialize the device"), &forceInitCmdData.inputQueryString)
checkVolumeQuery := app.Command("query-device", "run a given query string against a given device")
checkVolumeQueryCmdData := checkVolumeQueryCmd{}
checkVolumeQuery.Arg("block device","block device to partition and initialize").StringVar(&checkVolumeQueryCmdData.targetDevice)
volumequery.VolumeQueryVar(checkVolumeQuery.Arg("query string", "query string used to initialize the device"), &checkVolumeQueryCmdData.inputQueryString)
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
case rawQueryFromStdin.FullCommand():
// Run GetCandidateDisk but read from a JSON query.
jsonBytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalln("Error reading JSON from stdin.")
}
jsonRules := []volumequery.DeviceSelectionRule{}
if err := json.Unmarshal(jsonBytes, &jsonRules); err != nil {
log.Fatalln("Error unmarshalling query from JSON:", err)
}
devices, err := volumequery.GetDevicePaths(jsonRules)
if err != nil {
log.Fatalln(err)
}
for _, d := range devices {
fmt.Println(d)
}
case dumpDeviceRules.FullCommand():
rule, err := volumequery.GetFullSelectionRuleForDevice(dumpDeviceRulesCmd.targetDevice)
if err != nil {
log.Fatalln("Failed to query device:", err)
}
rules := []*volumequery.DeviceSelectionRule{rule}
b, err := json.MarshalIndent(rules,""," ")
if err != nil {
log.Fatalln("JSON marshalling failed:", err)
}
os.Stdout.Write(b)
os.Stdout.Write([]byte{'\n'})
case listRawCandidates.FullCommand():
devices, err := volumequery.GetDevicePaths([]volumequery.DeviceSelectionRule{cmdlineSelectionRule})
if err != nil {
log.Fatalln(err)
}
fmt.Fprintln(os.Stderr, "Listing ALL possible candidate devices (not filtered for usage)")
for _, d := range devices {
fmt.Println(d)
}
case listDevicePartitions.FullCommand():
devices, err := volumequery.GetPartitionDevicesFromDiskPath(listDevicePartitionsCmd.targetDevice)
if err != nil {
log.Fatalln("Failed to query device:", err)
}
fmt.Fprintln(os.Stderr, "Listing partitions of device")
devPaths := make([]string, 0, len(devices))
for d, _ := range devices {
devPaths = append(devPaths, d)
}
sort.Strings(devPaths)
for _, d := range devPaths {
fmt.Println(d)
}
case listUninitializedCandidates.FullCommand():
_, uninitialized, _, err := volumequery.GetCandidateDisks([]volumequery.DeviceSelectionRule{cmdlineSelectionRule})
if err != nil {
log.Fatalln("Failed while querying candidates:", err)
}
fmt.Fprintln(os.Stderr, "Listing uninitialized candidate devices for simple")
for _, d := range uninitialized {
fmt.Println(d)
}
case listInitializedCandidates.FullCommand():
initialized, _, _, err := volumequery.GetCandidateDisks([]volumequery.DeviceSelectionRule{cmdlineSelectionRule})
if err != nil {
log.Fatalln("Failed while querying candidates:", err)
}
fmt.Fprintln(os.Stderr, "Listing initialized candidate devices for simple")
for _, d := range initialized {
fmt.Println(d)
}
case listRejectedCandidates.FullCommand():
_, _, rejected, err := volumequery.GetCandidateDisks([]volumequery.DeviceSelectionRule{cmdlineSelectionRule})
if err != nil {
log.Fatalln("Failed while querying candidates:", err)
}
fmt.Fprintln(os.Stderr, "Listing rejected candidate devices for simple")
for _, d := range rejected {
fmt.Println(d)
}
case forceInitDisk.FullCommand():
if !forceInitCmdData.force == false {
if proceed := prompter.YesNo("Force initializing the given device. Are you sure?", false); !proceed {
log.Fatalln("Cancelled by user.")
}
}
log.Infoln("Forcibly initializing device:", forceInitCmdData.targetDevice)
err := volumesetup.InitializeBlockDevice(
forceInitCmdData.targetDevice,
forceInitCmdData.inputQueryString,
forceInitCmdData.hostname,
forceInitCmdData.machineid,
)
if err != nil {
log.Fatalln("Failed while setting up device:", err)
}
case checkVolumeQuery.FullCommand():
fmt.Fprintln(os.Stderr, "Checking query against device:", checkVolumeQueryCmdData.targetDevice)
labelPath, dataPath, err := volumequery.GetDiskLabelAndVolumePath(checkVolumeQueryCmdData.targetDevice)
if err != nil {
log.Fatalln("Not an initialized or locateable device:", err)
}
if matches, err := volumequery.VolumeQueryMatch(&checkVolumeQueryCmdData.inputQueryString, labelPath, dataPath); err != nil {
log.Fatalln("Error while trying to run matcher:", err)
} else if matches {
fmt.Fprintln(os.Stdout, "Match")
} else {
fmt.Fprintln(os.Stdout, "No Match")
}
}
}