Skip to content

Commit

Permalink
Merge pull request #32 from chaitin/feat(container)/binding
Browse files Browse the repository at this point in the history
feat(container): binding for name and oci state
  • Loading branch information
zhoubinxuan authored Sep 2, 2022
2 parents 6cc30c8 + 805d9f9 commit 90390dc
Show file tree
Hide file tree
Showing 10 changed files with 303 additions and 0 deletions.
17 changes: 17 additions & 0 deletions go/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
package docker

import (
"encoding/json"

api "github.com/chaitin/libveinmind/go"
"github.com/chaitin/libveinmind/go/pkg/behaviour"
"github.com/chaitin/libveinmind/go/pkg/binding"
Expand Down Expand Up @@ -157,6 +159,21 @@ func (c *Container) Runtime() *Docker {
return c.runtime
}

func (c *Container) Config() (*ContainerConfig, error) {
configBytes, err := c.container.DockerContainerConfig()
if err != nil {
return nil, err
}

var config ContainerConfig
err = json.Unmarshal(configBytes, &config)
if err != nil {
return nil, err
}

return &config, nil
}

// Layer represents a containerd layer, which is guaranteed to
// be the result of docker.Image.OpenLayer.
type Layer struct {
Expand Down
117 changes: 117 additions & 0 deletions go/docker/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package docker

import (
"time"
)

// ContainerConfig reference for config.json in docker runtime
type ContainerConfig struct {
State struct {
Running bool `json:"Running"`
Paused bool `json:"Paused"`
Restarting bool `json:"Restarting"`
OOMKilled bool `json:"OOMKilled"`
RemovalInProgress bool `json:"RemovalInProgress"`
Dead bool `json:"Dead"`
Pid int `json:"Pid"`
ExitCode int `json:"ExitCode"`
Error string `json:"Error"`
StartedAt time.Time `json:"StartedAt"`
FinishedAt time.Time `json:"FinishedAt"`
Health interface{} `json:"Health"`
} `json:"State"`
ID string `json:"ID"`
Created time.Time `json:"Created"`
Managed bool `json:"Managed"`
Path string `json:"Path"`
Args []string `json:"Args"`
Config struct {
Hostname string `json:"Hostname"`
Domainname string `json:"Domainname"`
User string `json:"User"`
AttachStdin bool `json:"AttachStdin"`
AttachStdout bool `json:"AttachStdout"`
AttachStderr bool `json:"AttachStderr"`
Tty bool `json:"Tty"`
OpenStdin bool `json:"OpenStdin"`
StdinOnce bool `json:"StdinOnce"`
Env []string `json:"Env"`
Cmd []string `json:"Cmd"`
Image string `json:"Image"`
Volumes map[string]struct{} `json:"Volumes"`
WorkingDir string `json:"WorkingDir"`
Entrypoint []string `json:"Entrypoint"`
OnBuild []string `json:"OnBuild"`
Labels map[string]string `json:"Labels"`
} `json:"Config"`
Image string `json:"Image"`
NetworkSettings struct {
Bridge string `json:"Bridge"`
SandboxID string `json:"SandboxID"`
HairpinMode bool `json:"HairpinMode"`
LinkLocalIPv6Address string `json:"LinkLocalIPv6Address"`
LinkLocalIPv6PrefixLen int `json:"LinkLocalIPv6PrefixLen"`
Networks struct {
Bridge struct {
IPAMConfig interface{} `json:"IPAMConfig"`
Links interface{} `json:"Links"`
Aliases interface{} `json:"Aliases"`
NetworkID string `json:"NetworkID"`
EndpointID string `json:"EndpointID"`
Gateway string `json:"Gateway"`
IPAddress string `json:"IPAddress"`
IPPrefixLen int `json:"IPPrefixLen"`
IPv6Gateway string `json:"IPv6Gateway"`
GlobalIPv6Address string `json:"GlobalIPv6Address"`
GlobalIPv6PrefixLen int `json:"GlobalIPv6PrefixLen"`
MacAddress string `json:"MacAddress"`
DriverOpts interface{} `json:"DriverOpts"`
IPAMOperational bool `json:"IPAMOperational"`
} `json:"bridge"`
} `json:"Networks"`
Service interface{} `json:"Service"`
Ports struct {
} `json:"Ports"`
SandboxKey string `json:"SandboxKey"`
SecondaryIPAddresses interface{} `json:"SecondaryIPAddresses"`
SecondaryIPv6Addresses interface{} `json:"SecondaryIPv6Addresses"`
IsAnonymousEndpoint bool `json:"IsAnonymousEndpoint"`
HasSwarmEndpoint bool `json:"HasSwarmEndpoint"`
} `json:"NetworkSettings"`
LogPath string `json:"LogPath"`
Name string `json:"Name"`
Driver string `json:"Driver"`
Os string `json:"OS"`
MountLabel string `json:"MountLabel"`
ProcessLabel string `json:"ProcessLabel"`
RestartCount int `json:"RestartCount"`
HasBeenStartedBefore bool `json:"HasBeenStartedBefore"`
HasBeenManuallyStopped bool `json:"HasBeenManuallyStopped"`
MountPoints map[string]struct {
Source string `json:"Source"`
Destination string `json:"Destination"`
Rw bool `json:"RW"`
Name string `json:"Name"`
Driver string `json:"Driver"`
Type string `json:"Type"`
Propagation string `json:"Propagation"`
Spec struct {
Type string `json:"Type"`
Source string `json:"Source"`
Target string `json:"Target"`
} `json:"Spec"`
SkipMountpointCreation bool `json:"SkipMountpointCreation"`
} `json:"MountPoints"`
SecretReferences interface{} `json:"SecretReferences"`
ConfigReferences interface{} `json:"ConfigReferences"`
AppArmorProfile string `json:"AppArmorProfile"`
HostnamePath string `json:"HostnamePath"`
HostsPath string `json:"HostsPath"`
ShmPath string `json:"ShmPath"`
ResolvConfPath string `json:"ResolvConfPath"`
SeccompProfile string `json:"SeccompProfile"`
NoNewPrivileges bool `json:"NoNewPrivileges"`
LocalLogCacheMeta struct {
HaveNotifyEnabled bool `json:"HaveNotifyEnabled"`
} `json:"LocalLogCacheMeta"`
}
2 changes: 2 additions & 0 deletions go/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ type Container interface {

Close() error
ID() string
Name() string
ImageID() string

OCISpec() (*specs.Spec, error)
OCIState() (*specs.State, error)
}

// Runtime is the connection established with a specific
Expand Down
16 changes: 16 additions & 0 deletions go/pkg/behaviour/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ func (c *Container) ID() string {
return c.h.ContainerID()
}

func (c *Container) Name() string {
return c.h.ContainerName()
}

func (c *Container) ImageID() string {
return c.h.ContainerImageID()
}
Expand All @@ -90,6 +94,18 @@ func (c *Container) OCISpec() (*specs.Spec, error) {
return result, nil
}

func (c *Container) OCIState() (*specs.State, error) {
bytes, err := c.h.ContainerOCIStateMarshalJSON()
if err != nil {
return nil, err
}
result := &specs.State{}
if err := json.Unmarshal(bytes, result); err != nil {
return nil, err
}
return result, nil
}

func NewContainer(h *binding.Handle) Container {
return Container{h: h}
}
19 changes: 19 additions & 0 deletions go/pkg/behaviour/process.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package behaviour

import (
"time"

api "github.com/chaitin/libveinmind/go"
"github.com/chaitin/libveinmind/go/pkg/binding"
)
Expand Down Expand Up @@ -96,6 +98,10 @@ func (p *Process) Pid() (int32, error) {
return p.h.ProcessPid()
}

func (p *Process) HostPid() (int32, error) {
return p.h.ProcessHostPid()
}

func (p *Process) Ppid() (int32, error) {
return p.h.ProcessPpid()
}
Expand All @@ -104,6 +110,19 @@ func (p *Process) Name() (string, error) {
return p.h.ProcessName()
}

func (p *Process) Status() (string, error) {
return p.h.ProcessStatus()
}

func (p *Process) CreateTime() (time.Time, error) {
timestamp, err := p.h.ProcessCreateTime()
if err != nil {
return time.Time{}, err
}

return time.Unix(0, timestamp*int64(time.Millisecond)), nil
}

func (p *Process) Close() {
p.h.Free()
}
51 changes: 51 additions & 0 deletions go/pkg/binding/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,14 @@ func (h Handle) ProcessPid() (int32, error) {
return int32(result), nil
}

func (h Handle) ProcessHostPid() (int32, error) {
var result C.int32_t
if err := handleError(C.veinmind_ProcessHostPid(&result, h.ID())); err != nil {
return 0, err
}
return int32(result), nil
}

func (h Handle) ProcessPpid() (int32, error) {
var result C.int32_t
if err := handleError(C.veinmind_ProcessPpid(&result, h.ID())); err != nil {
Expand All @@ -324,6 +332,22 @@ func (h Handle) ProcessName() (string, error) {
return result.String(), nil
}

func (h Handle) ProcessStatus() (string, error) {
var result Handle
if err := handleError(C.veinmind_ProcessStatus(result.Ptr(), h.ID())); err != nil {
return "", err
}
return result.String(), nil
}

func (h Handle) ProcessCreateTime() (int64, error) {
var result C.int64_t
if err := handleError(C.veinmind_ProcessCreateTime(&result, h.ID())); err != nil {
return 0, err
}
return int64(result), nil
}

func (h Handle) RuntimeListImageIDs() ([]string, error) {
result := new(Handle)
if err := handleError(C.veinmind_RuntimeListImageIDs(
Expand Down Expand Up @@ -441,6 +465,13 @@ func (h Handle) ContainerID() string {
return str.String()
}

func (h Handle) ContainerName() string {
var str Handle
assertNoError(C.veinmind_ContainerName(str.Ptr(), h.ID()))
defer str.Free()
return str.String()
}

func (h Handle) ContainerOCISpecMarshalJSON() ([]byte, error) {
var result Handle
if err := handleError(C.veinmind_ContainerOCISpecMarshalJSON(
Expand All @@ -451,6 +482,16 @@ func (h Handle) ContainerOCISpecMarshalJSON() ([]byte, error) {
return result.Bytes(), nil
}

func (h Handle) ContainerOCIStateMarshalJSON() ([]byte, error) {
var result Handle
if err := handleError(C.veinmind_ContainerOCIStateMarshalJSON(
result.Ptr(), h.ID())); err != nil {
return nil, err
}
defer result.Free()
return result.Bytes(), nil
}

func DockerMakeNewOptionList() Handle {
var result Handle
assertNoError(C.veinmind_DockerMakeNewOptionList(result.Ptr()))
Expand Down Expand Up @@ -526,6 +567,16 @@ func (h Handle) DockerImageNumLayers() int {
return int(numLayers)
}

func (h Handle) DockerContainerConfig() ([]byte, error) {
var result Handle
if err := handleError(C.veinmind_DockerContainerConfig(
result.Ptr(), h.ID())); err != nil {
return nil, err
}
defer result.Free()
return result.Bytes(), nil
}

func (h Handle) DockerLayerID() string {
var result Handle
assertNoError(C.veinmind_DockerLayerID(result.Ptr(), h.ID()))
Expand Down
7 changes: 7 additions & 0 deletions go/process.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package api

import (
"time"
)

type Process interface {
Close()
Children() ([]Process, error)
Expand All @@ -11,8 +15,11 @@ type Process interface {
Parent() (Process, error)
Ppid() (int32, error)
Pid() (int32, error)
HostPid() (int32, error)
Uids() ([]int32, error)
Name() (string, error)
Status() (string, error)
CreateTime() (time.Time, error)
}

type Psutil interface {
Expand Down
25 changes: 25 additions & 0 deletions python3/veinmind/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ def id(self):
with hstr as hstr:
return hstr.str()

# Retrieve the container Name by its open handle.
_name = binding.lookup(b"veinmind_ContainerName", b"VEINMIND_1.2")
def name(self):
"Retrieve runtime specific container ID."

hstr = binding.Handle()
binding.assert_no_error(Container._name(
hstr.ptr(), self.__handle__().val()))
with hstr as hstr:
return hstr.str()

# Retrieve the image ID associated with the container by its open handle
_image_id = binding.lookup(b"veinmind_ContainerImageID", b"VEINMIND_1.2")
def image_id(self):
Expand All @@ -41,6 +52,20 @@ def ocispec(self):
binding.handle_error(Container._ocispec_marshal_json(
hspec.ptr(), self.__handle__().val()))
hstr = None
with hspec as hspec:
hstr = hspec.bytes_to_str()
with hstr as hstr:
return json.load(io.StringIO(hstr.str()))

# Retrieve the parsed json of runtime OCI State format.
_ocistate_marshal_json = binding.lookup(b"veinmind_ContainerOCIStateMarshalJSON", b"VEINMIND_1.2")
def ocistate(self):
"Retrieve the runtime OCI Specification information."

hspec = binding.Handle()
binding.handle_error(Container._ocistate_marshal_json(
hspec.ptr(), self.__handle__().val()))
hstr = None
with hspec as hspec:
hstr = hspec.bytes_to_str()
with hstr as hstr:
Expand Down
Loading

0 comments on commit 90390dc

Please sign in to comment.