Wire in logic for selecting backing state impl

Signed-off-by: Matthew Heon <matthew.heon@gmail.com>

Closes: #229
Approved by: rhatdan
This commit is contained in:
Matthew Heon 2018-01-03 16:27:33 -05:00 committed by Atomic Bot
parent 5696dfef6e
commit b814a94c34
2 changed files with 32 additions and 9 deletions

View File

@ -81,15 +81,21 @@ func WithSignaturePolicy(path string) RuntimeOption {
} }
} }
// WithInMemoryState specifies that the runtime will be backed by an in-memory // WithStateType sets the backing state implementation for libpod
// state only, and state will not persist after the runtime is shut down // Please note that information is not portable between backing states
func WithInMemoryState() RuntimeOption { // As such, if this differs between two libpods running on the same system,
// they will not share containers, and unspecified behavior may occur
func WithStateType(storeType RuntimeStateStore) RuntimeOption {
return func(rt *Runtime) error { return func(rt *Runtime) error {
if rt.valid { if rt.valid {
return ErrRuntimeFinalized return ErrRuntimeFinalized
} }
rt.config.InMemoryState = true if storeType == InvalidStateStore {
return errors.Wrapf(ErrInvalidArg, "must provide a valid state store type")
}
rt.config.StateType = storeType
return nil return nil
} }

View File

@ -14,6 +14,21 @@ import (
"github.com/ulule/deepcopier" "github.com/ulule/deepcopier"
) )
// RuntimeStateStore is a constant indicating which state store implementation
// should be used by libpod
type RuntimeStateStore int
const (
// InvalidStateStore is an invalid state store
InvalidStateStore RuntimeStateStore = iota
// InMemoryStateStore is an in-memory state that will not persist data
// on containers and pods between libpod instances or after system
// reboot
InMemoryStateStore RuntimeStateStore = iota
// SQLiteStateStore is a state backed by a SQLite database
SQLiteStateStore RuntimeStateStore = iota
)
// A RuntimeOption is a functional option which alters the Runtime created by // A RuntimeOption is a functional option which alters the Runtime created by
// NewRuntime // NewRuntime
type RuntimeOption func(*Runtime) error type RuntimeOption func(*Runtime) error
@ -39,7 +54,7 @@ type RuntimeConfig struct {
InsecureRegistries []string InsecureRegistries []string
Registries []string Registries []string
SignaturePolicyPath string SignaturePolicyPath string
InMemoryState bool StateType RuntimeStateStore
RuntimePath string RuntimePath string
ConmonPath string ConmonPath string
ConmonEnvVars []string ConmonEnvVars []string
@ -57,7 +72,7 @@ var (
// Leave this empty so containers/storage will use its defaults // Leave this empty so containers/storage will use its defaults
StorageConfig: storage.StoreOptions{}, StorageConfig: storage.StoreOptions{},
ImageDefaultTransport: DefaultTransport, ImageDefaultTransport: DefaultTransport,
InMemoryState: false, StateType: SQLiteStateStore,
RuntimePath: "/usr/bin/runc", RuntimePath: "/usr/bin/runc",
ConmonPath: findConmonPath(), ConmonPath: findConmonPath(),
ConmonEnvVars: []string{ ConmonEnvVars: []string{
@ -176,14 +191,14 @@ func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) {
runtime.netPlugin = netPlugin runtime.netPlugin = netPlugin
// Set up the state // Set up the state
if runtime.config.InMemoryState { if runtime.config.StateType == InMemoryStateStore {
state, err := NewInMemoryState() state, err := NewInMemoryState()
if err != nil { if err != nil {
return nil, err return nil, err
} }
runtime.state = state runtime.state = state
} else { } else if runtime.config.StateType == SQLiteStateStore {
dbPath := filepath.Join(runtime.config.StaticDir, "state.sql") dbPath := filepath.Join(runtime.config.StaticDir, "sql_state.db")
specsDir := filepath.Join(runtime.config.StaticDir, "ocispec") specsDir := filepath.Join(runtime.config.StaticDir, "ocispec")
// Make a directory to hold JSON versions of container OCI specs // Make a directory to hold JSON versions of container OCI specs
@ -200,6 +215,8 @@ func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) {
return nil, err return nil, err
} }
runtime.state = state runtime.state = state
} else {
return nil, errors.Wrapf(ErrInvalidArg, "unrecognized state type passed")
} }
// We now need to see if the system has restarted // We now need to see if the system has restarted