2021-06-08 23:11:18 +03:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/anuvu/zot/pkg/log"
|
2021-10-28 12:10:01 +03:00
|
|
|
"github.com/anuvu/zot/pkg/storage"
|
|
|
|
guuid "github.com/gofrs/uuid"
|
2021-06-08 23:11:18 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type PostHandler struct {
|
2021-10-28 12:10:01 +03:00
|
|
|
StoreController storage.StoreController
|
|
|
|
Cfg Config
|
|
|
|
Log log.Logger
|
2021-06-08 23:11:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *PostHandler) Handler(w http.ResponseWriter, r *http.Request) {
|
2021-10-28 12:10:01 +03:00
|
|
|
var credentialsFile CredentialsFile
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if h.Cfg.CredentialsFile != "" {
|
|
|
|
credentialsFile, err = getFileCredentials(h.Cfg.CredentialsFile)
|
|
|
|
if err != nil {
|
|
|
|
h.Log.Error().Err(err).Msgf("sync http handler: couldn't get registry credentials from %s", h.Cfg.CredentialsFile)
|
|
|
|
WriteData(w, http.StatusInternalServerError, err.Error())
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
localCtx, policyCtx, err := getLocalContexts(h.Log)
|
2021-06-08 23:11:18 +03:00
|
|
|
if err != nil {
|
|
|
|
WriteData(w, http.StatusInternalServerError, err.Error())
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer policyCtx.Destroy() //nolint: errcheck
|
|
|
|
|
2021-10-28 12:10:01 +03:00
|
|
|
uuid, err := guuid.NewV4()
|
|
|
|
if err != nil {
|
|
|
|
WriteData(w, http.StatusInternalServerError, err.Error())
|
2021-06-08 23:11:18 +03:00
|
|
|
|
2021-10-28 12:10:01 +03:00
|
|
|
return
|
2021-06-08 23:11:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, regCfg := range h.Cfg.Registries {
|
|
|
|
upstreamRegistryName := strings.Replace(strings.Replace(regCfg.URL, "http://", "", 1), "https://", "", 1)
|
|
|
|
|
2021-10-28 12:10:01 +03:00
|
|
|
if err := syncRegistry(regCfg, h.StoreController, h.Log, localCtx, policyCtx,
|
|
|
|
credentialsFile[upstreamRegistryName], uuid.String()); err != nil {
|
|
|
|
h.Log.Err(err).Msg("sync http handler: error while syncing in")
|
2021-06-08 23:11:18 +03:00
|
|
|
WriteData(w, http.StatusInternalServerError, err.Error())
|
2021-10-28 12:10:01 +03:00
|
|
|
|
|
|
|
return
|
2021-06-08 23:11:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WriteData(w, http.StatusOK, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func WriteData(w http.ResponseWriter, status int, msg string) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(status)
|
2021-10-28 12:10:01 +03:00
|
|
|
_, _ = w.Write([]byte(fmt.Sprintf("error: %s", msg)))
|
2021-06-08 23:11:18 +03:00
|
|
|
}
|