mirror of
https://github.com/napnap75/multiarch-docker-images.git
synced 2025-12-15 03:04:19 +01:00
Added the ability to restart a container
This commit is contained in:
@@ -6,11 +6,23 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"time"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/eclipse/paho.mqtt.golang"
|
||||
)
|
||||
|
||||
func restartHandler(client mqtt.Client, msg mqtt.Message, dockerClient *client.Client, dockerContext context.Context) {
|
||||
fmt.Printf("Received message: %s from topic: %s\n", msg.Payload(), msg.Topic())
|
||||
var timeout = 30*time.Second
|
||||
err := dockerClient.ContainerRestart(dockerContext, string(msg.Payload()), &timeout)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Unable to restart container %s: %v\n", msg.Payload(), err)
|
||||
} else {
|
||||
fmt.Fprintf(os.Stdout, "Container %s restarted\n", msg.Payload())
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Handle interrupts to clean properly
|
||||
c := make(chan os.Signal)
|
||||
@@ -29,30 +41,31 @@ func main() {
|
||||
flag.Parse()
|
||||
|
||||
// Connect to the docker socket
|
||||
cli, err := client.NewClientWithOpts(client.FromEnv)
|
||||
dockerClient, err := client.NewClientWithOpts(client.FromEnv)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Unable to connect to docker: %v\n", err)
|
||||
return
|
||||
}
|
||||
ctx, _ := context.WithCancel(context.Background())
|
||||
dockerContext, _ := context.WithCancel(context.Background())
|
||||
|
||||
// Connect to the MQTT server
|
||||
opts := mqtt.NewClientOptions().AddBroker(*mqttServer)
|
||||
client := mqtt.NewClient(opts)
|
||||
if token := client.Connect(); token.Wait() && token.Error() != nil {
|
||||
fmt.Fprintf(os.Stderr, "Unable to connect to docker: %v\n", token.Error())
|
||||
opts := mqtt.NewClientOptions().AddBroker(*mqttServer)
|
||||
mqttClient := mqtt.NewClient(opts)
|
||||
if token := mqttClient.Connect(); token.Wait() && token.Error() != nil {
|
||||
fmt.Fprintf(os.Stderr, "Unable to connect to MQTT: %v\n", token.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
mqttClient.Subscribe(*mqttTopic + "/restart", 1, func(client mqtt.Client, msg mqtt.Message) { restartHandler(client, msg, dockerClient, dockerContext) }).Wait()
|
||||
|
||||
// Listen for events
|
||||
msgs, errs := cli.Events(ctx, types.EventsOptions{})
|
||||
msgs, errs := dockerClient.Events(dockerContext, types.EventsOptions{})
|
||||
for {
|
||||
select {
|
||||
case err := <- errs:
|
||||
fmt.Fprintf(os.Stderr, "Error while listening for docker events: %v\n", err)
|
||||
|
||||
case msg := <-msgs:
|
||||
client.Publish(*mqttTopic, 0, false, fmt.Sprintf("{ \"time\": %d, \"type\": \"%s\", \"name\": \"%s\", \"action\": \"%s\"}\n", msg.Time, msg.Type, msg.Actor.Attributes["name"], msg.Action))
|
||||
mqttClient.Publish(*mqttTopic + "/events", 0, false, fmt.Sprintf("{ \"time\": %d, \"type\": \"%s\", \"name\": \"%s\", \"action\": \"%s\"}", msg.Time, msg.Type, msg.Actor.Attributes["name"], msg.Action))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user