From c6fe4430b76ceeecd6b0b609cca8e705921db0c4 Mon Sep 17 00:00:00 2001 From: Matthew Heon Date: Tue, 7 Nov 2017 13:46:30 -0500 Subject: Compile-tested implementation of SQL-backed state Signed-off-by: Matthew Heon --- libpod/sql_state.go | 432 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 432 insertions(+) create mode 100644 libpod/sql_state.go (limited to 'libpod/sql_state.go') diff --git a/libpod/sql_state.go b/libpod/sql_state.go new file mode 100644 index 000000000..0564d124f --- /dev/null +++ b/libpod/sql_state.go @@ -0,0 +1,432 @@ +package libpod + +import ( + "database/sql" + "encoding/json" + "io/ioutil" + "os" + + "github.com/containers/storage" + _ "github.com/mattn/go-sqlite3" + "github.com/pkg/errors" + "github.com/sirupsen/logrus" +) + +// SqlState is a state implementation backed by a persistent SQLite3 database +type SQLState struct { + db *sql.DB + specsDir string + runtime *Runtime + lock storage.Locker + valid bool +} + +// NewSqlState initializes a SQL-backed state, created the database if necessary +func NewSqlState(dbPath, lockPath, specsDir string, runtime *Runtime) (State, error) { + state := new(SQLState) + + state.runtime = runtime + + // Make our lock file + lock, err := storage.GetLockfile(lockPath) + if err != nil { + return nil, errors.Wrapf(err, "error creating lockfile for state") + } + state.lock = lock + + // Make the directory that will hold JSON copies of container runtime specs + if err := os.MkdirAll(specsDir, 0750); err != nil { + // The directory is allowed to exist + if !os.IsExist(err) { + return nil, errors.Wrapf(err, "error creating OCI specs dir %s", specsDir) + } + } + state.specsDir = specsDir + + // Acquire the lock while we open the database and perform initial setup + state.lock.Lock() + defer state.lock.Unlock() + + // TODO add a separate temporary database for per-boot container + // state + + // Open the database + // Use loc=auto to get accurate locales for timestamps + db, err := sql.Open("sqlite3", dbPath+"?_loc=auto") + if err != nil { + return nil, errors.Wrapf(err, "error opening database") + } + + // Ensure connectivity + if err := db.Ping(); err != nil { + return nil, errors.Wrapf(err, "cannot establish connection to database") + } + + // Prepare database + if err := prepareDB(db); err != nil { + return nil, err + } + + state.db = db + + state.valid = true + + return state, nil +} + +// Close the state's database connection +func (s *SQLState) Close() error { + s.lock.Lock() + defer s.lock.Unlock() + + if !s.valid { + return ErrDBClosed + } + + s.valid = false + + err := s.db.Close() + if err != nil { + return errors.Wrapf(err, "error closing database") + } + + return nil +} + +// Container retrieves a container from its full ID +func (s *SQLState) Container(id string) (*Container, error) { + const query = `SELECT containers.*, + containerState.State, + containerState.ConfigPath, + containerState.RunDir, + containerState.MountPoint, + containerState.StartedTime, + containerState.FinishedTime, + containerState.ExitCode + FROM containers + INNER JOIN + containerState ON containers.Id = containerState.Id + WHERE containers.Id=?;` + + if !s.valid { + return nil, ErrDBClosed + } + + row := s.db.QueryRow(query, id) + + ctr, err := ctrFromScannable(row, s.runtime, s.specsDir) + if err != nil { + return nil, errors.Wrapf(err, "error retrieving container %s from database", id) + } + + return ctr, nil +} + +// LookupContainer retrieves a container by full or unique partial ID or name +func (s *SQLState) LookupContainer(idOrName string) (*Container, error) { + const query = `SELECT containers.*, + containerState.State, + containerState.ConfigPath, + containerState.RunDir, + containerState.MountPoint, + containerState.StartedTime, + containerState.FinishedTime, + containerState.ExitCode + FROM containers + INNER JOIN + containerState ON containers.Id = containerState.Id + WHERE (containers.Id LIKE ?) OR containers.Name=?;` + + if !s.valid { + return nil, ErrDBClosed + } + + rows, err := s.db.Query(query, idOrName+"%", idOrName) + if err != nil { + return nil, errors.Wrapf(err, "error retrieving container %s row from database", idOrName) + } + defer rows.Close() + + foundResult := false + var ctr *Container + for rows.Next() { + if foundResult { + return nil, errors.Wrapf(ErrCtrExists, "more than one result for ID or name %s", idOrName) + } + + var err error + ctr, err = ctrFromScannable(rows, s.runtime, s.specsDir) + if err != nil { + return nil, errors.Wrapf(err, "error retrieving container %s from database", idOrName) + } + foundResult = true + } + if err := rows.Err(); err != nil { + return nil, errors.Wrapf(err, "error retrieving rows for container ID or name %s", idOrName) + } + + if !foundResult { + return nil, errors.Wrapf(ErrNoSuchCtr, "no container with ID or name %s found", idOrName) + } + + return ctr, nil +} + +// HasContainer checks if the given container is present in the state +// It accepts a full ID +func (s *SQLState) HasContainer(id string) (bool, error) { + const query = "SELECT 1 FROM containers WHERE Id=?;" + + if !s.valid { + return false, ErrDBClosed + } + + row := s.db.QueryRow(query, id) + + var check int + err := row.Scan(&check) + if err != nil { + if err == sql.ErrNoRows { + return false, nil + } + + return false, errors.Wrapf(err, "error questing database for existence of container %s", id) + } else if check != 1 { + return false, errors.Wrapf(ErrInternal, "check digit for HasContainer query incorrect") + } + + return true, nil +} + +// AddContainer adds the given container to the state +// If the container belongs to a pod, that pod must already be present in the +// state, and the container will be added to the pod +func (s *SQLState) AddContainer(ctr *Container) (err error) { + const ( + addCtr = `INSERT INTO containers VALUES ( + ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? + );` + addCtrState = `INSERT INTO containerState VALUES ( + ?, ?, ?, ?, ?, ?, ?, ? + );` + ) + + if !s.valid { + return ErrDBClosed + } + + if !ctr.valid { + return ErrCtrRemoved + } + + labelsJSON, err := json.Marshal(ctr.config.Labels) + if err != nil { + return errors.Wrapf(err, "error marshaling container %s labels to JSON", ctr.ID()) + } + + // Save the container's runtime spec to disk + specJSON, err := json.Marshal(ctr.config.Spec) + if err != nil { + return errors.Wrapf(err, "error marshalling container %s spec to JSON", ctr.ID()) + } + specPath := getSpecPath(s.specsDir, ctr.ID()) + if err := ioutil.WriteFile(specPath, specJSON, 0750); err != nil { + return errors.Wrapf(err, "error saving container %s spec JSON to disk", ctr.ID()) + } + defer func() { + if err != nil { + if err2 := os.Remove(specPath); err2 != nil { + logrus.Errorf("Error removing container %s JSON spec from state: %v", ctr.ID(), err2) + } + } + }() + + s.lock.Lock() + defer s.lock.Unlock() + + tx, err := s.db.Begin() + if err != nil { + return errors.Wrapf(err, "error beginning database transaction") + } + defer func() { + if err != nil { + if err2 := tx.Rollback(); err2 != nil { + logrus.Errorf("Error rolling back transaction to add container %s: %v", ctr.ID(), err2) + } + } + }() + + // Add static container information + _, err = tx.Exec(addCtr, + ctr.ID(), + ctr.Name(), + ctr.config.MountLabel, + ctr.config.StaticDir, + boolToSQL(ctr.config.Stdin), + string(labelsJSON), + ctr.config.StopSignal, + timeToSQL(ctr.config.CreatedTime), + ctr.config.RootfsImageID, + ctr.config.RootfsImageName, + boolToSQL(ctr.config.UseImageConfig)) + if err != nil { + return errors.Wrapf(err, "error adding static information for container %s to database", ctr.ID()) + } + + // Add container state to the database + _, err = tx.Exec(addCtrState, + ctr.ID(), + ctr.state.State, + ctr.state.ConfigPath, + ctr.state.RunDir, + ctr.state.Mountpoint, + timeToSQL(ctr.state.StartedTime), + timeToSQL(ctr.state.FinishedTime), + ctr.state.ExitCode) + if err != nil { + return errors.Wrapf(err, "error adding container %s state to database", ctr.ID()) + } + + if err := tx.Commit(); err != nil { + return errors.Wrapf(err, "error committing transaction to add container %s", ctr.ID()) + } + + return nil +} + +// RemoveContainer removes the container from the state +func (s *SQLState) RemoveContainer(ctr *Container) error { + const ( + removeCtr = "DELETE FROM containers WHERE Id=?;" + removeState = "DELETE FROM containerState WHERE ID=?;" + ) + + s.lock.Lock() + defer s.lock.Unlock() + + ctr.lock.Lock() + defer ctr.lock.Unlock() + + if !s.valid { + return ErrDBClosed + } + + committed := false + + tx, err := s.db.Begin() + if err != nil { + return errors.Wrapf(err, "error beginning database transaction") + } + defer func() { + if err != nil && !committed { + if err2 := tx.Rollback(); err2 != nil { + logrus.Errorf("Error rolling back transaction to add container %s: %v", ctr.ID(), err2) + } + } + }() + + // Check rows acted on for the first transaction, verify we actually removed something + result, err := tx.Exec(removeCtr, ctr.ID()) + if err != nil { + return errors.Wrapf(err, "error removing container %s from containers table", ctr.ID()) + } + rows, err := result.RowsAffected() + if err != nil { + return errors.Wrapf(err, "error retrieving number of rows in transaction removing container %s", ctr.ID()) + } else if rows == 0 { + return ErrNoSuchCtr + } + + if _, err := tx.Exec(removeState, ctr.ID()); err != nil { + return errors.Wrapf(err, "error removing container %s from state table", ctr.ID()) + } + + if err := tx.Commit(); err != nil { + return errors.Wrapf(err, "error committing transaction to remove container %s", ctr.ID()) + } + + committed = true + + // Remove the container's JSON from disk + jsonPath := getSpecPath(s.specsDir, ctr.ID()) + if err := os.Remove(jsonPath); err != nil { + return errors.Wrapf(err, "error removing JSON spec from state for container %s", ctr.ID()) + } + + ctr.valid = false + + return nil +} + +// AllContainers retrieves all the containers presently in the state +func (s *SQLState) AllContainers() ([]*Container, error) { + // TODO maybe do an ORDER BY here? + const query = `SELECT containers.*, + containerState.State, + containerState.ConfigPath, + containerState.RunDir, + containerState.MountPoint, + containerState.StartedTime, + containerState.FinishedTime, + containerState.ExitCode + FROM containers + INNER JOIN + containerState ON containers.Id = containerState.Id;` + + if !s.valid { + return nil, ErrDBClosed + } + + rows, err := s.db.Query(query) + if err != nil { + return nil, errors.Wrapf(err, "error retrieving containers from database") + } + defer rows.Close() + + containers := []*Container{} + + for rows.Next() { + ctr, err := ctrFromScannable(rows, s.runtime, s.specsDir) + if err != nil { + return nil, err + } + + containers = append(containers, ctr) + } + if err := rows.Err(); err != nil { + return nil, errors.Wrapf(err, "error retrieving container rows") + } + + return containers, nil +} + +// Pod retrieves a pod by its full ID +func (s *SQLState) Pod(id string) (*Pod, error) { + return nil, ErrNotImplemented +} + +// LookupPod retrieves a pot by full or unique partial ID or name +func (s *SQLState) LookupPod(idOrName string) (*Pod, error) { + return nil, ErrNotImplemented +} + +// HasPod checks if a pod exists given its full ID +func (s *SQLState) HasPod(id string) (bool, error) { + return false, ErrNotImplemented +} + +// AddPod adds a pod to the state +// Only empty pods can be added to the state +func (s *SQLState) AddPod(pod *Pod) error { + return ErrNotImplemented +} + +// RemovePod removes a pod from the state +// Only empty pods can be removed +func (s *SQLState) RemovePod(pod *Pod) error { + return ErrNotImplemented +} + +func (s *SQLState) AllPods() ([]*Pod, error) { + return nil, ErrNotImplemented +} -- cgit v1.2.3-54-g00ecf From cb56716fc4bb632db84fab372fff8f5a9007ed3f Mon Sep 17 00:00:00 2001 From: Matthew Heon Date: Tue, 7 Nov 2017 14:57:29 -0500 Subject: Address review comments, fix gofmt and lint Signed-off-by: Matthew Heon --- libpod/container.go | 2 +- libpod/sql_state.go | 12 +++++++----- libpod/sql_state_internal.go | 4 +++- 3 files changed, 11 insertions(+), 7 deletions(-) (limited to 'libpod/sql_state.go') diff --git a/libpod/container.go b/libpod/container.go index bef3f1088..097455112 100644 --- a/libpod/container.go +++ b/libpod/container.go @@ -92,7 +92,7 @@ type containerConfig struct { RootfsImageName string `json:"rootfsImageName,omitempty"` UseImageConfig bool `json:"useImageConfig"` // SELinux mount label for root filesystem - MountLabel string `json:"MountLabel,omitempty"` + MountLabel string `json:"MountLabel,omitempty"` // Static directory for container content that will persist across // reboot StaticDir string `json:"staticDir"` diff --git a/libpod/sql_state.go b/libpod/sql_state.go index 0564d124f..1218bfa79 100644 --- a/libpod/sql_state.go +++ b/libpod/sql_state.go @@ -7,12 +7,14 @@ import ( "os" "github.com/containers/storage" - _ "github.com/mattn/go-sqlite3" "github.com/pkg/errors" "github.com/sirupsen/logrus" + + // Use SQLite backend for sql package + _ "github.com/mattn/go-sqlite3" ) -// SqlState is a state implementation backed by a persistent SQLite3 database +// SQLState is a state implementation backed by a persistent SQLite3 database type SQLState struct { db *sql.DB specsDir string @@ -22,7 +24,7 @@ type SQLState struct { } // NewSqlState initializes a SQL-backed state, created the database if necessary -func NewSqlState(dbPath, lockPath, specsDir string, runtime *Runtime) (State, error) { +func NewSQLState(dbPath, lockPath, specsDir string, runtime *Runtime) (State, error) { state := new(SQLState) state.runtime = runtime @@ -85,8 +87,7 @@ func (s *SQLState) Close() error { s.valid = false - err := s.db.Close() - if err != nil { + if err := s.db.Close(); err != nil { return errors.Wrapf(err, "error closing database") } @@ -427,6 +428,7 @@ func (s *SQLState) RemovePod(pod *Pod) error { return ErrNotImplemented } +// AllPods retrieves all pods presently in the state func (s *SQLState) AllPods() ([]*Pod, error) { return nil, ErrNotImplemented } diff --git a/libpod/sql_state_internal.go b/libpod/sql_state_internal.go index 2597c4bb9..3f5f695bb 100644 --- a/libpod/sql_state_internal.go +++ b/libpod/sql_state_internal.go @@ -7,10 +7,12 @@ import ( "path/filepath" "time" - _ "github.com/mattn/go-sqlite3" spec "github.com/opencontainers/runtime-spec/specs-go" "github.com/pkg/errors" "github.com/sirupsen/logrus" + + // Use SQLite backend for sql package + _ "github.com/mattn/go-sqlite3" ) // Performs database setup including by not limited to initializing tables in -- cgit v1.2.3-54-g00ecf From 763e3726493c122d9722e0e294634ae8d78c426b Mon Sep 17 00:00:00 2001 From: Matthew Heon Date: Thu, 9 Nov 2017 13:51:20 -0500 Subject: Wire SQL backed state into rest of libpod Signed-off-by: Matthew Heon --- libpod/container.go | 63 +++++++++++++-------- libpod/in_memory_state.go | 15 +++++ libpod/options.go | 14 +++++ libpod/runtime.go | 45 ++++++++++++--- libpod/runtime_ctr.go | 20 ++++--- libpod/sql_state.go | 140 ++++++++++++++++++++++++++++++++++++++++++++-- libpod/state.go | 4 ++ 7 files changed, 257 insertions(+), 44 deletions(-) (limited to 'libpod/sql_state.go') diff --git a/libpod/container.go b/libpod/container.go index 097455112..c8a09fe9e 100644 --- a/libpod/container.go +++ b/libpod/container.go @@ -150,10 +150,9 @@ func (c *Container) State() (ContainerState, error) { c.lock.Lock() defer c.lock.Unlock() - // TODO uncomment when working - // if err := c.runtime.ociRuntime.updateContainerStatus(c); err != nil { - // return ContainerStateUnknown, err - // } + if err := c.runtime.state.UpdateContainer(c); err != nil { + return ContainerStateUnknown, errors.Wrapf(err, "error updating container %s state", c.ID()) + } return c.state.State, nil } @@ -252,6 +251,10 @@ func (c *Container) Create() (err error) { c.lock.Lock() defer c.lock.Unlock() + if err := c.runtime.state.UpdateContainer(c); err != nil { + return errors.Wrapf(err, "error updating container %s state", c.ID()) + } + if !c.valid { return errors.Wrapf(ErrCtrRemoved, "container %s is not valid", c.ID()) } @@ -308,9 +311,12 @@ func (c *Container) Create() (err error) { logrus.Debugf("Created container %s in runc", c.ID()) - // TODO should flush this state to disk here c.state.State = ContainerStateCreated + if err := c.runtime.state.SaveContainer(c); err != nil { + return errors.Wrapf(err, "error saving container %s state", c.ID()) + } + return nil } @@ -319,6 +325,10 @@ func (c *Container) Start() error { c.lock.Lock() defer c.lock.Unlock() + if err := c.runtime.state.UpdateContainer(c); err != nil { + return errors.Wrapf(err, "error updating container %s state", c.ID()) + } + if !c.valid { return ErrCtrRemoved } @@ -334,10 +344,13 @@ func (c *Container) Start() error { logrus.Debugf("Started container %s", c.ID()) - // TODO should flush state to disk here c.state.StartedTime = time.Now() c.state.State = ContainerStateRunning + if err := c.runtime.state.SaveContainer(c); err != nil { + return errors.Wrapf(err, "error saving container %s state", c.ID()) + } + return nil } @@ -360,6 +373,25 @@ func (c *Container) Exec(cmd []string, tty bool, stdin bool) (string, error) { // Attach attaches to a container // Returns fully qualified URL of streaming server for the container func (c *Container) Attach(noStdin bool, keys string, attached chan<- bool) error { + if err := c.runtime.state.UpdateContainer(c); err != nil { + return errors.Wrapf(err, "error updating container %s state", c.ID()) + } + + if !c.valid { + return errors.Wrapf(ErrCtrRemoved, "container %s is not valid", c.ID()) + } + + if c.state.State == ContainerStateRunning || c.state.State == ContainerStatePaused { + return errors.Wrapf(ErrCtrStateInvalid, "cannot remove storage for container %s as it is running or paused", c.ID()) + } + + // TODO is it valid to attach to a frozen container? + if c.state.State == ContainerStateUnknown || + c.state.State == ContainerStateConfigured || + c.state.State == ContainerStatePaused { + return errors.Wrapf(ErrCtrStateInvalid, "can only attach to created, running, or stopped containers") + } + // Check the validity of the provided keys first var err error detachKeys := []byte{} @@ -369,25 +401,12 @@ func (c *Container) Attach(noStdin bool, keys string, attached chan<- bool) erro return errors.Wrapf(err, "invalid detach keys") } } - cStatus := c.state.State - if !(cStatus == ContainerStateRunning || cStatus == ContainerStateCreated) { - return errors.Errorf("%s is not created or running", c.Name()) - } resize := make(chan remotecommand.TerminalSize) defer close(resize) - err = c.attachContainerSocket(resize, noStdin, detachKeys, attached) + err = c.attachContainerSocket(resize, noStdin, detachKeys, attached) return err - - // TODO - // Re-enable this when mheon is done wth it - //if err != nil { - // return err - //} - //c.ContainerStateToDisk(c) - - //return err } // Mount mounts a container's filesystem on the host @@ -414,10 +433,6 @@ func (c *Container) Export(path string) error { // Commit commits the changes between a container and its image, creating a new // image -// If the container was not created from an image (for example, -// WithRootFSFromPath will create a container from a directory on the system), -// a new base image will be created from the contents of the container's -// filesystem func (c *Container) Commit() (*storage.Image, error) { return nil, ErrNotImplemented } diff --git a/libpod/in_memory_state.go b/libpod/in_memory_state.go index 87caac808..5d03e62e6 100644 --- a/libpod/in_memory_state.go +++ b/libpod/in_memory_state.go @@ -153,6 +153,21 @@ func (s *InMemoryState) RemoveContainer(ctr *Container) error { return nil } +// UpdateContainer updates a container's state +// As all state is in-memory, no update will be required +// As such this is a no-op +func (s *InMemoryState) UpdateContainer(ctr *Container) error { + return nil +} + +// SaveContainer saves a container's state +// As all state is in-memory, any changes are always reflected as soon as they +// are made +// As such this is a no-op +func (s *InMemoryState) SaveContainer(ctr *Container) error { + return nil +} + // AllContainers retrieves all containers from the state func (s *InMemoryState) AllContainers() ([]*Container, error) { ctrs := make([]*Container, 0, len(s.containers)) diff --git a/libpod/options.go b/libpod/options.go index fb64c0c41..4c21a70c9 100644 --- a/libpod/options.go +++ b/libpod/options.go @@ -94,6 +94,20 @@ func WithSignaturePolicy(path string) RuntimeOption { } } +// WithInMemoryState specifies that the runtime will be backed by an in-memory +// state only, and state will not persist after the runtime is shut down +func WithInMemoryState() RuntimeOption { + return func(rt *Runtime) error { + if rt.valid { + return ErrRuntimeFinalized + } + + rt.config.InMemoryState = true + + return nil + } +} + // WithOCIRuntime specifies an OCI runtime to use for running containers func WithOCIRuntime(runtimePath string) RuntimeOption { return func(rt *Runtime) error { diff --git a/libpod/runtime.go b/libpod/runtime.go index 80202c567..1bfb79947 100644 --- a/libpod/runtime.go +++ b/libpod/runtime.go @@ -2,6 +2,7 @@ package libpod import ( "os" + "path/filepath" "sync" is "github.com/containers/image/storage" @@ -35,6 +36,7 @@ type RuntimeConfig struct { InsecureRegistries []string Registries []string SignaturePolicyPath string + InMemoryState bool RuntimePath string ConmonPath string ConmonEnvVars []string @@ -52,6 +54,7 @@ var ( // Leave this empty so containers/storage will use its defaults StorageConfig: storage.StoreOptions{}, ImageDefaultTransport: "docker://", + InMemoryState: false, RuntimePath: "/usr/bin/runc", ConmonPath: "/usr/local/libexec/crio/conmon", ConmonEnvVars: []string{ @@ -114,13 +117,6 @@ func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) { SignaturePolicyPath: runtime.config.SignaturePolicyPath, } - // Set up the state - state, err := NewInMemoryState() - if err != nil { - return nil, err - } - runtime.state = state - // Make an OCI runtime to perform container operations ociRuntime, err := newOCIRuntime("runc", runtime.config.RuntimePath, runtime.config.ConmonPath, runtime.config.ConmonEnvVars, @@ -149,6 +145,34 @@ func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) { } } + // Set up the state + if runtime.config.InMemoryState { + state, err := NewInMemoryState() + if err != nil { + return nil, err + } + runtime.state = state + } else { + dbPath := filepath.Join(runtime.config.StaticDir, "state.sql") + lockPath := filepath.Join(runtime.config.TmpDir, "state.lck") + specsDir := filepath.Join(runtime.config.StaticDir, "ocispec") + + // Make a directory to hold JSON versions of container OCI specs + if err := os.MkdirAll(specsDir, 0755); err != nil { + // The directory is allowed to exist + if !os.IsExist(err) { + return nil, errors.Wrapf(err, "error creating runtime OCI specs directory %s", + specsDir) + } + } + + state, err := NewSQLState(dbPath, lockPath, specsDir, runtime) + if err != nil { + return nil, err + } + runtime.state = state + } + // Mark the runtime as valid - ready to be used, cannot be modified // further runtime.valid = true @@ -188,5 +212,10 @@ func (r *Runtime) Shutdown(force bool) error { r.valid = false _, err := r.store.Shutdown(force) - return err + if err != nil { + return err + } + + // TODO: Should always call this even if store.Shutdown failed + return r.state.Close() } diff --git a/libpod/runtime_ctr.go b/libpod/runtime_ctr.go index b23c65287..a8a5b789d 100644 --- a/libpod/runtime_ctr.go +++ b/libpod/runtime_ctr.go @@ -95,17 +95,21 @@ func (r *Runtime) RemoveContainer(c *Container, force bool) error { return ErrCtrRemoved } - // TODO check container status and unmount storage - // TODO check that no other containers depend on this container's - // namespaces - status, err := c.State() - if err != nil { + // Update the container to get current state + if err := r.state.UpdateContainer(c); err != nil { return err } - // A container cannot be removed if it is running - if status == ContainerStateRunning { - return errors.Wrapf(ErrCtrStateInvalid, "cannot remove container %s as it is running", c.ID()) + // Check that the container's in a good state to be removed + if !(c.state.State == ContainerStateConfigured || + c.state.State == ContainerStateCreated || + c.state.State == ContainerStateStopped) { + return errors.Wrapf(ErrCtrStateInvalid, "cannot remove container %s as it is running or paused", c.ID()) + } + + // Stop the container's storage + if err := c.teardownStorage(); err != nil { + return err } if err := r.state.RemoveContainer(c); err != nil { diff --git a/libpod/sql_state.go b/libpod/sql_state.go index 1218bfa79..4754bf71e 100644 --- a/libpod/sql_state.go +++ b/libpod/sql_state.go @@ -23,7 +23,7 @@ type SQLState struct { valid bool } -// NewSqlState initializes a SQL-backed state, created the database if necessary +// NewSQLState initializes a SQL-backed state, created the database if necessary func NewSQLState(dbPath, lockPath, specsDir string, runtime *Runtime) (State, error) { state := new(SQLState) @@ -295,6 +295,141 @@ func (s *SQLState) AddContainer(ctr *Container) (err error) { return nil } +// UpdateContainer updates a container's state from the database +func (s *SQLState) UpdateContainer(ctr *Container) error { + const query = `SELECT State, + ConfigPath, + RunDir, + Mountpoint, + StartedTime, + FinishedTime, + ExitCode + FROM containerState WHERE ID=?;` + + var ( + state int + configPath string + runDir string + mountpoint string + startedTimeString string + finishedTimeString string + exitCode int32 + ) + + if !s.valid { + return ErrDBClosed + } + + if !ctr.valid { + return ErrCtrRemoved + } + + row := s.db.QueryRow(query, ctr.ID()) + err := row.Scan( + &state, + &configPath, + &runDir, + &mountpoint, + &startedTimeString, + &finishedTimeString, + &exitCode) + if err != nil { + // The container may not exist in the database + if err == sql.ErrNoRows { + // Assume that the container was removed by another process + // As such make it invalid + ctr.valid = false + + return errors.Wrapf(ErrNoSuchCtr, "no container with ID %s found in database", ctr.ID()) + } + + return errors.Wrapf(err, "error parsing database state for container %s", ctr.ID()) + } + + newState := new(containerRuntimeInfo) + newState.State = ContainerState(state) + newState.ConfigPath = configPath + newState.RunDir = runDir + newState.Mountpoint = mountpoint + newState.ExitCode = exitCode + + if newState.Mountpoint != "" { + newState.Mounted = true + } + + startedTime, err := timeFromSQL(startedTimeString) + if err != nil { + return errors.Wrapf(err, "error parsing container %s started time", ctr.ID()) + } + newState.StartedTime = startedTime + + finishedTime, err := timeFromSQL(finishedTimeString) + if err != nil { + return errors.Wrapf(err, "error parsing container %s finished time", ctr.ID()) + } + newState.FinishedTime = finishedTime + + // New state compiled successfully, swap it into the current state + ctr.state = newState + + return nil +} + +// SaveContainer updates a container's state in the database +func (s *SQLState) SaveContainer(ctr *Container) error { + const update = `UPDATE containerState SET + State=?, + ConfigPath=?, + RunDir=?, + Mountpoint=?, + StartedTime=?, + FinishedTime=?, + ExitCode=? + WHERE Id=?;` + + s.lock.Lock() + defer s.lock.Unlock() + + if !s.valid { + return ErrDBClosed + } + + if !ctr.valid { + return ErrCtrRemoved + } + + tx, err := s.db.Begin() + if err != nil { + return errors.Wrapf(err, "error beginning database transaction") + } + defer func() { + if err != nil { + if err2 := tx.Rollback(); err2 != nil { + logrus.Errorf("Error rolling back transaction to add container %s: %v", ctr.ID(), err2) + } + } + }() + + // Add container state to the database + _, err = tx.Exec(update, + ctr.state.State, + ctr.state.ConfigPath, + ctr.state.RunDir, + ctr.state.Mountpoint, + timeToSQL(ctr.state.StartedTime), + timeToSQL(ctr.state.FinishedTime), + ctr.state.ExitCode) + if err != nil { + return errors.Wrapf(err, "error updating container %s state in database", ctr.ID()) + } + + if err := tx.Commit(); err != nil { + return errors.Wrapf(err, "error committing transaction to update container %s", ctr.ID()) + } + + return nil +} + // RemoveContainer removes the container from the state func (s *SQLState) RemoveContainer(ctr *Container) error { const ( @@ -305,9 +440,6 @@ func (s *SQLState) RemoveContainer(ctr *Container) error { s.lock.Lock() defer s.lock.Unlock() - ctr.lock.Lock() - defer ctr.lock.Unlock() - if !s.valid { return ErrDBClosed } diff --git a/libpod/state.go b/libpod/state.go index 41f44ac8e..4093f14f1 100644 --- a/libpod/state.go +++ b/libpod/state.go @@ -21,6 +21,10 @@ type State interface { // The container will only be removed from the state, not from the pod // which the container belongs to RemoveContainer(ctr *Container) error + // UpdateContainer updates a container's state from the backing store + UpdateContainer(ctr *Container) error + // SaveContainer saves a container's current state to the backing store + SaveContainer(ctr *Container) error // Retrieves all containers presently in state AllContainers() ([]*Container, error) -- cgit v1.2.3-54-g00ecf From 657cb1b7f6be1610d6888acf18bb77027108ea9e Mon Sep 17 00:00:00 2001 From: Matthew Heon Date: Thu, 9 Nov 2017 15:03:57 -0500 Subject: Fix lingering SQL error Signed-off-by: Matthew Heon --- libpod/sql_state.go | 16 ++++++++-------- libpod/sql_state_internal.go | 6 +++--- 2 files changed, 11 insertions(+), 11 deletions(-) (limited to 'libpod/sql_state.go') diff --git a/libpod/sql_state.go b/libpod/sql_state.go index 4754bf71e..424eb81fb 100644 --- a/libpod/sql_state.go +++ b/libpod/sql_state.go @@ -104,10 +104,10 @@ func (s *SQLState) Container(id string) (*Container, error) { containerState.StartedTime, containerState.FinishedTime, containerState.ExitCode - FROM containers - INNER JOIN - containerState ON containers.Id = containerState.Id - WHERE containers.Id=?;` + FROM containers + INNER JOIN + containerState ON containers.Id = containerState.Id + WHERE containers.Id=?;` if !s.valid { return nil, ErrDBClosed @@ -133,10 +133,10 @@ func (s *SQLState) LookupContainer(idOrName string) (*Container, error) { containerState.StartedTime, containerState.FinishedTime, containerState.ExitCode - FROM containers - INNER JOIN - containerState ON containers.Id = containerState.Id - WHERE (containers.Id LIKE ?) OR containers.Name=?;` + FROM containers + INNER JOIN + containerState ON containers.Id = containerState.Id + WHERE (containers.Id LIKE ?) OR containers.Name=?;` if !s.valid { return nil, ErrDBClosed diff --git a/libpod/sql_state_internal.go b/libpod/sql_state_internal.go index 3f5f695bb..da7c708e7 100644 --- a/libpod/sql_state_internal.go +++ b/libpod/sql_state_internal.go @@ -32,7 +32,7 @@ func prepareDB(db *sql.DB) (err error) { // Create a table for unchanging container data const createCtr = ` - CREATE TABLE IF NOT EXIST containers( + CREATE TABLE IF NOT EXISTS containers( Id TEXT NOT NULL PRIMARY KEY, Name TEXT NOT NULL UNIQUE, MountLabel TEXT NOT NULL, @@ -40,7 +40,7 @@ func prepareDB(db *sql.DB) (err error) { Stdin INTEGER NOT NULL, LabelsJSON TEXT NOT NULL, StopSignal INTEGER NOT NULL, - CreatedTime TEXT NOT NULL + CreatedTime TEXT NOT NULL, RootfsImageID TEXT NOT NULL, RootfsImageName TEXT NOT NULL, UseImageConfig INTEGER NOT NULL, @@ -52,7 +52,7 @@ func prepareDB(db *sql.DB) (err error) { // Create a table for changing container state const createCtrState = ` - CREATE TABLE IF NOT EXIST containerState( + CREATE TABLE IF NOT EXISTS containerState( Id TEXT NOT NULL PRIMARY KEY, State INTEGER NOT NULL, ConfigPath TEXT NOT NULL, -- cgit v1.2.3-54-g00ecf From 898138441d392c08fb658ed8654d5b1effa06a31 Mon Sep 17 00:00:00 2001 From: Matthew Heon Date: Thu, 9 Nov 2017 17:07:28 -0500 Subject: Need to provide ID of container being updated Signed-off-by: Matthew Heon --- libpod/sql_state.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'libpod/sql_state.go') diff --git a/libpod/sql_state.go b/libpod/sql_state.go index 424eb81fb..034fc03e1 100644 --- a/libpod/sql_state.go +++ b/libpod/sql_state.go @@ -418,7 +418,8 @@ func (s *SQLState) SaveContainer(ctr *Container) error { ctr.state.Mountpoint, timeToSQL(ctr.state.StartedTime), timeToSQL(ctr.state.FinishedTime), - ctr.state.ExitCode) + ctr.state.ExitCode, + ctr.ID()) if err != nil { return errors.Wrapf(err, "error updating container %s state in database", ctr.ID()) } -- cgit v1.2.3-54-g00ecf