config fix, WIP: better config auto-reload
This commit is contained in:
parent
adc6496c4c
commit
07ce39c794
12
cmd/root.go
12
cmd/root.go
@ -5,7 +5,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/Neur0toxine/sshpoke/internal/api/plugin"
|
"github.com/Neur0toxine/sshpoke/internal/api/plugin"
|
||||||
"github.com/Neur0toxine/sshpoke/internal/api/rest"
|
"github.com/Neur0toxine/sshpoke/internal/api/rest"
|
||||||
@ -46,12 +45,8 @@ var rootCmd = &cobra.Command{
|
|||||||
config.Rehash()
|
config.Rehash()
|
||||||
logger.Default.Info("configuration has been updated, restarting the app...")
|
logger.Default.Info("configuration has been updated, restarting the app...")
|
||||||
cancel()
|
cancel()
|
||||||
go server.DefaultManager.WaitForShutdown()
|
server.DefaultManager.Wait()
|
||||||
ctx, innerCancel := context.WithTimeout(context.Background(), 2*time.Second)
|
docker.Default.Wait()
|
||||||
defer innerCancel()
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
}
|
|
||||||
initApp()
|
initApp()
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -179,7 +174,8 @@ func runDockerEventListener(ctx context.Context) {
|
|||||||
func makeShutdownFunc(cancel func()) func(os.Signal) {
|
func makeShutdownFunc(cancel func()) func(os.Signal) {
|
||||||
return func(sig os.Signal) {
|
return func(sig os.Signal) {
|
||||||
cancel()
|
cancel()
|
||||||
server.DefaultManager.WaitForShutdown()
|
server.DefaultManager.Wait()
|
||||||
|
docker.Default.Wait()
|
||||||
logger.Sugar.Infof("received %s, exiting...", sig)
|
logger.Sugar.Infof("received %s, exiting...", sig)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ var Default *Docker
|
|||||||
type Docker struct {
|
type Docker struct {
|
||||||
cli *client.Client
|
cli *client.Client
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
|
wait chan struct{}
|
||||||
services map[smarttypes.MatchableString]config.ServiceLabels
|
services map[smarttypes.MatchableString]config.ServiceLabels
|
||||||
defaultServer string
|
defaultServer string
|
||||||
}
|
}
|
||||||
@ -35,6 +36,7 @@ func New(ctx context.Context, services []config.Service, defaultServer string) (
|
|||||||
return &Docker{
|
return &Docker{
|
||||||
cli: cli,
|
cli: cli,
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
|
wait: make(chan struct{}),
|
||||||
services: servicesMap,
|
services: servicesMap,
|
||||||
defaultServer: defaultServer,
|
defaultServer: defaultServer,
|
||||||
}, nil
|
}, nil
|
||||||
@ -104,7 +106,7 @@ func (d *Docker) Listen() (chan dto.Event, error) {
|
|||||||
select {
|
select {
|
||||||
case event := <-eventSource:
|
case event := <-eventSource:
|
||||||
eventType := dto.TypeFromAction(event.Action)
|
eventType := dto.TypeFromAction(event.Action)
|
||||||
if (eventType != dto.EventStart && eventType != dto.EventStop) || !actorEnabled(event.Actor) {
|
if (eventType != dto.EventStart && eventType != dto.EventStop) || !actorEnabled(event.Actor).Bool() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
container, err := d.cli.ContainerList(d.ctx, types.ContainerListOptions{
|
container, err := d.cli.ContainerList(d.ctx, types.ContainerListOptions{
|
||||||
@ -139,6 +141,7 @@ func (d *Docker) Listen() (chan dto.Event, error) {
|
|||||||
output <- newEvent
|
output <- newEvent
|
||||||
case err := <-errSource:
|
case err := <-errSource:
|
||||||
if errors.Is(err, context.Canceled) {
|
if errors.Is(err, context.Canceled) {
|
||||||
|
d.wait <- struct{}{}
|
||||||
logger.Sugar.Debug("stopping docker event listener...")
|
logger.Sugar.Debug("stopping docker event listener...")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -151,3 +154,7 @@ func (d *Docker) Listen() (chan dto.Event, error) {
|
|||||||
|
|
||||||
return output, nil
|
return output, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Docker) Wait() {
|
||||||
|
<-d.wait
|
||||||
|
}
|
||||||
|
@ -26,14 +26,14 @@ type labelsConfig struct {
|
|||||||
func actorEnabled(actor events.Actor) smarttypes.BoolStr {
|
func actorEnabled(actor events.Actor) smarttypes.BoolStr {
|
||||||
label, ok := actor.Attributes["sshpoke.enable"]
|
label, ok := actor.Attributes["sshpoke.enable"]
|
||||||
if !ok {
|
if !ok {
|
||||||
return false
|
return smarttypes.AsBoolStr(false)
|
||||||
}
|
}
|
||||||
return smarttypes.BoolFromStr(label)
|
return smarttypes.BoolStr(label)
|
||||||
}
|
}
|
||||||
|
|
||||||
func populateLabelsFromConfig(labels *labelsConfig, config *config.ServiceLabels) {
|
func populateLabelsFromConfig(labels *labelsConfig, config *config.ServiceLabels) {
|
||||||
if labels.Enable != config.Enable {
|
if labels.Enable.Bool() != config.Enable.Bool() {
|
||||||
labels.Enable = config.Enable
|
labels.Enable = smarttypes.AsBoolStr(config.Enable.Bool())
|
||||||
}
|
}
|
||||||
if labels.Server != config.Server {
|
if labels.Server != config.Server {
|
||||||
labels.Server = config.Server
|
labels.Server = config.Server
|
||||||
@ -59,7 +59,7 @@ func dockerContainerToInternal(
|
|||||||
if configLabels != nil {
|
if configLabels != nil {
|
||||||
populateLabelsFromConfig(&labels, configLabels)
|
populateLabelsFromConfig(&labels, configLabels)
|
||||||
}
|
}
|
||||||
if !labels.Enable {
|
if !labels.Enable.Bool() {
|
||||||
logger.Sugar.Debugf("skipping container %s because sshpoke is not enabled for it", container.ID)
|
logger.Sugar.Debugf("skipping container %s because sshpoke is not enabled for it", container.ID)
|
||||||
return result, false
|
return result, false
|
||||||
}
|
}
|
||||||
|
@ -203,7 +203,7 @@ func (m *Manager) PluginByToken(token string) plugin.Plugin {
|
|||||||
return server
|
return server
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Manager) WaitForShutdown() {
|
func (m *Manager) Wait() {
|
||||||
defer m.rw.RUnlock()
|
defer m.rw.RUnlock()
|
||||||
m.rw.RLock()
|
m.rw.RLock()
|
||||||
for _, srv := range m.servers {
|
for _, srv := range m.servers {
|
||||||
|
@ -1,19 +1,16 @@
|
|||||||
package smarttypes
|
package smarttypes
|
||||||
|
|
||||||
type BoolStr bool
|
import (
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
func BoolFromStr(str string) BoolStr {
|
type BoolStr string
|
||||||
return str == "true" || str == "1"
|
|
||||||
|
func AsBoolStr(val bool) BoolStr {
|
||||||
|
return BoolStr(strconv.FormatBool(val))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b BoolStr) MarshalText() ([]byte, error) {
|
func (b BoolStr) Bool() bool {
|
||||||
if b {
|
val, _ := strconv.ParseBool(string(b))
|
||||||
return []byte("true"), nil
|
return val
|
||||||
}
|
|
||||||
return []byte("false"), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *BoolStr) UnmarshalText(src []byte) error {
|
|
||||||
*b = BoolFromStr(string(src))
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user