diff --git a/Dockerfile b/Dockerfile index 750068d..67ff0b7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,15 @@ -from alpine -RUN apk update && apk add --no-cache docker-cli -COPY ./get_stats.sh /get_stats.sh +# from alpine +# RUN apk update && apk add --no-cache docker-cli +# COPY ./get_stats.sh /get_stats.sh -CMD [ "/bin/sh", "/get_stats.sh" ] -# ENTRYPOINT ["tail", "-f", "/dev/null"] \ No newline at end of file +# CMD [ "/bin/sh", "/get_stats.sh" ] +# # ENTRYPOINT ["tail", "-f", "/dev/null"] + +#### old sad sh version above here +from golang:alpine3.14 + +WORKDIR /go/src/app +COPY ./sandman.go . +RUN go build sandman.go + +CMD ["./sandman"] \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index c64eeb6..0140e13 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -5,11 +5,15 @@ services: build: . # image: sandman environment: - - PORT=81 + - PORT=81 + - LABEL=sandman ports: - - 81:81 + - 81:81 + volumes: + - /var/run/docker.sock:/var/run/docker.sock:ro + # command: tail -F asdf - command: /bin/sh /get_stats.sh + # command: /bin/sh /get_stats.sh whoami2: container_name: whoami2 @@ -17,6 +21,8 @@ services: command: --port 81 network_mode: service:whoami depends_on: - - whoami + - whoami # ports: - # - 80:80 \ No newline at end of file + # - 80:80 + labels: + - "com.sandman.marker=sandman" \ No newline at end of file diff --git a/sandman b/sandman new file mode 100755 index 0000000..1e88a9c Binary files /dev/null and b/sandman differ diff --git a/sandman.go b/sandman.go new file mode 100644 index 0000000..949f905 --- /dev/null +++ b/sandman.go @@ -0,0 +1,69 @@ +package main + +import ( + "fmt" + "os" + "os/exec" + "strconv" + "time" +) + +func main() { + label := os.Getenv("LABEL") + // port := os.Getenv("PORT") + inactive_seconds := 0 + host := true // true will represent on and false will represent off + + for { + // if the host is turned on + if host { + out, err := exec.Command("/bin/sh", "-c", "netstat -n | grep ESTABLISHED | awk '{ print $4 }' | rev | cut -d: -f1| rev | grep -w \"$PORT\" | wc -l").Output() + if err != nil { + fmt.Println(err.Error()) + return + } + fmt.Println("active clients: ", string(out)) + active_clients, _ := strconv.Atoi(string(out)) + if active_clients == 0 { + inactive_seconds++ + } + println(inactive_seconds, "seconds without an active client") + if inactive_seconds > 10 { + stop_containers(label) + host = false + } + } + + if !host { + println("time to check net stats for attempted connections") + // do more checking here + // check around in here to make sure the host doesn't magically restart and we have to deal with those again + // really we should probably be checking that all the time anyways + // out, err := exec.Command("/bin/sh", "-c", "netstat -n | grep ESTABLISHED | awk '{ print $4 }' | rev | cut -d: -f1| rev | grep -w \"$PORT\" | wc -l").Output() + + } + + time.Sleep(time.Second) + } + + fmt.Println("hello world") + // cmd := exec.Command(app, arg0, arg1, arg2, arg3) + // stdout, err := cmd.Output() + + // if err != nil { + // fmt.Println(err.Error()) + // return + // } + + // Print the output + // fmt.Println(string(stdout)) +} + +func stop_containers(label string) { + println("stopping contianer") + +} + +func start_containers(label string) { + // docker ps -a --no-trunc --filter label="com.sandman.marker=sandman" +}