| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- package main
- import (
- "context"
- "fmt"
- "github.com/docker/docker/api/types"
- "github.com/docker/docker/client"
- "sync"
- "time"
- )
- // var counter int
- var lock sync.Mutex
- /*
- {
- "<patp>":{
- "status":<container-status>
- }
- }
- */
- var urbitBroadcast = make(map[string]map[string]string)
- func broadcast() {
- for {
- fmt.Println(urbitBroadcast) // send to websocket as json blob
- time.Sleep(250 * time.Millisecond)
- }
- }
- // Example
- func getStatus(i int, patp string) {
- for {
- // Locking the shared state
- lock.Lock()
- // Create submap
- _, exist := urbitBroadcast[patp]
- if !exist {
- urbitBroadcast[patp] = make(map[string]string)
- }
- //Get container running status
- cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
- if err != nil {
- //panic(err)
- fmt.Println(err)
- } else {
- containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{})
- if err != nil {
- urbitBroadcast[patp]["status"] = "error"
- } else {
- for _, container := range containers {
- for _, name := range container.Names {
- fasPatp := "/" + patp
- if name == fasPatp {
- urbitBroadcast[patp]["status"] = container.Status
- }
- }
- }
- }
- }
- // Unlocking the shared state
- lock.Unlock()
- // Arbitary sleep
- time.Sleep(5 * time.Second)
- }
- }
- func main() {
- /*
- Temporary Hardcode -- from config file
- */
- piers := []string{"widwet-mornev-nallux-dozryl",
- "hocfur-wicnym-nallux-dozryl",
- "pinlyd-mattyd-nallux-dozryl",
- "solnys-dibmyn-nallux-dozryl",
- "wormun-fadwyl-nallux-dozryl",
- "nopmul-pollyt",
- "nalruc-nallux-dozryl",
- }
- go broadcast()
- for i, patp := range piers {
- go getStatus(i, patp)
- }
- // Waiting for the goroutines to complete
- var input string
- fmt.Scanln(&input)
- }
|