Skip to content

Commit

Permalink
fix: correct state for stopped container
Browse files Browse the repository at this point in the history
  • Loading branch information
nixpig committed Nov 5, 2024
1 parent 0c70442 commit 768e2bf
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 25 deletions.
32 changes: 21 additions & 11 deletions container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,24 +99,24 @@ func New(
}

func Load(bundle string) (*Container, error) {
s, err := os.ReadFile(filepath.Join(bundle, "state.json"))
c, err := os.ReadFile(filepath.Join(bundle, "config.json"))
if err != nil {
return nil, err
}

state := ContainerState{}
if err := json.Unmarshal([]byte(s), &state); err != nil {
return nil, err
conf := specs.Spec{}
if err := json.Unmarshal([]byte(c), &conf); err != nil {
return nil, fmt.Errorf("unmarshall state to struct: %w", err)
}

c, err := os.ReadFile(filepath.Join(bundle, "config.json"))
s, err := os.ReadFile(filepath.Join(bundle, conf.Root.Path, "state.json"))
if err != nil {
return nil, err
}

conf := specs.Spec{}
if err := json.Unmarshal([]byte(c), &conf); err != nil {
return nil, fmt.Errorf("unmarshall state to struct: %w", err)
state := ContainerState{}
if err := json.Unmarshal([]byte(s), &state); err != nil {
return nil, err
}

cntr := &Container{
Expand All @@ -132,7 +132,7 @@ func Load(bundle string) (*Container, error) {
}

func (c *Container) RefreshState() error {
b, err := os.ReadFile(filepath.Join(c.Bundle(), "state.json"))
b, err := os.ReadFile(filepath.Join(c.Rootfs(), "state.json"))
if err != nil {
return fmt.Errorf("refresh from state file: %w", err)
}
Expand All @@ -158,11 +158,12 @@ func (c *Container) Save(configPath string) error {
}

func (c *Container) CSave() error {
fmt.Println("state: ", c.State.Status)
return c.Save("/state.json")
}

func (c *Container) HSave() error {
return c.Save(filepath.Join(c.Bundle(), "state.json"))
return c.Save(filepath.Join(c.Rootfs(), "state.json"))
}

func (c *Container) ExecHooks(lifecycleHook string) error {
Expand Down Expand Up @@ -227,6 +228,15 @@ func (c *Container) Bundle() string {
return c.State.Bundle
}

func (c *Container) AbsBundle() string {
p, err := filepath.Abs(c.State.Bundle)
if err != nil {
return ""
}

return p
}

func (c *Container) SetID(id string) {
c.State.ID = id
}
Expand All @@ -240,5 +250,5 @@ func (c *Container) Rootfs() string {
return c.Spec.Root.Path
}

return filepath.Join(c.Bundle(), c.Spec.Root.Path)
return filepath.Join(c.AbsBundle(), c.Spec.Root.Path)
}
2 changes: 1 addition & 1 deletion container/container_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (c *Container) Init(reexec string, arg string) error {
return fmt.Errorf("execute createcontainer hooks: %w", err)
}

initSockAddr := filepath.Join(c.Bundle(), initSockFilename)
initSockAddr := filepath.Join(c.Rootfs(), initSockFilename)

var err error
c.initIPC.ch, c.initIPC.closer, err = ipc.NewReceiver(initSockAddr)
Expand Down
25 changes: 14 additions & 11 deletions container/container_reexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

func (c *Container) Reexec(log *zerolog.Logger) error {
var err error
c.initIPC.ch, c.initIPC.closer, err = ipc.NewSender(filepath.Join(c.Bundle(), initSockFilename))
c.initIPC.ch, c.initIPC.closer, err = ipc.NewSender(filepath.Join(c.Rootfs(), initSockFilename))
if err != nil {
return err
}
Expand Down Expand Up @@ -54,12 +54,12 @@ func (c *Container) Reexec(log *zerolog.Logger) error {

// set up the socket _before_ pivot root
if err := os.RemoveAll(
filepath.Join(c.Bundle(), containerSockFilename),
filepath.Join(c.Rootfs(), containerSockFilename),
); err != nil {
return err
}

listCh, listCloser, err := ipc.NewReceiver(filepath.Join(c.Bundle(), containerSockFilename))
listCh, listCloser, err := ipc.NewReceiver(filepath.Join(c.Rootfs(), containerSockFilename))
if err != nil {
return err
}
Expand All @@ -82,6 +82,12 @@ func (c *Container) Reexec(log *zerolog.Logger) error {
c.initIPC.ch <- []byte("ready")

startErr := ipc.WaitForMsg(listCh, "start", func() error {
// ch, closer, err := ipc.NewSender(filepath.Join(c.Rootfs(), containerSockFilename))
// if err != nil {
// return err
// }
// defer closer()

if err := filesystem.PivotRoot(c.Rootfs()); err != nil {
return err
}
Expand All @@ -105,6 +111,8 @@ func (c *Container) Reexec(log *zerolog.Logger) error {
}

if c.Spec.Root.Readonly {
// remount over existing root mount as readonly

// FIXME: subsequent attempts to update container state fail, either by
// write to state.json (readonly filesystem) or write to db (readonly db)
// probably we need to send message to a socket that handles it?
Expand Down Expand Up @@ -177,8 +185,6 @@ func (c *Container) Reexec(log *zerolog.Logger) error {

cmd.Dir = c.Spec.Process.Cwd

log.Info().Msg("✅ before credential set")

var ambientCapsFlags []uintptr
if c.Spec.Process != nil &&
c.Spec.Process.Capabilities != nil {
Expand All @@ -199,21 +205,18 @@ func (c *Container) Reexec(log *zerolog.Logger) error {
},
}

log.Info().Msg("✅ after credential set")

cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

log.Info().Msg("✅ before cmd run")

cmd.Run()

log.Info().Msg("✅ after cmd run")
// ch <- []byte("stopped")

c.SetStatus(specs.StateStopped)
if err := c.CSave(); err != nil {
return fmt.Errorf("write state file: %w", err)
log.Error().Err(err).Msg("failed to save stopped container state")
return fmt.Errorf("save container stopped state: %w", err)
}

return nil
Expand Down
13 changes: 12 additions & 1 deletion container/container_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (c *Container) Start() error {
return fmt.Errorf("execute startContainer hooks: %w", err)
}

conn, err := net.Dial("unix", filepath.Join(c.Bundle(), containerSockFilename))
conn, err := net.Dial("unix", filepath.Join(c.Rootfs(), containerSockFilename))
if err != nil {
return fmt.Errorf("dial socket: %w", err)
}
Expand Down Expand Up @@ -59,5 +59,16 @@ func (c *Container) Start() error {

// FIXME: ?? when process starts, should the process 'replace' the parent container process?

// ch, closer, err := ipc.NewReceiver(filepath.Join(c.Rootfs(), containerSockFilename))
// if err != nil {
// return err
// }
// defer closer()

// ipc.WaitForMsg(ch, "stopped", func() error {
// fmt.Println("STOPPED!")
// return nil
// })

return nil
}
11 changes: 10 additions & 1 deletion internal/commands/start.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package commands

import (
"fmt"
"time"

"github.com/nixpig/brownie/container"
"github.com/nixpig/brownie/internal/database"
"github.com/rs/zerolog"
Expand All @@ -21,5 +24,11 @@ func Start(opts *StartOpts, log *zerolog.Logger, db *database.DB) error {
return err
}

return cntr.Start()
if err := cntr.Start(); err != nil {
return fmt.Errorf("failed to start container: %w", err)
}

time.Sleep(time.Second * 3)
fmt.Println(cntr.Status())
return nil
}

0 comments on commit 768e2bf

Please sign in to comment.