Skip to content

Commit

Permalink
refactor: extract container exists logic
Browse files Browse the repository at this point in the history
  • Loading branch information
nixpig committed Dec 28, 2024
1 parent a31f9a1 commit fae2680
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 37 deletions.
42 changes: 15 additions & 27 deletions container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type ContainerState struct {
}

type ContainerOpts struct {
Bundle string
PIDFile string
ConsoleSocket string
Stdin *os.File
Expand All @@ -52,19 +53,9 @@ type ContainerOpts struct {

func New(
id string,
bundle string,
spec *specs.Spec,
opts *ContainerOpts,
) (*Container, error) {
b, err := os.ReadFile(filepath.Join(bundle, configFilename))
if err != nil {
return nil, fmt.Errorf("read new container config file: %w", err)
}

var spec *specs.Spec
if err := json.Unmarshal(b, &spec); err != nil {
return nil, fmt.Errorf("parse new container config: %w", err)
}

if spec.Linux == nil {
return nil, errors.New("only Linux containers are supported")
}
Expand All @@ -77,15 +68,10 @@ func New(
return nil, fmt.Errorf("version must be valid semver: %s", spec.Version)
}

absBundlePath, err := filepath.Abs(bundle)
if err != nil {
return nil, fmt.Errorf("absolute path from new container bundle: %w", err)
}

state := &ContainerState{
Version: OCIVersion,
ID: id,
Bundle: absBundlePath,
Bundle: opts.Bundle,
Annotations: spec.Annotations,
Status: specs.StateCreating,
}
Expand All @@ -96,18 +82,13 @@ func New(
Opts: opts,
}

if err := os.MkdirAll(
filepath.Join(containerRootDir, cntr.ID()),
0666,
); err != nil {
return nil, fmt.Errorf("create new container directory: %w", err)
}
return &cntr, nil
}

if err := cntr.Save(); err != nil {
return nil, fmt.Errorf("save new container: %w", err)
}
func Exists(containerRootDir string, id string) bool {
_, err := os.Stat(filepath.Join(containerRootDir, id))

return &cntr, nil
return err == nil
}

func Load(id string) (*Container, error) {
Expand Down Expand Up @@ -170,6 +151,13 @@ func (c *Container) RefreshState() error {
}

func (c *Container) Save() error {
if err := os.MkdirAll(
filepath.Join(containerRootDir, c.ID()),
0666,
); err != nil {
return fmt.Errorf("create container directory: %w", err)
}

b, err := json.Marshal(c.State)
if err != nil {
return fmt.Errorf("serialise container state for saving: %w", err)
Expand Down
44 changes: 34 additions & 10 deletions internal/commands/create.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package commands

import (
"encoding/json"
"fmt"
"os"
"path/filepath"

"github.com/nixpig/brownie/container"
"github.com/opencontainers/runtime-spec/specs-go"
)

const containerRootDir = "/var/lib/brownie/containers"
const (
containerRootDir = "/var/lib/brownie/containers"
configFilename = "config.json"
)

type CreateOpts struct {
ID string
Expand All @@ -20,19 +25,30 @@ type CreateOpts struct {
}

func Create(opts *CreateOpts) error {
if _, err := os.Stat(filepath.Join(
containerRootDir, opts.ID,
)); err == nil {
return fmt.Errorf(
"container already exists (%s): %w",
opts.ID, err,
)
if container.Exists(containerRootDir, opts.ID) {
return fmt.Errorf("container with id '%s' already exists", opts.ID)
}

bundle, err := filepath.Abs(opts.Bundle)
if err != nil {
return fmt.Errorf("absolute path from new container bundle: %w", err)
}

b, err := os.ReadFile(filepath.Join(opts.Bundle, configFilename))
if err != nil {
return fmt.Errorf("read new container config file: %w", err)
}

var spec *specs.Spec
if err := json.Unmarshal(b, &spec); err != nil {
return fmt.Errorf("parse new container config: %w", err)
}

cntr, err := container.New(
opts.ID,
opts.Bundle,
spec,
&container.ContainerOpts{
Bundle: bundle,
PIDFile: opts.PIDFile,
ConsoleSocket: opts.ConsoleSocket,
Stdin: os.Stdin,
Expand All @@ -44,5 +60,13 @@ func Create(opts *CreateOpts) error {
return fmt.Errorf("create container: %w", err)
}

return cntr.Init(opts.ReexecCmd, opts.ReexecArgs)
if err := cntr.Init(opts.ReexecCmd, opts.ReexecArgs); err != nil {
return fmt.Errorf("initialise container: %w", err)
}

if err := cntr.Save(); err != nil {
return fmt.Errorf("save container: %w", err)
}

return nil
}

0 comments on commit fae2680

Please sign in to comment.