2018-02-20 00:24:10 +02:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-10-22 01:14:02 +02:00
|
|
|
package datastore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"os"
|
2015-11-10 22:32:09 +02:00
|
|
|
"time"
|
2015-10-22 01:14:02 +02:00
|
|
|
|
2021-06-22 12:34:35 +02:00
|
|
|
"github.com/russross/meddler"
|
2021-09-23 13:33:59 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/store"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server/store/datastore/ddl"
|
2015-10-22 01:14:02 +02:00
|
|
|
|
2020-05-18 16:46:13 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-10-22 01:14:02 +02:00
|
|
|
)
|
|
|
|
|
2016-03-25 21:54:16 +02:00
|
|
|
// datastore is an implementation of a model.Store built on top
|
|
|
|
// of the sql/database driver with a relational database backend.
|
|
|
|
type datastore struct {
|
|
|
|
*sql.DB
|
2017-03-28 11:13:13 +02:00
|
|
|
|
|
|
|
driver string
|
|
|
|
config string
|
2016-03-25 21:54:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a database connection for the given driver and datasource
|
|
|
|
// and returns a new Store.
|
2015-10-28 01:48:05 +02:00
|
|
|
func New(driver, config string) store.Store {
|
2017-03-28 11:13:13 +02:00
|
|
|
return &datastore{
|
|
|
|
DB: open(driver, config),
|
|
|
|
driver: driver,
|
|
|
|
config: config,
|
|
|
|
}
|
2015-10-28 03:48:04 +02:00
|
|
|
}
|
|
|
|
|
2016-03-25 21:54:16 +02:00
|
|
|
// From returns a Store using an existing database connection.
|
2015-10-28 03:48:04 +02:00
|
|
|
func From(db *sql.DB) store.Store {
|
2017-03-28 11:13:13 +02:00
|
|
|
return &datastore{DB: db}
|
2015-10-22 01:14:02 +02:00
|
|
|
}
|
|
|
|
|
2016-03-25 21:54:16 +02:00
|
|
|
// open opens a new database connection with the specified
|
2015-10-22 01:14:02 +02:00
|
|
|
// driver and connection string and returns a store.
|
2016-03-25 21:54:16 +02:00
|
|
|
func open(driver, config string) *sql.DB {
|
2015-10-22 01:14:02 +02:00
|
|
|
db, err := sql.Open(driver, config)
|
|
|
|
if err != nil {
|
2016-03-25 21:54:16 +02:00
|
|
|
logrus.Errorln(err)
|
|
|
|
logrus.Fatalln("database connection failed")
|
2015-10-22 01:14:02 +02:00
|
|
|
}
|
2015-11-11 20:23:44 +02:00
|
|
|
if driver == "mysql" {
|
|
|
|
// per issue https://github.com/go-sql-driver/mysql/issues/257
|
|
|
|
db.SetMaxIdleConns(0)
|
|
|
|
}
|
2015-11-10 22:32:09 +02:00
|
|
|
|
2015-10-22 01:14:02 +02:00
|
|
|
setupMeddler(driver)
|
|
|
|
|
2015-11-10 22:32:09 +02:00
|
|
|
if err := pingDatabase(db); err != nil {
|
2016-03-25 21:54:16 +02:00
|
|
|
logrus.Errorln(err)
|
|
|
|
logrus.Fatalln("database ping attempts failed")
|
2015-11-10 22:32:09 +02:00
|
|
|
}
|
|
|
|
|
2015-10-22 01:14:02 +02:00
|
|
|
if err := setupDatabase(driver, db); err != nil {
|
2016-03-25 21:54:16 +02:00
|
|
|
logrus.Errorln(err)
|
|
|
|
logrus.Fatalln("migration failed")
|
2015-10-22 01:14:02 +02:00
|
|
|
}
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
2017-03-28 11:13:13 +02:00
|
|
|
// openTest opens a new database connection for testing purposes.
|
2015-10-22 01:14:02 +02:00
|
|
|
// The database driver and connection string are provided by
|
|
|
|
// environment variables, with fallback to in-memory sqlite.
|
|
|
|
func openTest() *sql.DB {
|
|
|
|
var (
|
|
|
|
driver = "sqlite3"
|
|
|
|
config = ":memory:"
|
|
|
|
)
|
Clean up config environment variables for server and agent (#218)
The goal here is to make consistent use of configuration environment variables prefixed `WOODPECKER_`. Where several variants existed, this PR aims to remove all but one option, leaving the most explicit.
This PR only changes server and agent code, but not documentation, in order to keep the PR digestible. Once we have consensus that this is correct, I'll change docs accordingly.
User (rather: admin) facing changes in this PR:
- In general, support for all server and agent config environment variables (env vars) starting with `DRONE_` is removed. The according `WOODPECKER_*` variables must be used instead.
- The env var `WOODPECKER_HOST` replaces `DRONE_HOST`, and `DRONE_SERVER_HOST`.
- The env var `WOODPECKER_AGENT_SECRET` is used to configure the shared secret which agents use to authenticate against the server. It replaces `WOODPECKER_SECRET`, `DRONE_SECRET`, `WOODPECKER_PASSWORD`, `DRONE_PASSWORD`, and `DRONE_AGENT_SECRET`.
- The env var `WOODPECKER_DATABASE_DRIVER` replaces `DRONE_DATABASE_DRIVER` and `DATABASE_DRIVER`.
- The env var `WOODPECKER_DATABASE_DATASOURCE` replaces `DRONE_DATABASE_DATASOURCE` and `DATABASE_CONFIG`.
2021-09-28 15:43:44 +02:00
|
|
|
if os.Getenv("WOODPECKER_DATABASE_DRIVER") != "" {
|
|
|
|
driver = os.Getenv("WOODPECKER_DATABASE_DRIVER")
|
|
|
|
config = os.Getenv("WOODPECKER_DATABASE_CONFIG")
|
2015-10-22 01:14:02 +02:00
|
|
|
}
|
2016-03-25 21:54:16 +02:00
|
|
|
return open(driver, config)
|
2015-10-22 01:14:02 +02:00
|
|
|
}
|
|
|
|
|
2017-03-28 11:13:13 +02:00
|
|
|
// newTest creates a new database connection for testing purposes.
|
|
|
|
// The database driver and connection string are provided by
|
|
|
|
// environment variables, with fallback to in-memory sqlite.
|
|
|
|
func newTest() *datastore {
|
|
|
|
var (
|
|
|
|
driver = "sqlite3"
|
|
|
|
config = ":memory:"
|
|
|
|
)
|
Clean up config environment variables for server and agent (#218)
The goal here is to make consistent use of configuration environment variables prefixed `WOODPECKER_`. Where several variants existed, this PR aims to remove all but one option, leaving the most explicit.
This PR only changes server and agent code, but not documentation, in order to keep the PR digestible. Once we have consensus that this is correct, I'll change docs accordingly.
User (rather: admin) facing changes in this PR:
- In general, support for all server and agent config environment variables (env vars) starting with `DRONE_` is removed. The according `WOODPECKER_*` variables must be used instead.
- The env var `WOODPECKER_HOST` replaces `DRONE_HOST`, and `DRONE_SERVER_HOST`.
- The env var `WOODPECKER_AGENT_SECRET` is used to configure the shared secret which agents use to authenticate against the server. It replaces `WOODPECKER_SECRET`, `DRONE_SECRET`, `WOODPECKER_PASSWORD`, `DRONE_PASSWORD`, and `DRONE_AGENT_SECRET`.
- The env var `WOODPECKER_DATABASE_DRIVER` replaces `DRONE_DATABASE_DRIVER` and `DATABASE_DRIVER`.
- The env var `WOODPECKER_DATABASE_DATASOURCE` replaces `DRONE_DATABASE_DATASOURCE` and `DATABASE_CONFIG`.
2021-09-28 15:43:44 +02:00
|
|
|
if os.Getenv("WOODPECKER_DATABASE_DRIVER") != "" {
|
|
|
|
driver = os.Getenv("WOODPECKER_DATABASE_DRIVER")
|
|
|
|
config = os.Getenv("WOODPECKER_DATABASE_CONFIG")
|
2017-03-28 11:13:13 +02:00
|
|
|
}
|
|
|
|
return &datastore{
|
|
|
|
DB: open(driver, config),
|
|
|
|
driver: driver,
|
|
|
|
config: config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-10 22:32:09 +02:00
|
|
|
// helper function to ping the database with backoff to ensure
|
|
|
|
// a connection can be established before we proceed with the
|
|
|
|
// database setup and migration.
|
|
|
|
func pingDatabase(db *sql.DB) (err error) {
|
|
|
|
for i := 0; i < 30; i++ {
|
|
|
|
err = db.Ping()
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
2016-03-25 21:54:16 +02:00
|
|
|
logrus.Infof("database ping failed. retry in 1s")
|
2015-11-10 22:32:09 +02:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-08 18:35:56 +02:00
|
|
|
// helper function to setup the database by performing
|
2015-10-22 01:14:02 +02:00
|
|
|
// automated database migration steps.
|
|
|
|
func setupDatabase(driver string, db *sql.DB) error {
|
2017-05-13 09:56:23 +02:00
|
|
|
return ddl.Migrate(driver, db)
|
2015-10-22 01:14:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// helper function to setup the meddler default driver
|
|
|
|
// based on the selected driver name.
|
|
|
|
func setupMeddler(driver string) {
|
|
|
|
switch driver {
|
|
|
|
case "sqlite3":
|
|
|
|
meddler.Default = meddler.SQLite
|
|
|
|
case "mysql":
|
|
|
|
meddler.Default = meddler.MySQL
|
|
|
|
case "postgres":
|
|
|
|
meddler.Default = meddler.PostgreSQL
|
|
|
|
}
|
|
|
|
}
|