| 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 » // Default database parameters. |
| 18 » METADATA_KEY = "readwrite" | 16 » PROD_DB_HOST = "173.194.253.125" |
| 19 | 17 » PROD_DB_PORT = 3306 |
| 20 » // Path where the SQLite database is stored when running locally. | 18 » PROD_DB_NAME = "buildbot" |
| 21 » SQLITE_DB_PATH = "./testing.db" | |
| 22 | |
| 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 | 19 |
| 34 TABLE_BUILDS = "builds" | 20 TABLE_BUILDS = "builds" |
| 35 TABLE_BUILD_REVISIONS = "buildRevisions" | 21 TABLE_BUILD_REVISIONS = "buildRevisions" |
| 36 TABLE_BUILD_STEPS = "buildSteps" | 22 TABLE_BUILD_STEPS = "buildSteps" |
| 37 ) | 23 ) |
| 38 | 24 |
| 39 var ( | 25 var ( |
| 40 DB *sqlx.DB = nil | 26 DB *sqlx.DB = nil |
| 41 ) | 27 ) |
| 42 | 28 |
| 43 // Setup the database to be shared across the app. | 29 // Setup the database to be shared across the app. |
| 44 func InitDB(conf *database.DatabaseConfig) error { | 30 func InitDB(conf *database.DatabaseConfig) error { |
| 45 » vdb := database.NewVersionedDB(conf) | 31 » db, err := sqlx.Open("mysql", conf.MySQLString) |
| 46 » dbVersion, err := vdb.DBVersion() | |
| 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 { | 32 if err != nil { |
| 66 return fmt.Errorf("Failed to open database: %v", err) | 33 return fmt.Errorf("Failed to open database: %v", err) |
| 67 } | 34 } |
| 35 DB = db |
| 68 return nil | 36 return nil |
| 69 } | 37 } |
| 70 | 38 |
| 71 // Returns the DB connection string for running in production where a | |
| 72 // metadata server is available. If 'local' is true it will always return | |
| 73 // "" (empty string). When used with Init() this will cause it to use a | |
| 74 // local SQLite database. If it's not local and the meta data server is | |
| 75 // unreachable it will terminate. | |
| 76 func ProdDatabaseConfig(local bool) *database.DatabaseConfig { | |
| 77 mysqlStr := "" | |
| 78 sqlitePath := SQLITE_DB_PATH | |
| 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 } | |
| 96 } | |
| 97 | |
| 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{ | 39 var v1_up = []string{ |
| 109 `CREATE TABLE IF NOT EXISTS builds ( | 40 `CREATE TABLE IF NOT EXISTS builds ( |
| 110 builder VARCHAR(100) NOT NULL, | 41 builder VARCHAR(100) NOT NULL, |
| 111 master VARCHAR(100) NOT NULL, | 42 master VARCHAR(100) NOT NULL, |
| 112 number INT NOT NULL, | 43 number INT NOT NULL, |
| 113 gotRevision VARCHAR(40), | 44 gotRevision VARCHAR(40), |
| 114 branch VARCHAR(100) NOT NULL, | 45 branch VARCHAR(100) NOT NULL, |
| 115 results INT, | 46 results INT, |
| 116 buildslave VARCHAR(100) NOT NULL, | 47 buildslave VARCHAR(100) NOT NULL, |
| 117 started DOUBLE, | 48 started DOUBLE, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 145 `DROP TABLE IF EXISTS buildRevisions`, | 76 `DROP TABLE IF EXISTS buildRevisions`, |
| 146 `DROP TABLE IF EXISTS builds`, | 77 `DROP TABLE IF EXISTS builds`, |
| 147 } | 78 } |
| 148 | 79 |
| 149 // Define the migration steps. | 80 // Define the migration steps. |
| 150 // Note: Only add to this list, once a step has landed in version control it | 81 // Note: Only add to this list, once a step has landed in version control it |
| 151 // must not be changed. | 82 // must not be changed. |
| 152 var migrationSteps = []database.MigrationStep{ | 83 var migrationSteps = []database.MigrationStep{ |
| 153 // version 1 | 84 // version 1 |
| 154 { | 85 { |
| 155 » » MySQLUp: v1_up, | 86 » » MySQLUp: v1_up, |
| 156 » » MySQLDown: v1_down, | 87 » » MySQLDown: v1_down, |
| 157 » » SQLiteUp: v1_up, | |
| 158 » » SQLiteDown: v1_down, | |
| 159 }, | 88 }, |
| 160 } | 89 } |
| 90 |
| 91 // MigrationSteps returns the database migration steps. |
| 92 func MigrationSteps() []database.MigrationStep { |
| 93 return migrationSteps |
| 94 } |
| OLD | NEW |