mirror of
https://github.com/containrrr/watchtower.git
synced 2026-01-17 14:35:28 +01:00
feat(api): add updates API v2
This commit is contained in:
parent
47091761a5
commit
4a922f5a32
4 changed files with 73 additions and 33 deletions
|
|
@ -113,6 +113,8 @@ func (api *API) Handler() http.Handler {
|
|||
// EnableUpdates registers the `updates` endpoints
|
||||
func (api *API) EnableUpdates(f updates.InvokedFunc, updateLock *sync.Mutex) {
|
||||
api.route("/v1/updates").post(updates.PostV1(f, updateLock))
|
||||
api.route("/v2/updates/apply").post(updates.PostV2Apply(f, updateLock))
|
||||
api.route("/v2/updates/check").post(updates.PostV2Check(f, updateLock))
|
||||
}
|
||||
|
||||
// EnableMetrics registers the `metrics` endpoints
|
||||
|
|
|
|||
37
pkg/api/updates/updates_v2.go
Normal file
37
pkg/api/updates/updates_v2.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package updates
|
||||
|
||||
import (
|
||||
. "github.com/containrrr/watchtower/pkg/api/prelude"
|
||||
"github.com/containrrr/watchtower/pkg/filters"
|
||||
"github.com/containrrr/watchtower/pkg/types"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func postV2(onInvoke InvokedFunc, updateLock *sync.Mutex, monitorOnly bool) HandlerFunc {
|
||||
return func(c *Context) Response {
|
||||
log.Info("Updates triggered by HTTP API request.")
|
||||
|
||||
images := parseImages(c.Request.URL)
|
||||
|
||||
if updateLock.TryLock() {
|
||||
defer updateLock.Unlock()
|
||||
|
||||
result := onInvoke(func(up *types.UpdateParams) {
|
||||
up.Filter = filters.FilterByImage(images, up.Filter)
|
||||
up.MonitorOnly = monitorOnly
|
||||
})
|
||||
return OK(result)
|
||||
} else {
|
||||
return Error(ErrUpdateRunning)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func PostV2Check(onInvoke InvokedFunc, updateLock *sync.Mutex) HandlerFunc {
|
||||
return postV2(onInvoke, updateLock, true)
|
||||
}
|
||||
|
||||
func PostV2Apply(onInvoke InvokedFunc, updateLock *sync.Mutex) HandlerFunc {
|
||||
return postV2(onInvoke, updateLock, false)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue