refactor: extract code from the container package
parent
4130b110c6
commit
d1abce889a
@ -0,0 +1,93 @@
|
|||||||
|
package lifecycle
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/containrrr/watchtower/pkg/container"
|
||||||
|
"github.com/containrrr/watchtower/pkg/types"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ExecutePreChecks tries to run the pre-check lifecycle hook for all containers included by the current filter.
|
||||||
|
func ExecutePreChecks(client container.Client, params types.UpdateParams) {
|
||||||
|
containers, err := client.ListContainers(params.Filter)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, container := range containers {
|
||||||
|
ExecutePreCheckCommand(client, container)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExecutePostChecks tries to run the post-check lifecycle hook for all containers included by the current filter.
|
||||||
|
func ExecutePostChecks(client container.Client, params types.UpdateParams) {
|
||||||
|
containers, err := client.ListContainers(params.Filter)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, container := range containers {
|
||||||
|
ExecutePostCheckCommand(client, container)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExecutePreCheckCommand tries to run the pre-check lifecycle hook for a single container.
|
||||||
|
func ExecutePreCheckCommand(client container.Client, container container.Container) {
|
||||||
|
command := container.GetLifecyclePreCheckCommand()
|
||||||
|
if len(command) == 0 {
|
||||||
|
log.Debug("No pre-check command supplied. Skipping")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Info("Executing pre-check command.")
|
||||||
|
if err := client.ExecuteCommand(container.ID(), command); err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExecutePostCheckCommand tries to run the post-check lifecycle hook for a single container.
|
||||||
|
func ExecutePostCheckCommand(client container.Client, container container.Container) {
|
||||||
|
command := container.GetLifecyclePostCheckCommand()
|
||||||
|
if len(command) == 0 {
|
||||||
|
log.Debug("No post-check command supplied. Skipping")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Info("Executing post-check command.")
|
||||||
|
if err := client.ExecuteCommand(container.ID(), command); err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExecutePreUpdateCommand tries to run the pre-update lifecycle hook for a single container.
|
||||||
|
func ExecutePreUpdateCommand(client container.Client, container container.Container) {
|
||||||
|
|
||||||
|
command := container.GetLifecyclePreUpdateCommand()
|
||||||
|
if len(command) == 0 {
|
||||||
|
log.Debug("No pre-update command supplied. Skipping")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Info("Executing pre-update command.")
|
||||||
|
if err := client.ExecuteCommand(container.ID(), command); err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExecutePostUpdateCommand tries to run the post-update lifecycle hook for a single container.
|
||||||
|
func ExecutePostUpdateCommand(client container.Client, newContainerID string) {
|
||||||
|
newContainer, err := client.GetContainer(newContainerID)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
command := newContainer.GetLifecyclePostUpdateCommand()
|
||||||
|
if len(command) == 0 {
|
||||||
|
log.Debug("No post-update command supplied. Skipping")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Info("Executing post-update command.")
|
||||||
|
if err := client.ExecuteCommand(newContainerID, command); err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,33 @@
|
|||||||
|
package registry
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/docker/docker/api/types"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetPullOptions creates a struct with all options needed for pulling images from a registry
|
||||||
|
func GetPullOptions(imageName string) (types.ImagePullOptions, error) {
|
||||||
|
auth, err := EncodedAuth(imageName)
|
||||||
|
log.Debugf("Got image name: %s", imageName)
|
||||||
|
if err != nil {
|
||||||
|
return types.ImagePullOptions{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debugf("Got auth value: %s", auth)
|
||||||
|
if auth == "" {
|
||||||
|
return types.ImagePullOptions{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return types.ImagePullOptions{
|
||||||
|
RegistryAuth: auth,
|
||||||
|
PrivilegeFunc: DefaultAuthHandler,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DefaultAuthHandler will be invoked if an AuthConfig is rejected
|
||||||
|
// It could be used to return a new value for the "X-Registry-Auth" authentication header,
|
||||||
|
// but there's no point trying again with the same value as used in AuthConfig
|
||||||
|
func DefaultAuthHandler() (string, error) {
|
||||||
|
log.Debug("Authentication request was rejected. Trying again without authentication")
|
||||||
|
return "", nil
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package container
|
package registry
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
@ -1,13 +1,12 @@
|
|||||||
package actions
|
package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
t "github.com/containrrr/watchtower/pkg/types"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// UpdateParams contains all different options available to alter the behavior of the Update func
|
// UpdateParams contains all different options available to alter the behavior of the Update func
|
||||||
type UpdateParams struct {
|
type UpdateParams struct {
|
||||||
Filter t.Filter
|
Filter Filter
|
||||||
Cleanup bool
|
Cleanup bool
|
||||||
NoRestart bool
|
NoRestart bool
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
Loading…
Reference in New Issue