aboutsummaryrefslogtreecommitdiff
path: root/libpod/sql_state.go
diff options
context:
space:
mode:
authorMatthew Heon <matthew.heon@gmail.com>2018-01-12 12:41:10 -0500
committerAtomic Bot <atomic-devel@projectatomic.io>2018-01-16 14:58:06 +0000
commit20df2196f2158d8656d1b38580d816567843a5e0 (patch)
treedeaf0b58a6e8753893ed99029a71b21433b0fa25 /libpod/sql_state.go
parentd2ec1f76287a55d05ef1378fa31d5474c2bcc0bf (diff)
downloadpodman-20df2196f2158d8656d1b38580d816567843a5e0.tar.gz
podman-20df2196f2158d8656d1b38580d816567843a5e0.tar.bz2
podman-20df2196f2158d8656d1b38580d816567843a5e0.zip
Add ability for states to track container dependencies
Also prevent containers with dependencies from being removed from in memory states. SQLite already enforced this via FOREIGN KEY constraints. Signed-off-by: Matthew Heon <matthew.heon@gmail.com> Closes: #220 Approved by: rhatdan
Diffstat (limited to 'libpod/sql_state.go')
-rw-r--r--libpod/sql_state.go45
1 files changed, 45 insertions, 0 deletions
diff --git a/libpod/sql_state.go b/libpod/sql_state.go
index 51ec25510..4abce8576 100644
--- a/libpod/sql_state.go
+++ b/libpod/sql_state.go
@@ -661,6 +661,51 @@ func (s *SQLState) SaveContainer(ctr *Container) error {
return nil
}
+// ContainerInUse checks if other containers depend on the given container
+// It returns the IDs of containers which depend on the given container
+func (s *SQLState) ContainerInUse(ctr *Container) ([]string, error) {
+ const inUseQuery = `SELECT Id FROM containers WHERE
+ IPCNsCtr=? OR
+ MountNsCtr=? OR
+ NetNsCtr=? OR
+ PIDNsCtr=? OR
+ UserNsCtr=? OR
+ UTSNsCtr=? OR
+ CgroupNsCtr=?;`
+
+ if !s.valid {
+ return nil, ErrDBClosed
+ }
+
+ if !ctr.valid {
+ return nil, ErrCtrRemoved
+ }
+
+ id := ctr.ID()
+
+ rows, err := s.db.Query(inUseQuery, id, id, id, id, id, id, id)
+ if err != nil {
+ return nil, errors.Wrapf(err, "error querying database for containers that depend on container %s", id)
+ }
+ defer rows.Close()
+
+ ids := []string{}
+
+ for rows.Next() {
+ var ctrID string
+ if err := rows.Scan(&ctrID); err != nil {
+ return nil, errors.Wrapf(err, "error scanning container IDs from db rows for container %s", id)
+ }
+
+ ids = append(ids, ctrID)
+ }
+ if err := rows.Err(); err != nil {
+ return nil, errors.Wrapf(err, "error retrieving rows for container %s", id)
+ }
+
+ return ids, nil
+}
+
// RemoveContainer removes the container from the state
func (s *SQLState) RemoveContainer(ctr *Container) error {
const (