wip: created example files with new config and implementing new config in init
This commit is contained in:
parent
6032b6c0c1
commit
8f68c4ace9
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "Golang Dev",
|
"name": "Golang Dev",
|
||||||
"image": "golang-dev:1.21-bookworm-user",
|
"image": "golang-dev:1.22-bookworm-user",
|
||||||
"mounts": [
|
"mounts": [
|
||||||
{
|
{
|
||||||
"source": "WingmateGoPath",
|
"source": "WingmateGoPath",
|
||||||
|
|||||||
@ -4,19 +4,43 @@ import (
|
|||||||
"gitea.suyono.dev/suyono/wingmate/config"
|
"gitea.suyono.dev/suyono/wingmate/config"
|
||||||
wminit "gitea.suyono.dev/suyono/wingmate/init"
|
wminit "gitea.suyono.dev/suyono/wingmate/init"
|
||||||
"gitea.suyono.dev/suyono/wingmate/task"
|
"gitea.suyono.dev/suyono/wingmate/task"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type wConfig struct {
|
type wConfig struct {
|
||||||
tasks *task.Tasks
|
tasks *task.Tasks
|
||||||
|
config *config.Config
|
||||||
|
viperMtx *sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *wConfig) Tasks() wminit.Tasks {
|
func (c *wConfig) Tasks() wminit.Tasks {
|
||||||
return c.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 {
|
func convert(cfg *config.Config) *wConfig {
|
||||||
retval := &wConfig{
|
retval := &wConfig{
|
||||||
tasks: task.NewTasks(),
|
tasks: task.NewTasks(),
|
||||||
|
config: cfg,
|
||||||
|
viperMtx: &sync.Mutex{},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, s := range cfg.Service {
|
for _, s := range cfg.Service {
|
||||||
|
|||||||
@ -14,7 +14,7 @@ func TestEntry_configPathEnv(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEntry_configPathPFlag(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()
|
main()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -2,14 +2,12 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"gitea.suyono.dev/suyono/wingmate"
|
"gitea.suyono.dev/suyono/wingmate"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -38,7 +36,6 @@ type Config struct {
|
|||||||
CronV0 []*Cron
|
CronV0 []*Cron
|
||||||
Service []ServiceTask
|
Service []ServiceTask
|
||||||
Cron []CronTask
|
Cron []CronTask
|
||||||
viperMtx *sync.Mutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Task struct {
|
type Task struct {
|
||||||
@ -108,7 +105,6 @@ func Read() (*Config, error) {
|
|||||||
serviceAvailable = false
|
serviceAvailable = false
|
||||||
cronAvailable = false
|
cronAvailable = false
|
||||||
outConfig := &Config{
|
outConfig := &Config{
|
||||||
viperMtx: &sync.Mutex{},
|
|
||||||
ServiceV0: make([]string, 0),
|
ServiceV0: make([]string, 0),
|
||||||
}
|
}
|
||||||
configPath := viper.GetString(ConfigPath)
|
configPath := viper.GetString(ConfigPath)
|
||||||
|
|||||||
20
docker/bookworm-newconfig/Dockerfile
Normal file
20
docker/bookworm-newconfig/Dockerfile
Normal file
@ -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" ]
|
||||||
7
docker/bookworm-newconfig/entry.sh
Normal file
7
docker/bookworm-newconfig/entry.sh
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#!/usr/bin/bash
|
||||||
|
|
||||||
|
if [ $# -gt 0 ]; then
|
||||||
|
exec "$@"
|
||||||
|
else
|
||||||
|
exec /usr/local/bin/wingmate
|
||||||
|
fi
|
||||||
5
docker/bookworm-newconfig/etc/wingmate/wingmate.yaml
Normal file
5
docker/bookworm-newconfig/etc/wingmate/wingmate.yaml
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
service:
|
||||||
|
one:
|
||||||
|
command: [ "/workspace/wingmate/cmd/experiment/starter/starter" ]
|
||||||
|
environ: [ "DUMMY_PATH=/workspace/wingmate/cmd/experiment/dummy/dummy" ]
|
||||||
|
|
||||||
@ -29,11 +29,7 @@ cron:
|
|||||||
for {
|
for {
|
||||||
if cron.TimeToRun(time.Now()) {
|
if cron.TimeToRun(time.Now()) {
|
||||||
wingmate.Log().Info().Str(cronTag, cron.Name()).Msg("executing")
|
wingmate.Log().Info().Str(cronTag, cron.Name()).Msg("executing")
|
||||||
if len(cron.Command()) == 1 {
|
cmd = exec.Command(cron.Command(), cron.Arguments()...)
|
||||||
cmd = exec.Command(cron.Command()[0])
|
|
||||||
} else {
|
|
||||||
cmd = exec.Command(cron.Command()[0], cron.Command()[1:]...)
|
|
||||||
}
|
|
||||||
iwg = &sync.WaitGroup{}
|
iwg = &sync.WaitGroup{}
|
||||||
|
|
||||||
if stdout, err = cmd.StdoutPipe(); err != nil {
|
if stdout, err = cmd.StdoutPipe(); err != nil {
|
||||||
|
|||||||
@ -23,7 +23,8 @@ type TaskStatus interface {
|
|||||||
|
|
||||||
type Task interface {
|
type Task interface {
|
||||||
Name() string
|
Name() string
|
||||||
Command() []string
|
Command() string
|
||||||
|
Arguments() []string
|
||||||
Environ() []string
|
Environ() []string
|
||||||
Setsid() bool
|
Setsid() bool
|
||||||
UserGroup() UserGroup
|
UserGroup() UserGroup
|
||||||
@ -47,6 +48,8 @@ type ServiceTask interface {
|
|||||||
|
|
||||||
type Config interface {
|
type Config interface {
|
||||||
Tasks() Tasks
|
Tasks() Tasks
|
||||||
|
WMPidProxyPath() string
|
||||||
|
WMExecPath() string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Init struct {
|
type Init struct {
|
||||||
|
|||||||
@ -33,11 +33,7 @@ func (i *Init) service(wg *sync.WaitGroup, task ServiceTask, exitFlag <-chan any
|
|||||||
service:
|
service:
|
||||||
for {
|
for {
|
||||||
failStatus = false
|
failStatus = false
|
||||||
if len(task.Command()) == 1 {
|
cmd = exec.Command(task.Command(), task.Arguments()...)
|
||||||
cmd = exec.Command(task.Command()[0])
|
|
||||||
} else {
|
|
||||||
cmd = exec.Command(task.Command()[0], task.Command()[1:]...)
|
|
||||||
}
|
|
||||||
iwg = &sync.WaitGroup{}
|
iwg = &sync.WaitGroup{}
|
||||||
|
|
||||||
if stdout, err = cmd.StdoutPipe(); err != nil {
|
if stdout, err = cmd.StdoutPipe(); err != nil {
|
||||||
|
|||||||
15
task/cron.go
15
task/cron.go
@ -126,9 +126,18 @@ func (c *CronTask) Name() string {
|
|||||||
return c.name
|
return c.name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CronTask) Command() []string {
|
func (c *CronTask) Command() string {
|
||||||
retval := make([]string, len(c.command))
|
return c.command[0]
|
||||||
copy(retval, c.command)
|
}
|
||||||
|
|
||||||
|
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
|
return retval
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
30
task/task.go
30
task/task.go
@ -125,13 +125,27 @@ func (t *ServiceTask) SetPidFile(path string) *ServiceTask {
|
|||||||
return t
|
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 {
|
func (t *ServiceTask) Name() string {
|
||||||
return t.name
|
return t.name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *ServiceTask) Command() []string {
|
func (t *ServiceTask) Command() string {
|
||||||
retval := make([]string, len(t.command))
|
return t.command[0]
|
||||||
copy(retval, t.command)
|
}
|
||||||
|
|
||||||
|
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
|
return retval
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -199,3 +213,13 @@ func (ug *userGroup) String() string {
|
|||||||
|
|
||||||
return ug.user
|
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
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user