Skip to content

Commit

Permalink
chore: removed dodgy logging solution; need to reimplement properly
Browse files Browse the repository at this point in the history
  • Loading branch information
nixpig committed Dec 14, 2024
1 parent 46bd3ce commit 9d60777
Show file tree
Hide file tree
Showing 19 changed files with 64 additions and 166 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This is a personal project for me to explore and better understand the OCI Runti
- internal/logging
- container/*
- internal/ipc
- [ ] Unit tests
- [ ] Implement seccomp
- [ ] Implement AppArmor
- [ ] Implement cgroups v2
Expand Down
34 changes: 14 additions & 20 deletions container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/nixpig/brownie/lifecycle"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/rs/zerolog"
"golang.org/x/mod/semver"
)

Expand All @@ -21,6 +20,8 @@ const (
containerSockFilename = "container.sock"
OCIVersion = "1.0.1-dev"
containerRootDir = "/var/lib/brownie/containers"
stateFilename = "state.json"
configFilename = "config.json"
)

type Container struct {
Expand Down Expand Up @@ -60,7 +61,7 @@ func New(
bundle string,
opts *ContainerOpts,
) (*Container, error) {
b, err := os.ReadFile(filepath.Join(bundle, "config.json"))
b, err := os.ReadFile(filepath.Join(bundle, configFilename))
if err != nil {
return nil, fmt.Errorf("read container config: %w", err)
}
Expand Down Expand Up @@ -117,7 +118,7 @@ func New(
}

func Load(id string) (*Container, error) {
s, err := os.ReadFile(filepath.Join(containerRootDir, id, "state.json"))
s, err := os.ReadFile(filepath.Join(containerRootDir, id, stateFilename))
if err != nil {
return nil, err
}
Expand All @@ -128,7 +129,7 @@ func Load(id string) (*Container, error) {
}

bundle := state.Bundle
c, err := os.ReadFile(filepath.Join(bundle, "config.json"))
c, err := os.ReadFile(filepath.Join(bundle, configFilename))
if err != nil {
return nil, err
}
Expand All @@ -143,35 +144,31 @@ func Load(id string) (*Container, error) {
Spec: &conf,
}

// if err := cntr.RefreshState(); err != nil {
// return nil, fmt.Errorf("refresh state: %w", err)
// }
//
if err := cntr.RefreshState(); err != nil {
return nil, err
}

return cntr, nil
}

func (c *Container) RefreshState(log *zerolog.Logger) error {
b, err := os.ReadFile(filepath.Join(containerRootDir, c.ID(), "state.json"))
func (c *Container) RefreshState() error {
b, err := os.ReadFile(filepath.Join(containerRootDir, c.ID(), stateFilename))
if err != nil {
log.Error().Err(err).Msg("(refresh) failed to read state file")
return fmt.Errorf("refresh from state file: %w", err)
}

if err := json.Unmarshal(b, c.State); err != nil {
log.Error().Err(err).Msg("(refresh) failed to unmarshal state")
return fmt.Errorf("unmarshall refreshed state: %w", err)
return fmt.Errorf("unmarshal refreshed state: %w", err)
}

process, err := os.FindProcess(c.State.PID)
if err != nil {
log.Error().Err(err).Msg("(refresh) failed to find process")
return err
}

if err := process.Signal(syscall.Signal(0)); err != nil {
c.SetStatus(specs.StateStopped)
if err := c.Save(); err != nil {
log.Error().Err(err).Msg("(refresh) failed to save container state")
return fmt.Errorf("(refresh) save container state: %w", err)
}
}
Expand All @@ -186,14 +183,11 @@ func (c *Container) Save() error {
}

if err := os.WriteFile(
filepath.Join(
containerRootDir,
c.ID(),
"state.json"),
filepath.Join(containerRootDir, c.ID(), stateFilename),
b,
0644,
); err != nil {
return fmt.Errorf("(save: %s) write state file: %w", c.State.Status, err)
return fmt.Errorf("write state file (%s): %w", c.State.Status, err)
}

if c.Opts != nil && c.Opts.PIDFile != "" {
Expand Down
7 changes: 2 additions & 5 deletions container/container_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import (
"os"
"path/filepath"
"syscall"

"github.com/rs/zerolog"
)

func (c *Container) Delete(force bool, log *zerolog.Logger) error {
func (c *Container) Delete(force bool) error {
if !force && !c.CanBeDeleted() {
return fmt.Errorf("container cannot be deleted in current state: %s", c.Status())
}
Expand All @@ -24,8 +22,7 @@ func (c *Container) Delete(force bool, log *zerolog.Logger) error {

// TODO: actually do the 'deleting'; rewind all the creation steps
if err := c.ExecHooks("poststop"); err != nil {
log.Warn().Err(err).Msg("failed to execute poststop hooks")
fmt.Println("WARNING: failed to execute poststop hooks")
fmt.Println("Warning: failed to execute poststop hooks")
}

if err := os.RemoveAll(
Expand Down
9 changes: 1 addition & 8 deletions container/container_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ import (
"github.com/nixpig/brownie/namespace"
"github.com/nixpig/brownie/terminal"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/rs/zerolog"
)

func (c *Container) Init(reexec string, arg string, log *zerolog.Logger) error {
func (c *Container) Init(reexec string, arg string) error {
if err := c.ExecHooks("createRuntime"); err != nil {
return fmt.Errorf("execute createruntime hooks: %w", err)
}
Expand Down Expand Up @@ -84,11 +83,6 @@ func (c *Container) Init(reexec string, arg string, log *zerolog.Logger) error {
return fmt.Errorf("connect pty: %w", err)
}

log.Info().
Int("consoleSocket", *c.State.ConsoleSocket).
Any("pty master", pty.Master.Name()).
Any("pty slave", pty.Slave.Name()).
Msg("send pty")
if err := terminal.SendPty(
*c.State.ConsoleSocket,
pty,
Expand All @@ -98,7 +92,6 @@ func (c *Container) Init(reexec string, arg string, log *zerolog.Logger) error {

} else {
// TODO: fall back to dup2 on stdin, stdout, stderr from c.Opts??
log.Info().Msg("not using console socket")
fmt.Println("TODO: implement fallback stdio??")
}

Expand Down
9 changes: 2 additions & 7 deletions container/container_kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ import (
"syscall"

"github.com/opencontainers/runtime-spec/specs-go"
"github.com/rs/zerolog"
)

func (c *Container) Kill(sig syscall.Signal, log *zerolog.Logger) error {
func (c *Container) Kill(sig syscall.Signal) error {
if !c.CanBeKilled() {
log.Error().Str("state", string(c.State.Status)).Msg("container cannot be killed in current state")
return fmt.Errorf("container cannot be killed in current state: %s", c.Status())
}

Expand All @@ -31,21 +29,18 @@ func (c *Container) Kill(sig syscall.Signal, log *zerolog.Logger) error {
}

if err := syscall.Kill(c.PID(), sig); err != nil {
log.Error().Err(err).Int("pid", c.PID()).Msg("failed to execute kill syscall")
return fmt.Errorf("failed to execute kill syscall (process: %d): %w", c.PID(), err)
}

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

// TODO: delete everything then
if err := c.ExecHooks("poststop"); err != nil {
log.Warn().Err(err).Msg("failed to execute poststop hooks")
fmt.Println("WARNING: failed to execute poststop hooks")
// TODO: log a warning???
fmt.Println("Warning: failed to execute poststop hooks")
}

return nil
Expand Down
3 changes: 1 addition & 2 deletions container/container_reexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ import (
"github.com/nixpig/brownie/internal/ipc"
"github.com/nixpig/brownie/user"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/rs/zerolog"
)

func (c *Container) Reexec(log *zerolog.Logger) error {
func (c *Container) Reexec() error {
var err error
c.initIPC.ch, c.initIPC.closer, err = ipc.NewSender(filepath.Join(containerRootDir, c.ID(), initSockFilename))
if err != nil {
Expand Down
20 changes: 6 additions & 14 deletions container/container_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import (
"path/filepath"

"github.com/opencontainers/runtime-spec/specs-go"
"github.com/rs/zerolog"
)

func (c *Container) Start(log *zerolog.Logger) error {
func (c *Container) Start() error {
if c.Spec.Process == nil {
c.SetStatus(specs.StateStopped)
if err := c.Save(); err != nil {
return fmt.Errorf("(start 1) write state file: %w", err)
return err
}
return nil
}
Expand All @@ -28,34 +27,27 @@ func (c *Container) Start(log *zerolog.Logger) error {
return fmt.Errorf("dial socket: %w", err)
}

// FIXME: 'prestart' hook is deprecated and appears to break 'docker run'??
if err := c.ExecHooks("prestart"); err != nil {
// TODO: run DELETE tasks here, then...
if err := c.ExecHooks("poststop"); err != nil {
log.Warn().Err(err).Msg("failed to execute poststop hooks")
fmt.Println("WARNING: failed to execute poststop hooks")
fmt.Println("Warning: failed to execute poststop hooks")
}

log.Warn().Err(err).Msg("failed to execute prestart hooks")
fmt.Println("Warning: failed to execute prestart hooks")
}

if _, err := conn.Write([]byte("start")); err != nil {
return fmt.Errorf("send start over ipc: %w", err)
}
defer conn.Close()

c.SetStatus(specs.StateRunning)
if err := c.Save(); err != nil {
// do something with err??
log.Error().Err(err).Msg("⁉️ host save state running")
fmt.Println(err)
return fmt.Errorf("save host container state: %w", err)
}
// FIXME: do these need to move up before the cmd.Wait call??
if err := c.ExecHooks("poststart"); err != nil {
// TODO: how to handle this (log a warning) from start command??
// FIXME: needs to 'log a warning'
log.Warn().Err(err).Msg("failed to execute poststart hook")
fmt.Println("WARNING: ", err)
fmt.Println("Warning: ", err)
}

return nil
Expand Down
Loading

0 comments on commit 9d60777

Please sign in to comment.