2023-01-03 09:18:30 +00:00
|
|
|
// Copyright (C) 2019-2023 Nicola Murino
|
2022-07-17 18:16:00 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-17 18:16:00 +00:00
|
|
|
|
2020-02-08 13:44:25 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-06-20 12:30:46 +00:00
|
|
|
"os"
|
|
|
|
|
2020-05-06 17:36:34 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/config"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/logger"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/service"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/util"
|
2020-02-08 13:44:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
initProviderCmd = &cobra.Command{
|
|
|
|
Use: "initprovider",
|
2021-10-03 06:14:57 +00:00
|
|
|
Short: "Initialize and/or updates the configured data provider",
|
2020-07-09 16:58:22 +00:00
|
|
|
Long: `This command reads the data provider connection details from the specified
|
2020-10-05 17:42:33 +00:00
|
|
|
configuration file and creates the initial structure or update the existing one,
|
|
|
|
as needed.
|
2020-02-08 13:44:25 +00:00
|
|
|
|
2020-10-05 17:42:33 +00:00
|
|
|
Some data providers such as bolt and memory does not require an initialization
|
|
|
|
but they could require an update to the existing data after upgrading SFTPGo.
|
2020-02-08 13:44:25 +00:00
|
|
|
|
2020-10-05 17:42:33 +00:00
|
|
|
For SQLite/bolt providers the database file will be auto-created if missing.
|
2020-02-08 13:44:25 +00:00
|
|
|
|
2020-07-09 16:58:22 +00:00
|
|
|
For PostgreSQL and MySQL providers you need to create the configured database,
|
2020-10-05 17:42:33 +00:00
|
|
|
this command will create/update the required tables as needed.
|
2020-02-08 13:44:25 +00:00
|
|
|
|
2020-10-05 17:42:33 +00:00
|
|
|
To initialize/update the data provider from the configuration directory simply use:
|
2020-02-08 13:44:25 +00:00
|
|
|
|
2020-07-09 16:58:22 +00:00
|
|
|
$ sftpgo initprovider
|
2020-02-08 13:44:25 +00:00
|
|
|
|
2022-07-23 17:38:15 +00:00
|
|
|
Any defined action is ignored.
|
2020-02-08 13:44:25 +00:00
|
|
|
Please take a look at the usage below to customize the options.`,
|
2022-07-19 21:28:33 +00:00
|
|
|
Run: func(_ *cobra.Command, _ []string) {
|
2020-02-08 13:44:25 +00:00
|
|
|
logger.DisableLogger()
|
|
|
|
logger.EnableConsoleLogger(zerolog.DebugLevel)
|
2021-07-11 13:26:51 +00:00
|
|
|
configDir = util.CleanDirInput(configDir)
|
2020-04-30 12:23:55 +00:00
|
|
|
err := config.LoadConfig(configDir, configFile)
|
|
|
|
if err != nil {
|
2022-02-28 16:05:18 +00:00
|
|
|
logger.ErrorToConsole("Unable to initialize data provider, config load error: %v", err)
|
2020-04-30 14:48:42 +00:00
|
|
|
return
|
2020-04-30 12:23:55 +00:00
|
|
|
}
|
2020-12-06 09:36:48 +00:00
|
|
|
kmsConfig := config.GetKMSConfig()
|
|
|
|
err = kmsConfig.Initialize()
|
|
|
|
if err != nil {
|
2022-02-28 16:05:18 +00:00
|
|
|
logger.ErrorToConsole("Unable to initialize KMS: %v", err)
|
2020-12-06 09:36:48 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2022-11-16 18:04:50 +00:00
|
|
|
mfaConfig := config.GetMFAConfig()
|
|
|
|
err = mfaConfig.Initialize()
|
|
|
|
if err != nil {
|
|
|
|
logger.ErrorToConsole("Unable to initialize MFA: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-02-08 13:44:25 +00:00
|
|
|
providerConf := config.GetProviderConf()
|
2022-07-23 17:38:15 +00:00
|
|
|
// ignore actions
|
|
|
|
providerConf.Actions.Hook = ""
|
|
|
|
providerConf.Actions.ExecuteFor = nil
|
|
|
|
providerConf.Actions.ExecuteOn = nil
|
2020-10-05 17:42:33 +00:00
|
|
|
logger.InfoToConsole("Initializing provider: %#v config file: %#v", providerConf.Driver, viper.ConfigFileUsed())
|
2020-04-30 12:23:55 +00:00
|
|
|
err = dataprovider.InitializeDatabase(providerConf, configDir)
|
2020-02-08 13:44:25 +00:00
|
|
|
if err == nil {
|
2020-10-05 17:42:33 +00:00
|
|
|
logger.InfoToConsole("Data provider successfully initialized/updated")
|
2020-08-30 11:50:43 +00:00
|
|
|
} else if err == dataprovider.ErrNoInitRequired {
|
2020-10-05 17:42:33 +00:00
|
|
|
logger.InfoToConsole("%v", err.Error())
|
2020-02-08 13:44:25 +00:00
|
|
|
} else {
|
2022-02-28 16:05:18 +00:00
|
|
|
logger.ErrorToConsole("Unable to initialize/update the data provider: %v", err)
|
2020-06-20 12:30:46 +00:00
|
|
|
os.Exit(1)
|
2020-02-08 13:44:25 +00:00
|
|
|
}
|
2022-02-28 16:05:18 +00:00
|
|
|
if providerConf.Driver != dataprovider.MemoryDataProviderName && loadDataFrom != "" {
|
|
|
|
service := service.Service{
|
|
|
|
LoadDataFrom: loadDataFrom,
|
|
|
|
LoadDataMode: loadDataMode,
|
|
|
|
LoadDataQuotaScan: loadDataQuotaScan,
|
|
|
|
LoadDataClean: loadDataClean,
|
|
|
|
}
|
|
|
|
if err = service.LoadInitialData(); err != nil {
|
|
|
|
logger.ErrorToConsole("Cannot load initial data: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
2020-02-08 13:44:25 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(initProviderCmd)
|
|
|
|
addConfigFlags(initProviderCmd)
|
2022-02-28 16:05:18 +00:00
|
|
|
addBaseLoadDataFlags(initProviderCmd)
|
2020-02-08 13:44:25 +00:00
|
|
|
}
|