diff --git a/cmd/castle/block-create.go b/cmd/castle/block-create.go index 6aba08b0f..36cde5732 100644 --- a/cmd/castle/block-create.go +++ b/cmd/castle/block-create.go @@ -3,6 +3,7 @@ package castle import ( "fmt" "net/http" + "os" "github.com/quantum/castle/pkg/castle/client" "github.com/quantum/castle/pkg/model" @@ -43,7 +44,8 @@ func createBlockImagesEntry(cmd *cobra.Command, args []string) error { c := client.NewCastleNetworkRestClient(client.GetRestURL(apiServerEndpoint), http.DefaultClient) out, err := createBlockImage(newImageName, newImagePoolName, newImageSize, c) if err != nil { - return err + fmt.Fprintln(os.Stderr, err) + os.Exit(1) } fmt.Println(out) diff --git a/cmd/castle/block-list.go b/cmd/castle/block-list.go index 5e8cd736a..08487975c 100644 --- a/cmd/castle/block-list.go +++ b/cmd/castle/block-list.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "net/http" + "os" "strings" "github.com/quantum/castle/pkg/castle/client" @@ -32,7 +33,8 @@ func listBlocksEntry(cmd *cobra.Command, args []string) error { e := &exec.CommandExecutor{} out, err := listBlocks(rbdSysBusPathDefault, c, e) if err != nil { - return err + fmt.Fprintln(os.Stderr, err) + os.Exit(1) } fmt.Println(out) diff --git a/cmd/castle/block-mount.go b/cmd/castle/block-mount.go index 353c6dfbe..e90dc1e99 100644 --- a/cmd/castle/block-mount.go +++ b/cmd/castle/block-mount.go @@ -62,7 +62,8 @@ func mountBlockEntry(cmd *cobra.Command, args []string) error { e := &exec.CommandExecutor{} out, err := mountBlock(mountImageName, mountImagePoolName, mountImagePath, rbdSysBusPathDefault, c, e) if err != nil { - return err + fmt.Fprintln(os.Stderr, err) + os.Exit(1) } fmt.Println(out) diff --git a/cmd/castle/block-unmount.go b/cmd/castle/block-unmount.go index 5df33f989..ad8b52f38 100644 --- a/cmd/castle/block-unmount.go +++ b/cmd/castle/block-unmount.go @@ -3,6 +3,7 @@ package castle import ( "fmt" "log" + "os" "path/filepath" "strings" @@ -37,7 +38,8 @@ func unmountBlockEntry(cmd *cobra.Command, args []string) error { e := &exec.CommandExecutor{} out, err := unmountBlock(unmountDeviceName, unmountPath, rbdSysBusPathDefault, e) if err != nil { - return err + fmt.Fprintln(os.Stderr, err) + os.Exit(1) } fmt.Println(out) diff --git a/cmd/castle/castle.go b/cmd/castle/castle.go index bb648ea84..bc55f1153 100644 --- a/cmd/castle/castle.go +++ b/cmd/castle/castle.go @@ -45,7 +45,8 @@ func Main() { addCommands() if err := rootCmd.Execute(); err != nil { - fmt.Printf("castle error: %+v\n", err) + fmt.Fprintln(os.Stderr, err) + os.Exit(1) } } diff --git a/cmd/castle/node-list.go b/cmd/castle/node-list.go index 153f12a26..c66713fdb 100644 --- a/cmd/castle/node-list.go +++ b/cmd/castle/node-list.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "net/http" + "os" "github.com/quantum/castle/pkg/castle/client" "github.com/quantum/castle/pkg/model" @@ -26,7 +27,8 @@ func listNodesEntry(cmd *cobra.Command, args []string) error { c := client.NewCastleNetworkRestClient(client.GetRestURL(apiServerEndpoint), http.DefaultClient) out, err := listNodes(c) if err != nil { - return err + fmt.Fprintln(os.Stderr, err) + os.Exit(1) } fmt.Print(out) diff --git a/cmd/castle/pool-create.go b/cmd/castle/pool-create.go index ed42c54d3..7a1f466dd 100644 --- a/cmd/castle/pool-create.go +++ b/cmd/castle/pool-create.go @@ -3,6 +3,7 @@ package castle import ( "fmt" "net/http" + "os" "github.com/quantum/castle/pkg/castle/client" "github.com/quantum/castle/pkg/model" @@ -56,7 +57,8 @@ func createPoolsEntry(cmd *cobra.Command, args []string) error { c := client.NewCastleNetworkRestClient(client.GetRestURL(apiServerEndpoint), http.DefaultClient) out, err := createPool(newPoolName, newPoolType, newPoolReplicaCount, newPoolDataChunks, newPoolCodingChunks, c) if err != nil { - return err + fmt.Fprintln(os.Stderr, err) + os.Exit(1) } fmt.Println(out) diff --git a/cmd/castle/pool-list.go b/cmd/castle/pool-list.go index 0e969a428..512ebc837 100644 --- a/cmd/castle/pool-list.go +++ b/cmd/castle/pool-list.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "net/http" + "os" "github.com/quantum/castle/pkg/castle/client" "github.com/quantum/castle/pkg/model" @@ -29,7 +30,8 @@ func listPoolsEntry(cmd *cobra.Command, args []string) error { c := client.NewCastleNetworkRestClient(client.GetRestURL(apiServerEndpoint), http.DefaultClient) out, err := listPools(c) if err != nil { - return err + fmt.Fprintln(os.Stderr, err) + os.Exit(1) } fmt.Print(out) diff --git a/cmd/castled/daemon.go b/cmd/castled/daemon.go index 6fa988b6d..8d302962d 100644 --- a/cmd/castled/daemon.go +++ b/cmd/castled/daemon.go @@ -48,5 +48,10 @@ func runDaemon(cmd *cobra.Command, args []string) error { } // run the specified daemon - return cephd.New().RunDaemon(daemonType, os.Args[passthruIndex:]...) + if err := cephd.New().RunDaemon(daemonType, os.Args[passthruIndex:]...); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + + return nil } diff --git a/cmd/castled/main.go b/cmd/castled/main.go index f4ec58a5b..15b3910d6 100644 --- a/cmd/castled/main.go +++ b/cmd/castled/main.go @@ -74,7 +74,7 @@ func init() { // load the environment variables setFlagsFromEnv(rootCmd.Flags()) - rootCmd.RunE = joinCluster + rootCmd.RunE = startJoinCluster } func addCommands() { @@ -82,12 +82,21 @@ func addCommands() { rootCmd.AddCommand(daemonCmd) } -func joinCluster(cmd *cobra.Command, args []string) error { +func startJoinCluster(cmd *cobra.Command, args []string) error { // verify required flags if err := flags.VerifyRequiredFlags(cmd, []string{}); err != nil { return err } + if err := joinCluster(); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + + return nil +} + +func joinCluster() error { // ensure the data root exists if err := os.MkdirAll(cfg.dataDir, 0744); err != nil { log.Printf("failed to create data directory at %s: %+v", cfg.dataDir, err)