mirror of
https://github.com/goharbor/harbor.git
synced 2024-12-23 17:17:46 +01:00
Revise code with review comments
Signed-off-by: stonezdj <stonezdj@gmail.com>
This commit is contained in:
parent
880051c08a
commit
7a5fbf718f
@ -90,6 +90,20 @@ func InitDatabase(database *models.Database) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// InitAndUpgradeDatabase - init database and upgrade when required
|
||||||
|
func InitAndUpgradeDatabase(database *models.Database) error {
|
||||||
|
if err := InitDatabase(database); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := UpgradeSchema(database); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := CheckSchemaVersion(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// CheckSchemaVersion checks that whether the schema version matches with the expected one
|
// CheckSchemaVersion checks that whether the schema version matches with the expected one
|
||||||
func CheckSchemaVersion() error {
|
func CheckSchemaVersion() error {
|
||||||
version, err := GetSchemaVersion()
|
version, err := GetSchemaVersion()
|
||||||
|
@ -64,17 +64,9 @@ func InitDatabaseFromEnv() {
|
|||||||
|
|
||||||
log.Infof("POSTGRES_HOST: %s, POSTGRES_USR: %s, POSTGRES_PORT: %d, POSTGRES_PWD: %s\n", dbHost, dbUser, dbPort, dbPassword)
|
log.Infof("POSTGRES_HOST: %s, POSTGRES_USR: %s, POSTGRES_PORT: %d, POSTGRES_PWD: %s\n", dbHost, dbUser, dbPort, dbPassword)
|
||||||
|
|
||||||
if err := dao.InitDatabase(database); err != nil {
|
if err := dao.InitAndUpgradeDatabase(database); err != nil {
|
||||||
log.Fatalf("failed to initialize database: %v", err)
|
log.Fatalf("failed to init and upgrade database : %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := dao.UpgradeSchema(database); err != nil {
|
|
||||||
log.Fatalf("failed to upgrade database schema: %v", err)
|
|
||||||
}
|
|
||||||
if err := dao.CheckSchemaVersion(); err != nil {
|
|
||||||
log.Fatalf("failed to check database schema version: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := updateUserInitialPassword(1, adminPwd); err != nil {
|
if err := updateUserInitialPassword(1, adminPwd); err != nil {
|
||||||
log.Fatalf("failed to init password for admin: %v", err)
|
log.Fatalf("failed to init password for admin: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,9 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/goharbor/harbor/src/common"
|
"github.com/goharbor/harbor/src/common"
|
||||||
"github.com/goharbor/harbor/src/common/config"
|
"github.com/goharbor/harbor/src/common/config"
|
||||||
"github.com/goharbor/harbor/src/common/config/metadata"
|
"github.com/goharbor/harbor/src/common/config/metadata"
|
||||||
@ -25,8 +28,6 @@ import (
|
|||||||
"github.com/goharbor/harbor/src/common/utils/log"
|
"github.com/goharbor/harbor/src/common/utils/log"
|
||||||
corecfg "github.com/goharbor/harbor/src/core/config"
|
corecfg "github.com/goharbor/harbor/src/core/config"
|
||||||
"github.com/goharbor/harbor/src/core/filter"
|
"github.com/goharbor/harbor/src/core/filter"
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ConfigAPI ...
|
// ConfigAPI ...
|
||||||
@ -67,7 +68,6 @@ type value struct {
|
|||||||
// Get returns configurations
|
// Get returns configurations
|
||||||
func (c *ConfigAPI) Get() {
|
func (c *ConfigAPI) Get() {
|
||||||
configs := c.cfgManager.GetUserCfgs()
|
configs := c.cfgManager.GetUserCfgs()
|
||||||
log.Infof("current configs %+v", configs)
|
|
||||||
m, err := convertForGet(configs)
|
m, err := convertForGet(configs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("failed to convert configurations: %v", err)
|
log.Errorf("failed to convert configurations: %v", err)
|
||||||
@ -109,11 +109,6 @@ func (c *ConfigAPI) Put() {
|
|||||||
log.Errorf("failed to upload configurations: %v", err)
|
log.Errorf("failed to upload configurations: %v", err)
|
||||||
c.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
|
c.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Everything is ok, detect the configurations to confirm if the option we are caring is changed.
|
|
||||||
if err := watchConfigChanges(m); err != nil {
|
|
||||||
log.Errorf("Failed to watch configuration change with error: %s\n", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConfigAPI) validateCfg(cfgs map[string]interface{}) (bool, error) {
|
func (c *ConfigAPI) validateCfg(cfgs map[string]interface{}) (bool, error) {
|
||||||
|
@ -93,16 +93,9 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to get database configuration: %v", err)
|
log.Fatalf("failed to get database configuration: %v", err)
|
||||||
}
|
}
|
||||||
if err := dao.InitDatabase(database); err != nil {
|
if err := dao.InitAndUpgradeDatabase(database); err != nil {
|
||||||
log.Fatalf("failed to initialize database: %v", err)
|
log.Fatalf("failed to initialize database: %v", err)
|
||||||
}
|
}
|
||||||
if err := dao.UpgradeSchema(database); err != nil {
|
|
||||||
log.Fatalf("failed to upgrade schema: %v", err)
|
|
||||||
}
|
|
||||||
if err := dao.CheckSchemaVersion(); err != nil {
|
|
||||||
log.Fatalf("failed to check schema version: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := config.Load(); err != nil {
|
if err := config.Load(); err != nil {
|
||||||
log.Fatalf("failed to load config: %v", err)
|
log.Fatalf("failed to load config: %v", err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user