You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.9 KiB

  1. package command
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/iamapi"
  8. "github.com/chrislusf/seaweedfs/weed/pb"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. "github.com/chrislusf/seaweedfs/weed/security"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. "github.com/gorilla/mux"
  13. "time"
  14. )
  15. var (
  16. iamStandaloneOptions IamOptions
  17. )
  18. type IamOptions struct {
  19. filer *string
  20. masters *string
  21. port *int
  22. }
  23. func init() {
  24. cmdIam.Run = runIam // break init cycle
  25. iamStandaloneOptions.filer = cmdIam.Flag.String("filer", "localhost:8888", "filer server address")
  26. iamStandaloneOptions.masters = cmdIam.Flag.String("master", "localhost:9333", "comma-separated master servers")
  27. iamStandaloneOptions.port = cmdIam.Flag.Int("port", 8111, "iam server http listen port")
  28. }
  29. var cmdIam = &Command{
  30. UsageLine: "iam [-port=8111] [-filer=<ip:port>] [-masters=<ip:port>,<ip:port>]",
  31. Short: "start a iam API compatible server",
  32. Long: "start a iam API compatible server.",
  33. }
  34. func runIam(cmd *Command, args []string) bool {
  35. return iamStandaloneOptions.startIamServer()
  36. }
  37. func (iamopt *IamOptions) startIamServer() bool {
  38. filerGrpcAddress, err := pb.ParseServerToGrpcAddress(*iamopt.filer)
  39. if err != nil {
  40. glog.Fatal(err)
  41. return false
  42. }
  43. grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
  44. for {
  45. err = pb.WithGrpcFilerClient(filerGrpcAddress, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  46. resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  47. if err != nil {
  48. return fmt.Errorf("get filer %s configuration: %v", filerGrpcAddress, err)
  49. }
  50. glog.V(0).Infof("IAM read filer configuration: %s", resp)
  51. return nil
  52. })
  53. if err != nil {
  54. glog.V(0).Infof("wait to connect to filer %s grpc address %s", *iamopt.filer, filerGrpcAddress)
  55. time.Sleep(time.Second)
  56. } else {
  57. glog.V(0).Infof("connected to filer %s grpc address %s", *iamopt.filer, filerGrpcAddress)
  58. break
  59. }
  60. }
  61. router := mux.NewRouter().SkipClean(true)
  62. _, iamApiServer_err := iamapi.NewIamApiServer(router, &iamapi.IamServerOption{
  63. Filer: *iamopt.filer,
  64. Port: *iamopt.port,
  65. FilerGrpcAddress: filerGrpcAddress,
  66. GrpcDialOption: grpcDialOption,
  67. })
  68. glog.V(0).Info("NewIamApiServer created")
  69. if iamApiServer_err != nil {
  70. glog.Fatalf("IAM API Server startup error: %v", iamApiServer_err)
  71. }
  72. httpS := &http.Server{Handler: router}
  73. listenAddress := fmt.Sprintf(":%d", *iamopt.port)
  74. iamApiListener, err := util.NewListener(listenAddress, time.Duration(10)*time.Second)
  75. if err != nil {
  76. glog.Fatalf("IAM API Server listener on %s error: %v", listenAddress, err)
  77. }
  78. glog.V(0).Infof("Start Seaweed IAM API Server %s at http port %d", util.Version(), *iamopt.port)
  79. if err = httpS.Serve(iamApiListener); err != nil {
  80. glog.Fatalf("IAM API Server Fail to serve: %v", err)
  81. }
  82. return true
  83. }