harbor/src/core/main.go

166 lines
4.9 KiB
Go
Raw Normal View History

// Copyright 2018 Project Harbor Authors
2017-04-13 12:54:58 +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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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.
2016-04-11 10:43:13 +02:00
package main
import (
"encoding/gob"
2016-04-11 10:43:13 +02:00
"fmt"
"os"
"os/signal"
"strconv"
"syscall"
2016-04-11 10:43:13 +02:00
2016-09-13 11:41:32 +02:00
"github.com/astaxie/beego"
_ "github.com/astaxie/beego/session/redis"
2016-04-11 10:43:13 +02:00
"github.com/goharbor/harbor/src/common/dao"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/common/utils"
"github.com/goharbor/harbor/src/common/utils/log"
"github.com/goharbor/harbor/src/core/api"
_ "github.com/goharbor/harbor/src/core/auth/authproxy"
_ "github.com/goharbor/harbor/src/core/auth/db"
_ "github.com/goharbor/harbor/src/core/auth/ldap"
_ "github.com/goharbor/harbor/src/core/auth/uaa"
"github.com/goharbor/harbor/src/core/config"
"github.com/goharbor/harbor/src/core/filter"
"github.com/goharbor/harbor/src/core/proxy"
"github.com/goharbor/harbor/src/core/service/token"
"github.com/goharbor/harbor/src/replication/ng"
2016-04-11 10:43:13 +02:00
)
const (
adminUserID = 1
)
func updateInitPassword(userID int, password string) error {
queryUser := models.User{UserID: userID}
user, err := dao.GetUser(queryUser)
if err != nil {
return fmt.Errorf("Failed to get user, userID: %d %v", userID, err)
}
if user == nil {
2016-11-14 07:46:20 +01:00
return fmt.Errorf("user id: %d does not exist", userID)
2016-04-11 10:43:13 +02:00
}
if user.Salt == "" {
2016-09-13 11:41:32 +02:00
salt := utils.GenerateRandomString()
2016-04-11 10:43:13 +02:00
user.Salt = salt
user.Password = password
err = dao.ChangeUserPassword(*user)
if err != nil {
return fmt.Errorf("Failed to update user encrypted password, userID: %d, err: %v", userID, err)
}
log.Infof("User id: %d updated its encypted password successfully.", userID)
} else {
log.Infof("User id: %d already has its encrypted password.", userID)
}
return nil
}
func gracefulShutdown(closing chan struct{}) {
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
log.Infof("capture system signal %s, to close \"closing\" channel", <-signals)
close(closing)
}
2016-04-11 10:43:13 +02:00
func main() {
beego.BConfig.WebConfig.Session.SessionOn = true
beego.BConfig.WebConfig.Session.SessionName = "sid"
// TODO
2016-07-27 14:12:53 +02:00
redisURL := os.Getenv("_REDIS_URL")
if len(redisURL) > 0 {
gob.Register(models.User{})
2016-07-27 14:12:53 +02:00
beego.BConfig.WebConfig.Session.SessionProvider = "redis"
beego.BConfig.WebConfig.Session.SessionProviderConfig = redisURL
}
beego.AddTemplateExt("htm")
2016-09-13 11:41:32 +02:00
2016-12-30 11:04:01 +01:00
log.Info("initializing configurations...")
if err := config.Init(); err != nil {
log.Fatalf("failed to initialize configurations: %v", err)
}
log.Info("configurations initialization completed")
2017-02-26 12:53:13 +01:00
token.InitCreators()
2016-12-30 11:04:01 +01:00
database, err := config.Database()
if err != nil {
log.Fatalf("failed to get database configuration: %v", err)
}
if err := dao.InitAndUpgradeDatabase(database); err != nil {
2016-12-30 11:04:01 +01:00
log.Fatalf("failed to initialize database: %v", err)
}
if err := config.Load(); err != nil {
log.Fatalf("failed to load config: %v", err)
}
2016-12-30 11:04:01 +01:00
password, err := config.InitialAdminPassword()
if err != nil {
log.Fatalf("failed to get admin's initia password: %v", err)
}
if err := updateInitPassword(adminUserID, password); err != nil {
2016-04-11 10:43:13 +02:00
log.Error(err)
}
2017-04-26 09:28:13 +02:00
// Init API handler
if err := api.Init(); err != nil {
log.Fatalf("Failed to initialize API handlers with error: %s", err.Error())
}
if config.WithClair() {
clairDB, err := config.ClairDB()
if err != nil {
log.Fatalf("failed to load clair database information: %v", err)
2017-07-06 18:38:38 +02:00
}
if err := dao.InitClairDB(clairDB); err != nil {
log.Fatalf("failed to initialize clair database: %v", err)
}
2017-07-06 18:38:38 +02:00
}
closing := make(chan struct{})
go gracefulShutdown(closing)
if err := ng.Init(closing); err != nil {
log.Fatalf("failed to init for replication: %v", err)
}
2017-06-18 07:51:42 +02:00
filter.Init()
2017-04-26 09:28:13 +02:00
beego.InsertFilter("/*", beego.BeforeRouter, filter.SecurityFilter)
beego.InsertFilter("/*", beego.BeforeRouter, filter.ReadonlyFilter)
beego.InsertFilter("/api/*", beego.BeforeRouter, filter.MediaTypeFilter("application/json", "multipart/form-data", "application/octet-stream"))
2017-04-26 09:28:13 +02:00
2016-04-11 10:43:13 +02:00
initRouters()
syncRegistry := os.Getenv("SYNC_REGISTRY")
sync, err := strconv.ParseBool(syncRegistry)
if err != nil {
log.Errorf("Failed to parse SYNC_REGISTRY: %v", err)
// if err set it default to false
sync = false
}
if sync {
if err := api.SyncRegistry(config.GlobalProjectMgr); err != nil {
log.Error(err)
}
} else {
log.Infof("Because SYNC_REGISTRY set false , no need to sync registry \n")
}
2017-05-02 13:14:47 +02:00
log.Info("Init proxy")
proxy.Init()
// go proxy.StartProxy()
2016-04-11 10:43:13 +02:00
beego.Run()
}