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.

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