mirror of
https://github.com/napnap75/multiarch-docker-images.git
synced 2026-02-05 10:28:59 +01:00
Added the ability to restart a container
This commit is contained in:
@@ -6,11 +6,23 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"time"
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/client"
|
"github.com/docker/docker/client"
|
||||||
"github.com/eclipse/paho.mqtt.golang"
|
"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() {
|
func main() {
|
||||||
// Handle interrupts to clean properly
|
// Handle interrupts to clean properly
|
||||||
c := make(chan os.Signal)
|
c := make(chan os.Signal)
|
||||||
@@ -29,30 +41,31 @@ func main() {
|
|||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
// Connect to the docker socket
|
// Connect to the docker socket
|
||||||
cli, err := client.NewClientWithOpts(client.FromEnv)
|
dockerClient, err := client.NewClientWithOpts(client.FromEnv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Unable to connect to docker: %v\n", err)
|
fmt.Fprintf(os.Stderr, "Unable to connect to docker: %v\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx, _ := context.WithCancel(context.Background())
|
dockerContext, _ := context.WithCancel(context.Background())
|
||||||
|
|
||||||
// Connect to the MQTT server
|
// Connect to the MQTT server
|
||||||
opts := mqtt.NewClientOptions().AddBroker(*mqttServer)
|
opts := mqtt.NewClientOptions().AddBroker(*mqttServer)
|
||||||
client := mqtt.NewClient(opts)
|
mqttClient := mqtt.NewClient(opts)
|
||||||
if token := client.Connect(); token.Wait() && token.Error() != nil {
|
if token := mqttClient.Connect(); token.Wait() && token.Error() != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Unable to connect to docker: %v\n", token.Error())
|
fmt.Fprintf(os.Stderr, "Unable to connect to MQTT: %v\n", token.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
mqttClient.Subscribe(*mqttTopic + "/restart", 1, func(client mqtt.Client, msg mqtt.Message) { restartHandler(client, msg, dockerClient, dockerContext) }).Wait()
|
||||||
|
|
||||||
// Listen for events
|
// Listen for events
|
||||||
msgs, errs := cli.Events(ctx, types.EventsOptions{})
|
msgs, errs := dockerClient.Events(dockerContext, types.EventsOptions{})
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case err := <- errs:
|
case err := <- errs:
|
||||||
fmt.Fprintf(os.Stderr, "Error while listening for docker events: %v\n", err)
|
fmt.Fprintf(os.Stderr, "Error while listening for docker events: %v\n", err)
|
||||||
|
|
||||||
case msg := <-msgs:
|
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