Add json storage for container storage
This is just a temporary storage solution to get containers running on the new code. Signed-off-by: Derek McGowan <derek@mcgstyle.net>
This commit is contained in:
parent
539742881d
commit
e1ed4a2ea4
@ -3,6 +3,9 @@ package main
|
|||||||
import (
|
import (
|
||||||
gocontext "context"
|
gocontext "context"
|
||||||
|
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/codes"
|
||||||
|
|
||||||
containersapi "github.com/containerd/containerd/api/services/containers"
|
containersapi "github.com/containerd/containerd/api/services/containers"
|
||||||
"github.com/containerd/containerd/api/services/execution"
|
"github.com/containerd/containerd/api/services/execution"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@ -43,7 +46,11 @@ var deleteCommand = cli.Command{
|
|||||||
ContainerID: id,
|
ContainerID: id,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "failed to delete container")
|
// Ignore error if task has already been removed, task is
|
||||||
|
// removed by default after run
|
||||||
|
if grpc.Code(errors.Cause(err)) != codes.NotFound {
|
||||||
|
return errors.Wrap(err, "failed to task container")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := snapshotter.Remove(ctx, id); err != nil {
|
if err := snapshotter.Remove(ctx, id); err != nil {
|
||||||
|
@ -65,18 +65,23 @@ var listCommand = cli.Command{
|
|||||||
w := tabwriter.NewWriter(os.Stdout, 10, 1, 3, ' ', 0)
|
w := tabwriter.NewWriter(os.Stdout, 10, 1, 3, ' ', 0)
|
||||||
fmt.Fprintln(w, "ID\tIMAGE\tPID\tSTATUS")
|
fmt.Fprintln(w, "ID\tIMAGE\tPID\tSTATUS")
|
||||||
for _, c := range response.Containers {
|
for _, c := range response.Containers {
|
||||||
var status string
|
var (
|
||||||
|
status string
|
||||||
|
pid uint32
|
||||||
|
)
|
||||||
task, ok := tasksByContainerID[c.ID]
|
task, ok := tasksByContainerID[c.ID]
|
||||||
if ok {
|
if ok {
|
||||||
status = task.Status.String()
|
status = task.Status.String()
|
||||||
|
pid = task.Pid
|
||||||
} else {
|
} else {
|
||||||
status = "STOPPED" // TODO(stevvooe): Is this assumption correct?
|
status = "STOPPED" // TODO(stevvooe): Is this assumption correct?
|
||||||
|
pid = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := fmt.Fprintf(w, "%s\t%s\t%d\t%s\n",
|
if _, err := fmt.Fprintf(w, "%s\t%s\t%d\t%s\n",
|
||||||
c.ID,
|
c.ID,
|
||||||
c.Image,
|
c.Image,
|
||||||
task.Pid,
|
pid,
|
||||||
status,
|
status,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
|
|
||||||
"github.com/Sirupsen/logrus"
|
"github.com/Sirupsen/logrus"
|
||||||
"github.com/containerd/console"
|
"github.com/containerd/console"
|
||||||
|
containersapi "github.com/containerd/containerd/api/services/containers"
|
||||||
"github.com/containerd/containerd/api/services/execution"
|
"github.com/containerd/containerd/api/services/execution"
|
||||||
"github.com/containerd/containerd/images"
|
"github.com/containerd/containerd/images"
|
||||||
"github.com/containerd/containerd/mount"
|
"github.com/containerd/containerd/mount"
|
||||||
@ -294,6 +295,11 @@ var runCommand = cli.Command{
|
|||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if context.Bool("rm") {
|
||||||
|
if _, err := containers.Delete(ctx, &containersapi.DeleteContainerRequest{ID: id}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
if status != 0 {
|
if status != 0 {
|
||||||
return cli.NewExitError("", int(status))
|
return cli.NewExitError("", int(status))
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package containers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
"github.com/boltdb/bolt"
|
"github.com/boltdb/bolt"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@ -37,21 +38,109 @@ func NewStore(tx *bolt.Tx) Store {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *storage) Get(ctx context.Context, id string) (Container, error) {
|
func (s *storage) Get(ctx context.Context, id string) (Container, error) {
|
||||||
panic("not implemented")
|
bkt := s.bucket()
|
||||||
|
if bkt == nil {
|
||||||
|
return Container{}, errors.Wrap(ErrNotFound, "bucket does not exist")
|
||||||
|
}
|
||||||
|
|
||||||
|
cb := bkt.Get([]byte(id))
|
||||||
|
if len(cb) == 0 {
|
||||||
|
return Container{}, errors.Wrap(ErrNotFound, "no content for id")
|
||||||
|
}
|
||||||
|
|
||||||
|
var container Container
|
||||||
|
if err := json.Unmarshal(cb, &container); err != nil {
|
||||||
|
return Container{}, errors.Wrap(err, "failed to unmarshal container")
|
||||||
|
}
|
||||||
|
|
||||||
|
return container, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *storage) List(ctx context.Context, filter string) ([]Container, error) {
|
func (s *storage) List(ctx context.Context, filter string) ([]Container, error) {
|
||||||
panic("not implemented")
|
containers := []Container{}
|
||||||
|
bkt := s.bucket()
|
||||||
|
if bkt == nil {
|
||||||
|
return containers, nil
|
||||||
|
}
|
||||||
|
err := bkt.ForEach(func(k, v []byte) error {
|
||||||
|
container := Container{ID: string(k)}
|
||||||
|
if err := json.Unmarshal(v, &container); err != nil {
|
||||||
|
return errors.Wrap(err, "failed to unmarshal container")
|
||||||
|
}
|
||||||
|
containers = append(containers, container)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return containers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *storage) Create(ctx context.Context, container Container) (Container, error) {
|
func (s *storage) Create(ctx context.Context, container Container) (Container, error) {
|
||||||
panic("not implemented")
|
bkt, err := s.createBucket()
|
||||||
|
if err != nil {
|
||||||
|
return Container{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
b := bkt.Get([]byte(container.ID))
|
||||||
|
if len(b) > 0 {
|
||||||
|
return Container{}, errors.Wrap(ErrExists, "content for id already exists")
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err = json.Marshal(container)
|
||||||
|
if err != nil {
|
||||||
|
return Container{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return container, bkt.Put([]byte(container.ID), b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *storage) Update(ctx context.Context, container Container) (Container, error) {
|
func (s *storage) Update(ctx context.Context, container Container) (Container, error) {
|
||||||
panic("not implemented")
|
bkt, err := s.createBucket()
|
||||||
|
if err != nil {
|
||||||
|
return Container{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
b := bkt.Get([]byte(container.ID))
|
||||||
|
if len(b) == 0 {
|
||||||
|
return Container{}, errors.Wrap(ErrNotFound, "no content for id")
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err = json.Marshal(container)
|
||||||
|
if err != nil {
|
||||||
|
return Container{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return container, bkt.Put([]byte(container.ID), b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *storage) Delete(ctx context.Context, id string) error {
|
func (s *storage) Delete(ctx context.Context, id string) error {
|
||||||
panic("not implemented")
|
bkt, err := s.createBucket()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
b := bkt.Get([]byte(id))
|
||||||
|
if len(b) == 0 {
|
||||||
|
return errors.Wrap(ErrNotFound, "no content for id")
|
||||||
|
}
|
||||||
|
|
||||||
|
return bkt.Delete([]byte(id))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *storage) bucket() *bolt.Bucket {
|
||||||
|
bkt := s.tx.Bucket(bucketKeyStorageVersion)
|
||||||
|
if bkt == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return bkt.Bucket(bucketKeyContainers)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *storage) createBucket() (*bolt.Bucket, error) {
|
||||||
|
bkt, err := s.tx.CreateBucketIfNotExists(bucketKeyStorageVersion)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return bkt.CreateBucketIfNotExists(bucketKeyContainers)
|
||||||
}
|
}
|
||||||
|
@ -128,7 +128,7 @@ func (r *Runtime) Create(ctx context.Context, id string, opts plugin.CreateOpts)
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
c := newTask(id, opts.Spec, s)
|
c := newTask(id, opts.Spec, s)
|
||||||
// after the task is create add it to the monitor
|
// after the task is created, add it to the monitor
|
||||||
if err = r.monitor.Monitor(c); err != nil {
|
if err = r.monitor.Monitor(c); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -144,7 +144,7 @@ func (s *Service) Create(ctx context.Context, r *api.CreateRequest) (*api.Create
|
|||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
if _, ok := s.tasks[r.ContainerID]; ok {
|
if _, ok := s.tasks[r.ContainerID]; ok {
|
||||||
s.mu.Unlock()
|
s.mu.Unlock()
|
||||||
return nil, plugin.ErrContainerExists
|
return nil, grpc.Errorf(codes.AlreadyExists, "task %v already exists", r.ContainerID)
|
||||||
}
|
}
|
||||||
c, err := runtime.Create(ctx, r.ContainerID, opts)
|
c, err := runtime.Create(ctx, r.ContainerID, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -457,7 +457,7 @@ func (s *Service) getTask(id string) (plugin.Task, error) {
|
|||||||
c, ok := s.tasks[id]
|
c, ok := s.tasks[id]
|
||||||
s.mu.Unlock()
|
s.mu.Unlock()
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, plugin.ErrContainerNotExist
|
return nil, grpc.Errorf(codes.NotFound, "task %v not found", id)
|
||||||
}
|
}
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user