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.

95 lines
3.0 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. filerAddress := pb.ServerAddress(*iamopt.filer)
  39. util.LoadConfiguration("security", false)
  40. grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
  41. for {
  42. err := pb.WithGrpcFilerClient(filerAddress, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  43. resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  44. if err != nil {
  45. return fmt.Errorf("get filer %s configuration: %v", filerAddress, err)
  46. }
  47. glog.V(0).Infof("IAM read filer configuration: %s", resp)
  48. return nil
  49. })
  50. if err != nil {
  51. glog.V(0).Infof("wait to connect to filer %s grpc address %s", *iamopt.filer, filerAddress.ToGrpcAddress())
  52. time.Sleep(time.Second)
  53. } else {
  54. glog.V(0).Infof("connected to filer %s grpc address %s", *iamopt.filer, filerAddress.ToGrpcAddress())
  55. break
  56. }
  57. }
  58. masters := pb.ServerAddresses(*iamopt.masters).ToAddresses()
  59. router := mux.NewRouter().SkipClean(true)
  60. _, iamApiServer_err := iamapi.NewIamApiServer(router, &iamapi.IamServerOption{
  61. Masters: masters,
  62. Filer: filerAddress,
  63. Port: *iamopt.port,
  64. GrpcDialOption: grpcDialOption,
  65. })
  66. glog.V(0).Info("NewIamApiServer created")
  67. if iamApiServer_err != nil {
  68. glog.Fatalf("IAM API Server startup error: %v", iamApiServer_err)
  69. }
  70. httpS := &http.Server{Handler: router}
  71. listenAddress := fmt.Sprintf(":%d", *iamopt.port)
  72. iamApiListener, err := util.NewListener(listenAddress, time.Duration(10)*time.Second)
  73. if err != nil {
  74. glog.Fatalf("IAM API Server listener on %s error: %v", listenAddress, err)
  75. }
  76. glog.V(0).Infof("Start Seaweed IAM API Server %s at http port %d", util.Version(), *iamopt.port)
  77. if err = httpS.Serve(iamApiListener); err != nil {
  78. glog.Fatalf("IAM API Server Fail to serve: %v", err)
  79. }
  80. return true
  81. }