Merge pull request #2378 from ywk253100/170524_sync

Refactor sync registry API
This commit is contained in:
Yan 2017-06-04 20:32:14 -07:00 committed by GitHub
commit 1171e4c4d6
6 changed files with 29 additions and 33 deletions

View File

@ -128,7 +128,7 @@ func init() {
_ = updateInitPassword(1, "Harbor12345")
//syncRegistry
if err := SyncRegistry(); err != nil {
if err := SyncRegistry(config.GlobalProjectMgr); err != nil {
log.Fatalf("failed to sync repositories from registry: %v", err)
}

View File

@ -16,35 +16,29 @@ package api
import (
"net/http"
"github.com/vmware/harbor/src/common/dao"
"github.com/vmware/harbor/src/common/utils/log"
"github.com/vmware/harbor/src/common/api"
)
// InternalAPI handles request of harbor admin...
type InternalAPI struct {
api.BaseAPI
BaseController
}
// Prepare validates the URL and parms
func (ia *InternalAPI) Prepare() {
var currentUserID int
currentUserID = ia.ValidateUser()
isAdmin, err := dao.IsAdminRole(currentUserID)
if err != nil {
log.Errorf("Error occurred in IsAdminRole:%v", err)
ia.CustomAbort(http.StatusInternalServerError, "Internal error.")
ia.BaseController.Prepare()
if !ia.SecurityCtx.IsAuthenticated() {
ia.HandleUnauthorized()
return
}
if !isAdmin {
log.Error("Guests doesn't have the permisson to request harbor internal API.")
ia.CustomAbort(http.StatusForbidden, "Guests doesn't have the permisson to request harbor internal API.")
if !ia.SecurityCtx.IsSysAdmin() {
ia.HandleForbidden(ia.SecurityCtx.GetUsername())
return
}
}
// SyncRegistry ...
func (ia *InternalAPI) SyncRegistry() {
err := SyncRegistry()
err := SyncRegistry(ia.ProjectMgr)
if err != nil {
ia.CustomAbort(http.StatusInternalServerError, "internal error")
}

View File

@ -31,6 +31,7 @@ import (
"github.com/vmware/harbor/src/common/utils/registry/auth"
registry_error "github.com/vmware/harbor/src/common/utils/registry/error"
"github.com/vmware/harbor/src/ui/config"
"github.com/vmware/harbor/src/ui/projectmanager"
)
//sysadmin has all privileges to all projects
@ -212,7 +213,7 @@ func addAuthentication(req *http.Request) {
}
// SyncRegistry syncs the repositories of registry with database.
func SyncRegistry() error {
func SyncRegistry(pm projectmanager.ProjectManager) error {
log.Infof("Start syncing repositories from registry to DB... ")
@ -236,7 +237,7 @@ func SyncRegistry() error {
var reposToAdd []string
var reposToDel []string
reposToAdd, reposToDel, err = diffRepos(reposInRegistry, reposInDB)
reposToAdd, reposToDel, err = diffRepos(reposInRegistry, reposInDB, pm)
if err != nil {
return err
}
@ -249,7 +250,7 @@ func SyncRegistry() error {
if err != nil {
log.Errorf("Error happens when counting pull count from access log: %v", err)
}
pro, err := dao.GetProjectByName(project)
pro, err := pm.Get(project)
if err != nil {
log.Errorf("failed to get project %s: %v", project, err)
continue
@ -299,7 +300,8 @@ func catalog() ([]string, error) {
return repositories, nil
}
func diffRepos(reposInRegistry []string, reposInDB []string) ([]string, []string, error) {
func diffRepos(reposInRegistry []string, reposInDB []string,
pm projectmanager.ProjectManager) ([]string, []string, error) {
var needsAdd []string
var needsDel []string
@ -314,7 +316,7 @@ func diffRepos(reposInRegistry []string, reposInDB []string) ([]string, []string
d := strings.Compare(repoInR, repoInD)
if d < 0 {
i++
exist, err := projectExists(repoInR)
exist, err := projectExists(pm, repoInR)
if err != nil {
log.Errorf("failed to check the existence of project %s: %v", repoInR, err)
continue
@ -377,7 +379,7 @@ func diffRepos(reposInRegistry []string, reposInDB []string) ([]string, []string
for i < len(reposInRegistry) {
repoInR = reposInRegistry[i]
i++
exist, err := projectExists(repoInR)
exist, err := projectExists(pm, repoInR)
if err != nil {
log.Errorf("failed to check whether project of %s exists: %v", repoInR, err)
continue
@ -397,9 +399,9 @@ func diffRepos(reposInRegistry []string, reposInDB []string) ([]string, []string
return needsAdd, needsDel, nil
}
func projectExists(repository string) (bool, error) {
func projectExists(pm projectmanager.ProjectManager, repository string) (bool, error) {
project, _ := utils.ParseRepository(repository)
return dao.ProjectExists(project)
return pm.Exist(project)
}
// TODO need a registry client which accept a raw token as param

View File

@ -40,9 +40,8 @@ var (
SecretStore *secret.Store
// AdminserverClient is a client for adminserver
AdminserverClient client.Client
// DBProjectManager is the project manager based on database,
// it is initialized only the deploy mode is standalone
DBProjectManager projectmanager.ProjectManager
// GlobalProjectMgr is initialized based on the deploy mode
GlobalProjectMgr projectmanager.ProjectManager
mg *comcfg.Manager
keyProvider comcfg.KeyProvider
)
@ -73,8 +72,8 @@ func Init() error {
// init secret store
initSecretStore()
// init project manager based on database
initDBProjectManager()
// init project manager based on deploy mode
initProjectManager()
return nil
}
@ -95,12 +94,13 @@ func initSecretStore() {
SecretStore = secret.NewStore(m)
}
func initDBProjectManager() {
func initProjectManager() {
if len(DeployMode()) == 0 ||
DeployMode() == common.DeployModeStandAlone {
log.Info("initializing the project manager based on database...")
DBProjectManager = &db.ProjectManager{}
GlobalProjectMgr = &db.ProjectManager{}
}
// TODO create project manager based on pms
}
// Load configurations

View File

@ -136,7 +136,7 @@ func getProjectManager(ctx *beegoctx.Context) projectmanager.ProjectManager {
if len(config.DeployMode()) == 0 ||
config.DeployMode() == common.DeployModeStandAlone {
log.Info("filling a project manager based on database...")
return config.DBProjectManager
return config.GlobalProjectMgr
}
// TODO create project manager based on pms

View File

@ -101,7 +101,7 @@ func main() {
beego.InsertFilter("/*", beego.BeforeRouter, filter.SecurityFilter)
initRouters()
if err := api.SyncRegistry(); err != nil {
if err := api.SyncRegistry(config.GlobalProjectMgr); err != nil {
log.Error(err)
}
log.Info("Init proxy")