Add tracking for container exec sessions to DB

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

Closes: #412
Approved by: baude
This commit is contained in:
Matthew Heon
2018-02-23 15:28:56 -05:00
committed by Atomic Bot
parent 920b66707e
commit 2a0c949b9b
6 changed files with 217 additions and 12 deletions

View File

@ -14,7 +14,7 @@ import (
// DBSchema is the current DB schema version
// Increments every time a change is made to the database's tables
const DBSchema = 11
const DBSchema = 12
// SQLState is a state implementation backed by a persistent SQLite3 database
type SQLState struct {
@ -102,7 +102,8 @@ func (s *SQLState) Refresh() (err error) {
Pid=?,
NetNSPath=?,
IPAddress=?,
SubnetMask=?;`
SubnetMask=?,
ExecSessions=?;`
if !s.valid {
return ErrDBClosed
@ -132,7 +133,8 @@ func (s *SQLState) Refresh() (err error) {
0,
"",
"",
"")
"",
"{}")
if err != nil {
return errors.Wrapf(err, "error refreshing database state")
}
@ -269,7 +271,8 @@ func (s *SQLState) UpdateContainer(ctr *Container) error {
Pid,
NetNSPath,
IPAddress,
SubnetMask
SubnetMask,
ExecSessions
FROM containerState WHERE ID=?;`
var (
@ -285,6 +288,7 @@ func (s *SQLState) UpdateContainer(ctr *Container) error {
netNSPath string
ipAddress string
subnetMask string
execSessions string
)
if !s.valid {
@ -308,7 +312,8 @@ func (s *SQLState) UpdateContainer(ctr *Container) error {
&pid,
&netNSPath,
&ipAddress,
&subnetMask)
&subnetMask,
&execSessions)
if err != nil {
// The container may not exist in the database
if err == sql.ErrNoRows {
@ -333,6 +338,11 @@ func (s *SQLState) UpdateContainer(ctr *Container) error {
newState.IPAddress = ipAddress
newState.SubnetMask = subnetMask
newState.ExecSessions = make(map[string]int)
if err := json.Unmarshal([]byte(execSessions), &newState.ExecSessions); err != nil {
return errors.Wrapf(err, "error parsing container %s exec sessions", ctr.ID())
}
if newState.Mountpoint != "" {
newState.Mounted = true
}
@ -395,13 +405,19 @@ func (s *SQLState) SaveContainer(ctr *Container) (err error) {
Pid=?,
NetNSPath=?,
IPAddress=?,
SubnetMask=?
SubnetMask=?,
ExecSessions=?
WHERE Id=?;`
if !ctr.valid {
return ErrCtrRemoved
}
execSessionsJSON, err := json.Marshal(ctr.state.ExecSessions)
if err != nil {
return errors.Wrapf(err, "error marshalling container %s exec sessions", ctr.ID())
}
netNSPath := ""
if ctr.state.NetNS != nil {
netNSPath = ctr.state.NetNS.Path()
@ -439,6 +455,7 @@ func (s *SQLState) SaveContainer(ctr *Container) (err error) {
netNSPath,
ctr.state.IPAddress,
ctr.state.SubnetMask,
execSessionsJSON,
ctr.ID())
if err != nil {
return errors.Wrapf(err, "error updating container %s state in database", ctr.ID())