From b04d7e3ac0581fb3dfd73a28878ab18eba57d03f Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Wed, 24 Apr 2019 00:18:01 -0700 Subject: [PATCH] weed server: also optionally start S3 gateway --- weed/command/s3.go | 38 ++++++++++++++++++++++---------------- weed/command/server.go | 27 +++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/weed/command/s3.go b/weed/command/s3.go index aefbdbb8e..014342766 100644 --- a/weed/command/s3.go +++ b/weed/command/s3.go @@ -15,7 +15,7 @@ import ( ) var ( - s3options S3Options + s3StandaloneOptions S3Options ) type S3Options struct { @@ -29,12 +29,12 @@ type S3Options struct { func init() { cmdS3.Run = runS3 // break init cycle - s3options.filer = cmdS3.Flag.String("filer", "localhost:8888", "filer server address") - s3options.filerBucketsPath = cmdS3.Flag.String("filer.dir.buckets", "/buckets", "folder on filer to store all buckets") - s3options.port = cmdS3.Flag.Int("port", 8333, "s3options server http listen port") - s3options.domainName = cmdS3.Flag.String("domainName", "", "suffix of the host name, {bucket}.{domainName}") - s3options.tlsPrivateKey = cmdS3.Flag.String("key.file", "", "path to the TLS private key file") - s3options.tlsCertificate = cmdS3.Flag.String("cert.file", "", "path to the TLS certificate file") + s3StandaloneOptions.filer = cmdS3.Flag.String("filer", "localhost:8888", "filer server address") + s3StandaloneOptions.filerBucketsPath = cmdS3.Flag.String("filer.dir.buckets", "/buckets", "folder on filer to store all buckets") + s3StandaloneOptions.port = cmdS3.Flag.Int("port", 8333, "s3 server http listen port") + s3StandaloneOptions.domainName = cmdS3.Flag.String("domainName", "", "suffix of the host name, {bucket}.{domainName}") + s3StandaloneOptions.tlsPrivateKey = cmdS3.Flag.String("key.file", "", "path to the TLS private key file") + s3StandaloneOptions.tlsCertificate = cmdS3.Flag.String("cert.file", "", "path to the TLS certificate file") } var cmdS3 = &Command{ @@ -49,7 +49,13 @@ func runS3(cmd *Command, args []string) bool { weed_server.LoadConfiguration("security", false) - filerGrpcAddress, err := parseFilerGrpcAddress(*s3options.filer) + return s3StandaloneOptions.startS3Server() + +} + +func (s3opt *S3Options) startS3Server() bool { + + filerGrpcAddress, err := parseFilerGrpcAddress(*s3opt.filer) if err != nil { glog.Fatal(err) return false @@ -58,10 +64,10 @@ func runS3(cmd *Command, args []string) bool { router := mux.NewRouter().SkipClean(true) _, s3ApiServer_err := s3api.NewS3ApiServer(router, &s3api.S3ApiServerOption{ - Filer: *s3options.filer, + Filer: *s3opt.filer, FilerGrpcAddress: filerGrpcAddress, - DomainName: *s3options.domainName, - BucketsPath: *s3options.filerBucketsPath, + DomainName: *s3opt.domainName, + BucketsPath: *s3opt.filerBucketsPath, GrpcDialOption: security.LoadClientTLS(viper.Sub("grpc"), "client"), }) if s3ApiServer_err != nil { @@ -70,22 +76,22 @@ func runS3(cmd *Command, args []string) bool { httpS := &http.Server{Handler: router} - listenAddress := fmt.Sprintf(":%d", *s3options.port) + listenAddress := fmt.Sprintf(":%d", *s3opt.port) s3ApiListener, err := util.NewListener(listenAddress, time.Duration(10)*time.Second) if err != nil { glog.Fatalf("S3 API Server listener on %s error: %v", listenAddress, err) } - if *s3options.tlsPrivateKey != "" { - if err = httpS.ServeTLS(s3ApiListener, *s3options.tlsCertificate, *s3options.tlsPrivateKey); err != nil { + if *s3opt.tlsPrivateKey != "" { + glog.V(0).Infof("Start Seaweed S3 API Server %s at https port %d", util.VERSION, *s3opt.port) + if err = httpS.ServeTLS(s3ApiListener, *s3opt.tlsCertificate, *s3opt.tlsPrivateKey); err != nil { glog.Fatalf("S3 API Server Fail to serve: %v", err) } - glog.V(0).Infof("Start Seaweed S3 API Server %s at https port %d", util.VERSION, *s3options.port) } else { + glog.V(0).Infof("Start Seaweed S3 API Server %s at http port %d", util.VERSION, *s3opt.port) if err = httpS.Serve(s3ApiListener); err != nil { glog.Fatalf("S3 API Server Fail to serve: %v", err) } - glog.V(0).Infof("Start Seaweed S3 API Server %s at http port %d", util.VERSION, *s3options.port) } return true diff --git a/weed/command/server.go b/weed/command/server.go index a5d2ddd21..8f1c900f3 100644 --- a/weed/command/server.go +++ b/weed/command/server.go @@ -4,6 +4,7 @@ import ( "github.com/chrislusf/raft/protobuf" "github.com/chrislusf/seaweedfs/weed/security" "github.com/spf13/viper" + "fmt" "net/http" "os" "runtime" @@ -29,6 +30,7 @@ type ServerOptions struct { var ( serverOptions ServerOptions filerOptions FilerOptions + s3Options S3Options ) func init() { @@ -49,6 +51,8 @@ var cmdServer = &Command{ Optionally, one filer server can be started. Logically, filer servers should not be in a cluster. They run with meta data on disk, not shared. So each filer server is different. + Also optionally, one S3 gateway can be started. + `, } @@ -72,6 +76,7 @@ var ( volumeMaxDataVolumeCounts = cmdServer.Flag.String("volume.max", "7", "maximum numbers of volumes, count[,count]...") pulseSeconds = cmdServer.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats") isStartingFiler = cmdServer.Flag.Bool("filer", false, "whether to start filer") + isStartingS3 = cmdServer.Flag.Bool("s3", false, "whether to start S3 gateway") serverWhiteList []string ) @@ -94,6 +99,12 @@ func init() { serverOptions.v.readRedirect = cmdServer.Flag.Bool("volume.read.redirect", true, "Redirect moved or non-local volumes.") serverOptions.v.publicUrl = cmdServer.Flag.String("volume.publicUrl", "", "publicly accessible address") + s3Options.filerBucketsPath = cmdServer.Flag.String("s3.filer.dir.buckets", "/buckets", "folder on filer to store all buckets") + s3Options.port = cmdServer.Flag.Int("s3.port", 8333, "s3 server http listen port") + s3Options.domainName = cmdServer.Flag.String("s3.domainName", "", "suffix of the host name, {bucket}.{domainName}") + s3Options.tlsPrivateKey = cmdServer.Flag.String("s3.key.file", "", "path to the TLS private key file") + s3Options.tlsCertificate = cmdServer.Flag.String("s3.cert.file", "", "path to the TLS certificate file") + } func runServer(cmd *Command, args []string) bool { @@ -113,6 +124,10 @@ func runServer(cmd *Command, args []string) bool { *isStartingFiler = true } + if *isStartingS3 { + *isStartingFiler = true + } + master := *serverIp + ":" + strconv.Itoa(*masterPort) filerOptions.masters = &master filerOptions.ip = serverIp @@ -128,6 +143,9 @@ func runServer(cmd *Command, args []string) bool { filerOptions.dataCenter = serverDataCenter filerOptions.disableHttp = serverDisableHttp + filerAddress := fmt.Sprintf("%s:%d", *serverIp, *filerOptions.port) + s3Options.filer = &filerAddress + if *filerOptions.defaultReplicaPlacement == "" { *filerOptions.defaultReplicaPlacement = *masterDefaultReplicaPlacement } @@ -164,6 +182,15 @@ func runServer(cmd *Command, args []string) bool { }() } + if *isStartingS3 { + go func() { + time.Sleep(2 * time.Second) + + s3Options.startS3Server() + + }() + } + var volumeWait sync.WaitGroup volumeWait.Add(1)