make some progress, switch to golang

This commit is contained in:
Morgan Patterson
2021-10-12 22:49:20 -07:00
parent ba055329bd
commit eac2fd1f51
4 changed files with 94 additions and 10 deletions

View File

@@ -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"]
# 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"]

View File

@@ -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
# - 80:80
labels:
- "com.sandman.marker=sandman"

BIN
sandman Executable file

Binary file not shown.

69
sandman.go Normal file
View File

@@ -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"
}