mirror of
https://github.com/drakkan/sftpgo.git
synced 2024-11-26 01:20:29 +00:00
initprovider: check if the provider is already initialized
exit with code 0 if no initialization is required
This commit is contained in:
parent
2746c0b0f1
commit
600a107699
9 changed files with 22 additions and 15 deletions
|
@ -46,6 +46,8 @@ Please take a look at the usage below to customize the options.`,
|
||||||
err = dataprovider.InitializeDatabase(providerConf, configDir)
|
err = dataprovider.InitializeDatabase(providerConf, configDir)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
logger.DebugToConsole("Data provider successfully initialized")
|
logger.DebugToConsole("Data provider successfully initialized")
|
||||||
|
} else if err == dataprovider.ErrNoInitRequired {
|
||||||
|
logger.DebugToConsole("%v", err.Error())
|
||||||
} else {
|
} else {
|
||||||
logger.WarnToConsole("Unable to initialize data provider: %v", err)
|
logger.WarnToConsole("Unable to initialize data provider: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
|
@ -703,7 +703,7 @@ func (p BoltProvider) reloadConfig() error {
|
||||||
|
|
||||||
// initializeDatabase does nothing, no initilization is needed for bolt provider
|
// initializeDatabase does nothing, no initilization is needed for bolt provider
|
||||||
func (p BoltProvider) initializeDatabase() error {
|
func (p BoltProvider) initializeDatabase() error {
|
||||||
return errNoInitRequired
|
return ErrNoInitRequired
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p BoltProvider) migrateDatabase() error {
|
func (p BoltProvider) migrateDatabase() error {
|
||||||
|
|
|
@ -95,11 +95,13 @@ var (
|
||||||
// ErrNoAuthTryed defines the error for connection closed before authentication
|
// ErrNoAuthTryed defines the error for connection closed before authentication
|
||||||
ErrNoAuthTryed = errors.New("no auth tryed")
|
ErrNoAuthTryed = errors.New("no auth tryed")
|
||||||
// ValidProtocols defines all the valid protcols
|
// ValidProtocols defines all the valid protcols
|
||||||
ValidProtocols = []string{"SSH", "FTP", "DAV"}
|
ValidProtocols = []string{"SSH", "FTP", "DAV"}
|
||||||
config Config
|
// ErrNoInitRequired defines the error returned by InitProvider if no inizialization is required
|
||||||
provider Provider
|
ErrNoInitRequired = errors.New("Data provider initialization is not required")
|
||||||
sqlPlaceholders []string
|
config Config
|
||||||
hashPwdPrefixes = []string{argonPwdPrefix, bcryptPwdPrefix, pbkdf2SHA1Prefix, pbkdf2SHA256Prefix,
|
provider Provider
|
||||||
|
sqlPlaceholders []string
|
||||||
|
hashPwdPrefixes = []string{argonPwdPrefix, bcryptPwdPrefix, pbkdf2SHA1Prefix, pbkdf2SHA256Prefix,
|
||||||
pbkdf2SHA512Prefix, pbkdf2SHA256B64SaltPrefix, md5cryptPwdPrefix, md5cryptApr1PwdPrefix, sha512cryptPwdPrefix}
|
pbkdf2SHA512Prefix, pbkdf2SHA256B64SaltPrefix, md5cryptPwdPrefix, md5cryptApr1PwdPrefix, sha512cryptPwdPrefix}
|
||||||
pbkdfPwdPrefixes = []string{pbkdf2SHA1Prefix, pbkdf2SHA256Prefix, pbkdf2SHA512Prefix, pbkdf2SHA256B64SaltPrefix}
|
pbkdfPwdPrefixes = []string{pbkdf2SHA1Prefix, pbkdf2SHA256Prefix, pbkdf2SHA512Prefix, pbkdf2SHA256B64SaltPrefix}
|
||||||
pbkdfPwdB64SaltPrefixes = []string{pbkdf2SHA256B64SaltPrefix}
|
pbkdfPwdB64SaltPrefixes = []string{pbkdf2SHA256B64SaltPrefix}
|
||||||
|
@ -108,7 +110,6 @@ var (
|
||||||
availabilityTicker *time.Ticker
|
availabilityTicker *time.Ticker
|
||||||
availabilityTickerDone chan bool
|
availabilityTickerDone chan bool
|
||||||
errWrongPassword = errors.New("password does not match")
|
errWrongPassword = errors.New("password does not match")
|
||||||
errNoInitRequired = errors.New("initialization is not required for this data provider")
|
|
||||||
credentialsDirPath string
|
credentialsDirPath string
|
||||||
sqlTableUsers = "users"
|
sqlTableUsers = "users"
|
||||||
sqlTableFolders = "folders"
|
sqlTableFolders = "folders"
|
||||||
|
@ -422,7 +423,7 @@ func InitializeDatabase(cnf Config, basePath string) error {
|
||||||
config = cnf
|
config = cnf
|
||||||
|
|
||||||
if config.Driver == BoltDataProviderName || config.Driver == MemoryDataProviderName {
|
if config.Driver == BoltDataProviderName || config.Driver == MemoryDataProviderName {
|
||||||
return errNoInitRequired
|
return ErrNoInitRequired
|
||||||
}
|
}
|
||||||
err := createProvider(basePath)
|
err := createProvider(basePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -667,7 +667,7 @@ func (p MemoryProvider) reloadConfig() error {
|
||||||
|
|
||||||
// initializeDatabase does nothing, no initilization is needed for memory provider
|
// initializeDatabase does nothing, no initilization is needed for memory provider
|
||||||
func (p MemoryProvider) initializeDatabase() error {
|
func (p MemoryProvider) initializeDatabase() error {
|
||||||
return errNoInitRequired
|
return ErrNoInitRequired
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p MemoryProvider) migrateDatabase() error {
|
func (p MemoryProvider) migrateDatabase() error {
|
||||||
|
|
|
@ -196,7 +196,7 @@ func (p MySQLProvider) initializeDatabase() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p MySQLProvider) migrateDatabase() error {
|
func (p MySQLProvider) migrateDatabase() error {
|
||||||
dbVersion, err := sqlCommonGetDatabaseVersion(p.dbHandle)
|
dbVersion, err := sqlCommonGetDatabaseVersion(p.dbHandle, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -195,7 +195,7 @@ func (p PGSQLProvider) initializeDatabase() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p PGSQLProvider) migrateDatabase() error {
|
func (p PGSQLProvider) migrateDatabase() error {
|
||||||
dbVersion, err := sqlCommonGetDatabaseVersion(p.dbHandle)
|
dbVersion, err := sqlCommonGetDatabaseVersion(p.dbHandle, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -766,7 +766,7 @@ func sqlCommonRollbackTransaction(tx *sql.Tx) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func sqlCommonGetDatabaseVersion(dbHandle *sql.DB) (schemaVersion, error) {
|
func sqlCommonGetDatabaseVersion(dbHandle *sql.DB, showInitWarn bool) (schemaVersion, error) {
|
||||||
var result schemaVersion
|
var result schemaVersion
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout)
|
ctx, cancel := context.WithTimeout(context.Background(), defaultSQLQueryTimeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
@ -774,7 +774,7 @@ func sqlCommonGetDatabaseVersion(dbHandle *sql.DB) (schemaVersion, error) {
|
||||||
stmt, err := dbHandle.PrepareContext(ctx, q)
|
stmt, err := dbHandle.PrepareContext(ctx, q)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
providerLog(logger.LevelWarn, "error preparing database query %#v: %v", q, err)
|
||||||
if strings.Contains(err.Error(), sqlTableSchemaVersion) {
|
if showInitWarn && strings.Contains(err.Error(), sqlTableSchemaVersion) {
|
||||||
logger.WarnToConsole("database query error, did you forgot to run the \"initprovider\" command?")
|
logger.WarnToConsole("database query error, did you forgot to run the \"initprovider\" command?")
|
||||||
}
|
}
|
||||||
return result, err
|
return result, err
|
||||||
|
|
|
@ -194,6 +194,10 @@ func (p SQLiteProvider) reloadConfig() error {
|
||||||
|
|
||||||
// initializeDatabase creates the initial database structure
|
// initializeDatabase creates the initial database structure
|
||||||
func (p SQLiteProvider) initializeDatabase() error {
|
func (p SQLiteProvider) initializeDatabase() error {
|
||||||
|
dbVersion, err := sqlCommonGetDatabaseVersion(p.dbHandle, false)
|
||||||
|
if err == nil && dbVersion.Version > 0 {
|
||||||
|
return ErrNoInitRequired
|
||||||
|
}
|
||||||
sqlUsers := strings.Replace(sqliteUsersTableSQL, "{{users}}", sqlTableUsers, 1)
|
sqlUsers := strings.Replace(sqliteUsersTableSQL, "{{users}}", sqlTableUsers, 1)
|
||||||
tx, err := p.dbHandle.Begin()
|
tx, err := p.dbHandle.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -218,7 +222,7 @@ func (p SQLiteProvider) initializeDatabase() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p SQLiteProvider) migrateDatabase() error {
|
func (p SQLiteProvider) migrateDatabase() error {
|
||||||
dbVersion, err := sqlCommonGetDatabaseVersion(p.dbHandle)
|
dbVersion, err := sqlCommonGetDatabaseVersion(p.dbHandle, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue