89 lines
2.4 KiB
Go
89 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/exec"
|
|
|
|
"github.com/docker/docker/client"
|
|
)
|
|
|
|
func main() {
|
|
secret := os.Getenv("WEBHOOK_SECRET")
|
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
|
if err != nil {
|
|
log.Fatal("failed to create docker client:", err)
|
|
}
|
|
defer cli.Close()
|
|
|
|
http.HandleFunc("/deploy", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
if r.Header.Get("X-Webhook-Secret") != secret {
|
|
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
// имя контейнера берём из query параметра
|
|
containerName := r.URL.Query().Get("container")
|
|
if containerName == "" {
|
|
http.Error(w, "container parameter is required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
// получаем инфо о контейнере чтобы узнать имя образа
|
|
inspect, err := cli.ContainerInspect(ctx, containerName)
|
|
if err != nil {
|
|
log.Printf("failed to inspect container %s: %v", containerName, err)
|
|
http.Error(w, "container not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
imageName := inspect.Config.Image
|
|
log.Printf("Container: %s, Image: %s", containerName, imageName)
|
|
|
|
// тянем новый образ
|
|
log.Println("Pulling new image...")
|
|
pull := exec.Command("docker", "pull", imageName)
|
|
pull.Stdout = os.Stdout
|
|
pull.Stderr = os.Stderr
|
|
if err := pull.Run(); err != nil {
|
|
log.Println("pull failed:", err)
|
|
http.Error(w, "pull failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
// перезапускаем контейнер
|
|
log.Printf("Restarting container %s...", containerName)
|
|
restart := exec.Command("docker", "restart", containerName)
|
|
restart.Stdout = os.Stdout
|
|
restart.Stderr = os.Stderr
|
|
if err := restart.Run(); err != nil {
|
|
log.Println("restart failed:", err)
|
|
http.Error(w, "restart failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
log.Printf("Deploy of %s completed", containerName)
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
json.NewEncoder(w).Encode(map[string]string{
|
|
"status": "ok",
|
|
"container": containerName,
|
|
"image": imageName,
|
|
})
|
|
})
|
|
|
|
log.Println("Webhook server listening on :9001")
|
|
log.Fatal(http.ListenAndServe(":9001", nil))
|
|
}
|