1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
package libpod
import (
"database/sql"
"encoding/json"
"io/ioutil"
"path/filepath"
"time"
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
// the database
func prepareDB(db *sql.DB) (err error) {
// TODO create pod tables
// TODO add Pod ID to CreateStaticContainer as a FOREIGN KEY referencing podStatic(Id)
// TODO add ctr shared namespaces information - A separate table, probably? So we can FOREIGN KEY the ID
// TODO schema migration might be necessary and should be handled here
// TODO add a table for the runtime, and refuse to load the database if the runtime configuration
// does not match the one in the database
// Enable foreign keys in SQLite
if _, err := db.Exec("PRAGMA foreign_keys = ON;"); err != nil {
return errors.Wrapf(err, "error enabling foreign key support in database")
}
// Create a table for unchanging container data
const createCtr = `
CREATE TABLE IF NOT EXIST containers(
Id TEXT NOT NULL PRIMARY KEY,
Name TEXT NOT NULL UNIQUE,
MountLabel TEXT NOT NULL,
StaticDir TEXT NOT NULL,
Stdin INTEGER NOT NULL,
LabelsJSON TEXT NOT NULL,
StopSignal INTEGER NOT NULL,
CreatedTime TEXT NOT NULL
RootfsImageID TEXT NOT NULL,
RootfsImageName TEXT NOT NULL,
UseImageConfig INTEGER NOT NULL,
CHECK (Stdin IN (0, 1)),
CHECK (UseImageConfig IN (0, 1)),
CHECK (StopSignal>0)
);
`
// Create a table for changing container state
const createCtrState = `
CREATE TABLE IF NOT EXIST containerState(
Id TEXT NOT NULL PRIMARY KEY,
State INTEGER NOT NULL,
ConfigPath TEXT NOT NULL,
RunDir TEXT NOT NULL,
Mountpoint TEXT NOT NULL,
StartedTime TEXT NUT NULL,
FinishedTime TEXT NOT NULL,
ExitCode INTEGER NOT NULL,
CHECK (State>0),
FOREIGN KEY (Id) REFERENCES containers(Id) DEFERRABLE INITIALLY DEFERRED
);
`
// Create the tables
tx, err := 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 create tables: %v", err2)
}
}
}()
if _, err := tx.Exec(createCtr); err != nil {
return errors.Wrapf(err, "error creating containers table in database")
}
if _, err := tx.Exec(createCtrState); err != nil {
return errors.Wrapf(err, "error creating container state table in database")
}
if err := tx.Commit(); err != nil {
return errors.Wrapf(err, "error committing table creation transaction in database")
}
return nil
}
// Get filename for OCI spec on disk
func getSpecPath(specsDir, id string) string {
return filepath.Join(specsDir, id)
}
// Convert a bool into SQL-readable format
func boolToSQL(b bool) int {
if b {
return 1
}
return 0
}
// Convert a bool from SQL-readable format
func boolFromSQL(i int) bool {
if i == 0 {
return false
}
return true
}
// Convert a time.Time into SQL-readable format
func timeToSQL(t time.Time) string {
return t.Format(time.RFC3339Nano)
}
// Convert a SQL-readable time back to a time.Time
func timeFromSQL(s string) (time.Time, error) {
return time.Parse(time.RFC3339Nano, s)
}
// Interface to abstract sql.Rows and sql.Row so they can both be used
type scannable interface {
Scan(dest ...interface{}) error
}
// Read a single container from a single row result in the database
func ctrFromScannable(row scannable, runtime *Runtime, specsDir string) (*Container, error) {
var (
id string
name string
mountLabel string
staticDir string
stdin int
labelsJSON string
stopSignal uint
createdTimeString string
rootfsImageID string
rootfsImageName string
useImageConfig int
state int
configPath string
runDir string
mountpoint string
startedTimeString string
finishedTimeString string
exitCode int32
)
err := row.Scan(
&id,
&name,
&mountLabel,
&staticDir,
&stdin,
&labelsJSON,
&stopSignal,
&createdTimeString,
&rootfsImageID,
&rootfsImageName,
&useImageConfig,
&state,
&configPath,
&runDir,
&mountpoint,
&startedTimeString,
&finishedTimeString,
&exitCode)
if err != nil {
if err == sql.ErrNoRows {
return nil, ErrNoSuchCtr
}
return nil, errors.Wrapf(err, "error parsing database row into container")
}
ctr := new(Container)
ctr.config = new(containerConfig)
ctr.state = new(containerRuntimeInfo)
ctr.config.ID = id
ctr.config.Name = name
ctr.config.RootfsImageID = rootfsImageID
ctr.config.RootfsImageName = rootfsImageName
ctr.config.UseImageConfig = boolFromSQL(useImageConfig)
ctr.config.MountLabel = mountLabel
ctr.config.StaticDir = staticDir
ctr.config.Stdin = boolFromSQL(stdin)
ctr.config.StopSignal = stopSignal
ctr.state.State = ContainerState(state)
ctr.state.ConfigPath = configPath
ctr.state.RunDir = runDir
ctr.state.Mountpoint = mountpoint
ctr.state.ExitCode = exitCode
// TODO should we store this in the database separately instead?
if ctr.state.Mountpoint != "" {
ctr.state.Mounted = true
}
labels := make(map[string]string)
if err := json.Unmarshal([]byte(labelsJSON), labels); err != nil {
return nil, errors.Wrapf(err, "error parsing container %s labels JSON", id)
}
ctr.config.Labels = labels
createdTime, err := timeFromSQL(createdTimeString)
if err != nil {
return nil, errors.Wrapf(err, "error parsing container %s created time", id)
}
ctr.config.CreatedTime = createdTime
startedTime, err := timeFromSQL(startedTimeString)
if err != nil {
return nil, errors.Wrapf(err, "error parsing container %s started time", id)
}
ctr.state.StartedTime = startedTime
finishedTime, err := timeFromSQL(finishedTimeString)
if err != nil {
return nil, errors.Wrapf(err, "error parsing container %s finished time", id)
}
ctr.state.FinishedTime = finishedTime
ctr.valid = true
ctr.runtime = runtime
// Retrieve the spec from disk
ociSpec := new(spec.Spec)
specPath := getSpecPath(specsDir, id)
fileContents, err := ioutil.ReadFile(specPath)
if err != nil {
return nil, errors.Wrapf(err, "error reading container %s OCI spec", id)
}
if err := json.Unmarshal(fileContents, ociSpec); err != nil {
return nil, errors.Wrapf(err, "error parsing container %s OCI spec", id)
}
ctr.config.Spec = ociSpec
return ctr, nil
}
|