Add spider-net and auto docker stuff
This commit is contained in:
parent
846016166e
commit
ca68c88fa9
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
*.zip
|
*.zip
|
||||||
.vscode
|
.vscode
|
||||||
|
.idea
|
||||||
|
15
docker-compose.yml
Normal file
15
docker-compose.yml
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
services:
|
||||||
|
spider-net:
|
||||||
|
build: spider-net
|
||||||
|
network_mode: host
|
||||||
|
restart: unless-stopped
|
||||||
|
environment:
|
||||||
|
SPIDER_HOSTNAME: spider
|
||||||
|
|
||||||
|
docker-socat:
|
||||||
|
build: docker-socat
|
||||||
|
restart: unless-stopped
|
||||||
|
ports:
|
||||||
|
- "2345:2375/tcp"
|
||||||
|
volumes:
|
||||||
|
- /var/run/docker.sock:/var/run/docker.sock
|
3
docker-socat/Dockerfile
Normal file
3
docker-socat/Dockerfile
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
FROM alpine:3.20
|
||||||
|
RUN apk add --no-cache socat
|
||||||
|
CMD ["/usr/bin/socat","tcp-listen:2375,reuseaddr,fork","unix:/var/run/docker.sock"]
|
3
docker.sh
Executable file
3
docker.sh
Executable file
@ -0,0 +1,3 @@
|
|||||||
|
export DOCKER_HOST=`lua get-ip.lua spider`:2345
|
||||||
|
echo Docker host is $DOCKER_HOST
|
||||||
|
docker $@
|
42
get-ip.lua
Normal file
42
get-ip.lua
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package.path = package.path .. ';./controller-host/?/init.lua;./controller-host/?.lua'
|
||||||
|
local mqtt = require("mqtt")
|
||||||
|
local client
|
||||||
|
|
||||||
|
local host = ...
|
||||||
|
|
||||||
|
function printTable(table, indentation)
|
||||||
|
indentation = indentation or ""
|
||||||
|
for name, value in pairs(table) do
|
||||||
|
print(indentation .. tostring(name) .. ": " .. tostring(value))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function onMessage(data)
|
||||||
|
print(data.payload)
|
||||||
|
os.exit()
|
||||||
|
end
|
||||||
|
|
||||||
|
local function onConnect(connack)
|
||||||
|
if connack.rc ~= 0 then
|
||||||
|
print("Connection to broker failed:", connack:reason_string())
|
||||||
|
os.exit(1)
|
||||||
|
end
|
||||||
|
|
||||||
|
assert(client:subscribe{
|
||||||
|
topic = "host/" .. host
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
client = mqtt.client {
|
||||||
|
uri = "mqtt.seeseepuff.be",
|
||||||
|
username = "tools",
|
||||||
|
clean = true,
|
||||||
|
reconnect = 5,
|
||||||
|
}
|
||||||
|
|
||||||
|
client:on {
|
||||||
|
connect = onConnect,
|
||||||
|
message = onMessage,
|
||||||
|
}
|
||||||
|
|
||||||
|
mqtt.run_ioloop(client)
|
1
spider-net/.gitignore
vendored
Normal file
1
spider-net/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
spider-net
|
13
spider-net/Dockerfile
Normal file
13
spider-net/Dockerfile
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
FROM golang:1.22.5
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
#COPY go.mod go.sum ./
|
||||||
|
#RUN go mod download
|
||||||
|
|
||||||
|
#COPY *.go ./
|
||||||
|
#RUN CGO_ENABLED=0 GOOS=linux go build -o /spider-net
|
||||||
|
|
||||||
|
COPY spider-net /spider-net
|
||||||
|
|
||||||
|
CMD ["/spider-net"]
|
3
spider-net/build_and_upload.sh
Executable file
3
spider-net/build_and_upload.sh
Executable file
@ -0,0 +1,3 @@
|
|||||||
|
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o spider-net
|
||||||
|
cd ..
|
||||||
|
./docker.sh compose up spider-net -d --build
|
10
spider-net/go.mod
Normal file
10
spider-net/go.mod
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
module spider-mqtt
|
||||||
|
|
||||||
|
go 1.22
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/eclipse/paho.mqtt.golang v1.4.3 // indirect
|
||||||
|
github.com/gorilla/websocket v1.5.0 // indirect
|
||||||
|
golang.org/x/net v0.8.0 // indirect
|
||||||
|
golang.org/x/sync v0.1.0 // indirect
|
||||||
|
)
|
8
spider-net/go.sum
Normal file
8
spider-net/go.sum
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
github.com/eclipse/paho.mqtt.golang v1.4.3 h1:2kwcUGn8seMUfWndX0hGbvH8r7crgcJguQNCyp70xik=
|
||||||
|
github.com/eclipse/paho.mqtt.golang v1.4.3/go.mod h1:CSYvoAlsMkhYOXh/oKyxa8EcBci6dVkLCbo5tTC1RIE=
|
||||||
|
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
|
||||||
|
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
|
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
|
||||||
|
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
|
||||||
|
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
|
||||||
|
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
68
spider-net/main.go
Normal file
68
spider-net/main.go
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getLocalIP() (string, error) {
|
||||||
|
addrs, err := net.InterfaceAddrs()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
for _, addr := range addrs {
|
||||||
|
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
||||||
|
if ipnet.IP.To4() != nil {
|
||||||
|
return ipnet.IP.String(), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", fmt.Errorf("no IP address found")
|
||||||
|
}
|
||||||
|
|
||||||
|
func publishIp(client mqtt.Client, topic string) {
|
||||||
|
log.Printf("Publishing IP")
|
||||||
|
ip, err := getLocalIP()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error getting IP address: %v\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
token := client.Publish(topic, 0, true, ip)
|
||||||
|
token.Wait()
|
||||||
|
if token.Error() != nil {
|
||||||
|
log.Printf("Error publishing IP address: %v\n", token.Error())
|
||||||
|
} else {
|
||||||
|
log.Printf("Published IP address: %s\n", ip)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
broker := "tcp://mqtt.seeseepuff.be:1883"
|
||||||
|
topic := "host/" + os.Getenv("SPIDER_HOSTNAME")
|
||||||
|
|
||||||
|
opts := mqtt.NewClientOptions()
|
||||||
|
opts.AddBroker(broker)
|
||||||
|
opts.SetClientID("spider-host-client")
|
||||||
|
client := mqtt.NewClient(opts)
|
||||||
|
|
||||||
|
token := client.Connect()
|
||||||
|
token.Wait()
|
||||||
|
if token.Error() != nil {
|
||||||
|
log.Fatalf("Error connecting to MQTT broker: %v\n", token.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
ticker := time.NewTicker(60 * time.Second)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
publishIp(client, topic)
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ticker.C:
|
||||||
|
publishIp(client, topic)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user