Skip to content

Commit

Permalink
fix(webconsole): use common options (#19329)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioito authored Jan 29, 2024
1 parent f33eb50 commit 4b73c96
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 66 deletions.
54 changes: 18 additions & 36 deletions cmd/cloudproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
Expand All @@ -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()
}
5 changes: 5 additions & 0 deletions pkg/apis/cloudproxy/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
20 changes: 18 additions & 2 deletions pkg/cloudproxy/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
22 changes: 16 additions & 6 deletions pkg/cloudproxy/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,36 @@ 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"
)

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)
Expand All @@ -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)
}
12 changes: 6 additions & 6 deletions pkg/image/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
22 changes: 6 additions & 16 deletions pkg/webconsole/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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()
}
Expand Down

0 comments on commit 4b73c96

Please sign in to comment.