This commit is contained in:
crusader 2018-04-17 19:28:37 +09:00
parent c70ce6375f
commit 08695e707b
4 changed files with 62 additions and 28 deletions

View File

@ -9,14 +9,12 @@ import (
"git.loafle.net/commons/configuration-go" "git.loafle.net/commons/configuration-go"
cdr "git.loafle.net/commons/di-go/registry" cdr "git.loafle.net/commons/di-go/registry"
logging "git.loafle.net/commons/logging-go" logging "git.loafle.net/commons/logging-go"
crc "git.loafle.net/commons/rpc-go/client"
occa "git.loafle.net/overflow/commons-go/core/annotation" occa "git.loafle.net/overflow/commons-go/core/annotation"
occi "git.loafle.net/overflow/commons-go/core/interfaces"
ocnc "git.loafle.net/overflow/commons-go/noauthprobe/config" ocnc "git.loafle.net/overflow/commons-go/noauthprobe/config"
"git.loafle.net/overflow/probe/client/central"
"git.loafle.net/overflow/probe/config" "git.loafle.net/overflow/probe/config"
// For service "git.loafle.net/overflow/probe/auth/service"
_ "git.loafle.net/overflow/probe/auth/service"
) )
type Authenticator struct { type Authenticator struct {
@ -25,6 +23,8 @@ type Authenticator struct {
authConfig ocnc.Auth authConfig ocnc.Auth
services []interface{}
stopChan chan struct{} stopChan chan struct{}
stopWg sync.WaitGroup stopWg sync.WaitGroup
} }
@ -56,9 +56,9 @@ func (a *Authenticator) EndableStart() (<-chan error, error) {
if nil != err { if nil != err {
return nil, err return nil, err
} }
a.services = services
client, err := central.NewAuth(a.HandleTempKey, services) if err := occi.ExecServices(a.services, occi.ServiceMethodInit, service.OrderedServices, false); nil != err {
if nil != err {
return nil, err return nil, err
} }
@ -66,7 +66,7 @@ func (a *Authenticator) EndableStart() (<-chan error, error) {
a.stopChan = make(chan struct{}) a.stopChan = make(chan struct{})
a.stopWg.Add(1) a.stopWg.Add(1)
go a.handleAuthenticator(client, authDoneChan, endChan) go a.handleAuthenticator(authDoneChan, endChan)
return endChan, nil return endChan, nil
} }
@ -78,6 +78,10 @@ func (a *Authenticator) Stop(ctx context.Context) error {
close(a.stopChan) close(a.stopChan)
a.stopWg.Wait() a.stopWg.Wait()
if err := occi.ExecServices(a.services, occi.ServiceMethodDestroy, service.OrderedServices, true); nil != err {
return err
}
a.stopChan = nil a.stopChan = nil
return nil return nil
@ -87,37 +91,27 @@ func (a *Authenticator) logHeader() string {
return "Authenticator:" return "Authenticator:"
} }
func (a *Authenticator) HandleTempKey(tempKey string) { func (a *Authenticator) handleAuthenticator(authDoneChan chan error, endChan chan<- error) {
logging.Logger().Infof("%s registered by central", a.logHeader())
a.authConfig.TempKey = &tempKey
if err := configuration.Save(a.authConfig, path.Join(a.ConfigDir, ocnc.ConfigFileName), true); nil != err {
logging.Logger().Errorf("%s %v", a.logHeader(), err)
return
}
}
func (a *Authenticator) handleAuthenticator(client *crc.Client, authDoneChan chan error, endChan chan<- error) {
var err error var err error
defer func() { defer func() {
if nil != client {
err = client.Stop(context.Background())
}
a.stopWg.Done() a.stopWg.Done()
endChan <- err endChan <- err
}() }()
if err = client.Start(); nil != err { err = occi.ExecServices(a.services, occi.ServiceMethodStart, service.OrderedServices, false)
logging.Logger().Error(err) if nil != err {
return return
} }
LOOP:
for { for {
select { select {
case err = <-authDoneChan: case err = <-authDoneChan:
return break LOOP
case <-a.stopChan: case <-a.stopChan:
return break LOOP
} }
} }
err = occi.ExecServices(a.services, occi.ServiceMethodStop, service.OrderedServices, true)
} }

View File

@ -10,8 +10,10 @@ import (
cda "git.loafle.net/commons/di-go/annotation" cda "git.loafle.net/commons/di-go/annotation"
cdr "git.loafle.net/commons/di-go/registry" cdr "git.loafle.net/commons/di-go/registry"
logging "git.loafle.net/commons/logging-go" logging "git.loafle.net/commons/logging-go"
crc "git.loafle.net/commons/rpc-go/client"
ocnc "git.loafle.net/overflow/commons-go/noauthprobe/config" ocnc "git.loafle.net/overflow/commons-go/noauthprobe/config"
ocpc "git.loafle.net/overflow/commons-go/probe/config" ocpc "git.loafle.net/overflow/commons-go/probe/config"
"git.loafle.net/overflow/probe/client/central"
"git.loafle.net/overflow/probe/config" "git.loafle.net/overflow/probe/config"
// For annotation // For annotation
@ -31,16 +33,36 @@ type NoAuthProbeService struct {
Config *config.Config `annotation:"@Resource(name='Config')"` Config *config.Config `annotation:"@Resource(name='Config')"`
AuthConfig *ocnc.Auth `annotation:"@Resource(name='AuthConfig')"` AuthConfig *ocnc.Auth `annotation:"@Resource(name='AuthConfig')"`
AuthDoneChan chan error `annotation:"@Resource(name='AuthDoneChan')"` AuthDoneChan chan error `annotation:"@Resource(name='AuthDoneChan')"`
client *crc.Client
}
func (s *NoAuthProbeService) Init() error {
client, err := central.NewAuth(s.HandleTempKey, s)
if nil != err {
return err
}
s.client = client
return nil
} }
func (s *NoAuthProbeService) Start() error { func (s *NoAuthProbeService) Start() error {
if err := s.client.Start(); nil != err {
return err
}
return nil return nil
} }
func (s *NoAuthProbeService) Stop(ctx context.Context) error { func (s *NoAuthProbeService) Stop() {
if err := s.client.Stop(context.Background()); nil != err {
logging.Logger().Error(err)
}
}
return nil func (s *NoAuthProbeService) Destroy() {
s.client = nil
} }
func (s *NoAuthProbeService) Accept(probeKey string) error { func (s *NoAuthProbeService) Accept(probeKey string) error {
@ -78,3 +100,13 @@ func (s *NoAuthProbeService) Deny() error {
s.AuthDoneChan <- nil s.AuthDoneChan <- nil
return nil return nil
} }
func (s *NoAuthProbeService) HandleTempKey(tempKey string) {
logging.Logger().Infof("registered by central")
s.AuthConfig.TempKey = &tempKey
if err := configuration.Save(s.AuthConfig, path.Join(s.ConfigDir, ocnc.ConfigFileName), true); nil != err {
logging.Logger().Error(err)
s.AuthDoneChan <- err
return
}
}

View File

@ -1,5 +1,13 @@
package service package service
import "reflect"
var (
OrderedServices = []reflect.Type{
reflect.TypeOf((*NoAuthProbeService)(nil)),
}
)
func InitPackage() { func InitPackage() {
} }

View File

@ -15,7 +15,7 @@ import (
"git.loafle.net/overflow/probe/config" "git.loafle.net/overflow/probe/config"
) )
func NewAuth(tempKeyHandler func(tempKey string), services []interface{}) (*crc.Client, error) { func NewAuth(tempKeyHandler func(tempKey string), services ...interface{}) (*crc.Client, error) {
config := config.GetConfig() config := config.GetConfig()
if nil == config { if nil == config {
return nil, fmt.Errorf("Config is not available") return nil, fmt.Errorf("Config is not available")