forked from Ivasoft/github-actions
Fix container ID detection also in cgroup v2
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
37
plugin.go
37
plugin.go
@@ -1,22 +1,21 @@
|
|||||||
package plugin
|
package plugin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
|
||||||
"bufio"
|
|
||||||
"path"
|
|
||||||
"context"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/drone-plugins/drone-github-actions/daemon"
|
"github.com/drone-plugins/drone-github-actions/daemon"
|
||||||
"github.com/drone-plugins/drone-github-actions/utils"
|
"github.com/drone-plugins/drone-github-actions/utils"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
"github.com/opencontainers/selinux/go-selinux"
|
|
||||||
docker "github.com/docker/docker/client"
|
docker "github.com/docker/docker/client"
|
||||||
|
"github.com/opencontainers/selinux/go-selinux"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -106,7 +105,7 @@ func (p Plugin) Exec() error {
|
|||||||
if selinux.GetEnabled() {
|
if selinux.GetEnabled() {
|
||||||
bindModifiers = ":z"
|
bindModifiers = ":z"
|
||||||
}
|
}
|
||||||
cmdArgs = append(cmdArgs, "--container-options", fmt.Sprintf("--volume=%s:%s%s", hostWorkDirPath, guestWorkDirPath, bindModifiers))
|
cmdArgs = append(cmdArgs, "--container-options", fmt.Sprintf("--volume=%s:%s%s", hostWorkDirPath, guestWorkDirPath, bindModifiers))
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := exec.Command("act", cmdArgs...)
|
cmd := exec.Command("act", cmdArgs...)
|
||||||
@@ -129,7 +128,7 @@ func trace(cmd *exec.Cmd) {
|
|||||||
|
|
||||||
func getWorkDirPath(p Plugin, ctx context.Context) (string, string, error) {
|
func getWorkDirPath(p Plugin, ctx context.Context) (string, string, error) {
|
||||||
// Connect to the docker
|
// Connect to the docker
|
||||||
docker, err := docker.NewClient("unix://" + daemon.StdDockerSocketPath, "v1.25", nil, nil)
|
docker, err := docker.NewClient("unix://"+daemon.StdDockerSocketPath, "v1.25", nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", errors.Wrap(err, "failed to create docker client")
|
return "", "", errors.Wrap(err, "failed to create docker client")
|
||||||
}
|
}
|
||||||
@@ -139,7 +138,7 @@ func getWorkDirPath(p Plugin, ctx context.Context) (string, string, error) {
|
|||||||
cntrId, err := getContainerId()
|
cntrId, err := getContainerId()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", errors.Wrap(err, "failed to get our container id")
|
return "", "", errors.Wrap(err, "failed to get our container id")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find the proper mount
|
// Find the proper mount
|
||||||
cwd, err := os.Getwd()
|
cwd, err := os.Getwd()
|
||||||
@@ -150,7 +149,7 @@ func getWorkDirPath(p Plugin, ctx context.Context) (string, string, error) {
|
|||||||
cntr, err := docker.ContainerInspect(ctx, cntrId)
|
cntr, err := docker.ContainerInspect(ctx, cntrId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", errors.Wrap(err, "failed to inspect ourselves as a container")
|
return "", "", errors.Wrap(err, "failed to inspect ourselves as a container")
|
||||||
}
|
}
|
||||||
for _, i := range cntr.Mounts {
|
for _, i := range cntr.Mounts {
|
||||||
if i.Destination == cwd {
|
if i.Destination == cwd {
|
||||||
return i.Source, i.Destination, nil
|
return i.Source, i.Destination, nil
|
||||||
@@ -161,7 +160,7 @@ func getWorkDirPath(p Plugin, ctx context.Context) (string, string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getContainerId() (string, error) {
|
func getContainerId() (string, error) {
|
||||||
file, err := os.Open("/proc/self/cgroup")
|
file, err := os.Open("/proc/self/mountinfo")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@@ -170,17 +169,23 @@ func getContainerId() (string, error) {
|
|||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
// The file name is the container identifier
|
// The file name is the container identifier
|
||||||
// Example: 12:rdma:/docker/a33940bf95ec6c57b6d79a3edfa784a1364ce840fb92c9a7f3951b9f7b0ccccb/docker/330d9b057fdd54b224cd38daf370524f7e8c567ccf95f33a2388f02c0b854982
|
// Example: 467 457 0:33 /var/lib/docker/containers/3005e3555a74d3196ea35c5a1ae54fe35a49d55102299208df8dc275b5e9309c/hostname /etc/hostname rw,noatime - btrfs /dev/sda3 rw,space_cache=v2,subvolid=5,subvol=/
|
||||||
|
// Example: 700 678 8:3 /var/lib/docker/containers/d012398a817e7c659f892bfdda00dd2a416a0443da0b31a4c86567ae4b77a6a7/hostname /etc/hostname rw,noatime - ext4 /dev/sda3 rw,noacl
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
pathOffset := strings.LastIndex(line, ":")
|
const prefix = "/docker/containers/"
|
||||||
|
pathOffset := strings.Index(line, prefix)
|
||||||
if pathOffset == -1 {
|
if pathOffset == -1 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
cgroup := line[pathOffset + 1:]
|
postPrefix := line[pathOffset+len(prefix):]
|
||||||
|
endOffset := strings.Index(postPrefix, "/")
|
||||||
|
if endOffset == -1 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
result := path.Base(cgroup)
|
result := postPrefix[:endOffset]
|
||||||
if result == "." || result == "/" {
|
if result == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -188,4 +193,4 @@ func getContainerId() (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return "", errors.New("no suitable cgroup entry found")
|
return "", errors.New("no suitable cgroup entry found")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user