feat: pidproxy and Makefile

This commit is contained in:
Suyono 2023-12-10 05:39:37 +00:00
parent 3dc69325c1
commit 4ec5750cd5
12 changed files with 208 additions and 18 deletions

View File

@ -17,7 +17,8 @@
"vscode": {
"extensions": [
"golang.go",
"ms-azuretools.vscode-docker"
"ms-azuretools.vscode-docker",
"ms-vscode.makefile-tools"
]
}
}

29
Makefile Normal file
View File

@ -0,0 +1,29 @@
all: wingmate dummy oneshot spawner starter pidproxy
wingmate:
$(MAKE) -C cmd/wingmate all
pidproxy:
$(MAKE) -C cmd/pidproxy all
dummy:
$(MAKE) -C cmd/experiment/dummy all
oneshot:
$(MAKE) -C cmd/experiment/oneshot all
spawner:
$(MAKE) -C cmd/experiment/spawner all
starter:
$(MAKE) -C cmd/experiment/starter all
clean:
$(MAKE) -C cmd/wingmate clean
$(MAKE) -C cmd/pidproxy clean
$(MAKE) -C cmd/experiment/dummy clean
$(MAKE) -C cmd/experiment/oneshot clean
$(MAKE) -C cmd/experiment/spawner clean
$(MAKE) -C cmd/experiment/starter clean

View File

@ -0,0 +1,5 @@
all:
go build -v
clean:
go clean -i -cache -testcache

View File

@ -0,0 +1,5 @@
all:
go build -v
clean:
go clean -i -cache -testcache

View File

@ -0,0 +1,5 @@
all:
go build -v
clean:
go clean -i -cache -testcache

View File

@ -0,0 +1,5 @@
all:
go build -v
clean:
go clean -i -cache -testcache

5
cmd/pidproxy/Makefile Normal file
View File

@ -0,0 +1,5 @@
all:
go build -v
clean:
go clean -i -cache -testcache

134
cmd/pidproxy/pidproxy.go Normal file
View File

@ -0,0 +1,134 @@
package main
import (
"log"
"os"
"os/exec"
"strconv"
"syscall"
"time"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/sys/unix"
)
const (
pidFileFlag = "pid-file"
)
var (
rootCmd = &cobra.Command{
Use: "wmpidproxy",
RunE: pidProxy,
}
childArgs []string
)
func main() {
var (
i int
arg string
selfArgs []string
found bool
)
if len(os.Args) <= 2 {
log.Println("invalid argument")
os.Exit(1)
}
rootCmd.PersistentFlags().StringP(pidFileFlag, "p", "", "location of pid file")
rootCmd.MarkFlagRequired(pidFileFlag)
viper.BindPFlag(pidFileFlag, rootCmd.PersistentFlags().Lookup(pidFileFlag))
found = false
for i, arg = range os.Args {
if arg == "--" {
found = true
selfArgs = os.Args[1:i]
if len(os.Args) <= i+1 {
log.Println("invalid argument")
os.Exit(1)
}
childArgs = os.Args[i+1:]
break
}
}
if !found {
log.Println("invalid argument")
os.Exit(1)
}
rootCmd.SetArgs(selfArgs)
if err := rootCmd.Execute(); err != nil {
log.Println(err)
os.Exit(1)
}
}
func pidProxy(cmd *cobra.Command, args []string) error {
pidfile := viper.GetString(pidFileFlag)
log.Printf("%s %v", pidfile, childArgs)
if len(childArgs) > 1 {
go startProcess(childArgs[0], childArgs[1:]...)
} else {
go startProcess(childArgs[0])
}
time.Sleep(time.Second)
var (
err error
pid int
)
for {
if pid, err = readPid(pidfile); err != nil {
return err
}
if err = unix.Kill(pid, syscall.Signal(0)); err != nil {
return err
}
time.Sleep(time.Second)
}
}
func readPid(pidFile string) (int, error) {
var (
file *os.File
err error
buf []byte
n int
pid64 int64
)
if file, err = os.Open(pidFile); err != nil {
return 0, err
}
defer func() {
_ = file.Close()
}()
buf = make([]byte, 1024)
n, err = file.Read(buf)
if err != nil {
return 0, err
}
pid64, err = strconv.ParseInt(string(buf[:n]), 10, 64)
if err != nil {
return 0, err
}
return int(pid64), nil
}
func startProcess(arg0 string, args ...string) {
if err := exec.Command(arg0, args...).Run(); err != nil {
log.Println("exec:", err)
return
}
}

5
cmd/wingmate/Makefile Normal file
View File

@ -0,0 +1,5 @@
all:
go build -v
clean:
go clean -i -cache -testcache

View File

@ -1,20 +1,8 @@
FROM golang:1.21-alpine as builder
ADD . /root/wingmate
WORKDIR /root/wingmate/cmd/wingmate
RUN go build -v
WORKDIR /root/wingmate/cmd/experiment/dummy
RUN go build -v
WORKDIR /root/wingmate/cmd/experiment/starter
RUN go build -v
WORKDIR /root/wingmate/cmd/experiment/spawner
RUN go build -v
WORKDIR /root/wingmate/cmd/experiment/oneshot
RUN go build -v
WORKDIR /root/wingmate/
RUN apk add make && make all
@ -26,6 +14,7 @@ COPY --from=builder /root/wingmate/cmd/experiment/dummy/dummy /usr/local/bin/wmd
COPY --from=builder /root/wingmate/cmd/experiment/starter/starter /usr/local/bin/wmstarter
COPY --from=builder /root/wingmate/cmd/experiment/oneshot/oneshot /usr/local/bin/wmoneshot
COPY --from=builder /root/wingmate/cmd/experiment/spawner/spawner /usr/local/bin/wmspawner
COPY --from=builder /root/wingmate/cmd/pidproxy/pidproxy /usr/local/bin/wmpidproxy
ADD --chmod=755 docker/alpine/entry.sh /usr/local/bin/entry.sh
ADD --chmod=755 docker/alpine/etc /etc

6
go.mod
View File

@ -3,8 +3,8 @@ module gitea.suyono.dev/suyono/wingmate
go 1.21
require (
github.com/rs/zerolog v1.30.0
github.com/spf13/cobra v1.7.0
github.com/rs/zerolog v1.31.0
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.17.0
github.com/stretchr/testify v1.8.4
golang.org/x/sys v0.15.0
@ -17,7 +17,7 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect

7
go.sum
View File

@ -48,6 +48,7 @@ github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnht
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@ -150,6 +151,8 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
@ -166,6 +169,8 @@ github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZV
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.30.0 h1:SymVODrcRsaRaSInD9yQtKbtWqwsfoPcRff/oRXLj4c=
github.com/rs/zerolog v1.30.0/go.mod h1:/tk+P47gFdPXq4QYjvCmT5/Gsug2nagsFWBWhAiSi1w=
github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A=
github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
@ -183,6 +188,8 @@ github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk=
github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=