OLD | NEW |
1 package buildbot | 1 package buildbot |
2 | 2 |
3 /* | 3 /* |
4 Store/Retrieve buildbot data in a database. | 4 Store/Retrieve buildbot data in a database. |
5 */ | 5 */ |
6 | 6 |
7 import ( | 7 import ( |
8 "fmt" | 8 "fmt" |
9 | 9 |
10 "github.com/golang/glog" | |
11 "github.com/jmoiron/sqlx" | 10 "github.com/jmoiron/sqlx" |
12 "skia.googlesource.com/buildbot.git/go/database" | 11 "skia.googlesource.com/buildbot.git/go/database" |
13 "skia.googlesource.com/buildbot.git/go/metadata" | |
14 ) | 12 ) |
15 | 13 |
16 const ( | 14 const ( |
17 // Key of the password for the readwrite user. | 15 // Key of the password for the readwrite user. |
18 METADATA_KEY = "readwrite" | 16 METADATA_KEY = "readwrite" |
19 | 17 |
20 » // Path where the SQLite database is stored when running locally. | 18 » // Production database. |
21 » SQLITE_DB_PATH = "./testing.db" | 19 » PROD_DATABASE = "buildbot" |
22 | 20 » PROD_DB_HOST = "tcp(173.194.253.125:3306)" |
23 » DATABASE = "buildbot" | |
24 | |
25 » // Template to generate the database connection string in production. | |
26 » // The IP address of the database is found here: | |
27 » // https://console.developers.google.com/project/31977622648/sql/inst
ances/skia-master-db/overview | |
28 » // And 3306 is the default port for MySQL. | |
29 » DB_CONN_TMPL = "%s:%s@tcp(173.194.253.125:3306)/%s?parseTime=true" | |
30 | |
31 » // Username of the read/write user. | |
32 » RW_USER = "readwrite" | |
33 | 21 |
34 TABLE_BUILDS = "builds" | 22 TABLE_BUILDS = "builds" |
35 TABLE_BUILD_REVISIONS = "buildRevisions" | 23 TABLE_BUILD_REVISIONS = "buildRevisions" |
36 TABLE_BUILD_STEPS = "buildSteps" | 24 TABLE_BUILD_STEPS = "buildSteps" |
37 ) | 25 ) |
38 | 26 |
39 var ( | 27 var ( |
40 DB *sqlx.DB = nil | 28 DB *sqlx.DB = nil |
41 ) | 29 ) |
42 | 30 |
43 // Setup the database to be shared across the app. | 31 // Setup the database to be shared across the app. |
44 func InitDB(conf *database.DatabaseConfig) error { | 32 func InitDB(local bool) error { |
45 » vdb := database.NewVersionedDB(conf) | 33 » conf := databaseConfig(local) |
46 » dbVersion, err := vdb.DBVersion() | 34 » db, err := sqlx.Open("mysql", conf.MySQLString) |
47 » if err != nil { | |
48 » » return fmt.Errorf("Could not determine database version: %v", er
r) | |
49 » } | |
50 » maxDBVersion := vdb.MaxDBVersion() | |
51 » if dbVersion < maxDBVersion { | |
52 » » glog.Infof("Migrating DB to version: %d", maxDBVersion) | |
53 » » if err = vdb.Migrate(maxDBVersion); err != nil { | |
54 » » » return fmt.Errorf("Could not migrate DB: %v", err) | |
55 » » } | |
56 » } | |
57 » if err = vdb.Close(); err != nil { | |
58 » » return fmt.Errorf("Could not close database: %v", err) | |
59 » } | |
60 » if conf.MySQLString != "" { | |
61 » » DB, err = sqlx.Open("mysql", conf.MySQLString) | |
62 » } else { | |
63 » » DB, err = sqlx.Open("sqlite3", conf.SQLiteFilePath) | |
64 » } | |
65 if err != nil { | 35 if err != nil { |
66 return fmt.Errorf("Failed to open database: %v", err) | 36 return fmt.Errorf("Failed to open database: %v", err) |
67 } | 37 } |
| 38 DB = db |
68 return nil | 39 return nil |
69 } | 40 } |
70 | 41 |
71 // Returns the DB connection string for running in production where a | 42 // databaseConfig returns the DB connection config. If 'local' is true it |
72 // metadata server is available. If 'local' is true it will always return | 43 // returns a config suitable for local testing. Otherwise it assumes that it's |
73 // "" (empty string). When used with Init() this will cause it to use a | 44 // running in production and a metadata server is available. |
74 // local SQLite database. If it's not local and the meta data server is | 45 func databaseConfig(local bool) *database.DatabaseConfig { |
75 // unreachable it will terminate. | 46 » if local { |
76 func ProdDatabaseConfig(local bool) *database.DatabaseConfig { | 47 » » return database.LocalDatabaseConfig(PROD_DATABASE, migrationStep
s) |
77 » mysqlStr := "" | 48 » } else { |
78 » sqlitePath := SQLITE_DB_PATH | 49 » » return database.ProdDatabaseConfig(PROD_DB_HOST, PROD_DATABASE,
migrationSteps) |
79 | |
80 » // We are in the production environment, so we look up the parameters. | |
81 » if !local { | |
82 » » // First, get the password from the metadata server. | |
83 » » // See https://developers.google.com/compute/docs/metadata#custo
m. | |
84 » » password, err := metadata.Get(METADATA_KEY) | |
85 » » if err != nil { | |
86 » » » glog.Fatalf("Failed to find metadata. Use 'local' flag w
hen running locally.") | |
87 » » } | |
88 » » mysqlStr, sqlitePath = fmt.Sprintf(DB_CONN_TMPL, RW_USER, passwo
rd, DATABASE), "" | |
89 » } | |
90 | |
91 » return &database.DatabaseConfig{ | |
92 » » MySQLString: mysqlStr, | |
93 » » SQLiteFilePath: sqlitePath, | |
94 » » MigrationSteps: migrationSteps, | |
95 } | 50 } |
96 } | 51 } |
97 | 52 |
98 // Returns a DB connection string for running a local testing MySQL instance. | |
99 func LocalMySQLTestDatabaseConfig(user, password string) *database.DatabaseConfi
g { | |
100 mysqlStr := fmt.Sprintf("%s:%s@/sk_testing", user, password) | |
101 return &database.DatabaseConfig{ | |
102 MySQLString: mysqlStr, | |
103 SQLiteFilePath: "", | |
104 MigrationSteps: migrationSteps, | |
105 } | |
106 } | |
107 | |
108 var v1_up = []string{ | 53 var v1_up = []string{ |
109 `CREATE TABLE IF NOT EXISTS builds ( | 54 `CREATE TABLE IF NOT EXISTS builds ( |
110 builder VARCHAR(100) NOT NULL, | 55 builder VARCHAR(100) NOT NULL, |
111 master VARCHAR(100) NOT NULL, | 56 master VARCHAR(100) NOT NULL, |
112 number INT NOT NULL, | 57 number INT NOT NULL, |
113 gotRevision VARCHAR(40), | 58 gotRevision VARCHAR(40), |
114 branch VARCHAR(100) NOT NULL, | 59 branch VARCHAR(100) NOT NULL, |
115 results INT, | 60 results INT, |
116 buildslave VARCHAR(100) NOT NULL, | 61 buildslave VARCHAR(100) NOT NULL, |
117 started DOUBLE, | 62 started DOUBLE, |
(...skipping 27 matching lines...) Expand all Loading... |
145 `DROP TABLE IF EXISTS buildRevisions`, | 90 `DROP TABLE IF EXISTS buildRevisions`, |
146 `DROP TABLE IF EXISTS builds`, | 91 `DROP TABLE IF EXISTS builds`, |
147 } | 92 } |
148 | 93 |
149 // Define the migration steps. | 94 // Define the migration steps. |
150 // Note: Only add to this list, once a step has landed in version control it | 95 // Note: Only add to this list, once a step has landed in version control it |
151 // must not be changed. | 96 // must not be changed. |
152 var migrationSteps = []database.MigrationStep{ | 97 var migrationSteps = []database.MigrationStep{ |
153 // version 1 | 98 // version 1 |
154 { | 99 { |
155 » » MySQLUp: v1_up, | 100 » » MySQLUp: v1_up, |
156 » » MySQLDown: v1_down, | 101 » » MySQLDown: v1_down, |
157 » » SQLiteUp: v1_up, | |
158 » » SQLiteDown: v1_down, | |
159 }, | 102 }, |
160 } | 103 } |
| 104 |
| 105 // MigrationSteps returns the database migration steps. |
| 106 func MigrationSteps() []database.MigrationStep { |
| 107 return migrationSteps |
| 108 } |
OLD | NEW |