Commit e0b19d5d authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent c91c63be
......@@ -24,12 +24,12 @@ import (
"context"
"flag"
"fmt"
"log"
"io"
"os"
"lab.nexedi.com/kirr/neo/go/neo/server"
"lab.nexedi.com/kirr/neo/go/xcommon/xnet"
zt "lab.nexedi.com/kirr/neo/go/zodb/zodbtools"
)
const masterSummary = "run master node"
......@@ -56,14 +56,13 @@ func masterMain(argv []string) {
if *cluster == "" {
// XXX vvv -> die or log.Fatalf ?
log.Fatal(os.Stderr, "cluster name must be provided")
os.Exit(2)
zt.Fatal("cluster name must be provided")
}
argv = flags.Args()
if len(argv) < 1 {
flags.Usage()
os.Exit(2)
zt.Exit(2)
}
net := xnet.NetPlain("tcp") // TODO + TLS; not only "tcp" ?
......@@ -81,6 +80,6 @@ func masterMain(argv []string) {
err := masterSrv.Run(ctx)
if err != nil {
log.Fatal(err)
zt.Fatal(err)
}
}
......@@ -24,14 +24,14 @@ import (
"context"
"flag"
"fmt"
"log"
"io"
"os"
"strings"
"lab.nexedi.com/kirr/neo/go/neo/server"
"lab.nexedi.com/kirr/neo/go/zodb/storage/fs1"
"lab.nexedi.com/kirr/neo/go/xcommon/xnet"
"lab.nexedi.com/kirr/neo/go/zodb/storage/fs1"
zt "lab.nexedi.com/kirr/neo/go/zodb/zodbtools"
)
const storageSummary = "run storage node"
......@@ -54,19 +54,18 @@ func storageMain(argv []string) {
flags.Parse(argv[1:])
if *cluster == "" {
// XXX vvv -> die or log.Fatalf ?
log.Fatal(os.Stderr, "cluster name must be provided")
os.Exit(2)
// XXX vvv -> die or Fatalf ?
zt.Fatal(os.Stderr, "cluster name must be provided")
}
masterv := strings.Split(*masters, ",")
if len(masterv) == 0 {
fmt.Fprintf(os.Stderr, "master list must be provided")
os.Exit(2)
zt.Exit(2)
}
if len(masterv) > 1 {
fmt.Fprintf(os.Stderr, "BUG neo/go POC currently supports only 1 master")
os.Exit(2)
zt.Exit(2)
}
master := masterv[0]
......@@ -74,13 +73,13 @@ func storageMain(argv []string) {
argv = flags.Args()
if len(argv) < 1 {
flags.Usage()
os.Exit(2)
zt.Exit(2)
}
// XXX hack to use existing zodb storage for data
zstor, err := fs1.Open(context.Background(), argv[0]) // XXX context.Background -> ?
if err != nil {
log.Fatal(err)
zt.Fatal(err)
}
net := xnet.NetPlain("tcp") // TODO + TLS; not only "tcp" ?
......@@ -98,6 +97,6 @@ func storageMain(argv []string) {
err = storSrv.Run(ctx)
if err != nil {
log.Fatal(err)
zt.Fatal(err)
}
}
......@@ -25,11 +25,11 @@ import (
"flag"
"fmt"
"io"
"log"
"os"
"lab.nexedi.com/kirr/neo/go/zodb"
"lab.nexedi.com/kirr/neo/go/zodb/storage/fs1"
zt "lab.nexedi.com/kirr/neo/go/zodb/zodbtools"
"lab.nexedi.com/kirr/go123/xbytes"
"lab.nexedi.com/kirr/go123/xerr"
......@@ -313,7 +313,7 @@ func dumpMain(argv []string) {
argv = flags.Args()
if len(argv) < 1 {
flags.Usage()
os.Exit(2)
zt.Exit(2)
}
storPath := argv[0]
......@@ -326,7 +326,7 @@ func dumpMain(argv []string) {
err := Dump(os.Stdout, storPath, fs1.IterForward, d)
if err != nil {
log.Fatal(err)
zt.Fatal(err)
}
}
......@@ -411,12 +411,12 @@ func tailMain(argv []string) {
argv = flags.Args()
if len(argv) < 1 {
flags.Usage()
os.Exit(2)
zt.Exit(2)
}
storPath := argv[0]
err := Dump(os.Stdout, storPath, fs1.IterBackward, &DumperFsTail{Ntxn: ntxn})
if err != nil {
log.Fatal(err)
zt.Fatal(err)
}
}
......@@ -25,9 +25,9 @@ import (
"fmt"
"io"
"os"
"log"
"lab.nexedi.com/kirr/neo/go/zodb/storage/fs1"
zt "lab.nexedi.com/kirr/neo/go/zodb/zodbtools"
)
// Reindex rebuilds index for FileStorage file @ path
......@@ -69,13 +69,13 @@ func reindexMain(argv []string) {
argv = flags.Args()
if len(argv) < 1 {
flags.Usage()
os.Exit(2)
zt.Exit(2)
}
storPath := argv[0]
err := Reindex(context.Background(), storPath)
if err != nil {
log.Fatal(err)
zt.Fatal(err)
}
}
......@@ -122,12 +122,12 @@ func verifyIdxMain(argv []string) {
argv = flags.Args()
if len(argv) < 1 {
flags.Usage()
os.Exit(2)
zt.Exit(2)
}
storPath := argv[0]
err := VerifyIndexFor(context.Background(), storPath)
if err != nil {
log.Fatal(err)
zt.Fatal(err)
}
}
......@@ -25,7 +25,6 @@ import (
"flag"
"fmt"
"io"
"log"
"os"
"lab.nexedi.com/kirr/neo/go/zodb"
......@@ -97,31 +96,31 @@ func catobjMain(argv []string) {
argv = flags.Args()
if len(argv) < 2 {
flags.Usage()
os.Exit(2)
Exit(2)
}
storUrl := argv[0]
if hashOnly && raw {
log.Fatal("-hashonly & -raw are incompatible")
Fatal("-hashonly & -raw are incompatible")
}
xidv := []zodb.Xid{}
for _, arg := range argv[1:] {
xid, err := zodb.ParseXid(arg)
if err != nil {
log.Fatal(err) // XXX recheck
Fatal(err) // XXX recheck
}
xidv = append(xidv, xid)
}
if raw && len(xidv) > 1 {
log.Fatal("only 1 object allowed with -raw")
Fatal("only 1 object allowed with -raw")
}
stor, err := zodb.OpenStorageURL(context.Background(), storUrl) // TODO read-only
if err != nil {
log.Fatal(err)
Fatal(err)
}
// TODO defer stor.Close()
......@@ -136,7 +135,7 @@ func catobjMain(argv []string) {
for _, xid := range xidv {
err = catobj(xid)
if err != nil {
log.Fatal(err)
Fatal(err)
}
}
}
......@@ -81,8 +81,41 @@ type MainProg struct {
HelpTopics HelpRegistry // provided help topics
}
// Exit is like os.Exit but makes sure deferred functions are run.
// Exit should be called from main goroutine.
func Exit(code int) {
panic(&programExit{code})
}
// Fatal is like log.Fatal but makes sure deferred functions are run.
// Fatal should be called from main goroutine.
func Fatal(v ...interface{}) {
log.Print(v...)
Exit(1)
}
// programExit is thrown when Exit or Fatal are called
type programExit struct {
code int
}
// Main is the main entry point for the program. Call it from main.
func (prog *MainProg) Main() {
// handle exit throw-requests
defer func() {
r := recover()
if e, _ := r.(*programExit); e != nil {
os.Exit(e.code)
}
if r != nil {
panic(r)
}
}()
prog.main()
}
func (prog *MainProg) main() {
flag.Usage = prog.usage
cpuprofile := flag.String("cpuprofile", "", "write cpu profile to `file`")
memprofile := flag.String("memprofile", "", "write memory profile to `file`")
......@@ -91,7 +124,7 @@ func (prog *MainProg) Main() {
if len(argv) == 0 {
prog.usage()
os.Exit(2)
Exit(2)
}
command := argv[0]
......@@ -100,10 +133,10 @@ func (prog *MainProg) Main() {
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal("could not create CPU profile: ", err)
Fatal("could not create CPU profile: ", err)
}
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
}
......@@ -112,11 +145,11 @@ func (prog *MainProg) Main() {
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal("could not create memory profile: ", err)
Fatal("could not create memory profile: ", err)
}
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err)
Fatal("could not write memory profile: ", err)
}
f.Close()
}
......@@ -134,7 +167,7 @@ func (prog *MainProg) Main() {
if cmd == nil {
fmt.Fprintf(os.Stderr, "%s: unknown subcommand \"%s\"\n", prog.Name, command)
fmt.Fprintf(os.Stderr, "Run '%s help' for usage.\n", prog.Name)
os.Exit(2)
Exit(2)
}
cmd.Main(argv)
......@@ -201,7 +234,7 @@ Use "%s help [topic]" for more information about that topic.
func (prog *MainProg) help(argv []string) {
if len(argv) < 2 { // help topic ...
prog.usage()
os.Exit(2)
Exit(2)
}
topic := argv[1]
......@@ -210,17 +243,17 @@ func (prog *MainProg) help(argv []string) {
command := prog.Commands.Lookup(topic)
if command != nil {
command.Usage(os.Stdout)
os.Exit(0)
Exit(0)
}
helpTopic := prog.helpTopics().Lookup(topic)
if helpTopic != nil {
fmt.Println(helpTopic.Text)
os.Exit(0)
Exit(0)
}
fmt.Fprintf(os.Stderr, "Unknown help topic `%s`. Run '%s help'.\n", topic, prog.Name)
os.Exit(2)
Exit(2)
}
// helpTopics returns provided help topics augmented with help on common topics
......
......@@ -44,7 +44,6 @@ import (
"flag"
"fmt"
"io"
"log"
"os"
"lab.nexedi.com/kirr/go123/xfmt"
......@@ -235,7 +234,7 @@ func dumpMain(argv []string) {
argv = flags.Args()
if len(argv) < 1 {
flags.Usage()
os.Exit(2)
Exit(2)
}
storUrl := argv[0]
......@@ -245,17 +244,17 @@ func dumpMain(argv []string) {
tidMin, tidMax, err := zodb.ParseTidRange(tidRange)
if err != nil {
log.Fatal(err) // XXX recheck
Fatal(err) // XXX recheck
}
stor, err := zodb.OpenStorageURL(context.Background(), storUrl) // TODO read-only
if err != nil {
log.Fatal(err)
Fatal(err)
}
// TODO defer stor.Close()
err = Dump(os.Stdout, stor, tidMin, tidMax, hashOnly)
if err != nil {
log.Fatal(err)
Fatal(err)
}
}
......@@ -26,7 +26,6 @@ import (
"flag"
"fmt"
"io"
"log"
"os"
"lab.nexedi.com/kirr/neo/go/zodb"
......@@ -112,17 +111,17 @@ func infoMain(argv []string) {
argv = flags.Args()
if len(argv) < 1 {
flags.Usage()
os.Exit(2)
Exit(2)
}
storUrl := argv[0]
stor, err := zodb.OpenStorageURL(context.Background(), storUrl) // TODO read-only
if err != nil {
log.Fatal(err)
Fatal(err)
}
err = Info(os.Stdout, stor, argv[1:])
if err != nil {
log.Fatal(err)
Fatal(err)
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment