-
Notifications
You must be signed in to change notification settings - Fork 0
/
historycmd.go
163 lines (149 loc) · 4.18 KB
/
historycmd.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
package commands
import (
"fmt"
"github.com/codegangsta/cli"
pb "github.com/cpssd/paranoid/proto/raft"
"github.com/cpssd/paranoid/raft"
"io/ioutil"
"log"
"os"
"os/exec"
"os/user"
"path"
"strconv"
)
func History(c *cli.Context) {
args := c.Args()
if len(args) < 1 {
cli.ShowCommandHelp(c, "history")
os.Exit(1)
}
usr, err := user.Current()
if err != nil {
Log.Fatal(err)
}
target := args[0]
if fileSystemExists(target) {
target = path.Join(usr.HomeDir, ".pfs", "filesystems", target, "meta", "raft", "raft_logs")
}
read(target, c)
}
// read shows the history of a log in the given directory
func read(directory string, c *cli.Context) {
filePath := path.Join(os.TempDir(), "log.pfslog")
logsToLogfile(directory, filePath, c)
defer os.Remove(filePath)
less := exec.Command("less", filePath)
less.Stdout = os.Stdout
less.Stdin = os.Stdin
less.Stderr = os.Stderr
less.Run()
}
// logsToLogFile converts the binary logs in the logDir paramenter
// to a human readable file in the given filePath paramenter.
func logsToLogfile(logDir, filePath string, c *cli.Context) {
files, err := ioutil.ReadDir(logDir)
if err != nil {
Log.Verbose("read dir:", logDir, "err:", err)
cli.ShowCommandHelp(c, "history")
os.Exit(1)
}
writeFile, err := os.Create(filePath)
if err != nil {
log.Fatalln(err)
}
defer writeFile.Close()
numRunes := len(strconv.Itoa(len(files)))
numRunesString := strconv.Itoa(numRunes)
for i := len(files) - 1; i >= 0; i-- {
file := files[i]
p, err := fileToProto(file, logDir)
if err != nil {
log.Fatalln(err)
}
writeFile.WriteString(toLine(i+1, numRunesString, p))
}
}
// toLine converts a protobuf object to a human readable string representation
// the pad parameter is the number of runes to be used for writing the logNum
// and Term in string form.
func toLine(logNum int, pad string, p *pb.LogEntry) string {
marker := fmt.Sprintf("%-"+pad+"d Term: %-"+pad+"d", logNum, p.Term)
if p.Entry.Type == pb.Entry_StateMachineCommand {
return fmt.Sprintln(marker, "Command:", commandString(p.Entry.Command))
} else if p.Entry.Type == pb.Entry_ConfigurationChange {
return fmt.Sprintln(marker, "ConfigChange:", configurationString(p.Entry.Config))
} else {
return fmt.Sprintln(marker, "Demo:", p.Entry.Demo)
}
}
func configurationString(conf *pb.Configuration) string {
typ := fmt.Sprintf("%-21s", configTypeString(conf.Type))
conf.Type = 0
return typ + fmt.Sprint(conf)
}
// commandString returns the string representation of a StateMachineCommand
func commandString(cmd *pb.StateMachineCommand) string {
typeStr := commandTypeString(cmd.Type)
cmd.Type = 0
size := len(cmd.Data)
if size > 0 {
cmd.Data = nil
return fmt.Sprint(fmt.Sprintf("%-9s", typeStr), cmd, "Data: ", bytesString(size))
}
return fmt.Sprint(fmt.Sprintf("%-9s", typeStr), cmd)
}
// bytesString returns the human readable representation of a data size=
func bytesString(bytes int) string {
if bytes < 1000 {
return fmt.Sprint(bytes, "B")
} else if bytes < 1000000 {
return fmt.Sprint(bytes/1000, "KB")
} else if bytes < 1000000000 {
return fmt.Sprint(bytes/1000000, "MB")
} else if bytes < 1000000000000 {
return fmt.Sprint(bytes/1000000000, "GB")
} else {
return fmt.Sprint(bytes/1000000000000, "TB")
}
}
// commandTypeString returns the string representation of a command log type.
func commandTypeString(ty uint32) string {
switch ty {
case raft.TYPE_WRITE:
return "Write"
case raft.TYPE_CREAT:
return "Creat"
case raft.TYPE_CHMOD:
return "Chmod"
case raft.TYPE_TRUNCATE:
return "Truncate"
case raft.TYPE_UTIMES:
return "Utimes"
case raft.TYPE_RENAME:
return "Rename"
case raft.TYPE_LINK:
return "Link"
case raft.TYPE_SYMLINK:
return "Symlink"
case raft.TYPE_UNLINK:
return "Unlink"
case raft.TYPE_MKDIR:
return "Mkdir"
case raft.TYPE_RMDIR:
return "Rmdir"
default:
return "Unknown"
}
}
// configTypeString returns the string representation of a configuration change log type.
func configTypeString(ty pb.Configuration_ConfigurationType) string {
switch ty {
case pb.Configuration_CurrentConfiguration:
return "Current Configuration"
case pb.Configuration_FutureConfiguration:
return "Future Configuration"
default:
return "Unknown"
}
}