diff --git a/cmd/foreman/main.go b/cmd/foreman/main.go index 06ab7d0..d11a8ec 100755 --- a/cmd/foreman/main.go +++ b/cmd/foreman/main.go @@ -1 +1,28 @@ package main + +import ( + "flag" + "fmt" + + "github.com/Eslam-Nawara/foreman" +) + +func main() { + + verbosePtr := flag.Bool("v", false, "run the program verbosely") + procfilePtr := flag.String("f", "Procfile", "specify the procfile path") + flag.Parse() + + f, err := foreman.New(*procfilePtr, *verbosePtr) + fmt.Println(*verbosePtr) + if err != nil { + fmt.Println(err) + return + } + err = f.Start() + if err != nil { + fmt.Println(err) + return + } + +} diff --git a/dump.rdb b/dump.rdb new file mode 100644 index 0000000..861732c Binary files /dev/null and b/dump.rdb differ diff --git a/foreman.go b/foreman.go index acf7959..e6df1d4 100755 --- a/foreman.go +++ b/foreman.go @@ -7,6 +7,8 @@ import ( "os" "os/exec" "os/signal" + "strconv" + "strings" "sync" "syscall" "time" @@ -188,10 +190,34 @@ func (f *Foreman) Exit(exitStatus int) { } func (foreman *Foreman) checkDeps(service parser.Service) error { + for _, dep := range service.Deps { + foreman.servicesMutex.Lock() + depService := foreman.services[dep] + foreman.servicesMutex.Unlock() + + if depService.Process == nil { + return errors.New("Broken dependency") + } + } return nil } func (foreman *Foreman) checkPort(service parser.Service, portType string) error { + var ports []string + switch portType { + case "tcp": + ports = service.Checks.TcpPorts + case "udp": + ports = service.Checks.UdpPorts + } + for _, port := range ports { + cmd := fmt.Sprintf("netstat -lnptu | grep %s | grep %s -m 1 | awk '{print $7}'", portType, port) + out, _ := exec.Command("bash", "-c", cmd).Output() + pid, err := strconv.Atoi(strings.Split(string(out), "/")[0]) + if err != nil || pid != service.Process.Pid { + return err + } + } return nil } diff --git a/go.mod b/go.mod index f53fff4..4cc9e30 100644 --- a/go.mod +++ b/go.mod @@ -2,15 +2,4 @@ module github.com/Eslam-Nawara/foreman go 1.19 -require ( - github.com/shirou/gopsutil v3.21.11+incompatible - gopkg.in/yaml.v3 v3.0.1 -) - -require ( - github.com/go-ole/go-ole v1.2.6 // indirect - github.com/tklauser/go-sysconf v0.3.11 // indirect - github.com/tklauser/numcpus v0.6.0 // indirect - github.com/yusufpapurcu/wmi v1.2.2 // indirect - golang.org/x/sys v0.3.0 // indirect -) +require gopkg.in/yaml.v3 v3.0.1 diff --git a/go.sum b/go.sum index cb11ec8..a62c313 100644 --- a/go.sum +++ b/go.sum @@ -1,17 +1,3 @@ -github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= -github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= -github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI= -github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= -github.com/tklauser/go-sysconf v0.3.11 h1:89WgdJhk5SNwJfu+GKyYveZ4IaJ7xAkecBo+KdJV0CM= -github.com/tklauser/go-sysconf v0.3.11/go.mod h1:GqXfhXY3kiPa0nAXPDIQIWzJbMCB7AmcWpGR8lSZfqI= -github.com/tklauser/numcpus v0.6.0 h1:kebhY2Qt+3U6RNK7UqpYNA+tJ23IBEGKkB7JQBfDYms= -github.com/tklauser/numcpus v0.6.0/go.mod h1:FEZLMke0lhOUG6w2JadTzp0a+Nl8PF/GFkQ5UVIcaL4= -github.com/yusufpapurcu/wmi v1.2.2 h1:KBNDSne4vP5mbSWnJbO+51IMOXJB67QiYCSBrubbPRg= -github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= -golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ= -golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=