From 8f68c4ace9c88934aeb409b519b3b26bfd1e0f1a Mon Sep 17 00:00:00 2001 From: Suyono Date: Sun, 24 Mar 2024 13:24:47 +1100 Subject: [PATCH] wip: created example files with new config and implementing new config in init --- .devcontainer/devcontainer.json | 2 +- cmd/wingmate/bridge.go | 28 +++++++++++++++-- cmd/wingmate/wingmate_test.go | 2 +- config/config.go | 10 ++----- docker/bookworm-newconfig/Dockerfile | 20 +++++++++++++ docker/bookworm-newconfig/entry.sh | 7 +++++ .../etc/wingmate/wingmate.yaml | 5 ++++ init/cron.go | 6 +--- init/init.go | 5 +++- init/service.go | 6 +--- task/cron.go | 15 ++++++++-- task/task.go | 30 +++++++++++++++++-- 12 files changed, 108 insertions(+), 28 deletions(-) create mode 100644 docker/bookworm-newconfig/Dockerfile create mode 100644 docker/bookworm-newconfig/entry.sh create mode 100644 docker/bookworm-newconfig/etc/wingmate/wingmate.yaml diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index ac57354..d8213f3 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,6 +1,6 @@ { "name": "Golang Dev", - "image": "golang-dev:1.21-bookworm-user", + "image": "golang-dev:1.22-bookworm-user", "mounts": [ { "source": "WingmateGoPath", diff --git a/cmd/wingmate/bridge.go b/cmd/wingmate/bridge.go index 0495139..cf7acc5 100644 --- a/cmd/wingmate/bridge.go +++ b/cmd/wingmate/bridge.go @@ -4,19 +4,43 @@ import ( "gitea.suyono.dev/suyono/wingmate/config" wminit "gitea.suyono.dev/suyono/wingmate/init" "gitea.suyono.dev/suyono/wingmate/task" + "github.com/spf13/viper" + "sync" ) type wConfig struct { - tasks *task.Tasks + tasks *task.Tasks + config *config.Config + viperMtx *sync.Mutex } func (c *wConfig) Tasks() wminit.Tasks { return c.tasks } +func (c *wConfig) WMPidProxyPath() string { + c.viperMtx.Lock() + defer c.viperMtx.Unlock() + + return viper.GetString(config.PidProxyPathConfig) +} + +func (c *wConfig) WMExecPath() string { + c.viperMtx.Lock() + defer c.viperMtx.Unlock() + + return viper.GetString(config.ExecPathConfig) +} + +func (c *wConfig) Reload() error { + return nil +} + func convert(cfg *config.Config) *wConfig { retval := &wConfig{ - tasks: task.NewTasks(), + tasks: task.NewTasks(), + config: cfg, + viperMtx: &sync.Mutex{}, } for _, s := range cfg.Service { diff --git a/cmd/wingmate/wingmate_test.go b/cmd/wingmate/wingmate_test.go index c2d86d0..02d23f4 100644 --- a/cmd/wingmate/wingmate_test.go +++ b/cmd/wingmate/wingmate_test.go @@ -14,7 +14,7 @@ func TestEntry_configPathEnv(t *testing.T) { } func TestEntry_configPathPFlag(t *testing.T) { - os.Args = []string{"wingmate", "--config", "/Volumes/Source/go/src/gitea.suyono.dev/suyono/wingmate/docker/bookworm/etc/wingmate"} + os.Args = []string{"wingmate", "--config", "/workspaces/wingmate/docker/bookworm-newconfig/etc/wingmate"} main() } diff --git a/config/config.go b/config/config.go index 362d7f8..ad8494b 100644 --- a/config/config.go +++ b/config/config.go @@ -2,14 +2,12 @@ package config import ( "errors" - "os" - "path/filepath" - "strings" - "sync" - "gitea.suyono.dev/suyono/wingmate" "github.com/spf13/viper" "golang.org/x/sys/unix" + "os" + "path/filepath" + "strings" ) const ( @@ -38,7 +36,6 @@ type Config struct { CronV0 []*Cron Service []ServiceTask Cron []CronTask - viperMtx *sync.Mutex } type Task struct { @@ -108,7 +105,6 @@ func Read() (*Config, error) { serviceAvailable = false cronAvailable = false outConfig := &Config{ - viperMtx: &sync.Mutex{}, ServiceV0: make([]string, 0), } configPath := viper.GetString(ConfigPath) diff --git a/docker/bookworm-newconfig/Dockerfile b/docker/bookworm-newconfig/Dockerfile new file mode 100644 index 0000000..9021fc9 --- /dev/null +++ b/docker/bookworm-newconfig/Dockerfile @@ -0,0 +1,20 @@ +FROM golang:1.21-bookworm as builder + +ADD . /root/wingmate +WORKDIR /root/wingmate/ +ARG TEST_BUILD +RUN make all && make DESTDIR=/usr/local/bin/wingmate install + + + +FROM debian:bookworm + +RUN ln -sf /usr/share/zoneinfo/Australia/Sydney /etc/localtime && \ + apt update && apt install -y procps && \ + useradd -m -s /bin/bash user1 +COPY --from=builder /usr/local/bin/wingmate/ /usr/local/bin/ +ADD --chmod=755 docker/bookworm/entry.sh /usr/local/bin/entry.sh +ADD --chmod=755 docker/bookworm/etc /etc + +ENTRYPOINT [ "/usr/local/bin/entry.sh" ] +CMD [ "/usr/local/bin/wingmate" ] \ No newline at end of file diff --git a/docker/bookworm-newconfig/entry.sh b/docker/bookworm-newconfig/entry.sh new file mode 100644 index 0000000..f1018b5 --- /dev/null +++ b/docker/bookworm-newconfig/entry.sh @@ -0,0 +1,7 @@ +#!/usr/bin/bash + +if [ $# -gt 0 ]; then + exec "$@" +else + exec /usr/local/bin/wingmate +fi \ No newline at end of file diff --git a/docker/bookworm-newconfig/etc/wingmate/wingmate.yaml b/docker/bookworm-newconfig/etc/wingmate/wingmate.yaml new file mode 100644 index 0000000..15073d3 --- /dev/null +++ b/docker/bookworm-newconfig/etc/wingmate/wingmate.yaml @@ -0,0 +1,5 @@ +service: + one: + command: [ "/workspace/wingmate/cmd/experiment/starter/starter" ] + environ: [ "DUMMY_PATH=/workspace/wingmate/cmd/experiment/dummy/dummy" ] + \ No newline at end of file diff --git a/init/cron.go b/init/cron.go index 057e922..a620662 100644 --- a/init/cron.go +++ b/init/cron.go @@ -29,11 +29,7 @@ cron: for { if cron.TimeToRun(time.Now()) { wingmate.Log().Info().Str(cronTag, cron.Name()).Msg("executing") - if len(cron.Command()) == 1 { - cmd = exec.Command(cron.Command()[0]) - } else { - cmd = exec.Command(cron.Command()[0], cron.Command()[1:]...) - } + cmd = exec.Command(cron.Command(), cron.Arguments()...) iwg = &sync.WaitGroup{} if stdout, err = cmd.StdoutPipe(); err != nil { diff --git a/init/init.go b/init/init.go index 2c1b095..f9a7728 100644 --- a/init/init.go +++ b/init/init.go @@ -23,7 +23,8 @@ type TaskStatus interface { type Task interface { Name() string - Command() []string + Command() string + Arguments() []string Environ() []string Setsid() bool UserGroup() UserGroup @@ -47,6 +48,8 @@ type ServiceTask interface { type Config interface { Tasks() Tasks + WMPidProxyPath() string + WMExecPath() string } type Init struct { diff --git a/init/service.go b/init/service.go index 703815d..c07af6e 100644 --- a/init/service.go +++ b/init/service.go @@ -33,11 +33,7 @@ func (i *Init) service(wg *sync.WaitGroup, task ServiceTask, exitFlag <-chan any service: for { failStatus = false - if len(task.Command()) == 1 { - cmd = exec.Command(task.Command()[0]) - } else { - cmd = exec.Command(task.Command()[0], task.Command()[1:]...) - } + cmd = exec.Command(task.Command(), task.Arguments()...) iwg = &sync.WaitGroup{} if stdout, err = cmd.StdoutPipe(); err != nil { diff --git a/task/cron.go b/task/cron.go index 9fe04b1..f50c3f2 100644 --- a/task/cron.go +++ b/task/cron.go @@ -126,9 +126,18 @@ func (c *CronTask) Name() string { return c.name } -func (c *CronTask) Command() []string { - retval := make([]string, len(c.command)) - copy(retval, c.command) +func (c *CronTask) Command() string { + return c.command[0] +} + +func (c *CronTask) Arguments() []string { + if len(c.command) == 1 { + return nil + } + + retval := make([]string, len(c.command)-1) + copy(retval, c.command[1:]) + return retval } diff --git a/task/task.go b/task/task.go index ecc8665..0f514e9 100644 --- a/task/task.go +++ b/task/task.go @@ -125,13 +125,27 @@ func (t *ServiceTask) SetPidFile(path string) *ServiceTask { return t } +func (t *ServiceTask) Validate() error { + // call this function for validate the field + return validate( /* input the validators here */ ) +} + func (t *ServiceTask) Name() string { return t.name } -func (t *ServiceTask) Command() []string { - retval := make([]string, len(t.command)) - copy(retval, t.command) +func (t *ServiceTask) Command() string { + return t.command[0] +} + +func (t *ServiceTask) Arguments() []string { + if len(t.command) == 1 { + return nil + } + + retval := make([]string, len(t.command)-1) + copy(retval, t.command[1:]) + return retval } @@ -199,3 +213,13 @@ func (ug *userGroup) String() string { return ug.user } + +func validate(validators ...func() error) error { + var err error + for _, v := range validators { + if err = v(); err != nil { + return err + } + } + return nil +}