Skip to content

Instantly share code, notes, and snippets.

@maxux
Created March 12, 2020 11:49
Show Gist options
  • Save maxux/f32646c41a8591b223deae177d9a0217 to your computer and use it in GitHub Desktop.
Save maxux/f32646c41a8591b223deae177d9a0217 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"context"
"fmt"
"io"
"sync"
// "github.com/threefoldtech/zos/pkg"
// "os"
// "path"
// "github.com/threefoldtech/zos/pkg/storage"
"github.com/containerd/containerd"
"github.com/containerd/containerd/cio"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/oci"
"github.com/containerd/containerd/runtime/restart"
)
func main() {
id, err := run()
fmt.Println(id)
fmt.Println(err)
}
func run() (string, error) {
ns := "debug"
id := "ab12cc"
client, err := containerd.New("/var/run/docker/containerd/containerd.sock")
if err != nil {
return id, err
}
defer client.Close()
ctx := namespaces.WithNamespace(context.Background(), ns)
l, err := client.Containers(ctx, "")
if err != nil {
return id, nil
}
for _, container := range l {
fmt.Println(container)
if err := container.Update(ctx, restart.WithNoRestarts); err != nil {
return id, err
}
task, err := container.Task(ctx, nil)
if err != nil {
return id, err
}
task.Delete(ctx)
err = container.Delete(ctx)
if err != nil {
return id, err
}
}
opts := []oci.SpecOpts{
oci.WithDefaultSpecForPlatform("linux/amd64"),
oci.WithRootFSPath("/mnt/storage/tmp/jimberoot"),
// oci.WithEnv(),
oci.WithHostResolvconf,
// removeRunMount(),
// withNetworkNamespace(data.Network.Namespace),
// withMounts(data.Mounts),
// WithMemoryLimit(data.Memory),
// WithCPUCount(data.CPU),
oci.WithProcessCwd("/mnt/storage/tmp/jimberoot"),
oci.WithProcessArgs("/bin/chmod"),
}
fmt.Println("create")
container, err := client.NewContainer(
ctx,
"debugtest-2",
containerd.WithNewSpec(opts...),
// this ensure that the container/task will be restarted automatically
// if it gets killed for whatever reason (mostly OOM killer)
// restart.WithStatus(containerd.Running),
)
if err != nil {
return id, err
}
spec, err := container.Spec(ctx)
if err != nil {
return id, err
}
fmt.Printf("args %+v\n", spec.Process.Args)
fmt.Printf("env %+v\n", spec.Process.Env)
fmt.Printf("root %+v\n", spec.Root)
for _, linxNS := range spec.Linux.Namespaces {
fmt.Printf("namespace %+v\n", linxNS.Type)
}
fmt.Printf("mounts %+v\n", spec.Mounts)
//
//
//
fifos, err := cio.NewFIFOSetInDir("", "", false)
if err != nil {
return id, err
}
direct, err := cio.NewDirectIO(ctx, fifos)
if err != nil {
return id, err
}
defer direct.Close()
var (
wg sync.WaitGroup
buf = bytes.NewBuffer(nil)
)
wg.Add(1)
go func() {
defer wg.Done()
io.Copy(buf, direct.Stdout)
fmt.Println(buf)
io.Copy(buf, direct.Stderr)
fmt.Println(buf)
}()
task, err := container.NewTask(ctx, func(id string) (cio.IO, error) { return direct, nil })
if err != nil {
return id, err
}
//
//
//
// call start on the task to execute the redis server
if err := task.Start(ctx); err != nil {
return id, err
}
fmt.Println("waiting")
// wait for the task to exit and get the exit status
status, err := task.Wait(ctx)
fmt.Println(status)
fmt.Println(err)
return container.ID(), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment