feat: Use databaseBackend config repace databaseEtcd

databaseBackend config contains type and url sub config, It can facilitate the expansion of other types of databases in the future.
This commit is contained in:
hudeng
2022-02-11 18:14:26 +08:00
committed by André Roth
parent f29449db14
commit 59bf4501e8
8 changed files with 47 additions and 13 deletions

View File

@@ -76,8 +76,8 @@ ifeq ($(RUN_LONG_TESTS), yes)
if [ ! -e ~/aptly-fixture-db ]; then git clone https://github.com/aptly-dev/aptly-fixture-db.git ~/aptly-fixture-db/; fi
if [ ! -e ~/aptly-fixture-pool ]; then git clone https://github.com/aptly-dev/aptly-fixture-pool.git ~/aptly-fixture-pool/; fi
# TODO: maybe we can skip imgrading levledb data to etcd
PATH=$(BINPATH)/:$(PATH) && . system/env/bin/activate && APTLY_VERSION=$(VERSION) $(PYTHON) system/leveldb2etcd.py --datadir ~/aptly-fixture-db
PATH=$(BINPATH)/:$(PATH) && . system/env/bin/activate && APTLY_ETCD_DATABASE="127.0.0.1:2379" APTLY_VERSION=$(VERSION) $(PYTHON) system/run.py --long $(TESTS)
PATH=$(BINPATH)/:$(PATH) && . system/env/bin/activate && $(PYTHON) system/leveldb2etcd.py --datadir ~/aptly-fixture-db
PATH=$(BINPATH)/:$(PATH) && . system/env/bin/activate && APTLY_DATABASE_TYPE="etcd" APTLY_DATABASE_URL="127.0.0.1:2379" APTLY_VERSION=$(VERSION) $(PYTHON) system/run.py --long $(TESTS)
endif
bench:

View File

@@ -3,6 +3,7 @@ package context
import (
gocontext "context"
"errors"
"fmt"
"math/rand"
"os"
@@ -289,8 +290,15 @@ func (context *AptlyContext) _database() (database.Storage, error) {
if context.database == nil {
var err error
if context.config().DatabaseEtcd != "" {
context.database, err = etcddb.NewDB(context.config().DatabaseEtcd)
if context.config().DatabaseBackend.Type == "etcd" {
context.database, err = etcddb.NewDB(context.config().DatabaseBackend.URL)
} else if context.config().DatabaseBackend.Type == "leveldb" {
if context.config().DatabaseBackend.DbPath != "" {
dbPath := filepath.Join(context.config().RootDir, context.config().DatabaseBackend.DbPath)
context.database, err = goleveldb.NewDB(dbPath)
} else {
return nil, errors.New("leveldb databaseBackend config invalid")
}
} else {
context.database, err = goleveldb.NewDB(context.dbPath())
}

View File

@@ -28,7 +28,10 @@ Configuration file is stored in JSON format (default values shown below):
{
"rootDir": "$HOME/.aptly",
"databaseEtcd": "",
"databaseBackend": {
"type": "",
"url": ""
},
"downloadConcurrency": 4,
"downloadSpeedLimit": 0,
"downloadRetries": 0,
@@ -120,8 +123,8 @@ Options:
the default for downloaded packages (`rootDir`/pool) and
the default for published repositories (`rootDir`/public)
* `databaseEtcd`:
the etcd database connection address is empty by default, which means it is not used
* `databaseBackend`:
the database config; if this config is empty, use levledb backend by default
* `downloadConcurrency`:
is a number of parallel download threads to use when downloading packages

View File

@@ -133,9 +133,17 @@ class BaseTest(object):
aptlyDir = ".aptly"
aptlyConfigFile = ".aptly.conf"
expectedCode = 0
databaseEtcd = os.environ.get("APTLY_ETCD_DATABASE")
if databaseEtcd is None:
databaseEtcd = ""
databaseType = os.environ.get("APTLY_DATABASE_TYPE")
databaseUrl = os.environ.get("APTLY_DATABASE_URL")
if databaseType is None:
databaseType = ""
if databaseUrl is None:
databaseUrl = ""
databaseBackend = {
"type": databaseType,
"url": databaseUrl,
}
configFile = {
"rootDir": f"{os.environ['HOME']}/{aptlyDir}",
@@ -157,6 +165,7 @@ class BaseTest(object):
"logFormat": "default",
"serveInAPIMode": True,
"databaseEtcd": databaseEtcd,
"databaseBackend": databaseBackend,
}
configOverride = {}
environmentOverride = {}

View File

@@ -30,5 +30,6 @@
"logLevel": "debug",
"logFormat": "default",
"serveInAPIMode": true,
"databaseEtcd": ""
"databaseEtcd": "",
"databaseBackend": {}
}

View File

@@ -30,5 +30,6 @@
"logLevel": "debug",
"logFormat": "default",
"serveInAPIMode": false,
"databaseEtcd": ""
"databaseEtcd": "",
"databaseBackend": {}
}

View File

@@ -41,6 +41,14 @@ type ConfigStructure struct { // nolint: maligned
LogFormat string `json:"logFormat"`
ServeInAPIMode bool `json:"serveInAPIMode"`
DatabaseEtcd string `json:"databaseEtcd"`
DatabaseBackend DBConfig `json:"databaseBackend"`
}
// DBConfig
type DBConfig struct {
Type string `json:"type"`
URL string `json:"url"`
DbPath string `json:"dbPath"`
}
type LocalPoolStorage struct {

View File

@@ -144,7 +144,11 @@ func (s *ConfigSuite) TestSaveConfig(c *C) {
" \"logLevel\": \"info\",\n"+
" \"logFormat\": \"json\",\n"+
" \"serveInAPIMode\": false,\n"+
" \"databaseEtcd\": \"\"\n"+
" \"databaseEtcd\": \"\",\n"+
" \"databaseBackend\": {\n"+
" \"type\": \"\",\n"+
" \"url\": \"\"\n"+
" }\n"+
"}")
}