preparations for soft deprecation of legacy notification args (#1377)

Co-authored-by: Simon Aronsson <simme@arcticbit.se>
This commit is contained in:
nils måsén 2022-11-01 00:00:00 +01:00 committed by GitHub
parent 2102a056de
commit cb555f539d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 505 additions and 167 deletions

View file

@ -0,0 +1,29 @@
package container
import (
"fmt"
"os"
"regexp"
"github.com/containrrr/watchtower/pkg/types"
)
var dockerContainerPattern = regexp.MustCompile(`[0-9]+:.*:/docker/([a-f|0-9]{64})`)
// GetRunningContainerID tries to resolve the current container ID from the current process cgroup information
func GetRunningContainerID() (cid types.ContainerID, err error) {
file, err := os.ReadFile(fmt.Sprintf("/proc/%d/cgroup", os.Getpid()))
if err != nil {
return
}
return getRunningContainerIDFromString(string(file)), nil
}
func getRunningContainerIDFromString(s string) types.ContainerID {
matches := dockerContainerPattern.FindStringSubmatch(s)
if len(matches) < 2 {
return ""
}
return types.ContainerID(matches[1])
}