Merge pull request #2597 from ywk253100/170621_jobservice

Call UI's API to get project information in Jobservice
This commit is contained in:
Wenkai Yin 2017-06-23 10:23:26 +08:00 committed by GitHub
commit 93924c092d
4 changed files with 108 additions and 21 deletions

View File

@ -1809,6 +1809,18 @@ definitions:
type: integer
format: int
description: The public status of the project.
enable_content_trust:
type: boolean
description: Whether content trust is enabled or not. If it is enabled, user cann't pull unsigned images from this project.
prevent_vulnerable_images_from_running:
type: boolean
description: Whether prevent the vulnerable images from running.
prevent_vulnerable_images_from_running_severity:
type: string
description: If the vulnerability is high than severity defined here, the images cann't be pulled.
automatically_scan_images_on_push:
type: boolean
description: Whether scan images automatically when pushing.
Project:
type: object
properties:
@ -1849,6 +1861,18 @@ definitions:
repo_count:
type: integer
description: The number of the repositories under this project.
enable_content_trust:
type: boolean
description: Whether content trust is enabled or not. If it is enabled, user cann't pull unsigned images from this project.
prevent_vulnerable_images_from_running:
type: boolean
description: Whether prevent the vulnerable images from running.
prevent_vulnerable_images_from_running_severity:
type: string
description: If the vulnerability is high than severity defined here, the images cann't be pulled.
automatically_scan_images_on_push:
type: boolean
description: Whether scan images automatically when pushing.
Manifest:
type: object
properties:

View File

@ -27,7 +27,6 @@ import (
"github.com/docker/distribution"
"github.com/docker/distribution/manifest/schema1"
"github.com/docker/distribution/manifest/schema2"
"github.com/vmware/harbor/src/common/dao"
"github.com/vmware/harbor/src/common/models"
comutils "github.com/vmware/harbor/src/common/utils"
"github.com/vmware/harbor/src/common/utils/log"
@ -182,13 +181,13 @@ func (c *Checker) Enter() (string, error) {
}
func (c *Checker) enter() (string, error) {
project, err := dao.GetProjectByName(c.project)
project, err := getProject(c.project)
if err != nil {
c.logger.Errorf("an error occurred while getting project %s in DB: %v", c.project, err)
c.logger.Errorf("failed to get project %s from %s: %v", c.project, c.srcURL, err)
return "", err
}
err = c.createProject(project.Public)
err = c.createProject(project)
if err == nil {
c.logger.Infof("project %s is created on %s with user %s", c.project, c.dstURL, c.dstUsr)
return StatePullManifest, nil
@ -207,16 +206,68 @@ func (c *Checker) enter() (string, error) {
return "", err
}
func (c *Checker) createProject(public int) error {
project := struct {
ProjectName string `json:"project_name"`
Public int `json:"public"`
}{
ProjectName: c.project,
Public: public,
func getProject(name string) (*models.Project, error) {
req, err := http.NewRequest(http.MethodGet, buildProjectURL(), nil)
if err != nil {
return nil, err
}
data, err := json.Marshal(project)
req.URL.Query().Set("name", name)
req.URL.Query().Encode()
req.AddCookie(&http.Cookie{
Name: models.UISecretCookie,
Value: config.JobserviceSecret(),
})
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
list := []*models.Project{}
if err = json.Unmarshal(data, &list); err != nil {
return nil, err
}
var project *models.Project
for _, p := range list {
if p.Name == name {
project = p
break
}
}
if project == nil {
return nil, fmt.Errorf("project %s not found", name)
}
return project, nil
}
func (c *Checker) createProject(project *models.Project) error {
pro := struct {
Name string `json:"project_name"`
Public int `json:"public"`
EnableContentTrust bool `json:"enable_content_trust"`
PreventVulnerableImagesFromRunning bool `json:"prevent_vulnerable_images_from_running"`
PreventVulnerableImagesFromRunningSeverity string `json:"prevent_vulnerable_images_from_running_severity"`
AutomaticallyScanImagesOnPush bool `json:"automatically_scan_images_on_push"`
}{
Name: project.Name,
Public: project.Public,
EnableContentTrust: project.EnableContentTrust,
PreventVulnerableImagesFromRunning: project.PreventVulnerableImagesFromRunning,
PreventVulnerableImagesFromRunningSeverity: project.PreventVulnerableImagesFromRunningSeverity,
AutomaticallyScanImagesOnPush: project.AutomaticallyScanImagesOnPush,
}
data, err := json.Marshal(pro)
if err != nil {
return err
}
@ -263,6 +314,10 @@ func (c *Checker) createProject(public int) error {
c.project, c.dstURL, c.dstUsr, resp.StatusCode, string(message))
}
func buildProjectURL() string {
return strings.TrimRight(config.LocalUIURL(), "/") + "/api/projects/"
}
// ManifestPuller pulls the manifest of a tag. And if no tag needs to be pulled,
// the next state that state machine should enter is "finished".
type ManifestPuller struct {

View File

@ -39,6 +39,10 @@ type ProjectAPI struct {
type projectReq struct {
ProjectName string `json:"project_name"`
Public int `json:"public"`
EnableContentTrust bool `json:"enable_content_trust"`
PreventVulnerableImagesFromRunning bool `json:"prevent_vulnerable_images_from_running"`
PreventVulnerableImagesFromRunningSeverity string `json:"prevent_vulnerable_images_from_running_severity"`
AutomaticallyScanImagesOnPush bool `json:"automatically_scan_images_on_push"`
}
const projectNameMaxLen int = 30
@ -119,6 +123,10 @@ func (p *ProjectAPI) Post() {
Name: pro.ProjectName,
Public: pro.Public,
OwnerName: p.SecurityCtx.GetUsername(),
EnableContentTrust: pro.EnableContentTrust,
PreventVulnerableImagesFromRunning: pro.PreventVulnerableImagesFromRunning,
PreventVulnerableImagesFromRunningSeverity: pro.PreventVulnerableImagesFromRunningSeverity,
AutomaticallyScanImagesOnPush: pro.AutomaticallyScanImagesOnPush,
})
if err != nil {
log.Errorf("Failed to add project, error: %v", err)

View File

@ -40,10 +40,10 @@ func initRouters() {
beego.Router("/harbor/sign-up", &controllers.IndexController{})
beego.Router("/harbor/dashboard", &controllers.IndexController{})
beego.Router("/harbor/projects", &controllers.IndexController{})
beego.Router("/harbor/projects/:id/repository", &controllers.IndexController{})
beego.Router("/harbor/projects/:id/replication", &controllers.IndexController{})
beego.Router("/harbor/projects/:id/member", &controllers.IndexController{})
beego.Router("/harbor/projects/:id/log", &controllers.IndexController{})
beego.Router("/harbor/projects/:id/repositories", &controllers.IndexController{})
beego.Router("/harbor/projects/:id/replications", &controllers.IndexController{})
beego.Router("/harbor/projects/:id/members", &controllers.IndexController{})
beego.Router("/harbor/projects/:id/logs", &controllers.IndexController{})
beego.Router("/harbor/tags/:id/*", &controllers.IndexController{})
beego.Router("/harbor/users", &controllers.IndexController{})