Skip to content

Commit

Permalink
add foreman cmd implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Eslam-Nawara committed Dec 18, 2022
1 parent c5c9f21 commit e7f826d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 26 deletions.
27 changes: 27 additions & 0 deletions cmd/foreman/main.go
Original file line number Diff line number Diff line change
@@ -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
}

}
Binary file added dump.rdb
Binary file not shown.
26 changes: 26 additions & 0 deletions foreman.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
"os/exec"
"os/signal"
"strconv"
"strings"
"sync"
"syscall"
"time"
Expand Down Expand Up @@ -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
}
13 changes: 1 addition & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 0 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down

0 comments on commit e7f826d

Please sign in to comment.