From 4b73c9667cbd37b55781abf953924421a0617dc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E8=BD=A9?= Date: Mon, 29 Jan 2024 11:29:23 +0800 Subject: [PATCH] fix(webconsole): use common options (#19329) --- cmd/cloudproxy/main.go | 54 +++++++++++-------------------- pkg/apis/cloudproxy/const.go | 5 +++ pkg/cloudproxy/options/options.go | 20 ++++++++++-- pkg/cloudproxy/service/service.go | 22 +++++++++---- pkg/image/service/service.go | 12 +++---- pkg/webconsole/service/service.go | 22 ++++--------- 6 files changed, 69 insertions(+), 66 deletions(-) diff --git a/cmd/cloudproxy/main.go b/cmd/cloudproxy/main.go index 6fec114d90e..01bcd414041 100644 --- a/cmd/cloudproxy/main.go +++ b/cmd/cloudproxy/main.go @@ -23,7 +23,6 @@ import ( "yunion.io/x/log" - common_app "yunion.io/x/onecloud/pkg/cloudcommon/app" "yunion.io/x/onecloud/pkg/cloudproxy/agent/worker" "yunion.io/x/onecloud/pkg/cloudproxy/options" "yunion.io/x/onecloud/pkg/cloudproxy/service" @@ -33,42 +32,29 @@ import ( func main() { defer atexit.Handle() - nop := true wg := &sync.WaitGroup{} ctx := context.Background() ctx = context.WithValue(ctx, "wg", wg) ctx, cancelFunc := context.WithCancel(ctx) var ( - opts = options.Get() - commonOpts = &opts.CommonOptions + opts = options.Get() ) - if opts.EnableAPIServer || opts.EnableProxyAgent { - common_app.InitAuth(commonOpts, func() { - log.Infof("Auth complete") - }) - } - if opts.EnableAPIServer { - nop = false - wg.Add(1) - go func() { - defer wg.Done() - service.StartService() - }() - } + wg.Add(1) + go func() { + defer wg.Done() + service.StartService() + }() if opts.EnableProxyAgent { - if opts.EnableAPIServer { - const d = "10m" - log.Infof("set proxy_agent_init_wait to %s", d) - opts.Options.ProxyAgentInitWait = d - } + const d = "10m" + log.Infof("set proxy_agent_init_wait to %s", d) + opts.Options.ProxyAgentInitWait = d if err := opts.Options.ValidateThenInit(); err != nil { log.Fatalf("proxy agent options validation: %v", err) } worker := worker.NewWorker(&opts.CommonOptions, &opts.Options) - nop = false go func() { worker.Start(ctx) pid := os.Getpid() @@ -79,17 +65,13 @@ func main() { p.Signal(syscall.SIGTERM) }() } - if nop { - log.Warningln("nothing to do. Please check configuration") - } else { - go func() { - sigChan := make(chan os.Signal) - signal.Notify(sigChan, syscall.SIGINT) - signal.Notify(sigChan, syscall.SIGTERM) - sig := <-sigChan - log.Infof("signal received: %s", sig) - cancelFunc() - }() - wg.Wait() - } + go func() { + sigChan := make(chan os.Signal) + signal.Notify(sigChan, syscall.SIGINT) + signal.Notify(sigChan, syscall.SIGTERM) + sig := <-sigChan + log.Infof("signal received: %s", sig) + cancelFunc() + }() + wg.Wait() } diff --git a/pkg/apis/cloudproxy/const.go b/pkg/apis/cloudproxy/const.go index e425398877e..74b12f211be 100644 --- a/pkg/apis/cloudproxy/const.go +++ b/pkg/apis/cloudproxy/const.go @@ -18,6 +18,11 @@ import ( "yunion.io/x/onecloud/pkg/util/choices" ) +const ( + SERVICE_TYPE = "cloudproxy" + SERVICE_VERSION = "" +) + const ( PM_SCOPE_VPC = "vpc" PM_SCOPE_NETWORK = "network" diff --git a/pkg/cloudproxy/options/options.go b/pkg/cloudproxy/options/options.go index cb88a719657..45281a55c25 100644 --- a/pkg/cloudproxy/options/options.go +++ b/pkg/cloudproxy/options/options.go @@ -18,12 +18,12 @@ import ( "os" "sync" + api "yunion.io/x/onecloud/pkg/apis/cloudproxy" common_options "yunion.io/x/onecloud/pkg/cloudcommon/options" agent_options "yunion.io/x/onecloud/pkg/cloudproxy/agent/options" ) type Options struct { - EnableAPIServer bool EnableProxyAgent bool common_options.CommonOptions @@ -43,8 +43,24 @@ func Get() *Options { &opts, os.Args, "cloudproxy.conf", - "cloudproxy", + api.SERVICE_TYPE, ) }) return &opts } + +func OnOptionsChange(oldO, newO interface{}) bool { + oldOpts := oldO.(*Options) + newOpts := newO.(*Options) + + changed := false + if common_options.OnCommonOptionsChange(&oldOpts.CommonOptions, &newOpts.CommonOptions) { + changed = true + } + + if common_options.OnDBOptionsChange(&oldOpts.DBOptions, &newOpts.DBOptions) { + changed = true + } + + return changed +} diff --git a/pkg/cloudproxy/service/service.go b/pkg/cloudproxy/service/service.go index 7b608645641..dd0528579da 100644 --- a/pkg/cloudproxy/service/service.go +++ b/pkg/cloudproxy/service/service.go @@ -18,13 +18,16 @@ import ( "context" "yunion.io/x/jsonutils" + "yunion.io/x/log" _ "yunion.io/x/sqlchemy/backends" + api "yunion.io/x/onecloud/pkg/apis/cloudproxy" "yunion.io/x/onecloud/pkg/cloudcommon" - app_common "yunion.io/x/onecloud/pkg/cloudcommon/app" + common_app "yunion.io/x/onecloud/pkg/cloudcommon/app" "yunion.io/x/onecloud/pkg/cloudcommon/consts" "yunion.io/x/onecloud/pkg/cloudcommon/db" "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient" + common_options "yunion.io/x/onecloud/pkg/cloudcommon/options" "yunion.io/x/onecloud/pkg/cloudproxy/models" "yunion.io/x/onecloud/pkg/cloudproxy/options" "yunion.io/x/onecloud/pkg/mcclient/auth" @@ -32,12 +35,19 @@ import ( func StartService() { var ( - opts = options.Get() - dbOpts = &opts.DBOptions - baseOpts = &opts.BaseOptions + opts = options.Get() + dbOpts = &opts.DBOptions + baseOpts = &opts.BaseOptions + commonOpts = &opts.CommonOptions ) - app := app_common.InitApp(baseOpts, true). + common_app.InitAuth(commonOpts, func() { + log.Infof("Auth complete") + }) + + common_options.StartOptionManager(opts, opts.ConfigSyncPeriodSeconds, api.SERVICE_TYPE, api.SERVICE_VERSION, options.OnOptionsChange) + + app := common_app.InitApp(baseOpts, true). OnException(func(method, path string, body jsonutils.JSONObject, err error) { ctx := context.Background() session := auth.GetAdminSession(ctx, baseOpts.Region) @@ -51,5 +61,5 @@ func StartService() { db.EnsureAppSyncDB(app, dbOpts, models.InitDB) defer cloudcommon.CloseDB() - app_common.ServeForever(app, baseOpts) + common_app.ServeForever(app, baseOpts) } diff --git a/pkg/image/service/service.go b/pkg/image/service/service.go index 18c4e7c4931..7912c502593 100644 --- a/pkg/image/service/service.go +++ b/pkg/image/service/service.go @@ -61,6 +61,12 @@ func StartService() { // log.Fatalf("glance service must running with root permissions") // } + app_common.InitAuth(commonOpts, func() { + log.Infof("Auth complete!!") + }) + + common_options.StartOptionManager(opts, opts.ConfigSyncPeriodSeconds, api.SERVICE_TYPE, api.SERVICE_VERSION, options.OnOptionsChange) + if opts.PortV2 > 0 { log.Infof("Port V2 %d is specified, use v2 port", opts.PortV2) opts.Port = opts.PortV2 @@ -96,10 +102,6 @@ func StartService() { log.Infof("Target image formats %#v", opts.TargetImageFormats) - app_common.InitAuth(commonOpts, func() { - log.Infof("Auth complete!!") - }) - if ok, err := hasVmwareAccount(); err != nil { log.Errorf("failed get vmware cloudaccounts") } else if ok { @@ -122,8 +124,6 @@ func StartService() { db.EnsureAppSyncDB(app, dbOpts, models.InitDB) - common_options.StartOptionManager(opts, opts.ConfigSyncPeriodSeconds, api.SERVICE_TYPE, api.SERVICE_VERSION, options.OnOptionsChange) - models.Init(options.Options.StorageDriver) if len(options.Options.DeployServerSocketPath) > 0 { diff --git a/pkg/webconsole/service/service.go b/pkg/webconsole/service/service.go index 1ed7c25952e..39b4e47ba5e 100644 --- a/pkg/webconsole/service/service.go +++ b/pkg/webconsole/service/service.go @@ -41,18 +41,18 @@ import ( "yunion.io/x/onecloud/pkg/webconsole/server" ) -func ensureBinExists(binPath string) { - if _, err := os.Stat(binPath); os.IsNotExist(err) { - log.Fatalf("Binary %s not exists", binPath) - } -} - func StartService() { opts := &o.Options commonOpts := &o.Options.CommonOptions common_options.ParseOptions(opts, os.Args, "webconsole.conf", api.SERVICE_TYPE) + app_common.InitAuth(commonOpts, func() { + log.Infof("Auth complete") + }) + + common_options.StartOptionManager(opts, opts.ConfigSyncPeriodSeconds, api.SERVICE_TYPE, api.SERVICE_VERSION, o.OnOptionsChange) + if opts.ApiServer == "" { log.Fatalf("--api-server must specified") } @@ -61,16 +61,6 @@ func StartService() { log.Fatalf("invalid --api-server %s", opts.ApiServer) } - for _, binPath := range []string{opts.IpmitoolPath} { - ensureBinExists(binPath) - } - - app_common.InitAuth(commonOpts, func() { - log.Infof("Auth complete") - }) - - common_options.StartOptionManager(opts, opts.ConfigSyncPeriodSeconds, api.SERVICE_TYPE, api.SERVICE_VERSION, o.OnOptionsChange) - registerSigTraps() start() }