func newRunCmdWithOpts()

in app/dubbo-cp/cmd/run.go [56:169]


func newRunCmdWithOpts(opts dubbo_cmd.RunCmdOpts) *cobra.Command {
	args := struct {
		configPath string
	}{}
	cmd := &cobra.Command{
		Use:   "run",
		Short: "Launch Control Plane",
		Long:  `Launch Control Plane.`,
		RunE: func(cmd *cobra.Command, _ []string) error {
			cfg := dubbo_cp.DefaultConfig()
			err := config.Load(args.configPath, &cfg)
			if err != nil {
				runLog.Error(err, "could not load the configuration")
				return err
			}

			gracefulCtx, ctx := opts.SetupSignalHandler()
			rt, err := bootstrap.Bootstrap(gracefulCtx, cfg)
			if err != nil {
				runLog.Error(err, "unable to set up Control Plane runtime")
				return err
			}
			// FIXME config sanitize 需移到bootstrap前
			cfgForDisplay, err := config.ConfigForDisplay(&cfg)
			if err != nil {
				runLog.Error(err, "unable to prepare config for display")
				return err
			}
			cfgBytes, err := config.ToJson(cfgForDisplay)
			if err != nil {
				runLog.Error(err, "unable to convert config to json")
				return err
			}
			runLog.Info(fmt.Sprintf("Current config %s", cfgBytes))
			runLog.Info(fmt.Sprintf("Running in mode `%s`", cfg.Mode))

			if err := os.RaiseFileLimit(); err != nil {
				runLog.Error(err, "unable to raise the open file limit")
			}

			if limit, _ := os.CurrentFileLimit(); limit < minOpenFileLimit {
				runLog.Info("for better performance, raise the open file limit",
					"minimim-open-files", minOpenFileLimit)
			}

			if err := admin.Setup(rt); err != nil {
				runLog.Error(err, "unable to set up admin")
				return err

			}

			if err := xds.Setup(rt); err != nil {
				runLog.Error(err, "unable to set up xds server")
				return err
			}

			if err := hds.Setup(rt); err != nil {
				runLog.Error(err, "unable to set up HDS")
				return err
			}
			if err := dp_server.SetupServer(rt); err != nil {
				runLog.Error(err, "unable to set up DP Server")
				return err
			}

			//if err := defaults.Setup(rt); err != nil {
			//	runLog.Error(err, "unable to set up Defaults")
			//	return err
			//}

			if err := dds_zone.Setup(rt); err != nil {
				runLog.Error(err, "unable to set up Zone DDS")
				return err
			}
			if err := dds_global.Setup(rt); err != nil {
				runLog.Error(err, "unable to set up Global DDS")
				return err
			}
			if err := mds.Setup(rt); err != nil {
				runLog.Error(err, "unable to set up mds")
				return err
			}
			if err := diagnostics.SetupServer(rt); err != nil {
				runLog.Error(err, "unable to set up Diagnostics server")
				return err
			}

			if rt.GetMode() == core.Test {
				if err := test.Setup(rt); err != nil {
					runLog.Error(err, "unable to set up test")
					return err
				}
			}

			runLog.Info("starting Control Plane", "version", dubbo_version.Build.Version)
			if err := rt.Start(gracefulCtx.Done()); err != nil {
				runLog.Error(err, "problem running Control Plane")
				return err
			}

			runLog.Info("stop signal received. Waiting 3 seconds for components to stop gracefully...")
			select {
			case <-ctx.Done():
				runLog.Info("all components have stopped")
			case <-time.After(gracefullyShutdownDuration):
				runLog.Info("forcefully stopped")
			}
			return nil
		},
	}
	// flags
	cmd.PersistentFlags().StringVarP(&args.configPath, "config-file", "c", "", "configuration file")
	return cmd
}