2018-07-18 17:37:09 +08:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2020-01-30 01:09:55 +08:00
|
|
|
"fmt"
|
2018-07-18 17:37:09 +08:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2019-06-05 16:30:24 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/security"
|
|
|
|
|
2020-01-30 01:09:55 +08:00
|
|
|
"github.com/gorilla/mux"
|
2019-06-05 16:30:24 +08:00
|
|
|
|
2018-07-18 17:37:09 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2018-07-22 08:39:10 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/s3api"
|
2018-07-18 17:37:09 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-04-24 15:18:01 +08:00
|
|
|
s3StandaloneOptions S3Options
|
2018-07-18 17:37:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type S3Options struct {
|
2018-07-19 16:21:44 +08:00
|
|
|
filer *string
|
|
|
|
filerBucketsPath *string
|
|
|
|
port *int
|
|
|
|
domainName *string
|
2018-07-23 12:28:54 +08:00
|
|
|
tlsPrivateKey *string
|
|
|
|
tlsCertificate *string
|
2018-07-18 17:37:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cmdS3.Run = runS3 // break init cycle
|
2019-04-24 15:18:01 +08:00
|
|
|
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")
|
2018-07-18 17:37:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var cmdS3 = &Command{
|
|
|
|
UsageLine: "s3 -port=8333 -filer=<ip:port>",
|
|
|
|
Short: "start a s3 API compatible server that is backed by a filer",
|
|
|
|
Long: `start a s3 API compatible server that is backed by a filer.
|
|
|
|
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
|
|
|
func runS3(cmd *Command, args []string) bool {
|
|
|
|
|
2019-06-05 16:30:24 +08:00
|
|
|
util.LoadConfiguration("security", false)
|
2019-02-19 04:11:52 +08:00
|
|
|
|
2019-04-24 15:18:01 +08:00
|
|
|
return s3StandaloneOptions.startS3Server()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s3opt *S3Options) startS3Server() bool {
|
|
|
|
|
|
|
|
filerGrpcAddress, err := parseFilerGrpcAddress(*s3opt.filer)
|
2018-07-18 17:37:09 +08:00
|
|
|
if err != nil {
|
|
|
|
glog.Fatal(err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
router := mux.NewRouter().SkipClean(true)
|
|
|
|
|
|
|
|
_, s3ApiServer_err := s3api.NewS3ApiServer(router, &s3api.S3ApiServerOption{
|
2019-04-24 15:18:01 +08:00
|
|
|
Filer: *s3opt.filer,
|
2018-07-18 17:37:09 +08:00
|
|
|
FilerGrpcAddress: filerGrpcAddress,
|
2019-04-24 15:18:01 +08:00
|
|
|
DomainName: *s3opt.domainName,
|
|
|
|
BucketsPath: *s3opt.filerBucketsPath,
|
2020-01-30 01:09:55 +08:00
|
|
|
GrpcDialOption: security.LoadClientTLS(util.GetViper(), "grpc.client"),
|
2018-07-18 17:37:09 +08:00
|
|
|
})
|
|
|
|
if s3ApiServer_err != nil {
|
|
|
|
glog.Fatalf("S3 API Server startup error: %v", s3ApiServer_err)
|
|
|
|
}
|
|
|
|
|
2018-07-23 12:28:54 +08:00
|
|
|
httpS := &http.Server{Handler: router}
|
|
|
|
|
2019-04-24 15:18:01 +08:00
|
|
|
listenAddress := fmt.Sprintf(":%d", *s3opt.port)
|
2018-07-23 12:28:54 +08:00
|
|
|
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)
|
2018-07-18 17:37:09 +08:00
|
|
|
}
|
|
|
|
|
2019-04-24 15:18:01 +08:00
|
|
|
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 {
|
2018-07-23 12:28:54 +08:00
|
|
|
glog.Fatalf("S3 API Server Fail to serve: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
2019-04-24 15:18:01 +08:00
|
|
|
glog.V(0).Infof("Start Seaweed S3 API Server %s at http port %d", util.VERSION, *s3opt.port)
|
2018-07-23 12:28:54 +08:00
|
|
|
if err = httpS.Serve(s3ApiListener); err != nil {
|
|
|
|
glog.Fatalf("S3 API Server Fail to serve: %v", err)
|
|
|
|
}
|
2018-07-18 17:37:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|